<?php
#-----------------------------------------------------#
#          ********* WAP-MOTORS *********             #
#             Made by   :  VANTUZ                     #
#               E-mail  :  vsem@pizdec.ru             #
#                 Site  :  http://pizdec.ru           #
#             WAP-Site  :  http://visavi.net          #
#                  ICQ  :  36-44-66                   #
#  Вы не имеете право вносить изменения в код скрипта #
#-----------------------------------------------------#
//------------------------- Функция подсчета посетителей -----------------------//
//clearstatcache();
$maxonline="200"; #сколько онлайн записей хранить
$file_online=file(BASEDIR."local/online.dat");
$ion = count($file_online);

if ($ion>=$maxonline) {
$fpon=fopen(BASEDIR."local/online.dat","w");
if($fpon){
flock ($fpon,LOCK_EX);
unset($file_online[0],$file_online[1]);
fputs($fpon, implode("",$file_online));
flock ($fpon,LOCK_UN);
fclose($fpon);}}



$time = time();
$oftime=$time-600;
$den=date("d");
$hour=date("H");
//$daysm=date("t");
$found = 0;
$user = "";
$arrtimehour=mktime(date("H"), 0, 0, date("m"), date("d"), date("Y"));
$arrtimeday=mktime(0, 0, 0, date("m"), date("d"), date("Y"));

$f = fopen(BASEDIR."local/online.dat","r+");
if($f){
flock($f,LOCK_EX);
while (!feof($f))
	{
    $user[] = fgets($f,65536);
	}
fseek($f,0,SEEK_SET);
ftruncate($f,0);

foreach($user as $val) {
list($savedtime,$savedip,$savedlog,$savedbrow) = explode("#",$val);
if($ip==$savedip){$found = 1;}
if($savedlog!="" && $savedlog==$log){$savedip=$ip;}
if($savedtime>$oftime && $ip!=$savedip && $savedip!=""){
fputs($f,$savedtime."#".$savedip."#".$savedlog."#".$savedbrow."#\r\n");
}
}

fputs($f,$time."#".$ip."#".$log."#".$brow."#\r\n");
	
fflush($f);
flock($f,LOCK_UN);
fclose ($f);
}

//--------------------------------------------------------------------//	 
//                           Запись хостов
//--------------------------------------------------------------------//
if($found==0){
$fp=fopen(BASEDIR."local/datacounter/host.dat","a+");
if($fp){
flock($fp,LOCK_EX);
while (!feof($fp))
	{
    $arcounts[] = fgets($fp,100);
	}
$count=explode("#",$arcounts[0]);

//----------------------- Статистика по 24 часам (хосты) ---------------------------------//
if($count[3]!=$hour){

$ufilecount = file(BASEDIR."local/datacounter/24_host.dat"); 
$udatacount = explode("|",$ufilecount[0]);

if($udatacount[0]!=""){
for($f=0;$f<24;$f++){
$udatadays = explode("-",$udatacount[0]);
$newtime=$udatadays[0]+7200;
$newzaptime=$udatadays[0]+3600;

if($time>$newtime){
array_unshift($udatacount,$newzaptime.'-0');
}}}

array_unshift($udatacount,$arrtimehour.'-'.$count[4]);

for ($uc=0; $uc<24; $uc++){
$utextcount.=$udatacount[$uc].'|';}

if($utextcount!=""){
$fpcount=fopen(BASEDIR."local/datacounter/24_host.dat","a+");    
flock($fpcount,LOCK_EX);           
ftruncate($fpcount,0);                                                                
fputs($fpcount,$utextcount);
fflush($fpcount);
flock($fpcount,LOCK_UN);
fclose($fpcount);  
chmod ($fpcount, 0666); 
chmod (BASEDIR."local/datacounter/24_host.dat", 0666); 
}
$count[3]=$hour; $count[4]=0;
}



if($count[0]!=$den){
//------------------------ Статистика по 31 дню (хосты) --------------------------------//
$cufilecount = file(BASEDIR."local/datacounter/31_host.dat"); 
$cudatacount = explode("|",$cufilecount[0]);

if($cudatacount[0]!=""){
for($f=0;$f<31;$f++){
$cudatadays = explode("-",$cudatacount[0]);
$cnewtime=$cudatadays[0]+172800;
$cnewzaptime=$cudatadays[0]+86400;

if($time>$cnewtime){
array_unshift($cudatacount,$cnewzaptime.'-0');
}}}


array_unshift($cudatacount,$arrtimeday.'-'.$count[1]);


for ($cuc=0; $cuc<31; $cuc++){
$cutextcount.=$cudatacount[$cuc].'|';}

if($cutextcount!=""){
$cfpcount=fopen(BASEDIR."local/datacounter/31_host.dat","a+");    
flock($cfpcount,LOCK_EX);           
ftruncate($cfpcount,0);                                                                
fputs($cfpcount,$cutextcount);
fflush($cfpcount);
flock($cfpcount,LOCK_UN);
fclose($cfpcount);  
chmod ($cfpcount, 0666); 
chmod (BASEDIR."local/datacounter/31_host.dat", 0666);  
}
$count[0]=$den; $count[1]=0;
}



//------------------------  Каждое посещение --------------------------------//
$count[1]++; $count[2]++; $count[4]++;
$zapis=$count[0].'#'.$count[1].'#'.$count[2].'#'.$count[3].'#'.$count[4].'#';
ftruncate($fp,0);
fputs($fp,$zapis);
fflush($fp);
flock($fp,LOCK_UN);
fclose($fp);
chmod ($fpcount, 0666); 
chmod (BASEDIR."local/datacounter/host.dat", 0666); 
}}else{
	
	
//--------------------------------------------------------------------//	 
//                           Запись хитов
//--------------------------------------------------------------------//
$fp=fopen(BASEDIR."local/datacounter/hits.dat","a+");
if($fp){
flock($fp,LOCK_EX);
while (!feof($fp))
	{
    $arcounts[] = fgets($fp,100);
	}
$count=explode("#",$arcounts[0]);




//----------------------- Статистика по 24 часам (хиты) ---------------------------------//
if($count[3]!=$hour){

$ufilecount = file(BASEDIR."local/datacounter/24_hits.dat"); 
$udatacount = explode("|",$ufilecount[0]);

if($udatacount[0]!=""){
for($f=0;$f<24;$f++){
$udatadays = explode("-",$udatacount[0]);
$newtime=$udatadays[0]+7200;
$newzaptime=$udatadays[0]+3600;

if($time>$newtime){
array_unshift($udatacount,$newzaptime.'-0');
}}}


array_unshift($udatacount,$arrtimehour.'-'.$count[4]);

for ($uc=0; $uc<24; $uc++){
$utextcount.=$udatacount[$uc].'|';}

if($utextcount!=""){
$fpcount=fopen(BASEDIR."local/datacounter/24_hits.dat","a+");    
flock($fpcount,LOCK_EX);           
ftruncate($fpcount,0);                                                                
fputs($fpcount,$utextcount);
fflush($fpcount);
flock($fpcount,LOCK_UN);
fclose($fpcount);  
chmod ($fpcount, 0666); 
chmod (BASEDIR."local/datacounter/24_hits.dat", 0666);
} 
$count[3]=$hour; $count[4]=0;
}





if($count[0]!=$den){
//------------------------ Статистика по 31 дню (хиты) --------------------------------//
$cufilecount = file(BASEDIR."local/datacounter/31_hits.dat"); 
$cudatacount = explode("|",$cufilecount[0]);

if($cudatacount[0]!=""){
for($f=0;$f<31;$f++){
$cudatadays = explode("-",$cudatacount[0]);
$cnewtime=$cudatadays[0]+172800;
$cnewzaptime=$cudatadays[0]+86400;

if($time>$cnewtime){
array_unshift($cudatacount,$cnewzaptime.'-0');
}}}


array_unshift($cudatacount,$arrtimeday.'-'.$count[1]);


for ($cuc=0; $cuc<31; $cuc++){
$cutextcount.=$cudatacount[$cuc].'|';}

if($cutextcount!=""){
$cfpcount=fopen(BASEDIR."local/datacounter/31_hits.dat","a+");    
flock($cfpcount,LOCK_EX);           
ftruncate($cfpcount,0);                                                                
fputs($cfpcount,$cutextcount);
fflush($cfpcount);
flock($cfpcount,LOCK_UN);
fclose($cfpcount);  
chmod ($cfpcount, 0666); 
chmod (BASEDIR."local/datacounter/31_hits.dat", 0666);
}  
$count[0]=$den; $count[1]=0;
}


//----------------------- Каждое посещение ----------------------------//
$count[1]++; $count[2]++; $count[4]++;
$zapis=$count[0].'#'.$count[1].'#'.$count[2].'#'.$count[3].'#'.$count[4].'#';
ftruncate($fp,0);
fputs($fp,$zapis);
fflush($fp);
flock($fp,LOCK_UN);
fclose($fp);
chmod ($fpcount, 0666); 
chmod (BASEDIR."local/datacounter/hits.dat", 0666); 
}
}

//-------------------- Вывод статистики ------------------------------//
$str = file_get_contents(BASEDIR."local/online.dat");
$substr_count = substr_count($str,"##");
$lines=file(BASEDIR."local/online.dat");
$counter_online=count($lines);
$counter_reg=$counter_online-$substr_count;

$count=file(BASEDIR."local/datacounter/host.dat");
$count =explode("#",$count[0]);
$counter_host=$count[1];
$counter_all=$count[2];

$hcount=file(BASEDIR."local/datacounter/hits.dat");
$hcount =explode("#",$hcount[0]);
$counter_hits=$hcount[1];
$counter_allhits=$hcount[2];

if($config_onlines=="1"){
echo'<a href="'.BASEDIR.'pages/online.php?'.SID.'">[Online: '.$counter_reg.'/'.$counter_online.']</a><br>';}

if($config_counters=="1"){ 
if($config_incount=="1"){ echo '<a href="'.BASEDIR.'pages/counter.php?'.SID.'">'.$counter_host.' | '.$counter_all.'</a><br>';}
if($config_incount=="2"){ echo '<a href="'.BASEDIR.'pages/counter.php?'.SID.'">'.$counter_hits.' | '.$counter_allhits.'</a><br>';}
if($config_incount=="3"){ echo '<a href="'.BASEDIR.'pages/counter.php?'.SID.'">'.$counter_host.' | '.$counter_hits.'</a><br>';}
if($config_incount=="4"){ echo '<a href="'.BASEDIR.'pages/counter.php?'.SID.'">'.$counter_all.' | '.$counter_allhits.'</a><br>';}
if($config_incount=="5"){ echo '<a href="'.BASEDIR.'pages/counter.php?'.SID.'"><img src="'.BASEDIR.'gallery/count.php" alt=""></a><br>';}
}
?>