<?

/* You must have the GD libraries compiled into PHP to make this thing work!
 * 
 * The following variables are probably the only thing you need to change.
 * 
 * $counterfile - Is exactly what it says. This file will be created if   
 *                it does not exist.                                      
 * 
 * $ipfile - Is a list of ip addresses or networks to not count in the
 *           counting process, ie. your own network. One entry per line,
 *           no rem statements or wild cards. This file is not necessary.
 *           example:
 *           10.1.
 *           192.168.1.1
 *           208.153.17.
 * 
 * NOTE: The directory that these files are in must have read/write access
 * for the HTTPD user. Either chmod 777 it or chown it to the correct user.
 * 
 * $fontsize - I hope this one is self explanatory.
 * 
 * $font - Path to the true type font to use.
 * 
 * If you want to change the font/background color, poke around in the code
 * to see where I have put other rems.
 * 
 * Thanks!
 * Bill Martin <bill@dunfoamin.org>
 */

$counterfile = "/user/gruppo_1/zito/WWW/php/supercounter.txt";
$ipfile = "/user/gruppo_1/zito/WWW/php/excludeip.txt";
$fontsize = "20";
$font = "/user/share/fonts/truetype/lithogrb.ttf";

function displayCounter($counterfile, $ipfile, $remadd) {
   if(file_exists($ipfile)) {
      $fp = fopen($ipfile,"r");
      while ($ips = preg_quote(trim(fgets($fp,50)))) {
	 if (preg_match("/$ips/",$remadd)) {
	    $nocount = 1;
	    break;
	 } else {
	    $nocount = 0;
	 }
      }
      fclose($fp);
   }
   $fp = fopen($counterfile,"a+");
   rewind($fp);
   $num = trim(fgets($fp,7));
   if ($num == "") {$num="0";}
   if ($nocount == "0") {
      $num += 1;
   }
   $num = trim($num);
   fclose($fp);
   $fp = fopen($counterfile,"r+");
   fputs($fp,$num);
   fclose($fp);
   return ($num);
}

function drawButton($num, $fontsize, $font) {
   Header("Content-type: image/gif");
   $string=$num;
   $textsize = imagettfbbox($fontsize, 0, $font, $string);
   $iw = $textsize[2]+43;
   $ih = abs($textsize[7])+9;
   $im = imagecreate($iw, $ih);
   // color definitions (I don't use them all)
   $white = ImageColorAllocate($im, 255, 255, 255);
   $black = ImageColorAllocate($im, 0, 0, 0);
   $yellow = ImageColorAllocate($im, 255, 255, 0);
   $trans = ImageColorAllocate($im, 5, 5, 5);
   imagecolortransparent($im, $trans);
   // this is where the box is drawn
   imagefill($im, 0, 0, $black);
   $px = ($iw-$textsize[4])/2;
   $py = (($ih+$textsize[7])/2)+abs($textsize[7])-1;
   // this is where the text is drawn
   ImageTTFText($im, $fontsize, 0 , $px, $py, $yellow, $font, $string);
   ImageGif($im);
   ImageDestroy($im);
}

$remadd = $REMOTE_ADDR;
$number = displayCounter($counterfile, $ipfile, $remadd);
drawButton($number, $fontsize, $font);

?>
