<?php
class netssyszone extends netsDb {
	private $sysErrorClassID='0000';
	private $sysErrorFuncID=0;
	private $sysErrorID=0;
	
	public function netssyszone($pConnection){
		$this->netsDb($pConnection);
	}
	public function Check(&$pTableDescriptAR,$pExceptionAR1,$pExceptionAR2){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';

		if(count($pExceptionAR1)==0){
			$pExceptionAR1=array();
		}
		parent::Check($pTableDescriptAR,$pExceptionAR1,$pExceptionAR2);
		//
		$tmpSTR='sztName';
		if(strlen($pTableDescriptAR[0]['Columns'][$tmpSTR]['Value'])<1 && !in_array($tmpSTR,$pExceptionAR2)){
			$this->sysLastError='提交内容错误:1.名称不能为空';
			return $this->sysErrorFuncID.'001';
		}
		
		return 0;
	}
	public function getZone_Byip($pIP,$pCache,&$pData_Zone){
		//-- ------ ### ------ --
		$tmpDefaultID=-1;
		$tmpDefaultName='所有地区';
		$tmpApp_Code=new netsCode();
		$pData_Zone=array(
			'sztID'=>$tmpDefaultID,
			'sztName'=>$tmpDefaultName,
			'sztIP'=>$pIP,
			);
		if ($this->sysFileConfig['Zone_AutoIdentification']==0) {
			return 1;
		}
		//-- #Get city --
		$tmpAR0=explode('.',$pIP);
		$tmpINT=0;
		if (count($tmpAR0)==4) {
			//#IPV4
			$tmpINT=
				$tmpApp_Code->Int32($tmpAR0[0],0)*256*256*256+
				$tmpApp_Code->Int32($tmpAR0[1],0)*256*256+
				$tmpApp_Code->Int32($tmpAR0[2],0)*256+
				$tmpApp_Code->Int32($tmpAR0[3],0);
		}else if (count($tmpAR0)==6) {
			//#IPV6
			$tmpINT=
				$tmpApp_Code->Int32($tmpAR0[0],0)*256*256*256*256*256+
				$tmpApp_Code->Int32($tmpAR0[1],0)*256*256*256*256+
				$tmpApp_Code->Int32($tmpAR0[2],0)*256*256*256+
				$tmpApp_Code->Int32($tmpAR0[3],0)*256*256+
				$tmpApp_Code->Int32($tmpAR0[4],0)*256+
				$tmpApp_Code->Int32($tmpAR0[5],0);
		}else {
			return 1;
		}
		//#Search
		if ($this->TestTableExist($this->sysTableHead.'enquiryip')==0 && 1==2) {
			//#From db
			$tmpSQL=' SELECT A.eniptID,A.eniptName,A.eniptParid,
				A.eniptInfo_Provinces,A.eniptInfo_City,A.eniptInfo_Zoneid
				FROM {Nets.TableHead}enquiryip AS A
				WHERE 1=1 AND A.eniptIP_0<={Nets.INT} AND A.eniptIP_1>={Nets.INT}
				ORDER BY A.eniptIP_0 DESC LIMIT 0,1 ';
			$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
			$tmpSTR=str_replace('{Nets.INT}',$tmpINT,$tmpSTR);
			$tmpData0=$this->Search($tmpSTR);
			if (count($tmpData0)>0) {
				$pData_Zone['sztName']=$tmpData0[0]['eniptInfo_City'];
			}
			
		}else {
			//#From http://product.unionnets.com
			$tmpURL=$this->sysFileConfig['sysCoreAPISITE'].$this->sysFileConfig['sysCoreZoneIPURL'].'?DIP='.$tmpINT;
			$tmpCONTENT=$this->readURL($tmpURL);
			$tmpAR0=explode('<!--sendRequest-->',$tmpCONTENT);
			$tmpAR0=explode('<!--S.Data-->',$tmpAR0[0]);
			if (count($tmpAR0)==2) {
				if ($tmpAR0[1]!='' && $tmpAR0[1]!='调试') {
					$pData_Zone['sztName']=$tmpAR0[1];
				}
			}
			
		}
		if ($pData_Zone['sztName']==$tmpDefaultName) {
			$tmpURL=str_replace('{Nets.IP}',$pIP,$this->sysFileConfig['ZoneIpDbUrl']);
			$tmpCONTENT=$this->readURL($tmpURL);
			$tmpCONTENT=iconv('GB2312',$this->sysCharset.'//IGNORE',$tmpCONTENT);
			$tmpAR0=explode('省',$tmpCONTENT);
			if (count($tmpAR0)>1) {
				$tmpAR0=explode('市',$tmpAR0[1]);
				$pData_Zone['sztName']=$tmpAR0[0];
			}
		}
		//#End
		if ($pData_Zone['sztName']==$tmpDefaultName) {
			$pData_Zone=array(
				'sztID'=>$tmpDefaultID,
				'sztName'=>$tmpDefaultName,
				'sztIP'=>$pIP,
				);
			return 1;
		}
		//-- ------ Get zone ------ --
		$tmpData0=array();
		if(count($pCache)>0){
			//-- #Cache --
			$tmpFlag=0;
			$Coun0=0;
			while(count($pCache)>$Coun0){
				if($pCache[$Coun0]['sztType']==3 && $pCache[$Coun0]['sztName']==$pData_Zone['sztName']){
					$tmpFlag=1;
					$tmpData0[]=$pCache[$Coun0];
				}
				$Coun0++;
			}
		}else{
			//-- #Db --
			$tmpSQL=' SELECT A.sztID,A.sztName
				FROM {Nets.TableHead}netssyszone AS A
				WHERE 1=1 AND A.sztRecycle=0 AND A.sztChecked>1 AND A.sztType=3 AND A.sztName=\'{Nets.Name}\' ';
			$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
			$tmpSTR=str_replace('{Nets.Name}',$pData_Zone['sztName'],$tmpSTR);
			$tmpData0=$this->Search($tmpSTR);
		}
		if(count($tmpData0)!=1){
			$pData_Zone=array(
				'sztID'=>$tmpDefaultID,
				'sztName'=>$tmpDefaultName,
				'sztIP'=>$pIP,
				);
			return 1;
		}
		$pData_Zone=array(
			'sztID'=>$tmpData0[0]['sztID'],
			'sztName'=>$tmpData0[0]['sztName'],
			'sztIP'=>$pIP,
			);
		
		
		return 0;
	}
	public function getZone_One($pID,$pCache,&$pData_Zone){
		//-- ------ Get zone ------ --
		if(count($pCache)>0){
			//-- #Cache --
			$tmpFlag=0;
			$Coun0=0;
			while(count($pCache)>$Coun0){
				if($pCache[$Coun0]['sztID']==$pID){
					$tmpFlag=1;
					$pData_Zone=$pCache[$Coun0];
					$Coun0=count($pCache)+1;
				}
				$Coun0++;
			}
			if($tmpFlag==0){
				$pData_Zone=array();
			}
		}else{
			//-- #Db --
			$tmpSQL=' SELECT A.sztID,A.sztName 
				FROM {Nets.TableHead}netssyszone AS A
				WHERE 1=1 AND A.sztRecycle=0 AND A.sztChecked>1 AND A.sztID={Nets.ID} ';
			$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
			$tmpSTR=str_replace('{Nets.ID}',$pID,$tmpSTR);
			$tmpData0=$this->Search($tmpSTR);
			if(count($tmpData0)==1){
				$pData_Zone=$tmpData0[0];
			}else{
				$pData_Zone=array();
			}
		}
		
		return 0;
	}
	public function getZone_Child($pID,$pCache,&$pData_Zone){
		//-- ------ Get zone ------ --
		if(count($pCache)>0){
			//-- #Cache --
			$Coun0=0;
			foreach($pCache as $Row0){
				if($Row0['sztParid']==$pID){
					$pData_Zone[]=$Row0;
				}
				$Coun0++;
			}
		}else{
			//-- #Db --
			$tmpSQL=' SELECT A.sztID,A.sztName 
				FROM {Nets.TableHead}netssyszone AS A
				WHERE 1=1 AND A.sztRecycle=0 AND A.sztChecked>1 AND A.sztParid={Nets.ID} ';
			$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
			$tmpSTR=str_replace('{Nets.ID}',$pID,$tmpSTR);
			$pData_Zone=$this->Search($tmpSTR);
		}
		
		return 0;
	}
	public function chooseZone($pID,$pCache,&$pData_Zone){
		//-- ------ Get zone ------ --
		$this->getZone_One($pID,$pCache,$pData_Zone);
		if(count($pData_Zone)==0){
			$pData_Zone=array(
				'sztID'=>-1,
				'sztName'=>'所有地区',
				);
			return 1;
		}
		//Write cookie
		$this->writeCookie($this->sysNormalCookiePath,$this->sysCookieHead.$this->sysZoneCookieName,$pData_Zone['sztID'].$this->sysSPLIT1.$pData_Zone['sztName']);
		
		return 0;
	}
	public function saveToFile(){
		//-- ------ ### ------ --
		//-- ### --
		$tmpSQL=' SELECT A.sztID,A.sztName,A.sztParid,A.sztType,A.sztSign,A.sztAlphabet,
			B.szctSourceR00,B.szctIntroduct
			FROM {Nets.TableHead}netssyszone AS A
			INNER JOIN {Nets.TableHead}netssyszonecont AS B ON B.szctIndexID=A.sztID
			WHERE 1=1 AND A.sztRecycle=0 AND A.sztChecked>1 ';
		$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
		$tmpData0=$this->Search($tmpSTR);
		//-- ### --
		$tmpCONTENT='<?php $netsAR_Zone=array(';
		$Coun0=0;
		foreach ($tmpData0 as $Row0){
			$tmpCONTENT.=' array(\'sztID\'=>\''.$Row0['sztID'].'\',\'sztName\'=>\''.$Row0['sztName'].'\',\'sztParid\'=>\''.$Row0['sztParid'].'\',\'sztType\'=>\''.$Row0['sztType'].'\',\'sztSign\'=>\''.$Row0['sztSign'].'\',\'sztAlphabet\'=>\''.$Row0['sztAlphabet'].'\',\'szctSourceR00\'=>\''.$Row0['szctSourceR00'].'\',\'szctIntroduct\'=>\''.$Row0['szctIntroduct'].'\',),
';
			
			$Coun0++;
		}
		$tmpCONTENT.=' ); ?>';
		//-- ### --
		$tmpPATH=$this->sysFileConfig['PathRoot'].$this->sysFileConfig['PathZoneCache'].'Cache_0.php';
		$this->writeFile($tmpPATH,$tmpCONTENT);
		return 0;
	}
	public function getZonePath($pID,$pCache){
		//-- ------ ### ------ --
		//-- ### --
		$tmpCoun_Path=0;
		$tmpAR_Path=array();
		$tmpFlag=0;
		$tmpSQL=' SELECT A.sztID,A.sztName,A.sztParid,A.sztType,A.sztSign,
			B.szctSourceR00,B.szctIntroduct
			FROM {Nets.TableHead}netssyszone AS A
			INNER JOIN {Nets.TableHead}netssyszonecont AS B ON B.szctIndexID=A.sztID
			WHERE 1=1 AND A.sztRecycle=0 AND A.sztChecked>1 AND A.sztID={Nets.ID} ';
		//-- ### --
		if (count($pCache)==0) {
			$tmpFlag=1;
		}
		//-- ### --
		$tmpAR0=array();
		if ($tmpFlag==0) {
			foreach ($pCache as $Row0){
				if ($Row0['sztID']==$pID) {
					$tmpAR0=$Row0;
				}
			}
		}else {
			$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
			$tmpSTR=str_replace('{Nets.ID}',$pID,$tmpSTR);
			$tmpData0=$this->Search($tmpSTR);
			foreach ($tmpData0 as $Row0){
				$tmpAR0=$Row0;
			}
		}
		$tmpAR_Path[$tmpCoun_Path]=$tmpAR0;
		$tmpCoun_Path++;
		
		$Coun0=0;
		while (count($tmpAR0)>0 && $Coun0<100) {
			if ($tmpFlag==0) {
				$Coun1=0;
				while(count($pCache)>$Coun1){
					if ($pCache[$Coun1]['sztID']==$tmpAR0['sztParid']) {
						$tmpAR0=$pCache[$Coun1];
						$Coun1=count($pCache)+1;
					}
					$Coun1++;
				}
			}else {
				$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
				$tmpSTR=str_replace('{Nets.ID}',$tmpAR0['sztParid'],$tmpSTR);
				$tmpData0=$this->Search($tmpSTR);
				foreach ($tmpData0 as $Row0){
					$tmpAR0=$Row0;
				}
			}
			if (count($tmpAR0)>0) {
				$tmpAR_Path[$tmpCoun_Path]=$tmpAR0;
				$tmpCoun_Path++;
			}
			if (count($tmpAR0)==0 || $tmpAR0['sztParid']==0) {
				$tmpAR0=array();
			}
			$Coun0++;
		}
		
		$tmpAR_Path=array_reverse($tmpAR_Path);
		return $tmpAR_Path;
	}
	
	
	
}
?>