-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuromillions.php
146 lines (134 loc) · 5.15 KB
/
euromillions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
$database = "./em.sqlite"; // database location
////////////////////////////////////////////////////////////
function sqlite($method, $cmd)
{
global $database, $values;
try {
if (!file_exists($database)) {
throw new PDOException("database not found");
}
$db = new PDO("sqlite:$database");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec("PRAGMA journal_mode = wal;");
switch ($method) {
case "query":
try {
$result = $db->query($cmd);
return $result->fetchALL(PDO::FETCH_ASSOC);
$db = null;
unset($db);
} catch (PDOException $e) {
echo "Error: " . $e->getMessage() . "<BR />Error Code: " . $e->getCode();
$db = null;
unset($db);
die();
}
break;
case "update":
try {
$update = $db->prepare($cmd);
$update->execute();
return $update->rowCount();
$db = null;
unset($db);
} catch (PDOException $e) {
echo "Error: " . $e->getMessage() . "<BR />Error Code: " . $e->getCode();
$db = null;
unset($db);
die();
}
break;
case "insert":
try {
$qry = $db->prepare($cmd);
$qry->execute($values);
return $db->lastInsertId();
$db = null;
unset($db);
} catch (PDOException $e) {
echo "Error: " . $e->getMessage() . "<BR />Error Code: " . $e->getCode();
$db = null;
unset($db);
die();
}
break;
default:
echo "Not Allowed";
exit();
}
} catch (PDOException $e) {
echo "Error: " .
$e->getMessage() .
"<BR />Error Code: " .
$e->getCode();
$db = null;
unset($db);
die();
}
}
function arrayToXML($array, SimpleXMLElement $xml, $child_name)
{
foreach ($array as $k => $v) {
if (is_array($v)) {
is_int($k)
? arrayToXML($v, $xml->addChild($child_name), $v)
: $this->arrayToXML(
$v,
$xml->addChild(strtolower($k)),
$child_name
);
} else {
is_int($k)
? $xml->addChild($child_name, $v)
: $xml->addChild(strtolower($k), $v);
}
}
return $xml->asXML();
}
$available_formats = ["txt", "xml", "json"];
$available_formats = array_fill_keys($available_formats, 1);
$f = $_REQUEST["format"];
if (!$available_formats[$f]) {
$f = "txt";
}
$q = trim($_REQUEST["result"]);
if (!preg_match('/^[a-z0-9 .\-]+$/i', $q)) {
$q = "";
}
if ($q == "") {
if ($_REQUEST["v"] == "0") {
$result = sqlite("query", "SELECT date, ball_1 || ' ' || ball_2 || ' ' || ball_3 || ' ' || ball_4 || ' ' || ball_5 AS balls, star_1 || ' ' || star_2 AS stars FROM euro_millions ORDER BY date DESC limit 1");
} else {
$result = sqlite("query", "SELECT date,ball_1,ball_2,ball_3,ball_4,ball_5,star_1,star_2 FROM euro_millions ORDER BY date DESC limit 1");
}
} elseif ($q == "all") {
if ($_REQUEST["v"] == "0") {
$result = sqlite("query", "SELECT date, ball_1 || ' ' || ball_2 || ' ' || ball_3 || ' ' || ball_4 || ' ' || ball_5 AS balls, star_1 || ' ' || star_2 AS stars FROM euro_millions ORDER BY date DESC");
} else {
$result = sqlite("query", "SELECT date,ball_1,ball_2,ball_3,ball_4,ball_5,star_1,star_2 FROM euro_millions ORDER BY date DESC");
}
} else {
if ($_REQUEST["v"] == "0") {
$result = sqlite("query", "SELECT date, ball_1 || ' ' || ball_2 || ' ' || ball_3 || ' ' || ball_4 || ' ' || ball_5 AS balls, star_1 || ' ' || star_2 AS stars FROM euro_millions WHERE date LIKE '%$q%' ORDER BY date DESC");
} else {
$result = sqlite("query", "SELECT date,ball_1,ball_2,ball_3,ball_4,ball_5,star_1,star_2 FROM euro_millions WHERE date LIKE '%$q%' ORDER BY date DESC");
}
}
if ($f == "xml") {
header("Content-Type: application/xml; charset=utf-8");
echo arrayToXML($result, new SimpleXMLElement("<euromillions/>"), "drawn");
} elseif ($f == "json") {
header("Content-Type: application/json; charset=utf-8");
echo json_encode(["drawns" => $result], JSON_PRETTY_PRINT, JSON_UNESCAPED_UNICODE);
} else {
header("Content-type: text/plain");
foreach ($result as $row) {
if ($_REQUEST["v"] == "0") {
echo $row["date"] . " = " . $row["balls"] . " + " . $row["stars"] . "\n";
} else {
echo $row["date"] . " = " . $row["ball_1"] . " - " . $row["ball_2"] . " - " . $row["ball_3"] . " - " . $row["ball_4"] . " - " . $row["ball_5"] . " + " . $row["star_1"] . " - " . $row["star_2"] . "\n";
}
}
}
?>