Location: PHPKode > scripts > Perfect Templates > perfect-templates/class.ptemplates.php
<?php

// +----------------------------------------------------------------------+
// | Perfect Scripts                                 class.ptemplates.php |
// | Brazilian Organization                                               |
// +----------------------------------------------------------------------+
// | Viva ao Linux!                                                       |
// | Porque nós amamos a liberdade!                                       |
// +----------------------------------------------------------------------+
// | Class Perfect Templates v0.2                                         |
// | Created By Eduardo Henrique Molina da Cruz                           |
// | <hide@address.com> UIN: 84314269                            |
// +----------------------------------------------------------------------+
// | http://www.perfectscripts.cjb.net                                    |
// +----------------------------------------------------------------------+
// | Under GPL License                                                    |
// +----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify |
// |   it under the terms of the GNU General Public License as published  |
// |   by the Free Software Foundation; either version 2 of the License,  |
// |   or (at your option) any later version.                             |
// |                                                                      |
// |   This program is distributed in the hope that it will be useful,    |
// |   but WITHOUT ANY WARRANTY; without even the implied warranty of     |
// |   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      |
// |   GNU General Public License for more details.                       |
// |                                                                      |
// |   You should have received a copy of the GNU General Public License  |
// |   along with this program; if not, write to the Free Software        |
// |   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA           |
// |   02111-1307  USA                                                    |
// +----------------------------------------------------------------------+

class p_templates {

 // Restore the primary template file
 var $file;
 
 // Array with the content of the template files
 var $templates = array();
 
 // Array with the global variables
 var $vars = array();

 // Array with the variables of the loops
 var $loops = array();

 // Array with the includes
 var $includes = array();

 // Array with the ifs
 var $ifs = array();

 //////////////////////////////////////////////////////////////////
 
 /*
 	Constructor
 */
 
 function p_templates($file)
 {
	$this->load($file);
	$this->file = $file;
 }


 /*
 	Error function
 */
 
 function error($error)
 {
	die($error);
 }


 /*
 	Load the template into memory
 */

 function load($file)
 {
 
	if (!file_exists($file)) $this->error("The file ".$file." does not exists.");
 
	if (!$this->templates[$file]) {
 
 	// Open the file
	
	$fp = fopen($file,"r") or $this->error("Could not open template file ".$file);
	$content = fread($fp,filesize($file));
	fclose($fp);

	$this->templates[$file] = $content;

	// Detects the variables

	$vars = preg_replace("/.*\{(var:[A-Za-z0-9\137]+)\}.*/Usi","\\1|",$content);
	$vars_a = explode("|",$vars);
	$size = sizeof($vars_a);

	for ($i=0;$i<$size;$i++) {

		if (preg_match("/^var:[A-Za-z0-9\137]+$/si",$vars_a[$i])) {
	
		$name = substr($vars_a[$i],4,strlen($vars_a[$i]));
		if (!$this->vars[$name]) $this->vars[$name] = null;
		}
	}

	// Detects the includes

	$incs = preg_replace("/.*\{(include:[A-Za-z0-9\137]+)\}.*/Usi","\\1|",$content);
	$incs_a = explode("|",$incs);
	$size = sizeof($incs_a);

	for ($i=0;$i<$size;$i++) {

		if (preg_match("/^include:[A-Za-z0-9\137]+$/si",$incs_a[$i])) {
	
		$name = substr($incs_a[$i],8,strlen($incs_a[$i]));
		if (!$this->includes[$name]) $this->includes[$name] = null;
		}
	}

	// Detects the loops

	$loops = preg_replace("/.*\{(loop:[A-Za-z0-9\137]+)\}.*/Usi","\\1|",$content);
	$loops_a = explode("|",$loops);
	$size = sizeof($loops_a);

	for ($i=0;$i<$size;$i++) {

		$loops_a[$i] = trim($loops_a[$i]);

		if (preg_match("/^loop:[A-Za-z0-9\137]+$/si",$loops_a[$i])) {
	
		$name = substr($loops_a[$i],5,strlen($loops_a[$i]));
		
		if (!$this->loops[$name]) {
		
		$this->loops[$name] = array();
		$this->loops[$name]['n'] = (int)0;
		$this->loops[$name]['hand'] = (int)0;
		$this->loops[$name]['subhand'] = (int)0;
		$this->loops[$name]['values'] = array();
		$this->loops[$name]['vars'] = null;
		$names = array();

		// Detects the variables of the actual loop

		$temp_1 = preg_replace("/.*\{loop:".$name."\}(.*)\{\/loop:".$loop."\}.*/si","\\1",$content);
		$lvars = preg_replace("/.*\{(var:".$name.":[A-Za-z0-9\137]+)\}.*/Usi","\\1|",$temp_1);
		
		$lvars_a = explode("|",$lvars);
		$size_2 = sizeof($lvars_a);
		$names = array();

		for ($j=0;$j<$size_2;$j++) {
	
			$lvars_a[$j] = trim($lvars_a[$j]);

			if (preg_match("/^var:".$name.":[A-Za-z0-9\137]+$/si",$lvars_a[$j])) {
			$temp_4 = strlen($name)+5;
			$name_2 = substr($lvars_a[$j],$temp_4,strlen($lvars_a[$j]));
			$names[] = $name_2;
			$this->loops[$name]['values'][$name_2] = array();
			}
		}
		
		$names = array_unique($names);
		$this->loops[$name]['vars'] = implode("|",$names);
		unset($names);

		}
	}
	}

	// Detects the ifs

	$ifs = preg_replace("/.*\{(if:[A-Za-z0-9\137]+)[\040]+.+\}.*/Usi","\\1|",$content);
	$ifs_a = explode("|",$ifs);
	$size = sizeof($ifs_a);

	for ($i=0;$i<$size;$i++) {

		$ifs_a[$i] = trim($ifs_a[$i]);

		if (preg_match("/^if:[A-Za-z0-9\137]+$/si",$ifs_a[$i])) {

		$name = substr($ifs_a[$i],3,strlen($ifs_a[$i]));
		$this->ifs[$name] = array();

		// Detects the first clause
	
		$first_a = preg_replace("/.*\{if:".$name."[\040]([\040]*[A-Za-z0-9\137\072]+[\040]*(==|!=)[\040]*[\042]?.*[\042]?[\040]*\)?[\040]*([\040]*(AND|OR)[\040]*)?[\040]*)\}.*\{\/if:".$name."\}.*/Usi","\\1<><>",$content);
		$temp = explode("<><>",$first_a);
		$size_2 = sizeof($temp);
		
		for ($j=0;$j<$size_2;$j++) {
			$temp[$j] = trim($temp[$j]);
			if (preg_match("/^([\040]*[A-Za-z0-9\137\072]+[\040]*(==|!=)[\040]*[\042]?.*[\042]?([\040]*(AND|OR)[\040]*)?[\040]*)*$/si",$temp[$j])) {
				$first = $temp[$j];
			}
		}

		$this->ifs[$name]['first'] = $first;

		$this->ifs[$name]['elseifs'] = array();

		// Detects the elseifs
	
		$elseifs = preg_replace("/.*\{elseif:".$name."[\040]([\040]*[A-Za-z0-9\137\072]+[\040]*(==|!=)[\040]*[\042]?.*[\042]?([\040]*(AND|OR)[\040]*)?[\040]*)\}.*\{\/elseif:".$name."\}.*/Usi","\\1<><>",$content);
		
		$elseifs_a =  explode("<><>",$elseifs);
		$size_2 = sizeof($elseifs_a);
	
		for ($j=0;$j<$size_2;$j++) {

			$elseifs_a[$j] = trim($elseifs_a[$j]);

			if (preg_match("/^([\040]*[A-Za-z0-9\137\072]+[\040]*(==|!=)[\040]*[\042]?.*[\042]?([\040]*(AND|OR)[\040]*)?[\040]*)*$/si",$elseifs_a[$j])) {

			$this->ifs[$name]['elseifs'][] = $elseifs_a[$j];

			}
		}

		// Detects if there is else

		if (preg_match("/\{else:".$name."\}/si",$content)) {
			$this->ifs[$name]['else'] = 1;
		}
		else {
			$this->ifs[$name]['else'] = 0;
		}
		}
	}

	}
	return 1;
 }


 /*
	Returns the content of the loop
 */

 function make_content($html,$loop)
 {
 	$find1 = "{loop:".$loop."}";
	$find2 = "{/loop:".$loop."}";
	$init = strpos($html,$find1) + strlen($find1);
	$html = substr($html,$init,strlen($html));
	$final = strpos($html,$find2);
	$html = substr($html,0,$final);
	return $html;
 }


 /*
 	Substitute the loop with his content
 */

 function make_content_replace($html,$loop,$temp)
 {
 	$find1 = "{loop:".$loop."}";
	$find2 = "{/loop:".$loop."}";
	$init = strpos($html,$find1);
	$final = strpos($html,$find2);
	$length = $final - $init + strlen($find2);
	$html = substr_replace($html,$temp,$init,$length);
	return $html;
 }


 /*
 	Substitute the loop var with its content
 */

 function make_lvars_replace($content,$loop,$var,$value)
 {
 	while (preg_match("/\{var:".$loop.":".$var."\}/si",$content)) {
 	$find = "{var:".$loop.":".$var."}";
	$init = strpos($content,$find);
	$length = strlen($find);
	$content = substr_replace($content,$value,$init,$length);
	}
	return $content;
 }


 /*
	Proccess the ifs content
 */

 function make_ifs_content($content,$name,$ret)
 { 
	if ($ret == "if") {
		$find = "{if:".$name." ";
		$pos = strpos($content,$find);
		$content_begin = substr($content,0,$pos);
		$tmp = substr($content,$pos);
		$init = $pos + 1 + strpos($tmp,"}");
		
		$find = "{/if:".$name."}";
		$final = strpos($content,$find);
		$length = $final - $init;
		$content_medium = substr($content,$init,$length);
		
		$find = "{/if:".$name."}";
		$pos = strpos($content,$find) + strlen($find);
		$content_final = substr($content,$pos);
		
		$content = $content_begin . $content_medium . $content_final;
		
		$content = $this->make_rm_elseifs($content,$name);
		$content = $this->make_rm_else($content,$name);
	}
	
	elseif (substr($ret,0,6) == "elseif") {
		$n = substr($ret,7);
		$temp = explode("{/elseif:".$name."}",$content);
		for ($i=0;$i<sizeof($temp);$i++) {
			if ($n == $i) {
				$find = "{elseif:".$name." ";
				$pos = strpos($temp[$i],$find);
				$tmp = substr($temp[$i],$pos);
				$pos = strpos($tmp,"}") + 1 + $pos;
				$temp_2 = substr($temp[$i],$pos);
			}
		}
		
		$find1 = "{if:".$name." ";
		$find2 = "{/if:".$name."}";
		$init = strpos($content,$find1);
		$final = strpos($content,$find2);
		$length = $final - $init + strlen($find2);
		$content = substr_replace($content,$temp_2,$init,$length);
		
		$content = $this->make_rm_elseifs($content,$name);
		$content = $this->make_rm_else($content,$name);
	}
	
	elseif ($ret == "else") {
		$content = preg_replace("/^(.*)\{else:".$name."\}(.*)\{\/else:".$name."\}(.*)$/si","\\1\\2\\3",$content);
		$content = $this->make_rm_if($content,$name);
		$content = $this->make_rm_elseifs($content,$name);
	}
	
	else {
		$content = $this->make_rm_if($content,$name);
		$content = $this->make_rm_elseifs($content,$name);
		$content = $this->make_rm_else($content,$name);
	}
	return $content;
 }


 /*
	Dependencies of make_ifs_content()
 */

 function make_rm_if($content,$name)
 {
	if (preg_match("/\{if:".$name."[\040].*\}/si",$content))
	{
	$find1 = "{if:".$name." ";
	$find2 = "{/if:".$name."}";
	$init = strpos($content,$find1);
	$final = strpos($content,$find2);
	$length = $final - $init + strlen($find2);
	$content = substr_replace($content,null,$init,$length);
	}
	return $content;
 }

 function make_rm_elseifs($content,$name)
 {
	while (preg_match("/\{elseif:".$name."[\040].*\}/si",$content))
	{
  	$find1 = "{elseif:".$name." ";
	$find2 = "{/elseif:".$name."}";
	$init = strpos($content,$find1);
	$final = strpos($content,$find2);
	$length = $final - $init + strlen($find2);
	$content = substr_replace($content,null,$init,$length);
	}
	return $content;
 }

 function make_rm_else($content,$name)
 {
	if (preg_match("/\{else:".$name."\}/si",$content))
	{
	$find1 = "{else:".$name."}";
	$find2 = "{/else:".$name."}";
	$init = strpos($content,$find1);
	$final = strpos($content,$find2);
	$length = $final - $init + strlen($find2);
	$content = substr_replace($content,null,$init,$length);
	}
	return $content;
 }


 /*
	Put the hand of the loop in the next line
 */

 function next_loop($loop)
 {
	if ($this->loops[$loop] !== false) {
		$this->loops[$loop]['n']++;
	}
	return 1;
 }

 /*
	Sets a global variable
 */

 function set_var($var,$value)
 {
	$this->vars[$var] = $value;
	return 1;
 }


 /*
	Sets a loop variable
 */

 function set_lvar($loop,$var,$value)
 {
	if (!is_array($this->loops[$loop]['values'][$var]) and $this->loops[$loop] !== false) {
		if (empty($this->loops[$loop]['vars'])) {
			$this->loops[$loop]['vars'] = $var;
		}
		else {
			$this->loops[$loop]['vars'] .= "|".$var;
		}
		$this->loops[$loop]['values'][$var][0][0] = $value;
	}

	else {
		$n = $this->loops[$loop]['n'];
		$this->loops[$loop]['values'][$var][$n][] = $value;
	}

	return 1;
 }
 

 /*
	Sets the file to be included
 */

 function set_include($name,$include)
 {
 	if ($this->includes[$name] !== false) {
	$this->load($include);
	$this->includes[$name] = $include;
	}
	return 1;
 }
 

 /*
	Substitute the includes
 */

 function parse_includes($html)
 {
	$include_keys = @array_keys($this->includes);
	if ($include_keys) {
	foreach ($include_keys as $key) {
		if (preg_match("/\{include:".$key."\}/si",$html)) {
		$temp = $this->templates[$this->includes[$key]];
		$html = str_replace("{include:".$key."}",$temp,$html);
		}
	}
	}
	return $html;
 }


 /*
	Substitute the variables
 */

 function parse_vars($html)
 {
	$vars_keys = @array_keys($this->vars);
	if ($vars_keys) {
	foreach ($vars_keys as $key) {
		$html = str_replace("{var:".$key."}",$this->vars[$key],$html);
	}
	}
	return $html;
 }


 /*
	Proccess the ifs, elseifs and elses
 */

 function parse_ifs($content,$loop=0)
 {

	$ifs_keys = @array_keys($this->ifs);
	$loops_keys = @array_keys($this->loops);
	$vars_keys = @array_keys($this->vars);
	
	if ($ifs_keys) {
	foreach ($ifs_keys as $key) {

		if ($loop != 0 && eregi("\{loop:",$content))
		{
			$test_init = strpos($content,"{loop:");
			$test_final = strpos($content,"{/loop:");
			
			if (eregi("\{if:".$key,substr($content,$test_init,$test_final)))
			{
				//$next = true;
				continue;
			}
		}

		$if = $this->ifs[$key]['first'];
		$elseifs = $this->ifs[$key]['elseifs'];
	
		while (preg_match("/\{if:".$key.".*\}/si",$content)) {
		
		// Prepare the loop vars
		
		if ($loops_keys) {
		foreach ($loops_keys as $loop) {
			$lvars = explode("|",$this->loops[$loop]['vars']);
			$hand = $this->loops[$loop]['hand'];
			$subhand = $this->loops[$loop]['subhand'];
			foreach ($lvars as $lvar) {
				$if = str_replace("var:".$loop.":".$lvar,"\$this->loops['".$loop."']['values']['".$lvar."'][".$hand."][".$subhand."]",$if);
				for ($i=0;$i<sizeof($elseifs);$i++) {
					$elseifs[$i] = str_replace("var:".$loop.":".$lvar,"\$this->loops['".$loop."']['values']['".$lvar."'][".$hand."][".$subhand."]",$elseifs[$i]);
				}
			}
		}
		}

		// Prepare the vars

		if ($vars_keys) {
		foreach ($vars_keys as $var) {
			$if = str_replace("var:".$var,"\$this->vars['".$var."']",$if);
			for ($i=0;$i<sizeof($elseifs);$i++) {
				$elseifs[$i] = str_replace("var:".$var,"\$this->vars['".$var."']",$elseifs[$i]);
		}
		}
		}

		// Prepare the command to eval
		
		$command = "if (".$if.") { \$ret = \"if\"; } \n";
		
		for ($i=0;$i<sizeof($elseifs);$i++) {
			$command .= "elseif (".$elseifs[$i].") { \$ret = \"elseif_".$i."\"; } \n";
		}
		
		if ($this->ifs[$key]['else']) {
			$command .= "else { \$ret = \"else\"; }";
		}

		eval($command);

		if ($ret == "if") {
			$content = $this->make_ifs_content($content,$key,$ret);
		}
		elseif (substr($ret,0,6) == "elseif") {
			$content = $this->make_ifs_content($content,$key,$ret);
		}
		elseif ($ret == "else") {
			$content = $this->make_ifs_content($content,$key,$ret);
		}
		else {
			$content = $this->make_ifs_content($content,$key,null);
		}
		}
	}
	}
	return $content;
 }


 /*
 	Substitute the loop vars
 */

 function parse_lvars($html)
 {
	$loops_keys = @array_keys($this->loops);
	
	if ($loops_keys) {

	foreach ($loops_keys as $loop) {

		$names_keys = explode("|",$this->loops[$loop]['vars']);
		$first = current($names_keys);

		while (preg_match("/\{loop:".$loop."\}/si",$html)) {
		$this->loops[$loop]['subhand'] = (int)0;
		$hand = $this->loops[$loop]['hand'];
		unset($temp);
		
		$size = sizeof($this->loops[$loop]['values'][$first][$hand]);

		$temp = $this->make_content($html,$loop);

		// Multiply the loop
		for ($i=0;$i<$size;$i++) {
			$temp_a[] = $temp;
		}
		unset($temp);
		
		for ($i=0;$i<$size;$i++) {
			foreach ($names_keys as $name) {
				$temp_a[$i] = $this->make_lvars_replace($temp_a[$i],$loop,$name,$this->loops[$loop]['values'][$name][$hand][$i]);
				$temp_a[$i] = $this->parse_ifs($temp_a[$i],1);
			}
			$this->loops[$loop]['subhand']++;
			$temp .= $temp_a[$i];
		}
		
		$html = $this->make_content_replace($html,$loop,$temp);
		unset($temp);
		unset($temp_a);

		$hand++;
		if ($this->loops[$loop]['values'][$first][$hand]) {
			$this->loops[$loop]['hand']++;
		}
		}
	}

	}
	return $html;
 }
  

 /*
	Prints the template
 */

 function tprint()
 {
	$html = $this->templates[$this->file];
	$html = $this->parse_includes($html);
	$html = $this->parse_lvars($html);
	$html = $this->parse_vars($html);
	$html = $this->parse_ifs($html);

	echo $html;
 }
 
} // end class

?>
Return current item: Perfect Templates