collect_stats(); $countobject->generate_output(); $countobject->onexit(); } else{ require("./functions.php"); $this->funcobject=new func(1); $this->error="Invalid domain!"; $this->funcobject->print_error($this->error,1); $this->funcobject->onexit(); die; } class counter{ var $domain; var $agent; var $time; var $ip; var $referer; var $host; var $funcobject; var $config; var $error; var $tmp=0; function counter(){ require("./functions.php"); $this->funcobject=new func(1); $this->domain=$this->funcobject->getDomain($_GET['d']); $this->agent=$_SERVER['HTTP_USER_AGENT']; $this->time=time(); $this->ip=sprintf('%u', ip2long($_SERVER['REMOTE_ADDR'])); $this->host=$this->gethostname(); if(isset($_SERVER['HTTP_REFERER'])){ $this->referer=$_SERVER['HTTP_REFERER']; } $this->config['error']=1; if($this->domain > 0){ $this->config=$this->funcobject->load_config($this->domain); $this->config['error']=0; } if($this->config['error']){ $this->error="Invalid domain!"; $this->funcobject->print_error($this->error,1); die; } } function onexit(){ $this->funcobject->onexit(); } function collect_stats(){ if($this->checkVisitorIp()){ $robot=0; $ip_ci=0; $browser_id=$this->getBrowser(); $browser_version=$this->tmp; $os_id=$this->getOs(); $robot_id=$this->getRobot(); $ip_ci=$this->getCountryCode(); $ip_cc=$this->getCountryCodeName($ip_ci); $sql="UPDATE tbf_count_ipcountry SET anzahl=anzahl+1 WHERE ci='".$ip_ci."' AND domain_id='".$this->domain."'"; $this->funcobject->dbquery($sql); if($robot_id!=1){ $sql="UPDATE tbf_count_robot SET anzahl=anzahl+1 WHERE robot_id='".$robot_id."' AND domain_id='".$this->domain."'"; $this->funcobject->dbquery($sql); } else{ $sql="UPDATE tbf_count_browser SET anzahl=anzahl+1 WHERE browser_id='".$browser_id."' AND domain_id='".$this->domain."'"; $this->funcobject->dbquery($sql); $sql="UPDATE tbf_count_os SET anzahl=anzahl+1 WHERE os_id='".$os_id."' AND domain_id='".$this->domain."'"; $this->funcobject->dbquery($sql); } /* Save the unknown agents that they can be reported to support new agents. */ if(($browser_id==1 || $os_id==1) && $robot_id==1){ $sql="INSERT INTO tbf_unknown_agents VALUES ('','".$this->agent."')"; $this->funcobject->dbquery($sql); } /* We have a supported robot, so copy the id for the XOR-code */ else if($robot_id != 1){ $robot=1; $browser_id=$robot_id; } $xor=$this->funcobject->getXORCode($browser_id,$os_id,$robot); $sql="INSERT INTO tbf_stats VALUES ('','".$xor."','".$browser_version."','".$this->domain."','".$this->time."','".$this->ip."','".$ip_cc."','".$this->host."','".$this->referer."')"; $this->funcobject->dbquery($sql); $this->deleteOldstats(); } } function checkVisitorIp(){ /* Check if the Visitor revists the site within the idle time. */ $valid=true; $sql="SELECT id FROM tbf_stats WHERE ip='".$this->ip."' AND domain_id='".$this->domain."' AND time > '".(time()-$this->config['idle_time'])."'"; $result=$this->funcobject->dbquery($sql); while($data=mysql_fetch_array($result)){ $valid=false; break; } /* If the IP is still valid, check whether it is one of the blocked ip addresses or not. */ if($valid){ /* Check the block file IPs */ if(file_exists("./blocked_ip.php")){ include("./blocked_ip.php"); $blocked_ip = sprintf("%u",ip2long($blocked_ip)); if($this->ip == $blocked_ip){ $valid = false; } } /* Check the db for blocked IPs */ $sql="SELECT COUNT(*) FROM tbf_blocked_ip WHERE ip_address='".$this->ip."'"; $count = mysql_result($this->funcobject->dbquery($sql),0); if($count > 0){ $valid = false; } } return $valid; } function getBrowser(){ $id=1; $tmp=$this->funcobject->load_browser(); reset($tmp); while(list($key,$value)=each($tmp)){ if(preg_match("/".$value."/i",$this->agent,$matches)){ $id=$key; if(isset($matches[1]) && strlen($matches[1]) > 0){ $this->tmp=$matches[1]; } } } return $id; } function getOs(){ $id=1; $tmp=$this->funcobject->load_os(); reset($tmp); while(list($key,$value)=each($tmp)){ if(preg_match("/".$value."/i",$this->agent)){ $id=$key; } } return $id; } function getRobot(){ $id=1; $tmp=$this->funcobject->load_robot(); reset($tmp); while(list($key,$value)=each($tmp)){ if(preg_match("/".$value."/i",$this->agent)){ $id=$key; } } return $id; } function getCountryCode(){ $ci=0; $sql="SELECT ci FROM tbf_geoip_iplist WHERE '".$this->ip."' BETWEEN start AND end"; $result=$this->funcobject->dbquery($sql); while($data=mysql_fetch_array($result)){ $ci=$data['ci']; } return $ci; } function getCountryCodeName($ci){ $sql="SELECT cc FROM tbf_geoip_cc WHERE ci='".$ci."'"; $cc=mysql_result($this->funcobject->dbquery($sql),0); return $cc; } function gethostname(){ // check if ip address is != local/reserved ip addresses (class a,b,c,d,e,loopback) if($this->ip > 3758096384 || ($this->ip > 3232235520 && $this->ip < 3232301055) || ($this->ip > 2886729728 && $this->ip < 2887778303) || ($this->ip > 167772160 && $this->ip < 184549375) || ($this->ip > 2130706432 && $this->ip < 2147483647)){ $host="N/A"; } else{ $host=gethostbyaddr(long2ip($this->ip)); } return $host; } function deleteOldstats(){ $sql="SELECT COUNT(*) FROM tbf_stats WHERE domain_id='".$this->domain."'"; $alle=mysql_result($this->funcobject->dbquery($sql),0); $sql="SELECT COUNT(*) FROM tbf_stats WHERE domain_id='".$this->domain."' AND time >= '".(time()-$this->config['idle_time'])."'"; $anzahl=mysql_result($this->funcobject->dbquery($sql),0); if($anzahl >= $this->config['show_in_detail']){ $loeschen=$alle-$anzahl; } else{ $loeschen=$alle-$this->config['show_in_detail']; } if($loeschen > 0){ $sql="DELETE FROM tbf_stats WHERE domain_id='".$this->domain."' ORDER BY time ASC LIMIT ".$loeschen; $this->funcobject->dbquery($sql); $sql="OPTIMIZE TABLE tbf_stats"; $this->funcobject->dbquery($sql); } } function generate_output(){ $sql="SELECT SUM(anzahl) FROM tbf_count_ipcountry WHERE domain_id='".$this->domain."'"; $result=$this->funcobject->dbquery($sql); $count=mysql_result($result, 0); $count+=$this->config['fake_counts']; $gfxset=$this->config['counter_template']; $htmlback=$this->config['background_color']; $htmlfore=$this->config['foreground_color']; $xpos=0; $n=StrLen($count); if($gfxset=="none"){ $countingpic = imagecreatetruecolor(1,1); $rgb=$this->hex2rgb($htmlback); $back = ImageColorAllocate ($countingpic,$rgb['r'],$rgb['g'],$rgb['b']); ImageFilledRectangle ($countingpic,0,0,1,1, $back); header ("Content-type: image/png"); imagepng($countingpic); imagedestroy($countingpic); } else if($gfxset=="text"){ $width=$n*8; $countingpic = imagecreatetruecolor($width,16); $rgb=$this->hex2rgb($htmlback); $back = ImageColorAllocate ($countingpic,$rgb['r'],$rgb['g'],$rgb['b']); $rgb=$this->hex2rgb($htmlfore); $fore = ImageColorAllocate ($countingpic,$rgb['r'],$rgb['g'],$rgb['b']); ImageFilledRectangle ($countingpic,0,0,$width,16, $back); ImageString($countingpic,4,0,0,$count,$fore); header ("Content-type: image/png"); imagepng($countingpic); imagedestroy($countingpic); } else{ // get the file type of the template $endung=".gif"; $handle=@opendir("./img/sets/".$gfxset."/"); while ($dirs = @readdir($handle)) { if(preg_match("/^0(\.[jpg|png|jpeg|gif]+)$/is",$dirs,$match)){ $endung=$match[1]; } } @closedir($handle); for ($i=0;$i<$n;$i++) { $name="vorlage".$i; $s=SubStr($count,$i,1); // open the appropriate format $tmp=getimagesize("./img/sets/".$gfxset."/0".$endung); if($tmp[2]==1){ $$name = @ImageCreateFromGIF ("./img/sets/".$gfxset."/".$s.$endung); } else if($tmp[2]==2){ $$name = @ImageCreateFromJPEG ("./img/sets/".$gfxset."/".$s.$endung); } else if($tmp[2]==3){ $$name = @ImageCreateFromPNG ("./img/sets/".$gfxset."/".$s.$endung); } else{ $this->error="Template has wrong format!"; } } if(!(!$$name) && strlen($this->error)==0){ $org_width=imagesx($$name); $org_height=imagesy($$name); $dest_width=$org_width*$n; $dest_height=$org_height; $countingpic = imagecreatetruecolor($dest_width,$org_height); $rgb=$this->hex2rgb($htmlback); $back = ImageColorAllocate ($countingpic,$rgb['r'],$rgb['g'],$rgb['b']); ImageFilledRectangle ($countingpic,0,0,$dest_width,$dest_height, $back); for($i=0;$i<$n;$i++){ $name="vorlage".$i; imagecopy($countingpic, $$name, $xpos, 0, 0, 0, $org_width, $org_height); imagedestroy($$name); $xpos+=$org_width; } header ("Content-type: image/png"); imagepng($countingpic); imagedestroy($countingpic); } else{ $this->funcobject->print_error($this->error,1); die; } } } function hex2rgb($hex) { $color = str_replace('#','',$hex); $rgb = array('r' => hexdec(substr($color,0,2)), 'g' => hexdec(substr($color,2,2)), 'b' => hexdec(substr($color,4,2))); return $rgb; } } ?>