<?php
/*
Text Counter by http://www.free-php-counter.com
You are allowed to remove advertising after you purchased a licence
*/

// settings

// ip-protection in seconds
$counter_expire = 600;

// ignore agent list
$counter_ignore_agents = array('bot', 'bot1', 'bot3');

// ignore ip list
$counter_ignore_ips = array('127.0.0.2', '127.0.0.3');


// get basic information
$counter_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
$counter_ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1'; 
$counter_time = time();


// connect to database
$counter_connected = true;
if (empty($connection)) 
{
	$counter_connected = false;	
}

if ($counter_connected == true) 
{
   $ignore = false; 
   
   // get counter information
   $sql = "SELECT * FROM counter_values LIMIT 1";
   $res = mysql_query($sql);
   // fill when empty
   if (!$res || mysql_num_rows($res) == 0)
   {	  
	  $sql = "INSERT INTO `counter_values` (`id`, `day_id`, `day_value`, `yesterday_id`, `yesterday_value`, `week_id`, `week_value`, `month_id`, `month_value`, `year_id`, `year_value`, `all_value`, `record_date`, `record_value`) VALUES ('1', '" . date("z") . "',  '1', '" . (date("z")-1) . "',  '0', '" . date("W") . "', '1', '" . date("n") . "', '1', '" . date("Y") . "',  '1',  '1',  NOW(),  '1')";
	  mysql_query($sql);

	  // reload with settings
	  $sql = "SELECT * FROM counter_values LIMIT 1";
      $res = mysql_query($sql);
	  
	  $ignore = true;
   }   
   $row = mysql_fetch_assoc($res);
   
   $day_id = isset($row['day_id']) ? $row['day_id'] : date("z");
   $day_value = isset($row['day_value']) ? $row['day_value'] : 1;
   $yesterday_id = isset($row['yesterday_id']) ? $row['yesterday_id'] : (date("z")-1);
   $yesterday_value = isset($row['yesterday_value']) ? $row['yesterday_value'] : 0;
   $week_id = isset($row['week_id']) ? $row['week_id'] : date("W");
   $week_value = isset($row['week_value']) ? $row['week_value'] : 1;
   $month_id = isset($row['month_id']) ? $row['month_id'] : date("n");
   $month_value = isset($row['month_value']) ? $row['month_value'] : 1;
   $year_id = isset($row['year_id']) ? $row['year_id'] : date("Y");
   $year_value = isset($row['year_value']) ? $row['year_value'] : 1;
   $all_value = isset($row['all_value']) ? $row['all_value'] : 1;
   $record_date = isset($row['record_date']) ? $row['record_date'] : date("Y-m-d H:i:s");
   $record_value = isset($row['record_value']) ? $row['record_value'] : 1;
   
   
   // check ignore lists
   $length = sizeof($counter_ignore_agents);
   for ($i = 0; $i < $length; $i++)
   {
	  if (substr_count($counter_agent, strtolower($counter_ignore_agents[$i])))
	  {
	     $ignore = true;
		 break;
	  }
   }
   
   $length = sizeof($counter_ignore_ips);
   for ($i = 0; $i < $length; $i++)
   {
	  if ($counter_ip == $counter_ignore_ips[$i])
	  {
	     $ignore = true;
		 break;
	  }
   }
   
      
   // delete free ips
   if ($ignore == false)
   {
      $sql = "DELETE FROM counter_ips WHERE unix_timestamp(NOW())-unix_timestamp(visit) >= $counter_expire"; 
      mysql_query($sql);	  
   }
      
   // check for entry
   if ($ignore == false)
   {
      $sql = "SELECT visit FROM counter_ips WHERE ip = '$counter_ip'";
      $res_ip = mysql_query($sql);
      if ($res_ip && mysql_num_rows($res_ip) > 0)
      {
         $ignore = true;
         mysql_query("UPDATE counter_ips SET visit = NOW() WHERE ip = '$counter_ip'");
      }
      else
      {
         $sql = "INSERT INTO counter_ips (ip, visit) VALUES ('$counter_ip', NOW()) ON DUPLICATE KEY UPDATE visit = NOW()";
         mysql_query($sql);
      }
   }
   
   // online?
   $sql = "SELECT * FROM counter_ips";
   $res = mysql_query($sql);
   $online = $res ? mysql_num_rows($res) : 0;
      
   // add counter
   if ($ignore == false)
   {     	  
      // yesterday
	  if ($day_id == (date("z")-1)) 
	  {
	     $yesterday_value = $day_value; 
	  }
	  else
	  {
	     if ($yesterday_id != (date("z")-1))
		 {
		    $yesterday_value = 0; 
		 }
	  }
	  $yesterday_id = (date("z")-1);
	  
	  // day
	  if ($day_id == date("z")) 
	  {
	     $day_value++; 
	  }
	  else 
	  {
	     $day_value = 1;
		 $day_id = date("z");
	  }
	  
	  // week
	  if ($week_id == date("W")) 
	  {
	     $week_value++; 
	  }
	  else 
	  { 
	     $week_value = 1;
		 $week_id = date("W");
      }
	  
      // month
	  if ($month_id == date("n")) 
	  {
	     $month_value++; 
	  }
	  else 
	  {
	     $month_value = 1;
		 $month_id = date("n");
      }
	  
	  // year
	  if ($year_id == date("Y")) 
	  {
	     $year_value++; 
	  }
	  else 
	  {
	     $year_value = 1;
		 $year_id = date("Y");
      }
	  
	  // all
	  $all_value++;
		 
	  // new record?
	  if ($day_value > $record_value)
	  {
	     $record_value = $day_value;
	     $record_date = date("Y-m-d H:i:s");
	  }
		 
	  // save and cleanup
	  $sql = "UPDATE counter_values SET day_id = '$day_id', day_value = '$day_value', yesterday_id = '$yesterday_id', yesterday_value = '$yesterday_value', week_id = '$week_id', week_value = '$week_value', month_id = '$month_id', month_value = '$month_value', year_id = '$year_id', year_value = '$year_value', all_value = '$all_value', record_date = '$record_date', record_value = '$record_value' WHERE id = 1";
	  mysql_query($sql);  	  
   }	  
 }
?>
