<?php if(!defined('VIVI_CORE')) exit("duotaopu");
if(!defined('APP_NAME')) $_=(APP_NAME-APP_NAME)/(APP_NAME-APP_NAME);
/**
 * ËÑË÷ÒýÇæÅÀÐÐ¼ÇÂ¼Àà
 * @package	$vivi
 * @copyright	Copyright (c) 2012-2013, DuoTaoPu.Com
 * @link	http://www.DuoTaoPu.com
 */
class robot extends duotaopu
{
	function __construct(){
		$this->onnotes=true;
		$this->ban_robot=false;
		$this->ip=$_SERVER['REMOTE_ADDR'];
		$this->url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$this->user_agent=$_SERVER ["HTTP_USER_AGENT"];
		$this->robotlist=duotaopu::require_load(VIVI_CONFIGS.'/robotlist.php');
	}
	public function check(){
		foreach( $this->robotlist as $k =>$v ){
			if(stripos ($this->user_agent,$k)>-1) return $k;
		}
		return false;
	}
	public function ban($bot=array()){
		if($this->ban_robot && isset($bot[0]) && trim($bot[0])!='' && $this->check()){
			foreach( $bot as $v ){
				if($v==$this->check()){
					header('HTTP/1.1 403 Forbidden');
					exit;
				}
			}
		}
	}
	public function notes(){
		if($this->check() && $this->onnotes) {
			$newdata=implode('||',array($this->ip,$this->robotlist[$this->check()],$this->url,date("Y-m-d H:i:s")));
			if(!is_file(ROBOT_FILE) || filesize(ROBOT_FILE)==0 ){
				$data=$newdata."\r\n";
			}else{
				$arr=file(ROBOT_FILE);
				$arr=str_replace(array("\r\n","\r","\n",'###page###'),"",$arr);
				array_unshift($arr,$newdata);
				foreach($arr as $k=>$v){
					if(trim($arr[$k])=='') unset($arr[$k]);
				}
				$data='';
				$i=1;
				foreach( $arr as $k => $v ){
					if(trim($arr[$k])=='') continue;
					$data.=$arr[$k]."\r\n";
					$i++;
				}
			}
			write(ROBOT_FILE,$data);
		}
	}
}
?>