commit | author | age
|
181529
|
1 |
<?php |
b1a6a5
|
2 |
require_once '../../lib/config.inc.php'; |
181529
|
3 |
$conf['start_session'] = false; |
b1a6a5
|
4 |
require_once '../../lib/app.inc.php'; |
181529
|
5 |
|
L |
6 |
if($conf['demo_mode'] == true) $app->error('This function is disabled in demo mode.'); |
|
7 |
|
|
8 |
header('Content-Type: application/json; charset=utf-8'); |
|
9 |
header('Access-Control-Allow-Origin: *'); |
|
10 |
header('Access-Control-Allow-Headers: X-Requested-With'); |
|
11 |
header('Cache-Control: no-cache, must-revalidate'); // HTTP/1.1 |
|
12 |
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past |
|
13 |
|
|
14 |
$type = addslashes($_GET['type']); |
|
15 |
$token = addslashes($_GET['token']); |
65ea2e
|
16 |
$server_id = $app->functions->intval($_GET['server']); |
181529
|
17 |
|
L |
18 |
$app->uses('getconf'); |
|
19 |
$interface_config = $app->getconf->get_global_config('misc'); |
|
20 |
$secret = $interface_config['monitor_key']; |
|
21 |
|
|
22 |
$out = array(); |
|
23 |
|
|
24 |
if($token == '' or $secret == '' or $token != $secret) { |
|
25 |
$out['state'] = 'syserror'; |
|
26 |
$out['data'] = 'Password empty or incorrect.'; |
|
27 |
$out['time'] = date('Y-m-d H:i'); |
|
28 |
} else { |
|
29 |
if($type == 'serverlist') { |
|
30 |
$sql = 'SELECT server_id, server_name FROM server WHERE 1 ORDER BY server_id'; |
|
31 |
$records = $app->db->queryAllRecords($sql); |
b1a6a5
|
32 |
foreach($records as $index => $rec) { |
cc7a82
|
33 |
$rec = $app->db->queryOneRecord("SELECT * FROM monitor_data WHERE server_id = ? AND state NOT IN ('ok', 'no_state', 'info')", $rec['server_id']); |
b1a6a5
|
34 |
if($rec) $records[$index]['state'] = 'warn'; |
MC |
35 |
else $records[$index]['state'] = 'ok'; |
|
36 |
} |
181529
|
37 |
$out['state'] = 'ok'; |
L |
38 |
$out['data'] = $records; |
b1a6a5
|
39 |
$out['time'] = date('Y-m-d H:i', $rec['created']); |
181529
|
40 |
} else { |
cc7a82
|
41 |
$rec = $app->db->queryOneRecord("SELECT * FROM monitor_data WHERE type = ? AND server_id = ?", $type, $server_id); |
181529
|
42 |
if(is_array($rec)) { |
L |
43 |
$out['state'] = $rec['state']; |
|
44 |
$out['data'] = unserialize(stripslashes($rec['data'])); |
|
45 |
if(is_array($out['data']) && sizeof($out['data']) > 0){ |
|
46 |
foreach($out['data'] as $key => $val){ |
|
47 |
if(!$val) $out['data'][$key] = " "; |
|
48 |
} |
|
49 |
} |
b1a6a5
|
50 |
$out['time'] = date('Y-m-d H:i', $rec['created']); |
181529
|
51 |
} else { |
L |
52 |
$out['state'] = 'syserror'; |
|
53 |
$out['data'] = 'No monitor record found.'; |
|
54 |
$out['time'] = date('Y-m-d H:i'); |
|
55 |
} |
|
56 |
$sql = 'SELECT server_id, server_name FROM server WHERE 1 ORDER BY server_id'; |
|
57 |
$records = $app->db->queryAllRecords($sql); |
|
58 |
$out['serverlist'] = $records; |
|
59 |
} |
|
60 |
} |
|
61 |
$out['type'] = $type; |
|
62 |
|
b1a6a5
|
63 |
function __json_encode($data) { |
MC |
64 |
if( is_array($data) || is_object($data) ) { |
|
65 |
$islist = is_array($data) && ( empty($data) || array_keys($data) === range(0, count($data)-1) ); |
|
66 |
|
|
67 |
if( $islist ) { |
|
68 |
$json = '[' . implode(',', array_map('__json_encode', $data) ) . ']'; |
|
69 |
} else { |
|
70 |
$items = array(); |
|
71 |
foreach( $data as $key => $value ) { |
|
72 |
$items[] = __json_encode("$key") . ':' . __json_encode($value); |
|
73 |
} |
|
74 |
$json = '{' . implode(',', $items) . '}'; |
|
75 |
} |
|
76 |
} elseif( is_string($data) ) { |
|
77 |
// Escape non-printable or Non-ASCII characters. |
|
78 |
// I also put the \\ character first, as suggested in comments on the 'addcslashes' page. |
|
79 |
$string = '"' . addcslashes($data, "\\\"\n\r\t/" . chr(8) . chr(12)) . '"'; |
|
80 |
$json = ''; |
|
81 |
$len = strlen($string); |
|
82 |
// Convert UTF-8 to Hexadecimal Codepoints. |
|
83 |
for( $i = 0; $i < $len; $i++ ) { |
|
84 |
|
|
85 |
$char = $string[$i]; |
|
86 |
$c1 = ord($char); |
|
87 |
|
|
88 |
// Single byte; |
|
89 |
if( $c1 <128 ) { |
|
90 |
$json .= ($c1 > 31) ? $char : sprintf("\\u%04x", $c1); |
|
91 |
continue; |
|
92 |
} |
|
93 |
|
|
94 |
// Double byte |
|
95 |
$c2 = ord($string[++$i]); |
|
96 |
if ( ($c1 & 32) === 0 ) { |
|
97 |
$json .= sprintf("\\u%04x", ($c1 - 192) * 64 + $c2 - 128); |
|
98 |
continue; |
|
99 |
} |
|
100 |
|
|
101 |
// Triple |
|
102 |
$c3 = ord($string[++$i]); |
|
103 |
if( ($c1 & 16) === 0 ) { |
|
104 |
$json .= sprintf("\\u%04x", (($c1 - 224) <<12) + (($c2 - 128) << 6) + ($c3 - 128)); |
|
105 |
continue; |
|
106 |
} |
|
107 |
|
|
108 |
// Quadruple |
|
109 |
$c4 = ord($string[++$i]); |
|
110 |
if( ($c1 & 8 ) === 0 ) { |
|
111 |
$u = (($c1 & 15) << 2) + (($c2>>4) & 3) - 1; |
|
112 |
|
|
113 |
$w1 = (54<<10) + ($u<<6) + (($c2 & 15) << 2) + (($c3>>4) & 3); |
|
114 |
$w2 = (55<<10) + (($c3 & 15)<<6) + ($c4-128); |
|
115 |
$json .= sprintf("\\u%04x\\u%04x", $w1, $w2); |
|
116 |
} |
|
117 |
} |
|
118 |
} else { |
|
119 |
// int, floats, bools, null |
|
120 |
$json = strtolower(var_export( $data, true )); |
|
121 |
} |
|
122 |
return $json; |
181529
|
123 |
} |
L |
124 |
|
|
125 |
if(function_exists('json_encode')) { // PHP >= 5.2 |
|
126 |
echo json_encode($out); |
|
127 |
} else { // PHP < 5.2 |
|
128 |
echo __json_encode($out); |
|
129 |
} |
|
130 |
exit; |
b1a6a5
|
131 |
?> |