| 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 ? ' active' : ''; |
|---|
| 88 | return '<div title="' . $v . '">' |
|---|
| 89 | . '<div class="barf' . $a . '" style="height: ' . (100 - $percent) . '%"></div>' |
|---|
| 90 | . '<div class="barv' . $a . '" style="background: rgb(' . "$r,$g,$b" . '); height: ' . $percent . '%"></div>' |
|---|
| 91 | . '</div>'; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | function get_cache_hits_graph($ci, $key) |
|---|
| 95 | { |
|---|
| 96 | if ($ci['cacheid'] == -1) { |
|---|
| 97 | $max = max($ci[$key]); |
|---|
| 98 | } |
|---|
| 99 | else { |
|---|
| 100 | $max = $GLOBALS['maxhits_by_hour'][$ci['type']]; |
|---|
| 101 | } |
|---|
| 102 | if (!$max) { |
|---|
| 103 | $max = 1; |
|---|
| 104 | } |
|---|
| 105 | $t = (time() / (60 * 60)) % 24; |
|---|
| 106 | $html = array(); |
|---|
| 107 | foreach ($ci[$key] as $i => $v) { |
|---|
| 108 | $html[] = bar_hits_percent($v, $v / $max, $i == $t); |
|---|
| 109 | } |
|---|
| 110 | return implode('', $html); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | $module = "cacher"; |
|---|
| 114 | if (!extension_loaded('XCache')) { |
|---|
| 115 | include("../common/header.tpl.php"); |
|---|
| 116 | echo '<h1>XCache is not loaded</h1>'; |
|---|
| 117 | ob_start(); |
|---|
| 118 | phpinfo(INFO_GENERAL); |
|---|
| 119 | $info = ob_get_clean(); |
|---|
| 120 | if (preg_match_all("!<tr>[^<]*<td[^>]*>[^<]*(?:Configuration|ini|Server API)[^<]*</td>[^<]*<td[^>]*>[^<]*</td>[^<]*</tr>!s", $info, $m)) { |
|---|
| 121 | echo '<div class="phpinfo">'; |
|---|
| 122 | echo 'PHP Info'; |
|---|
| 123 | echo '<table>'; |
|---|
| 124 | echo implode('', $m[0]); |
|---|
| 125 | echo '</table>'; |
|---|
| 126 | echo '</div>'; |
|---|
| 127 | } |
|---|
| 128 | if (preg_match('!<td class="v">(.*?\\.ini)!', $info, $m)) { |
|---|
| 129 | echo "Please check $m[1]"; |
|---|
| 130 | } |
|---|
| 131 | else if (preg_match('!Configuration File \\(php.ini\\) Path *</td><td class="v">([^<]+)!', $info, $m)) { |
|---|
| 132 | echo "Please put a php.ini in $m[1] and load XCache extension"; |
|---|
| 133 | } |
|---|
| 134 | else { |
|---|
| 135 | echo "You don't even have a php.ini yet?"; |
|---|
| 136 | } |
|---|
| 137 | echo "(See above)"; |
|---|
| 138 | include("../common/footer.tpl.php"); |
|---|
| 139 | exit; |
|---|
| 140 | } |
|---|
| 141 | $pcnt = xcache_count(XC_TYPE_PHP); |
|---|
| 142 | $vcnt = xcache_count(XC_TYPE_VAR); |
|---|
| 143 | |
|---|
| 144 | if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
|---|
| 145 | $remove = @ $_POST['remove']; |
|---|
| 146 | if ($remove && is_array($remove)) { |
|---|
| 147 | foreach ($remove as $name) { |
|---|
| 148 | xcache_unset($name); |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | $moduleinfo = null; |
|---|
| 154 | $type_none = -1; |
|---|
| 155 | if (!isset($_GET['type'])) { |
|---|
| 156 | $_GET['type'] = $type_none; |
|---|
| 157 | } |
|---|
| 158 | $_GET['type'] = $type = (int) $_GET['type']; |
|---|
| 159 | |
|---|
| 160 | // {{{ process clear |
|---|
| 161 | function processClear() |
|---|
| 162 | { |
|---|
| 163 | $type = isset($_POST['type']) ? $_POST['type'] : null; |
|---|
| 164 | if ($type != XC_TYPE_PHP && $type != XC_TYPE_VAR) { |
|---|
| 165 | $type = null; |
|---|
| 166 | } |
|---|
| 167 | if (isset($type)) { |
|---|
| 168 | $cacheid = (int) (isset($_POST['cacheid']) ? $_POST['cacheid'] : 0); |
|---|
| 169 | if (isset($_POST['clearcache'])) { |
|---|
| 170 | $count = xcache_count($type); |
|---|
| 171 | if ($cacheid >= 0) { |
|---|
| 172 | for ($cacheid = 0; $cacheid < $count; $cacheid ++) { |
|---|
| 173 | xcache_clear_cache($type, $cacheid); |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | else { |
|---|
| 177 | xcache_clear_cache($type); |
|---|
| 178 | } |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | processClear(); |
|---|
| 183 | // }}} |
|---|
| 184 | // {{{ load info/list |
|---|
| 185 | $cacheinfos = array(); |
|---|
| 186 | $total = array(); |
|---|
| 187 | $maxhits_by_hour = array(0, 0); |
|---|
| 188 | for ($i = 0; $i < $pcnt; $i ++) { |
|---|
| 189 | $data = xcache_info(XC_TYPE_PHP, $i); |
|---|
| 190 | if ($type === XC_TYPE_PHP) { |
|---|
| 191 | $data += xcache_list(XC_TYPE_PHP, $i); |
|---|
| 192 | } |
|---|
| 193 | $data['type'] = XC_TYPE_PHP; |
|---|
| 194 | $data['cache_name'] = "php#$i"; |
|---|
| 195 | $data['cacheid'] = $i; |
|---|
| 196 | $cacheinfos[] = $data; |
|---|
| 197 | $maxhits_by_hour[XC_TYPE_PHP] = max($maxhits_by_hour[XC_TYPE_PHP], max($data['hits_by_hour'])); |
|---|
| 198 | if ($pcnt >= 2) { |
|---|
| 199 | calc_total($total, $data); |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | if ($pcnt >= 2) { |
|---|
| 204 | $total['type'] = XC_TYPE_PHP; |
|---|
| 205 | $total['cache_name'] = _('Total'); |
|---|
| 206 | $total['cacheid'] = -1; |
|---|
| 207 | $total['gc'] = null; |
|---|
| 208 | $total['istotal'] = true; |
|---|
| 209 | $cacheinfos[] = $total; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | $total = array(); |
|---|
| 213 | for ($i = 0; $i < $vcnt; $i ++) { |
|---|
| 214 | $data = xcache_info(XC_TYPE_VAR, $i); |
|---|
| 215 | if ($type === XC_TYPE_VAR) { |
|---|
| 216 | $data += xcache_list(XC_TYPE_VAR, $i); |
|---|
| 217 | } |
|---|
| 218 | $data['type'] = XC_TYPE_VAR; |
|---|
| 219 | $data['cache_name'] = "var#$i"; |
|---|
| 220 | $data['cacheid'] = $i; |
|---|
| 221 | $cacheinfos[] = $data; |
|---|
| 222 | $maxhits_by_hour[XC_TYPE_VAR] = max($maxhits_by_hour[XC_TYPE_VAR], max($data['hits_by_hour'])); |
|---|
| 223 | if ($vcnt >= 2) { |
|---|
| 224 | calc_total($total, $data); |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | if ($vcnt >= 2) { |
|---|
| 229 | $total['type'] = XC_TYPE_VAR; |
|---|
| 230 | $total['cache_name'] = _('Total'); |
|---|
| 231 | $total['cacheid'] = -1; |
|---|
| 232 | $total['gc'] = null; |
|---|
| 233 | $total['istotal'] = true; |
|---|
| 234 | $cacheinfos[] = $total; |
|---|
| 235 | } |
|---|
| 236 | // }}} |
|---|
| 237 | // {{{ merge the list |
|---|
| 238 | switch ($type) { |
|---|
| 239 | case XC_TYPE_PHP: |
|---|
| 240 | case XC_TYPE_VAR: |
|---|
| 241 | $cachelist = array('type' => $type, 'cache_list' => array(), 'deleted_list' => array()); |
|---|
| 242 | if ($type == XC_TYPE_VAR) { |
|---|
| 243 | $cachelist['type_name'] = 'var'; |
|---|
| 244 | } |
|---|
| 245 | else { |
|---|
| 246 | $cachelist['type_name'] = 'php'; |
|---|
| 247 | } |
|---|
| 248 | foreach ($cacheinfos as $i => $c) { |
|---|
| 249 | if (!empty($c['istotal'])) { |
|---|
| 250 | continue; |
|---|
| 251 | } |
|---|
| 252 | if ($c['type'] == $type && isset($c['cache_list'])) { |
|---|
| 253 | foreach ($c['cache_list'] as $e) { |
|---|
| 254 | $e['cache_name'] = $c['cache_name']; |
|---|
| 255 | $cachelist['cache_list'][] = $e; |
|---|
| 256 | } |
|---|
| 257 | foreach ($c['deleted_list'] as $e) { |
|---|
| 258 | $e['cache_name'] = $c['cache_name']; |
|---|
| 259 | $cachelist['deleted_list'][] = $e; |
|---|
| 260 | } |
|---|
| 261 | } |
|---|
| 262 | } |
|---|
| 263 | if ($type == XC_TYPE_PHP) { |
|---|
| 264 | $inodes = array(); |
|---|
| 265 | $haveinode = false; |
|---|
| 266 | foreach ($cachelist['cache_list'] as $e) { |
|---|
| 267 | if (isset($e['file_inode'])) { |
|---|
| 268 | $haveinode = true; |
|---|
| 269 | break; |
|---|
| 270 | } |
|---|
| 271 | } |
|---|
| 272 | if (!$haveinode) { |
|---|
| 273 | foreach ($cachelist['deleted_list'] as $e) { |
|---|
| 274 | if (isset($e['file_inode'])) { |
|---|
| 275 | $haveinode = true; |
|---|
| 276 | break; |
|---|
| 277 | } |
|---|
| 278 | } |
|---|
| 279 | } |
|---|
| 280 | } |
|---|
| 281 | unset($data); |
|---|
| 282 | break; |
|---|
| 283 | |
|---|
| 284 | default: |
|---|
| 285 | $_GET['type'] = $type_none; |
|---|
| 286 | $cachelist = array(); |
|---|
| 287 | ob_start(); |
|---|
| 288 | phpinfo(INFO_MODULES); |
|---|
| 289 | $moduleinfo = ob_get_clean(); |
|---|
| 290 | if (preg_match_all('!(XCache[^<>]*)</a></h2>(.*?)<h2>!is', $moduleinfo, $m)) { |
|---|
| 291 | $moduleinfo = array(); |
|---|
| 292 | foreach ($m[1] as $i => $dummy) { |
|---|
| 293 | $moduleinfo[] = '<h3>' . trim($m[1][$i]) . '</h3>'; |
|---|
| 294 | $moduleinfo[] = str_replace('<br />', '', trim($m[2][$i])); |
|---|
| 295 | } |
|---|
| 296 | $moduleinfo = implode('', $moduleinfo); |
|---|
| 297 | } |
|---|
| 298 | else { |
|---|
| 299 | $moduleinfo = null; |
|---|
| 300 | } |
|---|
| 301 | break; |
|---|
| 302 | } |
|---|
| 303 | // }}} |
|---|
| 304 | |
|---|
| 305 | $type_php = XC_TYPE_PHP; |
|---|
| 306 | $type_var = XC_TYPE_VAR; |
|---|
| 307 | $listTypes = array($type_none => _('Statistics'), $type_php => _('List PHP'), $type_var => _('List Var Data')); |
|---|
| 308 | |
|---|
| 309 | include("cacher.tpl.php"); |
|---|
| 310 | |
|---|
| 311 | ?> |
|---|