<?php

include('lib.php');

function counter($name, $df)
{
 $maxdigit = 6;
 $fp=fopen($df, "r");
 $farr=fread($fp, filesize($df));
 $farr=explode("\n", $farr);
 fclose($fp);
 $arrcnt=count($farr);
 $added=0;
 for($i=0; $i<$arrcnt; $i++)
 {
  $data=explode("|", $farr[$i]);
  if($data[0]==$name)
  {
   $data[1]=$data[1]+1;
   $digits = strval($data[1]);
   $cnt = strlen($data[1]);
   if($cnt>$maxdigit)
    $maxdigit=$cnt;
   for($j=0; $j<$maxdigit; $j++)
   {
	if($j<$cnt)
	 $counterstr.="<img src=\"img/$digits[$j].gif\" alt=\"$j\">";
	else
	 $counterstr="<img src=\"img/0.gif\" alt=\"0\">".$counterstr;
   }
   $counterstr .= "<br>";
   $farr[$i]=implode("|", $data);
   $added=1;
   break;
  }
 }

 $fp=fopen($df, "w");
 if($added==0)
   fwrite($fp, $name."|1\n");
 $farr=implode("\n", $farr);
 fwrite($fp, $farr);
 fclose($fp);
 return $counterstr;
}

$fp=file($datafile);
$cnt=count($fp);
if(!$show || $show=="")
{
 echo $error4;
 include $bottom;
 Exit;
}
$yes=0;
for($i=0; $i<$cnt; $i++)
{
 $data = explode("|",$fp[$i]);
 if($show==$data[0])
 {
  $dt=trim($data[1]);
  if($dt=="")
   continue;
   $name=str_replace("%", "p", $name);
  if(ereg("^%#", $dt) && (!$name || trim($name)==""))
    {
	 echo $error1;
	 break;
	}
  $dt=str_replace("^%#", basename($name), $dt);
  $title=$data[2];
  if(trim($title)=="")
   $title=$deftitle;
  @include $top;
   if(file_exists($dt))
    include $dt;
   else
    echo $error2;
  $yes=1;
  break;
 }
}
if($yes=0)
{
 $counterstr=counter("error", $counterfile);
 echo $error3;
}
else
 $counterstr=counter($dt, $counterfile);
@include $bottom;
?>