File indexing completed on 2024-05-12 05:58:26

0001 <?php
0002 /*
0003   +----------------------------------------------------------------------+
0004   | APC                                                                  |
0005   +----------------------------------------------------------------------+
0006   | Copyright (c) 2006-2011 The PHP Group                                |
0007   +----------------------------------------------------------------------+
0008   | This source file is subject to version 3.01 of the PHP license,      |
0009   | that is bundled with this package in the file LICENSE, and is        |
0010   | available through the world-wide-web at the following url:           |
0011   | http://www.php.net/license/3_01.txt                                  |
0012   | If you did not receive a copy of the PHP license and are unable to   |
0013   | obtain it through the world-wide-web, please send a note to          |
0014   | license@php.net so we can mail you a copy immediately.               |
0015   +----------------------------------------------------------------------+
0016   | Authors: Ralf Becker <beckerr@php.net>                               |
0017   |          Rasmus Lerdorf <rasmus@php.net>                             |
0018   |          Ilia Alshanetsky <ilia@prohost.org>                         |
0019   +----------------------------------------------------------------------+
0020 
0021    All other licensing and usage conditions are those of the PHP Group.
0022 
0023  */
0024 
0025 $VERSION='$Id$';
0026 
0027 ////////// READ OPTIONAL CONFIGURATION FILE ////////////
0028 if (file_exists("apc.conf.php")) include("apc.conf.php");
0029 ////////////////////////////////////////////////////////
0030 
0031 ////////// BEGIN OF DEFAULT CONFIG AREA ///////////////////////////////////////////////////////////
0032 
0033 defaults('USE_AUTHENTICATION',0);     // Use (internal) authentication - best choice if 
0034                       // no other authentication is available
0035                       // If set to 0:
0036                       //  There will be no further authentication. You 
0037                       //  will have to handle this by yourself!
0038                       // If set to 1:
0039                       //  You need to change ADMIN_PASSWORD to make
0040                       //  this work!
0041 defaults('ADMIN_USERNAME','apc');       // Admin Username
0042 defaults('ADMIN_PASSWORD','password');    // Admin Password - CHANGE THIS TO ENABLE!!!
0043 
0044 // (beckerr) I'm using a clear text password here, because I've no good idea how to let 
0045 //           users generate a md5 or crypt password in a easy way to fill it in above
0046 
0047 //defaults('DATE_FORMAT', "d.m.Y H:i:s"); // German
0048 defaults('DATE_FORMAT', 'Y/m/d H:i:s');   // US
0049 
0050 defaults('GRAPH_SIZE',200);         // Image size
0051 
0052 //defaults('PROXY', 'tcp://127.0.0.1:8080');
0053 
0054 ////////// END OF DEFAULT CONFIG AREA /////////////////////////////////////////////////////////////
0055 
0056 
0057 // "define if not defined"
0058 function defaults($d,$v) {
0059   if (!defined($d)) define($d,$v); // or just @define(...)
0060 }
0061 
0062 // rewrite $PHP_SELF to block XSS attacks
0063 //
0064 $PHP_SELF= isset($_SERVER['PHP_SELF']) ? htmlentities(strip_tags($_SERVER['PHP_SELF'],''), ENT_QUOTES, 'UTF-8') : '';
0065 $time = time();
0066 $host = php_uname('n');
0067 if($host) { $host = '('.$host.')'; }
0068 if (isset($_SERVER['SERVER_ADDR'])) {
0069   $host .= ' ('.$_SERVER['SERVER_ADDR'].')';
0070 }
0071 
0072 // operation constants
0073 define('OB_HOST_STATS',1);
0074 define('OB_SYS_CACHE',2);
0075 define('OB_USER_CACHE',3);
0076 define('OB_SYS_CACHE_DIR',4);
0077 define('OB_VERSION_CHECK',9);
0078 
0079 // check validity of input variables
0080 $vardom=array(
0081   'OB'  => '/^\d+$/',     // operational mode switch
0082   'CC'  => '/^[01]$/',      // clear cache requested
0083   'DU'  => '/^.*$/',      // Delete User Key
0084   'SH'  => '/^[a-z0-9]+$/',   // shared object description
0085 
0086   'IMG' => '/^[123]$/',     // image to generate
0087   'LO'  => '/^1$/',       // login requested
0088 
0089   'COUNT' => '/^\d+$/',     // number of line displayed in list
0090   'SCOPE' => '/^[AD]$/',      // list view scope
0091   'SORT1' => '/^[AHSMCDTZ]$/',  // first sort key
0092   'SORT2' => '/^[DA]$/',      // second sort key
0093   'AGGR'  => '/^\d+$/',     // aggregation by dir level
0094   'SEARCH'  => '~^[a-zA-Z0-9/_.-]*$~'     // aggregation by dir level
0095 );
0096 
0097 // default cache mode
0098 $cache_mode='opcode';
0099 
0100 // cache scope
0101 $scope_list=array(
0102   'A' => 'cache_list',
0103   'D' => 'deleted_list'
0104 );
0105 
0106 // handle POST and GET requests
0107 if (empty($_REQUEST)) {
0108   if (!empty($_GET) && !empty($_POST)) {
0109     $_REQUEST = array_merge($_GET, $_POST);
0110   } else if (!empty($_GET)) {
0111     $_REQUEST = $_GET;
0112   } else if (!empty($_POST)) {
0113     $_REQUEST = $_POST;
0114   } else {
0115     $_REQUEST = array();
0116   }
0117 }
0118 
0119 // check parameter syntax
0120 foreach($vardom as $var => $dom) {
0121   if (!isset($_REQUEST[$var])) {
0122     $MYREQUEST[$var]=NULL;
0123   } else if (!is_array($_REQUEST[$var]) && preg_match($dom.'D',$_REQUEST[$var])) {
0124     $MYREQUEST[$var]=$_REQUEST[$var];
0125   } else {
0126     $MYREQUEST[$var]=$_REQUEST[$var]=NULL;
0127   }
0128 }
0129 
0130 // check parameter sematics
0131 if (empty($MYREQUEST['SCOPE'])) $MYREQUEST['SCOPE']="A";
0132 if (empty($MYREQUEST['SORT1'])) $MYREQUEST['SORT1']="H";
0133 if (empty($MYREQUEST['SORT2'])) $MYREQUEST['SORT2']="D";
0134 if (empty($MYREQUEST['OB']))  $MYREQUEST['OB']=OB_HOST_STATS;
0135 if (!isset($MYREQUEST['COUNT'])) $MYREQUEST['COUNT']=20;
0136 if (!isset($scope_list[$MYREQUEST['SCOPE']])) $MYREQUEST['SCOPE']='A';
0137 
0138 $MY_SELF=
0139   "$PHP_SELF".
0140   "?SCOPE=".$MYREQUEST['SCOPE'].
0141   "&SORT1=".$MYREQUEST['SORT1'].
0142   "&SORT2=".$MYREQUEST['SORT2'].
0143   "&COUNT=".$MYREQUEST['COUNT'];
0144 $MY_SELF_WO_SORT=
0145   "$PHP_SELF".
0146   "?SCOPE=".$MYREQUEST['SCOPE'].
0147   "&COUNT=".$MYREQUEST['COUNT'];
0148 
0149 // authentication needed?
0150 //
0151 if (!USE_AUTHENTICATION) {
0152   $AUTHENTICATED=1;
0153 } else {
0154   $AUTHENTICATED=0;
0155   if (ADMIN_PASSWORD!='password' && ($MYREQUEST['LO'] == 1 || isset($_SERVER['PHP_AUTH_USER']))) {
0156 
0157     if (!isset($_SERVER['PHP_AUTH_USER']) ||
0158       !isset($_SERVER['PHP_AUTH_PW']) ||
0159       $_SERVER['PHP_AUTH_USER'] != ADMIN_USERNAME ||
0160       $_SERVER['PHP_AUTH_PW'] != ADMIN_PASSWORD) {
0161       Header("WWW-Authenticate: Basic realm=\"APC Login\"");
0162       Header("HTTP/1.0 401 Unauthorized");
0163 
0164       echo <<<EOB
0165         <html><body>
0166         <h1>Rejected!</h1>
0167         <big>Wrong Username or Password!</big><br/>&nbsp;<br/>&nbsp;
0168         <big><a href='$PHP_SELF?OB={$MYREQUEST['OB']}'>Continue...</a></big>
0169         </body></html>
0170 EOB;
0171       exit;
0172       
0173     } else {
0174       $AUTHENTICATED=1;
0175     }
0176   }
0177 }
0178   
0179 // select cache mode
0180 if ($AUTHENTICATED && $MYREQUEST['OB'] == OB_USER_CACHE) {
0181   $cache_mode='user';
0182 }
0183 // clear cache
0184 if ($AUTHENTICATED && isset($MYREQUEST['CC']) && $MYREQUEST['CC']) {
0185   apc_clear_cache($cache_mode);
0186 }
0187 
0188 if ($AUTHENTICATED && !empty($MYREQUEST['DU'])) {
0189   apc_delete($MYREQUEST['DU']);
0190 }
0191 
0192 if(!function_exists('apc_cache_info') || !($cache=@apc_cache_info($cache_mode))) {
0193   echo "No cache info available.  APC does not appear to be running.";
0194   exit;
0195 }
0196 
0197 $cache_user = apc_cache_info('user', 1);  
0198 $mem=apc_sma_info();
0199 if(!$cache['num_hits']) { $cache['num_hits']=1; $time++; }  // Avoid division by 0 errors on a cache clear
0200 
0201 // don't cache this page
0202 //
0203 header("Cache-Control: no-store, no-cache, must-revalidate");  // HTTP/1.1
0204 header("Cache-Control: post-check=0, pre-check=0", false);
0205 header("Pragma: no-cache");                                    // HTTP/1.0
0206 
0207 function duration($ts) {
0208     global $time;
0209     $years = (int)((($time - $ts)/(7*86400))/52.177457);
0210     $rem = (int)(($time-$ts)-($years * 52.177457 * 7 * 86400));
0211     $weeks = (int)(($rem)/(7*86400));
0212     $days = (int)(($rem)/86400) - $weeks*7;
0213     $hours = (int)(($rem)/3600) - $days*24 - $weeks*7*24;
0214     $mins = (int)(($rem)/60) - $hours*60 - $days*24*60 - $weeks*7*24*60;
0215     $str = '';
0216     if($years==1) $str .= "$years year, ";
0217     if($years>1) $str .= "$years years, ";
0218     if($weeks==1) $str .= "$weeks week, ";
0219     if($weeks>1) $str .= "$weeks weeks, ";
0220     if($days==1) $str .= "$days day,";
0221     if($days>1) $str .= "$days days,";
0222     if($hours == 1) $str .= " $hours hour and";
0223     if($hours>1) $str .= " $hours hours and";
0224     if($mins == 1) $str .= " 1 minute";
0225     else $str .= " $mins minutes";
0226     return $str;
0227 }
0228 
0229 // create graphics
0230 //
0231 function graphics_avail() {
0232   return extension_loaded('gd');
0233 }
0234 if (isset($MYREQUEST['IMG']))
0235 {
0236   if (!graphics_avail()) {
0237     exit(0);
0238   }
0239 
0240   function fill_arc($im, $centerX, $centerY, $diameter, $start, $end, $color1,$color2,$text='',$placeindex=0) {
0241     $r=$diameter/2;
0242     $w=deg2rad((360+$start+($end-$start)/2)%360);
0243 
0244     
0245     if (function_exists("imagefilledarc")) {
0246       // exists only if GD 2.0.1 is avaliable
0247       imagefilledarc($im, $centerX+1, $centerY+1, $diameter, $diameter, $start, $end, $color1, IMG_ARC_PIE);
0248       imagefilledarc($im, $centerX, $centerY, $diameter, $diameter, $start, $end, $color2, IMG_ARC_PIE);
0249       imagefilledarc($im, $centerX, $centerY, $diameter, $diameter, $start, $end, $color1, IMG_ARC_NOFILL|IMG_ARC_EDGED);
0250     } else {
0251       imagearc($im, $centerX, $centerY, $diameter, $diameter, $start, $end, $color2);
0252       imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($start)) * $r, $centerY + sin(deg2rad($start)) * $r, $color2);
0253       imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($start+1)) * $r, $centerY + sin(deg2rad($start)) * $r, $color2);
0254       imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($end-1))   * $r, $centerY + sin(deg2rad($end))   * $r, $color2);
0255       imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($end))   * $r, $centerY + sin(deg2rad($end))   * $r, $color2);
0256       imagefill($im,$centerX + $r*cos($w)/2, $centerY + $r*sin($w)/2, $color2);
0257     }
0258     if ($text) {
0259       if ($placeindex>0) {
0260         imageline($im,$centerX + $r*cos($w)/2, $centerY + $r*sin($w)/2,$diameter, $placeindex*12,$color1);
0261         imagestring($im,4,$diameter, $placeindex*12,$text,$color1); 
0262         
0263       } else {
0264         imagestring($im,4,$centerX + $r*cos($w)/2, $centerY + $r*sin($w)/2,$text,$color1);
0265       }
0266     }
0267   } 
0268 
0269   function text_arc($im, $centerX, $centerY, $diameter, $start, $end, $color1,$text,$placeindex=0) {
0270     $r=$diameter/2;
0271     $w=deg2rad((360+$start+($end-$start)/2)%360);
0272 
0273     if ($placeindex>0) {
0274       imageline($im,$centerX + $r*cos($w)/2, $centerY + $r*sin($w)/2,$diameter, $placeindex*12,$color1);
0275       imagestring($im,4,$diameter, $placeindex*12,$text,$color1); 
0276         
0277     } else {
0278       imagestring($im,4,$centerX + $r*cos($w)/2, $centerY + $r*sin($w)/2,$text,$color1);
0279     }
0280   } 
0281   
0282   function fill_box($im, $x, $y, $w, $h, $color1, $color2,$text='',$placeindex='') {
0283     global $col_black;
0284     $x1=$x+$w-1;
0285     $y1=$y+$h-1;
0286 
0287     imagerectangle($im, $x, $y1, $x1+1, $y+1, $col_black);
0288     if($y1>$y) imagefilledrectangle($im, $x, $y, $x1, $y1, $color2);
0289     else imagefilledrectangle($im, $x, $y1, $x1, $y, $color2);
0290     imagerectangle($im, $x, $y1, $x1, $y, $color1);
0291     if ($text) {
0292       if ($placeindex>0) {
0293       
0294         if ($placeindex<16)
0295         {
0296           $px=5;
0297           $py=$placeindex*12+6;
0298           imagefilledrectangle($im, $px+90, $py+3, $px+90-4, $py-3, $color2);
0299           imageline($im,$x,$y+$h/2,$px+90,$py,$color2);
0300           imagestring($im,2,$px,$py-6,$text,$color1); 
0301           
0302         } else {
0303           if ($placeindex<31) {
0304             $px=$x+40*2;
0305             $py=($placeindex-15)*12+6;
0306           } else {
0307             $px=$x+40*2+100*intval(($placeindex-15)/15);
0308             $py=($placeindex%15)*12+6;
0309           }
0310           imagefilledrectangle($im, $px, $py+3, $px-4, $py-3, $color2);
0311           imageline($im,$x+$w,$y+$h/2,$px,$py,$color2);
0312           imagestring($im,2,$px+2,$py-6,$text,$color1); 
0313         }
0314       } else {
0315         imagestring($im,4,$x+5,$y1-16,$text,$color1);
0316       }
0317     }
0318   }
0319 
0320 
0321   $size = GRAPH_SIZE; // image size
0322   if ($MYREQUEST['IMG']==3)
0323     $image = imagecreate(2*$size+150, $size+10);
0324   else
0325     $image = imagecreate($size+50, $size+10);
0326 
0327   $col_white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
0328   $col_red   = imagecolorallocate($image, 0xD0, 0x60,  0x30);
0329   $col_green = imagecolorallocate($image, 0x60, 0xF0, 0x60);
0330   $col_black = imagecolorallocate($image,   0,   0,   0);
0331   imagecolortransparent($image,$col_white);
0332 
0333   switch ($MYREQUEST['IMG']) {
0334   
0335   case 1:
0336     $s=$mem['num_seg']*$mem['seg_size'];
0337     $a=$mem['avail_mem'];
0338     $x=$y=$size/2;
0339     $fuzz = 0.000001;
0340 
0341     // This block of code creates the pie chart.  It is a lot more complex than you
0342     // would expect because we try to visualize any memory fragmentation as well.
0343     $angle_from = 0;
0344     $string_placement=array();
0345     for($i=0; $i<$mem['num_seg']; $i++) { 
0346       $ptr = 0;
0347       $free = $mem['block_lists'][$i];
0348       uasort($free, 'block_sort');
0349       foreach($free as $block) {
0350         if($block['offset']!=$ptr) {       // Used block
0351           $angle_to = $angle_from+($block['offset']-$ptr)/$s;
0352           if(($angle_to+$fuzz)>1) $angle_to = 1;
0353           if( ($angle_to*360) - ($angle_from*360) >= 1) {
0354             fill_arc($image,$x,$y,$size,$angle_from*360,$angle_to*360,$col_black,$col_red);
0355             if (($angle_to-$angle_from)>0.05) {
0356               array_push($string_placement, array($angle_from,$angle_to));
0357             }
0358           }
0359           $angle_from = $angle_to;
0360         }
0361         $angle_to = $angle_from+($block['size'])/$s;
0362         if(($angle_to+$fuzz)>1) $angle_to = 1;
0363         if( ($angle_to*360) - ($angle_from*360) >= 1) {
0364           fill_arc($image,$x,$y,$size,$angle_from*360,$angle_to*360,$col_black,$col_green);
0365           if (($angle_to-$angle_from)>0.05) {
0366             array_push($string_placement, array($angle_from,$angle_to));
0367           }
0368         }
0369         $angle_from = $angle_to;
0370         $ptr = $block['offset']+$block['size'];
0371       }
0372       if ($ptr < $mem['seg_size']) { // memory at the end 
0373         $angle_to = $angle_from + ($mem['seg_size'] - $ptr)/$s;
0374         if(($angle_to+$fuzz)>1) $angle_to = 1;
0375         fill_arc($image,$x,$y,$size,$angle_from*360,$angle_to*360,$col_black,$col_red);
0376         if (($angle_to-$angle_from)>0.05) {
0377           array_push($string_placement, array($angle_from,$angle_to));
0378         }
0379       }
0380     }
0381     foreach ($string_placement as $angle) {
0382       text_arc($image,$x,$y,$size,$angle[0]*360,$angle[1]*360,$col_black,bsize($s*($angle[1]-$angle[0])));
0383     }
0384     break;
0385     
0386   case 2: 
0387     $s=$cache['num_hits']+$cache['num_misses'];
0388     $a=$cache['num_hits'];
0389     
0390     fill_box($image, 30,$size,50,-$a*($size-21)/$s,$col_black,$col_green,sprintf("%.1f%%",$cache['num_hits']*100/$s));
0391     fill_box($image,130,$size,50,-max(4,($s-$a)*($size-21)/$s),$col_black,$col_red,sprintf("%.1f%%",$cache['num_misses']*100/$s));
0392     break;
0393     
0394   case 3:
0395     $s=$mem['num_seg']*$mem['seg_size'];
0396     $a=$mem['avail_mem'];
0397     $x=130;
0398     $y=1;
0399     $j=1;
0400 
0401     // This block of code creates the bar chart.  It is a lot more complex than you
0402     // would expect because we try to visualize any memory fragmentation as well.
0403     for($i=0; $i<$mem['num_seg']; $i++) { 
0404       $ptr = 0;
0405       $free = $mem['block_lists'][$i];
0406       uasort($free, 'block_sort');
0407       foreach($free as $block) {
0408         if($block['offset']!=$ptr) {       // Used block
0409           $h=(GRAPH_SIZE-5)*($block['offset']-$ptr)/$s;
0410           if ($h>0) {
0411                                                 $j++;
0412             if($j<75) fill_box($image,$x,$y,50,$h,$col_black,$col_red,bsize($block['offset']-$ptr),$j);
0413                                                 else fill_box($image,$x,$y,50,$h,$col_black,$col_red);
0414                                         }
0415           $y+=$h;
0416         }
0417         $h=(GRAPH_SIZE-5)*($block['size'])/$s;
0418         if ($h>0) {
0419                                         $j++;
0420           if($j<75) fill_box($image,$x,$y,50,$h,$col_black,$col_green,bsize($block['size']),$j);
0421           else fill_box($image,$x,$y,50,$h,$col_black,$col_green);
0422                                 }
0423         $y+=$h;
0424         $ptr = $block['offset']+$block['size'];
0425       }
0426       if ($ptr < $mem['seg_size']) { // memory at the end 
0427         $h = (GRAPH_SIZE-5) * ($mem['seg_size'] - $ptr) / $s;
0428         if ($h > 0) {
0429           fill_box($image,$x,$y,50,$h,$col_black,$col_red,bsize($mem['seg_size']-$ptr),$j++);
0430         }
0431       }
0432     }
0433     break;
0434   case 4: 
0435     $s=$cache['num_hits']+$cache['num_misses'];
0436     $a=$cache['num_hits'];
0437             
0438     fill_box($image, 30,$size,50,-$a*($size-21)/$s,$col_black,$col_green,sprintf("%.1f%%",$cache['num_hits']*100/$s));
0439     fill_box($image,130,$size,50,-max(4,($s-$a)*($size-21)/$s),$col_black,$col_red,sprintf("%.1f%%",$cache['num_misses']*100/$s));
0440     break;
0441   
0442   }
0443   header("Content-type: image/png");
0444   imagepng($image);
0445   exit;
0446 }
0447 
0448 // pretty printer for byte values
0449 //
0450 function bsize($s) {
0451   foreach (array('','K','M','G') as $i => $k) {
0452     if ($s < 1024) break;
0453     $s/=1024;
0454   }
0455   return sprintf("%5.1f %sBytes",$s,$k);
0456 }
0457 
0458 // sortable table header in "scripts for this host" view
0459 function sortheader($key,$name,$extra='') {
0460   global $MYREQUEST, $MY_SELF_WO_SORT;
0461   
0462   if ($MYREQUEST['SORT1']==$key) {
0463     $MYREQUEST['SORT2'] = $MYREQUEST['SORT2']=='A' ? 'D' : 'A';
0464   }
0465   return "<a class=sortable href=\"$MY_SELF_WO_SORT$extra&SORT1=$key&SORT2=".$MYREQUEST['SORT2']."\">$name</a>";
0466 
0467 }
0468 
0469 // create menu entry 
0470 function menu_entry($ob,$title) {
0471   global $MYREQUEST,$MY_SELF;
0472   if ($MYREQUEST['OB']!=$ob) {
0473     return "<li><a href=\"$MY_SELF&OB=$ob\">$title</a></li>";
0474   } else if (empty($MYREQUEST['SH'])) {
0475     return "<li><span class=active>$title</span></li>";
0476   } else {
0477     return "<li><a class=\"child_active\" href=\"$MY_SELF&OB=$ob\">$title</a></li>";  
0478   }
0479 }
0480 
0481 function put_login_link($s="Login")
0482 {
0483   global $MY_SELF,$MYREQUEST,$AUTHENTICATED;
0484   // needs ADMIN_PASSWORD to be changed!
0485   //
0486   if (!USE_AUTHENTICATION) {
0487     return;
0488   } else if (ADMIN_PASSWORD=='password')
0489   {
0490     print <<<EOB
0491       <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>
0492 EOB;
0493   } else if ($AUTHENTICATED) {
0494     print <<<EOB
0495       '{$_SERVER['PHP_AUTH_USER']}'&nbsp;logged&nbsp;in!
0496 EOB;
0497   } else{
0498     print <<<EOB
0499       <a href="$MY_SELF&LO=1&OB={$MYREQUEST['OB']}">$s</a>
0500 EOB;
0501   }
0502 }
0503 
0504 function block_sort($array1, $array2)
0505 {
0506   if ($array1['offset'] > $array2['offset']) {
0507     return 1;
0508   } else {
0509     return -1;
0510   }
0511 }
0512 
0513 
0514 ?>
0515 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
0516 <html>
0517 <head><title>APC INFO <?php echo $host ?></title>
0518 <style><!--
0519 body { background:white; font-size:100.01%; margin:0; padding:0; }
0520 body,p,td,th,input,submit { font-size:0.8em;font-family:arial,helvetica,sans-serif; }
0521 * html body   {font-size:0.8em}
0522 * html p      {font-size:0.8em}
0523 * html td     {font-size:0.8em}
0524 * html th     {font-size:0.8em}
0525 * html input  {font-size:0.8em}
0526 * html submit {font-size:0.8em}
0527 td { vertical-align:top }
0528 a { color:black; font-weight:none; text-decoration:none; }
0529 a:hover { text-decoration:underline; }
0530 div.content { padding:1em 1em 1em 1em; position:absolute; width:97%; z-index:100; }
0531 
0532 
0533 div.head div.login {
0534   position:absolute;
0535   right: 1em;
0536   top: 1.2em;
0537   color:white;
0538   width:6em;
0539   }
0540 div.head div.login a {
0541   position:absolute;
0542   right: 0em;
0543   background:rgb(119,123,180);
0544   border:solid rgb(102,102,153) 2px;
0545   color:white;
0546   font-weight:bold;
0547   padding:0.1em 0.5em 0.1em 0.5em;
0548   text-decoration:none;
0549   }
0550 div.head div.login a:hover {
0551   background:rgb(193,193,244);
0552   }
0553 
0554 h1.apc { background:rgb(153,153,204); margin:0; padding:0.5em 1em 0.5em 1em; }
0555 * html h1.apc { margin-bottom:-7px; }
0556 h1.apc a:hover { text-decoration:none; color:rgb(90,90,90); }
0557 h1.apc div.logo span.logo {
0558   background:rgb(119,123,180);
0559   color:black;
0560   border-right: solid black 1px;
0561   border-bottom: solid black 1px;
0562   font-style:italic;
0563   font-size:1em;
0564   padding-left:1.2em;
0565   padding-right:1.2em;
0566   text-align:right;
0567   }
0568 h1.apc div.logo span.name { color:white; font-size:0.7em; padding:0 0.8em 0 2em; }
0569 h1.apc div.nameinfo { color:white; display:inline; font-size:0.4em; margin-left: 3em; }
0570 h1.apc div.copy { color:black; font-size:0.4em; position:absolute; right:1em; }
0571 hr.apc {
0572   background:white;
0573   border-bottom:solid rgb(102,102,153) 1px;
0574   border-style:none;
0575   border-top:solid rgb(102,102,153) 10px;
0576   height:12px;
0577   margin:0;
0578   margin-top:1px;
0579   padding:0;
0580 }
0581 
0582 ol,menu { margin:1em 0 0 0; padding:0.2em; margin-left:1em;}
0583 ol.menu li { display:inline; margin-right:0.7em; list-style:none; font-size:85%}
0584 ol.menu a {
0585   background:rgb(153,153,204);
0586   border:solid rgb(102,102,153) 2px;
0587   color:white;
0588   font-weight:bold;
0589   margin-right:0em;
0590   padding:0.1em 0.5em 0.1em 0.5em;
0591   text-decoration:none;
0592   margin-left: 5px;
0593   }
0594 ol.menu a.child_active {
0595   background:rgb(153,153,204);
0596   border:solid rgb(102,102,153) 2px;
0597   color:white;
0598   font-weight:bold;
0599   margin-right:0em;
0600   padding:0.1em 0.5em 0.1em 0.5em;
0601   text-decoration:none;
0602   border-left: solid black 5px;
0603   margin-left: 0px;
0604   }
0605 ol.menu span.active {
0606   background:rgb(153,153,204);
0607   border:solid rgb(102,102,153) 2px;
0608   color:black;
0609   font-weight:bold;
0610   margin-right:0em;
0611   padding:0.1em 0.5em 0.1em 0.5em;
0612   text-decoration:none;
0613   border-left: solid black 5px;
0614   }
0615 ol.menu span.inactive {
0616   background:rgb(193,193,244);
0617   border:solid rgb(182,182,233) 2px;
0618   color:white;
0619   font-weight:bold;
0620   margin-right:0em;
0621   padding:0.1em 0.5em 0.1em 0.5em;
0622   text-decoration:none;
0623   margin-left: 5px;
0624   }
0625 ol.menu a:hover {
0626   background:rgb(193,193,244);
0627   text-decoration:none;
0628   }
0629   
0630   
0631 div.info {
0632   background:rgb(204,204,204);
0633   border:solid rgb(204,204,204) 1px;
0634   margin-bottom:1em;
0635   }
0636 div.info h2 {
0637   background:rgb(204,204,204);
0638   color:black;
0639   font-size:1em;
0640   margin:0;
0641   padding:0.1em 1em 0.1em 1em;
0642   }
0643 div.info table {
0644   border:solid rgb(204,204,204) 1px;
0645   border-spacing:0;
0646   width:100%;
0647   }
0648 div.info table th {
0649   background:rgb(204,204,204);
0650   color:white;
0651   margin:0;
0652   padding:0.1em 1em 0.1em 1em;
0653   }
0654 div.info table th a.sortable { color:black; }
0655 div.info table tr.tr-0 { background:rgb(238,238,238); }
0656 div.info table tr.tr-1 { background:rgb(221,221,221); }
0657 div.info table td { padding:0.3em 1em 0.3em 1em; }
0658 div.info table td.td-0 { border-right:solid rgb(102,102,153) 1px; white-space:nowrap; }
0659 div.info table td.td-n { border-right:solid rgb(102,102,153) 1px; }
0660 div.info table td h3 {
0661   color:black;
0662   font-size:1.1em;
0663   margin-left:-0.3em;
0664   }
0665 
0666 div.graph { margin-bottom:1em }
0667 div.graph h2 { background:rgb(204,204,204);; color:black; font-size:1em; margin:0; padding:0.1em 1em 0.1em 1em; }
0668 div.graph table { border:solid rgb(204,204,204) 1px; color:black; font-weight:normal; width:100%; }
0669 div.graph table td.td-0 { background:rgb(238,238,238); }
0670 div.graph table td.td-1 { background:rgb(221,221,221); }
0671 div.graph table td { padding:0.2em 1em 0.4em 1em; }
0672 
0673 div.div1,div.div2 { margin-bottom:1em; width:35em; }
0674 div.div3 { position:absolute; left:40em; top:1em; width:580px; }
0675 //div.div3 { position:absolute; left:37em; top:1em; right:1em; }
0676 
0677 div.sorting { margin:1.5em 0em 1.5em 2em }
0678 .center { text-align:center }
0679 .aright { position:absolute;right:1em }
0680 .right { text-align:right }
0681 .ok { color:rgb(0,200,0); font-weight:bold}
0682 .failed { color:rgb(200,0,0); font-weight:bold}
0683 
0684 span.box {
0685   border: black solid 1px;
0686   border-right:solid black 2px;
0687   border-bottom:solid black 2px;
0688   padding:0 0.5em 0 0.5em;
0689   margin-right:1em;
0690 }
0691 span.green { background:#60F060; padding:0 0.5em 0 0.5em}
0692 span.red { background:#D06030; padding:0 0.5em 0 0.5em }
0693 
0694 div.authneeded {
0695   background:rgb(238,238,238);
0696   border:solid rgb(204,204,204) 1px;
0697   color:rgb(200,0,0);
0698   font-size:1.2em;
0699   font-weight:bold;
0700   padding:2em;
0701   text-align:center;
0702   }
0703   
0704 input {
0705   background:rgb(153,153,204);
0706   border:solid rgb(102,102,153) 2px;
0707   color:white;
0708   font-weight:bold;
0709   margin-right:1em;
0710   padding:0.1em 0.5em 0.1em 0.5em;
0711   }
0712 //-->
0713 </style>
0714 </head>
0715 <body>
0716 <div class="head">
0717   <h1 class="apc">
0718     <div class="logo"><span class="logo"><a href="http://pecl.php.net/package/APC">APC</a></span></div>
0719     <div class="nameinfo">Opcode Cache</div>
0720   </h1>
0721   <div class="login">
0722   <?php put_login_link(); ?>
0723   </div>
0724   <hr class="apc">
0725 </div>
0726 <?php
0727 
0728 
0729 // Display main Menu
0730 echo <<<EOB
0731   <ol class=menu>
0732   <li><a href="$MY_SELF&OB={$MYREQUEST['OB']}&SH={$MYREQUEST['SH']}">Refresh Data</a></li>
0733 EOB;
0734 echo
0735   menu_entry(1,'View Host Stats'),
0736   menu_entry(2,'System Cache Entries');
0737 if ($AUTHENTICATED) {
0738   echo menu_entry(4,'Per-Directory Entries');
0739 }
0740 echo
0741   menu_entry(3,'User Cache Entries'),
0742   menu_entry(9,'Version Check');
0743   
0744 if ($AUTHENTICATED) {
0745   echo <<<EOB
0746     <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>
0747 EOB;
0748 }
0749 echo <<<EOB
0750   </ol>
0751 EOB;
0752 
0753 
0754 // CONTENT
0755 echo <<<EOB
0756   <div class=content>
0757 EOB;
0758 
0759 // MAIN SWITCH STATEMENT 
0760 
0761 switch ($MYREQUEST['OB']) {
0762 
0763 
0764 
0765 
0766 
0767 // -----------------------------------------------
0768 // Host Stats
0769 // -----------------------------------------------
0770 case OB_HOST_STATS:
0771   $mem_size = $mem['num_seg']*$mem['seg_size'];
0772   $mem_avail= $mem['avail_mem'];
0773   $mem_used = $mem_size-$mem_avail;
0774   $seg_size = bsize($mem['seg_size']);
0775   $req_rate = sprintf("%.2f",($cache['num_hits']+$cache['num_misses'])/($time-$cache['start_time']));
0776   $hit_rate = sprintf("%.2f",($cache['num_hits'])/($time-$cache['start_time']));
0777   $miss_rate = sprintf("%.2f",($cache['num_misses'])/($time-$cache['start_time']));
0778   $insert_rate = sprintf("%.2f",($cache['num_inserts'])/($time-$cache['start_time']));
0779   $req_rate_user = sprintf("%.2f",($cache_user['num_hits']+$cache_user['num_misses'])/($time-$cache_user['start_time']));
0780   $hit_rate_user = sprintf("%.2f",($cache_user['num_hits'])/($time-$cache_user['start_time']));
0781   $miss_rate_user = sprintf("%.2f",($cache_user['num_misses'])/($time-$cache_user['start_time']));
0782   $insert_rate_user = sprintf("%.2f",($cache_user['num_inserts'])/($time-$cache_user['start_time']));
0783   $apcversion = phpversion('apc');
0784   $phpversion = phpversion();
0785   $number_files = $cache['num_entries']; 
0786     $size_files = bsize($cache['mem_size']);
0787   $number_vars = $cache_user['num_entries'];
0788     $size_vars = bsize($cache_user['mem_size']);
0789   $i=0;
0790   echo <<< EOB
0791     <div class="info div1"><h2>General Cache Information</h2>
0792     <table cellspacing=0><tbody>
0793     <tr class=tr-0><td class=td-0>APC Version</td><td>$apcversion</td></tr>
0794     <tr class=tr-1><td class=td-0>PHP Version</td><td>$phpversion</td></tr>
0795 EOB;
0796 
0797   if(!empty($_SERVER['SERVER_NAME']))
0798     echo "<tr class=tr-0><td class=td-0>APC Host</td><td>{$_SERVER['SERVER_NAME']} $host</td></tr>\n";
0799   if(!empty($_SERVER['SERVER_SOFTWARE']))
0800     echo "<tr class=tr-1><td class=td-0>Server Software</td><td>{$_SERVER['SERVER_SOFTWARE']}</td></tr>\n";
0801 
0802   echo <<<EOB
0803     <tr class=tr-0><td class=td-0>Shared Memory</td><td>{$mem['num_seg']} Segment(s) with $seg_size 
0804     <br/> ({$cache['memory_type']} memory, {$cache['locking_type']} locking)
0805     </td></tr>
0806 EOB;
0807   echo   '<tr class=tr-1><td class=td-0>Start Time</td><td>',date(DATE_FORMAT,$cache['start_time']),'</td></tr>';
0808   echo   '<tr class=tr-0><td class=td-0>Uptime</td><td>',duration($cache['start_time']),'</td></tr>';
0809   echo   '<tr class=tr-1><td class=td-0>File Upload Support</td><td>',$cache['file_upload_progress'],'</td></tr>';
0810   echo <<<EOB
0811     </tbody></table>
0812     </div>
0813 
0814     <div class="info div1"><h2>File Cache Information</h2>
0815     <table cellspacing=0><tbody>
0816     <tr class=tr-0><td class=td-0>Cached Files</td><td>$number_files ($size_files)</td></tr>
0817     <tr class=tr-1><td class=td-0>Hits</td><td>{$cache['num_hits']}</td></tr>
0818     <tr class=tr-0><td class=td-0>Misses</td><td>{$cache['num_misses']}</td></tr>
0819     <tr class=tr-1><td class=td-0>Request Rate (hits, misses)</td><td>$req_rate cache requests/second</td></tr>
0820     <tr class=tr-0><td class=td-0>Hit Rate</td><td>$hit_rate cache requests/second</td></tr>
0821     <tr class=tr-1><td class=td-0>Miss Rate</td><td>$miss_rate cache requests/second</td></tr>
0822     <tr class=tr-0><td class=td-0>Insert Rate</td><td>$insert_rate cache requests/second</td></tr>
0823     <tr class=tr-1><td class=td-0>Cache full count</td><td>{$cache['expunges']}</td></tr>
0824     </tbody></table>
0825     </div>
0826 
0827     <div class="info div1"><h2>User Cache Information</h2>
0828     <table cellspacing=0><tbody>
0829     <tr class=tr-0><td class=td-0>Cached Variables</td><td>$number_vars ($size_vars)</td></tr>
0830     <tr class=tr-1><td class=td-0>Hits</td><td>{$cache_user['num_hits']}</td></tr>
0831     <tr class=tr-0><td class=td-0>Misses</td><td>{$cache_user['num_misses']}</td></tr>
0832     <tr class=tr-1><td class=td-0>Request Rate (hits, misses)</td><td>$req_rate_user cache requests/second</td></tr>
0833     <tr class=tr-0><td class=td-0>Hit Rate</td><td>$hit_rate_user cache requests/second</td></tr>
0834     <tr class=tr-1><td class=td-0>Miss Rate</td><td>$miss_rate_user cache requests/second</td></tr>
0835     <tr class=tr-0><td class=td-0>Insert Rate</td><td>$insert_rate_user cache requests/second</td></tr>
0836     <tr class=tr-1><td class=td-0>Cache full count</td><td>{$cache_user['expunges']}</td></tr>
0837 
0838     </tbody></table>
0839     </div>
0840 
0841     <div class="info div2"><h2>Runtime Settings</h2><table cellspacing=0><tbody>
0842 EOB;
0843 
0844   $j = 0;
0845   foreach (ini_get_all('apc') as $k => $v) {
0846     echo "<tr class=tr-$j><td class=td-0>",$k,"</td><td>",str_replace(',',',<br />',$v['local_value']),"</td></tr>\n";
0847     $j = 1 - $j;
0848   }
0849 
0850   if($mem['num_seg']>1 || $mem['num_seg']==1 && count($mem['block_lists'][0])>1)
0851     $mem_note = "Memory Usage<br /><font size=-2>(multiple slices indicate fragments)</font>";
0852   else
0853     $mem_note = "Memory Usage";
0854 
0855   echo <<< EOB
0856     </tbody></table>
0857     </div>
0858 
0859     <div class="graph div3"><h2>Host Status Diagrams</h2>
0860     <table cellspacing=0><tbody>
0861 EOB;
0862   $size='width='.(GRAPH_SIZE+50).' height='.(GRAPH_SIZE+10);
0863   echo <<<EOB
0864     <tr>
0865     <td class=td-0>$mem_note</td>
0866     <td class=td-1>Hits &amp; Misses</td>
0867     </tr>
0868 EOB;
0869 
0870   echo
0871     graphics_avail() ? 
0872         '<tr>'.
0873         "<td class=td-0><img alt=\"\" $size src=\"$PHP_SELF?IMG=1&$time\"></td>".
0874         "<td class=td-1><img alt=\"\" $size src=\"$PHP_SELF?IMG=2&$time\"></td></tr>\n"
0875       : "",
0876     '<tr>',
0877     '<td class=td-0><span class="green box">&nbsp;</span>Free: ',bsize($mem_avail).sprintf(" (%.1f%%)",$mem_avail*100/$mem_size),"</td>\n",
0878     '<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",
0879     '</tr>',
0880     '<tr>',
0881     '<td class=td-0><span class="red box">&nbsp;</span>Used: ',bsize($mem_used ).sprintf(" (%.1f%%)",$mem_used *100/$mem_size),"</td>\n",
0882     '<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";
0883   echo <<< EOB
0884     </tr>
0885     </tbody></table>
0886 
0887     <br/>
0888     <h2>Detailed Memory Usage and Fragmentation</h2>
0889     <table cellspacing=0><tbody>
0890     <tr>
0891     <td class=td-0 colspan=2><br/>
0892 EOB;
0893 
0894   // Fragementation: (freeseg - 1) / total_seg
0895   $nseg = $freeseg = $fragsize = $freetotal = 0;
0896   for($i=0; $i<$mem['num_seg']; $i++) {
0897     $ptr = 0;
0898     foreach($mem['block_lists'][$i] as $block) {
0899       if ($block['offset'] != $ptr) {
0900         ++$nseg;
0901       }
0902       $ptr = $block['offset'] + $block['size'];
0903                         /* Only consider blocks <5M for the fragmentation % */
0904                         if($block['size']<(5*1024*1024)) $fragsize+=$block['size'];
0905                         $freetotal+=$block['size'];
0906     }
0907     $freeseg += count($mem['block_lists'][$i]);
0908   }
0909   
0910   if ($freeseg > 1) {
0911     $frag = sprintf("%.2f%% (%s out of %s in %d fragments)", ($fragsize/$freetotal)*100,bsize($fragsize),bsize($freetotal),$freeseg);
0912   } else {
0913     $frag = "0%";
0914   }
0915 
0916   if (graphics_avail()) {
0917     $size='width='.(2*GRAPH_SIZE+150).' height='.(GRAPH_SIZE+10);
0918     echo <<<EOB
0919       <img alt="" $size src="$PHP_SELF?IMG=3&$time">
0920 EOB;
0921   }
0922   echo <<<EOB
0923     </br>Fragmentation: $frag
0924     </td>
0925     </tr>
0926 EOB;
0927         if(isset($mem['adist'])) {
0928           foreach($mem['adist'] as $i=>$v) {
0929             $cur = pow(2,$i); $nxt = pow(2,$i+1)-1;
0930             if($i==0) $range = "1";
0931             else $range = "$cur - $nxt";
0932             echo "<tr><th align=right>$range</th><td align=right>$v</td></tr>\n";
0933           }
0934         }
0935         echo <<<EOB
0936     </tbody></table>
0937     </div>
0938 EOB;
0939     
0940   break;
0941 
0942 
0943 // -----------------------------------------------
0944 // User Cache Entries
0945 // -----------------------------------------------
0946 case OB_USER_CACHE:
0947   if (!$AUTHENTICATED) {
0948     echo '<div class="error">You need to login to see the user values here!<br/>&nbsp;<br/>';
0949     put_login_link("Login now!");
0950     echo '</div>';
0951     break;
0952   }
0953   $fieldname='info';
0954   $fieldheading='User Entry Label';
0955   $fieldkey='info';
0956 
0957 // -----------------------------------------------
0958 // System Cache Entries   
0959 // -----------------------------------------------
0960 case OB_SYS_CACHE:  
0961   if (!isset($fieldname))
0962   {
0963     $fieldname='filename';
0964     $fieldheading='Script Filename';
0965     if(ini_get("apc.stat")) $fieldkey='inode';
0966     else $fieldkey='filename'; 
0967   }
0968   if (!empty($MYREQUEST['SH']))
0969   {
0970     echo <<< EOB
0971       <div class="info"><table cellspacing=0><tbody>
0972       <tr><th>Attribute</th><th>Value</th></tr>
0973 EOB;
0974 
0975     $m=0;
0976     foreach($scope_list as $j => $list) {
0977       foreach($cache[$list] as $i => $entry) {
0978         if (md5($entry[$fieldkey])!=$MYREQUEST['SH']) continue;
0979         foreach($entry as $k => $value) {
0980           if (!$AUTHENTICATED) {
0981             // hide all path entries if not logged in
0982             $value=preg_replace('/^.*(\\/|\\\\)/','<i>&lt;hidden&gt;</i>/',$value);
0983           }
0984 
0985           if ($k == "num_hits") {
0986             $value=sprintf("%s (%.2f%%)",$value,$value*100/$cache['num_hits']);
0987           }
0988           if ($k == 'deletion_time') {
0989             if(!$entry['deletion_time']) $value = "None";
0990           }
0991           echo
0992             "<tr class=tr-$m>",
0993             "<td class=td-0>",ucwords(preg_replace("/_/"," ",$k)),"</td>",
0994             "<td class=td-last>",(preg_match("/time/",$k) && $value!='None') ? date(DATE_FORMAT,$value) : htmlspecialchars($value, ENT_QUOTES, 'UTF-8'),"</td>",
0995             "</tr>";
0996           $m=1-$m;
0997         }
0998         if($fieldkey=='info') {
0999           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>