Template.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. <?php
  2. class Template extends StarTemplate {
  3. var $StarTemplate_set;
  4. function __construct(){
  5. }
  6. function _init($template_Name){
  7. /* 设置模板引擎配置(数组) */
  8. $this->StarTemplate_set = array(
  9. /* 模板语法前后标示符 */
  10. 'left_tag' => '{',
  11. 'right_tag' => '}',
  12. /* 模板路径,以default为默认模板 (详细使用方法,请查看官方手册) */
  13. 'templateDir' => array('default' => 'template/'.$template_Name),
  14. /* 默认使用模板,此与模板路径键对应 */
  15. 'template_Name' => 'default',
  16. /* 模板文件后缀名 */
  17. 'templateExt' => '.html',
  18. /* 是否持续编译模板 (用于调试时用) */
  19. 'force_compile' => FALSE,
  20. /* 是否开启直接插入PHP代码 */
  21. 'PHP_off' => FALSE,
  22. /* 定义模板编译目录,结尾不要加斜杠 '/' */
  23. 'templateCompileDir' => 'data/cache/compile',
  24. /* 模板编译文件的后缀名 */
  25. 'templateCompileExt' => '.phpc',
  26. /* 是否使用输出缓存 */
  27. 'cache_is' => FALSE,
  28. /* 输出缓存标示符 默认为 当前URL 的MD5值 */
  29. /*
  30. * 此功能,在您在调用输出缓存时,需要指定的,以防止模板缓存重复,以影响您的程序输出
  31. * 可在使用时,自行定义
  32. *
  33. */
  34. 'cacheId' => md5($_SERVER['REQUEST_URI']),
  35. /* 输出缓存时间 单位秒 */
  36. 'cache_time' => 10,
  37. /* 输出缓存目录,结尾不要加斜杠 '/' */
  38. 'templateCacheDir' => 'data/cache/template',
  39. /* 输出缓存文件后缀名 */
  40. 'templateCacheExt' => '.phpo',
  41. /* 扩展功能(Function)插件存放路径,结尾不要加斜杠 '/' */
  42. 'templatePluginsDir' => 'StarTemplate_Plugins',
  43. /* 由本程序所创建的目录权限 代码 */
  44. 'dir_mode' => 0777,
  45. /* 被编译模板文件的大小限制 单位 M */
  46. 'file_max' => 1,
  47. /* 开启Gzip传输,提高传输速度 (此功能只在使用display是适用) */
  48. 'gzip_off' => false,
  49. /* 兼容选项,如果模板引擎输出空白,请开启此项 */
  50. 'compatible' => true,
  51. /* 此为调试时开启,可自动弹出一个窗口,窗口里为 StarTemplate 模板引擎的所有配置文件,包括注入的资源 (方便调试) */
  52. 'debug' => false,
  53. /* 是否存在已定义的 error_reporting */
  54. 'error_reporting' => false,
  55. 'classDir' => FCPATH
  56. /* 默认的提示语言为中文, 您可以编写简单的语言包,为此程序增加提示语言的可读性 */
  57. );
  58. $this->setConfig($this->StarTemplate_set);
  59. }
  60. }
  61. class StarTemplate
  62. {
  63. // 引擎选项
  64. private $arrayConfig = array();
  65. /* 模板编译信息提示 默认中文 ( 如果特定语言包存在,将自动读取 ) */
  66. public $StarTemplate_Class_Lang = array(
  67. ' 模板文件不存在或读取失败',
  68. ' 模板文件大小超出限制',
  69. ' 模板文件没有正常加载',
  70. '严重错误',
  71. '程序警告',
  72. '语法错误',
  73. '文件名称',
  74. '错误等级',
  75. '错误所在',
  76. '错误信息',
  77. '缓存文件路径',
  78. '错误源产生在',
  79. );
  80. // 类实例化 (进行数组设置)
  81. public function __construct($arrayConfig = array())
  82. {
  83. /* 获取当前类所在目录 */
  84. $this->arrayConfig['classDir'] = dirname(str_replace('\\','/',__FILE__));
  85. /* 载入配置 */
  86. $this->arrayConfig += $arrayConfig;
  87. /* 获取当前类名称 (防止类名称修改,导致报错机制失败) */
  88. $this->arrayConfig['ClassName'] = __CLASS__;
  89. /* 类被初始化时 自动读取语言包 (并判断语言包是否有效) */
  90. global $_StarTemplate_Class_Lang;
  91. if (is_array($_StarTemplate_Class_Lang))
  92. {
  93. $this->StarTemplate_Class_Lang += $_StarTemplate_Class_Lang;
  94. }
  95. /* 载入扩展功能文件 */
  96. $Plugins = $this->get_Template_Plugins();
  97. if (is_array($Plugins))
  98. {
  99. foreach ($Plugins as $p_path){ include $p_path; }
  100. }
  101. }
  102. /* 设置引擎 */
  103. public function setConfig($key, $value = null)
  104. {
  105. if (is_array($key))
  106. {
  107. $this->arrayConfig += $key;
  108. }else{
  109. $this->arrayConfig[$key] = $value;
  110. }
  111. }
  112. /* 获取当前模板引擎配置 */
  113. public function getConfig($key = null)
  114. {
  115. if ($key)
  116. {
  117. return $this->arrayConfig[$key];
  118. }
  119. return $this->arrayConfig;
  120. }
  121. /* 向模板引擎中注入变量 */
  122. public function assign($key,$val = null)
  123. {
  124. if (empty($key)) return '';
  125. if (is_array($key))
  126. {
  127. foreach ($key as $k=>$v)
  128. {
  129. $this->arrayConfig['GLOBALS'][$k] = $v;
  130. }
  131. }else{
  132. $this->arrayConfig['GLOBALS'][$key] = $val;
  133. }
  134. }
  135. /* 取得变量值 */
  136. private function & get_Value($key)
  137. {
  138. if (isset($this->arrayConfig['GLOBALS'][$key]))
  139. {
  140. return $this->arrayConfig['GLOBALS'][$key];
  141. }else{
  142. global $$key;
  143. if ($$key)
  144. {
  145. $this->assign($key,$$key);
  146. }
  147. return $$key;
  148. }
  149. }
  150. /* 取得模板路径 */
  151. private function get_Template_Path($templateName)
  152. {
  153. return $this->arrayConfig['templateDir'][(empty($this->arrayConfig['template_Name']) ? 'default' : $this->arrayConfig['template_Name'])].'/'.$templateName.$this->arrayConfig['templateExt'];
  154. }
  155. /* 获取模板编译路径 */
  156. private function get_Template_Compile_Path($templateName)
  157. {
  158. return $this->arrayConfig['classDir'].'/'.$this->arrayConfig['templateCompileDir'].'/'.md5($this->get_Template_Path($templateName)).$this->arrayConfig['templateCompileExt'];
  159. }
  160. /* 获取模板缓存路径 */
  161. private function get_Template_Cache_Path($templateName, $all = false)
  162. {
  163. if ($all)
  164. {
  165. if ($all === true)
  166. {
  167. $tmp_path = md5($templateName).'*';
  168. }else{
  169. $tmp_path = md5($templateName).$all;
  170. }
  171. }else{
  172. $tmp_path = md5($templateName).$this->arrayConfig['cacheId'];
  173. }
  174. return $this->arrayConfig['classDir'].'/'.$this->arrayConfig['templateCacheDir'].'/'.$tmp_path.$this->arrayConfig['templateCacheExt'];
  175. }
  176. /* 获取扩展功能文件列表 */
  177. private function get_Template_Plugins()
  178. {
  179. if (is_dir($this->arrayConfig['classDir'].'/'.$this->arrayConfig['templatePluginsDir'].'/'))
  180. {
  181. return glob($this->arrayConfig['classDir'].'/'.$this->arrayConfig['templatePluginsDir'].'/*.php');
  182. }
  183. }
  184. /* 判断缓存输出是否有效/是否开启缓存输出 */
  185. public function is_cached($templateName)
  186. {
  187. $_PATH = $this->get_Template_Cache_Path($templateName);
  188. if (!file_exists($_PATH))
  189. {
  190. return false;
  191. }elseif (filemtime($_PATH) + $this->arrayConfig['cache_time'] < time()){
  192. return false;
  193. }else{
  194. return true;
  195. }
  196. }
  197. /* 读取文件 */
  198. private function template_Read($PATH)
  199. {
  200. if (function_exists('file_get_contents'))
  201. {
  202. return file_get_contents($PATH);
  203. }else{
  204. $fopen = fopen($PATH,'r');
  205. $template_Content = '';
  206. do {
  207. $data = fread($fopen,1024);
  208. if (strlen($data)===0) break;
  209. $template_Content .= $data;
  210. }while(1);
  211. fclose($fopen);
  212. return $template_Content;
  213. }
  214. }
  215. /* 写入文件 */
  216. private function template_Write($PATH,$String)
  217. {
  218. /* 调用递归创建目录 */
  219. $this->template_CreateDir(dirname($PATH));
  220. /* 以写入方式打开文件句柄,开启 flock */
  221. $fopen = fopen($PATH,'w');
  222. flock($fopen, LOCK_EX + LOCK_NB);
  223. $fwrite = fwrite($fopen,$String);
  224. /* 失败重新尝试写入 */
  225. if (!$fwrite) $fwrite = fwrite($fopen,$String);
  226. flock($fopen, LOCK_UN + LOCK_NB);
  227. fclose($fopen);
  228. return $fwrite;
  229. }
  230. /* 循环创建目录 */
  231. private function template_CreateDir($Dir)
  232. {
  233. if (is_dir($Dir))
  234. return true;
  235. if (mkdir($Dir, $this->arrayConfig['dir_mode']))
  236. return true;
  237. if (!$this->template_CreateDir(dirname($Dir)))
  238. return false;
  239. return mkdir($Dir, $this->arrayConfig['dir_mode']);
  240. }
  241. public function getMicrotime()
  242. {
  243. list($microtime_1,$microtime_2) = explode(' ',microtime());
  244. return $microtime_1 + $microtime_2;
  245. }
  246. /* 输出模板 */
  247. public function display($templateName, $key = '',$Clean = 0)
  248. {
  249. $this->StarTemplate_Display($templateName,$Clean,'display',$key);
  250. }
  251. /* 返回输出模板 */
  252. public function fetch($templateName, $key = '')
  253. {
  254. $Clean = 0;
  255. return $this->StarTemplate_Display($templateName,$Clean,'output',$key);
  256. }
  257. /* 输出模板 */
  258. private function StarTemplate_Display($templateName,$Clean = 0,$display = '',$key = '')
  259. {
  260. /* 定义错误信息 */
  261. if (!$this->arrayConfig['error_reporting'])
  262. {
  263. $StarTemplate_old_err = error_reporting();
  264. error_reporting(E_ERROR | E_WARNING | E_PARSE);
  265. }
  266. /* 设定模板 */
  267. if ($key)
  268. {
  269. $tmp_key = $this->arrayConfig['template_Name'];
  270. $this->arrayConfig['template_Name'] = $key;
  271. }
  272. /* 引擎运行时间统计 */
  273. $this->arrayConfig['Runtime'] = $this->getMicrotime();
  274. if ($this->arrayConfig['cache_is'])
  275. $_PATH = $this->get_Template_Cache_Path($templateName);
  276. if (!$this->is_cached($templateName) || $Clean)
  277. {
  278. /* 将错误载入特定函数处理 */
  279. if ($this->arrayConfig['compatible'])
  280. {
  281. ob_start();
  282. }else{
  283. // 载入Debug类
  284. //$class_debug = new StarTemplate_debug($this);
  285. //ob_start(array($class_debug,'StarTemplate_xError'));
  286. }
  287. /* 进行模板编译 */
  288. include $this->StarTemplate_compile($templateName,$Clean);
  289. $StarTemplate_Content = ob_get_contents();
  290. ob_end_clean();
  291. }else{
  292. /* 读取缓存输出文件 */
  293. $StarTemplate_Content = $this->template_Read($_PATH);
  294. }
  295. /* 判断是否可以写入缓存输出内容 */
  296. if ($this->arrayConfig['cache_is'])
  297. {
  298. /* 判断缓存输出是否有效 */
  299. if (!$this->is_cached($templateName))
  300. $this->template_write($_PATH,$StarTemplate_Content);
  301. }
  302. if ($this->arrayConfig['debug'])
  303. {
  304. unset($this->arrayConfig['GLOBALS'][$this->arrayConfig['ClassName']]);
  305. ob_start(); print_r($this->arrayConfig); $debug = ob_get_contents(); ob_end_clean();
  306. $StarTemplate_Content .= '<div id="Me" style="display:none;">'.highlight_string($debug,1).'</div>'.'<script type="text/javascript">var code=document.getElementById("Me").innerHTML;var newwin=window.open("","","height=600 ,width=500,scrollbars=yes"); newwin.opener = null ;newwin.document.write(code); newwin.document.close();</script>';
  307. }
  308. switch ($display)
  309. {
  310. case 'display':
  311. echo $StarTemplate_Content;
  312. $StarTemplate_Content = null;
  313. break;
  314. }
  315. /* 返回执行时间 */
  316. $this->template_Runtime();
  317. if (isset($tmp_key)) $this->arrayConfig['template_Name'] = $tmp_key;
  318. if ($this->arrayConfig['gzip_off'] && ereg('gzip',$_SERVER['HTTP_ACCEPT_ENCODING']))
  319. {
  320. ob_start('ob_gzhandler');
  321. }
  322. /* 定义错误信息 */
  323. if (!$this->arrayConfig['error_reporting'])
  324. error_reporting($StarTemplate_old_err);
  325. return $StarTemplate_Content;
  326. }
  327. /*******************************************************************/
  328. /* 编译开始
  329. /*******************************************************************/
  330. /* 转换标示符 */
  331. private function ConverTag($Tag)
  332. {
  333. $_count = strlen($Tag);
  334. $new_array = array('{','}','[',']','$','(',')','*','+','.','?','\\','^','|');
  335. $Tag_ = '';
  336. for ($i=0;$i<$_count;$i++)
  337. {
  338. $Tag_ .= (in_array($Tag[$i],$new_array)?'\\':'').$Tag[$i];
  339. }
  340. return $Tag_;
  341. }
  342. /* 模板引擎编译 */
  343. private function StarTemplate_compile($templateName,$Clean = 0)
  344. {
  345. $_PATH = array();
  346. /* 取得有效模板路径 */
  347. $_PATH['From'] = $this->get_Template_Path($templateName);
  348. $_PATH['Save'] = $this->get_Template_Compile_Path($templateName);
  349. /* 判断模板文件是否存在 */
  350. if (!file_exists($_PATH['From'])) return $_PATH['From'].' {'.$templateName.$this->StarTemplate_Class_Lang[0].'}';
  351. /* 判断模板缓存文件是否需要更新 */
  352. if ($this->arrayConfig['force_compile']) $Clean = 1;
  353. if (!$Clean)
  354. {
  355. if (file_exists($_PATH['From']))
  356. $_fromt = filemtime($_PATH['From']);
  357. if (file_exists($_PATH['Save']))
  358. $_savet = filemtime($_PATH['Save']);
  359. if ($_fromt <= $_savet)
  360. {
  361. return $_PATH['Save'];
  362. }
  363. }
  364. /* 判断模板文件大小限制 */
  365. if (filesize($_PATH['From']) > $this->arrayConfig['file_max'] * 1024 * 1024) return $this->StarTemplate_Error($templateName.$this->StarTemplate_Class_Lang[1].' ('.$this->arrayConfig['file_max'].' M)');
  366. /* 取得有效标示 */
  367. $_Left = '(?<!!)'.$this->ConverTag($this->arrayConfig['left_tag']);
  368. $_Right = '((?<![!]))'.$this->ConverTag($this->arrayConfig['right_tag']);
  369. /* 取得模板源 */
  370. $StarTemplate_Conver = $this->template_read($_PATH['From']);
  371. /* 如果模板为空,不进行编译 */
  372. if (empty($StarTemplate_Conver))
  373. {
  374. $this->template_Write($_PATH['Save'],$StarTemplate_Conver);
  375. return $_PATH['Save'];
  376. }
  377. /* **************模板进行相关编译起始******************* */
  378. /*
  379. Start// write by xbantu 2009-06-06
  380. */
  381. $StarTemplate_Conver = trim($StarTemplate_Conver);
  382. preg_match_all('/'.$_Left.'Template (([\w|-|\/]{1,})|(\$([_a-zA-Z][\w]+)))'.$_Right.'/',$StarTemplate_Conver,$Include_);
  383. $Include_count = count($Include_[0]);
  384. /* 模板文件嵌套调用处理 */
  385. for ($i=0;$i< $Include_count;$i++)
  386. {
  387. /* 编译相应调入模板文件 */
  388. $StarTemplate_Conver = str_replace($Include_[0][$i],$this->arrayConfig['left_tag'].'eval include $this->StarTemplate_compile("'.$Include_[1][$i].'")'.$this->arrayConfig['right_tag'],$StarTemplate_Conver);
  389. /* 2009-06-07 放弃使用模板状态提示 */
  390. /*
  391. // 提示模板文件加载状态
  392. $Include_Tmp_Name = $this->get_StarTemplate_path($Include_[1][$i]);
  393. $StarTemplate_Conver = str_replace($Include_[0][$i],$this->StarTemplate_Left.' '.$Include_[1][$i].$this->StarTemplate_Class_Lang[2].$this->StarTemplate_Right,$StarTemplate_Conver);
  394. */
  395. }
  396. unset($Include_);
  397. /* 获取模板所使用变量 */
  398. preg_match_all('/\$([_a-zA-Z][\w]+)/',$StarTemplate_Conver,$Global_var);
  399. if (is_array($Global_var[1]))
  400. {
  401. $Global_var[1] = array_unique($Global_var[1]);
  402. $Global_var_Im = array('this','_GET','_POST','_COOKIE','_SERVER','_SESSION','_FILES','_ENV');
  403. $Global_var_out = '';
  404. foreach ($Global_var[1] as $val)
  405. {
  406. if (!in_array($val,$Global_var_Im))
  407. {
  408. $Global_var_out .= '$'.$val.' =& $this->get_Value(\''.$val.'\'); ';
  409. }
  410. }
  411. }
  412. /* 相关标签转换 */
  413. $Template_preg = array();
  414. $Template_Replace = array();
  415. /* 判断是否允许插入PHP */
  416. if ($this->arrayConfig['PHP_off'] === false)
  417. {
  418. $StarTemplate_Preg[] = '/<\?(=|php|)(.+?)\?>/is';
  419. $StarTemplate_Replace[] = '&lt;?\\1\\2?&gt;';
  420. }
  421. /*
  422. 此类编译的语法
  423. _if
  424. _elseif
  425. _else
  426. _for
  427. _while
  428. _foreach
  429. _eval
  430. _echo
  431. _print_r
  432. _变量输出
  433. */
  434. $StarTemplate_Preg[] = '/'.$_Left.'(else if|elseif) (.*?)'.$_Right.'/i';
  435. $StarTemplate_Preg[] = '/'.$_Left.'for (.*?)'.$_Right.'/i';
  436. $StarTemplate_Preg[] = '/'.$_Left.'while (.*?)'.$_Right.'/i';
  437. $StarTemplate_Preg[] = '/'.$_Left.'(loop|foreach) (.*?)'.$_Right.'/i';
  438. $StarTemplate_Preg[] = '/'.$_Left.'if (.*?)'.$_Right.'/i';
  439. $StarTemplate_Preg[] = '/'.$_Left.'else'.$_Right.'/i';
  440. $StarTemplate_Preg[] = '/'.$_Left."(eval|_)( |[\r\n])(.*?)".$_Right.'/is';
  441. $StarTemplate_Preg[] = '/'.$_Left.'_e (.*?)'.$_Right.'/is';
  442. $StarTemplate_Preg[] = '/'.$_Left.'_p (.*?)'.$_Right.'/i';
  443. $StarTemplate_Preg[] = '/'.$_Left.'\/(if|for|loop|foreach|eval|while)'.$_Right.'/i';
  444. $StarTemplate_Preg[] = '/'.$_Left.'((( *(\+\+|--) *)*?(([_a-zA-Z][\w]*\(.*?\))|\$((\w+)((\[|\()(\'|")?\$*\w*(\'|")?(\)|\]))*((->)?\$?(\w*)(\((\'|")?(.*?)(\'|")?\)|))){0,})( *\.?[^ \.]*? *)*?){1,})'.$_Right.'/i';
  445. $StarTemplate_Preg[] = "/( | ){0,}(\r\n){1,}\";/";
  446. $StarTemplate_Preg[] = '/'.$_Left.'(\#|\*)(.*?)(\#|\*)'.$_Right.'/';
  447. $StarTemplate_Preg[] = '/'.$_Left.'\%([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)'.$_Right.'/';
  448. /* 编译为相应的PHP文件语法 _所产生错误在运行时提示 */
  449. $StarTemplate_Replace[] = '<?php }else if (\\2){ ?>';
  450. $StarTemplate_Replace[] = '<?php for (\\1) { ?>';
  451. $StarTemplate_Replace[] = '<?php while (\\1) { ?>';
  452. $StarTemplate_Replace[] = '<?php foreach ((array)\\2) { $__i++; ?>';
  453. $StarTemplate_Replace[] = '<?php if (\\1){ ?>';
  454. $StarTemplate_Replace[] = '<?php }else{ ?>';
  455. $StarTemplate_Replace[] = '<?php \\3; ?>';
  456. $StarTemplate_Replace[] = '<?php echo \\1; ?>';
  457. $StarTemplate_Replace[] = '<?php print_r(\\1); ?>';
  458. $StarTemplate_Replace[] = '<?php } ?>';
  459. $StarTemplate_Replace[] = '<?php echo \\1;?>';
  460. $StarTemplate_Replace[] = '';
  461. $StarTemplate_Replace[] = '';
  462. $StarTemplate_Replace[] = '<?php echo $this->lang_array[\'\\1\'];?>';
  463. /* 在有必要时 开启 */
  464. //ksort($StarTemplate_Preg);
  465. //ksort($StarTemplate_Replace);
  466. /* 执行正则分析编译 */
  467. $StarTemplate_Conver=preg_replace($StarTemplate_Preg,$StarTemplate_Replace,$StarTemplate_Conver);
  468. /* 过滤敏感字符 */
  469. $StarTemplate_Conver = str_replace(array('!'.$this->arrayConfig['right_tag'],'!'.$this->arrayConfig['left_tag'],'?><?php'),array($this->arrayConfig['right_tag'],$this->arrayConfig['left_tag'],''),$StarTemplate_Conver);
  470. /* 整理输出缓存内容 */
  471. if ($Global_var_out)
  472. {
  473. $StarTemplate_Conver = "<?php $Global_var_out ?>\r\n".$StarTemplate_Conver;
  474. }
  475. $this->template_write($_PATH['Save'],$StarTemplate_Conver);
  476. /*
  477. End conver;
  478. */
  479. /* ***************模板进行相关编译结束******************** */
  480. return $_PATH['Save'];
  481. }
  482. /* 类错误信息输出 */
  483. private function StarTemplate_Error($Msg)
  484. {
  485. echo $Msg;
  486. /*exit;*/
  487. }
  488. /* 清理缓存输出 或缓存 */
  489. public function StarTemplate_clean($type = 'cache')
  490. {
  491. switch ($type)
  492. {
  493. /* 判断是否是输出缓存 */
  494. case 'cache':
  495. $_PATH = dirname($this->get_Template_Cache_Path('_'));
  496. $_END = $this->arrayConfig['templateCacheExt'];
  497. $_PATH_ = glob($_PATH.'/*'.$_END);
  498. break;
  499. /* 判断是否是模板缓存 */
  500. case 'compile':
  501. $_PATH = dirname($this->get_Template_Compile_Path('_'));
  502. $_END = $this->arrayConfig['templateCompileExt'];
  503. $_PATH_ = glob($_PATH.'/*'.$_END);
  504. break;
  505. }
  506. /* 判断是否是输出缓存 */
  507. if (!empty($type) && empty($_PATH))
  508. {
  509. echo $_PATH = $this->get_Template_Cache_Path($type, true);
  510. $_PATH_ = glob($_PATH);
  511. }
  512. if ($_PATH_)
  513. {
  514. if (is_array($_PATH_))
  515. {
  516. $j = 0;
  517. foreach ($_PATH_ as $val)
  518. {
  519. if (file_exists($val))
  520. {
  521. unlink($val);
  522. $j ++;
  523. }
  524. }
  525. return $j;
  526. }else{
  527. return false;
  528. }
  529. }
  530. return false;
  531. }
  532. /* 获取实时模板引擎运行时间 */
  533. public function template_Runtime()
  534. {
  535. /* 返回执行时间 */
  536. return $this->arrayConfig['Runtime'] = round($this->getMicrotime() - $this->arrayConfig['Runtime'],5);
  537. }
  538. /* 释放资源 */
  539. public function __destruct()
  540. {
  541. $this->arrayConfig = null;
  542. }
  543. //类结束
  544. }
  545. class StarTemplate_debug
  546. {
  547. private $thiss = null;
  548. /* 类实例化($this) */
  549. public function __construct($thiss)
  550. {
  551. $this->thiss = $thiss;
  552. }
  553. /* 模板错误输出 */
  554. public function StarTemplate_xError($_StarTemplate_error)
  555. {
  556. /* 获取最后一次错误记录 */
  557. $_StarTemplate_error_ = error_get_last();
  558. $_StarTemplate_error = array();
  559. /* 判断是否重要错误信息 */
  560. switch ($_StarTemplate_error_['type'])
  561. {
  562. case 1: $_StarTemplate_error['type'] = $this->thiss->StarTemplate_Class_Lang[3];
  563. case 2: $_StarTemplate_error['type'] = $this->thiss->StarTemplate_Class_Lang[4];
  564. case 4: $_StarTemplate_error['type'] = $this->thiss->StarTemplate_Class_Lang[5];
  565. default:
  566. $_StarTemplate_error['type'] = '1';
  567. }
  568. /* 错误信息格式化 */
  569. if ($_StarTemplate_error['type'])
  570. {
  571. $_StarTemplate_error['body'] = file($_StarTemplate_error_['file']);
  572. $_StarTemplate_error['err_'] = "<meta http-equiv=Content-Type content=\"text/html;charset=utf-8\"><font size=2 color=#333333>\r\n";
  573. $_StarTemplate_error['err_'] .= "[StarTemplate] <br /><br />\r\n";
  574. $_StarTemplate_error['err_'] .= $this->thiss->StarTemplate_Class_Lang[6].': '.str_replace($this->thiss->StarTemplate_Cache_End,'',basename($_StarTemplate_error_['file']))."<br />\r\n";
  575. $_StarTemplate_error['err_'] .= $this->thiss->StarTemplate_Class_Lang[7].': '.$_StarTemplate_error['type']."<br />\r\n";
  576. $_StarTemplate_error['err_'] .= $this->thiss->StarTemplate_Class_Lang[8].': '.htmlspecialchars($_StarTemplate_error['body'][$_StarTemplate_error_['line']-1])."<br />\r\n";
  577. $_StarTemplate_error['err_'] .= $this->thiss->StarTemplate_Class_Lang[9].': '.$_StarTemplate_error_['message']."<br /><br />\r\n";
  578. $_StarTemplate_error['err_'] .= "</font><font size=2 color=#999999>\r\n";
  579. $_StarTemplate_error['err_'] .= $this->thiss->StarTemplate_Class_Lang[10].': {SERVER_PATH}/'.$this->thiss->StarTemplate_Cache_Dir.'/'.basename($_StarTemplate_error_['file'])."<br />\r\n";
  580. $_StarTemplate_error['err_'] .= $this->thiss->StarTemplate_Class_Lang[11].': '.$_StarTemplate_error_['line']." {$this->thiss->StarTemplate_Class_Lang[12]}<br />\r\n";
  581. $_StarTemplate_error['err_'] .= "</font>\r\n";
  582. }
  583. return $_StarTemplate_error['err_'];
  584. }
  585. }