Location: PHPKode > scripts > STC > stc/template.php
<?php
/**
 * Template&#31867;:&#19968;&#20010;&#31616;&#21333;&#30340;&#27169;&#29256;&#22788;&#29702;&#31867;,&#25903;&#25345;&#27169;&#29256;&#21464;&#37327;&#23450;&#20041;,&#26465;&#20214;&#35821;&#21477;,&#24490;&#29615;&#35821;&#21477;,&#27169;&#29256;&#23548;&#20837;&#35821;&#21477;
 * &#29256;&#26412;:0.4 Alpha
 * &#20316;&#32773;:lazy
 * &#37038;&#20214;:o0lazy0o at gmail dot com
 * &#29256;&#26435;:&#26080;,&#21487;&#20219;&#24847;&#20351;&#29992;;&#22914;&#26524;&#21487;&#20197;&#35831;&#20445;&#30041;&#27492;&#20449;&#24687;,&#22914;&#24744;&#23545;&#23427;&#20570;&#20986;&#20462;&#25913;&#21450;&#28155;&#21152;&#21151;&#33021;&#35831;&#21457;&#19968;&#20010;&#25335;&#36125;&#32473;&#25105;,&#35874;&#35874;!
 * Log:
 *	0.4=>
 * 		&#21152;&#20837;:&#30452;&#25509;&#25903;&#25345;define&#23450;&#20041;&#30340;&#21464;&#37327;,&#25903;&#25345;&#20351;&#29992;&#27169;&#29256;&#27880;&#20876;&#36807;&#30340;&#31867;&#36319;&#20989;&#25968;(&#19981;&#25903;&#25345;&#22312;if&#21644;section&#35821;&#21477;&#20869;&#20351;&#29992;&#20989;&#25968;,&#19979;&#19968;&#29256;&#26412;&#32771;&#34385;&#25903;&#25345;)
 * 		&#20462;&#22797;:&#27169;&#26495;&#20462;&#25913;&#21518;&#31532;&#19968;&#27425;&#26080;&#27861;&#26174;&#31034;&#30340;bug
 * 			&#20462;&#22797;Import&#21253;&#21547;&#27169;&#29256;&#25991;&#20214;&#21518;&#26080;&#27861;&#20351;&#29992;&#30340;bug
 *	0.3 &#21152;&#20837; &#24490;&#29615;&#25903;&#25345; &#21644; &#21253;&#21547;&#27169;&#29256;&#25903;&#25345;,&#21435;&#38500;&#27169;&#29256;&#20869;&#30340;<? <?php ?>&#26631;&#35782;(&#23433;&#20840;&#38382;&#39064;)
 * 	0.2 &#21152;&#20837; &#22810;&#25968;&#32452;&#25903;&#25345; &#21644; &#33258;&#23450;&#20041;&#35821;&#21477;&#36319;&#21464;&#37327;&#26631;&#35782;&#31526;
 *	0.1 &#26368;&#21021;&#29256;&#26412;,&#21482;&#25903;&#25345; &#27169;&#29256;&#21464;&#37327;(&#38750;&#25968;&#32452;)&#36319;&#26465;&#20214;&#35821;&#21477;
 * Welcome to http://www.52radio.net/
 * &#20363;&#23376;:&#35831;&#26597;&#30475;example.php,example.tpl,Import.tpl
 * &#27880;&#24847;:&#27492;&#29256;&#26412;&#21482;&#20570;&#20102;&#31616;&#21333;&#27979;&#35797;.
 */
Class template extends application{

	Var $VL='{';
	Var $VR='}';
	Var $SL='<!--';
	Var $SR='-->';
	Var $Data=array();
	Var $Func=array('date'=>true,'long2ip'=>true,'strftime'=>true);
	Var $Class=array();
	Var $TplPath='';
	Var $CachePath='.';
	Var $Filename='';
	Var $Savename='';
	Var $Content='';

	Function template($Filename=null){
		$this->Filename=isset($Filename)?$Filename:null;
		defined('TPL')?$this->TplPath=TPL:'';
		defined('CACHE')?$this->CachePath=CACHE:'';
		defined('STYLE')?$this->TplPath=STYLE:'';
	}

	Function assign($Name,$Value){
		return $this->Data[$Name]=$Value;
	}

	Function reg_func($Name){
		return $this->Func[$Name]=true;
	}

	Function reg_class($Name){
		return class_exists($Name)?$this->Class[$Name]=true:null;
	}

	Function parse_var(){
		$RegExp[]='/<{\(((?:(?:[a-zA-Z_](?:.[a-zA-Z0-9_])*)+)|(?:[0-9]{1,9}))\)}>/e';
		$Replace[]="\$this->process_var('\$1',false)";

		$RegExp[]="/{$this->VL}((?:[a-zA-Z_](?:.[a-zA-Z0-9_])*)+){$this->VR}/e";
		$Replace[]="\$this->process_var('\$1',true)";
		return $this->Content=preg_replace($RegExp,$Replace,$this->Content);
	}

	Function parse_if(){
		$RegExp[]="/{$this->SL} if ((?:[a-zA-Z_](?:.[a-zA-Z0-9_])*)+) (==|<=|>=|<|>) ((?:[a-zA-Z_](?:.[a-zA-Z0-9_])*)+|'[a-zA-Z0-9]+'|[0-9]+) {$this->SR}/";
		$Replace[]="<?php if( <{(\$1)}> \$2 <{(\$3)}> ){ ?>\n";

		$RegExp[]="/{$this->SL} elseif ((?:[a-zA-Z_](?:.[a-zA-Z0-9_])*)+) (==|<=|>=|<|>) ((?:[a-zA-Z_](?:.[a-zA-Z0-9_])*)+|'[a-zA-Z0-9]+'|[0-9]+) {$this->SR}/";
		$Replace[]="<?php }elseif( <{(\$1)}> \$2 <{(\$3)}> ){ ?>\n";

		return $this->Content=preg_replace($RegExp,$Replace,$this->Content);
	}

	Function parse_for(){
		$RegExp="/{$this->SL} section ([a-zA-Z_][a-zA-Z0-9_]+) from ((?:[a-zA-Z_](?:.[a-zA-Z0-9_])*)+) {$this->SR}/";
		$Replace="<?php \n";
		$Replace.="foreach( <{(\$2)}> as \$__KEY => \$\${1} ){ \n";
		$Replace.="\$this->assign('__KEY',\$__KEY); \n";
		$Replace.="\$this->assign('\$1',\$\$1); \n";
		$Replace.="?> \n";
		return $this->Content=preg_replace($RegExp,$Replace,$this->Content);
	}

	Function parse_end(){
		$Search[]="{$this->SL} else {$this->SR}";
		$Search[]="{$this->SL} endif {$this->SR}";
		$Search[]="{$this->SL} endsection {$this->SR}";
		$Replace[]="<?php }else{ ?>";
		$Replace[]="<?php } ?>";
		$Replace[]="<?php } ?>";
		return $this->Content=str_replace($Search,$Replace,$this->Content);
	}

	Function parse_func(){
		$RegExp="/{$this->SL} ([a-zA-Z0-9_]+)(\(\)|\([^\)]+\)) {$this->SR}/e";
		$Replace="\$this->process_func('\$1','\$2')";
		return $this->Content=preg_replace($RegExp,$Replace,$this->Content);
	}

	Function parse_class(){
		$RegExp="/{$this->SL} ([a-zA-Z0-9_]+)::([a-zA-Z0-9_]+)(\(\)|\([^\)]+\)) {$this->SR}/e";
		$Replace="\$this->process_class('\$1','\$2','\$3')";
		return $this->Content=preg_replace($RegExp,$Replace,$this->Content);
	}

	Function parse_import($Filename){
		$Backup['Filename']=$this->Filename;
		$Backup['Savename']=$this->Savename;
		$Backup['Content']=$this->Content;
		$Return = $this->display($Filename,true);
		unset($this->Filename,$this->Savename,$this->Content);
		$this->Filename=$Backup['Filename'];
		$this->Savename=$Backup['Savename'];
		return $Return;
	}

	Function process_var($Value,$Echo=false){
		if(is_numeric($Value) or $Value=='TRUE' or $Value=='FALSE'){
			return $Value;
		}
		if(defined($Value)){
			return $Echo?'<?php echo '.$Value.'?>':$Value;
		}
		$VarArray=explode('.',$Value);
		$Count=count($VarArray);
		if($Count>1){
			for($Tmp=1;$Tmp<$Count;$Tmp++){
				is_numeric($VarArray[$Tmp])?$VarStr.="[{$VarArray[$Tmp]}]":$VarStr.="[\"{$VarArray[$Tmp]}\"]";
			}
			$PHPVar=strtoupper($VarArray[0]);
			if($PHPVar=='_GET' or $PHPVar=='_POST' or $PHPVar=='_COOKIE' or $PHPVar=='_SESSION'){
				return $Echo?'<?php echo $'.$PHPVar.$VarStr.'?>':'$'.$PHPVar.$VarStr;
			}else{
				return $Echo?'<?php echo $this->Data["'.$VarArray[0].'"]'.$VarStr.' ?>':'$this->Data["'.$VarArray[0].'"]'.$VarStr;
			}
		}
		if(array_key_exists($Value,$this->Data)){
			return $Echo?'<?php echo $this->Data["'.$Value.'"] ?>':'$this->Data["'.$Value.'"]';
		}
		return $Echo==false?'"'.str_replace('"','\"',$Value).'"':'<?php echo $this->Data["'.$Value.'"] ?>';
	}

	Function process_func($FuncName,$FuncArgv){
		if(!array_key_exists($FuncName,$this->Func)){
			return 'Error:The Function=>'.$FuncName.'Was Not Been Register';
		}
		$Length=&strlen($FuncArgv);
		$FuncArgv=substr($FuncArgv,1,$Length-2);
		$FuncArgv=explode(',',$FuncArgv);
		$Count=count($FuncArgv);
		if($Count==1){
			$FuncArgv=&$FuncArgv[0];
			return empty($FuncArgv)?"<?php echo $FuncName(); ?> \n":"<?php echo $FuncName(<{(".trim($FuncArgv,"'\"").")}>); ?> \n";
		}else{
			$Argv=$Tmp=null;
			$Count--;
			foreach($FuncArgv as $TmpArgv){
				$Argv .= $Tmp==$Count?'<{('.trim($TmpArgv,"'\"").')}>':'<{('.trim($TmpArgv,"'\"").')}>,';
				$Tmp++;
			}
			return "<?php echo $FuncName(".$Argv."); ?> \n";
		}
	}

	Function process_class($ClassName,$FuncName,$FuncArgv){
		if(class_exists($ClassName) and in_array($ClassName,$this->Class)){
			$Return="<?php \$$ClassName=new $ClassName(); ?> \n";
			$Return.="<?php if(method_exists(\$$ClassName,'$FuncName')){ ?> \n";
			$Length=&strlen($FuncArgv);
			$FuncArgv=substr($FuncArgv,1,$Length-2);
			$FuncArgv=explode(',',$FuncArgv);
			$Count=count($FuncArgv);
			if($Count==1){
				$FuncArgv=&$FuncArgv[0];
				$Return .= empty($FuncArgv)?"<?php echo \${$ClassName}->{$FuncName}(); ?> \n":"<?php echo \${$ClassName}->{$FuncName}(<{(".trim($FuncArgv,"'\"").")}>); ?> \n";
			}else{
				$Argv=$Tmp=null;
				$Count--;
				foreach($FuncArgv as $TmpArgv){
					$Argv .= $Tmp==$Count?'<{('.trim($TmpArgv,"'\"").')}>':'<{('.trim($TmpArgv,"'\"").')}>,';
					$Tmp++;
				}
				$Return .= "<?php echo \${$ClassName}->{$FuncName}(".$Argv."); ?> \n";
			}
			$Return .= "<?php }else{ ?> \n";
			$Return .= "<?php echo 'The Function=>{$FuncName} Not Exists!' ?>\n";
			$Return .= "<?php }?> \n";
			return $Return;
		}else{
			return "The Class=>$ClassName Was Not Been Register";
		}
	}

	Function compile(){
		$FuncList[]='parse_if';
		$FuncList[]='parse_for';
		$FuncList[]='parse_end';
		$FuncList[]='parse_func';
		$FuncList[]='parse_class';
		$FuncList[]='parse_var';

		foreach($FuncList as $Call){
			if($this->$Call()==false){
				$this->error($Call.'=>Compile Fail.');
			}
		}
		if(is_writeable($this->CachePath)){
			$Handle=fopen($this->CachePath.'/'.$this->Savename,'wb+');
			flock($Handle,LOCK_EX+LOCK_NB);
			fwrite($Handle,$this->Content);
			fclose($Handle);
			return $this->CachePath.$this->Savename;
		}else{
			return $this->error('Cound not write to cache'.$this->CachePath);
		}
	}

	Function display($Filename,$Import=false){
		empty($Filename)?empty($this->Filename)?$this->error('Not Tpl File Define'):$this->Filename:$this->Filename=$Filename;
		$HolisticTPL=$this->TplPath.$this->Filename;
		if(!file_exists($HolisticTPL)){
			$this->error($Filename.' => No Such Tpl File Exists...'.$HolisticTPL);
		}
		$this->Savename=md5($HolisticTPL).'.tpl.php';
		$HolisticCache=$this->CachePath.'/'.$this->Savename;
		$this->Content=$this->remove_php_tag(file_get_contents($HolisticTPL));
		if(preg_match("/{$this->SL} import '((?:[a-zA-Z_](?:.[a-zA-Z0-9_])*)+)' {$this->SR}/",$this->Content)){
			$RegExp="/{$this->SL} import '((?:[a-zA-Z_](?:.[a-zA-Z0-9_])*)+)' {$this->SR}/e";
			$Replace="'<?php require_once(\"'.\$this->parse_import('\${1}').'\") ?>'";
			$Tmp=preg_replace($RegExp,$Replace,$this->Content);
			$this->Content=$Tmp;
		}
		if(file_exists($HolisticCache)){
			$TplFileTime=$this->m_time($HolisticTPL);
			$CacheFileTime=$this->m_time($HolisticCache);
			if($TplFileTime < $CacheFileTime){
				return $Import?$this->Savename:require($HolisticCache);
			}
		}
		$Return = $this->compile();
		return $Import==true?($Return):(file_exists($Return)?require($Return):$this->Content);
	}

	Function result($Filename){
		ob_start();
		$this->display($Filename);
		$Contents=ob_get_contents();
		ob_end_clean();
		return $Contents;
	}

	Function m_time($File){
		return file_exists($File)?filemtime($File):false;
	}

	Function remove_php_tag($Strings){
		$Search[]='<?';
		$Search[]='?>';
		$Replace[]='&lt;?';
		$Replace[]='?&gt;';
		return str_replace($Search,$Replace,$Strings);
	}
	/*
	Function error($ErrStr){
		exit(
		"<html><head><title>Error & Stop</title></head><body>".
		"<div align='center'>".
		"<font color='red' size='6'>Template Error:</font> {$ErrStr}".
		"<br /><br />Exec Time: ".$this->exectime()."ms".
		"</div></body></html>"
		);
	}
	*/
}
?>
Return current item: STC