/
functions.php
80 lines (70 loc) · 2.08 KB
/
functions.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
<?php
if (!file_exists('db.sqlite')) {
$db = new SQLite3("db.sqlite");
dbsetup();
}
else {
$db = new SQLite3("db.sqlite");
}
function dbsetup() {
global $db;
$db->exec("CREATE TABLE events "
. "(datetime INTEGER, device TEXT, source TEXT, summary TEXT, details TEXT)");
$db->exec("CREATE TABLE views "
. "(name TEXT, datefrom INTEGER, dateuntil DATETIME, filter TEXT)");
}
function getEvents() {
global $db;
$result = $db->query('SELECT rowid, datetime, device, source, summary, details FROM events '
. 'WHERE datetime > ' . strtotime("June 2015") . ' AND datetime < ' . strtotime("December 2015")
. ' ORDER BY datetime');
$results = [];
while ($row = $result->fetchArray()) {
$row["printableDatetime"] = date('j M H:i:s', $row['datetime']);
$results[] = $row;
}
return json_encode($results);
}
function getViews() {
global $db;
$result = $db->query('SELECT name, datefrom, dateuntil, filter FROM views');
$results = [];
while ($row = $result->fetchArray()) {
$results[] = $row;
}
return json_encode($results);
}
function getDevices() {
return ['home', 'work', 'phone'];
}
function insertEvent($unixtime, $device, $source, $summary) {
global $db;
$unixtime = intval($unixtime);
if (in_array($device, getDevices(), true) !== true) {
die("Excuse me, what device is '$device'?");
}
if (empty($summary)) {
die("Error: empty summary.");
}
$device = SQLite3::escapeString($device);
$source = SQLite3::escapeString($source);
$summary = SQLite3::escapeString($summary);
$details = SQLite3::escapeString($details);
$ok = $db->exec("INSERT INTO events VALUES($unixtime, '$device', '$source', '$summary','$details')");
if (!$ok) {
die("Error on $unixtime,$device,$source,$summary,$details");
}
return $ok;
}
function insertView($name, $datefrom, $dateuntil, $filter) {
global $db;
$name = SQLite3::escapeString($name);
$datefrom = intval($datefrom);
$dateuntil = intval($dateuntil);
$filter = SQLite3::escapeString($filter);
$ok = $db->exec("INSERT INTO views VALUES('$name', $datefrom, $dateuntil, '$filter')");
if (!$ok) {
die("Error on $name");
}
return $ok;
}