Completed
Branch master (f6660f)
by Stefan
02:22
created
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) {
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

23
    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...
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>");
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
34
    //~ $array = ['username' => 'stweil', 'quux' => 'baz'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
35
    //~ $newJSON = json_encode($array);
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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);
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
74
    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

74
    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...
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