anonymous()
last analyzed

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 3
1
<?php
2
3
// Copyright (C) 2014-2023 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
require_once 'globals.php';
10
debug('db.php: begin');
11
12
require_once 'DBConnector.class.php';
13
$dbcon = palma\DBConnector::getInstance();
14
15
$remote = $_SERVER['REMOTE_ADDR'];
16
$isAllowed = false;
17
18
$newJSON = '{}';
19
$oldJSON = '';
20
21
if (!empty($_REQUEST['json'])) {
22
  $oldJSON = $_REQUEST['json'];
23
  $oldJSONarr = json_decode($oldJSON, true);
24
  if ($oldJSONarr != null) {
25
    array_walk_recursive($oldJSONarr, function (&$value, $key) {
0 ignored issues
show
Unused Code introduced by
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

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

77
  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...
78
    if (is_string($value) && preg_match('/^http/', $value)) {
79
      $value = rawurlencode($value);
80
    }
81
  });
82
  $newJSON = json_encode($database);
83
  if ($oldJSON != $newJSON) {
84
    debug("db.php: new $newJSON");
85
    break;
86
  }
87
88
  sleep(1);
89
}
90
91
touch("/var/run/palma/last_activity");
92
93
echo($newJSON);
94