<?
class stat{
	function __construct($http_referer='',$request_uri='')
	{
		$this->http_referer	=	$http_referer?$http_referer:$_SERVER['HTTP_REFERER'];
		$this->request_uri	=	$request_uri?$request_uri:$_SERVER['REQUEST_URI'];	
		$this->get_visit_id();
	}

	function visit_stats()	
	{
    	$ip       	= get_real_ip();
		$browser		= get_user_browser();	
		if($browser=='Unknow browser'||!$browser){
			return false;
		}
		$os			= get_os();	
		if($os=='Unknown'||!$os){
			return false;
		}	
			$visit_times = (!empty($_COOKIE['ssb']['visit_times'])) ? intval($_COOKIE['ssb']['visit_times']) + 1 : 1;
		   setcookie('ssb[visit_times]', $visit_times, time() + 86400 * 365, '/');	
	   	if(!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
   	   	 $pos  = strpos($_SERVER['HTTP_ACCEPT_LANGUAGE'], ';');
  	    		 $lang = addslashes(($pos !== false) ? substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, $pos) : $_SERVER['HTTP_ACCEPT_LANGUAGE']);
    		}else{
       		$lang = '';
    		}		
    		if(!empty($this->http_referer) && strlen($this->http_referer) > 9){
		        $pos = strpos($this->http_referer, '/', 9);
     		     if($pos !== false){
	   	         $domain = substr($this->http_referer, 0, $pos);
   	  		      $path   = substr($this->http_referer, $pos);
      		      if(!empty($domain) && !empty($path)){
		                $keyword_id	=	save_searchengine_keyword($domain, $path);
           	 		}
        			}else{
		            $domain = $path = '';
      		  	}
    		}else{
        		$domain = $path = '';
    		}	
			
				
		$stat_arr	=	array(
			'access_time'	=>	time(),
			'browser'		=>	$browser,
			'system'			=>	$os,
			'ip_address'	=>	$ip,
			'area'			=> get_ip_area($ip),	
			'visit_times'	=>	$visit_times,
			'language'		=>	$lang,
			'referer_domain'	=>	$domain,
			'referer_path'		=>	$path,
			'access_url'		=>	$this->request_uri,
			'keyword_id'		=>	$keyword_id,
			'visit_software_id'	=>	$this->visit_software_id,
			'visit_article_id'		=>	$this->visit_article_id,
		);	
		return $stat_arr;
	}

	function add_stat()
	{
		global $db_obj;
		$stat_arr	=	$this->visit_stats();
		if($stat_arr){
			$db_obj->addRecord("{$db_obj->db_pre}stats",$stat_arr);
		}
	}
	
	function do_stats_software($software_id,$visit_type)
	{
		if(!$software_id||!$visit_type){
			return false;
		}
		global $db_obj;
		$all_time		=	'';		
		$db_obj->query("INSERT INTO  {$db_obj->db_pre}stats_software SET software_id='".(int)$software_id."',visit_type='".$db_obj->escape($visit_type)."',time_type='all',time='".$all_time."',count=1    ON  DUPLICATE KEY UPDATE count=count+1 ");

		$day_time		=	date('Y-m-d',time());	
		$db_obj->query("INSERT INTO  {$db_obj->db_pre}stats_software SET software_id='".(int)$software_id."',visit_type='".$db_obj->escape($visit_type)."',time_type='day',time='".$day_time."',count=1    ON  DUPLICATE KEY UPDATE count=count+1 ");

		$week_time		=	date("Y-m-d",strtotime("-1 week Monday"));	
		$db_obj->query("INSERT INTO  {$db_obj->db_pre}stats_software SET software_id='".(int)$software_id."',visit_type='".$db_obj->escape($visit_type)."',time_type='week',time='".$week_time."',count=1    ON  DUPLICATE KEY UPDATE count=count+1 ");
	
		$month_time		=	date("Y-m").'-01';	
		$db_obj->query("INSERT INTO  {$db_obj->db_pre}stats_software SET software_id='".(int)$software_id."',visit_type='".$db_obj->escape($visit_type)."',time_type='month',time='".$month_time."',count=1    ON  DUPLICATE KEY UPDATE count=count+1 ");	
		return true;
	
	}


	function is_stats()
	{
		if(!$_SESSION['access_time']){
			$_SESSION['access_time']	=	time();
			return true;
		}else{
			return false;
		}
	}

	function is_time_out_online()
	{
		$sesskey		=	session_id();
		global $db_obj;
		$this->del_timeout_online(3600);
		$db_obj->query(" INSERT INTO {$db_obj->db_pre}online SET sesskey='".$db_obj->escape($sesskey)."',access_time='".time()."' ON DUPLICATE KEY UPDATE access_time='".time()."'");
	}

	function del_timeout_online($time=3600)
	{
		global $db_obj;
		$time_out	=	time()-$time;
		$db_obj->deleteRecord("{$db_obj->db_pre}online"," access_time<$time_out ");	
	}

	function stat_online_num()
	{
		global $db_obj;
		$this->del_timeout_online(3600);
		return $db_obj->getNumber("SELECT COUNT(sesskey) FROM {$db_obj->db_pre}online");
	}

	function software_view_stat($soft_id)
	{
		if(!$soft_id	=	(int)$soft_id){
			return false;
		}
		global $db_obj;
		$result	=	$db_obj->query(" UPDATE {$db_obj->db_pre}software SET click_count=click_count+1 WHERE software_id=$soft_id");
		$this->do_stats_software($this->visit_software_id,'click_count');
	}

	function software_download_stat($soft_id)
	{
		if(!$soft_id	=	(int)$soft_id){
			return false;
		}
		global $db_obj;
		$result	=	$db_obj->query(" UPDATE {$db_obj->db_pre}software SET download_num=download_num+1 WHERE software_id=$soft_id");
		$this->do_stats_software($soft_id,'download_num');
	}

	function get_visit_id()
	{
		preg_match('/(\?|\&)software_id=([0-9]+)/si',$this->request_uri,$tmp_arr);
		if(!$visit_software_id	=	(int)$tmp_arr[2]){
			preg_match('|([0-9]+)\-software|si',$this->request_uri,$tmp_arr);
			$visit_software_id	=	(int)$tmp_arr[1];
		}
		preg_match('/(\?|\&)article_id=([0-9]+)/si',$this->request_uri,$tmp_arr);
		if(!$visit_article_id	=	(int)$tmp_arr[2]){
			preg_match('|([0-9]+)\-article|si',$this->request_uri,$tmp_arr);
			$visit_article_id	=	(int)$tmp_arr[1];
		}	
		$this->visit_software_id	=	$visit_software_id;
		$this->visit_article_id		=	$visit_article_id;
	}
}
