PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/core/class/template.class.php

https://github.com/alin40404/FanweShare
PHP | 557 lines | 406 code | 52 blank | 99 comment | 32 complexity | 955558c61cacb1106b51274a96976977 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 方维购物分享网站系统 (Build on ThinkPHP)
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2011 http://fanwe.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. /**
  8. * template.class.php
  9. *
  10. * 模板处理类
  11. *
  12. * @package class
  13. * @author awfigq <awfigq@qq.com>
  14. */
  15. class Template {
  16. var $sub_templates = array();
  17. var $replace_code = array('search' => array(), 'replace' => array());
  18. var $blocks = array();
  19. var $language = array();
  20. var $file = '';
  21. /**
  22. * 解析模板并生成缓存文件
  23. * @param string $tpl_file 模板名称
  24. * @param string $tpl_dir 模板目录
  25. * @param string $file 模板路径
  26. * @param string $cache_file 模板缓存地址
  27. * @return void
  28. */
  29. function parseTemplate($tpl_file, $tpl_dir, $file, $cache_file)
  30. {
  31. $base_file = basename(FANWE_ROOT.$tpl_file, '.htm');
  32. $this->file = $file;
  33. if(!@$fp = fopen(FANWE_ROOT.$tpl_file, 'r'))
  34. {
  35. $tpl = $tpl_dir.'/'.$file.'.htm';
  36. $tpl_file = $tpl_file != $tpl ? $tpl.'", "'.$tpl_file : $tpl_file;
  37. $this->error('template_not_found', $tpl_file);
  38. }
  39. $template = @fread($fp, filesize(FANWE_ROOT.$tpl_file));
  40. fclose($fp);
  41. $var_regexp = "((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(\-\>)?[a-zA-Z0-9_\x7f-\xff]*)(\[[a-zA-Z0-9_\-\.\"\'\[\]\$\x7f-\xff]+\])*)";
  42. $const_regexp = "([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)";
  43. $this->sub_templates = array();
  44. for($i = 1; $i <= 3; $i++)
  45. {
  46. if(strExists($template, '{subtemplate'))
  47. {
  48. $template = preg_replace("/[\n\r\t]*(\<\!\-\-)?\{subtemplate\s+([a-z0-9_:\/]+)\}(\-\-\>)?[\n\r\t]*/ies", "\$this->loadSubTemplate('\\2','$tpl_dir')", $template);
  49. }
  50. }
  51. $template = preg_replace("/([\n\r]+)\t+/s", "\\1", $template);
  52. $template = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}", $template);
  53. $template = preg_replace("/\<\?php(.*?)\?\>/ies", "\$this->phpTags('\\1')", $template);
  54. $template = preg_replace("/[\n\r\t]*\{css\s+(.+?)\}[\n\r\t]*/ies", "\$this->cssTags('\\1')", $template);
  55. $template = preg_replace("/[\n\r\t]*\{script\s+(.+?)\}[\n\r\t]*/ies", "\$this->scriptTags('\\1')", $template);
  56. $template = preg_replace("/\{lang\s+(.+?)\}/ies", "\$this->languageVar('\\1')", $template);
  57. $template = preg_replace("/\{sprintf\s+(.+?)(?:|\s+(.+?))\}/ies", "\$this->sprintfTags('\\1','\\2')", $template);
  58. $template = preg_replace("/\{nl2br\s+(.+?)\}/ies", "\$this->nl2brTags('\\1')", $template);
  59. $template = preg_replace("/[\n\r\t]*\{advlayout\s+id=(\d+)(?:\scount=(\d+))*(?:\starget=(.+?))*\}[\n\r\t]*/is", "<!--dynamic advLayout args=\\1,\\2,\\3-->", $template);
  60. $template = preg_replace("/[\n\r\t]*\{advlayout\s+name='(.+?)'(?:\stmpl='(.+?)')*(?:\starget='(.+?)')*\}[\n\r\t]*/is", "<!--dynamic advLayoutName args=\\1,\\2,\\3-->", $template);
  61. $template = preg_replace("/[\n\r\t]*\{getimg\s+(.+?)\s+(.+?)\s+(.+?)(?:\s+(.+?))?\}[\n\r\t]*/ies", "\$this->getImgTags('\\1','\\2','\\3','\\4')", $template);
  62. $template = preg_replace("/[\n\r\t]*\{getuser\s+(.+?)\s+(\d+?)(?:\s+'(.*?)')?(?:\s+'(.*?)')?(?:\s+'(.*?)')?\}[\n\r\t]*/ies", "\$this->getUserTags('\\1','\\2','\\3','\\4','\\5')", $template);
  63. $template = preg_replace("/[\n\r\t]*\{getfollow\s+(.+?)\s+(.+?)\}[\n\r\t]*/is", "<!--getfollow \\1 \\2-->", $template);
  64. $template = preg_replace("/[\n\r\t]*\{date\s+(.+?)(?:|\s+'(.+?)')\}[\n\r\t]*/ie", "\$this->dateTags('\\1','\\2')", $template);
  65. $template = preg_replace("/[\n\r\t]*\{timelag\s+(.+?)\}[\n\r\t]*/ie", "\$this->timeLagTags('\\1')", $template);
  66. $template = preg_replace("/[\n\r\t]*\{avatar\s+(.+?)(?:\s+(.+?))?(?:\s+(.+?))?(?:\s+(.+?))?\}[\n\r\t]*/ies", "\$this->avatarTags('\\1','\\2','\\3','\\4')", $template);
  67. $template = preg_replace("/[\n\r\t]*\{cutstr\s+(.+?)\s+(.+?)(?:|\s(.+?))\}[\n\r\t]*/ies", "\$this->cutstrTags('\\1','\\2','\\3')", $template);
  68. $template = preg_replace("/[\n\r\t]*\{u\s+(.+?)(?:|\s+(.+?))\}[\n\r\t]*/ies", "\$this->uTags('\\1','\\2')", $template);
  69. $template = preg_replace("/[\n\r\t]*\{eval\s+(.+?)\s*\}[\n\r\t]*/ies", "\$this->evalTags('\\1')", $template);
  70. $template = str_replace("{LF}", "<?=\"\\n\"?>", $template);
  71. $template = preg_replace("/\{(\\\$[a-zA-Z0-9_\-\>\[\]\'\"\$\.\x7f-\xff]+)\}/s", "<?=\\1?>", $template);
  72. $template = preg_replace("/[\n\r\t]*\{dynamic\s+(.+?)\}[\n\r\t]*/is", "<!--dynamic \\1-->", $template);
  73. $template = preg_replace("/$var_regexp/es", "template::addQuote('<?=\\1?>')", $template);
  74. $template = preg_replace("/\<\?\=\<\?\=$var_regexp\?\>\?\>/es", "\$this->addQuote('<?=\\1?>')", $template);
  75. $header_add = '';
  76. if(!empty($this->sub_templates))
  77. {
  78. $header_add .= "\n0\n";
  79. foreach($this->sub_templates as $fname)
  80. {
  81. $header_add .= "|| checkTplRefresh('$tpl_file', '$fname', ".TIMESTAMP.", '$cache_file', '$tpl_dir', '$file')\n";
  82. }
  83. $header_add .= ';';
  84. }
  85. if(!empty($this->blocks))
  86. {
  87. //$header_add .= "\n";
  88. //$header_add .= "block_get('".implode(',', $this->blocks)."');";
  89. }
  90. $template = "<? if(!defined('IN_FANWE')) exit('Access Denied'); {$header_add}?>\n$template";
  91. $template = preg_replace("/[\n\r\t]*\{template\s+([a-z0-9_:\/]+)\}[\n\r\t]*/ies", "\$this->stripvTags('<? include template(\'\\1\'); ?>')", $template);
  92. $template = preg_replace("/[\n\r\t]*\{template\s+(.+?)\}[\n\r\t]*/ies", "\$this->stripvTags('<? include template(\'\\1\'); ?>')", $template);
  93. $template = preg_replace("/[\n\r\t]*\{echo\s+(.+?)\}[\n\r\t]*/ies", "\$this->stripvTags('<? echo \\1; ?>')", $template);
  94. $template = preg_replace("/([\n\r\t]*)\{if\s+(.+?)\}([\n\r\t]*)/ies", "\$this->stripvTags('\\1<? if(\\2) { ?>\\3')", $template);
  95. $template = preg_replace("/([\n\r\t]*)\{elseif\s+(.+?)\}([\n\r\t]*)/ies", "\$this->stripvTags('\\1<? } elseif(\\2) { ?>\\3')", $template);
  96. $template = preg_replace("/\{else\}/i", "<? } else { ?>", $template);
  97. $template = preg_replace("/\{\/if\}/i", "<? } ?>", $template);
  98. $template = preg_replace("/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\}[\n\r\t]*/ies", "\$this->stripvTags('<? if(is_array(\\1)) { foreach(\\1 as \\2) { ?>')", $template);
  99. $template = preg_replace("/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}[\n\r\t]*/ies", "\$this->stripvTags('<? if(is_array(\\1)) { foreach(\\1 as \\2 => \\3) { ?>')", $template);
  100. $template = preg_replace("/\{\/loop\}/i", "<? } } ?>", $template);
  101. $template = preg_replace("/\{$const_regexp\}/s", "<?=\\1?>", $template);
  102. if(!empty($this->replace_code))
  103. {
  104. $template = str_replace($this->replace_code['search'], $this->replace_code['replace'], $template);
  105. }
  106. $template = preg_replace("/ \?\>[\n\r]*\<\? /s", " ", $template);
  107. if(!@$fp = fopen(PUBLIC_ROOT.$cache_file, 'w'))
  108. {
  109. $this->error('directory_not_found', dirname(PUBLIC_ROOT.$cache_file));
  110. }
  111. //$template = preg_replace("/\"(http)?[\w\.\/:]+\?[^\"]+?&[^\"]+?\"/e", "\$this->transAmp('\\0')", $template);
  112. $template = preg_replace("/\<script[^\>]*?src=\"(.+?)\"(.*?)\>\s*\<\/script\>/ies", "\$this->stripScriptAmp('\\1', '\\2')", $template);
  113. //$template = preg_replace("/[\n\r\t]*\{block\s+([a-zA-Z0-9_\[\]]+)\}(.+?)\{\/block\}/ies", "\$this->stripBlock('\\1', '\\2')", $template);
  114. flock($fp, 2);
  115. fwrite($fp, $template);
  116. fclose($fp);
  117. }
  118. /**
  119. * 解析模板字符串
  120. * @param string $template 模板字符串
  121. * @return string
  122. */
  123. function parseString($template)
  124. {
  125. $var_regexp = "((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(\-\>)?[a-zA-Z0-9_\x7f-\xff]*)(\[[a-zA-Z0-9_\-\.\"\'\[\]\$\x7f-\xff]+\])*)";
  126. $const_regexp = "([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)";
  127. $template = preg_replace("/([\n\r]+)\t+/s", "\\1", $template);
  128. $template = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}", $template);
  129. $template = preg_replace("/\<\?php(.*?)\?\>/ies", "\$this->phpTags('\\1')", $template);
  130. $template = preg_replace("/[\n\r\t]*\{css\s+(.+?)\}[\n\r\t]*/ies", "\$this->cssTags('\\1')", $template);
  131. $template = preg_replace("/[\n\r\t]*\{script\s+(.+?)\}[\n\r\t]*/ies", "\$this->scriptTags('\\1')", $template);
  132. $template = preg_replace("/\{lang\s+(.+?)\}/ies", "\$this->languageVar('\\1')", $template);
  133. $template = preg_replace("/\{sprintf\s+(.+?)(?:|\s+(.+?))\}/ies", "\$this->sprintfTags('\\1','\\2')", $template);
  134. $template = preg_replace("/\{nl2br\s+(.+?)\}/ies", "\$this->nl2brTags('\\1')", $template);
  135. $template = preg_replace("/[\n\r\t]*\{getimg\s+(.+?)\s+(.+?)\s+(.+?)(?:\s+(.+?))?\}[\n\r\t]*/ies", "\$this->getImgTags('\\1','\\2','\\3','\\4')", $template);
  136. $template = preg_replace("/[\n\r\t]*\{getuser\s+(.+?)\s+(\d+?)(?:\s+'(.*?)')?(?:\s+'(.*?)')?(?:\s+'(.*?)')?\}[\n\r\t]*/ies", "\$this->getUserTags('\\1','\\2','\\3','\\4','\\5')", $template);
  137. $template = preg_replace("/[\n\r\t]*\{getfollow\s+(.+?)\s+(.+?)\}[\n\r\t]*/is", "<!--getfollow \\1 \\2-->", $template);
  138. $template = preg_replace("/[\n\r\t]*\{date\s+(.+?)(?:|\s+'(.+?)')\}[\n\r\t]*/ie", "\$this->dateTags('\\1','\\2')", $template);
  139. $template = preg_replace("/[\n\r\t]*\{timelag\s+(.+?)\}[\n\r\t]*/ie", "\$this->timeLagTags('\\1')", $template);
  140. $template = preg_replace("/[\n\r\t]*\{avatar\s+(.+?)(?:\s+(.+?))?(?:\s+(.+?))?(?:\s+(.+?))?\}[\n\r\t]*/ies", "\$this->avatarTags('\\1','\\2','\\3','\\4')", $template);
  141. $template = preg_replace("/[\n\r\t]*\{cutstr\s+(.+?)\s+(.+?)(?:|\s(.+?))\}[\n\r\t]*/ies", "\$this->cutstrTags('\\1','\\2','\\3')", $template);
  142. $template = preg_replace("/[\n\r\t]*\{u\s+(.+?)(?:|\s+(.+?))\}[\n\r\t]*/ies", "\$this->uTags('\\1','\\2')", $template);
  143. $template = preg_replace("/[\n\r\t]*\{echo\s+(.+?)\}[\n\r\t]*/ies", "\$this->stripvTags('<? echo \\1; ?>')", $template);
  144. $template = preg_replace("/[\n\r\t]*\{eval\s+(.+?)\s*\}[\n\r\t]*/ies", "\$this->evalTags('\\1')", $template);
  145. $template = str_replace("{LF}", "<?=\"\\n\"?>", $template);
  146. $template = preg_replace("/\{(\\\$[a-zA-Z0-9_\-\>\[\]\'\"\$\.\x7f-\xff]+)\}/s", "<?=\\1?>", $template);
  147. $template = preg_replace("/$var_regexp/es", "template::addQuote('<?=\\1?>')", $template);
  148. $template = preg_replace("/\<\?\=\<\?\=$var_regexp\?\>\?\>/es", "\$this->addQuote('<?=\\1?>')", $template);
  149. $template = preg_replace("/([\n\r\t]*)\{if\s+(.+?)\}([\n\r\t]*)/ies", "\$this->stripvTags('\\1<? if(\\2) { ?>\\3')", $template);
  150. $template = preg_replace("/([\n\r\t]*)\{elseif\s+(.+?)\}([\n\r\t]*)/ies", "\$this->stripvTags('\\1<? } elseif(\\2) { ?>\\3')", $template);
  151. $template = preg_replace("/\{else\}/i", "<? } else { ?>", $template);
  152. $template = preg_replace("/\{\/if\}/i", "<? } ?>", $template);
  153. $template = preg_replace("/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\}[\n\r\t]*/ies", "\$this->stripvTags('<? if(is_array(\\1)) foreach(\\1 as \\2) { ?>')", $template);
  154. $template = preg_replace("/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}[\n\r\t]*/ies", "\$this->stripvTags('<? if(is_array(\\1)) foreach(\\1 as \\2 => \\3) { ?>')", $template);
  155. $template = preg_replace("/\{\/loop\}/i", "<? } ?>", $template);
  156. $template = preg_replace("/\{$const_regexp\}/s", "<?=\\1?>", $template);
  157. if(!empty($this->replace_code))
  158. {
  159. $template = str_replace($this->replace_code['search'], $this->replace_code['replace'], $template);
  160. }
  161. $template = preg_replace("/ \?\>[\n\r]*\<\? /s", " ", $template);
  162. //$template = preg_replace("/\"(http)?[\w\.\/:]+\?[^\"]+?&[^\"]+?\"/e", "\$this->transAmp('\\0')", $template);
  163. $template = preg_replace("/\<script[^\>]*?src=\"(.+?)\"(.*?)\>\s*\<\/script\>/ies", "\$this->stripScriptAmp('\\1', '\\2')", $template);
  164. return $template;
  165. }
  166. /**
  167. * 解析模板中的语言标签
  168. * @param string $var 语言标签
  169. * @return string
  170. */
  171. function languageVar($var)
  172. {
  173. !isset($this->language['template']) && $this->language['template'] = array();
  174. $lang_var = &$this->language['template'];
  175. list($path,$name) = explode('/', $var);
  176. if(empty($name))
  177. {
  178. $name = $path;
  179. if(!isset($lang_var[$name]))
  180. {
  181. $lang = array();
  182. @include fimport('language/template');
  183. $this->language['template'] = array_merge($this->language['template'], $lang);
  184. if(@include(FANWE_ROOT.'tpl/'.TMPL.'/template.lang.php'))
  185. $this->language['template'] = array_merge($this->language['template'], $lang);
  186. }
  187. }
  188. if(!isset($lang_var[$name]))
  189. {
  190. $lang = array();
  191. @include fimport('language/'.$path);
  192. $this->language['template'] = array_merge($this->language['template'], $lang);
  193. }
  194. if(isset($lang_var[$name]))
  195. return $lang_var[$name];
  196. else
  197. return $var;
  198. }
  199. /**
  200. * 解析模板中的sprintf标签
  201. */
  202. function sprintfTags($lang,$args = '')
  203. {
  204. $lang = $this->languageVar($lang);
  205. if(!empty($args))
  206. {
  207. $temp = '';
  208. $args = explode(',',$args);
  209. $jg=',';
  210. foreach($args as $arg)
  211. {
  212. if(preg_match('/\$.+/i',$arg))
  213. $temp.= $jg.$arg;
  214. else
  215. $temp.= $jg.'"'.$arg.'"';
  216. }
  217. }
  218. else
  219. $temp='array()';
  220. $i = count($this->replace_code['search']);
  221. $this->replace_code['search'][$i] = $search = "<!--SPRINTF_TAG_$i-->";
  222. $this->replace_code['replace'][$i] = "<?php echo sprintf('$lang'$temp); ?>";
  223. return $search;
  224. }
  225. /**
  226. * 解析模板中的nl2br标签
  227. */
  228. function nl2brTags($var)
  229. {
  230. if(preg_match('/\$.+/i',$var))
  231. $var = $var;
  232. else
  233. $var = '"'.$var.'"';
  234. $i = count($this->replace_code['search']);
  235. $this->replace_code['search'][$i] = $search = "<!--SPRINTF_TAG_$i-->";
  236. $this->replace_code['replace'][$i] = "<?php echo nl2br($var); ?>";
  237. return $search;
  238. }
  239. /**
  240. * 解析模板中的广告位标签
  241. * @param string $id 广告位ID
  242. * @param string $count 显示数量
  243. * @param string $target 显示条件
  244. * @return string
  245. */
  246. function advLayoutTags($id,$count,$target)
  247. {
  248. return "<!--dynamic advLayout args=$id,$count,$target-->";
  249. }
  250. /**
  251. * 解析模板中的eval标签
  252. * @param string $php
  253. * @return string
  254. */
  255. function evalTags($php)
  256. {
  257. $php = str_replace('\"', '"', $php);
  258. $i = count($this->replace_code['search']);
  259. $this->replace_code['search'][$i] = $search = "<!--EVAL_TAG_$i-->";
  260. $this->replace_code['replace'][$i] = "<?php $php ?>";
  261. return $search;
  262. }
  263. /**
  264. * 解析模板中的php代码
  265. * @param string $php
  266. * @return string
  267. */
  268. function phpTags($php)
  269. {
  270. $i = count($this->replace_code['search']);
  271. $this->replace_code['search'][$i] = $search = "<!--PHP_TAG_$i-->";
  272. $this->replace_code['replace'][$i] = "<?php $php ?>";
  273. return $search;
  274. }
  275. function avatarTags($id,$type = 's',$code = '',$is_src = 0)
  276. {
  277. if(empty($code))
  278. $code = '"'.$code.'"';
  279. $is_src = intval($is_src);
  280. $i = count($this->replace_code['search']);
  281. $this->replace_code['search'][$i] = $search = "<!--AVATAR_TAG_$i-->";
  282. $this->replace_code['replace'][$i] = "<?php echo avatar($id,'$type',$code,$is_src);?>";
  283. return $search;
  284. }
  285. function cutstrTags($str,$len,$dot = '')
  286. {
  287. if(empty($dot))
  288. $dot = '...';
  289. $i = count($this->replace_code['search']);
  290. $this->replace_code['search'][$i] = $search = "<!--CUTSTR_TAG_$i-->";
  291. $this->replace_code['replace'][$i] = "<?php echo cutStr($str,$len,'$dot');?>";
  292. return $search;
  293. }
  294. /**
  295. * 解析模板中的date标签
  296. * @param string $php
  297. * @return string
  298. */
  299. function dateTags($time,$format = '')
  300. {
  301. $i = count($this->replace_code['search']);
  302. $this->replace_code['search'][$i] = $search = "<!--DATE_TAG_$i-->";
  303. if(empty($format))
  304. $this->replace_code['replace'][$i] = "<?php echo fToDate($time); ?>";
  305. else
  306. $this->replace_code['replace'][$i] = "<?php echo fToDate($time,'$format'); ?>";
  307. return $search;
  308. }
  309. /**
  310. * 解析模板中的timelag标签
  311. * @param string $php
  312. * @return string
  313. */
  314. function timeLagTags($time)
  315. {
  316. $i = count($this->replace_code['search']);
  317. $this->replace_code['search'][$i] = $search = "<!--TIME_LAG_TAG_$i-->";
  318. $this->replace_code['replace'][$i] = "<?php echo getBeforeTimelag($time); ?>";
  319. return $search;
  320. }
  321. /**
  322. * 解析模板中的getimg标签
  323. * @param string $php
  324. * @return string
  325. */
  326. function getImgTags($url,$width=0,$height=0,$gen=0)
  327. {
  328. if(preg_match('/\$.+/i',$width))
  329. $width = '"'.$width.'"';
  330. else
  331. $width = (int)$width;
  332. if(preg_match('/\$.+/i',$height))
  333. $height = '"'.$height.'"';
  334. else
  335. $height = (int)$height;
  336. $gen = (int)$gen;
  337. $i = count($this->replace_code['search']);
  338. $this->replace_code['search'][$i] = $search = "<!--GETIMG_TAG_$i-->";
  339. $this->replace_code['replace'][$i] = "<?php echo getImgName($url,$width,$height,$gen); ?>";
  340. return $search;
  341. }
  342. /**
  343. * 解析模板中的getuser标签
  344. * @return string
  345. */
  346. function getUserTags($uid,$is_mark,$type,$class,$tpl)
  347. {
  348. $user_type = 0;
  349. $is_mark = (int)$is_mark;
  350. $img_type = '';
  351. $img_size = '0';
  352. $link_class = '';
  353. $img_class = '';
  354. $tpl = trim($tpl);
  355. $type = trim($type);
  356. if(!empty($type))
  357. {
  358. $type = explode(',',$type);
  359. $img_type = $type[0];
  360. if(isset($type[1]) && !empty($type[1]))
  361. $img_size = $type[1];
  362. $user_type = 1;
  363. }
  364. $class = trim($class);
  365. if(!empty($class))
  366. {
  367. $class = explode(',',$class);
  368. $link_class = $class[0];
  369. if(isset($class[1]))
  370. $img_class = $class[1];
  371. }
  372. $i = count($this->replace_code['search']);
  373. $this->replace_code['search'][$i] = $search = "<!--GETUSER_TAG_$i-->";
  374. $this->replace_code['replace'][$i] = "<?php echo setTplUserFormat($uid,$user_type,$is_mark,'$img_type',$img_size,'$link_class','$img_class','$tpl'); ?>";
  375. return $search;
  376. }
  377. /**
  378. * 解析模板中的u标签
  379. * @param string $php
  380. * @return string
  381. */
  382. function uTags($url,$args = '')
  383. {
  384. if(!empty($args))
  385. {
  386. $temp = 'array(';
  387. $args = explode(',',$args);
  388. $jg='';
  389. foreach($args as $arg)
  390. {
  391. $arg = explode('=',$arg);
  392. if(preg_match('/\$.+/i',$arg[1]))
  393. $temp.= $jg.'"'.$arg[0].'"=>'.$arg[1];
  394. else
  395. $temp.= $jg.'"'.$arg[0].'"=>"'.$arg[1].'"';
  396. $jg=',';
  397. }
  398. $temp.=')';
  399. }
  400. else
  401. $temp='array()';
  402. $i = count($this->replace_code['search']);
  403. $this->replace_code['search'][$i] = $search = "<!--U_TAG_$i-->";
  404. $this->replace_code['replace'][$i] = "<?php echo FU('$url',$temp); ?>";
  405. return $search;
  406. }
  407. /**
  408. * 解析模板中的css标签
  409. * @param string $php
  410. * @return string
  411. */
  412. function cssTags($url)
  413. {
  414. $i = count($this->replace_code['search']);
  415. $this->replace_code['search'][$i] = $search = "<!--CSS_TAG_$i-->";
  416. $this->replace_code['replace'][$i] = "<?php echo cssParse($url); ?>";
  417. return $search;
  418. }
  419. /**
  420. * 解析模板中的script标签
  421. * @param string $php
  422. * @return string
  423. */
  424. function scriptTags($url)
  425. {
  426. $i = count($this->replace_code['search']);
  427. $this->replace_code['search'][$i] = $search = "<!--SCRIPT_TAG_$i-->";
  428. $this->replace_code['replace'][$i] = "<?php echo scriptParse($url); ?>";
  429. return $search;
  430. }
  431. /**
  432. * 加载子模板
  433. * @param string $file 模板文件
  434. * @param string $tpl_dir 模板目录
  435. * @return string
  436. */
  437. function loadSubTemplate($file,$tpl_dir)
  438. {
  439. $tpl_file = template($file,$tpl_dir, 1);
  440. if($content = @implode('', file(FANWE_ROOT.$tpl_file)))
  441. {
  442. $this->sub_templates[] = $tpl_file;
  443. return $content;
  444. }
  445. else
  446. {
  447. return '<!-- '.$file.' -->';
  448. }
  449. }
  450. function transAmp($str)
  451. {
  452. $str = str_replace('&', '&amp;', $str);
  453. $str = str_replace('&amp;amp;', '&amp;', $str);
  454. $str = str_replace('\"', '"', $str);
  455. return $str;
  456. }
  457. function addQuote($var)
  458. {
  459. return str_replace("\\\"", "\"", preg_replace("/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s", "['\\1']", $var));
  460. }
  461. function stripvTags($expr, $statement = '')
  462. {
  463. $expr = str_replace("\\\"", "\"", preg_replace("/\<\?\=(\\\$.+?)\?\>/s", "\\1", $expr));
  464. $statement = str_replace("\\\"", "\"", $statement);
  465. return $expr.$statement;
  466. }
  467. function stripScriptAmp($s, $extra)
  468. {
  469. $extra = str_replace('\\"', '"', $extra);
  470. $s = str_replace('&amp;', '&', $s);
  471. return "<script src=\"$s\" type=\"text/javascript\"$extra></script>";
  472. }
  473. function stripBlock($var, $s)
  474. {
  475. $s = str_replace('\\"', '"', $s);
  476. $s = preg_replace("/<\?=\\\$(.+?)\?>/", "{\$\\1}", $s);
  477. preg_match_all("/<\?=(.+?)\?>/e", $s, $constary);
  478. $constadd = '';
  479. $constary[1] = array_unique($constary[1]);
  480. foreach($constary[1] as $const) {
  481. $constadd .= '$__'.$const.' = '.$const.';';
  482. }
  483. $s = preg_replace("/<\?=(.+?)\?>/", "{\$__\\1}", $s);
  484. $s = str_replace('?>', "\n\$$var .= <<<EOF\n", $s);
  485. $s = str_replace('<?', "\nEOF;\n", $s);
  486. return "<?\n$constadd\$$var = <<<EOF\n".$s."\nEOF;\n?>";
  487. }
  488. function error($message, $tpl_name)
  489. {
  490. require_once fimport('class/error');
  491. FanweError::templateError($message, $tpl_name);
  492. }
  493. }
  494. ?>