1 | <?php |
---|
2 | |
---|
3 | include "./common.php"; |
---|
4 | |
---|
5 | function freeblock_to_graph($freeblocks, $size) // {{{ |
---|
6 | { |
---|
7 | global $config; |
---|
8 | |
---|
9 | // cached in static variable |
---|
10 | static $graph_initial; |
---|
11 | if (!isset($graph_initial)) { |
---|
12 | $graph_initial = array_fill(0, $config['percent_graph_width'], 0); |
---|
13 | } |
---|
14 | $graph = $graph_initial; |
---|
15 | foreach ($freeblocks as $b) { |
---|
16 | $begin = $b['offset'] / $size * $config['percent_graph_width']; |
---|
17 | $end = ($b['offset'] + $b['size']) / $size * $config['percent_graph_width']; |
---|
18 | |
---|
19 | if ((int) $begin == (int) $end) { |
---|
20 | $v = $end - $begin; |
---|
21 | $graph[(int) $v] += $v - (int) $v; |
---|
22 | } |
---|
23 | else { |
---|
24 | $graph[(int) $begin] += 1 - ($begin - (int) $begin); |
---|
25 | $graph[(int) $end] += $end - (int) $end; |
---|
26 | for ($i = (int) $begin + 1, $e = (int) $end; $i < $e; $i ++) { |
---|
27 | $graph[$i] += 1; |
---|
28 | } |
---|
29 | } |
---|
30 | } |
---|
31 | $html = array(); |
---|
32 | $c = 255; |
---|
33 | foreach ($graph as $k => $v) { |
---|
34 | if ($config['percent_graph_type'] != 'free') { |
---|
35 | $v = 1 - $v; |
---|
36 | } |
---|
37 | $v = (int) ($v * $c); |
---|
38 | $r = $g = $c - $v; |
---|
39 | $b = $c; |
---|
40 | $html[] = '<div style="background: rgb(' . "$r,$g,$b" . ')"></div>'; |
---|
41 | } |
---|
42 | return implode('', $html); |
---|
43 | } |
---|
44 | // }}} |
---|
45 | function calc_total(&$total, $data) // {{{ |
---|
46 | { |
---|
47 | foreach ($data as $k => $v) { |
---|
48 | switch ($k) { |
---|
49 | case 'type': |
---|
50 | case 'cache_name': |
---|
51 | case 'cacheid': |
---|
52 | case 'free_blocks': |
---|
53 | continue 2; |
---|
54 | } |
---|
55 | if (!isset($total[$k])) { |
---|
56 | $total[$k] = $v; |
---|
57 | } |
---|
58 | else { |
---|
59 | switch ($k) { |
---|
60 | case 'hits_by_hour': |
---|
61 | case 'hits_by_second': |
---|
62 | foreach ($data[$k] as $kk => $vv) { |
---|
63 | $total[$k][$kk] += $vv; |
---|
64 | } |
---|
65 | break; |
---|
66 | |
---|
67 | default: |
---|
68 | $total[$k] += $v; |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | // }}} |
---|
74 | function array_avg($a) // {{{ |
---|
75 | { |
---|
76 | if (count($a) == 0) { |
---|
77 | return ''; |
---|
78 | } |
---|
79 | return array_sum($a) / count($a); |
---|
80 | } |
---|
81 | // }}} |
---|
82 | function bar_hits_percent($v, $percent, $active) // {{{ |
---|
83 | { |
---|
84 | $r = 220 + (int) ($percent * 25); |
---|
85 | $g = $b = 220 - (int) ($percent * 220); |
---|
86 | $percent = (int) ($percent * 100); |
---|
87 | $a = $active ? ' class="active"' : ''; |
---|
88 | $height = 20 - 1 * 2; |
---|
89 | $valueHeight = ceil($height * $percent / 100); |
---|
90 | $paddingHeight = $height - $valueHeight; |
---|
91 | $valueHeight = $valueHeight ? $valueHeight . "px" : 0; |
---|
92 | $paddingHeight = $paddingHeight ? $paddingHeight . "px" : 0; |
---|
93 | return '<a title="' . $v . '" href="javascript:;"' . $a . '>' |
---|
94 | . ($paddingHeight ? '<span style="height: ' . $paddingHeight . '"></span>' : '') |
---|
95 | . ($valueHeight ? '<span style="background: rgb(' . "$r,$g,$b" . '); height: ' . $valueHeight . '"></span>' : '') |
---|
96 | . '</a>'; |
---|
97 | } |
---|
98 | // }}} |
---|
99 | function get_cache_hits_graph($ci, $key) // {{{ |
---|
100 | { |
---|
101 | global $maxHitsByHour; |
---|
102 | if ($ci['cacheid'] == -1) { |
---|
103 | $max = max($ci[$key]); |
---|
104 | } |
---|
105 | else { |
---|
106 | $max = $maxHitsByHour[$ci['type']]; |
---|
107 | } |
---|
108 | if (!$max) { |
---|
109 | $max = 1; |
---|
110 | } |
---|
111 | $t = (time() / (60 * 60)) % 24; |
---|
112 | |
---|
113 | $html = array(); |
---|
114 | $width = count($ci[$key]) * 2; |
---|
115 | $html[] = '<div class="hitsgraph" style="width: ' . $width . 'px">'; |
---|
116 | foreach ($ci[$key] as $i => $v) { |
---|
117 | $html[] = bar_hits_percent($v, $v / $max, $i == $t); |
---|
118 | } |
---|
119 | $html[] = "</div>"; |
---|
120 | return implode('', $html); |
---|
121 | } |
---|
122 | // }}} |
---|
123 | function getModuleInfo() // {{{ |
---|
124 | { |
---|
125 | ob_start(); |
---|
126 | phpinfo(INFO_MODULES); |
---|
127 | $moduleInfo = ob_get_clean(); |
---|
128 | if (!preg_match_all('!(XCache[^<>]*)</a></h2>(.*?)<h2>!is', $moduleInfo, $m)) { |
---|
129 | return; |
---|
130 | } |
---|
131 | |
---|
132 | $moduleInfo = array(); |
---|
133 | foreach ($m[1] as $i => $dummy) { |
---|
134 | $caption = trim($m[1][$i]); |
---|
135 | $info = str_replace('<br />', '', trim($m[2][$i])); |
---|
136 | |
---|
137 | $regex = '!<table[^>]*>!'; |
---|
138 | if (preg_match($regex, $info)) { |
---|
139 | $moduleInfo[] = preg_replace($regex, "\\0<caption>$caption</caption>", $info, 1); |
---|
140 | } |
---|
141 | else { |
---|
142 | $moduleInfo[] = "<h3>$caption</h3>"; |
---|
143 | $moduleInfo[] = $info; |
---|
144 | } |
---|
145 | } |
---|
146 | $moduleInfo = implode('', $moduleInfo); |
---|
147 | if (ini_get("xcache.test")) { |
---|
148 | ob_start(); |
---|
149 | include "./sub/testcoredump.tpl.php"; |
---|
150 | $test_coredump = trim(ob_get_clean()); |
---|
151 | $moduleInfo = str_replace('xcache.coredump_directory', 'xcache.coredump_directory' . $test_coredump, $moduleInfo); |
---|
152 | } |
---|
153 | return $moduleInfo; |
---|
154 | } |
---|
155 | // }}} |
---|
156 | function getCacheInfos() // {{{ |
---|
157 | { |
---|
158 | static $cacheInfos; |
---|
159 | if (isset($cacheInfos)) { |
---|
160 | return $cacheInfos; |
---|
161 | } |
---|
162 | |
---|
163 | $phpCacheCount = xcache_count(XC_TYPE_PHP); |
---|
164 | $varCacheCount = xcache_count(XC_TYPE_VAR); |
---|
165 | |
---|
166 | $cacheInfos = array(); |
---|
167 | $total = array(); |
---|
168 | global $maxHitsByHour; |
---|
169 | $maxHitsByHour = array(0, 0); |
---|
170 | for ($i = 0; $i < $phpCacheCount; $i ++) { |
---|
171 | $data = xcache_info(XC_TYPE_PHP, $i); |
---|
172 | if ($_GET['do'] === 'listphp') { |
---|
173 | $data += xcache_list(XC_TYPE_PHP, $i); |
---|
174 | } |
---|
175 | $data['type'] = XC_TYPE_PHP; |
---|
176 | $data['cache_name'] = "php#$i"; |
---|
177 | $data['cacheid'] = $i; |
---|
178 | $cacheInfos[] = $data; |
---|
179 | $maxHitsByHour[XC_TYPE_PHP] = max($maxHitsByHour[XC_TYPE_PHP], max($data['hits_by_hour'])); |
---|
180 | if ($phpCacheCount >= 2) { |
---|
181 | calc_total($total, $data); |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|
185 | if ($phpCacheCount >= 2) { |
---|
186 | $total['type'] = XC_TYPE_PHP; |
---|
187 | $total['cache_name'] = _T('Total'); |
---|
188 | $total['cacheid'] = -1; |
---|
189 | $total['gc'] = null; |
---|
190 | $total['istotal'] = true; |
---|
191 | unset($total['compiling']); |
---|
192 | $cacheInfos[] = $total; |
---|
193 | } |
---|
194 | |
---|
195 | $total = array(); |
---|
196 | for ($i = 0; $i < $varCacheCount; $i ++) { |
---|
197 | $data = xcache_info(XC_TYPE_VAR, $i); |
---|
198 | if ($_GET['do'] === 'listvar') { |
---|
199 | $data += xcache_list(XC_TYPE_VAR, $i); |
---|
200 | } |
---|
201 | $data['type'] = XC_TYPE_VAR; |
---|
202 | $data['cache_name'] = "var#$i"; |
---|
203 | $data['cacheid'] = $i; |
---|
204 | $cacheInfos[] = $data; |
---|
205 | $maxHitsByHour[XC_TYPE_VAR] = max($maxHitsByHour[XC_TYPE_VAR], max($data['hits_by_hour'])); |
---|
206 | if ($varCacheCount >= 2) { |
---|
207 | calc_total($total, $data); |
---|
208 | } |
---|
209 | } |
---|
210 | |
---|
211 | if ($varCacheCount >= 2) { |
---|
212 | $total['type'] = XC_TYPE_VAR; |
---|
213 | $total['cache_name'] = _T('Total'); |
---|
214 | $total['cacheid'] = -1; |
---|
215 | $total['gc'] = null; |
---|
216 | $total['istotal'] = true; |
---|
217 | $cacheInfos[] = $total; |
---|
218 | } |
---|
219 | return $cacheInfos; |
---|
220 | } |
---|
221 | // }}} |
---|
222 | function getEntryList() // {{{ |
---|
223 | { |
---|
224 | static $entryList; |
---|
225 | if (isset($entryList)) { |
---|
226 | return $entryList; |
---|
227 | } |
---|
228 | $entryList = array('cache_list' => array(), 'deleted_list' => array()); |
---|
229 | if ($_GET['do'] == 'listphp') { |
---|
230 | $entryList['type'] = XC_TYPE_PHP; |
---|
231 | } |
---|
232 | else { |
---|
233 | $entryList['type'] = XC_TYPE_VAR; |
---|
234 | } |
---|
235 | foreach (getCacheInfos() as $i => $c) { |
---|
236 | if (!empty($c['istotal'])) { |
---|
237 | continue; |
---|
238 | } |
---|
239 | if ($c['type'] == $entryList['type'] && isset($c['cache_list'])) { |
---|
240 | foreach ($c['cache_list'] as $e) { |
---|
241 | $e['cache_name'] = $c['cache_name']; |
---|
242 | $entryList['cache_list'][] = $e; |
---|
243 | } |
---|
244 | foreach ($c['deleted_list'] as $e) { |
---|
245 | $e['cache_name'] = $c['cache_name']; |
---|
246 | $entryList['deleted_list'][] = $e; |
---|
247 | } |
---|
248 | } |
---|
249 | } |
---|
250 | return $entryList; |
---|
251 | } |
---|
252 | // }}} |
---|
253 | |
---|
254 | if (!extension_loaded('XCache')) { |
---|
255 | header("Location: ../diagnosis"); |
---|
256 | exit; |
---|
257 | } |
---|
258 | |
---|
259 | xcache_count(XC_TYPE_PHP); // trigger auth |
---|
260 | xcache_admin_namespace(); |
---|
261 | |
---|
262 | $doTypes = array( |
---|
263 | '' => _T('Summary'), |
---|
264 | 'listphp' => _T('List PHP'), |
---|
265 | 'listvar' => _T('List Var Data'), |
---|
266 | ); |
---|
267 | |
---|
268 | function processPOST() // {{{ |
---|
269 | { |
---|
270 | if (isset($_POST['remove']) && is_array($_POST['remove'])) { |
---|
271 | foreach ($_POST['remove'] as $name) { |
---|
272 | if (is_string($name)) { |
---|
273 | xcache_unset($name); |
---|
274 | } |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | $type = isset($_POST['type']) ? $_POST['type'] : null; |
---|
279 | if ($type != XC_TYPE_PHP && $type != XC_TYPE_VAR) { |
---|
280 | $type = null; |
---|
281 | } |
---|
282 | if (isset($type)) { |
---|
283 | $cacheid = (int) (isset($_POST['cacheid']) ? $_POST['cacheid'] : 0); |
---|
284 | if (isset($_POST['clearcache'])) { |
---|
285 | xcache_clear_cache($type, $cacheid); |
---|
286 | } |
---|
287 | if (isset($_POST['enable'])) { |
---|
288 | xcache_enable_cache($type, $cacheid); |
---|
289 | } |
---|
290 | if (isset($_POST['disable'])) { |
---|
291 | xcache_enable_cache($type, $cacheid, false); |
---|
292 | } |
---|
293 | } |
---|
294 | |
---|
295 | if (isset($_POST['coredump'])) { |
---|
296 | xcache_coredump(); |
---|
297 | } |
---|
298 | } |
---|
299 | // }}} |
---|
300 | |
---|
301 | processPOST(); |
---|
302 | |
---|
303 | if (!isset($_GET['do'])) { |
---|
304 | $_GET['do'] = ''; |
---|
305 | } |
---|
306 | |
---|
307 | switch ($_GET['do']) { |
---|
308 | case 'listphp': |
---|
309 | case 'listvar': |
---|
310 | include "./listentries.tpl.php"; |
---|
311 | break; |
---|
312 | |
---|
313 | default: |
---|
314 | include "./summary.tpl.php"; |
---|
315 | break; |
---|
316 | } |
---|
317 | |
---|
318 | ?> |
---|