1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
// Copyright (C) 2014 Universitätsbibliothek Mannheim |
4
|
|
|
// See file LICENSE for license details. |
5
|
|
|
|
6
|
|
|
// Poll up to 300 s for changes in the database and return its data in |
7
|
|
|
// JSON format. The previous data is passed via URL request (?json=DATA). |
8
|
|
|
|
9
|
|
|
// TODO: Use db triggers instead of time based polling. |
10
|
|
|
|
11
|
|
|
require_once('DBConnector.class.php'); |
12
|
|
|
$dbcon = new DBConnector(); |
13
|
|
|
|
14
|
|
|
$remote = $_SERVER['REMOTE_ADDR']; |
15
|
|
|
$isAllowed = false; |
16
|
|
|
|
17
|
|
|
$newJSON = '{}'; |
18
|
|
|
$oldJSON = ''; |
19
|
|
|
|
20
|
|
|
if (!empty($_REQUEST['json'])) { |
21
|
|
|
$oldJSON = $_REQUEST['json']; |
22
|
|
|
$oldJSONarr = json_decode($oldJSON, true); |
23
|
|
|
array_walk_recursive($oldJSONarr, function(&$value, $key) { |
|
|
|
|
24
|
|
|
if (is_string($value) && preg_match('/^http/', $value)) { |
25
|
|
|
$value = rawurlencode($value); |
26
|
|
|
} |
27
|
|
|
}); |
28
|
|
|
$oldJSON = json_encode($oldJSONarr); |
29
|
|
|
trace("db old: $oldJSON"); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
for ($t = 0; $t < 300; $t++) { |
33
|
|
|
//~ echo("waiting for db change...<br>"); |
|
|
|
|
34
|
|
|
//~ $array = ['username' => 'stweil', 'quux' => 'baz']; |
|
|
|
|
35
|
|
|
//~ $newJSON = json_encode($array); |
|
|
|
|
36
|
|
|
|
37
|
|
|
$database = array(); |
38
|
|
|
|
39
|
|
|
$table = $dbcon->query('select * from setting'); |
40
|
|
|
$data = array(); |
41
|
|
|
while ($row = $table->fetchArray(SQLITE3_ASSOC)) { |
42
|
|
|
array_push($data, $row); |
43
|
|
|
} |
44
|
|
|
$database['setting'] = $data; |
45
|
|
|
|
46
|
|
|
$table = $dbcon->query('select * from address'); |
47
|
|
|
$data = array(); |
48
|
|
|
while ($row = $table->fetchArray(SQLITE3_ASSOC)) { |
49
|
|
|
array_push($data, $row); |
50
|
|
|
$isAllowed = $isAllowed || ($row['address'] == $remote); |
51
|
|
|
} |
52
|
|
|
if (!$isAllowed) { |
53
|
|
|
// Some unauthorized host tried to read the database. |
54
|
|
|
// Don't return any data. |
55
|
|
|
break; |
56
|
|
|
} |
57
|
|
|
$database['address'] = $data; |
58
|
|
|
|
59
|
|
|
$table = $dbcon->query('select * from user'); |
60
|
|
|
$data = array(); |
61
|
|
|
while ($row = $table->fetchArray(SQLITE3_ASSOC)) { |
62
|
|
|
array_push($data, $row); |
63
|
|
|
} |
64
|
|
|
$database['user'] = $data; |
65
|
|
|
|
66
|
|
|
$table = $dbcon->query('select * from window'); |
67
|
|
|
$data = array(); |
68
|
|
|
while ($row = $table->fetchArray(SQLITE3_ASSOC)) { |
69
|
|
|
array_push($data, $row); |
70
|
|
|
} |
71
|
|
|
$database['window'] = $data; |
72
|
|
|
|
73
|
|
|
//~ $newJSON = json_encode($database, JSON_PRETTY_PRINT); |
|
|
|
|
74
|
|
|
array_walk_recursive($database, function(&$value, $key) { |
|
|
|
|
75
|
|
|
if (is_string($value) && preg_match('/^http/', $value)) { |
76
|
|
|
$value = rawurlencode($value); |
77
|
|
|
} |
78
|
|
|
}); |
79
|
|
|
$newJSON = json_encode($database); |
80
|
|
|
if ($oldJSON != $newJSON) { |
81
|
|
|
trace("db new: $newJSON"); |
82
|
|
|
break; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
sleep(1); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
touch("last_activity"); |
89
|
|
|
|
90
|
|
|
echo($newJSON); |
91
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.