Location: PHPKode > scripts > Arab Temp > arab_Temp.php
<?php
/**
###################################################################################################
#######//						&#1604;&#1575; &#1575;&#1604;&#1607; &#1575;&#1604;&#1575; &#1575;&#1604;&#1604;&#1607; &#1605;&#1581;&#1605;&#1583; &#1585;&#1587;&#1608;&#1604; &#1575;&#1604;&#1604;&#1607;
###################################################################################################
#######//---------------------------------------------------------------------------------
#######// 					&#1605;&#1576;&#1585;&#1605;&#1580; &#1606;&#1592;&#1575;&#1605;  &#1575;&#1604;&#1602;&#1575;&#1604;&#1576; &#1575;&#1604;&#1593;&#1585;&#1576;&#1610;  &#1605;&#1581;&#1605;&#1583;&#1607;&#1604;&#1575;&#1604;
#######//		 &#1608;&#1575;&#1604;&#1606;&#1592;&#1575;&#1605; &#1605;&#1580;&#1575;&#1606;&#1610; &#1608;&#1610;&#1581;&#1602; &#1604;&#1603; &#1575;&#1587;&#1578;&#1582;&#1583;&#1575;&#1605;&#1577; &#1608;&#1575; &#1578;&#1593;&#1583;&#1610;&#1604; &#1593;&#1604;&#1610;&#1577; &#1576;&#1583;&#1608;&#1606; &#1581;&#1584;&#1601; &#1575;&#1604;&#1581;&#1602;&#1608;&#1602; &#1575;&#1604;&#1582;&#1575;&#1589;&#1577; &#1576;&#1610; &#1605;&#1606; &#1601;&#1590;&#1604;&#1603;
#######//			&#1575;&#1604;&#1575;&#1610;&#1605;&#1610;&#1604;:	hide@address.com
#######//---------------------------------------------------------------------------------
###################################################################################################
*/
  class arab_Temp{
  	//----------------------------
  	// templte dir
  	protected $Temp_dir		=FALSE;
  	//----------------------------
  	// templte cache dir
  	protected $Cache_dir	=FALSE;
  	//----------------------------
  	// templte vars
  	protected $var_Temp		=array();
  	//----------------------------
  	// templte Cache time
  	protected $Cache_Time	=FALSE;
  	//----------------------------
  	// templte file html
  	protected $FHTML		=FALSE;
  	//----------------------------
  	// $_post array
  	protected $POST			=array();
  	//----------------------------
  	// $_GET array
  	protected $GET			=array();
  	
  	//----------------------------
  	function __construct($config=array()){
		$this->Temp_dir		=$this->Chack_Slach($config['temp']."/");
		$this->Cache_dir	=$this->Chack_Slach(((isset($config['cache']) and $config['cache']!='')?$config['cache']:'arab_TempCache')."/");
		$this->Cache_Time	=$config['time'];
		$this->POST			=&$_POST;
		$this->GET			=&$_GET;
		define('ARABTEMP',true);	
  	}
  	//---------------------------
  	// chack if there any 2 slash
  	function Chack_Slach($value){
  		return preg_replace('/[\/]{2,}/','/',$value);
  	}
  	//---------------------------
  	// Error Massage
  	function __Error($error){
  		die("<h1>Error\t\t:\t\t<font color='red'>".$error.'</font></h1>');
  	}
  	//---------------------------
  	// load templte file
  	function __loadFile($file){
  		if(!@file_exists($this->Temp_dir)){
  			$this->__Error('Templtes dir not found');
  		}elseif(!@file_exists($this->Cache_dir)){
  			@mkdir($this->Cache_dir,0755);
		}elseif(!@file_exists($this->Temp_dir.$file)){
  			$this->__Error('Templtes '.$file.' not found');
		}else{
			$this->FHTML=@file_get_contents($this->Temp_dir.$file);
		}
  	}
  	//-----------------------------------------------------------------------------------------------
  	//-----------------------------------------------------------------------------------------------
  	//-----------------------------------------------------------------------------------------------
  	//-----------------------------------------------------------------------------------------------
  	//-----------------------------------------------------------------------------------------------
  	function _var($vars){
  		
  		if(preg_match('/\-\>/',$vars[1])){
  			if(preg_match('/\[(\w\.\-\>]+)\]/',$vars[1])){
  				$var=call_user_func(array($this,'__callback'),$vars[1]);
  				$var='$this->var_Temp'.$var;
  			}else{
  				$var=call_user_func(array($this,'Obj_var'),$vars[1]);
  			}
  		}else{
  			if($vars[1]==""){
  				return false;
  			}
  			
  			$var=call_user_func_array(array($this,'__callback'),$vars[1]);
			$var='$this->var_Temp'.$var;
  		}
  		return $var;
	}
	function _varss($vars){
		$var=preg_replace_callback('/\$([\w\.\-\>\:\[\]\$]+)/',array($this,'_var'),$vars);
		return $var;
	}
  	function __callback($vars){
  		preg_match('/\[(.+)\]/',$vars,$Rmatchs);
  		$Rma=str_replace(".","@",$Rmatchs[0]);
  		$vars=str_replace($Rmatchs[0],$Rma,$vars);
  		$vars=explode(".",$vars);
  		foreach($vars as $var){
  			if($var !=''){
  				if(is_numeric($var)){
  					$arr[]="[".$var."]";
  				}elseif(preg_match('/([\w]+)\[(.+)\]/',$var,$matchs)){
  					preg_match_all('/\[(.+)\]/U',$var,$Rm);
  					foreach($Rm[1] as $Rram){
  						$Rram=str_replace("@",'.',$Rram);
  						$SubVar.="[\$this->var_Temp".$this->__callback($Rram)."]";
  					}
  					$arr[]="['".$matchs[1]."']".$SubVar;
  					
  				}else{
  					$arr[]="['".$var."']";
  				}
  			}
  		}
  		if(count($arr)>0){
  		return implode("",$arr);
  		}
  	}
  	function Obj_var($vars){
  		$explode=explode("->",$vars);
  		$ObjName=$explode[0];
  		unset($explode[0]);
  		$ObjEnd=end($explode);
  		unset($explode[count($explode)]);
	 	 if(stripos($ObjEnd,":")){
	 	 	$ObjEnd=explode(":",$ObjEnd);
	 	 	$explode[]=$ObjEnd[0];
	 	 	unset($ObjEnd[0]);
	 	 	$ObjEnd=implode(",",$ObjEnd);
	 	 	$exp=call_user_func(array($this,'_varss'),$ObjEnd);
	 	 	return "\$this->var_Temp['".$ObjName."']->".implode('->',$explode).'('.$exp.')';
	 	 }
	 	 $exp=explode(".",$ObjEnd);
	 	 $ObjEnd=$exp[0];
	 	 unset($exp[0]);
	 	 $exp=implode(".",$exp);
	 	 $exp=call_user_func(array($this,'__callback'),$exp);
	 	 $explode[]=$ObjEnd;
  		return "\$this->var_Temp['".$ObjName."']->".implode('->',$explode).$exp;
  	}
  	//----------------------
  	// _IF 
  	function  __IF($vars){
  		$vars[2]=str_replace("||",' OR  ',$vars[2]);
  		if(stripos($vars[2],"|")){
  			$var=call_user_func(array($this,'suport_function'),$vars[2]);
  		}else{
  			$var=call_user_func(array($this,'_varss'),$vars[2]);
  		}
  		$IF=strtoupper($vars[1]);
  		$IF=($IF=='IF')?"IF":"}ELSEIF";
  		return '<?php '.$IF.'('.$var.'){?>';
  	}
  	//----------------------
  	// _IF SHORT
  	function  __IF_SHORT($vars){
  		$IF="{IF $vars[1]}\n";
  		$IF.='{'.trim($vars[2]).'}'."\n";
  		$IF.="{else}\n";
  		$IF.='{'.trim($vars[3]).'}'."\n";
  		$IF.="{/if}";
  		return $IF;
  	}
  	//-------------------------------------------
  	// Loop area
  	function __Foreach($vars){
  		$From=call_user_func(array($this,'_varss'),$vars[1]);
  		$As=call_user_func(array($this,'_varss'),$vars[2]);
  		$Forloop='<?php '."\n";
  		$Forloop.='$_FROM='.$From.';if(!is_array($_FROM) and !is_object($_FROM)){settype($_FROM,"array");}';
  		$Forloop.='if(count($_FROM)){';
  		$Forloop.='foreach($_FROM  as '.$As.'){?>';
  		return $Forloop;
  	}
  	function __Foreach_Each($vars){
  		$From=call_user_func(array($this,'_varss'),$vars[1]);
  		$Forloop='<?php '."\n";
  		$Forloop.='$_FROM='.$From.';if(!is_array($_FROM) and !is_object($_FROM)){settype($_FROM,"array");}';
  		$Forloop.='if(count($_FROM)){';
  		$Forloop.='foreach($_FROM  as '.$From.'){?>';
  		return $Forloop;
  	}
  	function __Foreach_k_v($vars){
  		$From=call_user_func(array($this,'_varss'),$vars[1]);
  		$keys=call_user_func(array($this,'_varss'),$vars[2]);
  		$value=call_user_func(array($this,'_varss'),$vars[3]);
  		$Forloop='<?php '."\n";
  		$Forloop.='$_FROM='.$From.';if(!is_array($_FROM) and !is_object($_FROM)){settype($_FROM,"array");}';
  		$Forloop.='if(count($_FROM)){';
  		$Forloop.='foreach($_FROM  as '.$keys.'=>'.$value.'){?>';
  		return $Forloop;

  	}
  	function __For($vars){
  		$From=call_user_func(array($this,'_varss'),$vars[3]);
  		$ID=call_user_func(array($this,'_varss'),$vars[2]);
  		if(preg_match('/^[0-9]+$/',$vars[3])){
  			$Forloop='<?php '."\n";
			$Forloop.='$Counts_Froms='.$From.';'."\n".'if($Counts_Froms){';
		}elseif(preg_match('/^[\w\.\$\-\>]+$/',$vars[3])){
			$Forloop='<?php '."\n";
			$Forloop.='if(!is_array('.$From.') and !is_object('.$From.')){settype('.$From.',"array");}';
			$Forloop.='$Counts_Froms=count('.$From.');'."\n".'if($Counts_Froms){';
		}
  		$DO=strtoupper($vars[1]);
  		if($DO=='FOR'){
  			$Forloop.='for('.$ID.'=0;'.$ID.'<$Counts_Froms;'.$ID.'++){?>';
		}elseif($DO=='WHILE'){
			$Forloop.=$ID.'=0;'."\n".'while('.$ID.'<$Counts_Froms){?>';
		}
  		return $Forloop;
  	}
  	//-----------------------------------
  	// echo varbers
  	function __echo($vars){
  		preg_match('/\$([\w\.\-\>\:\[\]\$\=\|\+]+)/',$vars[0],$aar);
  		if(stripos($aar[0],"=") or stripos($aar[0],"+")){
  			if(stripos($aar[0],"|")){
  				$exp=explode("=",$aar[0]);
  				$var=call_user_func(array($this,'_varss'),$exp[0]);
  				$vfunc=call_user_func(array($this,'suport_function'),$exp[1]);
  				return '<?php  '.$var.'='.$vfunc.';?>';
  			}
			$var=call_user_func(array($this,'_varss'),$aar[0]);
  			return '<?php  '.$var.';?>';
  		}
   		$var=call_user_func(array($this,'_varss'),$aar[0]);
  		return '<?php echo '.$var.';?>';
  	}
  	//-------------------------------------
  	// function suport
  	function suport_function($vars){
  		if(preg_match('/\|/',$vars)){
  			$exp=explode("|",$vars);
  			$F=$exp[0];
  			unset($exp[0]);
  			$var=implode(",",$exp);
  			$var=call_user_func(array($this,'_varss'),$var);
  			$Funct='  '.$F."(".$var.")";
  		}else{
  			$exp=explode(":",$vars);
  			$F=$exp[0];
  			unset($exp[0]);
  			$var=implode(",",$exp);
  			$var=call_user_func(array($this,'_varss'),$var);
  			$Funct=' echo '.$F."(".$var.")";
  		}
  		return $Funct;
  	}
  	//------------------------------------
  	// function 
  	function _Function($vars){
  		preg_match('/([\w\.\-\>\:\[\]\$\|\'\"\/\=\;]+)/i',$vars[0],$matchs);
  		$var=call_user_func(array($this,'suport_function'),$matchs[1]);
  		return "<?php ".$var."; ?>";
  		
  	}
  	//---------------------------------------
  	// include area
  	function __include($vars){
  		$var=call_user_func(array($this,'_varss'),$vars[1]);
  		if($var == $vars[1]){
  			return "<?php echo \$this->display('".$vars[1]."');?>";
  		}
  		return "<?php echo \$this->display(".$var.");?>";
  	}
  	function __include_DB($vars){
  		$name=call_user_func(array($this,'_varss'),$vars[1]);
  		$body=call_user_func(array($this,'_varss'),$vars[2]);
		return "<?php echo \$this->display(".$name.",".$body.");?>";
  	}
  	function __include_Eval($vars){
  		$body=call_user_func(array($this,'_varss'),$vars[1]);
		return "<?php echo \$this->_Eval_Data(".$body.");?>";
  	}
  	function _Eval_Data($vars){
  		$this->FHTML=$vars;
  		$this->__replace();
  		ob_start();
  		eval('?>'.$vars);
  		$Ev=ob_get_contents();
  		ob_get_clean();
  		return $Ev;
  	}
  	//----------------------------------------------------------------
  	function __Switch($vars){
  		$var=call_user_func(array($this,'_varss'),$vars[1]);
  		return '<?php SWITCH('.$var.'){?>';
  	}
  	function __replace(){
  		$this->FHTML=preg_replace_callback('/\{\((.+)\)\?(.+)\:(.+)\}/iU',array($this,'__IF_SHORT'),$this->FHTML);
  		$this->FHTML=preg_replace_callback('/\{(IF|ELSEIF)(.+)\}/iU',array($this,'__IF'),$this->FHTML);
 	 	$this->FHTML=preg_replace_callback('/\{FOREACH\s+FROM=(.+)\s+AS=(.+)\}/iU',array($this,'__Foreach'),$this->FHTML);
  		$this->FHTML=preg_replace_callback('/\{FOREACH\s+FROM=(.+)\s+KEY=(.+)\s+VALUE=(.+)\}/iU',array($this,'__Foreach_k_v'),$this->FHTML);
  		$this->FHTML=preg_replace_callback('/\{FOREACH(.+)\}/i',array($this,'__Foreach_Each'),$this->FHTML);
  		$this->FHTML=preg_replace_callback('/\{(FOR|WHILE)\s+FORI=(.+)\s+FROM=(.+)\}/iU',array($this,'__For'),$this->FHTML);
  		$this->FHTML=preg_replace_callback('/\{INCLUDE\s+NAME=(.+)\s+BODY=(.+)\}/iU',array($this,'__include_DB'),$this->FHTML);
  		$this->FHTML=preg_replace_callback('/\{INCLUDE\s+NAME=(.+)\}/iU',array($this,'__include'),$this->FHTML);
  		$this->FHTML=preg_replace_callback('/\{INCLUDE\s+BODY=(.+)\}/iU',array($this,'__include_Eval'),$this->FHTML);
  		$this->FHTML=preg_replace_callback('/\{SWITCH\s+(.+)\}/iU',array($this,'__Switch'),$this->FHTML);
  		$this->FHTML=preg_replace('/\{ELSE\}/ix','<?php }ELSE{ ?>',$this->FHTML);
  		$this->FHTML=preg_replace('/\{CASE\}/ix','<?php CASE "$1": ?>',$this->FHTML);
  		$this->FHTML=preg_replace('/\{DEFAULT\}/ix','<?php DEFAULT: ?>',$this->FHTML);
  		$this->FHTML=preg_replace('/(\{\/CASE\}|\{\/DEFAULT\})/ix','<?php BREAK; ?>',$this->FHTML);
  		$this->FHTML=preg_replace('/(\{\/LOOP\})/ix','<?php  } } ?>',$this->FHTML);
  		$this->FHTML=preg_replace('/(\{\/FOREACH\})/i','<?php }unset($_FROM); } ?>',$this->FHTML);
  		$this->FHTML=preg_replace('/(\{\/IF\}|\{\/SWITCH\})/i','<?php  } ?>',$this->FHTML);
  		$this->FHTML=preg_replace_callback('/\{\$(.+)\}/iU',array($this,'__echo'),$this->FHTML);
  		$this->FHTML=preg_replace_callback('/\{([\w]+)([\w\.\-\>\:\[\]\$\'\"\|\/\=\;]+)\}/iU',array($this,'_Function'),$this->FHTML);
		$Char=array(
		' == '	=>"#\s+eq\s+#i",
		' != ' 	=>"#\s+neq\s+#i",
		' <  '	=>"#\s+gt\s+#i",
		' >  ' 	=>"#\s+it\s+#i",
		' <= ' 	=>"#\s+gteq\s+#i",
		' >= ' 	=>"#\s+iteq\s+#i");
		$this->FHTML=preg_replace(array_values($Char),array_keys($Char),$this->FHTML);
  	}
  	//-----------------------------------------------------------------------------------------------
  	//-----------------------------------------------------------------------------------------------
  	//-----------------------------------------------------------------------------------------------
  	//-----------------------------------------------------------------------------------------------
  	//-----------------------------------------------------------------------------------------------
  	//----------------------------------
  	//assign
  	function assign($key,$value){
  		$this->var_Temp[$key]=$value;
  		unset($value);
  	}
  	//-------------------------------------------
  	// clear array vars
  	function clear_assign($vrname=false,$expl=array()){
  		if($vrname===FALSE){
  			foreach($this->var_Temp as $key=>$value){
  				if(!in_array($vrname,$expl)){
  				unset($this->var_Temp[$key]);
  				}
  			}
  			return true;
  		}
  			unset($this->var_Temp[$vrname]);
  			return true;
  	}
  	//---------------------------------------------
  	// get var from array vars
  	function get_var($varname){
  		if(isset($this->var_Temp[$varname])){
  			return $this->var_Temp[$varname];
  		}
  	}
  	//---------------------------
  	// get var from POST
  	function POST($varname,$TRUE=FALSE){
  		$varname=$this->POST[$varname];
  		if($TRUE===TRUE){
  			$varname=trim($varname);
  			$varname=addslashes($varname);
  			$varname=strip_tags($varname);
  			return $varname;
  		}
  		switch(gettype($varname)){
  			default:
  				$varname=trim($varname);
  				$varname=addslashes($varname);
			break;
			case 'string':
				$varname=trim($varname);
  				$varname=addslashes($varname);
			break;
			case 'boolean':
			case 'integer':
			case 'double':
				$varname=(int)$varname;
			break;
  		}
  		return $varname;
  	}
  	//---------------------------
  	// get var from GET
  	function GET($varname,$TRUE=FALSE){
  		$varname=$this->GET[$varname];
  		if($TRUE===TRUE){
  			$varname=trim($varname);
  			$varname=addslashes($varname);
  			$varname=strip_tags($varname);
  			return $varname;
  		}
  		$varname=(int)$varname;
  		return $varname;
  	}
  	//------------------------
  	// Create Cache file
  	function Make_Cache($file){
  		$ArabTemp="<?php if(!defined('ARABTEMP')){die('&#1604;&#1575; &#1610;&#1581;&#1602; &#1604;&#1603; &#1575;&#1604;&#1583;&#1582;&#1608;&#1604; &#1575;&#1604;&#1610; &#1607;&#1584;&#1575; &#1575;&#1604;&#1589;&#1601;&#1581;&#1577; &#1605;&#1606; &#1601;&#1590;&#1604;&#1603;  &#1575;&#1585;&#1580;&#1593; &#1575;&#1604;&#1610; &#1575;&#1604;&#1589;&#1601;&#1581;&#1577; &#1575;&#1604;&#1585;&#1574;&#1610;&#1587;&#1610;&#1577;');}\n";	
$ArabTemp.="###########################################################################################
#######//						&#1604;&#1575; &#1575;&#1604;&#1607; &#1575;&#1604;&#1575; &#1575;&#1604;&#1604;&#1607; &#1605;&#1581;&#1605;&#1583; &#1585;&#1587;&#1608;&#1604; &#1575;&#1604;&#1604;&#1607;
###########################################################################################
#######//---------------------------------------------------------------------------------
#######// 					&#1605;&#1576;&#1585;&#1605;&#1580; &#1606;&#1592;&#1575;&#1605;  &#1575;&#1604;&#1602;&#1575;&#1604;&#1576; &#1575;&#1604;&#1593;&#1585;&#1576;&#1610;  &#1605;&#1581;&#1605;&#1583;&#1607;&#1604;&#1575;&#1604;
#######//		 &#1608;&#1575;&#1604;&#1606;&#1592;&#1575;&#1605; &#1605;&#1580;&#1575;&#1606;&#1610; &#1608;&#1610;&#1581;&#1602; &#1604;&#1603; &#1575;&#1587;&#1578;&#1582;&#1583;&#1575;&#1605;&#1577; &#1608;&#1575; &#1578;&#1593;&#1583;&#1610;&#1604; &#1593;&#1604;&#1610;&#1577; &#1576;&#1583;&#1608;&#1606; &#1581;&#1584;&#1601; &#1575;&#1604;&#1581;&#1602;&#1608;&#1602; &#1575;&#1604;&#1582;&#1575;&#1589;&#1577; &#1576;&#1610; &#1605;&#1606; &#1601;&#1590;&#1604;&#1603;
#######//			&#1575;&#1604;&#1575;&#1610;&#1605;&#1610;&#1604; 
#######//				hide@address.com
#######//---------------------------------------------------------------------------------
#######//			&#1575;&#1587;&#1605; &#1575;&#1604;&#1605;&#1604;&#1601;:$file
############################################################################################\n?>\n";
  		$this->FHTML=$ArabTemp.$this->FHTML;
  		$cache=$this->Cache_dir.md5(sha1("tpl_".str_replace("/",".",$file))).".php";
  		$Fp=@fopen($cache,"w");
  		@fwrite($Fp,$this->FHTML);
  		@fclose($Fp);
  		return $cache;
  	}
  	function display($file,$HTML=FALSE){
  		if($HTML===FALSE){
  			$this->__loadFile($file);
  		}else{
  			$this->FHTML=$HTML;
  		}
  		$this->__replace();
  		$cache=$this->Make_Cache($file);
  		@ob_start();
  		include($cache);
  		$page=@ob_get_contents();
  		@ob_get_clean();
  		return $page;
  	}
  	
  }
  
?>
Return current item: Arab Temp