<?

// Timeout in 4 seconds
$watch_dog_pid = exec("{ sleep 4 && kill -15 \$PPID; } <&- >&- 2>&- & echo $!");

global $_COOKIE,$page_id;

header('Pragma: no-cache');
header('Content-type: text/css');

require_once 'mainfile.php';

function iptoint($ip_address)
{
  if (preg_match("/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/",$ip_address,$ip)
      )
    {
      $retip=$ip[1]*16777216 + $ip[2]*65536 + $ip[3]*256 + $ip[4];
      return $retip;
    }
}

function counter($ip_addr)
{
  global $prefix, $dbi, $page_id;
  /* Get the Browser data */


  /* Start Detailed Statistics */

  $dot = date("d-m-Y-H");
  $now = explode ("-",$dot);
  $nowHour = $now[3];
  $nowYear = $now[2];
  $nowMonth = $now[1];
  $nowDate = $now[0];

  $resultyear = sql_query("select year from ".$prefix."_stats_year where year='$nowYear'",$dbi);
  $jml = sql_num_rows($resultyear,$dbi);
  if ($jml <= 0) {
    sql_query("insert into ".$prefix."_stats_year values('$nowYear','0')",$dbi);
    for ($i=1;$i<=12;$i++) {
      sql_query("insert into ".$prefix."_stats_month values('$nowYear','$i','0')",$dbi);
      if ($i == 1) $TotalDay = 31;
      if ($i == 2) {
	if (($nowYear % 4) == 0) {
	  $TotalDay = 28;
	} else {
	  $TotalDay = 29;
	}
      }
      if ($i == 3) $TotalDay = 31;
      if ($i == 4) $TotalDay = 30;
      if ($i == 5) $TotalDay = 31;
      if ($i == 6) $TotalDay = 30;
      if ($i == 7) $TotalDay = 31;
      if ($i == 8) $TotalDay = 31;
      if ($i == 9) $TotalDay = 30;
      if ($i == 10) $TotalDay = 31;
      if ($i == 11) $TotalDay = 30;
      if ($i == 12) $TotalDay = 31;
      for ($k=1;$k<=$TotalDay;$k++) {
	sql_query("insert into ".$prefix."_stats_date values('$nowYear','$i','$k','0')",$dbi);
      }
    }
  }


  sql_query("update ".$prefix."_stats_year set hits=hits+1 where year='$nowYear'",$dbi);
  sql_query("update ".$prefix."_stats_month set hits=hits+1 where (year='$nowYear') and (month='$nowMonth')",$dbi);
  sql_query("update ".$prefix."_stats_date set hits=hits+1 where (year='$nowYear') and (month='$nowMonth') and (date='$nowDate')",$dbi);
#sql_query("update ".$prefix."_stats_hour set hits=hits+1 where (year='$nowYear') and (month='$nowMonth') and (date='$nowDate') and (hour='$nowHour')",$dbi);
  #

  $str_name=getenv("REQUEST_URI");


  if ($page_id > 0) {
    if (($keep_page_id = $page_id)==183) $page_id=35;
    else if ($page_id==25) $page_id=1;
    if (!sql_query("insert into nuke_stats_str(id, hits, data) values ('$page_id', 1, curdate())",$dbi,1))
      $res=sql_query("update nuke_stats_str set hits=hits+1 where id='$page_id' and data=CURDATE()",$dbi);

       //статистика по IP отключена
      /*	

    if (sql_query("insert into nuke_stats_str_ip (id, ip, data, hits) values ('$page_id', '$ip_addr', CURDATE(),1)",$dbi,1))
      {
	$ip_int=iptoint($ip_addr);
	if ($res_geo1=sql_query("select geo_country,geo_region,geo_town from nuke_geo where geo_ip<=$ip_int and geo_ip+geo_mask>=$ip_int",$dbi))
	  {
	    list($geo_country,$geo_region,$geo_town)=sql_fetch_row($res_geo1, $dbi);
	    if (!$geo_country) $geo_country="0";
	    if (!$geo_region) $geo_region="0";
	    if (!$geo_town) $geo_town="0";
	    sql_free_result($res_geo1, $dbi);
	    sql_query("UPDATE nuke_stats_str_ip SET country=$geo_country, region=$geo_region, town=$geo_town WHERE id=$page_id AND ip='$ip_addr' AND data=CURDATE()",$dbi);
	  }
      }
    else
      $res=sql_query("update nuke_stats_str_ip set hits=hits+1 where id='$page_id' and ip='$ip_addr' and data=CURDATE()",$dbi);
      */

    $page_id = $keep_page_id;
  };

}

if (!isset($_COOKIE['X-proxy-mix-IP']))
  $ip_addr=getenv("REMOTE_ADDR");
 else
   $ip_addr=$_COOKIE['X-proxy-mix-IP'];
$page_id = intval($_GET['page_id']);
// Fix page_id
if ($page_id==240 || $page_id==247 || $page_id==267) $page_id=35;
else if (($page_id>=177 && $page_id<=181) || $page_id==189 || $page_id==190 || $page_id==194) $page_id=177;
else if ($page_id>=199 &&  $page_id<240) $page_id=199;
else if ($page_id==249 || $page_id==12 || $page_id==18) $page_id=1;
else if ($page_id==188) $page_id=181;
else if ($page_id==24) $page_id=20;
else if ($page_id==191 || $page_id==246) $page_id=161;
if ($_GET['uri'] && $_SERVER['HTTP_REFERER'] && $page_id>0) counter($ip_addr);

exec("kill -9 $watch_dog_pid");
?>
