<?php
class netssyssheme extends netsDb {
	private $sysErrorClassID='0000';
	private $sysErrorFuncID=0;
	private $sysErrorID=0;
	
	public function netssyssheme($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='sstName';
		if(strlen($pTableDescriptAR[0]['Columns'][$tmpSTR]['Value'])<1 && !in_array($tmpSTR,$pExceptionAR2)){
			$this->sysLastError='提交内容错误:1.名称不能为空';
			return $this->sysErrorFuncID.'001';
		}
		
		return 0;
	}
	
	public function makeHtml_BYID(&$pDescriptions,&$pIndexSection,&$pIndexCell){
		$this->sysErrorFuncID=$this->sysErrorClassID.'02';
		//-- ------ # ------ --
		$tmpSTR=$this->getSheme($pDescriptions[0]['Columns']['uitID']['Value']);
		if($tmpSTR==''){
			$this->sysLastError='生成失败:请先创建HTML页面生成规则';
			return $this->sysErrorFuncID.'002';
		}
		
		$pDescriptions[1]['Columns']['ssctHtmlDescription']['Value']=$tmpSTR;
		$tmpINT=$this->makeHtml($pDescriptions,$pIndexSection,$pIndexCell);
		
		return 0;
	}
	public function makeHtml($pDescriptions,&$pIndexSection,&$pIndexCell){
		$tmpAR0=$this->getRequestURL($pDescriptions[1]['Columns']['ssctHtmlDescription']['Value'],$pIndexSection,$pIndexCell);
		//-- ------ #Make html ------ --
		if(count($tmpAR0)==0){
			$this->sysLastMessage='所有HTML页面已生成完毕';
			$pIndexCell=99999;
			return $this->sysErrorFuncID.'001';
		}
		foreach ($tmpAR0 as $Cell){
			$tmpCONTENT=$this->readURL($this->sysFileConfig['sysURLbussinessRoot'].$Cell['ORIGIN_URL']);
			$tmpCONTENT=$this->replaceURL($pDescriptions[1]['Columns']['ssctHtmlDescription']['Value'],$Cell['ORIGIN_URL'],$Cell['HTML_URL'],$tmpCONTENT);
			$this->writeFile($this->sysFileConfig['PathRoot'].$Cell['HTML_URL'],$tmpCONTENT);
		}
		
		$this->sysLastMessage='已成功生成了段号为['.$pIndexCell.']的静态页面。';
		//$this->sysLastMessage='所有HTML页面已生成完毕';
		$pIndexCell++;
		return 0;
	}
	public function getRequestURL($pSTR,&$pIndexSection,&$pIndexCell){
		$tmpStartFlag='{Nets';
		$tmpEndFlag='}';
		$tmpSPLIT0='.';
		$tmpSPLIT1='-';
		$tmpSPLIT2=',';
		$tmpCode=new netsCode();
		//-- ------ #Read rules ------ --
		$tmpAR0=explode($this->sysSPLIT4.$this->sysSPLIT4,$pSTR);
		if(count($tmpAR0)-1<$pIndexSection){
			return array();
		}
		
		$tmpLINES=explode($this->sysSPLIT4,$tmpAR0[$pIndexSection]);
		if(count($tmpLINES)==0){
			return array();
		}
		if($tmpLINES[0]==''){
			return array();
		}
		//-- ------ #Array by rules ------ --
		$tmpRULES=array();
		$Coun0=1;
		while (count($tmpLINES)>$Coun0) {
			$tmpAR0=explode($this->sysSPLIT6,$tmpLINES[$Coun0]);
			$tmpLINE_A='';
			$tmpLINE_B='';
			if (count($tmpAR0)==2){
				$tmpLINE_A=$tmpAR0[0];
				$tmpLINE_B=$tmpAR0[1];
			}
			$tmpAR0=$this->getRules($tmpLINE_A,$tmpLINE_B);
			$tmpRULES=array_merge($tmpRULES,$tmpAR0);
			$Coun0++;
		}
		//-- ------ # ------ --
		$tmpAR0=$tmpRULES;
		$tmpRULES=array();
		$Coun0=0;
		foreach ($tmpAR0 as $Cell){
			if($pIndexCell*$this->sysFileConfig['MakeHtmlSegmentSize']<$Coun0 && ($pIndexCell+1)*$this->sysFileConfig['MakeHtmlSegmentSize']>$Coun0){
				$tmpRULES[]=$Cell;
			}
			$Coun0++;
		}
		
		return $tmpRULES;
	}
	public function replaceURL($pSTR,$pOriginpath,$pSavepath,$pContent){
		$tmpStartFlag='{Nets';
		$tmpEndFlag='}';
		$tmpSPLIT0='.';
		$tmpSPLIT1='-';
		$tmpSPLIT2=',';
		$tmpSPLIT3='/';
		$tmpDEFINE0='..';
		//-- ------ #Read rules ------ --
		$tmpSTR=str_replace($this->sysSPLIT4.$this->sysSPLIT4,$this->sysSPLIT4,$pSTR);
		$tmpLINES=explode($this->sysSPLIT4,$tmpSTR);
		if(count($tmpLINES)==0){
			return $pContent;
		}
		if($tmpLINES[0]==''){
			return $pContent;
		}
		//-- ------ #Save back string ------ --
		$tmpAR0=explode($tmpSPLIT3,$pOriginpath);
		array_pop($tmpAR0);
		$tmpSTR='';
		foreach ($tmpAR0 as $Cell){
			$tmpSTR.=$tmpDEFINE0.$tmpSPLIT3;
		}
		$tmpBACKSTRING_A=$tmpSTR;
		
		$tmpAR0=explode($tmpSPLIT3,$pSavepath);
		array_pop($tmpAR0);
		$tmpSTR='';
		foreach ($tmpAR0 as $Cell){
			$tmpSTR.=$tmpDEFINE0.$tmpSPLIT3;
		}
		$tmpBACKSTRING_B=$tmpSTR;
		//-- ------ #LINES ------ --
		foreach ($tmpLINES as $cellLINES){
			$tmpAR0=explode($this->sysSPLIT6,$cellLINES);
			$tmpLINE_A='';
			$tmpLINE_B='';
			if (count($tmpAR0)==2){
				$tmpLINE_A=$tmpAR0[0];
				$tmpLINE_B=$tmpAR0[1];
				//Reread URL
				$tmpLINE_A=$tmpBACKSTRING_A.$tmpLINE_A;
				$tmpLINE_B=$tmpBACKSTRING_B.$tmpLINE_B;
				$tmpRULES=$this->getRules($tmpLINE_A,$tmpLINE_B);
				//Final replace
				foreach ($tmpRULES as $Cell){
					$pContent=str_replace($Cell['ORIGIN_URL'],$Cell['HTML_URL'],$pContent);
				}
			}
		}
		//-- ------ #Replace UI path ------ --
		$tmpAR0=array('Component','Config','Platform','Mana','User','Test','Template',
			'Plus','Ui','Uploadfiles',
			'Common/Core0',);
		foreach ($tmpAR0 as $Cell){
			$pContent=str_replace($tmpBACKSTRING_A.$Cell.$tmpSPLIT3,$tmpBACKSTRING_B.$Cell.$tmpSPLIT3,$pContent);
		}
		return $pContent;
	}
	public function getRules($pOriginurl,$pHtmlurl){
		$tmpStartFlag='{Nets';
		$tmpEndFlag='}';
		$tmpSPLIT0='.';
		$tmpSPLIT1='-';
		$tmpSPLIT2=',';
		
		$tmpLINE_A=$pOriginurl;
		$tmpLINE_B=$pHtmlurl;
		//Get flags
		$tmpFLAGS=array();
		$tmpStartIndex=0;
		$tmpEndIndex=0;
		$Coun1=0;
		while(strlen($tmpLINE_A)>$tmpStartIndex){
			$tmpStartIndex=stripos($tmpLINE_A,$tmpStartFlag,$tmpStartIndex);
			if($tmpStartIndex==''){
				$tmpStartIndex=strlen($tmpLINE_A);
			}else {
				$tmpStartIndex+=strlen($tmpStartFlag);
				$tmpEndIndex=stripos($tmpLINE_A,$tmpEndFlag,$tmpStartIndex);
				$tmpFLAGS[]=substr($tmpLINE_A,$tmpStartIndex,$tmpEndIndex-$tmpStartIndex);
			}
			$Coun1++;
		}
		//Read indexs
		$tmpINDEXS=array();
		$Coun1=0;
		foreach ($tmpFLAGS as $Cell){
			$tmpAR0=explode($tmpSPLIT0,$Cell);
			$tmpINDEXS[$Coun1]['NAME']=$tmpAR0[0];
			$tmpINDEXS[$Coun1]['ONAME']=$Cell;
			if(stripos($Cell,$tmpSPLIT1,0)!=''){
				$tmpAR0=explode($tmpSPLIT1,$tmpAR0[1]);
				$Coun2=0;
				$Coun3=$tmpAR0[1];
				while($Coun3>$tmpAR0[0]-1){
					$tmpINDEXS[$Coun1]['INDEXS'][$Coun2]=$Coun3;
					$Coun3--;
					$Coun2++;
				}
			}else if(stripos($Cell,$tmpSPLIT2,0)!=''){
				$tmpINDEXS[$Coun1]['INDEXS']=explode($tmpSPLIT2,$tmpAR0[1]);
				array_pop($tmpINDEXS[$Coun1]['INDEXS']);
			}
			$Coun1++;
		}
		//Make urls
		if(count($tmpINDEXS)>0){
			$tmpLINES_TMP_A=array();
			$tmpLINES_TMP_B=array();
			$Coun1=0;
			while (count($tmpINDEXS[0]['INDEXS'])>$Coun1) {
				$tmpAR0=array('ORIGIN_URL'=>$tmpLINE_A,'HTML_URL'=>$tmpLINE_B);
				$tmpAR0['ORIGIN_URL']=str_replace($tmpStartFlag.$tmpINDEXS[0]['ONAME'].$tmpEndFlag,$tmpINDEXS[0]['INDEXS'][$Coun1],$tmpAR0['ORIGIN_URL']);
				$tmpAR0['HTML_URL']=str_replace($tmpStartFlag.$tmpINDEXS[0]['NAME'].$tmpEndFlag,$tmpINDEXS[0]['INDEXS'][$Coun1],$tmpAR0['HTML_URL']);
				$tmpLINES_TMP_A[]=$tmpLINES_TMP_B[]=$tmpAR0;
				$Coun1++;
			}
			$Coun1=1;
			while (count($tmpINDEXS)>$Coun1) {
				$tmpLINES_TMP_A=array();
				$Coun2=0;
				while (count($tmpLINES_TMP_B)>$Coun2) {
					$Coun3=0;
					while (count($tmpINDEXS[$Coun1]['INDEXS'])>$Coun3) {
						$tmpAR0=$tmpLINES_TMP_B[$Coun2];
						$tmpAR0['ORIGIN_URL']=str_replace($tmpStartFlag.$tmpINDEXS[$Coun1]['ONAME'].$tmpEndFlag,$tmpINDEXS[$Coun1]['INDEXS'][$Coun3],$tmpAR0['ORIGIN_URL']);
						$tmpAR0['HTML_URL']=str_replace($tmpStartFlag.$tmpINDEXS[$Coun1]['NAME'].$tmpEndFlag,$tmpINDEXS[$Coun1]['INDEXS'][$Coun3],$tmpAR0['HTML_URL']);
						$tmpLINES_TMP_A[]=$tmpAR0;
						$Coun3++;
					}
					$Coun2++;
				}
				$tmpLINES_TMP_B=$tmpLINES_TMP_A;
				$Coun1++;
			}
			$tmpRULES=$tmpLINES_TMP_B;
		}else {
			$tmpAR0=array('ORIGIN_URL'=>$tmpLINE_A,'HTML_URL'=>$tmpLINE_B);
			$tmpRULES[]=$tmpAR0;
		}
		return $tmpRULES;
	}
	public function getNotes($pSTR){
		$tmpAR0=explode($this->sysSPLIT4.$this->sysSPLIT4,$pSTR);
		$tmpLINES=array();
		foreach ($tmpAR0 as $Cell){
			$tmpAR1=explode($this->sysSPLIT4,$Cell);
			$tmpLINES[]=str_replace('//','',$tmpAR1[0]);
		}
		return $tmpLINES;
	}
	public function getSheme($pID){
		$tmpHead='<?php /* ';
		$tmpEnd=' */ ?>';
		//-- ------ ### ------ --
		//-- #Read file --
		$tmpPATH=$this->sysFileConfig['PathRoot'].$this->sysFileConfig['sysDataCachePath'].'nets/sheme_'.$pID;
		$tmpSTR=$this->readFile($tmpPATH);
		$tmpSTR=str_replace($tmpHead,'',$tmpSTR);
		$tmpSTR=str_replace($tmpEnd,'',$tmpSTR);
		//-- #SQL --
		if ($tmpSTR=='') {
			$tmpSQL=' SELECT B.ssctHtmlDescription
				FROM {Nets.TableHead}netssyssheme AS A
				INNER JOIN {Nets.TableHead}netssysshemecont AS B ON B.ssctIndexID=A.sstID
				WHERE 1=1 AND A.sstID={Nets.ID} ';
			$tmpSTR=str_replace('{Nets.TableHead}',$this->sysTableHead,$tmpSQL);
			$tmpSTR=str_replace('{Nets.ID}',$pID,$tmpSTR);
			$tmpData0=$this->Search($tmpSTR);
			if (count($tmpData0)>0){
				$tmpSTR=$tmpData0[0]['ssctHtmlDescription'];
				//-- #Write file --
				$tmpSTR=$tmpHead.$tmpSTR.$tmpEnd;
				$this->writeFile($tmpPATH,$tmpSTR);
			}
		}

		return $tmpSTR;
	}
	/**
	 * Non html : &Html=0
	 *
	 * @param unknown_type $pURL
	 */
	public function HtmlExist($pURL,$pRULES){
		//-- ------ #Html URL ------ --
		if ($pURL=='') {
			$pURL='#Here';
		}
		$tmpSTR=$pURL;
		//-- #URL --
		foreach ($pRULES as $tmpRULE){
			$tmpAR0=explode($this->sysSPLIT6,$tmpRULE);
			if (count($tmpAR0)==2) {
				$tmpAR1=$this->getRules($tmpAR0[0],$tmpAR0[1]);
				foreach ($tmpAR1 as $Row0){
					if ($Row0['ORIGIN_URL']==$pURL) {
						$tmpSTR=$Row0['HTML_URL'];
					}
				}
			}
		}
		//-- #IsExist --
		if(file_exists($tmpSTR)){
		}else {
			$tmpSTR=$pURL;
		}
		
		return $tmpSTR;
	}
}
?>