Passed
Push — master ( d6c0c0...b57efa )
by Stefan
03:48
created
Severity
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 palma\DBConnector();
13
14
$remote = $_SERVER['REMOTE_ADDR'];
15
$isAllowed = false;
16
17
$newJSON = '{}';
18
$oldJSON = '';
19
20
require_once('globals.php');
21
if (!empty($_REQUEST['json'])) {
22
    $oldJSON = $_REQUEST['json'];
23
    $oldJSONarr = json_decode($oldJSON, true);
24
    array_walk_recursive($oldJSONarr, function (&$value, $key) {
0 ignored issues
show
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

24
    array_walk_recursive($oldJSONarr, function (&$value, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25
        if (is_string($value) && preg_match('/^http/', $value)) {
26
            $value = rawurlencode($value);
27
        }
28
    });
29
    $oldJSON = json_encode($oldJSONarr);
30
    trace("db old: $oldJSON");
31
}
32
33
for ($t = 0; $t < 300; $t++) {
34
    //~ echo("waiting for db change...<br>");
35
    //~ $array = ['username' => 'stweil', 'quux' => 'baz'];
36
    //~ $newJSON = json_encode($array);
37
38
    $database = array();
39
40
    $table = $dbcon->query('select * from setting');
41
    $data = array();
42
    while ($row = $table->fetchArray(SQLITE3_ASSOC)) {
43
        array_push($data, $row);
44
    }
45
    $database['setting'] = $data;
46
47
    $table = $dbcon->query('select * from address');
48
    $data = array();
49
    while ($row = $table->fetchArray(SQLITE3_ASSOC)) {
50
        array_push($data, $row);
51
        $isAllowed = $isAllowed || ($row['address'] == $remote);
52
    }
53
    if (!$isAllowed) {
54
        // Some unauthorized host tried to read the database.
55
        // Don't return any data.
56
        break;
57
    }
58
    $database['address'] = $data;
59
60
    $table = $dbcon->query('select * from user');
61
    $data = array();
62
    while ($row = $table->fetchArray(SQLITE3_ASSOC)) {
63
        array_push($data, $row);
64
    }
65
    $database['user'] = $data;
66
67
    $table = $dbcon->query('select * from window');
68
    $data = array();
69
    while ($row = $table->fetchArray(SQLITE3_ASSOC)) {
70
        array_push($data, $row);
71
    }
72
    $database['window'] = $data;
73
74
    //~ $newJSON = json_encode($database, JSON_PRETTY_PRINT);
75
    array_walk_recursive($database, function (&$value, $key) {
0 ignored issues
show
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

75
    array_walk_recursive($database, function (&$value, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
        if (is_string($value) && preg_match('/^http/', $value)) {
77
            $value = rawurlencode($value);
78
        }
79
    });
80
    $newJSON = json_encode($database);
81
    if ($oldJSON != $newJSON) {
82
        trace("db new: $newJSON");
83
        break;
84
    }
85
86
    sleep(1);
87
}
88
89
touch("/var/run/palma/last_activity");
90
91
echo($newJSON);
92