1 | <?php |
---|
2 | |
---|
3 | include("./common.php"); |
---|
4 | |
---|
5 | class Cycle |
---|
6 | { |
---|
7 | var $values; |
---|
8 | var $i; |
---|
9 | var $count; |
---|
10 | |
---|
11 | function Cycle($v) |
---|
12 | { |
---|
13 | $this->values = func_get_args(); |
---|
14 | $this->i = -1; |
---|
15 | $this->count = count($this->values); |
---|
16 | } |
---|
17 | |
---|
18 | function next() |
---|
19 | { |
---|
20 | $this->i = ($this->i + 1) % $this->count; |
---|
21 | return $this->values[$this->i]; |
---|
22 | } |
---|
23 | |
---|
24 | function cur() |
---|
25 | { |
---|
26 | return $this->values[$this->i]; |
---|
27 | } |
---|
28 | |
---|
29 | function reset() |
---|
30 | { |
---|
31 | $this->i = -1; |
---|
32 | } |
---|
33 | } |
---|
34 | |
---|
35 | function number_formats($a, $keys) |
---|
36 | { |
---|
37 | foreach ($keys as $k) { |
---|
38 | $a[$k] = number_format($a[$k]); |
---|
39 | } |
---|
40 | return $a; |
---|
41 | } |
---|
42 | |
---|
43 | function size($size) |
---|
44 | { |
---|
45 | $size = (int) $size; |
---|
46 | if ($size < 1024) |
---|
47 | return number_format($size, 2) . ' b'; |
---|
48 | |
---|
49 | if ($size < 1048576) |
---|
50 | return number_format($size / 1024, 2) . ' K'; |
---|
51 | |
---|
52 | return number_format($size / 1048576, 2) . ' M'; |
---|
53 | } |
---|
54 | |
---|
55 | function age($time) |
---|
56 | { |
---|
57 | if (!$time) return ''; |
---|
58 | $delta = REQUEST_TIME - $time; |
---|
59 | |
---|
60 | if ($delta < 0) { |
---|
61 | $delta = -$delta; |
---|
62 | } |
---|
63 | |
---|
64 | static $seconds = array(1, 60, 3600, 86400, 604800, 2678400, 31536000); |
---|
65 | static $name = array('s', 'm', 'h', 'd', 'w', 'M', 'Y'); |
---|
66 | |
---|
67 | for ($i = 6; $i >= 0; $i --) { |
---|
68 | if ($delta >= $seconds[$i]) { |
---|
69 | $ret = (int) ($delta / $seconds[$i]); |
---|
70 | return $ret . ' ' . $name[$i]; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | return '0 s'; |
---|
75 | } |
---|
76 | |
---|
77 | function freeblock_to_graph($freeblocks, $size) |
---|
78 | { |
---|
79 | global $graph_width, $usage_graph_width, $free_graph_width; |
---|
80 | |
---|
81 | // cached in static variable |
---|
82 | static $graph_initial; |
---|
83 | if (!isset($graph_initial)) { |
---|
84 | $graph_initial = array_fill(0, $graph_width, 0); |
---|
85 | } |
---|
86 | $graph = $graph_initial; |
---|
87 | foreach ($freeblocks as $b) { |
---|
88 | $begin = $b['offset'] / $size * $graph_width; |
---|
89 | $end = ($b['offset'] + $b['size']) / $size * $graph_width; |
---|
90 | |
---|
91 | if ((int) $begin == (int) $end) { |
---|
92 | $v = $end - $begin; |
---|
93 | $graph[(int) $v] += $v - (int) $v; |
---|
94 | } |
---|
95 | else { |
---|
96 | $graph[(int) $begin] += 1 - ($begin - (int) $begin); |
---|
97 | $graph[(int) $end] += $end - (int) $end; |
---|
98 | for ($i = (int) $begin + 1, $e = (int) $end; $i < $e; $i ++) { |
---|
99 | $graph[$i] += 1; |
---|
100 | } |
---|
101 | } |
---|
102 | } |
---|
103 | $html = array(); |
---|
104 | $c = 255; |
---|
105 | foreach ($graph as $k => $v) { |
---|
106 | if (!isset($free_graph_width)) { |
---|
107 | $v = 1 - $v; |
---|
108 | } |
---|
109 | $v = (int) ($v * $c); |
---|
110 | $r = $g = $c - $v; |
---|
111 | $b = $c; |
---|
112 | $html[] = '<div style="background: rgb(' . "$r,$g,$b" . ')"></div>'; |
---|
113 | } |
---|
114 | return implode('', $html); |
---|
115 | } |
---|
116 | |
---|
117 | function calc_total(&$total, $data) |
---|
118 | { |
---|
119 | foreach ($data as $k => $v) { |
---|
120 | switch ($k) { |
---|
121 | case 'type': |
---|
122 | case 'cache_name': |
---|
123 | case 'cacheid': |
---|
124 | case 'free_blocks': |
---|
125 | continue 2; |
---|
126 | } |
---|
127 | if (!isset($total[$k])) { |
---|
128 | $total[$k] = $v; |
---|
129 | } |
---|
130 | else { |
---|
131 | switch ($k) { |
---|
132 | case 'hits_by_hour': |
---|
133 | case 'hits_by_second': |
---|
134 | foreach ($data[$k] as $kk => $vv) { |
---|
135 | $total[$k][$kk] += $vv; |
---|
136 | } |
---|
137 | break; |
---|
138 | |
---|
139 | default: |
---|
140 | $total[$k] += $v; |
---|
141 | } |
---|
142 | } |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | function array_avg($a) |
---|
147 | { |
---|
148 | if (count($a) == 0) { |
---|
149 | return ''; |
---|
150 | } |
---|
151 | return array_sum($a) / count($a); |
---|
152 | } |
---|
153 | |
---|
154 | function bar_hits_percent($v, $percent, $active) |
---|
155 | { |
---|
156 | $r = 220 + (int) ($percent * 25); |
---|
157 | $g = $b = 220 - (int) ($percent * 220); |
---|
158 | $percent = (int) ($percent * 100); |
---|
159 | $a = $active ? ' active' : ''; |
---|
160 | return '<div title="' . $v . '">' |
---|
161 | . '<div class="barf' . $a . '" style="height: ' . (100 - $percent) . '%"></div>' |
---|
162 | . '<div class="barv' . $a . '" style="background: rgb(' . "$r,$g,$b" . '); height: ' . $percent . '%"></div>' |
---|
163 | . '</div>'; |
---|
164 | } |
---|
165 | |
---|
166 | function hits_to_graph($hits) |
---|
167 | { |
---|
168 | $max = 0; |
---|
169 | foreach ($hits as $v) { |
---|
170 | if ($max < $v) { |
---|
171 | $max = $v; |
---|
172 | } |
---|
173 | } |
---|
174 | if (!$max) { |
---|
175 | return ''; |
---|
176 | } |
---|
177 | $t = (time() / (60 * 60)) % 24; |
---|
178 | $html = array(); |
---|
179 | foreach ($hits as $i => $v) { |
---|
180 | $html[] = bar_hits_percent($v, $v / $max, $i == $t); |
---|
181 | } |
---|
182 | return implode('', $html); |
---|
183 | } |
---|
184 | |
---|
185 | function switcher($name, $options) |
---|
186 | { |
---|
187 | $n = isset($_GET[$name]) ? $_GET[$name] : null; |
---|
188 | $html = array(); |
---|
189 | foreach ($options as $k => $v) { |
---|
190 | $html[] = sprintf('<a href="?%s=%s"%s>%s</a>', $name, $k, $k == $n ? ' class="active"' : '', $v); |
---|
191 | } |
---|
192 | return implode(' ', $html); |
---|
193 | } |
---|
194 | |
---|
195 | if (!extension_loaded('XCache')) { |
---|
196 | echo '<h1>XCache is not loaded</h1>'; |
---|
197 | ob_start(); |
---|
198 | phpinfo(); |
---|
199 | $info = ob_get_clean(); |
---|
200 | if (preg_match('!<td class="v">(.*\\.ini)!', $info, $m)) { |
---|
201 | echo "Please check $m[1]"; |
---|
202 | } |
---|
203 | else if (preg_match('!Configuration File \\(php.ini\\) Path *</td><td class="v">([^<]+)!', $info, $m)) { |
---|
204 | echo "Please put a php.ini in $m[1] and load XCache extension"; |
---|
205 | } |
---|
206 | else { |
---|
207 | echo "You don't even have a php.ini yet?"; |
---|
208 | } |
---|
209 | exit; |
---|
210 | } |
---|
211 | $pcnt = xcache_count(XC_TYPE_PHP); |
---|
212 | $vcnt = xcache_count(XC_TYPE_VAR); |
---|
213 | |
---|
214 | if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
---|
215 | $remove = @ $_POST['remove']; |
---|
216 | if ($remove && is_array($remove)) { |
---|
217 | foreach ($remove as $name) { |
---|
218 | xcache_unset($name); |
---|
219 | } |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | $moduleinfo = null; |
---|
224 | $type_none = -1; |
---|
225 | if (!isset($_GET['type'])) { |
---|
226 | $_GET['type'] = $type_none; |
---|
227 | } |
---|
228 | $_GET['type'] = $type = (int) $_GET['type']; |
---|
229 | |
---|
230 | // {{{ process clear |
---|
231 | function processClear() |
---|
232 | { |
---|
233 | $type = isset($_POST['type']) ? $_POST['type'] : null; |
---|
234 | if ($type != XC_TYPE_PHP && $type != XC_TYPE_VAR) { |
---|
235 | $type = null; |
---|
236 | } |
---|
237 | if (isset($type)) { |
---|
238 | $cacheid = (int) (isset($_POST['cacheid']) ? $_POST['cacheid'] : 0); |
---|
239 | if (isset($_POST['clearcache'])) { |
---|
240 | $count = xcache_count($type); |
---|
241 | if ($cacheid == $count) { |
---|
242 | for ($cacheid = 0; $cacheid < $count; $cacheid ++) { |
---|
243 | xcache_clear_cache($type, $cacheid); |
---|
244 | } |
---|
245 | } |
---|
246 | else { |
---|
247 | xcache_clear_cache($type, $cacheid); |
---|
248 | } |
---|
249 | } |
---|
250 | } |
---|
251 | } |
---|
252 | processClear(); |
---|
253 | // }}} |
---|
254 | // {{{ load info/list |
---|
255 | $cacheinfos = array(); |
---|
256 | $total = array(); |
---|
257 | for ($i = 0; $i < $pcnt; $i ++) { |
---|
258 | $data = xcache_info(XC_TYPE_PHP, $i); |
---|
259 | if ($type === XC_TYPE_PHP) { |
---|
260 | $data += xcache_list(XC_TYPE_PHP, $i); |
---|
261 | } |
---|
262 | $data['type'] = XC_TYPE_PHP; |
---|
263 | $data['cache_name'] = "php#$i"; |
---|
264 | $data['cacheid'] = $i; |
---|
265 | $cacheinfos[] = $data; |
---|
266 | if ($pcnt >= 2) { |
---|
267 | calc_total($total, $data); |
---|
268 | } |
---|
269 | } |
---|
270 | |
---|
271 | if ($pcnt >= 2) { |
---|
272 | $total['type'] = XC_TYPE_PHP; |
---|
273 | $total['cache_name'] = _T('Total'); |
---|
274 | $total['cacheid'] = $pcnt; |
---|
275 | $total['gc'] = null; |
---|
276 | $total['istotal'] = true; |
---|
277 | $cacheinfos[] = $total; |
---|
278 | } |
---|
279 | |
---|
280 | $total = array(); |
---|
281 | for ($i = 0; $i < $vcnt; $i ++) { |
---|
282 | $data = xcache_info(XC_TYPE_VAR, $i); |
---|
283 | if ($type === XC_TYPE_VAR) { |
---|
284 | $data += xcache_list(XC_TYPE_VAR, $i); |
---|
285 | } |
---|
286 | $data['type'] = XC_TYPE_VAR; |
---|
287 | $data['cache_name'] = "var#$i"; |
---|
288 | $data['cacheid'] = $i; |
---|
289 | $cacheinfos[] = $data; |
---|
290 | if ($vcnt >= 2) { |
---|
291 | calc_total($total, $data); |
---|
292 | } |
---|
293 | } |
---|
294 | |
---|
295 | if ($vcnt >= 2) { |
---|
296 | $total['type'] = XC_TYPE_VAR; |
---|
297 | $total['cache_name'] = _T('Total'); |
---|
298 | $total['cacheid'] = $vcnt; |
---|
299 | $total['gc'] = null; |
---|
300 | $total['istotal'] = true; |
---|
301 | $cacheinfos[] = $total; |
---|
302 | } |
---|
303 | // }}} |
---|
304 | // {{{ merge the list |
---|
305 | switch ($type) { |
---|
306 | case XC_TYPE_PHP: |
---|
307 | case XC_TYPE_VAR: |
---|
308 | $cachelist = array('type' => $type, 'cache_list' => array(), 'deleted_list' => array()); |
---|
309 | if ($type == XC_TYPE_VAR) { |
---|
310 | $cachelist['type_name'] = 'var'; |
---|
311 | } |
---|
312 | else { |
---|
313 | $cachelist['type_name'] = 'php'; |
---|
314 | } |
---|
315 | foreach ($cacheinfos as $i => $c) { |
---|
316 | if (!empty($c['istotal'])) { |
---|
317 | continue; |
---|
318 | } |
---|
319 | if ($c['type'] == $type && isset($c['cache_list'])) { |
---|
320 | foreach ($c['cache_list'] as $e) { |
---|
321 | $e['cache_name'] = $c['cache_name']; |
---|
322 | $cachelist['cache_list'][] = $e; |
---|
323 | } |
---|
324 | foreach ($c['deleted_list'] as $e) { |
---|
325 | $e['cache_name'] = $c['cache_name']; |
---|
326 | $cachelist['deleted_list'][] = $e; |
---|
327 | } |
---|
328 | } |
---|
329 | } |
---|
330 | if ($type == XC_TYPE_PHP) { |
---|
331 | $inodes = array(); |
---|
332 | $haveinode = false; |
---|
333 | foreach ($cachelist['cache_list'] as $e) { |
---|
334 | if (isset($e['inode'])) { |
---|
335 | $haveinode = true; |
---|
336 | } |
---|
337 | } |
---|
338 | if (!$haveinode) { |
---|
339 | foreach ($cachelist['deleted_list'] as $e) { |
---|
340 | if (isset($e['inode'])) { |
---|
341 | $haveinode = true; |
---|
342 | } |
---|
343 | } |
---|
344 | } |
---|
345 | } |
---|
346 | unset($data); |
---|
347 | break; |
---|
348 | |
---|
349 | default: |
---|
350 | $_GET['type'] = $type_none; |
---|
351 | $cachelist = array(); |
---|
352 | ob_start(); |
---|
353 | phpinfo(INFO_MODULES); |
---|
354 | $moduleinfo = ob_get_clean(); |
---|
355 | if (preg_match('!XCache</a></h2>(.*?)<h2>!is', $moduleinfo, $m)) { |
---|
356 | $moduleinfo = $m[1]; |
---|
357 | } |
---|
358 | else { |
---|
359 | $moduleinfo = null; |
---|
360 | } |
---|
361 | break; |
---|
362 | } |
---|
363 | // }}} |
---|
364 | |
---|
365 | $type_php = XC_TYPE_PHP; |
---|
366 | $type_var = XC_TYPE_VAR; |
---|
367 | $types = array($type_none => _T('Statistics'), $type_php => _T('List PHP'), $type_var => _T('List Var Data')); |
---|
368 | $xcache_version = XCACHE_VERSION; |
---|
369 | $xcache_modules = XCACHE_MODULES; |
---|
370 | |
---|
371 | include("xcache.tpl.php"); |
---|
372 | |
---|
373 | ?> |
---|