<?
include($_SERVER["DOCUMENT_ROOT"]."/"."mysql.php");
$q=@mysqli_query($mysql_link,"USE `hits`");
body($_GET,"pg");
if((!
array_key_exists("pg",$_GET))||(!array_key_exists("do",$_GET))||(!array_key_exists("re",$_GET))) die();
$r=regs($_GET,hits($_SERVER),"pg","do","re","st","ht");
define("st_file","st");
if(
strpos($_SERVER["HTTP_USER_AGENT"],"MSIE")>0)$iexplore=TRUE;else $iexplore=FALSE;
if(
$iexplore){
    echo(
"<table border='0' width='90%' style='font: 6.5pt. Times New Roman;'><tr>");
    if(
array_key_exists("us",$_GET)) echo("<td align='left'>".row_ref($_GET,"us","pg",$r[1]," is ",$r[0],"your"," hit"));
    if(
array_key_exists("co",$_GET)) echo("<td align='center'>".row_ref($_GET,"co","pg","","","all","statistics",""));
    echo(
"<td align='right'>".row_ref($_GET,"pg","pg",$r[0]," is ",$r[2],"my"," hit"));
    echo(
"</table>");
}else{
    echo(
"<font size=1>");
    if(
array_key_exists("us",$_GET)) echo(row_ref($_GET,"us","pg",$r[1]," is ",$r[0],"your"," hit")."&nbsp;&nbsp;&nbsp;");
    if(
array_key_exists("co",$_GET)) echo(row_ref($_GET,"co","pg","","","all","statistics","")."&nbsp;&nbsp;&nbsp;");
    echo(
row_ref($_GET,"pg","pg",$r[0]," is ",$r[2],"my"," hit"));
    echo(
"</font>");
}
function 
hits(&$server){
    
$ip=""$co="";//country location
    
if(array_key_exists("HTTP_X_FORWARDED_FOR",$server)){
        
$addr=trim(strtolower($server["HTTP_X_FORWARDED_FOR"]));//remote
        
if($addr$ip.=$addr." "$name=get_host($addr,$co); if($name$ip.=$name." ";
    }
    if(
array_key_exists("REMOTE_ADDR",$server)){
        
$addr=trim(strtolower($server["REMOTE_ADDR"]));//gateway
        
if($addr$ip.=$addr." "$name=get_host($addr,$co); if($name$ip.=$name." ";
    }
    if(
array_key_exists("HTTP_VIA",$server)){
        
$addr=trim(strtolower($server["HTTP_VIA"])); $name="";//proxy
        
if($addr){
            
$b=explode("(",$addr,2);
            
$addr=$b[0];
            
$i=strrpos($addr,":");
            if(
$i>0){
                
$addr=trim(substr($addr,0,$i));
                
$b=explode(" ",$addr);
                
$addr=$b[count($b)-1];
                
$name=get_host($addr,$co);
            }
        }
        if(
$addr$ip.=$addr." "; if($name$ip.=$name." ";//end proxy
    
}
    if(!
$co$co="ro";//local IP
    
$q=mysqli_query($GLOBALS["mysql_link"],"SELECT `co` FROM `co` WHERE `co`='".$co."'");
    
$n=mysqli_num_rows($q);
    
mysqli_free_result($q);
    if(
$n==0)$co="";
    if(!
$co)
        if(
array_key_exists("HTTP_VIA",$server))
            
$co=get_whois($server["HTTP_VIA"]);
    if(!
$co)
        if(
array_key_exists("REMOTE_ADDR",$server))
            
$co=get_whois($server["REMOTE_ADDR"]);
    if(!
$co)
        if(
array_key_exists("HTTP_X_FORWARDED_FOR",$server))
            
$co=get_whois($server["HTTP_X_FORWARDED_FOR"]);
    return array(
addslashes(trim($ip)),trim($co));
}
function 
regs($gets,$hits,$pg,$do,$re,$table,$cont){
    
$q=mysqli_query($GLOBALS["mysql_link"],"SHOW COLUMNS FROM `".$table."`");
    for(
$i=0;$r=mysqli_fetch_row($q);$cols[$i++]=$r[0]);
    
mysqli_free_result($q);
    
$q_id=array_shift($cols);$q_field=array_shift($cols);array_shift($cols);
    if(!
in_array($gets[$pg],$cols)) die("msg to admin: Please add `".$gets[$pg]."` field to ".$table." table");
    if(!
in_array($gets[$do],$cols)) die("msg to admin: Please add `".$gets[$do]."` field to ".$table." table");
    if(!
in_array($gets[$re],$cols)) die("msg to admin: Please add `".$gets[$re]."` field to ".$table." table");
    
$q=mysqli_query($GLOBALS["mysql_link"],"SELECT `".$q_id."`, `".implode("`, `",$cols)."` FROM `".$table."` WHERE `".$q_field."` = '".$hits[0]."' LIMIT 1");
    
$r=mysqli_fetch_row($q); mysqli_free_result($q);
    
$thits=3;$coln=count($cols);
    if(
$r){
        for(
$i=1;$i<$coln;$i++) $thits+=$r[$i];
        
$q=mysqli_query($GLOBALS["mysql_link"],"UPDATE `".$table."` SET `".$gets[$pg]."` = `".$gets[$pg]."` + 1, `".$gets[$do]."` = `".$gets[$do]."` + 1, `".$gets[$re]."` = `".$gets[$re]."` + 1 WHERE `".$q_id."` = '".$r[0]."'");
        if(!
$q) die("msg to admin: UPDATE `".$table."` failed.");  
    }else{
        
$r array_fill(0,$coln,0);
        for(
$i=0;$i<$coln;$i++) if($cols[$i]==$gets[$re]) {$r[$i]=1;break;}
        for(;
$i<$coln;$i++) if($cols[$i]==$gets[$do]) {$r[$i]=1;break;}
        for(;
$i<$coln;$i++) if($cols[$i]==$gets[$pg]) {$r[$i]=1;break;}
        
$q=mysqli_query($GLOBALS["mysql_link"],"INSERT INTO `".$table."` VALUES ('', '".$hits[0]."', '".$hits[1]."', '".implode("', '",$r)."')");
        if(!
$q) die("msg to admin: INSERT `".$table."` failed.");
        
$r[0]=mysqli_insert_id($GLOBALS["c"]);
    }
    
$dt date("y:m:d:H:i:s"time());
    
$q=mysqli_query($GLOBALS["mysql_link"],"INSERT INTO `".$cont."` VALUES ('', '".$r[0]."', '".$gets[$pg]."', '".$dt."')");
    if(!
$q) die("msg to admin: INSERT `".$cont."` failed.");
    return array(
$r[0],round($thits/3),$gets[$pg]);//uid, hits count, pag
}
function 
get_host($addrs,&$location){
    
$vxf=explode(",",trim(strtolower($addrs)));//remote addresses
    
$nvxf=count($vxf);
    
$vxf_n=""$loc_h="";
    for(
$i=0;$i<$nvxf;$i++) if(is_addr($vxf[$i])){
        
$b="host ".$vxf[$i]; $a=`$b`;
        
$b=explode(" ",$a); $a=$b[count($b)-1]; $b=strrpos($a,".");
        if(
$b){
            
$a=strtolower(substr($a,0,$b));
            
$vxf_n.=$a." ";
            
$loc_h=substr($a,strrpos($a,".")+1,100);
        }
        if(!
$location$location=get_location($vxf[$i]);
        if(!
$location) if($loc_h){
            if(
$loc_h=="uk"$loc_h="gb";
            
$location=$loc_h;
        }
        if(!
$location$location=get_whois($vxf[$i]);
    }
    return 
trim($vxf_n);
}
function 
get_whois(&$addr){
    
$b="whois ".$addr." | grep ountry:"$a=`$b`; if(!$a) return "";
    
$b=explode(":",$a); $a=strtolower(trim($b[count($b)-1]));
    if(
$a){ if($a=="uk"$a="gb"; return $a; }
    return 
"";
}
function 
get_location(&$addr){
    
$a=explode(".",$addr); $vx=$a[3]+256*$a[2]+256*256*$a[1]+256*256*256*$a[0];
    
$q=mysqli_query($GLOBALS["mysql_link"],"SELECT `co` FROM `ad` WHERE 1 AND `vb` <= '".$vx."' AND `ve`>= '".$vx."' LIMIT 1");
    
$n=mysqli_num_rows($q);
    if(
$n==0){
        
mysqli_free_result($q);
        return(
"");
    }else{
        
$r=mysqli_fetch_row($q);
        
mysqli_free_result($q);
        return(
strtolower($r[0]));
    }
}
function 
is_addr($addr){
    
$a=explode(".",$addr);
    if(!(
count($a)==4)) return 0;//IPv4
    
for($i=0;$i<count($a);$i++)
        if(!
is_numeric($a[$i])) return 0;
    if(
count($a)>1){
        if((
$a[0]=="172")&&($a[1]>15)&&($a[1]<32)) return 0;
        if((
$a[0]=="192")&&($a[1]=="168")) return 0;
        if(
$a[0]=="10") return 0;
    }
    return 
1;
}
function 
body(&$get,$pp){
    if(
array_key_exists($pp,$get)){
        
$a=mysqli_query($GLOBALS["mysql_link"],"SELECT `bg`,`tx` FROM `di` WHERE `pg` = '".$get[$pp]."'");
        
$r=mysqli_fetch_row($a); mysqli_free_result($a);
        if(
$r[0]) echo("<body bgcolor='#".$r[0]."' text='#".$r[1]."' link='#".$r[1]."' vlink='#".$r[1]."' alink='#".$r[1]."'>");
    }
}
function 
row_ref(&$get,$ac,$pp,$t1,$t2,$t3,$t4,$t5){
    return 
$t1.$t2."<A HRef='".st_file.".php?".$ac."=".$t3."&".$pp."=".$get[$pp]."' Target='_blank'>".$t4."</A>".$t5;
}
?>