Advertisement
Guest User

apc.php

a guest
Jul 22nd, 2011
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 46.35 KB | None | 0 0
  1. <?php
  2. /*
  3.   +----------------------------------------------------------------------+
  4.   | APC                                                                  |
  5.   +----------------------------------------------------------------------+
  6.   | Copyright (c) 2006-2011 The PHP Group                                |
  7.   +----------------------------------------------------------------------+
  8.   | This source file is subject to version 3.01 of the PHP license,      |
  9.   | that is bundled with this package in the file LICENSE, and is        |
  10.   | available through the world-wide-web at the following url:           |
  11.   | http://www.php.net/license/3_01.txt                                  |
  12.   | If you did not receive a copy of the PHP license and are unable to   |
  13.   | obtain it through the world-wide-web, please send a note to          |
  14.   | license@php.net so we can mail you a copy immediately.               |
  15.   +----------------------------------------------------------------------+
  16.   | Authors: Ralf Becker <beckerr@php.net>                               |
  17.   |          Rasmus Lerdorf <rasmus@php.net>                             |
  18.   |          Ilia Alshanetsky <ilia@prohost.org>                         |
  19.   +----------------------------------------------------------------------+
  20.  
  21.    All other licensing and usage conditions are those of the PHP Group.
  22.  
  23.  */
  24.  
  25. $VERSION='$Id$';
  26.  
  27. ////////// READ OPTIONAL CONFIGURATION FILE ////////////
  28. if (file_exists("apc.conf.php")) include("apc.conf.php");
  29. ////////////////////////////////////////////////////////
  30.  
  31. ////////// BEGIN OF DEFAULT CONFIG AREA ///////////////////////////////////////////////////////////
  32.  
  33. defaults('USE_AUTHENTICATION',1);           // Use (internal) authentication - best choice if
  34.                                             // no other authentication is available
  35.                                             // If set to 0:
  36.                                             //  There will be no further authentication. You
  37.                                             //  will have to handle this by yourself!
  38.                                             // If set to 1:
  39.                                             //  You need to change ADMIN_PASSWORD to make
  40.                                             //  this work!
  41. defaults('ADMIN_USERNAME','apc');           // Admin Username
  42. defaults('ADMIN_PASSWORD','password');      // Admin Password - CHANGE THIS TO ENABLE!!!
  43.  
  44. // (beckerr) I'm using a clear text password here, because I've no good idea how to let
  45. //           users generate a md5 or crypt password in a easy way to fill it in above
  46.  
  47. //defaults('DATE_FORMAT', "d.m.Y H:i:s");   // German
  48. defaults('DATE_FORMAT', 'Y/m/d H:i:s');     // US
  49.  
  50. defaults('GRAPH_SIZE',200);                 // Image size
  51.  
  52. //defaults('PROXY', 'tcp://127.0.0.1:8080');
  53.  
  54. ////////// END OF DEFAULT CONFIG AREA /////////////////////////////////////////////////////////////
  55.  
  56.  
  57. // "define if not defined"
  58. function defaults($d,$v) {
  59.     if (!defined($d)) define($d,$v); // or just @define(...)
  60. }
  61.  
  62. // rewrite $PHP_SELF to block XSS attacks
  63. //
  64. $PHP_SELF= isset($_SERVER['PHP_SELF']) ? htmlentities(strip_tags($_SERVER['PHP_SELF'],''), ENT_QUOTES, 'UTF-8') : '';
  65. $time = time();
  66. $host = php_uname('n');
  67. if($host) { $host = '('.$host.')'; }
  68. if (isset($_SERVER['SERVER_ADDR'])) {
  69.   $host .= ' ('.$_SERVER['SERVER_ADDR'].')';
  70. }
  71.  
  72. // operation constants
  73. define('OB_HOST_STATS',1);
  74. define('OB_SYS_CACHE',2);
  75. define('OB_USER_CACHE',3);
  76. define('OB_SYS_CACHE_DIR',4);
  77. define('OB_VERSION_CHECK',9);
  78.  
  79. // check validity of input variables
  80. $vardom=array(
  81.     'OB'    => '/^\d+$/',           // operational mode switch
  82.     'CC'    => '/^[01]$/',          // clear cache requested
  83.     'DU'    => '/^.*$/',            // Delete User Key
  84.     'SH'    => '/^[a-z0-9]+$/',     // shared object description
  85.  
  86.     'IMG'   => '/^[123]$/',         // image to generate
  87.     'LO'    => '/^1$/',             // login requested
  88.  
  89.     'COUNT' => '/^\d+$/',           // number of line displayed in list
  90.     'SCOPE' => '/^[AD]$/',          // list view scope
  91.     'SORT1' => '/^[AHSMCDTZ]$/',    // first sort key
  92.     'SORT2' => '/^[DA]$/',          // second sort key
  93.     'AGGR'  => '/^\d+$/',           // aggregation by dir level
  94.     'SEARCH'    => '~^[a-zA-Z0-1/_.-]*$~'           // aggregation by dir level
  95. );
  96.  
  97. // default cache mode
  98. $cache_mode='opcode';
  99.  
  100. // cache scope
  101. $scope_list=array(
  102.     'A' => 'cache_list',
  103.     'D' => 'deleted_list'
  104. );
  105.  
  106. // handle POST and GET requests
  107. if (empty($_REQUEST)) {
  108.     if (!empty($_GET) && !empty($_POST)) {
  109.         $_REQUEST = array_merge($_GET, $_POST);
  110.     } else if (!empty($_GET)) {
  111.         $_REQUEST = $_GET;
  112.     } else if (!empty($_POST)) {
  113.         $_REQUEST = $_POST;
  114.     } else {
  115.         $_REQUEST = array();
  116.     }
  117. }
  118.  
  119. // check parameter syntax
  120. foreach($vardom as $var => $dom) {
  121.     if (!isset($_REQUEST[$var])) {
  122.         $MYREQUEST[$var]=NULL;
  123.     } else if (!is_array($_REQUEST[$var]) && preg_match($dom.'D',$_REQUEST[$var])) {
  124.         $MYREQUEST[$var]=$_REQUEST[$var];
  125.     } else {
  126.         $MYREQUEST[$var]=$_REQUEST[$var]=NULL;
  127.     }
  128. }
  129.  
  130. // check parameter sematics
  131. if (empty($MYREQUEST['SCOPE'])) $MYREQUEST['SCOPE']="A";
  132. if (empty($MYREQUEST['SORT1'])) $MYREQUEST['SORT1']="H";
  133. if (empty($MYREQUEST['SORT2'])) $MYREQUEST['SORT2']="D";
  134. if (empty($MYREQUEST['OB']))    $MYREQUEST['OB']=OB_HOST_STATS;
  135. if (!isset($MYREQUEST['COUNT'])) $MYREQUEST['COUNT']=20;
  136. if (!isset($scope_list[$MYREQUEST['SCOPE']])) $MYREQUEST['SCOPE']='A';
  137.  
  138. $MY_SELF=
  139.     "$PHP_SELF".
  140.     "?SCOPE=".$MYREQUEST['SCOPE'].
  141.     "&SORT1=".$MYREQUEST['SORT1'].
  142.     "&SORT2=".$MYREQUEST['SORT2'].
  143.     "&COUNT=".$MYREQUEST['COUNT'];
  144. $MY_SELF_WO_SORT=
  145.     "$PHP_SELF".
  146.     "?SCOPE=".$MYREQUEST['SCOPE'].
  147.     "&COUNT=".$MYREQUEST['COUNT'];
  148.  
  149. // authentication needed?
  150. //
  151. if (!USE_AUTHENTICATION) {
  152.     $AUTHENTICATED=1;
  153. } else {
  154.     $AUTHENTICATED=0;
  155.     if (ADMIN_PASSWORD!='password' && ($MYREQUEST['LO'] == 1 || isset($_SERVER['PHP_AUTH_USER']))) {
  156.  
  157.         if (!isset($_SERVER['PHP_AUTH_USER']) ||
  158.             !isset($_SERVER['PHP_AUTH_PW']) ||
  159.             $_SERVER['PHP_AUTH_USER'] != ADMIN_USERNAME ||
  160.             $_SERVER['PHP_AUTH_PW'] != ADMIN_PASSWORD) {
  161.             Header("WWW-Authenticate: Basic realm=\"APC Login\"");
  162.             Header("HTTP/1.0 401 Unauthorized");
  163.  
  164.             echo <<<EOB
  165.                 <html><body>
  166.                 <h1>Rejected!</h1>
  167.                 <big>Wrong Username or Password!</big><br/>&nbsp;<br/>&nbsp;
  168.                 <big><a href='$PHP_SELF?OB={$MYREQUEST['OB']}'>Continue...</a></big>
  169.                 </body></html>
  170. EOB;
  171.             exit;
  172.            
  173.         } else {
  174.             $AUTHENTICATED=1;
  175.         }
  176.     }
  177. }
  178.    
  179. // select cache mode
  180. if ($AUTHENTICATED && $MYREQUEST['OB'] == OB_USER_CACHE) {
  181.     $cache_mode='user';
  182. }
  183. // clear cache
  184. if ($AUTHENTICATED && isset($MYREQUEST['CC']) && $MYREQUEST['CC']) {
  185.     apc_clear_cache($cache_mode);
  186. }
  187.  
  188. if ($AUTHENTICATED && !empty($MYREQUEST['DU'])) {
  189.     apc_delete($MYREQUEST['DU']);
  190. }
  191.  
  192. if(!function_exists('apc_cache_info') || !($cache=@apc_cache_info($cache_mode))) {
  193.     echo "No cache info available.  APC does not appear to be running.";
  194.   exit;
  195. }
  196.  
  197. $cache_user = apc_cache_info('user', 1);  
  198. $mem=apc_sma_info();
  199. if(!$cache['num_hits']) { $cache['num_hits']=1; $time++; }  // Avoid division by 0 errors on a cache clear
  200.  
  201. // don't cache this page
  202. //
  203. header("Cache-Control: no-store, no-cache, must-revalidate");  // HTTP/1.1
  204. header("Cache-Control: post-check=0, pre-check=0", false);
  205. header("Pragma: no-cache");                                    // HTTP/1.0
  206.  
  207. function duration($ts) {
  208.     global $time;
  209.     $years = (int)((($time - $ts)/(7*86400))/52.177457);
  210.     $rem = (int)(($time-$ts)-($years * 52.177457 * 7 * 86400));
  211.     $weeks = (int)(($rem)/(7*86400));
  212.     $days = (int)(($rem)/86400) - $weeks*7;
  213.     $hours = (int)(($rem)/3600) - $days*24 - $weeks*7*24;
  214.     $mins = (int)(($rem)/60) - $hours*60 - $days*24*60 - $weeks*7*24*60;
  215.     $str = '';
  216.     if($years==1) $str .= "$years year, ";
  217.     if($years>1) $str .= "$years years, ";
  218.     if($weeks==1) $str .= "$weeks week, ";
  219.     if($weeks>1) $str .= "$weeks weeks, ";
  220.     if($days==1) $str .= "$days day,";
  221.     if($days>1) $str .= "$days days,";
  222.     if($hours == 1) $str .= " $hours hour and";
  223.     if($hours>1) $str .= " $hours hours and";
  224.     if($mins == 1) $str .= " 1 minute";
  225.     else $str .= " $mins minutes";
  226.     return $str;
  227. }
  228.  
  229. // create graphics
  230. //
  231. function graphics_avail() {
  232.     return extension_loaded('gd');
  233. }
  234. if (isset($MYREQUEST['IMG']))
  235. {
  236.     if (!graphics_avail()) {
  237.         exit(0);
  238.     }
  239.  
  240.     function fill_arc($im, $centerX, $centerY, $diameter, $start, $end, $color1,$color2,$text='',$placeindex=0) {
  241.         $r=$diameter/2;
  242.         $w=deg2rad((360+$start+($end-$start)/2)%360);
  243.  
  244.        
  245.         if (function_exists("imagefilledarc")) {
  246.             // exists only if GD 2.0.1 is avaliable
  247.             imagefilledarc($im, $centerX+1, $centerY+1, $diameter, $diameter, $start, $end, $color1, IMG_ARC_PIE);
  248.             imagefilledarc($im, $centerX, $centerY, $diameter, $diameter, $start, $end, $color2, IMG_ARC_PIE);
  249.             imagefilledarc($im, $centerX, $centerY, $diameter, $diameter, $start, $end, $color1, IMG_ARC_NOFILL|IMG_ARC_EDGED);
  250.         } else {
  251.             imagearc($im, $centerX, $centerY, $diameter, $diameter, $start, $end, $color2);
  252.             imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($start)) * $r, $centerY + sin(deg2rad($start)) * $r, $color2);
  253.             imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($start+1)) * $r, $centerY + sin(deg2rad($start)) * $r, $color2);
  254.             imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($end-1))   * $r, $centerY + sin(deg2rad($end))   * $r, $color2);
  255.             imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($end))   * $r, $centerY + sin(deg2rad($end))   * $r, $color2);
  256.             imagefill($im,$centerX + $r*cos($w)/2, $centerY + $r*sin($w)/2, $color2);
  257.         }
  258.         if ($text) {
  259.             if ($placeindex>0) {
  260.                 imageline($im,$centerX + $r*cos($w)/2, $centerY + $r*sin($w)/2,$diameter, $placeindex*12,$color1);
  261.                 imagestring($im,4,$diameter, $placeindex*12,$text,$color1);
  262.                
  263.             } else {
  264.                 imagestring($im,4,$centerX + $r*cos($w)/2, $centerY + $r*sin($w)/2,$text,$color1);
  265.             }
  266.         }
  267.     }
  268.  
  269.     function text_arc($im, $centerX, $centerY, $diameter, $start, $end, $color1,$text,$placeindex=0) {
  270.         $r=$diameter/2;
  271.         $w=deg2rad((360+$start+($end-$start)/2)%360);
  272.  
  273.         if ($placeindex>0) {
  274.             imageline($im,$centerX + $r*cos($w)/2, $centerY + $r*sin($w)/2,$diameter, $placeindex*12,$color1);
  275.             imagestring($im,4,$diameter, $placeindex*12,$text,$color1);
  276.                
  277.         } else {
  278.             imagestring($im,4,$centerX + $r*cos($w)/2, $centerY + $r*sin($w)/2,$text,$color1);
  279.         }
  280.     }
  281.    
  282.     function fill_box($im, $x, $y, $w, $h, $color1, $color2,$text='',$placeindex='') {
  283.         global $col_black;
  284.         $x1=$x+$w-1;
  285.         $y1=$y+$h-1;
  286.  
  287.         imagerectangle($im, $x, $y1, $x1+1, $y+1, $col_black);
  288.         if($y1>$y) imagefilledrectangle($im, $x, $y, $x1, $y1, $color2);
  289.         else imagefilledrectangle($im, $x, $y1, $x1, $y, $color2);
  290.         imagerectangle($im, $x, $y1, $x1, $y, $color1);
  291.         if ($text) {
  292.             if ($placeindex>0) {
  293.            
  294.                 if ($placeindex<16)
  295.                 {
  296.                     $px=5;
  297.                     $py=$placeindex*12+6;
  298.                     imagefilledrectangle($im, $px+90, $py+3, $px+90-4, $py-3, $color2);
  299.                     imageline($im,$x,$y+$h/2,$px+90,$py,$color2);
  300.                     imagestring($im,2,$px,$py-6,$text,$color1);
  301.                    
  302.                 } else {
  303.                     if ($placeindex<31) {
  304.                         $px=$x+40*2;
  305.                         $py=($placeindex-15)*12+6;
  306.                     } else {
  307.                         $px=$x+40*2+100*intval(($placeindex-15)/15);
  308.                         $py=($placeindex%15)*12+6;
  309.                     }
  310.                     imagefilledrectangle($im, $px, $py+3, $px-4, $py-3, $color2);
  311.                     imageline($im,$x+$w,$y+$h/2,$px,$py,$color2);
  312.                     imagestring($im,2,$px+2,$py-6,$text,$color1);  
  313.                 }
  314.             } else {
  315.                 imagestring($im,4,$x+5,$y1-16,$text,$color1);
  316.             }
  317.         }
  318.     }
  319.  
  320.  
  321.     $size = GRAPH_SIZE; // image size
  322.     if ($MYREQUEST['IMG']==3)
  323.         $image = imagecreate(2*$size+150, $size+10);
  324.     else
  325.         $image = imagecreate($size+50, $size+10);
  326.  
  327.     $col_white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
  328.     $col_red   = imagecolorallocate($image, 0xD0, 0x60,  0x30);
  329.     $col_green = imagecolorallocate($image, 0x60, 0xF0, 0x60);
  330.     $col_black = imagecolorallocate($image,   0,   0,   0);
  331.     imagecolortransparent($image,$col_white);
  332.  
  333.     switch ($MYREQUEST['IMG']) {
  334.    
  335.     case 1:
  336.         $s=$mem['num_seg']*$mem['seg_size'];
  337.         $a=$mem['avail_mem'];
  338.         $x=$y=$size/2;
  339.         $fuzz = 0.000001;
  340.  
  341.         // This block of code creates the pie chart.  It is a lot more complex than you
  342.         // would expect because we try to visualize any memory fragmentation as well.
  343.         $angle_from = 0;
  344.         $string_placement=array();
  345.         for($i=0; $i<$mem['num_seg']; $i++) {  
  346.             $ptr = 0;
  347.             $free = $mem['block_lists'][$i];
  348.             uasort($free, 'block_sort');
  349.             foreach($free as $block) {
  350.                 if($block['offset']!=$ptr) {       // Used block
  351.                     $angle_to = $angle_from+($block['offset']-$ptr)/$s;
  352.                     if(($angle_to+$fuzz)>1) $angle_to = 1;
  353.                     if( ($angle_to*360) - ($angle_from*360) >= 1) {
  354.                         fill_arc($image,$x,$y,$size,$angle_from*360,$angle_to*360,$col_black,$col_red);
  355.                         if (($angle_to-$angle_from)>0.05) {
  356.                             array_push($string_placement, array($angle_from,$angle_to));
  357.                         }
  358.                     }
  359.                     $angle_from = $angle_to;
  360.                 }
  361.                 $angle_to = $angle_from+($block['size'])/$s;
  362.                 if(($angle_to+$fuzz)>1) $angle_to = 1;
  363.                 if( ($angle_to*360) - ($angle_from*360) >= 1) {
  364.                     fill_arc($image,$x,$y,$size,$angle_from*360,$angle_to*360,$col_black,$col_green);
  365.                     if (($angle_to-$angle_from)>0.05) {
  366.                         array_push($string_placement, array($angle_from,$angle_to));
  367.                     }
  368.                 }
  369.                 $angle_from = $angle_to;
  370.                 $ptr = $block['offset']+$block['size'];
  371.             }
  372.             if ($ptr < $mem['seg_size']) { // memory at the end
  373.                 $angle_to = $angle_from + ($mem['seg_size'] - $ptr)/$s;
  374.                 if(($angle_to+$fuzz)>1) $angle_to = 1;
  375.                 fill_arc($image,$x,$y,$size,$angle_from*360,$angle_to*360,$col_black,$col_red);
  376.                 if (($angle_to-$angle_from)>0.05) {
  377.                     array_push($string_placement, array($angle_from,$angle_to));
  378.                 }
  379.             }
  380.         }
  381.         foreach ($string_placement as $angle) {
  382.             text_arc($image,$x,$y,$size,$angle[0]*360,$angle[1]*360,$col_black,bsize($s*($angle[1]-$angle[0])));
  383.         }
  384.         break;
  385.        
  386.     case 2:
  387.         $s=$cache['num_hits']+$cache['num_misses'];
  388.         $a=$cache['num_hits'];
  389.        
  390.         fill_box($image, 30,$size,50,-$a*($size-21)/$s,$col_black,$col_green,sprintf("%.1f%%",$cache['num_hits']*100/$s));
  391.         fill_box($image,130,$size,50,-max(4,($s-$a)*($size-21)/$s),$col_black,$col_red,sprintf("%.1f%%",$cache['num_misses']*100/$s));
  392.         break;
  393.        
  394.     case 3:
  395.         $s=$mem['num_seg']*$mem['seg_size'];
  396.         $a=$mem['avail_mem'];
  397.         $x=130;
  398.         $y=1;
  399.         $j=1;
  400.  
  401.         // This block of code creates the bar chart.  It is a lot more complex than you
  402.         // would expect because we try to visualize any memory fragmentation as well.
  403.         for($i=0; $i<$mem['num_seg']; $i++) {  
  404.             $ptr = 0;
  405.             $free = $mem['block_lists'][$i];
  406.             uasort($free, 'block_sort');
  407.             foreach($free as $block) {
  408.                 if($block['offset']!=$ptr) {       // Used block
  409.                     $h=(GRAPH_SIZE-5)*($block['offset']-$ptr)/$s;
  410.                     if ($h>0) {
  411.                                                 $j++;
  412.                         if($j<75) fill_box($image,$x,$y,50,$h,$col_black,$col_red,bsize($block['offset']-$ptr),$j);
  413.                                                 else fill_box($image,$x,$y,50,$h,$col_black,$col_red);
  414.                                         }
  415.                     $y+=$h;
  416.                 }
  417.                 $h=(GRAPH_SIZE-5)*($block['size'])/$s;
  418.                 if ($h>0) {
  419.                                         $j++;
  420.                     if($j<75) fill_box($image,$x,$y,50,$h,$col_black,$col_green,bsize($block['size']),$j);
  421.                     else fill_box($image,$x,$y,50,$h,$col_black,$col_green);
  422.                                 }
  423.                 $y+=$h;
  424.                 $ptr = $block['offset']+$block['size'];
  425.             }
  426.             if ($ptr < $mem['seg_size']) { // memory at the end
  427.                 $h = (GRAPH_SIZE-5) * ($mem['seg_size'] - $ptr) / $s;
  428.                 if ($h > 0) {
  429.                     fill_box($image,$x,$y,50,$h,$col_black,$col_red,bsize($mem['seg_size']-$ptr),$j++);
  430.                 }
  431.             }
  432.         }
  433.         break;
  434.     case 4:
  435.         $s=$cache['num_hits']+$cache['num_misses'];
  436.         $a=$cache['num_hits'];
  437.                
  438.         fill_box($image, 30,$size,50,-$a*($size-21)/$s,$col_black,$col_green,sprintf("%.1f%%",$cache['num_hits']*100/$s));
  439.         fill_box($image,130,$size,50,-max(4,($s-$a)*($size-21)/$s),$col_black,$col_red,sprintf("%.1f%%",$cache['num_misses']*100/$s));
  440.         break;
  441.    
  442.     }
  443.     header("Content-type: image/png");
  444.     imagepng($image);
  445.     exit;
  446. }
  447.  
  448. // pretty printer for byte values
  449. //
  450. function bsize($s) {
  451.     foreach (array('','K','M','G') as $i => $k) {
  452.         if ($s < 1024) break;
  453.         $s/=1024;
  454.     }
  455.     return sprintf("%5.1f %sBytes",$s,$k);
  456. }
  457.  
  458. // sortable table header in "scripts for this host" view
  459. function sortheader($key,$name,$extra='') {
  460.     global $MYREQUEST, $MY_SELF_WO_SORT;
  461.    
  462.     if ($MYREQUEST['SORT1']==$key) {
  463.         $MYREQUEST['SORT2'] = $MYREQUEST['SORT2']=='A' ? 'D' : 'A';
  464.     }
  465.     return "<a class=sortable href=\"$MY_SELF_WO_SORT$extra&SORT1=$key&SORT2=".$MYREQUEST['SORT2']."\">$name</a>";
  466.  
  467. }
  468.  
  469. // create menu entry
  470. function menu_entry($ob,$title) {
  471.     global $MYREQUEST,$MY_SELF;
  472.     if ($MYREQUEST['OB']!=$ob) {
  473.         return "<li><a href=\"$MY_SELF&OB=$ob\">$title</a></li>";
  474.     } else if (empty($MYREQUEST['SH'])) {
  475.         return "<li><span class=active>$title</span></li>";
  476.     } else {
  477.         return "<li><a class=\"child_active\" href=\"$MY_SELF&OB=$ob\">$title</a></li>";   
  478.     }
  479. }
  480.  
  481. function put_login_link($s="Login")
  482. {
  483.     global $MY_SELF,$MYREQUEST,$AUTHENTICATED;
  484.     // needs ADMIN_PASSWORD to be changed!
  485.     //
  486.     if (!USE_AUTHENTICATION) {
  487.         return;
  488.     } else if (ADMIN_PASSWORD=='password')
  489.     {
  490.         print <<<EOB
  491.             <a href="#" onClick="javascript:alert('You need to set a password at the top of apc.php before this will work!');return false";>$s</a>
  492. EOB;
  493.     } else if ($AUTHENTICATED) {
  494.         print <<<EOB
  495.             '{$_SERVER['PHP_AUTH_USER']}'&nbsp;logged&nbsp;in!
  496. EOB;
  497.     } else{
  498.         print <<<EOB
  499.             <a href="$MY_SELF&LO=1&OB={$MYREQUEST['OB']}">$s</a>
  500. EOB;
  501.     }
  502. }
  503.  
  504. function block_sort($array1, $array2)
  505. {
  506.     if ($array1['offset'] > $array2['offset']) {
  507.         return 1;
  508.     } else {
  509.         return -1;
  510.     }
  511. }
  512.  
  513.  
  514. ?>
  515. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  516. <html>
  517. <head><title>APC INFO <?php echo $host ?></title>
  518. <style><!--
  519. body { background:white; font-size:100.01%; margin:0; padding:0; }
  520. body,p,td,th,input,submit { font-size:0.8em;font-family:arial,helvetica,sans-serif; }
  521. * html body   {font-size:0.8em}
  522. * html p      {font-size:0.8em}
  523. * html td     {font-size:0.8em}
  524. * html th     {font-size:0.8em}
  525. * html input  {font-size:0.8em}
  526. * html submit {font-size:0.8em}
  527. td { vertical-align:top }
  528. a { color:black; font-weight:none; text-decoration:none; }
  529. a:hover { text-decoration:underline; }
  530. div.content { padding:1em 1em 1em 1em; position:absolute; width:97%; z-index:100; }
  531.  
  532.  
  533. div.head div.login {
  534.     position:absolute;
  535.     right: 1em;
  536.     top: 1.2em;
  537.     color:white;
  538.     width:6em;
  539.     }
  540. div.head div.login a {
  541.     position:absolute;
  542.     right: 0em;
  543.     background:rgb(119,123,180);
  544.     border:solid rgb(102,102,153) 2px;
  545.     color:white;
  546.     font-weight:bold;
  547.     padding:0.1em 0.5em 0.1em 0.5em;
  548.     text-decoration:none;
  549.     }
  550. div.head div.login a:hover {
  551.     background:rgb(193,193,244);
  552.     }
  553.  
  554. h1.apc { background:rgb(153,153,204); margin:0; padding:0.5em 1em 0.5em 1em; }
  555. * html h1.apc { margin-bottom:-7px; }
  556. h1.apc a:hover { text-decoration:none; color:rgb(90,90,90); }
  557. h1.apc div.logo span.logo {
  558.     background:rgb(119,123,180);
  559.     color:black;
  560.     border-right: solid black 1px;
  561.     border-bottom: solid black 1px;
  562.     font-style:italic;
  563.     font-size:1em;
  564.     padding-left:1.2em;
  565.     padding-right:1.2em;
  566.     text-align:right;
  567.     }
  568. h1.apc div.logo span.name { color:white; font-size:0.7em; padding:0 0.8em 0 2em; }
  569. h1.apc div.nameinfo { color:white; display:inline; font-size:0.4em; margin-left: 3em; }
  570. h1.apc div.copy { color:black; font-size:0.4em; position:absolute; right:1em; }
  571. hr.apc {
  572.     background:white;
  573.     border-bottom:solid rgb(102,102,153) 1px;
  574.     border-style:none;
  575.     border-top:solid rgb(102,102,153) 10px;
  576.     height:12px;
  577.     margin:0;
  578.     margin-top:1px;
  579.     padding:0;
  580. }
  581.  
  582. ol,menu { margin:1em 0 0 0; padding:0.2em; margin-left:1em;}
  583. ol.menu li { display:inline; margin-right:0.7em; list-style:none; font-size:85%}
  584. ol.menu a {
  585.     background:rgb(153,153,204);
  586.     border:solid rgb(102,102,153) 2px;
  587.     color:white;
  588.     font-weight:bold;
  589.     margin-right:0em;
  590.     padding:0.1em 0.5em 0.1em 0.5em;
  591.     text-decoration:none;
  592.     margin-left: 5px;
  593.     }
  594. ol.menu a.child_active {
  595.     background:rgb(153,153,204);
  596.     border:solid rgb(102,102,153) 2px;
  597.     color:white;
  598.     font-weight:bold;
  599.     margin-right:0em;
  600.     padding:0.1em 0.5em 0.1em 0.5em;
  601.     text-decoration:none;
  602.     border-left: solid black 5px;
  603.     margin-left: 0px;
  604.     }
  605. ol.menu span.active {
  606.     background:rgb(153,153,204);
  607.     border:solid rgb(102,102,153) 2px;
  608.     color:black;
  609.     font-weight:bold;
  610.     margin-right:0em;
  611.     padding:0.1em 0.5em 0.1em 0.5em;
  612.     text-decoration:none;
  613.     border-left: solid black 5px;
  614.     }
  615. ol.menu span.inactive {
  616.     background:rgb(193,193,244);
  617.     border:solid rgb(182,182,233) 2px;
  618.     color:white;
  619.     font-weight:bold;
  620.     margin-right:0em;
  621.     padding:0.1em 0.5em 0.1em 0.5em;
  622.     text-decoration:none;
  623.     margin-left: 5px;
  624.     }
  625. ol.menu a:hover {
  626.     background:rgb(193,193,244);
  627.     text-decoration:none;
  628.     }
  629.    
  630.    
  631. div.info {
  632.     background:rgb(204,204,204);
  633.     border:solid rgb(204,204,204) 1px;
  634.     margin-bottom:1em;
  635.     }
  636. div.info h2 {
  637.     background:rgb(204,204,204);
  638.     color:black;
  639.     font-size:1em;
  640.     margin:0;
  641.     padding:0.1em 1em 0.1em 1em;
  642.     }
  643. div.info table {
  644.     border:solid rgb(204,204,204) 1px;
  645.     border-spacing:0;
  646.     width:100%;
  647.     }
  648. div.info table th {
  649.     background:rgb(204,204,204);
  650.     color:white;
  651.     margin:0;
  652.     padding:0.1em 1em 0.1em 1em;
  653.     }
  654. div.info table th a.sortable { color:black; }
  655. div.info table tr.tr-0 { background:rgb(238,238,238); }
  656. div.info table tr.tr-1 { background:rgb(221,221,221); }
  657. div.info table td { padding:0.3em 1em 0.3em 1em; }
  658. div.info table td.td-0 { border-right:solid rgb(102,102,153) 1px; white-space:nowrap; }
  659. div.info table td.td-n { border-right:solid rgb(102,102,153) 1px; }
  660. div.info table td h3 {
  661.     color:black;
  662.     font-size:1.1em;
  663.     margin-left:-0.3em;
  664.     }
  665.  
  666. div.graph { margin-bottom:1em }
  667. div.graph h2 { background:rgb(204,204,204);; color:black; font-size:1em; margin:0; padding:0.1em 1em 0.1em 1em; }
  668. div.graph table { border:solid rgb(204,204,204) 1px; color:black; font-weight:normal; width:100%; }
  669. div.graph table td.td-0 { background:rgb(238,238,238); }
  670. div.graph table td.td-1 { background:rgb(221,221,221); }
  671. div.graph table td { padding:0.2em 1em 0.4em 1em; }
  672.  
  673. div.div1,div.div2 { margin-bottom:1em; width:35em; }
  674. div.div3 { position:absolute; left:40em; top:1em; width:580px; }
  675. //div.div3 { position:absolute; left:37em; top:1em; right:1em; }
  676.  
  677. div.sorting { margin:1.5em 0em 1.5em 2em }
  678. .center { text-align:center }
  679. .aright { position:absolute;right:1em }
  680. .right { text-align:right }
  681. .ok { color:rgb(0,200,0); font-weight:bold}
  682. .failed { color:rgb(200,0,0); font-weight:bold}
  683.  
  684. span.box {
  685.     border: black solid 1px;
  686.     border-right:solid black 2px;
  687.     border-bottom:solid black 2px;
  688.     padding:0 0.5em 0 0.5em;
  689.     margin-right:1em;
  690. }
  691. span.green { background:#60F060; padding:0 0.5em 0 0.5em}
  692. span.red { background:#D06030; padding:0 0.5em 0 0.5em }
  693.  
  694. div.authneeded {
  695.     background:rgb(238,238,238);
  696.     border:solid rgb(204,204,204) 1px;
  697.     color:rgb(200,0,0);
  698.     font-size:1.2em;
  699.     font-weight:bold;
  700.     padding:2em;
  701.     text-align:center;
  702.     }
  703.    
  704. input {
  705.     background:rgb(153,153,204);
  706.     border:solid rgb(102,102,153) 2px;
  707.     color:white;
  708.     font-weight:bold;
  709.     margin-right:1em;
  710.     padding:0.1em 0.5em 0.1em 0.5em;
  711.     }
  712. //-->
  713. </style>
  714. </head>
  715. <body>
  716. <div class="head">
  717.     <h1 class="apc">
  718.         <div class="logo"><span class="logo"><a href="http://pecl.php.net/package/APC">APC</a></span></div>
  719.         <div class="nameinfo">Opcode Cache</div>
  720.     </h1>
  721.     <div class="login">
  722.     <?php put_login_link(); ?>
  723.     </div>
  724.     <hr class="apc">
  725. </div>
  726. <?php
  727.  
  728.  
  729. // Display main Menu
  730. echo <<<EOB
  731.     <ol class=menu>
  732.     <li><a href="$MY_SELF&OB={$MYREQUEST['OB']}&SH={$MYREQUEST['SH']}">Refresh Data</a></li>
  733. EOB;
  734. echo
  735.     menu_entry(1,'View Host Stats'),
  736.     menu_entry(2,'System Cache Entries');
  737. if ($AUTHENTICATED) {
  738.     echo menu_entry(4,'Per-Directory Entries');
  739. }
  740. echo
  741.     menu_entry(3,'User Cache Entries'),
  742.     menu_entry(9,'Version Check');
  743.    
  744. if ($AUTHENTICATED) {
  745.     echo <<<EOB
  746.         <li><a class="aright" href="$MY_SELF&CC=1&OB={$MYREQUEST['OB']}" onClick="javascript:return confirm('Are you sure?');">Clear $cache_mode Cache</a></li>
  747. EOB;
  748. }
  749. echo <<<EOB
  750.     </ol>
  751. EOB;
  752.  
  753.  
  754. // CONTENT
  755. echo <<<EOB
  756.     <div class=content>
  757. EOB;
  758.  
  759. // MAIN SWITCH STATEMENT
  760.  
  761. switch ($MYREQUEST['OB']) {
  762.  
  763.  
  764.  
  765.  
  766.  
  767. // -----------------------------------------------
  768. // Host Stats
  769. // -----------------------------------------------
  770. case OB_HOST_STATS:
  771.     $mem_size = $mem['num_seg']*$mem['seg_size'];
  772.     $mem_avail= $mem['avail_mem'];
  773.     $mem_used = $mem_size-$mem_avail;
  774.     $seg_size = bsize($mem['seg_size']);
  775.     $req_rate = sprintf("%.2f",($cache['num_hits']+$cache['num_misses'])/($time-$cache['start_time']));
  776.     $hit_rate = sprintf("%.2f",($cache['num_hits'])/($time-$cache['start_time']));
  777.     $miss_rate = sprintf("%.2f",($cache['num_misses'])/($time-$cache['start_time']));
  778.     $insert_rate = sprintf("%.2f",($cache['num_inserts'])/($time-$cache['start_time']));
  779.     $req_rate_user = sprintf("%.2f",($cache_user['num_hits']+$cache_user['num_misses'])/($time-$cache_user['start_time']));
  780.     $hit_rate_user = sprintf("%.2f",($cache_user['num_hits'])/($time-$cache_user['start_time']));
  781.     $miss_rate_user = sprintf("%.2f",($cache_user['num_misses'])/($time-$cache_user['start_time']));
  782.     $insert_rate_user = sprintf("%.2f",($cache_user['num_inserts'])/($time-$cache_user['start_time']));
  783.     $apcversion = phpversion('apc');
  784.     $phpversion = phpversion();
  785.     $number_files = $cache['num_entries'];
  786.     $size_files = bsize($cache['mem_size']);
  787.     $number_vars = $cache_user['num_entries'];
  788.     $size_vars = bsize($cache_user['mem_size']);
  789.     $i=0;
  790.     echo <<< EOB
  791.         <div class="info div1"><h2>General Cache Information</h2>
  792.         <table cellspacing=0><tbody>
  793.         <tr class=tr-0><td class=td-0>APC Version</td><td>$apcversion</td></tr>
  794.         <tr class=tr-1><td class=td-0>PHP Version</td><td>$phpversion</td></tr>
  795. EOB;
  796.  
  797.     if(!empty($_SERVER['SERVER_NAME']))
  798.         echo "<tr class=tr-0><td class=td-0>APC Host</td><td>{$_SERVER['SERVER_NAME']} $host</td></tr>\n";
  799.     if(!empty($_SERVER['SERVER_SOFTWARE']))
  800.         echo "<tr class=tr-1><td class=td-0>Server Software</td><td>{$_SERVER['SERVER_SOFTWARE']}</td></tr>\n";
  801.  
  802.     echo <<<EOB
  803.         <tr class=tr-0><td class=td-0>Shared Memory</td><td>{$mem['num_seg']} Segment(s) with $seg_size
  804.     <br/> ({$cache['memory_type']} memory, {$cache['locking_type']} locking)
  805.     </td></tr>
  806. EOB;
  807.     echo   '<tr class=tr-1><td class=td-0>Start Time</td><td>',date(DATE_FORMAT,$cache['start_time']),'</td></tr>';
  808.     echo   '<tr class=tr-0><td class=td-0>Uptime</td><td>',duration($cache['start_time']),'</td></tr>';
  809.     echo   '<tr class=tr-1><td class=td-0>File Upload Support</td><td>',$cache['file_upload_progress'],'</td></tr>';
  810.     echo <<<EOB
  811.         </tbody></table>
  812.         </div>
  813.  
  814.         <div class="info div1"><h2>File Cache Information</h2>
  815.         <table cellspacing=0><tbody>
  816.         <tr class=tr-0><td class=td-0>Cached Files</td><td>$number_files ($size_files)</td></tr>
  817.         <tr class=tr-1><td class=td-0>Hits</td><td>{$cache['num_hits']}</td></tr>
  818.         <tr class=tr-0><td class=td-0>Misses</td><td>{$cache['num_misses']}</td></tr>
  819.         <tr class=tr-1><td class=td-0>Request Rate (hits, misses)</td><td>$req_rate cache requests/second</td></tr>
  820.         <tr class=tr-0><td class=td-0>Hit Rate</td><td>$hit_rate cache requests/second</td></tr>
  821.         <tr class=tr-1><td class=td-0>Miss Rate</td><td>$miss_rate cache requests/second</td></tr>
  822.         <tr class=tr-0><td class=td-0>Insert Rate</td><td>$insert_rate cache requests/second</td></tr>
  823.         <tr class=tr-1><td class=td-0>Cache full count</td><td>{$cache['expunges']}</td></tr>
  824.         </tbody></table>
  825.         </div>
  826.  
  827.         <div class="info div1"><h2>User Cache Information</h2>
  828.         <table cellspacing=0><tbody>
  829.     <tr class=tr-0><td class=td-0>Cached Variables</td><td>$number_vars ($size_vars)</td></tr>
  830.         <tr class=tr-1><td class=td-0>Hits</td><td>{$cache_user['num_hits']}</td></tr>
  831.         <tr class=tr-0><td class=td-0>Misses</td><td>{$cache_user['num_misses']}</td></tr>
  832.         <tr class=tr-1><td class=td-0>Request Rate (hits, misses)</td><td>$req_rate_user cache requests/second</td></tr>
  833.         <tr class=tr-0><td class=td-0>Hit Rate</td><td>$hit_rate_user cache requests/second</td></tr>
  834.         <tr class=tr-1><td class=td-0>Miss Rate</td><td>$miss_rate_user cache requests/second</td></tr>
  835.         <tr class=tr-0><td class=td-0>Insert Rate</td><td>$insert_rate_user cache requests/second</td></tr>
  836.         <tr class=tr-1><td class=td-0>Cache full count</td><td>{$cache_user['expunges']}</td></tr>
  837.  
  838.         </tbody></table>
  839.         </div>
  840.  
  841.         <div class="info div2"><h2>Runtime Settings</h2><table cellspacing=0><tbody>
  842. EOB;
  843.  
  844.     $j = 0;
  845.     foreach (ini_get_all('apc') as $k => $v) {
  846.         echo "<tr class=tr-$j><td class=td-0>",$k,"</td><td>",str_replace(',',',<br />',$v['local_value']),"</td></tr>\n";
  847.         $j = 1 - $j;
  848.     }
  849.  
  850.     if($mem['num_seg']>1 || $mem['num_seg']==1 && count($mem['block_lists'][0])>1)
  851.         $mem_note = "Memory Usage<br /><font size=-2>(multiple slices indicate fragments)</font>";
  852.     else
  853.         $mem_note = "Memory Usage";
  854.  
  855.     echo <<< EOB
  856.         </tbody></table>
  857.         </div>
  858.  
  859.         <div class="graph div3"><h2>Host Status Diagrams</h2>
  860.         <table cellspacing=0><tbody>
  861. EOB;
  862.     $size='width='.(GRAPH_SIZE+50).' height='.(GRAPH_SIZE+10);
  863.     echo <<<EOB
  864.         <tr>
  865.         <td class=td-0>$mem_note</td>
  866.         <td class=td-1>Hits &amp; Misses</td>
  867.         </tr>
  868. EOB;
  869.  
  870.     echo
  871.         graphics_avail() ?
  872.               '<tr>'.
  873.               "<td class=td-0><img alt=\"\" $size src=\"$PHP_SELF?IMG=1&$time\"></td>".
  874.               "<td class=td-1><img alt=\"\" $size src=\"$PHP_SELF?IMG=2&$time\"></td></tr>\n"
  875.             : "",
  876.         '<tr>',
  877.         '<td class=td-0><span class="green box">&nbsp;</span>Free: ',bsize($mem_avail).sprintf(" (%.1f%%)",$mem_avail*100/$mem_size),"</td>\n",
  878.         '<td class=td-1><span class="green box">&nbsp;</span>Hits: ',$cache['num_hits'].sprintf(" (%.1f%%)",$cache['num_hits']*100/($cache['num_hits']+$cache['num_misses'])),"</td>\n",
  879.         '</tr>',
  880.         '<tr>',
  881.         '<td class=td-0><span class="red box">&nbsp;</span>Used: ',bsize($mem_used ).sprintf(" (%.1f%%)",$mem_used *100/$mem_size),"</td>\n",
  882.         '<td class=td-1><span class="red box">&nbsp;</span>Misses: ',$cache['num_misses'].sprintf(" (%.1f%%)",$cache['num_misses']*100/($cache['num_hits']+$cache['num_misses'])),"</td>\n";
  883.     echo <<< EOB
  884.         </tr>
  885.         </tbody></table>
  886.  
  887.         <br/>
  888.         <h2>Detailed Memory Usage and Fragmentation</h2>
  889.         <table cellspacing=0><tbody>
  890.         <tr>
  891.         <td class=td-0 colspan=2><br/>
  892. EOB;
  893.  
  894.     // Fragementation: (freeseg - 1) / total_seg
  895.     $nseg = $freeseg = $fragsize = $freetotal = 0;
  896.     for($i=0; $i<$mem['num_seg']; $i++) {
  897.         $ptr = 0;
  898.         foreach($mem['block_lists'][$i] as $block) {
  899.             if ($block['offset'] != $ptr) {
  900.                 ++$nseg;
  901.             }
  902.             $ptr = $block['offset'] + $block['size'];
  903.                         /* Only consider blocks <5M for the fragmentation % */
  904.                         if($block['size']<(5*1024*1024)) $fragsize+=$block['size'];
  905.                         $freetotal+=$block['size'];
  906.         }
  907.         $freeseg += count($mem['block_lists'][$i]);
  908.     }
  909.    
  910.     if ($freeseg > 1) {
  911.         $frag = sprintf("%.2f%% (%s out of %s in %d fragments)", ($fragsize/$freetotal)*100,bsize($fragsize),bsize($freetotal),$freeseg);
  912.     } else {
  913.         $frag = "0%";
  914.     }
  915.  
  916.     if (graphics_avail()) {
  917.         $size='width='.(2*GRAPH_SIZE+150).' height='.(GRAPH_SIZE+10);
  918.         echo <<<EOB
  919.             <img alt="" $size src="$PHP_SELF?IMG=3&$time">
  920. EOB;
  921.     }
  922.     echo <<<EOB
  923.         </br>Fragmentation: $frag
  924.         </td>
  925.         </tr>
  926. EOB;
  927.         if(isset($mem['adist'])) {
  928.           foreach($mem['adist'] as $i=>$v) {
  929.             $cur = pow(2,$i); $nxt = pow(2,$i+1)-1;
  930.             if($i==0) $range = "1";
  931.             else $range = "$cur - $nxt";
  932.             echo "<tr><th align=right>$range</th><td align=right>$v</td></tr>\n";
  933.           }
  934.         }
  935.         echo <<<EOB
  936.         </tbody></table>
  937.         </div>
  938. EOB;
  939.        
  940.     break;
  941.  
  942.  
  943. // -----------------------------------------------
  944. // User Cache Entries
  945. // -----------------------------------------------
  946. case OB_USER_CACHE:
  947.     if (!$AUTHENTICATED) {
  948.     echo '<div class="error">You need to login to see the user values here!<br/>&nbsp;<br/>';
  949.         put_login_link("Login now!");
  950.         echo '</div>';
  951.         break;
  952.     }
  953.     $fieldname='info';
  954.     $fieldheading='User Entry Label';
  955.     $fieldkey='info';
  956.  
  957. // -----------------------------------------------
  958. // System Cache Entries    
  959. // -----------------------------------------------
  960. case OB_SYS_CACHE: 
  961.     if (!isset($fieldname))
  962.     {
  963.         $fieldname='filename';
  964.         $fieldheading='Script Filename';
  965.         if(ini_get("apc.stat")) $fieldkey='inode';
  966.         else $fieldkey='filename';
  967.     }
  968.     if (!empty($MYREQUEST['SH']))
  969.     {
  970.         echo <<< EOB
  971.             <div class="info"><table cellspacing=0><tbody>
  972.             <tr><th>Attribute</th><th>Value</th></tr>
  973. EOB;
  974.  
  975.         $m=0;
  976.         foreach($scope_list as $j => $list) {
  977.             foreach($cache[$list] as $i => $entry) {
  978.                 if (md5($entry[$fieldkey])!=$MYREQUEST['SH']) continue;
  979.                 foreach($entry as $k => $value) {
  980.                     if (!$AUTHENTICATED) {
  981.                         // hide all path entries if not logged in
  982.                         $value=preg_replace('/^.*(\\/|\\\\)/','<i>&lt;hidden&gt;</i>/',$value);
  983.                     }
  984.  
  985.                     if ($k == "num_hits") {
  986.                         $value=sprintf("%s (%.2f%%)",$value,$value*100/$cache['num_hits']);
  987.                     }
  988.                     if ($k == 'deletion_time') {
  989.                         if(!$entry['deletion_time']) $value = "None";
  990.                     }
  991.                     echo
  992.                         "<tr class=tr-$m>",
  993.                         "<td class=td-0>",ucwords(preg_replace("/_/"," ",$k)),"</td>",
  994.                         "<td class=td-last>",(preg_match("/time/",$k) && $value!='None') ? date(DATE_FORMAT,$value) : htmlspecialchars($value, ENT_QUOTES, 'UTF-8'),"</td>",
  995.                         "</tr>";
  996.                     $m=1-$m;
  997.                 }
  998.                 if($fieldkey=='info') {
  999.                     echo "<tr class=tr-$m><td class=td-0>Stored Value</td><td class=td-last><pre>";
  1000.                     $output = var_export(apc_fetch($entry[$fieldkey]),true);
  1001.                     echo htmlspecialchars($output, ENT_QUOTES, 'UTF-8');
  1002.                     echo "</pre></td></tr>\n";
  1003.                 }
  1004.                 break;
  1005.             }
  1006.         }
  1007.  
  1008.         echo <<<EOB
  1009.             </tbody></table>
  1010.             </div>
  1011. EOB;
  1012.         break;
  1013.     }
  1014.  
  1015.     $cols=6;
  1016.     echo <<<EOB
  1017.         <div class=sorting><form>Scope:
  1018.         <input type=hidden name=OB value={$MYREQUEST['OB']}>
  1019.         <select name=SCOPE>
  1020. EOB;
  1021.     echo
  1022.         "<option value=A",$MYREQUEST['SCOPE']=='A' ? " selected":"",">Active</option>",
  1023.         "<option value=D",$MYREQUEST['SCOPE']=='D' ? " selected":"",">Deleted</option>",
  1024.         "</select>",
  1025.         ", Sorting:<select name=SORT1>",
  1026.         "<option value=H",$MYREQUEST['SORT1']=='H' ? " selected":"",">Hits</option>",
  1027.         "<option value=Z",$MYREQUEST['SORT1']=='Z' ? " selected":"",">Size</option>",
  1028.         "<option value=S",$MYREQUEST['SORT1']=='S' ? " selected":"",">$fieldheading</option>",
  1029.         "<option value=A",$MYREQUEST['SORT1']=='A' ? " selected":"",">Last accessed</option>",
  1030.         "<option value=M",$MYREQUEST['SORT1']=='M' ? " selected":"",">Last modified</option>",
  1031.         "<option value=C",$MYREQUEST['SORT1']=='C' ? " selected":"",">Created at</option>",
  1032.         "<option value=D",$MYREQUEST['SORT1']=='D' ? " selected":"",">Deleted at</option>";
  1033.     if($fieldname=='info') echo
  1034.         "<option value=D",$MYREQUEST['SORT1']=='T' ? " selected":"",">Timeout</option>";
  1035.     echo
  1036.         '</select>',
  1037.         '<select name=SORT2>',
  1038.         '<option value=D',$MYREQUEST['SORT2']=='D' ? ' selected':'','>DESC</option>',
  1039.         '<option value=A',$MYREQUEST['SORT2']=='A' ? ' selected':'','>ASC</option>',
  1040.         '</select>',
  1041.         '<select name=COUNT onChange="form.submit()">',
  1042.         '<option value=10 ',$MYREQUEST['COUNT']=='10' ? ' selected':'','>Top 10</option>',
  1043.         '<option value=20 ',$MYREQUEST['COUNT']=='20' ? ' selected':'','>Top 20</option>',
  1044.         '<option value=50 ',$MYREQUEST['COUNT']=='50' ? ' selected':'','>Top 50</option>',
  1045.         '<option value=100',$MYREQUEST['COUNT']=='100'? ' selected':'','>Top 100</option>',
  1046.         '<option value=150',$MYREQUEST['COUNT']=='150'? ' selected':'','>Top 150</option>',
  1047.         '<option value=200',$MYREQUEST['COUNT']=='200'? ' selected':'','>Top 200</option>',
  1048.         '<option value=500',$MYREQUEST['COUNT']=='500'? ' selected':'','>Top 500</option>',
  1049.         '<option value=0  ',$MYREQUEST['COUNT']=='0'  ? ' selected':'','>All</option>',
  1050.         '</select>',
  1051.     '&nbsp; Search: <input name=SEARCH value="',$MYREQUEST['SEARCH'],'" type=text size=25/>',
  1052.         '&nbsp;<input type=submit value="GO!">',
  1053.         '</form></div>';
  1054.  
  1055.   if (isset($MYREQUEST['SEARCH'])) {
  1056.    // Don't use preg_quote because we want the user to be able to specify a
  1057.    // regular expression subpattern.
  1058.    $MYREQUEST['SEARCH'] = '/'.str_replace('/', '\\/', $MYREQUEST['SEARCH']).'/i';
  1059.    if (preg_match($MYREQUEST['SEARCH'], 'test') === false) {
  1060.      echo '<div class="error">Error: enter a valid regular expression as a search query.</div>';
  1061.      break;
  1062.    }
  1063.   }
  1064.  
  1065.   echo
  1066.         '<div class="info"><table cellspacing=0><tbody>',
  1067.         '<tr>',
  1068.         '<th>',sortheader('S',$fieldheading,  "&OB=".$MYREQUEST['OB']),'</th>',
  1069.         '<th>',sortheader('H','Hits',         "&OB=".$MYREQUEST['OB']),'</th>',
  1070.         '<th>',sortheader('Z','Size',         "&OB=".$MYREQUEST['OB']),'</th>',
  1071.         '<th>',sortheader('A','Last accessed',"&OB=".$MYREQUEST['OB']),'</th>',
  1072.         '<th>',sortheader('M','Last modified',"&OB=".$MYREQUEST['OB']),'</th>',
  1073.         '<th>',sortheader('C','Created at',   "&OB=".$MYREQUEST['OB']),'</th>';
  1074.  
  1075.     if($fieldname=='info') {
  1076.         $cols+=2;
  1077.          echo '<th>',sortheader('T','Timeout',"&OB=".$MYREQUEST['OB']),'</th>';
  1078.     }
  1079.     echo '<th>',sortheader('D','Deleted at',"&OB=".$MYREQUEST['OB']),'</th></tr>';
  1080.  
  1081.     // builds list with alpha numeric sortable keys
  1082.     //
  1083.     $list = array();
  1084.     foreach($cache[$scope_list[$MYREQUEST['SCOPE']]] as $i => $entry) {
  1085.         switch($MYREQUEST['SORT1']) {
  1086.             case 'A': $k=sprintf('%015d-',$entry['access_time']);   break;
  1087.             case 'H': $k=sprintf('%015d-',$entry['num_hits']);      break;
  1088.             case 'Z': $k=sprintf('%015d-',$entry['mem_size']);      break;
  1089.             case 'M': $k=sprintf('%015d-',$entry['mtime']);         break;
  1090.             case 'C': $k=sprintf('%015d-',$entry['creation_time']); break;
  1091.             case 'T': $k=sprintf('%015d-',$entry['ttl']);           break;
  1092.             case 'D': $k=sprintf('%015d-',$entry['deletion_time']); break;
  1093.             case 'S': $k='';                                        break;
  1094.         }
  1095.         if (!$AUTHENTICATED) {
  1096.             // hide all path entries if not logged in
  1097.             $list[$k.$entry[$fieldname]]=preg_replace('/^.*(\\/|\\\\)/','*hidden*/',$entry);
  1098.         } else {
  1099.             $list[$k.$entry[$fieldname]]=$entry;
  1100.         }
  1101.     }
  1102.  
  1103.     if ($list) {
  1104.        
  1105.         // sort list
  1106.         //
  1107.         switch ($MYREQUEST['SORT2']) {
  1108.             case "A":   krsort($list);  break;
  1109.             case "D":   ksort($list);   break;
  1110.         }
  1111.        
  1112.         // output list
  1113.         $i=0;
  1114.         foreach($list as $k => $entry) {
  1115.       if(!$MYREQUEST['SEARCH'] || preg_match($MYREQUEST['SEARCH'], $entry[$fieldname]) != 0) {  
  1116.         $field_value = htmlentities(strip_tags($entry[$fieldname],''), ENT_QUOTES, 'UTF-8');
  1117.         echo
  1118.           '<tr class=tr-',$i%2,'>',
  1119.           "<td class=td-0><a href=\"$MY_SELF&OB=",$MYREQUEST['OB'],"&SH=",md5($entry[$fieldkey]),"\">",$field_value,'</a></td>',
  1120.           '<td class="td-n center">',$entry['num_hits'],'</td>',
  1121.           '<td class="td-n right">',$entry['mem_size'],'</td>',
  1122.           '<td class="td-n center">',date(DATE_FORMAT,$entry['access_time']),'</td>',
  1123.           '<td class="td-n center">',date(DATE_FORMAT,$entry['mtime']),'</td>',
  1124.           '<td class="td-n center">',date(DATE_FORMAT,$entry['creation_time']),'</td>';
  1125.  
  1126.         if($fieldname=='info') {
  1127.           if($entry['ttl'])
  1128.             echo '<td class="td-n center">'.$entry['ttl'].' seconds</td>';
  1129.           else
  1130.             echo '<td class="td-n center">None</td>';
  1131.         }
  1132.         if ($entry['deletion_time']) {
  1133.  
  1134.           echo '<td class="td-last center">', date(DATE_FORMAT,$entry['deletion_time']), '</td>';
  1135.         } else if ($MYREQUEST['OB'] == OB_USER_CACHE) {
  1136.  
  1137.           echo '<td class="td-last center">';
  1138.           echo '[<a href="', $MY_SELF, '&OB=', $MYREQUEST['OB'], '&DU=', urlencode($entry[$fieldkey]), '">Delete Now</a>]';
  1139.           echo '</td>';
  1140.         } else {
  1141.           echo '<td class="td-last center"> &nbsp; </td>';
  1142.         }
  1143.         echo '</tr>';
  1144.         $i++;
  1145.         if ($i == $MYREQUEST['COUNT'])
  1146.           break;
  1147.       }
  1148.         }
  1149.        
  1150.     } else {
  1151.         echo '<tr class=tr-0><td class="center" colspan=',$cols,'><i>No data</i></td></tr>';
  1152.     }
  1153.     echo <<< EOB
  1154.         </tbody></table>
  1155. EOB;
  1156.  
  1157.     if ($list && $i < count($list)) {
  1158.         echo "<a href=\"$MY_SELF&OB=",$MYREQUEST['OB'],"&COUNT=0\"><i>",count($list)-$i,' more available...</i></a>';
  1159.     }
  1160.  
  1161.     echo <<< EOB
  1162.         </div>
  1163. EOB;
  1164.     break;
  1165.  
  1166.  
  1167. // -----------------------------------------------
  1168. // Per-Directory System Cache Entries
  1169. // -----------------------------------------------
  1170. case OB_SYS_CACHE_DIR: 
  1171.     if (!$AUTHENTICATED) {
  1172.         break;
  1173.     }
  1174.  
  1175.     echo <<<EOB
  1176.         <div class=sorting><form>Scope:
  1177.         <input type=hidden name=OB value={$MYREQUEST['OB']}>
  1178.         <select name=SCOPE>
  1179. EOB;
  1180.     echo
  1181.         "<option value=A",$MYREQUEST['SCOPE']=='A' ? " selected":"",">Active</option>",
  1182.         "<option value=D",$MYREQUEST['SCOPE']=='D' ? " selected":"",">Deleted</option>",
  1183.         "</select>",
  1184.         ", Sorting:<select name=SORT1>",
  1185.         "<option value=H",$MYREQUEST['SORT1']=='H' ? " selected":"",">Total Hits</option>",
  1186.         "<option value=Z",$MYREQUEST['SORT1']=='Z' ? " selected":"",">Total Size</option>",
  1187.         "<option value=T",$MYREQUEST['SORT1']=='T' ? " selected":"",">Number of Files</option>",
  1188.         "<option value=S",$MYREQUEST['SORT1']=='S' ? " selected":"",">Directory Name</option>",
  1189.         "<option value=A",$MYREQUEST['SORT1']=='A' ? " selected":"",">Avg. Size</option>",
  1190.         "<option value=C",$MYREQUEST['SORT1']=='C' ? " selected":"",">Avg. Hits</option>",
  1191.         '</select>',
  1192.         '<select name=SORT2>',
  1193.         '<option value=D',$MYREQUEST['SORT2']=='D' ? ' selected':'','>DESC</option>',
  1194.         '<option value=A',$MYREQUEST['SORT2']=='A' ? ' selected':'','>ASC</option>',
  1195.         '</select>',
  1196.         '<select name=COUNT onChange="form.submit()">',
  1197.         '<option value=10 ',$MYREQUEST['COUNT']=='10' ? ' selected':'','>Top 10</option>',
  1198.         '<option value=20 ',$MYREQUEST['COUNT']=='20' ? ' selected':'','>Top 20</option>',
  1199.         '<option value=50 ',$MYREQUEST['COUNT']=='50' ? ' selected':'','>Top 50</option>',
  1200.         '<option value=100',$MYREQUEST['COUNT']=='100'? ' selected':'','>Top 100</option>',
  1201.         '<option value=150',$MYREQUEST['COUNT']=='150'? ' selected':'','>Top 150</option>',
  1202.         '<option value=200',$MYREQUEST['COUNT']=='200'? ' selected':'','>Top 200</option>',
  1203.         '<option value=500',$MYREQUEST['COUNT']=='500'? ' selected':'','>Top 500</option>',
  1204.         '<option value=0  ',$MYREQUEST['COUNT']=='0'  ? ' selected':'','>All</option>',
  1205.         '</select>',
  1206.         ", Group By Dir Level:<select name=AGGR>",
  1207.         "<option value='' selected>None</option>";
  1208.         for ($i = 1; $i < 10; $i++)
  1209.             echo "<option value=$i",$MYREQUEST['AGGR']==$i ? " selected":"",">$i</option>";
  1210.         echo '</select>',
  1211.         '&nbsp;<input type=submit value="GO!">',
  1212.         '</form></div>',
  1213.  
  1214.         '<div class="info"><table cellspacing=0><tbody>',
  1215.         '<tr>',
  1216.         '<th>',sortheader('S','Directory Name', "&OB=".$MYREQUEST['OB']),'</th>',
  1217.         '<th>',sortheader('T','Number of Files',"&OB=".$MYREQUEST['OB']),'</th>',
  1218.         '<th>',sortheader('H','Total Hits', "&OB=".$MYREQUEST['OB']),'</th>',
  1219.         '<th>',sortheader('Z','Total Size', "&OB=".$MYREQUEST['OB']),'</th>',
  1220.         '<th>',sortheader('C','Avg. Hits',  "&OB=".$MYREQUEST['OB']),'</th>',
  1221.         '<th>',sortheader('A','Avg. Size',  "&OB=".$MYREQUEST['OB']),'</th>',
  1222.         '</tr>';
  1223.  
  1224.     // builds list with alpha numeric sortable keys
  1225.     //
  1226.     $tmp = $list = array();
  1227.     foreach($cache[$scope_list[$MYREQUEST['SCOPE']]] as $entry) {
  1228.         $n = dirname($entry['filename']);
  1229.         if ($MYREQUEST['AGGR'] > 0) {
  1230.             $n = preg_replace("!^(/?(?:[^/\\\\]+[/\\\\]){".($MYREQUEST['AGGR']-1)."}[^/\\\\]*).*!", "$1", $n);
  1231.         }
  1232.         if (!isset($tmp[$n])) {
  1233.             $tmp[$n] = array('hits'=>0,'size'=>0,'ents'=>0);
  1234.         }
  1235.         $tmp[$n]['hits'] += $entry['num_hits'];
  1236.         $tmp[$n]['size'] += $entry['mem_size'];
  1237.         ++$tmp[$n]['ents'];
  1238.     }
  1239.  
  1240.     foreach ($tmp as $k => $v) {
  1241.         switch($MYREQUEST['SORT1']) {
  1242.             case 'A': $kn=sprintf('%015d-',$v['size'] / $v['ents']);break;
  1243.             case 'T': $kn=sprintf('%015d-',$v['ents']);     break;
  1244.             case 'H': $kn=sprintf('%015d-',$v['hits']);     break;
  1245.             case 'Z': $kn=sprintf('%015d-',$v['size']);     break;
  1246.             case 'C': $kn=sprintf('%015d-',$v['hits'] / $v['ents']);break;
  1247.             case 'S': $kn = $k;                 break;
  1248.         }
  1249.         $list[$kn.$k] = array($k, $v['ents'], $v['hits'], $v['size']);
  1250.     }
  1251.  
  1252.     if ($list) {
  1253.        
  1254.         // sort list
  1255.         //
  1256.         switch ($MYREQUEST['SORT2']) {
  1257.             case "A":   krsort($list);  break;
  1258.             case "D":   ksort($list);   break;
  1259.         }
  1260.        
  1261.         // output list
  1262.         $i = 0;
  1263.         foreach($list as $entry) {
  1264.             echo
  1265.                 '<tr class=tr-',$i%2,'>',
  1266.                 "<td class=td-0>",$entry[0],'</a></td>',
  1267.                 '<td class="td-n center">',$entry[1],'</td>',
  1268.                 '<td class="td-n center">',$entry[2],'</td>',
  1269.                 '<td class="td-n center">',$entry[3],'</td>',
  1270.                 '<td class="td-n center">',round($entry[2] / $entry[1]),'</td>',
  1271.                 '<td class="td-n center">',round($entry[3] / $entry[1]),'</td>',
  1272.                 '</tr>';
  1273.  
  1274.             if (++$i == $MYREQUEST['COUNT']) break;
  1275.         }
  1276.        
  1277.     } else {
  1278.         echo '<tr class=tr-0><td class="center" colspan=6><i>No data</i></td></tr>';
  1279.     }
  1280.     echo <<< EOB
  1281.         </tbody></table>
  1282. EOB;
  1283.  
  1284.     if ($list && $i < count($list)) {
  1285.         echo "<a href=\"$MY_SELF&OB=",$MYREQUEST['OB'],"&COUNT=0\"><i>",count($list)-$i,' more available...</i></a>';
  1286.     }
  1287.  
  1288.     echo <<< EOB
  1289.         </div>
  1290. EOB;
  1291.     break;
  1292.  
  1293. // -----------------------------------------------
  1294. // Version check
  1295. // -----------------------------------------------
  1296. case OB_VERSION_CHECK:
  1297.     echo <<<EOB
  1298.         <div class="info"><h2>APC Version Information</h2>
  1299.         <table cellspacing=0><tbody>
  1300.         <tr>
  1301.         <th></th>
  1302.         </tr>
  1303. EOB;
  1304.   if (defined('PROXY')) {
  1305.     $ctxt = stream_context_create( array( 'http' => array( 'proxy' => PROXY, 'request_fulluri' => True ) ) );
  1306.     $rss = @file_get_contents("http://pecl.php.net/feeds/pkg_apc.rss", False, $ctxt);
  1307.   } else {
  1308.     $rss = @file_get_contents("http://pecl.php.net/feeds/pkg_apc.rss");
  1309.   }
  1310.     if (!$rss) {
  1311.         echo '<tr class="td-last center"><td>Unable to fetch version information.</td></tr>';
  1312.     } else {
  1313.         $apcversion = phpversion('apc');
  1314.  
  1315.         preg_match('!<title>APC ([0-9.]+)</title>!', $rss, $match);
  1316.         echo '<tr class="tr-0 center"><td>';
  1317.         if (version_compare($apcversion, $match[1], '>=')) {
  1318.             echo '<div class="ok">You are running the latest version of APC ('.$apcversion.')</div>';
  1319.             $i = 3;
  1320.         } else {
  1321.             echo '<div class="failed">You are running an older version of APC ('.$apcversion.'),
  1322.                 newer version '.$match[1].' is available at <a href="http://pecl.php.net/package/APC/'.$match[1].'">
  1323.                 http://pecl.php.net/package/APC/'.$match[1].'</a>
  1324.                 </div>';
  1325.             $i = -1;
  1326.         }
  1327.         echo '</td></tr>';
  1328.         echo '<tr class="tr-0"><td><h3>Change Log:</h3><br/>';
  1329.  
  1330.         preg_match_all('!<(title|description)>([^<]+)</\\1>!', $rss, $match);
  1331.         next($match[2]); next($match[2]);
  1332.  
  1333.         while (list(,$v) = each($match[2])) {
  1334.             list(,$ver) = explode(' ', $v, 2);
  1335.             if ($i < 0 && version_compare($apcversion, $ver, '>=')) {
  1336.                 break;
  1337.             } else if (!$i--) {
  1338.                 break;
  1339.             }
  1340.             echo "<b><a href=\"http://pecl.php.net/package/APC/$ver\">".htmlspecialchars($v, ENT_QUOTES, 'UTF-8')."</a></b><br><blockquote>";
  1341.             echo nl2br(htmlspecialchars(current($match[2]), ENT_QUOTES, 'UTF-8'))."</blockquote>";
  1342.             next($match[2]);
  1343.         }
  1344.         echo '</td></tr>';
  1345.     }
  1346.     echo <<< EOB
  1347.         </tbody></table>
  1348.         </div>
  1349. EOB;
  1350.     break;
  1351.  
  1352. }
  1353.  
  1354. echo <<< EOB
  1355.     </div>
  1356. EOB;
  1357.  
  1358. ?>
  1359.  
  1360. <!-- <?php echo "\nBased on APCGUI By R.Becker\n$VERSION\n"?> -->
  1361. </body>
  1362. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement