<?
class ExtensibleTemplate {
/**
* @access private
*/
private $TemplateVars = array();
/**
* @var string Specifies the path where template files are stored
*/
public $TemplateDir = ".";
/**
* @var string Specifies the path where plugins files are stored
*/
public $PluginsDir = ".";
/**
* @var string Specifies the main template file name
*/
public $MainHolder = "";
/**
* @var bool Specifies debugging for varios purposes
*/
public $Debug=false;
/**
* @var bool Specifies debugging for varios purposes
*/
public $DoDebugRV=false;
/**
* @var bool Specifies debugging for varios purposes
*/
public $Debugging=false;
/**
* @access private
*/
private $LimitStrMoreIndicator="...";
public $RelocateLinks=false;
public function __construct() {
$this->PluginsDir=dirname(__FILE__)."/plugins";
require_once(dirname(__FILE__)."/exttpl_block.api.php");
require_once(dirname(__FILE__)."/exttpl_simple.api.php");
require_once(dirname(__FILE__)."/exttpl_function.api.php");
define("PARAM_STRING",1);
define("PARAM_ARRAY",2);
define("PARAM_BOOLEAN",3);
define("PARAM_INT",4);
if (isset($_SESSION))
foreach($_SESSION as $k => $v) $this->SetValue("SESSION_$k",$v);
}
private function SanitizeAccented($theStr) {
$aux=$theStr;
$chars=array(225,233,237,243,250,241,209,193,201,205,211,218,191);
for ($i=0; $i<count($chars); $i++) $aux=str_replace(chr($chars[$i]),"&#".$chars[$i].";",$aux);
return $aux;
}
private function prCheckParameters($params,$paramDef) {
if (!is_array($params)) return false;
if (!is_array($paramDef)) return false;
foreach($paramDef as $key=>$type) {
if (!isset($params[$key])) return false;
switch ($type) {
case PARAM_STRING:
break;
case PARAM_ARRAY:
if (!is_array($params[$key])) return false;
break;
case PARAM_BOOLEAN:
if (!is_bool($params[$key])) return false;
break;
case PARAM_INT:
if (!is_int($params[$key])) return false;
break;
}
}
return true;
}
private function prloadTemplate($templateFile="") {
if (empty($templateFile)) $templateFile=$this->MainHolder;
if (substr($templateFile,0,1)=="/") $templateFile=substr($templateFile,1);
if (file_exists($this->TemplateDir."/".$templateFile) && is_file($this->TemplateDir."/".$templateFile)) {
return file_get_contents($this->TemplateDir."/".$templateFile);
} else {
return false;
}
}
private function prGetValue($param,$vars) {
$paramIntVal=(int)$param;
$numerico=($paramIntVal===$param);
if (!$numerico) {
if (isset($vars["$param"])) {
return $vars[$param];
} else
return $param;
} else {
return $param;
}
}
private function prParseParameters($theParamStr) {
$params=array();
if (preg_match_all('/([A-Za-z_0-9]*)="([A-Za-z0-9_\s\+\-\*\/\.\(\)\[\]=><!\'&|]*)"/s',$theParamStr,$matches,PREG_SET_ORDER)) {
foreach($matches as $match) {
$theParamValue=$match[2];
if (substr($theParamValue,-1)=="/") $theParamValue=substr($theParamValue,0,-1);
$params[strtoupper($match[1])]=str_replace('"','',$theParamValue);
}
} elseif (preg_match_all('/([A-Za-z_0-9]*)=([A-Za-z0-9_\+\-\*\/\.\(\)\[\]=><!]*)/s',$theParamStr,$matches,PREG_SET_ORDER)) {
foreach($matches as $match) {
$theParamValue=$match[2];
if (substr($theParamValue,-1)=="/") $theParamValue=substr($theParamValue,0,-1);
$params[strtoupper($match[1])]=str_replace('"','',$theParamValue);
}
}
return $params;
}
private function prProcessBlockTags($source,$vars="") {
if (empty($vars)) $vars=$this->TemplateVars;
$auxPlaceHolder=$source;
if(preg_match_all("/(<TPL:([a-z]*)(\s+)([^>]*)(\s*)>)(.*?)(<\/TPL:\\2>)/Usi",$auxPlaceHolder,$matches,PREG_SET_ORDER)) {
foreach($matches as $match) {
$tplCommand="bt_".$match[2];
$tplCmdParams=$this->prParseParameters($match[4]);
$tplBlock=$match[6];
$retVal=$tplBlock;
if (method_exists($this,$tplCommand))
$retVal=$this->{$tplCommand}($tplCmdParams,$tplBlock,$vars);
else {
if (file_exists($this->PluginsDir."/bltag.".strtolower($match[2]).".php")) {
require_once($this->PluginsDir."/bltag.".strtolower($match[2]).".php");
$clsName="block_tag_".strtolower($match[2]);
if (class_exists($clsName,false)) {
$clsInst=new $clsName();
$retVal=$clsInt->Process($tplCmdParams,$tplBlock,$vars);
}
}
}
$auxPlaceHolder=str_replace($match[0],$retVal,$auxPlaceHolder);
}
}
$auxPlaceHolder=$this->prProcessSimpleTags($auxPlaceHolder,$vars);
return $auxPlaceHolder;
}
function prProcessSimpleTags($theBlock,$vars="",$caller="") {
if (empty($vars)) $vars=$this->TemplateVars;
$auxPlaceHolder=$theBlock;
if(preg_match_all("/(<TPL:([a-z]*)(\s+)([^>]*)([\s]*)([\/]*)\/>)/si",$auxPlaceHolder,$matches,PREG_SET_ORDER)) {
foreach($matches as $match) {
$tplCommand="tg_".$match[2];
$tplCmdParams=$this->prParseParameters($match[4]);
if (substr($tplCmdParams,-1)=="/") $tplCmdParams=substr($tplCmdParams,0,-1);
// $retVal=$this->{$tplCommand}($tplCmdParams,$vars,$caller);
if (method_exists($this,$tplCommand))
$retVal=$this->{$tplCommand}($tplCmdParams,$vars,$caller);
else {
if (file_exists($this->PluginsDir."/tag.".strtolower($match[2]).".php")) {
require_once($this->PluginsDir."/tag.".strtolower($match[2]).".php");
$clsName="simple_tag_".strtolower($match[2]);
if (class_exists($clsName,false)) {
$clsInst=new $clsName();
$retVal=$clsInt->Process($tplCmdParams,$vars,$caller);
}
}
}
$auxPlaceHolder=str_replace($match[0],$retVal,$auxPlaceHolder);
}
}
return $auxPlaceHolder;
}
function prProcessSpecialFunctions($where,$vars="",$caller="") {
if (empty($vars)) $vars=$this->TemplateVars;
$auxPlaceHolder=$where;
if(preg_match_all("/(#([a-z]*) ([A-Za-z0-9_\s\=\+\-\*\/\.\(\)]*) #)/s",$where,$matches,PREG_SET_ORDER)) {
foreach($matches as $match) {
$tplCommand="sf_".$match[2];
$tplCmdParams=$this->prParseParameters($match[3]);
$tplBlock=$match[4];
// $retVal=$this->{$tplCommand}($tplCmdParams,$vars,$caller);
if (method_exists($this,$tplCommand))
$retVal=$this->{$tplCommand}($tplCmdParams,$vars,$caller);
else {
if (file_exists($this->PluginsDir."/function.".strtolower($match[2]).".php")) {
require_once($this->PluginsDir."/function.".strtolower($match[2]).".php");
$clsName="special_function_".strtolower($match[2]);
if (class_exists($clsName,false)) {
$clsInst=new $clsName();
$retVal=$clsInt->Process($tplCmdParams,$vars,$caller);
}
}
}
$auxPlaceHolder=str_replace($match[0],$retVal,$auxPlaceHolder);
}
}
return $auxPlaceHolder;
}
private function prReplaceValues($where,$values="",$callerName="") {
return $where;
}
private function prProcessTemplate($placeHolder,$vars="") {
if (empty($vars)) $vars=$this->TemplateVars;
$auxPlaceHolder=$placeHolder;
$auxPlaceHolder=$this->prProcessBlockTags($auxPlaceHolder,$vars);
if ($this->ClearRemainingTokens) {
$notSetTokens=array();
preg_match_all("'{([A-Za-z0-9_]*)}'si",$auxPlaceHolder,$parts);
if (count($parts[0])>0) {
for($i=0;$i<count($parts[0]);$i++)
$notSetTokens[$parts[0][$i]]="1";
foreach($notSetTokens as $tok => $dummy) $auxPlaceHolder=str_replace($tok,"",$auxPlaceHolder);
}
}
return $auxPlaceHolder;
}
public function ProcessFile($theFile,$vars="") {
if (empty($vars)) $vars=$this->TemplateVars;
$ph=$this->prLoadTemplate($theFile);
$auxPH=$ph;
$retVal=$this->prProcessTemplate($auxPH);
return $retVal;
}
public function SetTemplateDir($theTemplateDir) {
if (file_exists($theTemplateDir) && is_dir($theTemplateDir)) {
$this->TemplateDir=$theTemplateDir;
return true;
} else
return false;
}
public function SetPluginDir($thePluginDir) {
if (file_exists($thePluginDir) && is_dir($thePluginDir)) {
$this->PluginsDir=$thePluginDir;
return true;
} else
return false;
}
public function SetValue($Token, $Value,$Sanitize="") {
if ($Sanitize===true)
$this->TemplateVars["$Token"]=$this->SanitizeAccented($Value);
else
$this->TemplateVars["$Token"]=$Value;
}
public function __call($method,$params) {
if (substr($method,0,2)==="pr")
return $params[0];
}
/*
Below are the block tag processing functions
*/
private function bt_foreach($Params,$tplBlock,$vars) {
if (isset($Params["CONTROL"]))
$tplVars=$vars[$Params["CONTROL"]];
else
return $tplBlock;
if (isset($Params["ITEM"]))
$loopIdentifier=$Params["ITEM"].".";
else
return $tplBlock;
if (!is_array($tplVars) || (count($tplVars)==0)) return $tplBlock;
//
$_tplVars=array();
foreach($tplVars as $idx => $rec) {
$_rec=array();
if (is_array($rec)) {
foreach($rec as $f => $v) $_rec[$loopIdentifier.$f]=$v;
$_tplVars[]=$_rec;
}
}
$tplVars=$_tplVars;
$source=array();
foreach($tplVars as $idx => $record) {
$src=$tplBlock;
foreach($record as $key => $val)
if (!is_array($val)) $src=str_replace("{".$key."}",$val,$src);
$src=$this->prProcessTemplate($src,array($key=>$val));
$source[]=$src;
}
$auxPlaceHolder=implode("\n",$source);
return $auxPlaceHolder;
}
/*
Below are the single tag processing functions
*/
private function tg_htmlselect($Params) {
if (!$this->prCheckParameters($Params,array("NAME"=>PARAM_STRING))) return "";
$cboName=$Params["NAME"];
$cboOptions=$this->prGetValue($Params["OPTIONS"],$this->TemplateVars);
$htmlCode="<select name=\"$cboName\"";
if (isset($Params["MULTIPLE"])) $htmlCode.=" multiple";
if (isset($Params["CLASS"])) $htmlCode.=" class=\"".$Params["CLASS"]."\"";
if (isset($Params["ID"])) $htmlCode.=" id=\"".$Params["ID"]."\"";
if (isset($Params["SIZE"])) $htmlCode.=" size=\"".$Params["SIZE"]."\"";
foreach(array("ONCLICK","ONCHANGE","ONBLUR") as $event)
if (isset($Params[$event])) $htmlCode.=" $event=\"".$Params[$event]."\"";
$htmlCode.=">\n";
$options=array();
foreach($cboOptions as $idx => $option) {
if (is_array($option)) {
$_option=array();
$keys=array_keys($option);
foreach($keys as $key) $_option[strtoupper($key)]=$option[$key];
if (isset($_option["VALUE"]) && isset($_option["TEXT"])) {
$theOption="<option value=\"".$_option["VALUE"]."\"";
if (isset($_option["SELECTED"]))
$theOption.=" selected";
$theOption.=">".$_option["TEXT"]."</option>";
$options[]=$theOption;
}
}
}
$htmlCode.=implode("\n",$options)."\n</select>\n";
return $htmlCode;
}
private function tg_htmlradio($Params) {
if (!$this->prCheckParameters($Params,array("NAME"=>PARAM_STRING))) return "";
$cboName=$Params["NAME"];
$cboOptions=$this->prGetValue($Params["OPTIONS"],$this->TemplateVars);
$options=array();
foreach($cboOptions as $idx => $option) {
if (is_array($option)) {
$_option=array();
$keys=array_keys($option);
foreach($keys as $key) $_option[strtoupper($key)]=$option[$key];
if (isset($_option["VALUE"]) && isset($_option["TEXT"])) {
$htmlCode="<input type=\"radio\" name=\"$cboName\"";
if (isset($Params["CLASS"])) $htmlCode.=" class=\"".$Params["CLASS"]."\"";
if (isset($Params["ID"])) $htmlCode.=" id=\"".$Params["ID"]."\"";
$htmlCode.=" value=\"".$_option["VALUE"]."\"";
if (isset($_option["CHECKED"]))
$htmlCode.=" checked";
$htmlCode.=" />".$_option["TEXT"];
$options[]=$htmlCode;
}
}
}
$htmlCode=implode("\n",$options)."\n";
return $htmlCode;
}
private function tg_htmlcheck($Params) {
if (!$this->prCheckParameters($Params,array("NAME"=>PARAM_STRING))) return "";
$cboName=$Params["NAME"];
$cboOptions=$this->prGetValue($Params["OPTIONS"],$this->TemplateVars);
$options=array();
foreach($cboOptions as $idx => $option) {
if (is_array($option)) {
$_option=array();
$keys=array_keys($option);
foreach($keys as $key) $_option[strtoupper($key)]=$option[$key];
if (isset($_option["VALUE"]) && isset($_option["TEXT"])) {
$htmlCode="<input type=\"checkbox\" name=\"{$cboName}[]\"";
if (isset($Params["CLASS"])) $htmlCode.=" class=\"".$Params["CLASS"]."\"";
if (isset($Params["ID"])) $htmlCode.=" id=\"".$Params["ID"]."\"";
$htmlCode.=" value=\"".$_option["VALUE"]."\"";
if (isset($_option["CHECKED"]))
$htmlCode.=" checked";
$htmlCode.=" />".$_option["TEXT"];
$options[]=$htmlCode;
}
}
}
$htmlCode=implode("\n",$options)."\n";
return $htmlCode;
}
private function tg_Include($Params) {
if (!$this->prCheckParameters($Params,array("FILE"=>PARAM_STRING))) return "";
$fileName="";
$baseDir="";
if(isset($Params["FILE"])) $fileName=$Params["FILE"];
if(isset($Params["DIR"])) $baseDir=$Params["DIR"];
if (!empty($baseDir)) $fileName=$baseDir."/".$fileName;
if (file_exists($this->TemplateDir."/".$fileName)) {
$retVal=$this->ProcessFile($fileName);
} else
$retVal="";
return $retVal;
}
}
?>