<?php
// BSM Template Class v1.03
// By Dr.NP
// Create data: 11-26-2005
class BsmTpl {
var $classname = 'BsmTpl'; var $tpl_root_dir = 'templates/'; var $tpl_cache_root_dir = 'sitedata/template/'; var $tpl_dir = ''; var $tpl_file_ext = 'htm'; var $tpl_cache_file_ext = 'php'; var $tpl_lang_file_ext = 'lang'; var $tpl_static_file_ext = 'html'; var $tpl_name = 'default'; var $default_tpl_name = 'default'; var $default_db_handle = '$db'; var $default_lang = 'zh-cn'; function BsmTpl ($root_dir = '', $cache_root_dir = '') { if ($root_dir != '') $this->tpl_root_dir = $root_dir; if ($cache_root_dir != '') $this->tpl_cache_root_dir = $cache_root_dir; } function parse_template ($tplfile, $objfile) { $nest = 5; if (!@$fp = fopen ($tplfile, 'r')) { die ("Current template file '" . $tplfile. " ' not found or have no access!"); } $template = fread ($fp, filesize ($tplfile)); fclose ($fp); $var_regexp = "((\$[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*)([[a-zA-Z0-9_\"'$x7f-xff]+])*)"; $const_regexp = "([a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*)"; $template = preg_replace ("/s*{templates+(.+?)}s*/ies", "file_get_contents('{$this->tpl_dir}\1.{$this->tpl_file_ext}')", $template); $template = preg_replace ("/([nr]+)t+/s", "\1", $template); $template = preg_replace ("//s", "{\1}", $template); //$template = preg_replace ("/{langs+(.+?)}/ies", "languagevar('\1')", $template);
$template = str_replace ("{LF}", "=\"\n\"?>", $template); $template = preg_replace ("/{(\$[a-zA-Z0-9_[]'\"$x7f-xff]+)}/s", "=\1?>", $template); $template = preg_replace ("/$var_regexp/es", "addquote('=\1?>')", $template); $template = preg_replace ("/==$var_regexp?>?>/es", "addquote('=\1?>')", $template); $template = preg_replace ("/s*{evals+(.+?)}s*/ies", "stripvtags('nn', '')", $template); $template = preg_replace ("/s*{elseifs+(.+?)}s*/ies", "stripvtags('nn', '')", $template); $template = preg_replace ("/s*{else}s*/is", "nn", $template); $template = preg_replace ("/s*{dates+(.+?)s+(.+?)}s*/ies", "stripvtags('nn', '')", $template); for($i = 0; $i < $nest; $i++) { $template = preg_replace ("/s*{loops+(S+)s+(S+)}s*(.+?)s*{/loop}s*/ies", "stripvtags('n if(is_array(\1)) { foreach(\1 as \2) { ?>','n\3n } } ?>n')", $template); $template = preg_replace ("/s*{loops+(S+)s+(S+)s+(S+)}s*(.+?)s*{/loop}s*/ies", "stripvtags('n if(is_array(\1)) { foreach(\1 as \2 => \3) { ?>','n\4n } } ?>n')", $template); $template = preg_replace ("/s*{ifs+(.+?)}s*(.+?)s*{/if}s*/ies", "stripvtags('n if(\1) { ?>','n\2n } ?>n')", $template); } $template = preg_replace ("/{$const_regexp}/s", "=\1?>", $template); $template = preg_replace ("/ ?>[nr]* /s", " ", $template); >')/ies", $template, $arr); for ($i = 0; $i < sizeof ($arr[0]); $i++) { $template = str_replace ($arr[0][$i], '->parse(' . $arr[1][$i] . ')', $template); } */ $template = str_replace ('', '<?php echo time(); ?>', $template); $template = str_replace ('', '<?php echo ' . $this->default_db_handle . '->num_queries; ?>', $template); /* if (!@$fp = fopen($objfile, 'w')) { die ("Directory '" . $this->tpl_root . '/' . $this->tpl_cache_root . "' not found or have no access!"); } flock ($fp, 3); fwrite ($fp, $template); fclose ($fp); */ return $template; } function parse ($file) { global $page_time, $page_time_start; $tplfile = $this->tpl_dir . $file . '.' . $this->tpl_file_ext; $objfile = $this->tpl_cache_root_dir . '/' . $this->tpl_name . '_' . $file . '.tpl.' . $this->tpl_cache_file_ext; if (!file_exists($tplfile)) { $tplfile = $this->tpl_root_dir . '/' . $this->default_tpl_name . '/' . $file.'.'.$this->tpl_file_ext; $objfile = $this->tpl_cache_root_dir . '/' . $this->default_tpl_name . '_' . $file . '.tpl.' . $this->tpl_cache_file_ext; } if ((@filemtime($tplfile) > @filemtime($objfile)) || (!@file_exists($objfile))) { //$this->parse_template ($tplfile, $objfile); if (!@$fp = fopen($objfile, 'w')) { die ("Directory '" . $this->tpl_root . '/' . $this->tpl_cache_root . "' not found or have no access!"); } flock ($fp, 3); fwrite ($fp, $this->parse_template ($tplfile, $objfile)); fclose ($fp); } list ($usec, $sec) = explode (" ", microtime ()); $page_time_end = $usec + $sec; $page_time = sprintf ("%0.6f", $page_time_end - $page_time_start); return $objfile; } function set_tpl_db_handle ($dh) { $this->defalt_db_handle = '$' . $dh; } function set_tpl_name ($name) { $tpl_dir = $this->tpl_root_dir . '/' . $name . '/'; if ($name != '' && is_dir ($tpl_dir)) { $this->tpl_name = $name; $this->tpl_dir = str_replace ('//', '/', $tpl_dir); } else { $this->tpl_name = $this->default_tpl_name; $this->tpl_dir = str_replace ('//', '/', $this->tpl_root_dir . '/' . $this->tpl_name . '/'); } } function set_language ($langMeta) { $langFile = $this->tpl_dir . $langMeta . '.lang'; clearstatcache (); if (@is_readable ($langFile)) { return $langFile; } elseif (@is_readable ($this->tpl_dir . $this->default_lang . '.' . $this->tpl_lang_file_ext)) { $langFile = $this->tpl_dir . $this->default_lang . '.' . $this->tpl_lang_file_ext; $langMeta = $this->default_lang; return $langFile; } elseif (@is_readable ($this->tpl_root_dir . '/' . $this->default_tpl_name . '/' . $langMeta . '.lang')) { $langFile = $this->tpl_root_dir . '/' . $this->default_tpl_name . '/' . $langMeta . '.lang'; return $langFile; } elseif (@is_readable ($this->tpl_root_dir . '/' . $this->default_tpl_name . '/' . $this->default_lang . '.' . $this->tpl_lang_file_ext)) { $langFile = $this->tpl_root_dir . '/' . $this->default_tpl_name . '/' . $this->default_lang . '.' . $this->tpl_lang_file_ext; $langMeta = $this->default_lang; return $langFile; } else die ('Accept Langfile:' . $langFile . ' did not exist or has no access!'); } function dsp () { global $mSession; if ($mSession['do_gzip']) { $gzip_contents = ob_get_contents (); ob_end_clean (); $gzip_size = strlen ($gzip_contents); $gzip_crc = crc32 ($gzip_contents); $gzip_contents = gzcompress ($gzip_contents, 9); $gzip_contents = substr ($gzip_contents, 0, strlen ($gzip_contents) - 4); echo "x1fx8bx08x00x00x00x00x00"; echo $gzip_contents; echo pack ('V', $gzip_crc); echo pack ('V', $gzip_size); } else ob_end_flush (); } function get_static_html ($file) { // Just for Test... $static_file = '../../data/static/' . $file . '.' . $this->tpl_static_file_ext; if (@$fp = fopen ($static_file, 'wb')) { fwrite ($fp, ob_get_contents ()); fclose ($fp); } ob_end_clean (); return; } }
function addquote ($var) { return str_replace("\\"", """, preg_replace("/[([a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*)]/s", "['\1']", $var)); } function stripvtags ($expr, $statement) { $expr = str_replace("\\"", """, preg_replace("/=(\$[a-zA-Z_x7f-xff][a-zA-Z0-9_[]\"'x7f-xff]*)?>/s", "\1", $expr)); $statement = str_replace("\\"", ""
|