tbrehm
2011-04-13 11201c5e3b31d35e23b03f40e3d242014f15ce49
commit | author | age
f7a376 1 <?php
T 2 require_once('../../lib/config.inc.php');
3 $conf['start_session'] = false;
4 require_once('../../lib/app.inc.php');
5
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']);
16 $server_id = intval($_GET['server']);
17
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);
32         $out['state'] = 'ok';
33         $out['data'] = $records;
34         $out['time'] = date('Y-m-d H:i',$rec['created']);
35     } else {
36         $rec = $app->db->queryOneRecord("SELECT * FROM monitor_data WHERE type = '$type' AND server_id = $server_id");
37         if(is_array($rec)) {
38             $out['state'] = $rec['state'];
39             $out['data'] = unserialize(stripslashes($rec['data']));
40             if(is_array($out['data']) && sizeof($out['data']) > 0){
41                 foreach($out['data'] as $key => $val){
42                     if(!$val) $out['data'][$key] = "&nbsp;";
43                 }
44             }
45             $out['time'] = date('Y-m-d H:i',$rec['created']);
46         } else {
47             $out['state'] = 'syserror';
48             $out['data'] = 'No monitor record found.';
49             $out['time'] = date('Y-m-d H:i');
50         }
51         $sql = 'SELECT server_id, server_name FROM server WHERE 1 ORDER BY server_id';
52         $records = $app->db->queryAllRecords($sql);
53         $out['serverlist'] = $records;
54     }
55 }
56 $out['type'] = $type;
57
11201c 58 function __json_encode($data) {           
T 59     if( is_array($data) || is_object($data) ) {
60         $islist = is_array($data) && ( empty($data) || array_keys($data) === range(0,count($data)-1) );
61        
62         if( $islist ) {
63             $json = '[' . implode(',', array_map('__json_encode', $data) ) . ']';
64         } else {
65             $items = Array();
66             foreach( $data as $key => $value ) {
67                 $items[] = __json_encode("$key") . ':' . __json_encode($value);
68             }
69             $json = '{' . implode(',', $items) . '}';
70         }
71     } elseif( is_string($data) ) {
72         # Escape non-printable or Non-ASCII characters.
73         # I also put the \\ character first, as suggested in comments on the 'addcslashes' page.
74         $string = '"' . addcslashes($data, "\\\"\n\r\t/" . chr(8) . chr(12)) . '"';
75         $json    = '';
76         $len    = strlen($string);
77         # Convert UTF-8 to Hexadecimal Codepoints.
78         for( $i = 0; $i < $len; $i++ ) {
79            
80             $char = $string[$i];
81             $c1 = ord($char);
82            
83             # Single byte;
84             if( $c1 <128 ) {
85                 $json .= ($c1 > 31) ? $char : sprintf("\\u%04x", $c1);
86                 continue;
87             }
88            
89             # Double byte
90             $c2 = ord($string[++$i]);
91             if ( ($c1 & 32) === 0 ) {
92                 $json .= sprintf("\\u%04x", ($c1 - 192) * 64 + $c2 - 128);
93                 continue;
94             }
95            
96             # Triple
97             $c3 = ord($string[++$i]);
98             if( ($c1 & 16) === 0 ) {
99                 $json .= sprintf("\\u%04x", (($c1 - 224) <<12) + (($c2 - 128) << 6) + ($c3 - 128));
100                 continue;
101             }
102                
103             # Quadruple
104             $c4 = ord($string[++$i]);
105             if( ($c1 & 8 ) === 0 ) {
106                 $u = (($c1 & 15) << 2) + (($c2>>4) & 3) - 1;
107            
108                 $w1 = (54<<10) + ($u<<6) + (($c2 & 15) << 2) + (($c3>>4) & 3);
109                 $w2 = (55<<10) + (($c3 & 15)<<6) + ($c4-128);
110                 $json .= sprintf("\\u%04x\\u%04x", $w1, $w2);
111             }
112         }
113     } else {
114         # int, floats, bools, null
115         $json = strtolower(var_export( $data, true ));
116     }
117     return $json;
118 }
119
120 if(function_exists('json_encode')) { // PHP >= 5.2
121     echo json_encode($out);
122 } else { // PHP < 5.2
123     echo __json_encode($out);
124 }
f7a376 125 exit;
T 126 ?>