-
Notifications
You must be signed in to change notification settings - Fork 156
/
db.php
93 lines (76 loc) · 2.55 KB
/
db.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
<?php
// Copyright (C) 2014 Universitätsbibliothek Mannheim
// See file LICENSE for license details.
// Poll up to 300 s for changes in the database and return its data in
// JSON format. The previous data is passed via URL request (?json=DATA).
// TODO: Use db triggers instead of time based polling.
require_once('DBConnector.class.php');
$dbcon = new palma\DBConnector();
$remote = $_SERVER['REMOTE_ADDR'];
$isAllowed = false;
$newJSON = '{}';
$oldJSON = '';
require_once('globals.php');
if (!empty($_REQUEST['json'])) {
$oldJSON = $_REQUEST['json'];
$oldJSONarr = json_decode($oldJSON, true);
if ($oldJSONarr != null) {
array_walk_recursive($oldJSONarr, function (&$value, $key) {
if (is_string($value) && preg_match('/^http/', $value)) {
$value = rawurlencode($value);
}
});
$oldJSON = json_encode($oldJSONarr);
}
trace("db old: $oldJSON");
}
for ($t = 0; $t < 300; $t++) {
//~ echo("waiting for db change...<br>");
//~ $array = ['username' => 'stweil', 'quux' => 'baz'];
//~ $newJSON = json_encode($array);
$database = array();
$table = $dbcon->query('select * from setting');
$data = array();
while ($row = $table->fetchArray(SQLITE3_ASSOC)) {
array_push($data, $row);
}
$database['setting'] = $data;
$table = $dbcon->query('select * from address');
$data = array();
while ($row = $table->fetchArray(SQLITE3_ASSOC)) {
array_push($data, $row);
$isAllowed = $isAllowed || ($row['address'] == $remote);
}
if (!$isAllowed) {
// Some unauthorized host tried to read the database.
// Don't return any data.
break;
}
$database['address'] = $data;
$table = $dbcon->query('select * from user');
$data = array();
while ($row = $table->fetchArray(SQLITE3_ASSOC)) {
array_push($data, $row);
}
$database['user'] = $data;
$table = $dbcon->query('select * from window');
$data = array();
while ($row = $table->fetchArray(SQLITE3_ASSOC)) {
array_push($data, $row);
}
$database['window'] = $data;
//~ $newJSON = json_encode($database, JSON_PRETTY_PRINT);
array_walk_recursive($database, function (&$value, $key) {
if (is_string($value) && preg_match('/^http/', $value)) {
$value = rawurlencode($value);
}
});
$newJSON = json_encode($database);
if ($oldJSON != $newJSON) {
trace("db new: $newJSON");
break;
}
sleep(1);
}
touch("/var/run/palma/last_activity");
echo($newJSON);