<?php
if (!defined('IN_SITE'))
{
    die('Hacking attempt!');
}
class Template
{
		var $root = ".";
		var $filename = array();
		var $filetype = array();
		var $filecontent = array();
		var $blockvar = array();
		var $blockvalue = array();
		var $blockreplace = array();
		var $loopvar = 0;
		var $loopflag = array();
		var $varname = array();
		var $varvalue = array();
		var $varreplace = array();
		var $counter = 0;
		function Template($root = ".")
		{
				if (!is_dir($root))
				{
						$this->halt("Root folder \"<b>" . $root . "</b>\" does not exist or it is not a folder!");
				}
				$this->root = $root;
		}
		function set_root($root)
		{
				if (!is_dir($root))
				{
						$this->halt("Root folder \"<b>" . $root . "</b>\" does not exist or it is not a folder!");
				}
				$this->root = $root;
		}
		function set_files($_41D63, $_417A5 = "dynamic")
		{
				if (is_array($_41D63))
				{
						reset(&$_41D63);
						while (list($key, $_4174B) = each(&$_41D63))
						{
								if (!isset($this->filename[$key]))
								{
										$this->filetype[$key] = $_417A5;
										$this->filename[$key] = $this->check_file_exist($_4174B);
								}
						}
				}
				else
				{
						$this->halt("set_files() ==> File " . $_41D63 . " is not array");
				}
		}
		function unset_files($_41DBB)
		{
				if (is_array($_41DBB))
				{
						reset(&$_41DBB);
						while (list(, $key) = each(&$_41DBB))
						{
								$this->filename[$key] = "";
								$this->filetype[$key] = "";
								$this->filecontent[$key] = "";
						}
				}
				else
				{
						$this->filename[$_41DBB] = "";
						$this->filetype[$_41DBB] = "";
						$this->filecontent[$_41DBB] = "";
				}
		}
		function set_vars($_41DC2)
		{
				if (is_array($_41DC2))
				{
						reset(&$_41DC2);
						while (list($key, $_41DFC) = each(&$_41DC2))
						{
								$this->varname[$key] = "/" .preg_quote("{" . $key . "}") . "/";
								$this->varvalue[$key] = $_41DFC;
								$this->varreplace[$key] = "'.\$this->varvalue[\"" . $key . "\"].'";
						}
				}
				else
				{
						$this->halt("set_vars() ==> Var is not array");
				}
		}
		function set_block_vars($blockname, $var_array = "")
		{
				if (empty($blockname))
				{
						$this->halt("set_block_vars() ==> Blockname is empty.");
						return false;
				}
				if (!isset($this->loopflag[$blockname]))
				{
						++$this->loopvar;
						$this->loopflag[$blockname] = $this->loopvar;
				}
				$shortblock = "loop_" . $this->loopflag[$blockname];
				$shortvar = "\$_var_" . $this->loopflag[$blockname];
				if (strpos($blockname, ":"))
				{
						$block_replace = "";
						$str_loop = "";
						$str_loop_2 = "";
						$block_count = "\$this->blockvalue[\"" . $shortblock . "\"]";
						$length = strlen($blockname);
						$i = 0;
						for (; $i < $length; ++$i)
						{
								if (substr($blockname, $i, 1) == ":")
								{
										$block = substr($blockname, 0, $i);
										$parent = $this->count_parent_array($block, $str_loop);
										$block_replace .= "[" . $parent['var'] . "]";
										$block_count .= "[" . $parent['count'] . "]";
										$str_loop .= "[" . $parent['count'] . "]";
										$str_loop_2 .= "[" . $parent['var'] . "]";
								}
						}
						$str_loop .= "[]";
						$str_loop_2 .= "[" . $shortvar . "]";
						$str = "if (isset(" . $block_count . ")){" . $block_count . "++;}else{" . $block_count . "=1;}";
						eval($str);
				}
				else
				{
						if (!isset($this->blockvalue[$shortblock]))
						{
								$this->blockvalue[$shortblock] = 1;
						}
						else
						{
								++$this->blockvalue[$shortblock];
						}
						$block_replace = "";
						$str_loop = "[]";
						$str_loop_2 = "[" . $shortvar . "]";
				}
				$this->blockvar[$shortblock] = $shortvar;
				$this->blockreplace[$shortblock] = "\$this->blockvalue[\"" . $shortblock . "\"]" . $block_replace;
				if (is_array($var_array))
				{
						reset(&$var_array);
						while (list($key, $value) = each(&$var_array))
						{
								$this->varname[$shortblock . ":" . $key] = "/" . preg_quote("{" . $blockname . ":" . $key . "}") . "/";
								$str = "\$this->varvalue[\"" . $shortblock . ":" . $key . "\"]" . $str_loop . " = \$value;";
								eval($str);
								$this->varreplace[$shortblock . ":" . $key] = "'.\$this->varvalue[\"" . $shortblock . ":" . $key . "\"]" . $str_loop_2 . ".'";
						}
				}
				return true;
		}
		function count_parent_array($blockname, $str_loop)
		{
				$block['count'] = 0;
				if (isset($this->loopflag[$blockname]))
				{
						$shortblock = "loop_" . $this->loopflag[$blockname];
						$block['var'] = "\$_var_" . $this->loopflag[$blockname];
						if (isset($this->blockvalue[$shortblock]))
						{
								$str = "\r\n\t\t\t\tif (is_array(\$this->blockvalue[\$shortblock]" . $str_loop . ")){\r\n\t\t\t\t\$block[\"count\"] = sizeof(\$this->blockvalue[\$shortblock]" . $str_loop . ") - 1;\r\n\t\t\t\t}\r\n\t\t\t\telse{\r\n\t\t\t\t\$block[\"count\"] = \$this->blockvalue[\$shortblock]" . $str_loop . " - 1 ;\r\n\t\t\t\t}";
								eval($str);
								return $block;
						}
				}
				else
				{
						if (!isset($this->loopflag[$blockname]))
						{
								++$this->loopvar;
								$this->loopflag[$blockname] = $this->loopvar;
						}
						$block['var'] = "\$_var_" . $this->loopflag[$blockname];
				}
				return $block;
		}
		function show($handle = "", $return_str = 0)
		{
				if (empty($handle))
				{
						reset(&$this->filename);
						while (list($key) = each(&$this->filename))
						{
								if (!empty($key))
								{
										$this->show($key, $return_str);
								}
						}
						return true;
				}
				if (is_array($handle))
				{
						reset(&$handle);
						while (list(, $key) = each(&$handle))
						{
								if (!empty($key))
								{
										$this->show($key, $return_str);
								}
						}
						return true;
				}
				if (!$this->loadfile($handle))
				{
						$this->halt("Could not load " . $handle);
						return false;
				}
				if (isset($this->filetype[$handle]) && $this->filetype[$handle] == "static")
				{
						echo implode("\n", $this->filecontent[$handle]);
				}
				else
				{
						$number_lines = count($this->filecontent[$handle]);
						$lines = $this->filecontent[$handle];
						$i = 0;
						for (; $i < $number_lines; ++$i)
						{
								if (preg_match("#<!-- START: (.*?) -->#", $lines[$i]))
								{
										$block = trim(preg_replace("#<!-- START: (.*?) -->#", "\\1", $lines[$i]));
										if (isset($this->loopflag[$block]))
										{
												$shortblock = "loop_" . $this->loopflag[$block];
												$lines[$i] = "\r\n\t\t\t\t\t    \$_counter_" . $this->counter . " = isset(" . $this->blockreplace[$shortblock] . ") ? " . $this->blockreplace[$shortblock] . " : 0;\r\n\t\t\t\t\t    for (\$_var_" . $this->loopflag[$block] . "=0; \$_var_" . $this->loopflag[$block] . "<\$_counter_" . $this->counter . "; \$_var_" . $this->loopflag[$block] . "++){";
												++$this->counter;
										}
										else
										{
												$lines[$i] = "if (false){";
										}
								}
								else
										if (preg_match("#<!-- END: (.*?) -->#", $lines[$i]))
										{
												$lines[$i] = "}";
										}
										else
												if (preg_match("#<!-- START IF: (.*?) -->#", $lines[$i]))
												{
														$block = trim(preg_replace("#<!-- START IF: (.*?) -->#", "\\1", $lines[$i]));
														if (isset($this->loopflag[$block]))
														{
																$shortblock = "loop_" . $this->loopflag[$block];
																$lines[$i] = "if ( isset(" . $this->blockreplace[$shortblock] . ") && " . $this->blockreplace[$shortblock] . " ){";
														}
														else
														{
																$lines[$i] = "if (false){";
														}
												}
												else
														if (preg_match("#<!-- END IF: (.*?) -->#", $lines[$i]))
														{
																$lines[$i] = "}";
														}
														else
																if (preg_match("#<!-- START NOIF: (.*?) -->#", $lines[$i]))
																{
																		$block = trim(preg_replace("#<!-- START NOIF: (.*?) -->#si", "\\1", $lines[$i]));
																		if (isset($this->loopflag[$block]))
																		{
																				$shortblock = "loop_" . $this->loopflag[$block];
																				$lines[$i] = "if ( !isset(" . $this->blockreplace[$shortblock] . ") || !" . $this->blockreplace[$shortblock] . "){";
																		}
																		else
																		{
																				$lines[$i] = "if (true){";
																		}
																}
																else
																		if (preg_match("#<!-- END NOIF: (.*?) -->#", $lines[$i]))
																		{
																				$lines[$i] = "}";
																		}
																		else
																		{
																				$this->replace_line($lines[$i], $return_str);
																		}
						}
						$str = implode("\n", $lines);
						$str = @preg_replace($this->varname, $this->varreplace, $str);
						if ($return_str)
						{
								$str = "\$_str\t= \"\";\n" . $str;
								eval($str);
								return $_str;
						}
						eval($str);
				}
				return true;
		}
		function replace_line(&$_42734, $_42433 = 0)
		{
				$_42734 = str_replace("\\", "\\\\", $_42734);
				$_42734 = str_replace("'", "\'", $_42734);
				if ($_42433)
				{
						$_42734 = "$" . "_str .= '" . $_42734 . "';";
				}
				else
				{
						$_42734 = "echo '" . $_42734 . "';";
				}
		}
		function check_file_exist($_4174B = "", $dir = "")
		{
				if (empty($_4174B))
				{
						$this->halt("Get empty filename");
						return false;
				}
				if (substr($_4174B, 0, 1) == "/")
				{
						if (!empty($dir))
						{
								$_4174B = $dir . $_4174B;
						}
						else
						{
								$_4174B = $this->root . $_4174B;
						}
				}
				else
						if (!empty($dir))
						{
								$_4174B = $dir . "/" . $_4174B;
						}
						else
						{
								$_4174B = $this->root . "/" . $_4174B;
						}
						if (!file_exists($_4174B))
						{
								$this->halt("{$_4174B} does not exist");
								return false;
						}
				return $_4174B;
		}
		function loadfile($handle)
		{
				if (isset($this->filecontent[$handle]))
				{
						return true;
				}
				if (!isset($this->filename[$handle]))
				{
						$this->halt("{$handle} is not a valid handle");
						return false;
				}
				$this->filecontent[$handle] = @file($this->filename[$handle]);
				return true;
		}
		function page_transfer($_4276D, $_427B0, $_42842 = 5, $_42890 = "transfer.tpl")
		{
				global $DB;
				@$DB->close();
				if (empty($_427B0))
				{
						$this->halt("There isn't page_to value");
				}
				$this->set_files(array("transfer" => $_42890));
				$this->set_vars(array("MESSAGE" => $_4276D, "SECONDS" => $_42842, "PAGE_TO" => $_427B0));
				$this->show("transfer");
				exit();
		}
		function fast_transfer($_427B0)
		{
				global $DB;
				@$DB->close();
				if (empty($_427B0))
				{
						$this->halt("Doesn't have page_to value");
				}
				if (headers_sent())
				{
						echo "<br><br><b>Transfer Page Error: Header has been sent</b>.<br><a href='javascript:history.back(-1);'>Go Back</a>";
						exit();
				}
				header("Location: " . $_427B0);
				die();
		}
		function message_die($_4276D)
		{
				global $DB;
				global $Lang;
				@$DB->close();
				$this->set_vars(array("MESSAGE" => $_4276D, "L_PAGE_TITLE" => $Lang->data['general_message_die'], "L_CLOSE" => $Lang->data['general_close_window']));
				$this->set_files(array("die" => "message_die.tpl"));
				$this->show('die');
				die();
		}
		function halt($_4276D)
		{
				echo "<b>Template Error:</b>\n<br>";
				echo $_4276D;
				exit();
		}
}
?>
