| [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 | |
|---|
| [133] | 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 | } |
|---|
| [421] | 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 | } |
|---|
| [133] | 206 | else { |
|---|
| [142] | 207 | echo "You don't even have a php.ini yet?"; |
|---|
| [133] | 208 | } |
|---|
| 209 | exit; |
|---|
| 210 | } |
|---|
| [34] | 211 | $pcnt = xcache_count(XC_TYPE_PHP); |
|---|
| 212 | $vcnt = xcache_count(XC_TYPE_VAR); |
|---|
| 213 | |
|---|
| [371] | 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 | |
|---|
| [147] | 223 | $moduleinfo = null; |
|---|
| [34] | 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'])) { |
|---|
| [521] | 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 | } |
|---|
| [34] | 249 | } |
|---|
| 250 | } |
|---|
| 251 | } |
|---|
| 252 | processClear(); |
|---|
| 253 | // }}} |
|---|
| 254 | // {{{ load info/list |
|---|
| 255 | $cacheinfos = array(); |
|---|
| [521] | 256 | $total = array(); |
|---|
| [34] | 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; |
|---|
| [521] | 266 | if ($pcnt >= 2) { |
|---|
| [522] | 267 | calc_total($total, $data); |
|---|
| [521] | 268 | } |
|---|
| [34] | 269 | } |
|---|
| [521] | 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(); |
|---|
| [34] | 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; |
|---|
| [704] | 290 | if ($vcnt >= 2) { |
|---|
| [522] | 291 | calc_total($total, $data); |
|---|
| [521] | 292 | } |
|---|
| [34] | 293 | } |
|---|
| [521] | 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 | } |
|---|
| [34] | 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) { |
|---|
| [521] | 316 | if (!empty($c['istotal'])) { |
|---|
| 317 | continue; |
|---|
| 318 | } |
|---|
| [34] | 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(); |
|---|
| [84] | 332 | $haveinode = false; |
|---|
| [34] | 333 | foreach ($cachelist['cache_list'] as $e) { |
|---|
| [868] | 334 | if (isset($e['file_inode'])) { |
|---|
| [114] | 335 | $haveinode = true; |
|---|
| [868] | 336 | break; |
|---|
| [84] | 337 | } |
|---|
| [114] | 338 | } |
|---|
| 339 | if (!$haveinode) { |
|---|
| 340 | foreach ($cachelist['deleted_list'] as $e) { |
|---|
| [868] | 341 | if (isset($e['file_inode'])) { |
|---|
| [114] | 342 | $haveinode = true; |
|---|
| [868] | 343 | break; |
|---|
| [114] | 344 | } |
|---|
| [34] | 345 | } |
|---|
| 346 | } |
|---|
| 347 | } |
|---|
| 348 | unset($data); |
|---|
| 349 | break; |
|---|
| 350 | |
|---|
| 351 | default: |
|---|
| 352 | $_GET['type'] = $type_none; |
|---|
| 353 | $cachelist = array(); |
|---|
| [147] | 354 | ob_start(); |
|---|
| 355 | phpinfo(INFO_MODULES); |
|---|
| 356 | $moduleinfo = ob_get_clean(); |
|---|
| [382] | 357 | if (preg_match('!XCache</a></h2>(.*?)<h2>!is', $moduleinfo, $m)) { |
|---|
| [147] | 358 | $moduleinfo = $m[1]; |
|---|
| 359 | } |
|---|
| 360 | else { |
|---|
| 361 | $moduleinfo = null; |
|---|
| 362 | } |
|---|
| [34] | 363 | break; |
|---|
| 364 | } |
|---|
| 365 | // }}} |
|---|
| 366 | |
|---|
| 367 | $type_php = XC_TYPE_PHP; |
|---|
| 368 | $type_var = XC_TYPE_VAR; |
|---|
| [123] | 369 | $types = array($type_none => _T('Statistics'), $type_php => _T('List PHP'), $type_var => _T('List Var Data')); |
|---|
| [934] | 370 | $php_version = phpversion(); |
|---|
| [107] | 371 | $xcache_version = XCACHE_VERSION; |
|---|
| 372 | $xcache_modules = XCACHE_MODULES; |
|---|
| [34] | 373 | |
|---|
| 374 | include("xcache.tpl.php"); |
|---|
| 375 | |
|---|
| 376 | ?> |
|---|