<?php
/********************************************************************/
/*
@Class Name: PLATO
@Version: 0.0.5
@Author: Rick Hopkins
@Date: 01.14.2004
@Description:
[ Plato is a php based template engine. It uses regular expressions, preg commands ]
[ and string commands to search through template files (*.tpl) and replace them with ]
[ the assigned variables. Plato also has plugins for simplifying some task such as ]
[ certain html functions like creating a select box. See documentation for a complete ]
[ list of all plato html functions, or any other questions you might have. ]
*/
/********************************************************************/
class plato
{ //class in session
//declare variables
var $templateDir;
var $pluginDir;
var $plugins;
var $assignVars;
var $startTime;
var $endTime;
var $parseTime;
var $parseFlag;
/************************************************/
//function plato() will be the constructor class for plato class
function plato($tmpDir = "", $plgDir = "")
{
if (strlen($tmpDir) > 0){
$this->templateDir = $tmpDir;
} else {
$this->templateDir = sprintf("tpl/");
}
if (strlen($plgDir) > 0){
$this->pluginDir = $plgDir;
} else {
$this->pluginDir = sprintf("lib/plugins/");
}
$this->plugins = array();
$this->loadPlugins();
$this->startTime = "";
$this->endTime = "";
$this->parseTime = "";
$this->parseFlag = 0;
//print($this->listPlugins());
}
/************************************************/
//function loadPlugins() will load the available plugins
function loadPlugins()
{
$i = 0;
$openDir = opendir($this->pluginDir);
while($pg = readdir($openDir)){
if ($pg != "." && $pg != ".."){
include($this->pluginDir.$pg);
$pgFunc = explode(".", $pg);
$this->plugins[$i]["name"] = $pg;
$this->plugins[$i]["function"] = $pgFunc[1];
$i++;
}
}
}
/************************************************/
//function listPlugins() will list the plugins that are loaded
function listPlugins()
{
$pg = sprintf("Plugins Loaded:<br>");
$pg .= sprintf("-----------------<br>");
for ($i = 0; $i < count($this->plugins); $i++){
$pg .= sprintf("Filename: %s<br>Function: %s<br><br>", $this->plugins[$i]["name"], $this->plugins[$i]["function"]);
}
return $pg;
}
/************************************************/
//function assign() will assign a value to a variable
function assign($var, $val)
{
$this->unAssign($var);
$this->assignVars[$var] = $val;
}
/************************************************/
//function unAssign() will clear a variable from assignment
function unAssign($var)
{
$this->assignVars[$var] = "";
}
/************************************************/
//function display() will parse the templates and display complete
function display($file)
{
// get time start parsing file
$this->startTime = $this->getMicrotime();
$file = implode("", file($this->templateDir.$file));
//perform search and replace for all include calls
$file = $this->performIncludes($file);
//perform search and replace for all loop calls
$file = $this->performLoops($file);
//perform search and replace for all function calls
$file = $this->performFuncCalls($file);
//perform search and replace for all common variables
$file = $this->performVars($file);
// display file
print($file);
// get time done parsing file, set parse flag
$this->endTime = $this->getMicrotime();
$this->parseFlag = 1;
}
/************************************************/
//function performIncludes() will bring in all included files
function performIncludes($str)
{
// perform search and replace for all include files - must be done first to include code from other files
$pattern = sprintf("'{plato_include.*\}'i");
preg_match_all($pattern, $str, $include);
for ($i = 0; $i < count($include[0]); $i++){
parse_str(str_replace("{", "", str_replace("}", "", $include[0][$i])), $inc);
if ($inc["ref"] == 1){
$file = $this->assignVars[$inc["plato_include"]];
} else {
$file = $inc["plato_include"];
}
switch ($inc["type"]){
case "tpl":
$incFile = file($this->templateDir.$file);
$strTag = implode("", $incFile);
break;
case "css":
$strTag = sprintf("<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">", $inc["plato_include"]);
break;
case "js":
$strTag = sprintf("<script language=\"javascript\" src=\"%s\"></script>", $inc["plato_include"]);
break;
}
$str = str_replace($include[0][$i], $strTag, $str);
}
//for nested includes
if (preg_match($pattern, $str) > 0){
$str = $this->performIncludes($str);
}
return $str;
}
/************************************************/
//function performFuncCalls() will perform all calls to functions
function performFuncCalls($str)
{
// perform search and replace for all function calls
$pattern = sprintf("'{plato_func.*\}'");
preg_match_all($pattern, $str, $func);
for ($i = 0; $i < count($func[0]); $i++){
parse_str(str_replace("{", "", str_replace("}", "", $func[0][$i])), $args);
if ($args["plato_func"]){
$args["ref"] == 1 ? $str = str_replace($func[0][$i], $args["plato_func"]($this->assignVars[$args["var"]]), $str) : $str = str_replace($func[0][$i], $args["plato_func"]($args["var"]), $str);
}
}
return $str;
}
/************************************************/
//function performLoops() will do loops for now until I get it working
function performLoops($str)
{
$pattern = sprintf("'\{plato_loop=([a-zA-Z0-9_]*)&id=([a-zA-Z0-9_]*)\}(.*)?\{/\\2*\}'si");
preg_match_all($pattern, $str, $loop);
$loopTimes = count($this->assignVars[$loop[1][0]]);
$loopStr = sprintf("%s", $loop[3][0]);
// <-- for nested loops
$subPattern = sprintf("'\{plato_loop=[a-zA-Z0-9_]*&id=[a-zA-Z0-9_]*\}'i");
if (preg_match($subPattern, $loopStr) > 0){
$loopStr = $this->performLoops($loopStr);
}
// -->
$pat = sprintf("'\{[a-z0-9]*\}'i");
preg_match_all($pat, $loopStr, $varMatch);
for ($i = 0; $i < $loopTimes; $i++){
$tmpStr[$i] = $loopStr;
for ($j = 0; $j < count($varMatch[0]); $j++){
$key = str_replace("{", "", str_replace("}", "", $varMatch[0][$j]));
$tmpStr[$i] = str_replace("\{$key}", $this->assignVars[$key][$i], $tmpStr[$i]);
}
}
$loopStr = implode("", $tmpStr);
$str = str_replace($loop[0][0], $loopStr, $str);
return $str;
}
/************************************************/
//function performVars() will replace all common variables with assigned values
function performVars($str)
{
// perform search for all single variables and replace with assign()
for ($i = 0; $i < count($this->assignVars); $i++){
$key = key($this->assignVars);
$search = sprintf("{%s}", $key);
$replace = sprintf("%s", $this->assignVars[$key]);
$str = str_replace($search, $replace, $str);
next($this->assignVars);
}
return $str;
}
/************************************************/
//function getMicrotime() will return the time in seconds since the epoch - used for timing the parsing of pages
function getMicrotime()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
/************************************************/
//function getParseTime() will return the amount of time it took to parse the file
function getParseTime()
{
if ($this->parseFlag == 1){
$this->parseTime = sprintf("%1.5f seconds", $this->endTime - $this->startTime);
return $this->parseTime;
} else {
return $this->parseFlag;
}
}
/************************************************/
} //class out
/********************************************************************/
?>