root/trunk/services/info.php

Revision trunk,183, 6.2 kB (checked in by Suren A. Chilingaryan <csa@dside.dyndns.org>, 7 months ago)

Info service

Line 
1 <?php
2
3 global $ADEI;
4 global $ADEI_SETUP;
5 global $fix_time;
6
7 if (strtolower($_REQUEST['encoding']=='text')) $text = true;
8 else $text = false;
9
10 $target = $_REQUEST['target'];
11
12 try {
13     $req = new REQUEST;
14     $xslt = $req->GetProp('xslt');
15 } catch (ADEIException $ae) {
16     $xslt = false;
17 }
18
19 switch ($target) {
20  case "version":
21     if (file_exists("VERSION")) {
22     $stat = stat("VERSION");
23     $date = date("Ymd", $stat["mtime"]);
24             
25     $version = file_get_contents("VERSION");
26     if (preg_match("/^\s*([\d.]+)\s*(cvs)?/i", $version, $m)) {
27             $version = $m[1];
28         if ($m[2]) $release = 0;
29             else $release = 1;
30     } else {
31         $release = 1;
32     }
33     
34         if ($release) $title = $version;
35     else $title = "$version-$date";
36     } else {
37     $error = translate("Version information is not available");
38     }
39  break;
40  case "cache":
41     try {
42     $req = new GROUPRequest();
43     $fix_time = $req->GetTimeFormat();
44     $multi_mode = false;
45     } catch (ADEIException $ae) {
46     if (($_GET['db_server'])||($_GET['db_name'])||($_GET['db_group'])) {
47         throw $ae;
48     }
49     $multi_mode = true;
50     }
51
52     try {
53         $cache = new CACHEDB();
54         $flags = REQUEST::NEED_ITEMINFO|REQUEST::NEED_INFO|CACHE::TABLE_INFO;
55     if ($multi_mode) {
56         $info = $cache->GetCacheList($flags);
57     } else {
58         $info = array();
59         $postfix = $cache->GetCachePostfix($req->props['db_group'], $req->props['db_name'], $req->props['db_server']);
60         $info[0] = $cache->GetExtendedCacheInfo($postfix, $flags);
61     }
62     } catch (ADEIException $ex) {
63     $ex->logInfo(NULL, $req);
64     $error = xml_escape($ex->getInfo());
65     }
66  break;
67  case "log":
68     try {
69     $cur = time();
70
71     $interval = $_REQUEST['interval'];
72     $filter = json_decode(stripslashes($_REQUEST['filter']), true);
73
74     if (isset($_REQUEST['priority'])) {
75         $priority = $_REQUEST['priority'];
76     } else {
77         $priority = LOG_ERR;
78     }
79
80     if (preg_match("/^(\d+)-(\d+)$/", $interval, $m)) {
81         $from = $m[1];
82         $to = $m[2];
83     
84         if ($to > $cur) $to = $cur;
85     
86         if ($to < $from) {
87         $from = false;
88         $to = false;
89         }
90     } else if (preg_match("/^(\d+)$/", $interval, $m)) {
91         $to = $cur;
92         $from = $to - $interval;
93     }
94     
95     if ((!$from)||(!$to)) {
96         $to = $cur;
97         $from = $to - $to%86400;
98     }
99
100     $filter = array(
101         "setup" => $ADEI_SETUP
102     );
103     foreach (array("setup", "session", "source", "pid", "client") as $prop) {
104         if (isset($_REQUEST[$prop])) {
105         if ($_REQUEST[$prop]==="") {
106             unset($filter[$prop]);
107         } else {
108             $filter[$prop] = $_REQUEST[$prop];
109         }
110         }
111     }
112     $logs = adeiGetLogs($from, $to, $priority, $filter);
113     } catch (ADEIException $ex) {
114     $ex->logInfo(NULL, $req);
115     $error = xml_escape($ex->getInfo());
116     }
117  break;
118  default:
119     if (isset($_GET['target'])) $error = translate("Unknown info target (%s) is specified", $_GET['target']);
120     else $error = translate("The info target is not specified");
121 }
122
123 function OutputCacheInfo($out, &$item) {
124     global $fix_time;
125
126     if (is_array($item['info']['tables'])) {
127     $item['info']['resolutions'] = implode(',',array_keys($item['info']['tables']));
128     }
129
130     if ($fix_time) {
131     if ($item['first']) $info['first'] = date($fix_time, $info['first']);
132     if ($item['last']) $info['last'] = date($fix_time, $info['last']);
133     }
134     
135     $properties = array("db_server", "db_name", "db_group", "supported");
136     $info_properties = array("first", "last", "records", "dbsize", "resolutions", "width");
137
138     $extra = "";
139     foreach ($item as $prop => $value) {
140     if (!in_array($prop, $properties)) continue;
141         $extra .= " $prop=\"" . xml_escape($value) . "\"";
142     }
143     foreach ($item['info'] as $prop => $value) {
144     if (!in_array($prop, $info_properties)) continue;
145         $extra .= " $prop=\"" . xml_escape($value) . "\"";
146     }
147     fwrite($out, " <Value$extra/>\n");
148 }
149
150 function OutputLog($out, $item) {
151     $item['time'] = $item['time']->format("Y-m-d\Th:i:s.uP");
152     $extra = "";
153     $properties = array("priority", "setup", "source", "session", "client", "message");
154     foreach ($item as $prop => $value) {
155     if (!in_array($prop, $properties)) continue;
156         $extra .= " $prop=\"" . xml_escape($value) . "\"";
157     }
158     fwrite($out, " <Value$extra/>\n");
159 }
160
161 if ($text) {
162     header("Content-type: text/plain");
163     header("Cache-Control: no-cache, must-revalidate");
164     header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
165
166     if ($error) echo translate("Error: %s\n", $error);
167     else {
168     switch ($target) {
169      case "version":
170         echo $title;
171      break;
172      case "log":
173         foreach($logs as $item) {
174             echo $item['time']->format("Y-m-d\Th:i:s.uP") . " " . html_entity_decode($item['message']) . "\n";
175         }
176      break;
177      default:
178         echo translate("Error: Target %s is not supported text encoding\n", $target);
179     }
180     }
181     exit;
182 } else {
183     header("Content-type: text/xml");
184     header("Cache-Control: no-cache, must-revalidate");
185     header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
186 }
187
188 if ($xslt) {
189     $temp_file = tempnam(sys_get_temp_dir(), 'adei_version.');
190     $out = @fopen($temp_file, "w");
191     if (!$out) $error = translate("I'm not able to create temporary file \"%s\"", $temp_file);
192 } else {
193     $out = fopen("php://output", "w");
194 }
195
196 if (true) {
197     fwrite($out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
198     if ($error) {
199     fwrite($out, "<result><Error>$error</Error></result>");
200     $error = false;
201     } else {
202     fwrite($out, "<result>\n");
203
204     switch ($target) {
205      case "version":
206         fwrite($out, " <version date=\"${date}\" release=\"${release}\" version=\"${version}\">${title}</version>\n");
207         fwrite($out, " <capabilities/>\n");
208 //        fwrite($out, " <server>${_SERVER['SERVER_SIGNATURE']}</server>\n");
209         fwrite($out, " <php>" . phpversion() . "</php>\n");
210      break;
211      case "cache":
212         foreach ($info as &$item) {
213             OutputCacheInfo($out, $item);
214         }
215      break;
216      case "log":
217         foreach($logs as $log) {
218         OutputLog($out, $log);
219         }
220      break;
221     }
222     fwrite($out, "</result>");
223     }
224     fclose($out);
225 }
226
227
228 if (($xslt)&&(!$error)) {
229     try {
230     echo $ADEI->TransformXML($xslt, $temp_file);
231     } catch (ADEIException $ex) {
232     $ex->logInfo(NULL, $reader?$reader:$req);
233     $error = $ADEI->EscapeForXML($ex->getInfo());
234     }
235     @unlink($temp_file);
236 }
237
238 if ($error) {
239     echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
240     echo "<result><Error>$error</Error></result>";
241 }
242
243 ?>
Note: See TracBrowser for help on using the browser.