| [34] | 1 | <?php |
|---|
| 2 | |
|---|
| [123] | 3 | include("./common.php"); |
|---|
| [34] | 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 | |
|---|
| [144] | 43 | function size($size) |
|---|
| [34] | 44 | { |
|---|
| 45 | $size = (int) $size; |
|---|
| 46 | if ($size < 1024) |
|---|
| [144] | 47 | return number_format($size, 2) . ' b'; |
|---|
| [34] | 48 | |
|---|
| 49 | if ($size < 1048576) |
|---|
| [144] | 50 | return number_format($size / 1024, 2) . ' K'; |
|---|
| [34] | 51 | |
|---|
| [144] | 52 | return number_format($size / 1048576, 2) . ' M'; |
|---|
| [34] | 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); |
|---|
| [60] | 65 | static $name = array('s', 'm', 'h', 'd', 'w', 'M', 'Y'); |
|---|
| [34] | 66 | |
|---|
| 67 | for ($i = 6; $i >= 0; $i --) { |
|---|
| 68 | if ($delta >= $seconds[$i]) { |
|---|
| 69 | $ret = (int) ($delta / $seconds[$i]); |
|---|
| [60] | 70 | return $ret . ' ' . $name[$i]; |
|---|
| [34] | 71 | } |
|---|
| 72 | } |
|---|
| [249] | 73 | |
|---|
| 74 | return '0 s'; |
|---|
| [34] | 75 | } |
|---|
| 76 | |
|---|
| [517] | 77 | function freeblock_to_graph($freeblocks, $size) |
|---|
| 78 | { |
|---|
| [902] | 79 | global $config; |
|---|
| [517] | 80 | |
|---|
| 81 | // cached in static variable |
|---|
| 82 | static $graph_initial; |
|---|
| 83 | if (!isset($graph_initial)) { |
|---|
| [902] | 84 | $graph_initial = array_fill(0, $config['percent_graph_width'], 0); |
|---|
| [517] | 85 | } |
|---|
| 86 | $graph = $graph_initial; |
|---|
| 87 | foreach ($freeblocks as $b) { |
|---|
| [902] | 88 | $begin = $b['offset'] / $size * $config['percent_graph_width']; |
|---|
| 89 | $end = ($b['offset'] + $b['size']) / $size * $config['percent_graph_width']; |
|---|
| [517] | 90 | |
|---|
| [520] | 91 | if ((int) $begin == (int) $end) { |
|---|
| 92 | $v = $end - $begin; |
|---|
| 93 | $graph[(int) $v] += $v - (int) $v; |
|---|
| [517] | 94 | } |
|---|
| [520] | 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 | } |
|---|
| [517] | 102 | } |
|---|
| 103 | $html = array(); |
|---|
| 104 | $c = 255; |
|---|
| 105 | foreach ($graph as $k => $v) { |
|---|
| [902] | 106 | if ($config['percent_graph_type'] != 'free') { |
|---|
| [526] | 107 | $v = 1 - $v; |
|---|
| 108 | } |
|---|
| [517] | 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 | |
|---|
| [522] | 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) { |
|---|
| [552] | 132 | case 'hits_by_hour': |
|---|
| 133 | case 'hits_by_second': |
|---|
| [522] | 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 | |
|---|
| [538] | 154 | function bar_hits_percent($v, $percent, $active) |
|---|
| [522] | 155 | { |
|---|
| 156 | $r = 220 + (int) ($percent * 25); |
|---|
| 157 | $g = $b = 220 - (int) ($percent * 220); |
|---|
| 158 | $percent = (int) ($percent * 100); |
|---|
| [535] | 159 | $a = $active ? ' active' : ''; |
|---|
| [538] | 160 | return '<div title="' . $v . '">' |
|---|
| [535] | 161 | . '<div class="barf' . $a . '" style="height: ' . (100 - $percent) . '%"></div>' |
|---|
| 162 | . '<div class="barv' . $a . '" style="background: rgb(' . "$r,$g,$b" . '); height: ' . $percent . '%"></div>' |
|---|
| [522] | 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 | } |
|---|
| [535] | 177 | $t = (time() / (60 * 60)) % 24; |
|---|
| [522] | 178 | $html = array(); |
|---|
| [535] | 179 | foreach ($hits as $i => $v) { |
|---|
| [538] | 180 | $html[] = bar_hits_percent($v, $v / $max, $i == $t); |
|---|
| [522] | 181 | } |
|---|
| 182 | return implode('', $html); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| [34] | 185 | function switcher($name, $options) |
|---|
| 186 | { |
|---|
| 187 | $n = isset($_GET[$name]) ? $_GET[$name] : null; |
|---|
| 188 | $html = array(); |
|---|
| 189 | foreach ($options as $k => $v) { |
|---|
| [250] | 190 | $html[] = sprintf('<a href="?%s=%s"%s>%s</a>', $name, $k, $k == $n ? ' class="active"' : '', $v); |
|---|
| [34] | 191 | } |
|---|
| [933] | 192 | return implode('', $html); |
|---|
| [34] | 193 | } |
|---|
| 194 | |
|---|
| [1038] | 195 | function th($name, $attrs = null) |
|---|
| 196 | { |
|---|
| 197 | $translated = __($name); |
|---|
| 198 | if ($translated == $name) { |
|---|
| 199 | $translated = "$name|$name"; |
|---|
| 200 | } |
|---|
| 201 | list($text, $title) = explode('|', $translated, 2); |
|---|
| 202 | return sprintf('%s<th%s id="%s" title="%s"><a href="javascript:" onclick="resort(this); return false">%s</a></th>%s' |
|---|
| 203 | , "\t" |
|---|
| 204 | , $attrs ? " $attrs" : "" |
|---|
| 205 | , $name, htmlspecialchars(trim($title)), trim($text) |
|---|
| 206 | , "\n"); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| [133] | 209 | if (!extension_loaded('XCache')) { |
|---|
| 210 | echo '<h1>XCache is not loaded</h1>'; |
|---|
| 211 | ob_start(); |
|---|
| 212 | phpinfo(); |
|---|
| 213 | $info = ob_get_clean(); |
|---|
| 214 | if (preg_match('!<td class="v">(.*\\.ini)!', $info, $m)) { |
|---|
| 215 | echo "Please check $m[1]"; |
|---|
| 216 | } |
|---|
| [421] | 217 | else if (preg_match('!Configuration File \\(php.ini\\) Path *</td><td class="v">([^<]+)!', $info, $m)) { |
|---|
| 218 | echo "Please put a php.ini in $m[1] and load XCache extension"; |
|---|
| 219 | } |
|---|
| [133] | 220 | else { |
|---|
| [142] | 221 | echo "You don't even have a php.ini yet?"; |
|---|
| [133] | 222 | } |
|---|
| 223 | exit; |
|---|
| 224 | } |
|---|
| [34] | 225 | $pcnt = xcache_count(XC_TYPE_PHP); |
|---|
| 226 | $vcnt = xcache_count(XC_TYPE_VAR); |
|---|
| 227 | |
|---|
| [371] | 228 | if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
|---|
| 229 | $remove = @ $_POST['remove']; |
|---|
| 230 | if ($remove && is_array($remove)) { |
|---|
| 231 | foreach ($remove as $name) { |
|---|
| 232 | xcache_unset($name); |
|---|
| 233 | } |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| [147] | 237 | $moduleinfo = null; |
|---|
| [34] | 238 | $type_none = -1; |
|---|
| 239 | if (!isset($_GET['type'])) { |
|---|
| 240 | $_GET['type'] = $type_none; |
|---|
| 241 | } |
|---|
| 242 | $_GET['type'] = $type = (int) $_GET['type']; |
|---|
| 243 | |
|---|
| 244 | // {{{ process clear |
|---|
| 245 | function processClear() |
|---|
| 246 | { |
|---|
| 247 | $type = isset($_POST['type']) ? $_POST['type'] : null; |
|---|
| 248 | if ($type != XC_TYPE_PHP && $type != XC_TYPE_VAR) { |
|---|
| 249 | $type = null; |
|---|
| 250 | } |
|---|
| 251 | if (isset($type)) { |
|---|
| 252 | $cacheid = (int) (isset($_POST['cacheid']) ? $_POST['cacheid'] : 0); |
|---|
| 253 | if (isset($_POST['clearcache'])) { |
|---|
| [521] | 254 | $count = xcache_count($type); |
|---|
| [976] | 255 | if ($cacheid >= 0) { |
|---|
| [521] | 256 | for ($cacheid = 0; $cacheid < $count; $cacheid ++) { |
|---|
| 257 | xcache_clear_cache($type, $cacheid); |
|---|
| 258 | } |
|---|
| 259 | } |
|---|
| 260 | else { |
|---|
| [976] | 261 | xcache_clear_cache($type); |
|---|
| [521] | 262 | } |
|---|
| [34] | 263 | } |
|---|
| 264 | } |
|---|
| 265 | } |
|---|
| 266 | processClear(); |
|---|
| 267 | // }}} |
|---|
| 268 | // {{{ load info/list |
|---|
| 269 | $cacheinfos = array(); |
|---|
| [521] | 270 | $total = array(); |
|---|
| [34] | 271 | for ($i = 0; $i < $pcnt; $i ++) { |
|---|
| 272 | $data = xcache_info(XC_TYPE_PHP, $i); |
|---|
| 273 | if ($type === XC_TYPE_PHP) { |
|---|
| 274 | $data += xcache_list(XC_TYPE_PHP, $i); |
|---|
| 275 | } |
|---|
| 276 | $data['type'] = XC_TYPE_PHP; |
|---|
| 277 | $data['cache_name'] = "php#$i"; |
|---|
| 278 | $data['cacheid'] = $i; |
|---|
| 279 | $cacheinfos[] = $data; |
|---|
| [521] | 280 | if ($pcnt >= 2) { |
|---|
| [522] | 281 | calc_total($total, $data); |
|---|
| [521] | 282 | } |
|---|
| [34] | 283 | } |
|---|
| [521] | 284 | |
|---|
| 285 | if ($pcnt >= 2) { |
|---|
| 286 | $total['type'] = XC_TYPE_PHP; |
|---|
| [1038] | 287 | $total['cache_name'] = _('Total'); |
|---|
| [976] | 288 | $total['cacheid'] = -1; |
|---|
| [521] | 289 | $total['gc'] = null; |
|---|
| 290 | $total['istotal'] = true; |
|---|
| 291 | $cacheinfos[] = $total; |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | $total = array(); |
|---|
| [34] | 295 | for ($i = 0; $i < $vcnt; $i ++) { |
|---|
| 296 | $data = xcache_info(XC_TYPE_VAR, $i); |
|---|
| 297 | if ($type === XC_TYPE_VAR) { |
|---|
| 298 | $data += xcache_list(XC_TYPE_VAR, $i); |
|---|
| 299 | } |
|---|
| 300 | $data['type'] = XC_TYPE_VAR; |
|---|
| 301 | $data['cache_name'] = "var#$i"; |
|---|
| 302 | $data['cacheid'] = $i; |
|---|
| 303 | $cacheinfos[] = $data; |
|---|
| [704] | 304 | if ($vcnt >= 2) { |
|---|
| [522] | 305 | calc_total($total, $data); |
|---|
| [521] | 306 | } |
|---|
| [34] | 307 | } |
|---|
| [521] | 308 | |
|---|
| 309 | if ($vcnt >= 2) { |
|---|
| 310 | $total['type'] = XC_TYPE_VAR; |
|---|
| [1038] | 311 | $total['cache_name'] = _('Total'); |
|---|
| [976] | 312 | $total['cacheid'] = -1; |
|---|
| [521] | 313 | $total['gc'] = null; |
|---|
| 314 | $total['istotal'] = true; |
|---|
| 315 | $cacheinfos[] = $total; |
|---|
| 316 | } |
|---|
| [34] | 317 | // }}} |
|---|
| 318 | // {{{ merge the list |
|---|
| 319 | switch ($type) { |
|---|
| 320 | case XC_TYPE_PHP: |
|---|
| 321 | case XC_TYPE_VAR: |
|---|
| 322 | $cachelist = array('type' => $type, 'cache_list' => array(), 'deleted_list' => array()); |
|---|
| 323 | if ($type == XC_TYPE_VAR) { |
|---|
| 324 | $cachelist['type_name'] = 'var'; |
|---|
| 325 | } |
|---|
| 326 | else { |
|---|
| 327 | $cachelist['type_name'] = 'php'; |
|---|
| 328 | } |
|---|
| 329 | foreach ($cacheinfos as $i => $c) { |
|---|
| [521] | 330 | if (!empty($c['istotal'])) { |
|---|
| 331 | continue; |
|---|
| 332 | } |
|---|
| [34] | 333 | if ($c['type'] == $type && isset($c['cache_list'])) { |
|---|
| 334 | foreach ($c['cache_list'] as $e) { |
|---|
| 335 | $e['cache_name'] = $c['cache_name']; |
|---|
| 336 | $cachelist['cache_list'][] = $e; |
|---|
| 337 | } |
|---|
| 338 | foreach ($c['deleted_list'] as $e) { |
|---|
| 339 | $e['cache_name'] = $c['cache_name']; |
|---|
| 340 | $cachelist['deleted_list'][] = $e; |
|---|
| 341 | } |
|---|
| 342 | } |
|---|
| 343 | } |
|---|
| 344 | if ($type == XC_TYPE_PHP) { |
|---|
| 345 | $inodes = array(); |
|---|
| [84] | 346 | $haveinode = false; |
|---|
| [34] | 347 | foreach ($cachelist['cache_list'] as $e) { |
|---|
| [868] | 348 | if (isset($e['file_inode'])) { |
|---|
| [114] | 349 | $haveinode = true; |
|---|
| [868] | 350 | break; |
|---|
| [84] | 351 | } |
|---|
| [114] | 352 | } |
|---|
| 353 | if (!$haveinode) { |
|---|
| 354 | foreach ($cachelist['deleted_list'] as $e) { |
|---|
| [868] | 355 | if (isset($e['file_inode'])) { |
|---|
| [114] | 356 | $haveinode = true; |
|---|
| [868] | 357 | break; |
|---|
| [114] | 358 | } |
|---|
| [34] | 359 | } |
|---|
| 360 | } |
|---|
| 361 | } |
|---|
| 362 | unset($data); |
|---|
| 363 | break; |
|---|
| 364 | |
|---|
| 365 | default: |
|---|
| 366 | $_GET['type'] = $type_none; |
|---|
| 367 | $cachelist = array(); |
|---|
| [147] | 368 | ob_start(); |
|---|
| 369 | phpinfo(INFO_MODULES); |
|---|
| 370 | $moduleinfo = ob_get_clean(); |
|---|
| [1026] | 371 | if (preg_match_all('!XCache[^<]*</a></h2>(.*?)<h2>!is', $moduleinfo, $m)) { |
|---|
| 372 | $moduleinfo = implode('', $m[1]); |
|---|
| [147] | 373 | } |
|---|
| 374 | else { |
|---|
| 375 | $moduleinfo = null; |
|---|
| 376 | } |
|---|
| [34] | 377 | break; |
|---|
| 378 | } |
|---|
| 379 | // }}} |
|---|
| 380 | |
|---|
| 381 | $type_php = XC_TYPE_PHP; |
|---|
| 382 | $type_var = XC_TYPE_VAR; |
|---|
| [1038] | 383 | $types = array($type_none => _('Statistics'), $type_php => _('List PHP'), $type_var => _('List Var Data')); |
|---|
| [934] | 384 | $php_version = phpversion(); |
|---|
| [107] | 385 | $xcache_version = XCACHE_VERSION; |
|---|
| 386 | $xcache_modules = XCACHE_MODULES; |
|---|
| [34] | 387 | |
|---|
| 388 | include("xcache.tpl.php"); |
|---|
| 389 | |
|---|
| 390 | ?> |
|---|