<?php
class template
{
	var $Error_Reporting=false;
	var $Error_LogFile="./errors.php";
	var $template_dir= "./template/";
	var $sadrzaj;
	var $exit_on_display=true;
	var $tag="{";
	var $closetag="}";
	var $arrays=array();
	var $variables=array();
	var $array_help="";
	var $intlevel=0;
	var $do=array();
	var $disable_time_to_date=true;
	function error($ernum)
	{
		if(!$this->Error_Reporting) return;
		if(!file_exists($this->Error_LogFile)) die("Error with template the page cannot be displayed!");
		require_once($this->Error_LogFile);
		die($error[$ernum]);

	}
	function start($template_file)
	{
		if(!file_exists($this->template_dir.$template_file)) $this->error(1);
		$fp=fopen($this->template_dir.$template_file,"r");
		$this->sadrzaj=fread($fp,filesize($this->template_dir.$template_file));
		$this->inc();
	}

	function inc()
	{
		$file=strstr($this->sadrzaj,$this->tag."INCLUDE '");
		$file=substr($file,10,strpos($file,"'}")-10);
		if(file_exists($this->template_dir.$file))
		{
			if(!$file) return;
			$fp=fopen($this->template_dir.$file,"r");

			$replaceaj=fread($fp,filesize($this->template_dir.$file));
			$this->sadrzaj=str_replace($this->tag."INCLUDE '$file'".$this->closetag,$replaceaj,$this->sadrzaj);
			if(ereg($this->tag."INCLUDE '",$this->sadrzaj)) $this->inc();
		}
	}
	function assign($htmlvar,$phpvar=0,$dateformat="d.m.Y, H:i")
	{
		if((!is_array($htmlvar))AND(!is_array($phpvar)))
		{
		if(!$this->disable_time_to_date)
		{
			if((strlen($phpvar)==10)AND(is_numeric($phpvar)))
			$phpvar=date($dateformat,$phpvar);
		}
		$this->variables[$htmlvar]=$phpvar;
		}
		if((is_array($htmlvar))AND($phpvar==0))
		{
			foreach($htmlvar as $a=>$b)
			{
				if(!$this->disable_time_to_date)
				{
					if((strlen($b)==10)AND(is_numeric($b)))
					$b=date($dateformat,$b);
				}
				$this->assign($a,$b);
			}
		}
		if((is_array($phpvar))AND(!is_array($htmlvar)))
		{
			$this->arrays[$htmlvar]=$phpvar;
		}
	}
	function arrays($array=0,$level="")
	{
		$this->array_help[$this->intlevel]=$level;
		if($array==0)$array=$this->arrays;

		$Keys = array_keys( $array );
		foreach( $Keys as $OneKey )
		{
			$this->array_help[$this->intlevel]=$OneKey.'.';
			if ( is_array($array[$OneKey]) )
			$this->arrays($array[$OneKey],$OneKey,$this->intlevel++);
			else{
				$f="";
				for($i=0;$i<$this->intlevel;$i++)
				$f.=$this->array_help[$i];
				$f.=$OneKey;
				$this->assign($f,$array[$OneKey]);
			}
		}
		$this->intlevel--;
	}
	function variables()
	{
		foreach($this->variables as $id=>$value)
		{
			$this->sadrzaj=str_replace($this->tag.'$'.$id.$this->closetag,$value,$this->sadrzaj);
		}
	}
	function replace($old,$new)
	{
		$this->sadrzaj=str_replace($old,$new,$this->sadrzaj);
	}
	function addloop($name,$incrementator,$repeat,$incrementator_start=0,$step=1)
	{
		if(empty($this->sadrzaj))
		{
			$this->do[]='$this->addloop('."'$name','$incrementator','$repeat','$incrementator_start','$step');";
		}
		$od=strpos($this->sadrzaj,$this->tag."LOOP name='$name'".$this->closetag)+strlen($this->tag."LOOP name='$name'".$this->closetag);

		$looptext=substr(substr($this->sadrzaj,$od),0,strpos(substr($this->sadrzaj,$od),$this->tag."/LOOP name='$name'".$this->closetag));

		$txt="";
		for($i=$incrementator_start;$i<$repeat;$i=$i+$step)
		{
			$txt.= str_replace('$'.$incrementator,$i,$looptext);
		}
		$this->sadrzaj=str_replace($this->tag."LOOP name='$name'".$this->closetag.$looptext.$this->tag."/LOOP name='$name'".$this->closetag,$txt,$this->sadrzaj);

	}
	function clean()
	{
		$this->sadrzaj=preg_replace('/\{(.*)\}/','',$this->sadrzaj);

	}
	function display()
	{
		if(!empty($this->do))
		{
			foreach($this->do as $v)
			{
				eval($v);
			}
		}
		$this->inc();
		$this->variables();
		$this->arrays();
		$this->variables();
		$this->clean();
		echo $this->sadrzaj;
		if($this->exit_on_display) exit(0);
	}
	function show($filename)
	{
		$this->start($filename);
		$this->display();
	}
}