/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
183 by Suren A. Chilingaryan
Info service
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
511 by Suren A. Chilingaryan
Log newly created cache tables and provide service to get this information
19
function GetInterval() {
20
    global $_REQUEST;
21
    
22
    $cur = time();
23
    $interval = $_REQUEST['interval'];
24
25
    if (preg_match("/^(\d+)-(\d+)$/", $interval, $m)) {
26
	$from = $m[1];
27
	$to = $m[2];
28
    
29
	if ($to > $cur) $to = $cur;
30
    
31
	if ($to < $from) {
32
	    $from = false;
33
	    $to = false;
34
        }
35
    } else if (preg_match("/^(\d+)$/", $interval, $m)) {
36
	$to = $cur;
37
	$from = $to - $interval;
38
    }
39
    
40
    return array($from, $to);
41
}
42
43
183 by Suren A. Chilingaryan
Info service
44
switch ($target) {
45
 case "version":
46
    if (file_exists("VERSION")) {
47
	$stat = stat("VERSION");
48
	$date = date("Ymd", $stat["mtime"]);
49
			
50
	$version = file_get_contents("VERSION");
51
	if (preg_match("/^\s*([\d.]+)\s*(cvs)?/i", $version, $m)) {
52
    	    $version = $m[1];
53
	    if ($m[2]) $release = 0;
54
    	    else $release = 1;
55
	} else {
56
	    $release = 1;
57
	}
58
    
59
        if ($release) $title = $version;
60
	else $title = "$version-$date";
61
    } else {
62
	$error = translate("Version information is not available");
63
    }
64
 break;
505 by Suren A. Chilingaryan
Report cache size in info service
65
 case "size":
66
    try {
67
	$cache = new CACHEDB();
68
	$size = $cache->GetSize();
69
    } catch (ADEIException $ex) {
70
	$ex->logInfo(NULL, $req);
71
	$error = xml_escape($ex->getInfo());
72
    }
73
 break;
183 by Suren A. Chilingaryan
Info service
74
 case "cache":
75
    try {
76
	$req = new GROUPRequest();
77
	$fix_time = $req->GetTimeFormat();
78
	$multi_mode = false;
79
    } catch (ADEIException $ae) {
80
	if (($_GET['db_server'])||($_GET['db_name'])||($_GET['db_group'])) {
81
	    throw $ae;
82
	}
83
	$multi_mode = true;
84
    }
85
86
    try {
87
        $cache = new CACHEDB();
88
        $flags = REQUEST::NEED_ITEMINFO|REQUEST::NEED_INFO|CACHE::TABLE_INFO;
89
	if ($multi_mode) {
90
	    $info = $cache->GetCacheList($flags);
91
	} else {
92
	    $info = array();
93
	    $postfix = $cache->GetCachePostfix($req->props['db_group'], $req->props['db_name'], $req->props['db_server']);
94
	    $info[0] = $cache->GetExtendedCacheInfo($postfix, $flags);
95
	}
96
    } catch (ADEIException $ex) {
97
	$ex->logInfo(NULL, $req);
98
	$error = xml_escape($ex->getInfo());
99
    }
100
 break;
101
 case "log":
102
    try {
103
	$filter = json_decode(stripslashes($_REQUEST['filter']), true);
104
105
	if (isset($_REQUEST['priority'])) {
106
	    $priority = $_REQUEST['priority'];
107
	} else {
108
	    $priority = LOG_ERR;
109
	}
110
511 by Suren A. Chilingaryan
Log newly created cache tables and provide service to get this information
111
	list($from, $to) = GetInterval();
183 by Suren A. Chilingaryan
Info service
112
	if ((!$from)||(!$to)) {
511 by Suren A. Chilingaryan
Log newly created cache tables and provide service to get this information
113
	    $to = time();
183 by Suren A. Chilingaryan
Info service
114
	    $from = $to - $to%86400;
115
	} 
116
117
	$filter = array(
118
	    "setup" => $ADEI_SETUP
119
	);
120
	foreach (array("setup", "session", "source", "pid", "client") as $prop) {
121
	    if (isset($_REQUEST[$prop])) {
122
		if ($_REQUEST[$prop]==="") {
123
		    unset($filter[$prop]);
124
		} else {
125
		    $filter[$prop] = $_REQUEST[$prop];
126
		}
127
	    }
128
	}
129
	$logs = adeiGetLogs($from, $to, $priority, $filter);
130
    } catch (ADEIException $ex) {
131
	$ex->logInfo(NULL, $req);
132
	$error = xml_escape($ex->getInfo());
133
    }
134
 break;
511 by Suren A. Chilingaryan
Log newly created cache tables and provide service to get this information
135
 case "cachelog":
136
    $filter = array();
137
    foreach (array('id', 'action', 'resolution', 'archive') as $key) {
138
        if (isset($_GET[$key])) {
139
            $filter[$key] = $_GET[$key];
140
        }
141
    }
142
143
    list($from, $to) = GetInterval();
144
    if ($from & $to) 
145
        $filter['interval'] = array($from, $to);
146
    
147
    try {
148
        $cache = new CACHEDB();
149
        $logs = $cache->CacheLogGet($filter);
150
    } catch (ADEIException $ex) {
151
	$ex->logInfo(NULL, $req);
152
	$error = xml_escape($ex->getInfo());
153
    }
154
 break;
183 by Suren A. Chilingaryan
Info service
155
 default:
156
    if (isset($_GET['target'])) $error = translate("Unknown info target (%s) is specified", $_GET['target']);
157
    else $error = translate("The info target is not specified");
158
}
159
160
function OutputCacheInfo($out, &$item) {
161
    global $fix_time;
162
163
    if (is_array($item['info']['tables'])) {
164
	$item['info']['resolutions'] = implode(',',array_keys($item['info']['tables']));
165
    }
166
167
    if ($fix_time) {
168
	if ($item['first']) $info['first'] = date($fix_time, $info['first']);
169
	if ($item['last']) $info['last'] = date($fix_time, $info['last']);
170
    }
171
    
172
    $properties = array("db_server", "db_name", "db_group", "supported");
173
    $info_properties = array("first", "last", "records", "dbsize", "resolutions", "width");
174
175
    $extra = "";
176
    foreach ($item as $prop => $value) {
177
	if (!in_array($prop, $properties)) continue;
178
        $extra .= " $prop=\"" . xml_escape($value) . "\"";
179
    }
442 by Suren A. Chilingaryan
Fix few warnings in apache error log
180
    if (is_array($item['info'])) {
181
	foreach ($item['info'] as $prop => $value) {
182
	    if (!in_array($prop, $info_properties)) continue;
183
	    $extra .= " $prop=\"" . xml_escape($value) . "\"";
184
	}
183 by Suren A. Chilingaryan
Info service
185
    }
186
    fwrite($out, " <Value$extra/>\n");
187
}
188
189
function OutputLog($out, $item) {
190
    $item['time'] = $item['time']->format("Y-m-d\Th:i:s.uP");
191
    $extra = "";
192
    $properties = array("priority", "setup", "source", "session", "client", "message");
193
    foreach ($item as $prop => $value) {
194
	if (!in_array($prop, $properties)) continue;
195
        $extra .= " $prop=\"" . xml_escape($value) . "\"";
196
    }
197
    fwrite($out, " <Value$extra/>\n");
198
}
199
511 by Suren A. Chilingaryan
Log newly created cache tables and provide service to get this information
200
function OutputCacheLog($out, $item) {
201
    if (isset($item['time'])) {
202
        $time = new DateTime($item['time']);
203
        $item['time'] = $time->format("Y-m-d\Th:i:sP");
204
    }
205
    
206
    $extra = "";
207
    $properties = array("id", "time", "action", "db_server", "db_name", "db_group", "resolution");
208
    foreach ($item as $prop => $value) {
209
	if (!in_array($prop, $properties)) continue;
210
        $extra .= " $prop=\"" . xml_escape($value) . "\"";
211
    }
212
    fwrite($out, " <Value$extra/>\n");
213
}
214
215
183 by Suren A. Chilingaryan
Info service
216
if ($text) {
217
    header("Content-type: text/plain");
218
    header("Cache-Control: no-cache, must-revalidate");
219
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
220
221
    if ($error) echo translate("Error: %s\n", $error);
222
    else {
223
	switch ($target) {
224
	 case "version":
225
	    echo $title;
226
	 break;
505 by Suren A. Chilingaryan
Report cache size in info service
227
	 case "size":
228
	    if ($size) echo $size['size'];
229
	    else echo "";
183 by Suren A. Chilingaryan
Info service
230
	 case "log":
231
	    foreach($logs as $item) {
232
	        echo $item['time']->format("Y-m-d\Th:i:s.uP") . " " . html_entity_decode($item['message']) . "\n";
233
	    }
234
	 break;
235
	 default:
236
	    echo translate("Error: Target %s is not supported text encoding\n", $target);
237
	}
238
    }
239
    exit;
240
} else {
241
    header("Content-type: text/xml");
242
    header("Cache-Control: no-cache, must-revalidate");
243
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
244
}
245
246
if ($xslt) {
247
    $temp_file = tempnam(sys_get_temp_dir(), 'adei_version.');
248
    $out = @fopen($temp_file, "w");
249
    if (!$out) $error = translate("I'm not able to create temporary file \"%s\"", $temp_file);
250
} else {
251
    $out = fopen("php://output", "w");
252
}
253
254
if (true) {
255
    fwrite($out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
256
    if ($error) {
257
	fwrite($out, "<result><Error>$error</Error></result>");
258
	$error = false;
259
    } else {
260
	fwrite($out, "<result>\n");
261
262
	switch ($target) {
263
	 case "version":
264
	    fwrite($out, " <version date=\"${date}\" release=\"${release}\" version=\"${version}\">${title}</version>\n");
265
	    fwrite($out, " <capabilities/>\n");
266
//	    fwrite($out, " <server>${_SERVER['SERVER_SIGNATURE']}</server>\n");
267
	    fwrite($out, " <php>" . phpversion() . "</php>\n");
268
	 break;
505 by Suren A. Chilingaryan
Report cache size in info service
269
	 case "size":
270
	    fwrite($out, " <size size=\"{$size['size']}\""); 
271
	    if ($size['data']) fwrite($out, " data=\"{$size['data']}\"");
272
	    if ($size['index']) fwrite($out, " index=\"{$size['index']}\"");
273
	    fwrite($out, " />\n");
274
	 break;
183 by Suren A. Chilingaryan
Info service
275
	 case "cache":
276
	    foreach ($info as &$item) {
277
	        OutputCacheInfo($out, $item);
278
	    }
279
	 break;
280
	 case "log":
281
	    foreach($logs as $log) {
282
		OutputLog($out, $log);
283
	    }
284
	 break;
511 by Suren A. Chilingaryan
Log newly created cache tables and provide service to get this information
285
         case "cachelog":
286
	    foreach($logs as $log) {
287
		OutputCacheLog($out, $log);
288
	    }
289
	 break;
183 by Suren A. Chilingaryan
Info service
290
	}
291
	fwrite($out, "</result>");
292
    } 
293
    fclose($out);
294
}
295
296
297
if (($xslt)&&(!$error)) {
298
    try {
299
	echo $ADEI->TransformXML($xslt, $temp_file);
300
    } catch (ADEIException $ex) {
301
	$ex->logInfo(NULL, $reader?$reader:$req);
302
	$error = $ADEI->EscapeForXML($ex->getInfo());
303
    }
304
    @unlink($temp_file);
305
}
306
307
if ($error) {
308
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
309
    echo "<result><Error>$error</Error></result>";
310
}
311
312
?>