<script language="php">
#echo "n=$n";
#
# add code to 
#        1) pick a different font counter
#           put the new fonts in the same subdirecttory
#        2) pick a 1 pixel gif image
#           which of course is a different font
#        3) allow it to be called with out any arguments
#           to make it harder for the censors on indy media
#           to find it.
#           ie instead of calling it as
#                     <img src="counter.php?n=mr_liberal">
#           just make it
#                     <img src="c.jpg.php">
#           and with that make it put out an invisible gif 
#           to some default counter. maybe based on the web page
#           number
#
#
#
# this program has been moved to the root directory and named 
#             counter.php
# it was orginally named
#             counter4.php
# in this subdirectory
#             images_for_counters
# with a full path of
#             images_for_counters/counter4.php
#
# the argument 
#             d=n
# is no longer used. 
# that was for the digit to display
# since we display ALL the digits that is
# a moot issue
#            
# the argument 
#             c=name
# is used and name is the counter name to fetch
# from the SQL database and display
#
# the argument 
#             l=log_subdirectory
#
# has been added to the PHP version
# it is the subdirectory of the log file
#
#
# added this code so we can include it from
#       jpg.php
# $len is the min number of digits to display

#
# get a prefix to use for the font images
# if its null we dont use a prefix
#
if ($font == '') {
  $font=$_REQUEST['font'];
}
#
# if they enter a garbage font make it null
# later on change the code to verify the font file and
# if it dont exist change the font to null
#
if (! ($font == 'hb' ||       $font == 'hbig' ||      $font == 'hm' || 
       $font == 'hs' ||       $font == 'black1x1' || 
       $font == 'white1x1' || $font == 'mrliberal' || $font == 'kevin' ||   
       $font == 'x' || 
       $font == 'clear' || 
       $font == '' )) 
       {
           $font='';
       }

if ($len == '') {
  $len=$_REQUEST['len'];
}
#echo "len=$len<br>";
if (!preg_match ( "/^[0-9][0-9]*$/", $len)) {
 #echo "len=$len and is NOT numeric<br>";
 $len=1;
}
# and set n= to something in the jpg.php code

# if $l exists then it has been set by jpg.php
#
if ($l == '' ) {
  #
  # if it doesnt exist then check here to see
  # if i was passed to us via
  #       counter.php?l=xxxx
  #
  $l=$_REQUEST['l'];
}
#
# if the log file is null after all of this use the default log file
#
if ($l == '' ) {
  $log_file_subdir="counter/";
}
#echo "log directory ='$l'<br>";
# i removed the ability to specify other log file directorys
#
# only allow them to change the log directory to one I chose
#
#if ($l == 'counter_icecream_anarchist' ||
#    $l == 'counter_other' ||
#    $l == 'tempe_pig_log' ||
#    $l == 'counter_mr_liberal') {
#    $log_file_subdir="$l/";
#    #echo "new log file directory $log_file_subdir<br>";
#}
#
# build the string which is written to the log file
#
$logout="";
#
# get the current date and time in PHOENIX
# kind of like the perl code but slightly different
# more like the UNIX date command
#
$logout.=date("Y/m/d H:i:s",(time()-(7*60*60)));
$logout.=" ${REMOTE_ADDR} ";
$logout.=" ${HTTP_REFERER}";
$logout.=" ${REQUEST_URI} ";
#$logout.=" REMOTE_PORT=${REMOTE_PORT} ";
#$logout.=" HTTP_X_FORWARDED_FOR=${HTTP_X_FORWARDED_FOR} ";
#$logout.=" HTTP_REFERER=${HTTP_REFERER} ";
#$logout.=" HTTP_USER_AGENT=${HTTP_USER_AGENT} ";
#$logout.=" HTTP_VIA=${HTTP_VIA} ";
#$logout.=" REQUEST_URI=${REQUEST_URI} ";
#$logout.=" SERVER_ADDR=${SERVER_ADDR} ";
#$logout.=" REMOTE_ADDR=${REMOTE_ADDR} ";
#echo "<p>$logout<br>";
#
# write the stuff to the log file
#
$file=$log_file_subdir."ip_log.txt";
$F=fopen($file,"a");
fwrite ( $F, "$logout\n" );
$rc=fclose($F);
</script>
<script language="php">
#
# ok their IP address has been written to the log file
# now generate a counter and display it
#
#echo "Database Server: :mysql1.100ws.com<br>";
#echo "database name:miksup_data<br>";
#echo "username <br>";
#echo "table counters<br>";
#echo "more<blockquote>MySQL 4.1.11-Debian_4sarge5 running on<br>mysql1.100ws.com as<br> #miksup_data@ws2.100ws.com</blockquote>";
#
# added this code so we can include it from
#       jpg.php
# and set n= to something in the jpg.php code

if ($n == '' ) {
   $n=$_REQUEST['n'];
}
#
# n=counter_name
# n is the counter name and it must be in the SQL table
# or this routine dies!
#
# if the name is null make it one space so we can look it up in the SQL table as a space
#
#echo "counter name(1)='$n'<br>";
if ($n == "") {
 $n=" ";
}

#echo "counter name(2)='$n'<br>";







 #
 # this include file has the passwords and 
 # database names defined in it  
 #
 # for some reason this require writes to the STDOUT
 # which screws up the gif output
 #
 #require('sql_passwords.php');
 $z_hardcoded_password='Amigos666';
 $z_hardcoded_database='tanryi_data';
 $z_hardcoded_sqlurl='mysql1.100ws.com';
 #
 # connect to SQL
 #
 $link = mysql_connect($z_hardcoded_sqlurl, 
                       $z_hardcoded_database, 
                       $z_hardcoded_password )
                       or die('Could not connect: ' . mysql_error());
 #
 # select the database I want to use
 #
 mysql_select_db($z_hardcoded_database) or die('Could not select database');

 #
 #lock the sql table
 #its not THAT IMPORTANT so we wont die if it fails
 #
 #mysql_query("LOCK TABLES counters WRITE;") or die("Can't lock tables");
 mysql_query("LOCK TABLES counters WRITE;");
 $zcounter_value=do_sql_select($n);
 if ($zcounter_value == -1) { 
    $n=' ';
    $zcounter_value=do_sql_select($n);
 }
 #echo "counter value(1)=$zcounter_value<br>";

 $number=$zcounter_value;

 $nextval=$zcounter_value+1;
 $query = 'update `counters` set count='.$nextval.' WHERE name=\''.$n.'\'';
 $result = mysql_query($query) or die('Query failed: ' . mysql_error());
 #
 # unlock the SQL tables
 #
 mysql_query("UNLOCK TABLES");

 // Free resultset
 #
 # close the SQL connection
 #
 // Closing connection
 mysql_close($link);
 #
 # add leading zeros to the number
 #
 while( $len > strlen($number)) {
  $number="0".$number;
  #echo "$number<br>";
 }
 #
 # now we take $number and build an image from it
 #
 # set these to the image width and height
 # after we caculate it from the digits in the image
 $xxheight=1;
 $xxwidth=0;
 #
 # read in the 10 digit images
 # may have to change this from PNG to GIF  to JPG
 #
 $ifiles[0]='images_for_counters/'.$font.'0.gif';
 $ifiles[1]='images_for_counters/'.$font.'1.gif';
 $ifiles[2]='images_for_counters/'.$font.'2.gif';
 $ifiles[3]='images_for_counters/'.$font.'3.gif';
 $ifiles[4]='images_for_counters/'.$font.'4.gif';
 $ifiles[5]='images_for_counters/'.$font.'5.gif';
 $ifiles[6]='images_for_counters/'.$font.'6.gif';
 $ifiles[7]='images_for_counters/'.$font.'7.gif';
 $ifiles[8]='images_for_counters/'.$font.'8.gif';
 $ifiles[9]='images_for_counters/'.$font.'9.gif';
 #
 # loop thru and open a gif file for each number from 0 to 9
 #
 for ($i=0;$i<10;$i++) {
   #list($zwidth, $zheight, $ztype, $zattr)=getimagesize($ifiles[$i]);
   $size=getimagesize($ifiles[$i]);
   $gif_width[$i]=$size[0];
   $gif_height[$i]=$size[1];
   if ($gif_height[$i] >  $xxheight ) {
     $xxheight=$gif_height[$i];
   }
   $gif[$i]=@imagecreatefromgif ($ifiles[$i]);
   if (!$gif[$i]) {
     echo "error reading file $ifiles[$i] for image $i<br>";
   }  
 }
#
# get the total width of the image
# by adding up the width of each digit in the number
#
 for ($i=0;$i<strlen($number);$i++) {
  $thisdigit=substr($number,$i,1);
  $xxwidth+=$gif_width[$thisdigit];
 }
 #
 # create the image
 #
 $image=ImageCreate($xxwidth, $xxheight);
 #
 # set the R,G,B background color of the imagee
 #
 $background_color=ImageColorAllocate($image,255,255,255);
 #
 # define RGB black as a color used on the image
 #
 $black=ImageColorAllocate($image,0,0,0);
 #
 # copy the color $black into the image between 0,0 and 50,50
 #
 #ImageFilledRectangle($image,0,0,$xxwidth,$xxheight,$black);
 #
 # convert the $number to an image
 # copy each digit of the number to our image from left to right
 #
 $next_x=0;
 for ($i=0;$i<strlen($number);$i++) {
  $thisdigit=substr($number,$i,1);
  $zfile=$ifiles[$thisdigit];
  $zhandle=$gif[$thisdigit];
  #
  # copy the gif image to our image
  #
  $rc=imagecopy ( $image, $zhandle, 
                  $next_x, ($xxheight - $gif_height[$thisdigit]) , 
                  0, 0,
                  $gif_width[$thisdigit],$xxheight);
  $rc=$rc?"true":"failed";
  $next_x=$next_x+ $gif_width[$thisdigit];
 }
 #
 # return the image
 #
 header("Content-Type: image/png");
 ImagePNG($image);




function do_sql_select($counter_name) {
 #
 # assume select will fail and return -1 as an error code
 #
 $rc=-1;
 #echo "counter_name=$counter_name<br>";
 #
 # build the query to run
 #
 $query = 'SELECT count FROM `counters` where name=\''.$counter_name.'\' LIMIT 0, 30 ';
 #$query = 'SELECT count FROM `counters` where name=\''.$n.'\' LIMIT 0, 30 ';
 #$query = 'SELECT count FROM `counters` where name=\'frog\' LIMIT 0, 30 ';
 #
 # run the query
 #
 $result = mysql_query($query) or die('Query failed: ' . mysql_error());
 #
 # loop thru and get the results of the query
 # but in this case there will only be ONE result
 #
 $zcounter_value="";
 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    foreach ($line as $col_value) {
        $zcounter_value=$col_value;
    }
 }
 mysql_free_result($result);
 #echo "zcounter_value='$zcounter_value'<br>";
 if ($zcounter_value != '') {
   $rc=$zcounter_value;
 }
 #echo "rc=$rc<br>";
 return $rc;
}
</script>