<?php
class netsHtml extends netsPageCache {
	private $sysErrorClassID='0000';
	private $sysErrorFuncID=0;
	private $sysErrorID=0;
	
	public function netsHtml(){
		$this->netsPageCache();
		$this->getConfig();
	}
	
	public function Message($pFlag,$pTitle,$pMessage,$pRedirect){
		$this->sysErrorFuncID=$this->sysErrorClassID.'2';
		$tmpCode=new netsCode();
		//-- ### --
		if ($pFlag=='SUCCESS' && $pRedirect!='' && $this->sysFileConfig['MessageFrameAble']==0) {
			header('Location: '.$pRedirect);
			exit();
		}
		
		//-- ### --
		if($pTitle==''){
			$pTitle='系统消息 - '.$this->sysFileConfig['sysTitle'];
		}else{
			$pTitle=$pTitle.' - '.$this->sysFileConfig['sysTitle'];
		}
		$pTitle=$tmpCode->FilterINPUT($pTitle,0);
		$pMessage=$tmpCode->FilterINPUT($pMessage,0);
		
		$tmpSTR='tmpFrames.showMessage(\'{Nets.Title}\',\'{Nets.Message}\',\'{Nets.Redirect}\',0);';
		$tmpSTR=str_replace('{Nets.Title}',$pTitle,$tmpSTR);
		$tmpSTR=str_replace('{Nets.Message}',$pMessage,$tmpSTR);
		$tmpSTR=str_replace('{Nets.Redirect}',$pRedirect,$tmpSTR);
		return $tmpSTR;
	}
	public function Message_Frame($pCode,$pMessagePath,$pTitle,$pMessage,$pRedirect){
		$this->sysErrorFuncID=$this->sysErrorClassID.'1';
		$tmpCode=new netsCode();
		
		if($pTitle==''){
			$pTitle='系统消息 - '.$this->sysFileConfig['sysTitle'];
		}else{
			$pTitle=$pTitle.' - '.$this->sysFileConfig['sysTitle'];
		}
		
		$tmpSTR='window.location=\'{Nets.Path}?DCode={Nets.Code}&DMessage={Nets.Title},{Nets.Message},{Nets.Redirect}\';';
		$tmpSTR=str_replace('{Nets.Path}',$pMessagePath,$tmpSTR);
		$tmpSTR=str_replace('{Nets.Code}',$pCode,$tmpSTR);
		$tmpSTR=str_replace('{Nets.Title}',$pTitle,$tmpSTR);
		$tmpSTR=str_replace('{Nets.Message}',$pMessage,$tmpSTR);
		$tmpSTR=str_replace('{Nets.Redirect}',$pRedirect,$tmpSTR);	
		return $tmpSTR;
	}
	public function Message_Js($pMessage){
		$this->sysErrorFuncID=$this->sysErrorClassID.'1';
		$tmpSTR="alert('{Nets.Message}');";
		$tmpSTR=str_replace('{Nets.Message}',$pSTR,$tmpSTR);
		return $tmpSTR;
	}
	public function Redirect($pURL){
		$this->sysErrorFuncID=$this->sysErrorClassID.'4';
		header('Location: '.$pURL);
		exit();
		return 0;
	}
	public function Redirect_Js($pURL){
		$this->sysErrorFuncID=$this->sysErrorClassID.'4';
		$tmpSTR="window.location='{Nets.URL}';";
		$tmpSTR=str_replace('{Nets.URL}',$pURL,$tmpSTR);
		return $tmpSTR;
	}
	public function Redirect_Js_Target($pURL,$pTarget){
		$this->sysErrorFuncID=$this->sysErrorClassID.'3';
		$tmpSTR="window.open('{Nets.URL}','{Nets.Target}');";
		$tmpSTR=str_replace('{Nets.URL}',$pURL,$tmpSTR);
		$tmpSTR=str_replace('{Nets.Target}',$pTarget,$tmpSTR);
		return $tmpSTR;
	}
	
	public function AnalyerRequest($pSTR,$pDefault,$pType){
		$tmpSPLIT0='------Nets,------';
		$tmpCode=new netsCode();
		
		$tmpAR0=array();
		$tmpAR1=explode($this->sysSPLIT1,$pDefault);
		//Analyer request
		if($pType==0){
			//Querystring
			if(!isset($_GET[$pSTR])){
				return $tmpAR1;
			}
			$tmpAR0=explode($this->sysSPLIT1,$tmpCode->FilterINPUT($_GET[$pSTR],1));
		}else if($pType==1){
			//Form
			if(!isset($_POST[$pSTR])){
				return array($pDefault);
			}
			$tmpSTR='';
			$tmpAR0=$_POST[$pSTR];
			if(is_array($tmpAR0)){
				foreach ($tmpAR0 as $Cell0){
					$tmpSTR.=$Cell0.$this->sysSPLIT1;
				}
			}else {
				$tmpSTR=$tmpAR0;
			}
			$tmpSTR=$tmpCode->FilterINPUT($tmpSTR,1);
			$tmpAR0=array($tmpSTR);
			$tmpAR1=array($pDefault);
		}else if($pType==2){
			//Cookie
			if(!isset($_COOKIE[$pSTR])){
				return $tmpAR1;
			}
			$tmpAR0=explode($this->sysSPLIT1,$tmpCode->FilterINPUT($_COOKIE[$pSTR],1));
		}else if($pType==3){
			//Security Cookie
			if(!isset($_COOKIE[$pSTR])){
				return $tmpAR1;
			}
			$tmpAR0=array($_COOKIE[$pSTR]);
		}
		if(count($tmpAR0)!=count($tmpAR1)){
			return $tmpAR1;
		}
		//-- #Check format --
		/*
		$Coun0=0;
		while(count($tmpAR0)>$Coun0){
			if($tmpCode->Int32($tmpAR1[$Coun0],0)==$tmpAR1[$Coun0]){
				$tmpAR0[$Coun0]=$tmpCode->Int32($tmpAR1[$Coun0],0);
			}
			if($tmpCode->Decimal($tmpAR1[$Coun0],0.00)==$tmpAR1[$Coun0]){
				$tmpAR0[$Coun0]=$tmpCode->Decimal($tmpAR1[$Coun0],0.00);
			}
			$Coun0++;
		}
		*/
		return $tmpAR0;
	}
	public function getAllPost(&$pSTR){
		$tmpSTR='';
		foreach($_POST as $dset => $Value){
    		$tmpSTR='$_POST["'.$dset.'"]='.$Value.$this->sysSPLIT4;
		}
		
		$pSTR=$tmpSTR;
		return 0;
	}
	public function getSiteHome(){
		$tmpAR0=array('/Mana/','/Platform/','/QPlatform/','/User/','/Test/','/Plus/',
			'/Config/','/Component/','/Inc/','/Template/','/Html/','/Ui/','/Uploadfiles/',);
		
		$tmpHome='';
		$tmpPath='';
		if(isset($_SERVER['HTTP_X_FORWARDED_HOST'])){
			$tmpHome=$_SERVER['HTTP_X_FORWARDED_HOST'];
			$tmpPath=$_SERVER['HTTP_X_FORWARDED_HOST'].$_SERVER['PHP_SELF'];
		}else if(isset($_SERVER['HTTP_HOST'])){
			$tmpHome=$_SERVER['HTTP_HOST'];
			$tmpPath=$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
		}
		
		foreach ($tmpAR0 as $Cell){
			$tmpAR1=explode($Cell,$tmpPath);
			if(count($tmpAR1)==2){
				$tmpHome=$tmpAR1[0].$this->sysSPLIT9;
			}
		}
		$tmpSTR='http://'.$tmpHome;
		
		return $tmpSTR;
	}
	public function getSiteURL(){
		$hostPath='';
		if(isset($_SERVER['HTTP_X_FORWARDED_HOST'])){
			$hostPath=$_SERVER['HTTP_X_FORWARDED_HOST'].$_SERVER['PHP_SELF'];
		}else if(isset($_SERVER['HTTP_HOST'])){
			$hostPath=$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
		}
		$tmpSTR='http://'.$hostPath;
		
		return $tmpSTR;
	}
	public function getURL(){
		$hostPath='';
		if(isset($_SERVER['HTTP_X_FORWARDED_HOST'])){
			$hostPath=$_SERVER['HTTP_X_FORWARDED_HOST'].$_SERVER['PHP_SELF'];
		}else if(isset($_SERVER['HTTP_HOST'])){
			$hostPath=$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
		}
		$tmpSTR='http://'.$hostPath.'?'.$_SERVER["QUERY_STRING"];
		
		return $tmpSTR;
	}
	public function getSitePath($pPath){
		$tmpSTR='';
		$tmpSPLIT='/';
		if(isset($_SERVER['HTTP_X_FORWARDED_HOST'])){
			$tmpSTR=$_SERVER['HTTP_X_FORWARDED_HOST'].$_SERVER['PHP_SELF'];
		}else if(isset($_SERVER['HTTP_HOST'])){
			$tmpSTR=$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
		}
		$tmpAR0=explode($tmpSPLIT,$tmpSTR);
		$tmpSTR='';
		array_pop($tmpAR0);
		foreach ($tmpAR0 as $Cell){
			$tmpSTR.=$Cell.$tmpSPLIT;
		}
		
		return 'http://'.$tmpSTR.$pPath;
	}
	public function getIP() {
		$tmpSTR='';
		if(array_key_exists('HTTP_CLIENT_IP', $_SERVER)){
			$tmpSTR=$_SERVER['HTTP_CLIENT_IP'];
		}else if(array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)){
			$tmpSTR=$_SERVER['HTTP_X_FORWARDED_FOR'];
		}else {
			$tmpSTR=$_SERVER['REMOTE_ADDR'];
		}
		
		return $tmpSTR;
	}
	public function submitLimitRead($pURL,$pViews,$pTime,$pFreeze){
		$this->sysErrorFuncID=$this->sysErrorClassID.'1';
		$tmpCode=new netsCode();
		//-- ------ #Check parameter ------ --
		if($pViews<0){
			$pViews=$this->sysFileConfig['SubmitLimitViews'];
		}
		if($pTime<0){
			$pTime=$this->sysFileConfig['SubmitLimitTime'];
		}
		if($pFreeze<0){
			$pFreeze=$this->sysFileConfig['SubmitLimitFreeze'];
		}
		//-- ------ # ------ --
		$tmpTemplate=array('URL'=>$pURL,'FirstTime'=>date($this->sysTimeFormat),'FreezeTime'=>'',
			'Time_Long'=>$pTime,'Freeze_Long'=>$pFreeze,'Views_Total'=>$pViews,'Views_Yet'=>1,);
		//Read cookie
		$tmpMain=array();
		$tmpSTR=$this->readCookie($this->sysSubmitTimeLimitName);
		$tmpAR0=explode($this->sysSPLIT15,$tmpSTR);
		array_pop($tmpAR0);
		if (count($tmpAR0)==count($tmpTemplate)) {
			$tmpMain['URL']=$tmpAR0[0];
			$tmpMain['FirstTime']=$tmpAR0[1];
			$tmpMain['FreezeTime']=$tmpAR0[2];
			$tmpMain['Time_Long']=$tmpAR0[3];
			$tmpMain['Freeze_Long']=$tmpAR0[4];
			$tmpMain['Views_Total']=$tmpAR0[5];
			$tmpMain['Views_Yet']=$tmpAR0[6];
		}
		//Check URL exist
		$tmpIndex=-1;
		$Coun0=0;
		foreach ($tmpMain as $Cell){
			if($Cell['URL']==$pURL){
				$tmpIndex=$Coun0;
			}
			$Coun0++;
		}
		//Done
		if($tmpIndex<0){
			//Add new
			$tmpAR0=$tmpTemplate;
			$tmpAR0['URL']=$pURL;
			$tmpAR0['FirstTime']=date($this->sysTimeFormat);
			$tmpMain[]=$tmpAR0;
		}else {
			$tmpFirstTime=$tmpCode->TimeToInt($tmpMain[$tmpIndex]['FirstTime']);
			$tmpFreezeTime=$tmpCode->TimeToInt($tmpMain[$tmpIndex]['FreezeTime']);
			$tmpNowTime=$tmpCode->TimeToInt(date($this->sysTimeFormat));
			if($tmpFreezeTime>0){
				//Freezed
				if($tmpMain[$tmpIndex]['Freeze_Long']>=($tmpNowTime-$tmpFreezeTime)){
					//In the period of time
					return $this->sysErrorFuncID.'001';
				}else {
					//In time
					$tmpMain[$tmpIndex]['Views_Yet']=1;
					$tmpMain[$tmpIndex]['FreezeTime']='';
				}
			}else {
				//UnFreeze
				if($tmpMain[$tmpIndex]['Time_Long']<=($tmpNowTime-$tmpFirstTime)){
					//In the period of time
					$tmpMain[$tmpIndex]['Views_Yet']=$tmpCode->Int32($tmpMain[$tmpIndex]['Views_Yet'],1)+1;
					//Check views count
					if($tmpMain[$tmpIndex]['Views_Total']<$tmpMain[$tmpIndex]['Views_Yet']){
						$tmpMain[$tmpIndex]['FreezeTime']=date($this->sysTimeFormat);
						//Write cookie
						$tmpSTR='';
						foreach ($tmpMain as $Row_Main){
							$tmpSTR=$Row_Main.$this->sysSPLIT15;
						}
						$this->writeCookie($this->sysNormalCookiePath,$this->sysSubmitTimeLimitName,$tmpSTR);
						return $this->sysErrorFuncID.'001';
					}
				}else {
					//In time
					$tmpMain[$tmpIndex]['Views_Yet']=1;
					$tmpMain[$tmpIndex]['FreezeTime']='';
				}
			}
		}
		
		return 0;
	}
	public function submitLimitWrite($pURL,$pViews,$pTime,$pFreeze){
		$this->sysErrorFuncID=$this->sysErrorClassID.'1';
		$tmpCode=new netsCode();
		//-- ------ #Check parameter ------ --
		if($pViews<0){
			$pViews=$this->sysFileConfig['SubmitLimitViews'];
		}
		if($pTime<0){
			$pTime=$this->sysFileConfig['SubmitLimitTime'];
		}
		if($pFreeze<0){
			$pFreeze=$this->sysFileConfig['SubmitLimitFreeze'];
		}
		//-- ------ # ------ --
		$tmpTemplate=array('URL'=>$pURL,'FirstTime'=>date($this->sysTimeFormat),'FreezeTime'=>'',
			'Time_Long'=>$pTime,'Freeze_Long'=>$pFreeze,'Views_Total'=>$pViews,'Views_Yet'=>1,);
		//Read cookie
		$tmpMain=array();
		$tmpSTR=$this->readCookie($this->sysSubmitTimeLimitName);
		$tmpAR0=explode($this->sysSPLIT15,$tmpSTR);
		array_pop($tmpAR0);
		if (count($tmpAR0)==count($tmpTemplate)) {
			$tmpMain['URL']=$tmpAR0[0];
			$tmpMain['FirstTime']=$tmpAR0[1];
			$tmpMain['FreezeTime']=$tmpAR0[2];
			$tmpMain['Time_Long']=$tmpAR0[3];
			$tmpMain['Freeze_Long']=$tmpAR0[4];
			$tmpMain['Views_Total']=$tmpAR0[5];
			$tmpMain['Views_Yet']=$tmpAR0[6];
		}
		//Check URL exist
		$tmpIndex=-1;
		$Coun0=0;
		foreach ($tmpMain as $Cell){
			if($Cell['URL']==$pURL){
				$tmpIndex=$Coun0;
			}
			$Coun0++;
		}
		//Done
		if($tmpIndex<0){
			//Add new
			$tmpAR0=$tmpTemplate;
			$tmpAR0['URL']=$pURL;
			$tmpAR0['FirstTime']=date($this->sysTimeFormat);
			$tmpMain[]=$tmpAR0;
		}else {
			$tmpFirstTime=$tmpCode->TimeToInt($tmpMain[$tmpIndex]['FirstTime']);
			$tmpFreezeTime=$tmpCode->TimeToInt($tmpMain[$tmpIndex]['FreezeTime']);
			$tmpNowTime=$tmpCode->TimeToInt(date($this->sysTimeFormat));
			if($tmpFreezeTime>0){
				//Freezed
				if($tmpMain[$tmpIndex]['Freeze_Long']>=($tmpNowTime-$tmpFreezeTime)){
					//In the period of time
					return $this->sysErrorFuncID.'001';
				}else {
					//In time
					$tmpMain[$tmpIndex]['Views_Yet']=1;
					$tmpMain[$tmpIndex]['FreezeTime']='';
				}
			}else {
				//UnFreeze
				if($tmpMain[$tmpIndex]['Time_Long']<=($tmpNowTime-$tmpFirstTime)){
					//In the period of time
					$tmpMain[$tmpIndex]['Views_Yet']=$tmpCode->Int32($tmpMain[$tmpIndex]['Views_Yet'],1)+1;
					//Check views count
					if($tmpMain[$tmpIndex]['Views_Total']<$tmpMain[$tmpIndex]['Views_Yet']){
						$tmpMain[$tmpIndex]['FreezeTime']=date($this->sysTimeFormat);
						//Write cookie
						$tmpSTR='';
						foreach ($tmpMain as $Row_Main){
							$tmpSTR=$Row_Main.$this->sysSPLIT15;
						}
						$this->writeCookie($this->sysNormalCookiePath,$this->sysSubmitTimeLimitName,$tmpSTR);
						return $this->sysErrorFuncID.'001';
					}
				}else {
					//In time
					$tmpMain[$tmpIndex]['Views_Yet']=1;
					$tmpMain[$tmpIndex]['FreezeTime']='';
				}
			}
		}
		//Write cookie
		$tmpSTR='';
		foreach ($tmpMain as $Row_Main){
			$tmpSTR=$Row_Main.$this->sysSPLIT15;
		}
		$this->writeCookie($this->sysNormalCookiePath,$this->sysSubmitTimeLimitName,$tmpSTR);
		return 0;
	}
	
	
}
?>