1
0
mirror of https://e.coding.net/circlecloud/MinecraftAccount.git synced 2025-11-26 21:46:09 +00:00

首次提交...

Signed-off-by: j502647092 <jtb1@163.com>
This commit is contained in:
j502647092
2015-11-01 22:25:03 +08:00
commit 2003fda7cb
409 changed files with 77938 additions and 0 deletions

View File

@@ -0,0 +1,392 @@
<?php
/**
* SmartTemplate Class
*
* 'Compiles' HTML-Templates to PHP Code
*
*
* Usage Example I:
*
* $page = new SmartTemplate( "template.html" );
* $page->assign( 'TITLE', 'TemplateDemo - Userlist' );
* $page->assign( 'user', DB_read_all( 'select * from ris_user' ) );
* $page->output();
*
* Usage Example II:
*
* $data = array(
* 'TITLE' => 'TemplateDemo - Userlist',
* 'user' => DB_read_all( 'select * from ris_user' )
* );
* $page = new SmartTemplate( "template.html" );
* $page->output( $data );
*
*
* @author Philipp v. Criegern philipp@criegern.com
* @author Manuel 'EndelWar' Dalla Lana endelwar@aregar.it
* @version 1.2.1 03.07.2006
*
* CVS ID: $Id: class.smarttemplate.php 2504 2011-12-28 07:35:29Z liu21st $
*/
class SmartTemplate
{
/**
* Whether to store compiled php code or not (for debug purpose)
*
* @access public
*/
var $reuse_code = true;
/**
* Directory where all templates are stored
* Can be overwritten by global configuration array $_CONFIG['template_dir']
*
* @access public
*/
var $template_dir = 'templates/';
/**
* Where to store compiled templates
* Can be overwritten by global configuration array $_CONFIG['smarttemplate_compiled']
*
* @access public
*/
var $temp_dir = 'templates_c/';
/**
* Temporary folder for output cache storage
* Can be overwritten by global configuration array $_CONFIG['smarttemplate_cache']
*
* @access public
*/
var $cache_dir = 'templates_c/';
/**
* Default Output Cache Lifetime in Seconds
* Can be overwritten by global configuration array $_CONFIG['cache_lifetime']
*
* @access public
*/
var $cache_lifetime = 600;
/**
* Temporary file for output cache storage
*
* @access private
*/
var $cache_filename;
/**
* The template filename
*
* @access private
*/
var $tpl_file;
/**
* The compiled template filename
*
* @access private
*/
var $cpl_file;
/**
* Template content array
*
* @access private
*/
var $data = array();
/**
* Parser Class
*
* @access private
*/
var $parser;
/**
* Debugger Class
*
* @access private
*/
var $debugger;
/**
* SmartTemplate Constructor
*
* @access public
* @param string $template_filename Template Filename
*/
function SmartTemplate ( $template_filename = '' )
{
global $_CONFIG;
if (!empty($_CONFIG['smarttemplate_compiled']))
{
$this->temp_dir = $_CONFIG['smarttemplate_compiled'];
}
if (!empty($_CONFIG['smarttemplate_cache']))
{
$this->cache_dir = $_CONFIG['smarttemplate_cache'];
}
if (is_numeric($_CONFIG['cache_lifetime']))
{
$this->cache_lifetime = $_CONFIG['cache_lifetime'];
}
if (!empty($_CONFIG['template_dir']) && is_file($_CONFIG['template_dir'] . '/' . $template_filename))
{
$this->template_dir = $_CONFIG['template_dir'];
}
$this->tpl_file = $template_filename;
}
// DEPRECATED METHODS
// Methods used in older parser versions, soon will be removed
function set_templatefile ($template_filename) { $this->tpl_file = $template_filename; }
function add_value ($name, $value ) { $this->assign($name, $value); }
function add_array ($name, $value ) { $this->append($name, $value); }
/**
* Assign Template Content
*
* Usage Example:
* $page->assign( 'TITLE', 'My Document Title' );
* $page->assign( 'userlist', array(
* array( 'ID' => 123, 'NAME' => 'John Doe' ),
* array( 'ID' => 124, 'NAME' => 'Jack Doe' ),
* );
*
* @access public
* @param string $name Parameter Name
* @param mixed $value Parameter Value
* @desc Assign Template Content
*/
function assign ( $name, $value = '' )
{
if (is_array($name))
{
foreach ($name as $k => $v)
{
$this->data[$k] = $v;
}
}
else
{
$this->data[$name] = $value;
}
}
/**
* Assign Template Content
*
* Usage Example:
* $page->append( 'userlist', array( 'ID' => 123, 'NAME' => 'John Doe' ) );
* $page->append( 'userlist', array( 'ID' => 124, 'NAME' => 'Jack Doe' ) );
*
* @access public
* @param string $name Parameter Name
* @param mixed $value Parameter Value
* @desc Assign Template Content
*/
function append ( $name, $value )
{
if (is_array($value))
{
$this->data[$name][] = $value;
}
elseif (!is_array($this->data[$name]))
{
$this->data[$name] .= $value;
}
}
/**
* Parser Wrapper
* Returns Template Output as a String
*
* @access public
* @param array $_top Content Array
* @return string Parsed Template
* @desc Output Buffer Parser Wrapper
*/
function result ( $_top = '' )
{
ob_start();
$this->output( $_top );
$result = ob_get_contents();
ob_end_clean();
return $result;
}
/**
* Execute parsed Template
* Prints Parsing Results to Standard Output
*
* @access public
* @param array $_top Content Array
* @desc Execute parsed Template
*/
function output ( $_top = '' )
{
global $_top;
// Make sure that folder names have a trailing '/'
if (strlen($this->template_dir) && substr($this->template_dir, -1) != '/')
{
$this->template_dir .= '/';
}
if (strlen($this->temp_dir) && substr($this->temp_dir, -1) != '/')
{
$this->temp_dir .= '/';
}
// Prepare Template Content
if (!is_array($_top))
{
if (strlen($_top))
{
$this->tpl_file = $_top;
}
$_top = $this->data;
}
$_obj = &$_top;
$_stack_cnt = 0;
$_stack[$_stack_cnt++] = $_obj;
// Check if template is already compiled
$cpl_file_name = preg_replace('/[:\/.\\\\]/', '_', $this->tpl_file);
if (strlen($cpl_file_name) > 0)
{
$this->cpl_file = $this->temp_dir . $cpl_file_name . '.php';
$compile_template = true;
if ($this->reuse_code)
{
if (is_file($this->cpl_file))
{
if ($this->mtime($this->cpl_file) > $this->mtime($this->template_dir . $this->tpl_file))
{
$compile_template = false;
}
}
}
if ($compile_template)
{
if (@include_once("class.smarttemplateparser.php"))
{
$this->parser = new SmartTemplateParser($this->template_dir . $this->tpl_file);
if (!$this->parser->compile($this->cpl_file))
{
exit( "SmartTemplate Parser Error: " . $this->parser->error );
}
}
else
{
exit( "SmartTemplate Error: Cannot find class.smarttemplateparser.php; check SmartTemplate installation");
}
}
// Execute Compiled Template
include($this->cpl_file);
}
else
{
exit( "SmartTemplate Error: You must set a template file name");
}
// Delete Global Content Array in order to allow multiple use of SmartTemplate class in one script
unset ($_top);
}
/**
* Debug Template
*
* @access public
* @param array $_top Content Array
* @desc Debug Template
*/
function debug ( $_top = '' )
{
// Prepare Template Content
if (!$_top)
{
$_top = $this->data;
}
if (@include_once("class.smarttemplatedebugger.php"))
{
$this->debugger = new SmartTemplateDebugger($this->template_dir . $this->tpl_file);
$this->debugger->start($_top);
}
else
{
exit( "SmartTemplate Error: Cannot find class.smarttemplatedebugger.php; check SmartTemplate installation");
}
}
/**
* Start Ouput Content Buffering
*
* Usage Example:
* $page = new SmartTemplate('template.html');
* $page->use_cache();
* ...
*
* @access public
* @desc Output Cache
*/
function use_cache ( $key = '' )
{
if (empty($_POST))
{
$this->cache_filename = $this->cache_dir . 'cache_' . md5($_SERVER['REQUEST_URI'] . serialize($key)) . '.ser';
if (($_SERVER['HTTP_CACHE_CONTROL'] != 'no-cache') && ($_SERVER['HTTP_PRAGMA'] != 'no-cache') && @is_file($this->cache_filename))
{
if ((time() - filemtime($this->cache_filename)) < $this->cache_lifetime)
{
readfile($this->cache_filename);
exit;
}
}
ob_start( array( &$this, 'cache_callback' ) );
}
}
/**
* Output Buffer Callback Function
*
* @access private
* @param string $output
* @return string $output
*/
function cache_callback ( $output )
{
if ($hd = @fopen($this->cache_filename, 'w'))
{
fputs($hd, $output);
fclose($hd);
}
return $output;
}
/**
* Determine Last Filechange Date (if File exists)
*
* @access private
* @param string $filename
* @return mixed
* @desc Determine Last Filechange Date
*/
function mtime ( $filename )
{
if (@is_file($filename))
{
$ret = filemtime($filename);
return $ret;
}
}
}
?>

View File

@@ -0,0 +1,456 @@
<?php
/**
* SmartTemplateDebugger Class
* Used by SmartTemplate Class
*
* @desc Used by SmartTemplate Class
* @author Philipp v. Criegern philipp@criegern.com
* @author Manuel 'EndelWar' Dalla Lana endelwar@aregar.it
* @version 1.2.1 03.07.2006
*
* CVS ID: $Id: class.smarttemplatedebugger.php 2504 2011-12-28 07:35:29Z liu21st $
*/
class SmartTemplateDebugger
{
/**
* The template Filename
*
* @access private
*/
var $filename;
/**
* The template itself
*
* @access private
*/
var $template;
/**
* SmartTemplateParser Constructor
*
* @param string $template_filename HTML Template Filename
*/
function SmartTemplateDebugger ( $template_filename )
{
$this->filename = $template_filename;
// Load Template
if ($hd = @fopen($template_filename, "r"))
{
$this->template = fread($hd, filesize($template_filename));
fclose($hd);
}
else
{
$this->template = "SmartTemplate Debugger Error: File not found: '$template_filename'";
}
$this->tab[0] = '';
for ($i=1; $i < 10; $i++) {
$this->tab[$i] = str_repeat(' ', $i);
}
}
/**
* Main Template Parser
*
* @param string $compiled_template_filename Compiled Template Filename
* @desc Creates Compiled PHP Template
*/
function start ( $vars )
{
$page = $this->template;
$page = preg_replace("/(<!-- BEGIN [ a-zA-Z0-9_.]* -->)/", "\n$1\n", $page);
$page = preg_replace("/(<!-- IF .+? -->)/", "\n$1\n", $page);
$page = preg_replace("/(<!-- END.*? -->)/", "\n$1\n", $page);
$page = preg_replace("/(<!-- ELSEIF .+? -->)/", "\n$1\n", $page);
$page = preg_replace("/(<!-- ELSE [ a-zA-Z0-9_.]*-->)/", "\n$1\n", $page);
$page = $this->highlight_html($page);
$rows = explode("\n", $page);
$page_arr = array();
$level = 0;
$blocklvl = 0;
$rowcnt = 0;
$spancnt = 0;
$offset = 22;
$lvl_block = array();
$lvl_row = array();
$lvl_typ = array();
foreach ($rows as $row)
{
if ($row = trim($row))
{
$closespan = false;
if (substr($row, $offset, 12) == '&lt;!-- END ')
{
if ($level < 1)
{
$level++;
$error[$rowcnt] = "END Without BEGIN";
}
elseif ($lvl_typ[$level] != 'BEGIN')
{
$error[$lvl_row[$level]] = "IF without ENDIF";
$error[$rowcnt] = "END Without BEGIN";
}
$blocklvl--;
$level--;
$closespan = true;
}
if (substr($row, $offset, 14) == '&lt;!-- ENDIF ')
{
if ($level < 1)
{
$level++;
$error[$rowcnt] = "ENDIF Without IF";
}
elseif ($lvl_typ[$level] != 'IF')
{
$error[$lvl_row[$level]] = "BEGIN without END";
$error[$rowcnt] = "ENDIF Without IF";
}
$closespan = true;
$level--;
}
if ($closespan)
{
$page_arr[$rowcnt-1] .= '</span>';
}
$this_row = $this->tab[$level] . $row;
if (substr($row, $offset, 12) == '&lt;!-- ELSE')
{
if ($level < 1)
{
$error[$rowcnt] = "ELSE Without IF";
}
elseif ($lvl_typ[$level] != 'IF')
{
$error[$rowcnt] = "ELSE Without IF";
}
else
{
$this_row = $this->tab[$level-1] . $row;
}
}
if (substr($row, $offset, 14) == '&lt;!-- BEGIN ')
{
if ($blocklvl == 0)
{
if ($lp = strpos($row, '--&gt;'))
{
if ($blockname = trim(substr($row, $offset + 14, $lp -$offset -14)))
{
if ($nr = count($vars[$blockname]))
{
$this_row .= $this->toggleview("$nr Entries");
}
else
{
$this_row .= $this->toggleview("Emtpy");
}
}
}
}
else
{
$this_row .= $this->toggleview('[');
}
$blocklvl++;
$level++;
$lvl_row[$level] = $rowcnt;
$lvl_typ[$level] = 'BEGIN';
}
elseif (substr($row, $offset, 11) == '&lt;!-- IF ')
{
$level++;
$lvl_row[$level] = $rowcnt;
$lvl_typ[$level] = 'IF';
$this_row .= $this->toggleview();
}
$page_arr[] = $this_row;
$lvl_block[$rowcnt] = $blocklvl;
$rowcnt++;
}
}
if ($level > 0)
{
$error[$lvl_row[$level]] = "Block not closed";
}
$page = join("\n", $page_arr);
$rows = explode("\n", $page);
$cnt = count($rows);
for ($i = 0; $i < $cnt; $i++)
{
// Add Errortext
if (isset($error))
{
if ($err = $error[$i])
{
$rows[$i] = '<b>' . $rows[$i] . ' ERROR: ' . $err . '!</b>';
}
}
// Replace Scalars
if (preg_match_all('/{([a-zA-Z0-9_. &;]+)}/', $rows[$i], $var))
{
foreach ($var[1] as $tag)
{
$fulltag = $tag;
if ($delim = strpos($tag, ' &gt; '))
{
$tag = substr($tag, 0, $delim);
}
if (substr($tag, 0, 4) == 'top.')
{
$title = $this->tip($vars[substr($tag, 4)]);
}
elseif ($lvl_block[$i] == 0)
{
$title = $this->tip($vars[$tag]);
}
else
{
$title = '[BLOCK?]';
}
$code = '<b title="' . $title . '">{' . $fulltag . '}</b>';
$rows[$i] = str_replace('{'.$fulltag.'}', $code, $rows[$i]);
}
}
// Replace Extensions
if (preg_match_all('/{([a-zA-Z0-9_]+):([^}]*)}/', $rows[$i], $var))
{
foreach ($var[2] as $tmpcnt => $tag)
{
$fulltag = $tag;
if ($delim = strpos($tag, ' &gt; '))
{
$tag = substr($tag, 0, $delim);
}
if (strpos($tag, ','))
{
list($tag, $addparam) = explode(',', $tag, 2);
}
$extension = $var[1][$tmpcnt];
if (substr($tag, 0, 4) == 'top.')
{
$title = $this->tip($vars[substr($tag, 4)]);
}
elseif ($lvl_block[$i] == 0)
{
$title = $this->tip($vars[$tag]);
}
else
{
$title = '[BLOCK?]';
}
$code = '<b title="' . $title . '">{' . $extension . ':' . $fulltag . '}</b>';
$rows[$i] = str_replace('{'.$extension . ':' . $fulltag .'}', $code, $rows[$i]);
}
}
// 'IF nnn' Blocks
if (preg_match_all('/&lt;!-- IF ([a-zA-Z0-9_.]+) --&gt;/', $rows[$i], $var))
{
foreach ($var[1] as $tag)
{
if (substr($tag, 0, 4) == 'top.')
{
$title = $this->tip($vars[substr($tag, 4)]);
}
elseif ($lvl_block[$i] == 0)
{
$title = $this->tip($vars[$tag]);
}
else
{
$title = '[BLOCK?]';
}
$code = '<span title="' . $title . '">&lt;!-- IF ' . $tag . ' --&gt;</span>';
$rows[$i] = str_replace("&lt;!-- IF $tag --&gt;", $code, $rows[$i]);
if ($title == '[NULL]')
{
$rows[$i] = str_replace('Hide', 'Show', $rows[$i]);
$rows[$i] = str_replace('block', 'none', $rows[$i]);
}
}
}
}
$page = join("<br>", $rows);
// Print Header
echo '<html><head><script type="text/javascript">
function toggleVisibility(el, src) {
var v = el.style.display == "block";
var str = src.innerHTML;
el.style.display = v ? "none" : "block";
src.innerHTML = v ? str.replace(/Hide/, "Show") : str.replace(/Show/, "Hide");}
</script></head><body>';
// Print Index
echo '<font face="Arial" Size="3"><b>';
echo 'SmartTemplate Debugger<br>';
echo '<font size="2"><li>PHP-Script: ' . $_SERVER['PATH_TRANSLATED'] . '</li><li>Template: ' . $this->filename . '</li></font><hr>';
echo '<li><a href="#template_code">Template</a></li>';
echo '<li><a href="#compiled_code">Compiled Template</a></li>';
echo '<li><a href="#data_code">Data</a></li>';
echo '</b></font><hr>';
// Print Template
echo '<a name="template_code"><br><font face="Arial" Size="3"><b>Template:</b>&nbsp;[<a href="javascript:void(\'\');" onclick="toggleVisibility(document.getElementById(\'Template\'), this); return false">Hide Ouptut</a>]</font><br>';
echo '<table border="0" cellpadding="4" cellspacing="1" width="100%" bgcolor="#C6D3EF"><tr><td bgcolor="#F0F0F0"><pre id="Template" style="display:block">';
echo $page;
echo '</pre></td></tr></table>';
// Print Compiled Template
if (@include_once ("class.smarttemplateparser.php"))
{
$parser = new SmartTemplateParser($this->filename);
$compiled = $parser->compile();
echo '<a name="compiled_code"><br><br><font face="Arial" Size="3"><b>Compiled Template:</b>&nbsp;[<a href="javascript:void(\'\');" onclick="toggleVisibility(document.getElementById(\'Compiled\'), this); return false">Hide Ouptut</a>]</font><br>';
echo '<table border="0" cellpadding="4" cellspacing="1" width="100%" bgcolor="#C6D3EF"><tr><td bgcolor="#F0F0F0"><pre id="Compiled" style="display:block">';
highlight_string($compiled);
echo '</pre></td></tr></table>';
}
else
{
exit( "SmartTemplate Error: Cannot find class.smarttemplateparser.php; check SmartTemplate installation");
}
// Print Data
echo '<a name="data_code"><br><br><font face="Arial" Size="3"><b>Data:</b>&nbsp;[<a href="javascript:void(\'\');" onclick="toggleVisibility(document.getElementById(\'Data\'), this); return false">Hide Ouptut</a>]</font><br>';
echo '<table border="0" cellpadding="4" cellspacing="1" width="100%" bgcolor="#C6D3EF"><tr><td bgcolor="#F0F0F0"><pre id="Data" style="display:block">';
echo $this->vardump($vars);
echo '</pre></td></tr></table></body></html>';
}
/**
* Insert Hide/Show Layer Switch
*
* @param string $suffix Additional Text
* @desc Insert Hide/Show Layer Switch
*/
function toggleview ( $suffix = '')
{
global $spancnt;
$spancnt++;
if ($suffix)
{
$suffix .= ':';
}
$ret = '[' . $suffix . '<a href="javascript:void(\'\');" onclick="toggleVisibility(document.getElementById(\'Block' . $spancnt . '\'), this); return false">Hide Block</a>]<span id="Block' . $spancnt . '" style="display:block">';
return $ret;
}
/**
* Create Title Text
*
* @param string $value Content
* @desc Create Title Text
*/
function tip ( $value )
{
if (empty($value))
{
return "[NULL]";
}
else
{
$ret = htmlentities(substr($value,0,200));
return $ret;
}
}
/**
* Recursive Variable Display Output
*
* @param mixed $var Content
* @param int $depth Incremented Indent Counter for Recursive Calls
* @return string Variable Content
* @access private
* @desc Recursive Variable Display Output
*/
function vardump($var, $depth = 0)
{
if (is_array($var))
{
$result = "Array (" . count($var) . ")<BR>";
foreach(array_keys($var) as $key)
{
$result .= $this->tab[$depth] . "<B>$key</B>: " . $this->vardump($var[$key], $depth+1);
}
return $result;
}
else
{
$ret = htmlentities($var) . "<BR>";
return $ret;
}
}
/**
* Splits Template-Style Variable Names into an Array-Name/Key-Name Components
*
* @param string $tag Variale Name used in Template
* @return array Array Name, Key Name
* @access private
* @desc Splits Template-Style Variable Names into an Array-Name/Key-Name Components
*/
function var_name($tag)
{
$parent_level = 0;
while (substr($tag, 0, 7) == 'parent.')
{
$tag = substr($tag, 7);
$parent_level++;
}
if (substr($tag, 0, 4) == 'top.')
{
$ret = array('_stack[0]', substr($tag,4));
return $ret;
}
elseif ($parent_level)
{
$ret = array('_stack[$_stack_cnt-'.$parent_level.']', $tag);
return $ret;
}
else
{
$ret = array('_obj', $tag);
return $ret;
}
}
/**
* Highlight HTML Source
*
* @param string $code HTML Source
* @return string Hightlighte HTML Source
* @access private
* @desc Highlight HTML Source
*/
function highlight_html ( $code )
{
$code = htmlentities($code);
$code = preg_replace('/([a-zA-Z_]+)=/', '<font color="#FF0000">$1=</font>', $code);
$code = preg_replace('/(&lt;[\/a-zA-Z0-9&;]+)/', '<font color="#0000FF">$1</font>', $code);
$code = str_replace('&lt;!--', '<font color="#008080">&lt;!--', $code);
$code = str_replace('--&gt;', '--&gt;</font>', $code);
$code = preg_replace('/[\r\n]+/', "\n", $code);
return $code;
}
}
?>

View File

@@ -0,0 +1,365 @@
<?php
/**
* SmartTemplateParser Class
* Used by SmartTemplate Class
*
* @desc Used by SmartTemplate Class
* @author Philipp v. Criegern philipp@criegern.com
* @author Manuel 'EndelWar' Dalla Lana endelwar@aregar.it
* @version 1.2.1 03.07.2006
*
* CVS ID: $Id: class.smarttemplateparser.php 2504 2011-12-28 07:35:29Z liu21st $
*/
class SmartTemplateParser
{
/**
* The template itself
*
* @access private
*/
var $template;
/**
* The template filename used to extract the dirname for subtemplates
*
* @access private
*/
var $template_dir;
/**
* List of used SmartTemplate Extensions
*
* @access private
*/
var $extension_tagged = array();
/**
* Error messages
*
* @access public
*/
var $error;
/**
* SmartTemplateParser Constructor
*
* @param string $template_filename HTML Template Filename
*/
function SmartTemplateParser ( $template_filename )
{
// Load Template
if ($hd = @fopen($template_filename, "r"))
{
if (filesize($template_filename))
{
$this->template = fread($hd, filesize($template_filename));
}
else
{
$this->template = "SmartTemplate Parser Error: File size is zero byte: '$template_filename'";
}
fclose($hd);
// Extract the name of the template directory
$this->template_dir = dirname($template_filename);
}
else
{
$this->template = "SmartTemplate Parser Error: File not found: '$template_filename'";
}
}
/**
* Main Template Parser
*
* @param string $compiled_template_filename Compiled Template Filename
* @desc Creates Compiled PHP Template
*/
function compile( $compiled_template_filename = '' )
{
if (empty($this->template))
{
return;
}
/* Quick hack to allow subtemplates */
if(eregi("<!-- INCLUDE", $this->template))
{
while ($this->count_subtemplates() > 0)
{
preg_match_all('/<!-- INCLUDE ([a-zA-Z0-9_.]+) -->/', $this->template, $tvar);
foreach($tvar[1] as $subfile)
{
if(file_exists($this->template_dir . "/$subfile"))
{
$subst = implode('',file($this->template_dir . "/$subfile"));
}
else
{
$subst = 'SmartTemplate Parser Error: Subtemplate not found: \''.$subfile.'\'';
}
$this->template = str_replace("<!-- INCLUDE $subfile -->", $subst, $this->template);
}
}
}
// END, ELSE Blocks
$page = preg_replace("/<!-- ENDIF.+?-->/", "<?php\n}\n?>", $this->template);
$page = preg_replace("/<!-- END[ a-zA-Z0-9_.]* -->/", "<?php\n}\n\$_obj=\$_stack[--\$_stack_cnt];}\n?>", $page);
$page = str_replace("<!-- ELSE -->", "<?php\n} else {\n?>", $page);
// 'BEGIN - END' Blocks
if (preg_match_all('/<!-- BEGIN ([a-zA-Z0-9_.]+) -->/', $page, $var))
{
foreach ($var[1] as $tag)
{
list($parent, $block) = $this->var_name($tag);
$code = "<?php\n"
. "if (!empty(\$$parent"."['$block'])){\n"
. "if (!is_array(\$$parent"."['$block']))\n"
. "\$$parent"."['$block']=array(array('$block'=>\$$parent"."['$block']));\n"
. "\$_tmp_arr_keys=array_keys(\$$parent"."['$block']);\n"
. "if (\$_tmp_arr_keys[0]!='0')\n"
. "\$$parent"."['$block']=array(0=>\$$parent"."['$block']);\n"
. "\$_stack[\$_stack_cnt++]=\$_obj;\n"
. "foreach (\$$parent"."['$block'] as \$rowcnt=>\$$block) {\n"
. "\$$block"."['ROWCNT']=\$rowcnt;\n"
. "\$$block"."['ALTROW']=\$rowcnt%2;\n"
. "\$$block"."['ROWBIT']=\$rowcnt%2;\n"
. "\$_obj=&\$$block;\n?>";
$page = str_replace("<!-- BEGIN $tag -->", $code, $page);
}
}
// 'IF nnn=mmm' Blocks
if (preg_match_all('/<!-- (ELSE)?IF ([a-zA-Z0-9_.]+)[ ]*([!=<>]+)[ ]*(["]?[^"]*["]?) -->/', $page, $var))
{
foreach ($var[2] as $cnt => $tag)
{
list($parent, $block) = $this->var_name($tag);
$cmp = $var[3][$cnt];
$val = $var[4][$cnt];
$else = ($var[1][$cnt] == 'ELSE') ? '} else' : '';
if ($cmp == '=')
{
$cmp = '==';
}
if (preg_match('/"([^"]*)"/',$val,$matches))
{
$code = "<?php\n$else"."if (\$$parent"."['$block'] $cmp \"".$matches[1]."\"){\n?>";
}
elseif (preg_match('/([^"]*)/',$val,$matches))
{
list($parent_right, $block_right) = $this->var_name($matches[1]);
$code = "<?php\n$else"."if (\$$parent"."['$block'] $cmp \$$parent_right"."['$block_right']){\n?>";
}
$page = str_replace($var[0][$cnt], $code, $page);
}
}
// 'IF nnn' Blocks
if (preg_match_all('/<!-- (ELSE)?IF ([a-zA-Z0-9_.]+) -->/', $page, $var))
{
foreach ($var[2] as $cnt => $tag)
{
$else = ($var[1][$cnt] == 'ELSE') ? '} else' : '';
list($parent, $block) = $this->var_name($tag);
$code = "<?php\n$else"."if (!empty(\$$parent"."['$block'])){\n?>";
$page = str_replace($var[0][$cnt], $code, $page);
}
}
// Replace Scalars
if (preg_match_all('/{([a-zA-Z0-9_. >]+)}/', $page, $var))
{
foreach ($var[1] as $fulltag)
{
// Determin Command (echo / $obj[n]=)
list($cmd, $tag) = $this->cmd_name($fulltag);
list($block, $skalar) = $this->var_name($tag);
$code = "<?php\n$cmd \$$block"."['$skalar'];\n?>\n";
$page = str_replace('{'.$fulltag.'}', $code, $page);
}
}
// ROSI Special: Replace Translations
if (preg_match_all('/<"([a-zA-Z0-9_.]+)">/', $page, $var))
{
foreach ($var[1] as $tag)
{
list($block, $skalar) = $this->var_name($tag);
$code = "<?php\necho gettext('$skalar');\n?>\n";
$page = str_replace('<"'.$tag.'">', $code, $page);
}
}
// Include Extensions
$header = '';
if (preg_match_all('/{([a-zA-Z0-9_]+):([^}]*)}/', $page, $var))
{
foreach ($var[2] as $cnt => $tag)
{
// Determin Command (echo / $obj[n]=)
list($cmd, $tag) = $this->cmd_name($tag);
$extension = $var[1][$cnt];
if (!isset($this->extension_tagged[$extension]))
{
$header .= "include_once \"smarttemplate_extensions/smarttemplate_extension_$extension.php\";\n";
$this->extension_tagged[$extension] = true;
}
if (!strlen($tag))
{
$code = "<?php\n$cmd smarttemplate_extension_$extension();\n?>\n";
}
elseif (substr($tag, 0, 1) == '"')
{
$code = "<?php\n$cmd smarttemplate_extension_$extension($tag);\n?>\n";
}
elseif (strpos($tag, ','))
{
list($tag, $addparam) = explode(',', $tag, 2);
list($block, $skalar) = $this->var_name($tag);
if (preg_match('/^([a-zA-Z_]+)/', $addparam, $match))
{
$nexttag = $match[1];
list($nextblock, $nextskalar) = $this->var_name($nexttag);
$addparam = substr($addparam, strlen($nexttag));
$code = "<?php\n$cmd smarttemplate_extension_$extension(\$$block"."['$skalar'],\$$nextblock"."['$nextskalar']"."$addparam);\n?>\n";
}
else
{
$code = "<?php\n$cmd smarttemplate_extension_$extension(\$$block"."['$skalar'],$addparam);\n?>\n";
}
}
else
{
list($block, $skalar) = $this->var_name($tag);
$code = "<?php\n$cmd smarttemplate_extension_$extension(\$$block"."['$skalar']);\n?>\n";
}
$page = str_replace($var[0][$cnt], $code, $page);
}
}
// Add Include Header
if (isset($header) && !empty($header))
{
$page = "<?php\n$header\n?>$page";
}
// Store Code to Temp Dir
if (strlen($compiled_template_filename))
{
if ($hd = fopen($compiled_template_filename, "w"))
{
fwrite($hd, $page);
fclose($hd);
return true;
}
else
{
$this->error = "Could not write compiled file.";
return false;
}
}
else
{
return $page;
}
}
/**
* Splits Template-Style Variable Names into an Array-Name/Key-Name Components
* {example} : array( "_obj", "example" ) -> $_obj['example']
* {example.value} : array( "_obj['example']", "value" ) -> $_obj['example']['value']
* {example.0.value} : array( "_obj['example'][0]", "value" ) -> $_obj['example'][0]['value']
* {top.example} : array( "_stack[0]", "example" ) -> $_stack[0]['example']
* {parent.example} : array( "_stack[$_stack_cnt-1]", "example" ) -> $_stack[$_stack_cnt-1]['example']
* {parent.parent.example} : array( "_stack[$_stack_cnt-2]", "example" ) -> $_stack[$_stack_cnt-2]['example']
*
* @param string $tag Variale Name used in Template
* @return array Array Name, Key Name
* @access private
* @desc Splits Template-Style Variable Names into an Array-Name/Key-Name Components
*/
function var_name($tag)
{
$parent_level = 0;
while (substr($tag, 0, 7) == 'parent.')
{
$tag = substr($tag, 7);
$parent_level++;
}
if (substr($tag, 0, 4) == 'top.')
{
$obj = '_stack[0]';
$tag = substr($tag,4);
}
elseif ($parent_level)
{
$obj = '_stack[$_stack_cnt-'.$parent_level.']';
}
else
{
$obj = '_obj';
}
while (is_int(strpos($tag, '.')))
{
list($parent, $tag) = explode('.', $tag, 2);
if (is_numeric($parent))
{
$obj .= "[" . $parent . "]";
}
else
{
$obj .= "['" . $parent . "']";
}
}
$ret = array($obj, $tag);
return $ret;
}
/**
* Determine Template Command from Variable Name
* {variable} : array( "echo", "variable" ) -> echo $_obj['variable']
* {variable > new_name} : array( "_obj['new_name']=", "variable" ) -> $_obj['new_name']= $_obj['variable']
*
* @param string $tag Variale Name used in Template
* @return array Array Command, Variable
* @access private
* @desc Determine Template Command from Variable Name
*/
function cmd_name($tag)
{
if (preg_match('/^(.+) > ([a-zA-Z0-9_.]+)$/', $tag, $tagvar))
{
$tag = $tagvar[1];
list($newblock, $newskalar) = $this->var_name($tagvar[2]);
$cmd = "\$$newblock"."['$newskalar']=";
}
else
{
$cmd = "echo";
}
$ret = array($cmd, $tag);
return $ret;
}
/**
* @return int Number of subtemplate included
* @access private
* @desc Count number of subtemplates included in current template
*/
function count_subtemplates()
{
preg_match_all('/<!-- INCLUDE ([a-zA-Z0-9_.]+) -->/', $this->template, $tvar);
$count_subtemplates = count($tvar[1]);
$ret = intval($count_subtemplates);
return $ret;
}
}
?>