Template.class.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Think;
  12. /**
  13. * ThinkPHP内置模板引擎类
  14. * 支持XML标签和普通标签的模板解析
  15. * 编译型模板引擎 支持动态缓存
  16. */
  17. use Think\Hook as Hook;
  18. //use Think\Crypt\Driver\Think as Think;
  19. use Think\Storage as Storage;
  20. use Think\Think as Think;
  21. class Template
  22. {
  23. // 模板页面中引入的标签库列表
  24. protected $tagLib = array();
  25. // 当前模板文件
  26. protected $templateFile = '';
  27. // 模板变量
  28. public $tVar = array();
  29. public $config = array();
  30. private $literal = array();
  31. private $block = array();
  32. /**
  33. * 架构函数
  34. * @access public
  35. */
  36. public function __construct()
  37. {
  38. $this->config['cache_path'] = C('CACHE_PATH');
  39. $this->config['template_suffix'] = C('TMPL_TEMPLATE_SUFFIX');
  40. $this->config['cache_suffix'] = C('TMPL_CACHFILE_SUFFIX');
  41. $this->config['tmpl_cache'] = C('TMPL_CACHE_ON');
  42. $this->config['cache_time'] = C('TMPL_CACHE_TIME');
  43. $this->config['taglib_begin'] = $this->stripPreg(C('TAGLIB_BEGIN'));
  44. $this->config['taglib_end'] = $this->stripPreg(C('TAGLIB_END'));
  45. $this->config['tmpl_begin'] = $this->stripPreg(C('TMPL_L_DELIM'));
  46. $this->config['tmpl_end'] = $this->stripPreg(C('TMPL_R_DELIM'));
  47. $this->config['default_tmpl'] = C('TEMPLATE_NAME');
  48. $this->config['layout_item'] = C('TMPL_LAYOUT_ITEM');
  49. }
  50. private function stripPreg($str)
  51. {
  52. return str_replace(
  53. array('{', '}', '(', ')', '|', '[', ']', '-', '+', '*', '.', '^', '?'),
  54. array('\{', '\}', '\(', '\)', '\|', '\[', '\]', '\-', '\+', '\*', '\.', '\^', '\?'),
  55. $str);
  56. }
  57. // 模板变量获取和设置
  58. public function get($name)
  59. {
  60. if (isset($this->tVar[$name])) {
  61. return $this->tVar[$name];
  62. } else {
  63. return false;
  64. }
  65. }
  66. public function set($name, $value)
  67. {
  68. $this->tVar[$name] = $value;
  69. }
  70. /**
  71. * 加载模板
  72. * @access public
  73. * @param string $templateFile 模板文件
  74. * @param array $templateVar 模板变量
  75. * @param string $prefix 模板标识前缀
  76. * @return void
  77. */
  78. public function fetch($templateFile, $templateVar, $prefix = '')
  79. {
  80. $this->tVar = $templateVar;
  81. $templateCacheFile = $this->loadTemplate($templateFile, $prefix);
  82. Storage::load($templateCacheFile, $this->tVar, null, 'tpl');
  83. }
  84. /**
  85. * 加载主模板并缓存
  86. * @access public
  87. * @param string $templateFile 模板文件
  88. * @param string $prefix 模板标识前缀
  89. * @return string
  90. * @throws ThinkExecption
  91. */
  92. public function loadTemplate($templateFile, $prefix = '')
  93. {
  94. if (is_file($templateFile)) {
  95. $this->templateFile = $templateFile;
  96. // 读取模板文件内容
  97. $tmplContent = file_get_contents($templateFile);
  98. } else {
  99. $tmplContent = $templateFile;
  100. }
  101. // 根据模版文件名定位缓存文件
  102. $tmplCacheFile = $this->config['cache_path'] . $prefix . md5($templateFile) . $this->config['cache_suffix'];
  103. // 判断是否启用布局
  104. if (C('LAYOUT_ON')) {
  105. if (false !== strpos($tmplContent, '{__NOLAYOUT__}')) {
  106. // 可以单独定义不使用布局
  107. $tmplContent = str_replace('{__NOLAYOUT__}', '', $tmplContent);
  108. } else {
  109. // 替换布局的主体内容
  110. $layoutFile = THEME_PATH . C('LAYOUT_NAME') . $this->config['template_suffix'];
  111. // 检查布局文件
  112. if (!is_file($layoutFile)) {
  113. E(L('_TEMPLATE_NOT_EXIST_') . ':' . $layoutFile);
  114. }
  115. $tmplContent = str_replace($this->config['layout_item'], $tmplContent, file_get_contents($layoutFile));
  116. }
  117. }
  118. // 编译模板内容
  119. $tmplContent = $this->compiler($tmplContent);
  120. Storage::put($tmplCacheFile, trim($tmplContent), 'tpl');
  121. return $tmplCacheFile;
  122. }
  123. /**
  124. * 编译模板文件内容
  125. * @access protected
  126. * @param mixed $tmplContent 模板内容
  127. * @return string
  128. */
  129. protected function compiler($tmplContent)
  130. {
  131. //模板解析
  132. $tmplContent = $this->parse($tmplContent);
  133. // 还原被替换的Literal标签
  134. $tmplContent = preg_replace_callback('/<!--###literal(\d+)###-->/is', array($this, 'restoreLiteral'), $tmplContent);
  135. // 添加安全代码
  136. $tmplContent = '<?php if (!defined(\'THINK_PATH\')) exit();?>' . $tmplContent;
  137. // 优化生成的php代码
  138. $tmplContent = str_replace('?><?php', '', $tmplContent);
  139. // 模版编译过滤标签
  140. Hook::listen('template_filter', $tmplContent);
  141. return strip_whitespace($tmplContent);
  142. }
  143. /**
  144. * 模板解析入口
  145. * 支持普通标签和TagLib解析 支持自定义标签库
  146. * @access public
  147. * @param string $content 要解析的模板内容
  148. * @return string
  149. */
  150. public function parse($content)
  151. {
  152. // 内容为空不解析
  153. if (empty($content)) {
  154. return '';
  155. }
  156. $begin = $this->config['taglib_begin'];
  157. $end = $this->config['taglib_end'];
  158. // 检查include语法
  159. $content = $this->parseInclude($content);
  160. // 检查PHP语法
  161. $content = $this->parsePhp($content);
  162. // 首先替换literal标签内容
  163. $content = preg_replace_callback('/' . $begin . 'literal' . $end . '(.*?)' . $begin . '\/literal' . $end . '/is', array($this, 'parseLiteral'), $content);
  164. // 获取需要引入的标签库列表
  165. // 标签库只需要定义一次,允许引入多个一次
  166. // 一般放在文件的最前面
  167. // 格式:<taglib name="html,mytag..." />
  168. // 当TAGLIB_LOAD配置为true时才会进行检测
  169. if (C('TAGLIB_LOAD')) {
  170. $this->getIncludeTagLib($content);
  171. if (!empty($this->tagLib)) {
  172. // 对导入的TagLib进行解析
  173. foreach ($this->tagLib as $tagLibName) {
  174. $this->parseTagLib($tagLibName, $content);
  175. }
  176. }
  177. }
  178. // 预先加载的标签库 无需在每个模板中使用taglib标签加载 但必须使用标签库XML前缀
  179. if (C('TAGLIB_PRE_LOAD')) {
  180. $tagLibs = explode(',', C('TAGLIB_PRE_LOAD'));
  181. foreach ($tagLibs as $tag) {
  182. $this->parseTagLib($tag, $content);
  183. }
  184. }
  185. // 内置标签库 无需使用taglib标签导入就可以使用 并且不需使用标签库XML前缀
  186. $tagLibs = explode(',', C('TAGLIB_BUILD_IN'));
  187. foreach ($tagLibs as $tag) {
  188. $this->parseTagLib($tag, $content, true);
  189. }
  190. //解析普通模板标签 {$tagName}
  191. $content = preg_replace_callback('/(' . $this->config['tmpl_begin'] . ')([^\d\w\s' . $this->config['tmpl_begin'] . $this->config['tmpl_end'] . '].+?)(' . $this->config['tmpl_end'] . ')/is', array($this, 'parseTag'), $content);
  192. return $content;
  193. }
  194. // 检查PHP语法
  195. protected function parsePhp($content)
  196. {
  197. if (ini_get('short_open_tag')) {
  198. // 开启短标签的情况要将<?标签用echo方式输出 否则无法正常输出xml标识
  199. $content = preg_replace('/(<\?(?!php|=|$))/i', '<?php echo \'\\1\'; ?>' . "\n", $content);
  200. }
  201. // PHP语法检查
  202. if (C('TMPL_DENY_PHP') && false !== strpos($content, '<?php')) {
  203. E(L('_NOT_ALLOW_PHP_'));
  204. }
  205. return $content;
  206. }
  207. // 解析模板中的布局标签
  208. protected function parseLayout($content)
  209. {
  210. // 读取模板中的布局标签
  211. $find = preg_match('/' . $this->config['taglib_begin'] . 'layout\s(.+?)\s*?\/' . $this->config['taglib_end'] . '/is', $content, $matches);
  212. if ($find) {
  213. //替换Layout标签
  214. $content = str_replace($matches[0], '', $content);
  215. //解析Layout标签
  216. $array = $this->parseXmlAttrs($matches[1]);
  217. if (!C('LAYOUT_ON') || C('LAYOUT_NAME') != $array['name']) {
  218. // 读取布局模板
  219. $layoutFile = THEME_PATH . $array['name'] . $this->config['template_suffix'];
  220. $replace = isset($array['replace']) ? $array['replace'] : $this->config['layout_item'];
  221. // 替换布局的主体内容
  222. $content = str_replace($replace, $content, file_get_contents($layoutFile));
  223. }
  224. } else {
  225. $content = str_replace('{__NOLAYOUT__}', '', $content);
  226. }
  227. return $content;
  228. }
  229. // 解析模板中的include标签
  230. protected function parseInclude($content, $extend = true)
  231. {
  232. // 解析继承
  233. if ($extend) {
  234. $content = $this->parseExtend($content);
  235. }
  236. // 解析布局
  237. $content = $this->parseLayout($content);
  238. // 读取模板中的include标签
  239. $find = preg_match_all('/' . $this->config['taglib_begin'] . 'include\s(.+?)\s*?\/' . $this->config['taglib_end'] . '/is', $content, $matches);
  240. if ($find) {
  241. for ($i = 0; $i < $find; $i++) {
  242. $include = $matches[1][$i];
  243. $array = $this->parseXmlAttrs($include);
  244. $file = $array['file'];
  245. unset($array['file']);
  246. $content = str_replace($matches[0][$i], $this->parseIncludeItem($file, $array, $extend), $content);
  247. }
  248. }
  249. return $content;
  250. }
  251. // 解析模板中的extend标签
  252. protected function parseExtend($content)
  253. {
  254. $begin = $this->config['taglib_begin'];
  255. $end = $this->config['taglib_end'];
  256. // 读取模板中的继承标签
  257. $find = preg_match('/' . $begin . 'extend\s(.+?)\s*?\/' . $end . '/is', $content, $matches);
  258. if ($find) {
  259. //替换extend标签
  260. $content = str_replace($matches[0], '', $content);
  261. // 记录页面中的block标签
  262. preg_replace_callback('/' . $begin . 'block\sname=[\'"](.+?)[\'"]\s*?' . $end . '(.*?)' . $begin . '\/block' . $end . '/is', array($this, 'parseBlock'), $content);
  263. // 读取继承模板
  264. $array = $this->parseXmlAttrs($matches[1]);
  265. $content = $this->parseTemplateName($array['name']);
  266. $content = $this->parseInclude($content, false); //对继承模板中的include进行分析
  267. // 替换block标签
  268. $content = $this->replaceBlock($content);
  269. } else {
  270. $content = preg_replace_callback('/' . $begin . 'block\sname=[\'"](.+?)[\'"]\s*?' . $end . '(.*?)' . $begin . '\/block' . $end . '/is', function ($match) {return stripslashes($match[2]);}, $content);
  271. }
  272. return $content;
  273. }
  274. /**
  275. * 分析XML属性
  276. * @access private
  277. * @param string $attrs XML属性字符串
  278. * @return array
  279. */
  280. private function parseXmlAttrs($attrs)
  281. {
  282. $xml = '<tpl><tag ' . $attrs . ' /></tpl>';
  283. $xml = simplexml_load_string($xml);
  284. if (!$xml) {
  285. E(L('_XML_TAG_ERROR_'));
  286. }
  287. $xml = (array) ($xml->tag->attributes());
  288. $array = array_change_key_case($xml['@attributes']);
  289. return $array;
  290. }
  291. /**
  292. * 替换页面中的literal标签
  293. * @access private
  294. * @param string $content 模板内容
  295. * @return string|false
  296. */
  297. private function parseLiteral($content)
  298. {
  299. if (is_array($content)) {
  300. $content = $content[1];
  301. }
  302. if (trim($content) == '') {
  303. return '';
  304. }
  305. //$content = stripslashes($content);
  306. $i = count($this->literal);
  307. $parseStr = "<!--###literal{$i}###-->";
  308. $this->literal[$i] = $content;
  309. return $parseStr;
  310. }
  311. /**
  312. * 还原被替换的literal标签
  313. * @access private
  314. * @param string $tag literal标签序号
  315. * @return string|false
  316. */
  317. private function restoreLiteral($tag)
  318. {
  319. if (is_array($tag)) {
  320. $tag = $tag[1];
  321. }
  322. // 还原literal标签
  323. $parseStr = $this->literal[$tag];
  324. // 销毁literal记录
  325. unset($this->literal[$tag]);
  326. return $parseStr;
  327. }
  328. /**
  329. * 记录当前页面中的block标签
  330. * @access private
  331. * @param string $name block名称
  332. * @param string $content 模板内容
  333. * @return string
  334. */
  335. private function parseBlock($name, $content = '')
  336. {
  337. if (is_array($name)) {
  338. $content = $name[2];
  339. $name = $name[1];
  340. }
  341. $this->block[$name] = $content;
  342. return '';
  343. }
  344. /**
  345. * 替换继承模板中的block标签
  346. * @access private
  347. * @param string $content 模板内容
  348. * @return string
  349. */
  350. private function replaceBlock($content)
  351. {
  352. static $parse = 0;
  353. $begin = $this->config['taglib_begin'];
  354. $end = $this->config['taglib_end'];
  355. $reg = '/(' . $begin . 'block\sname=[\'"](.+?)[\'"]\s*?' . $end . ')(.*?)' . $begin . '\/block' . $end . '/is';
  356. if (is_string($content)) {
  357. do {
  358. $content = preg_replace_callback($reg, array($this, 'replaceBlock'), $content);
  359. } while ($parse && $parse--);
  360. return $content;
  361. } elseif (is_array($content)) {
  362. if (preg_match('/' . $begin . 'block\sname=[\'"](.+?)[\'"]\s*?' . $end . '/is', $content[3])) {
  363. //存在嵌套,进一步解析
  364. $parse = 1;
  365. $content[3] = preg_replace_callback($reg, array($this, 'replaceBlock'), "{$content[3]}{$begin}/block{$end}");
  366. return $content[1] . $content[3];
  367. } else {
  368. $name = $content[2];
  369. $content = $content[3];
  370. $content = isset($this->block[$name]) ? $this->block[$name] : $content;
  371. return $content;
  372. }
  373. }
  374. }
  375. /**
  376. * 搜索模板页面中包含的TagLib库
  377. * 并返回列表
  378. * @access public
  379. * @param string $content 模板内容
  380. * @return string|false
  381. */
  382. public function getIncludeTagLib(&$content)
  383. {
  384. //搜索是否有TagLib标签
  385. $find = preg_match('/' . $this->config['taglib_begin'] . 'taglib\s(.+?)(\s*?)\/' . $this->config['taglib_end'] . '\W/is', $content, $matches);
  386. if ($find) {
  387. //替换TagLib标签
  388. $content = str_replace($matches[0], '', $content);
  389. //解析TagLib标签
  390. $array = $this->parseXmlAttrs($matches[1]);
  391. $this->tagLib = explode(',', $array['name']);
  392. }
  393. return;
  394. }
  395. /**
  396. * TagLib库解析
  397. * @access public
  398. * @param string $tagLib 要解析的标签库
  399. * @param string $content 要解析的模板内容
  400. * @param boolean $hide 是否隐藏标签库前缀
  401. * @return string
  402. */
  403. public function parseTagLib($tagLib, &$content, $hide = false)
  404. {
  405. $begin = $this->config['taglib_begin'];
  406. $end = $this->config['taglib_end'];
  407. if (strpos($tagLib, '\\')) {
  408. // 支持指定标签库的命名空间
  409. $className = $tagLib;
  410. $tagLib = substr($tagLib, strrpos($tagLib, '\\') + 1);
  411. } else {
  412. $className = 'Think\\Template\TagLib\\' . ucwords($tagLib);
  413. }
  414. $tLib = \Think\Think::instance($className);
  415. $that = $this;
  416. foreach ($tLib->getTags() as $name => $val) {
  417. $tags = array($name);
  418. if (isset($val['alias'])) {
  419. // 别名设置
  420. $tags = explode(',', $val['alias']);
  421. $tags[] = $name;
  422. }
  423. $level = isset($val['level']) ? $val['level'] : 1;
  424. $closeTag = isset($val['close']) ? $val['close'] : true;
  425. foreach ($tags as $tag) {
  426. $parseTag = !$hide ? $tagLib . ':' . $tag : $tag; // 实际要解析的标签名称
  427. if (!method_exists($tLib, '_' . $tag)) {
  428. // 别名可以无需定义解析方法
  429. $tag = $name;
  430. }
  431. $n1 = empty($val['attr']) ? '(\s*?)' : '\s([^' . $end . ']*)';
  432. $this->tempVar = array($tagLib, $tag);
  433. if (!$closeTag) {
  434. $patterns = '/' . $begin . $parseTag . $n1 . '\/(\s*?)' . $end . '/is';
  435. $content = preg_replace_callback($patterns, function ($matches) use ($tLib, $tag, $that) {
  436. return $that->parseXmlTag($tLib, $tag, $matches[1], $matches[2]);
  437. }, $content);
  438. } else {
  439. $patterns = '/' . $begin . $parseTag . $n1 . $end . '(.*?)' . $begin . '\/' . $parseTag . '(\s*?)' . $end . '/is';
  440. for ($i = 0; $i < $level; $i++) {
  441. $content = preg_replace_callback($patterns, function ($matches) use ($tLib, $tag, $that) {
  442. return $that->parseXmlTag($tLib, $tag, $matches[1], $matches[2]);
  443. }, $content);
  444. }
  445. }
  446. }
  447. }
  448. }
  449. /**
  450. * 解析标签库的标签
  451. * 需要调用对应的标签库文件解析类
  452. * @access public
  453. * @param object $tagLib 标签库对象实例
  454. * @param string $tag 标签名
  455. * @param string $attr 标签属性
  456. * @param string $content 标签内容
  457. * @return string|false
  458. */
  459. public function parseXmlTag($tagLib, $tag, $attr, $content)
  460. {
  461. if (ini_get('magic_quotes_sybase')) {
  462. $attr = str_replace('\"', '\'', $attr);
  463. }
  464. $parse = '_' . $tag;
  465. $content = trim($content);
  466. $tags = $tagLib->parseXmlAttr($attr, $tag);
  467. return $tagLib->$parse($tags, $content);
  468. }
  469. /**
  470. * 模板标签解析
  471. * 格式: {TagName:args [|content] }
  472. * @access public
  473. * @param string $tagStr 标签内容
  474. * @return string
  475. */
  476. public function parseTag($tagStr)
  477. {
  478. if (is_array($tagStr)) {
  479. $tagStr = $tagStr[2];
  480. }
  481. //if (MAGIC_QUOTES_GPC) {
  482. $tagStr = stripslashes($tagStr);
  483. //}
  484. $flag = substr($tagStr, 0, 1);
  485. $flag2 = substr($tagStr, 1, 1);
  486. $name = substr($tagStr, 1);
  487. if ('$' == $flag && '.' != $flag2 && '(' != $flag2) {
  488. //解析模板变量 格式 {$varName}
  489. return $this->parseVar($name);
  490. } elseif ('-' == $flag || '+' == $flag) {
  491. // 输出计算
  492. return '<?php echo ' . $flag . $name . ';?>';
  493. } elseif (':' == $flag) {
  494. // 输出某个函数的结果
  495. return '<?php echo ' . $name . ';?>';
  496. } elseif ('~' == $flag) {
  497. // 执行某个函数
  498. return '<?php ' . $name . ';?>';
  499. } elseif (substr($tagStr, 0, 2) == '//' || (substr($tagStr, 0, 2) == '/*' && substr(rtrim($tagStr), -2) == '*/')) {
  500. //注释标签
  501. return '';
  502. }
  503. // 未识别的标签直接返回
  504. return C('TMPL_L_DELIM') . $tagStr . C('TMPL_R_DELIM');
  505. }
  506. /**
  507. * 模板变量解析,支持使用函数
  508. * 格式: {$varname|function1|function2=arg1,arg2}
  509. * @access public
  510. * @param string $varStr 变量数据
  511. * @return string
  512. */
  513. public function parseVar($varStr)
  514. {
  515. $varStr = trim($varStr);
  516. static $_varParseList = array();
  517. //如果已经解析过该变量字串,则直接返回变量值
  518. if (isset($_varParseList[$varStr])) {
  519. return $_varParseList[$varStr];
  520. }
  521. $parseStr = '';
  522. $varExists = true;
  523. if (!empty($varStr)) {
  524. $varArray = explode('|', $varStr);
  525. //取得变量名称
  526. $var = array_shift($varArray);
  527. if ('Think.' == substr($var, 0, 6)) {
  528. // 所有以Think.打头的以特殊变量对待 无需模板赋值就可以输出
  529. $name = $this->parseThinkVar($var);
  530. } elseif (false !== strpos($var, '.')) {
  531. //支持 {$var.property}
  532. $vars = explode('.', $var);
  533. $var = array_shift($vars);
  534. switch (strtolower(C('TMPL_VAR_IDENTIFY'))) {
  535. case 'array': // 识别为数组
  536. $name = '$' . $var;
  537. foreach ($vars as $key => $val) {
  538. $name .= '["' . $val . '"]';
  539. }
  540. break;
  541. case 'obj': // 识别为对象
  542. $name = '$' . $var;
  543. foreach ($vars as $key => $val) {
  544. $name .= '->' . $val;
  545. }
  546. break;
  547. default: // 自动判断数组或对象 只支持二维
  548. $name = 'is_array($' . $var . ')?$' . $var . '["' . $vars[0] . '"]:$' . $var . '->' . $vars[0];
  549. }
  550. } elseif (false !== strpos($var, '[')) {
  551. //支持 {$var['key']} 方式输出数组
  552. $name = "$" . $var;
  553. preg_match('/(.+?)\[(.+?)\]/is', $var, $match);
  554. $var = $match[1];
  555. } elseif (false !== strpos($var, ':') && false === strpos($var, '(') && false === strpos($var, '::') && false === strpos($var, '?')) {
  556. //支持 {$var:property} 方式输出对象的属性
  557. $vars = explode(':', $var);
  558. $var = str_replace(':', '->', $var);
  559. $name = "$" . $var;
  560. $var = $vars[0];
  561. } else {
  562. $name = "$$var";
  563. }
  564. //对变量使用函数
  565. if (count($varArray) > 0) {
  566. $name = $this->parseVarFunction($name, $varArray);
  567. }
  568. $parseStr = '<?php echo (' . $name . '); ?>';
  569. }
  570. $_varParseList[$varStr] = $parseStr;
  571. return $parseStr;
  572. }
  573. /**
  574. * 对模板变量使用函数
  575. * 格式 {$varname|function1|function2=arg1,arg2}
  576. * @access public
  577. * @param string $name 变量名
  578. * @param array $varArray 函数列表
  579. * @return string
  580. */
  581. public function parseVarFunction($name, $varArray)
  582. {
  583. //对变量使用函数
  584. $length = count($varArray);
  585. //取得模板禁止使用函数列表
  586. $template_deny_funs = explode(',', C('TMPL_DENY_FUNC_LIST'));
  587. for ($i = 0; $i < $length; $i++) {
  588. $args = explode('=', $varArray[$i], 2);
  589. //模板函数过滤
  590. $fun = trim($args[0]);
  591. switch ($fun) {
  592. case 'default': // 特殊模板函数
  593. $name = '(isset(' . $name . ') && (' . $name . ' !== ""))?(' . $name . '):' . $args[1];
  594. break;
  595. default: // 通用模板函数
  596. if (!in_array($fun, $template_deny_funs)) {
  597. if (isset($args[1])) {
  598. if (strstr($args[1], '###')) {
  599. $args[1] = str_replace('###', $name, $args[1]);
  600. $name = "$fun($args[1])";
  601. } else {
  602. $name = "$fun($name,$args[1])";
  603. }
  604. } else if (!empty($args[0])) {
  605. $name = "$fun($name)";
  606. }
  607. }
  608. }
  609. }
  610. return $name;
  611. }
  612. /**
  613. * 特殊模板变量解析
  614. * 格式 以 $Think. 打头的变量属于特殊模板变量
  615. * @access public
  616. * @param string $varStr 变量字符串
  617. * @return string
  618. */
  619. public function parseThinkVar($varStr)
  620. {
  621. $vars = explode('.', $varStr);
  622. $vars[1] = strtoupper(trim($vars[1]));
  623. $parseStr = '';
  624. if (count($vars) >= 3) {
  625. $vars[2] = trim($vars[2]);
  626. switch ($vars[1]) {
  627. case 'SERVER':
  628. $parseStr = '$_SERVER[\'' . strtoupper($vars[2]) . '\']';
  629. break;
  630. case 'GET':
  631. $parseStr = '$_GET[\'' . $vars[2] . '\']';
  632. break;
  633. case 'POST':
  634. $parseStr = '$_POST[\'' . $vars[2] . '\']';
  635. break;
  636. case 'COOKIE':
  637. if (isset($vars[3])) {
  638. $parseStr = '$_COOKIE[\'' . $vars[2] . '\'][\'' . $vars[3] . '\']';
  639. } else {
  640. $parseStr = 'cookie(\'' . $vars[2] . '\')';
  641. }
  642. break;
  643. case 'SESSION':
  644. if (isset($vars[3])) {
  645. $parseStr = '$_SESSION[\'' . $vars[2] . '\'][\'' . $vars[3] . '\']';
  646. } else {
  647. $parseStr = 'session(\'' . $vars[2] . '\')';
  648. }
  649. break;
  650. case 'ENV':
  651. $parseStr = '$_ENV[\'' . strtoupper($vars[2]) . '\']';
  652. break;
  653. case 'REQUEST':
  654. $parseStr = '$_REQUEST[\'' . $vars[2] . '\']';
  655. break;
  656. case 'CONST':
  657. $parseStr = strtoupper($vars[2]);
  658. break;
  659. case 'LANG':
  660. $parseStr = 'L("' . $vars[2] . '")';
  661. break;
  662. case 'CONFIG':
  663. if (isset($vars[3])) {
  664. $vars[2] .= '.' . $vars[3];
  665. }
  666. $parseStr = 'C("' . $vars[2] . '")';
  667. break;
  668. default:break;
  669. }
  670. } else if (count($vars) == 2) {
  671. switch ($vars[1]) {
  672. case 'NOW':
  673. $parseStr = "date('Y-m-d g:i a',time())";
  674. break;
  675. case 'VERSION':
  676. $parseStr = 'THINK_VERSION';
  677. break;
  678. case 'TEMPLATE':
  679. $parseStr = "'" . $this->templateFile . "'"; //'C("TEMPLATE_NAME")';
  680. break;
  681. case 'LDELIM':
  682. $parseStr = 'C("TMPL_L_DELIM")';
  683. break;
  684. case 'RDELIM':
  685. $parseStr = 'C("TMPL_R_DELIM")';
  686. break;
  687. default:
  688. if (defined($vars[1])) {
  689. $parseStr = $vars[1];
  690. }
  691. }
  692. }
  693. return $parseStr;
  694. }
  695. /**
  696. * 加载公共模板并缓存 和当前模板在同一路径,否则使用相对路径
  697. * @access private
  698. * @param string $tmplPublicName 公共模板文件名
  699. * @param array $vars 要传递的变量列表
  700. * @return string
  701. */
  702. private function parseIncludeItem($tmplPublicName, $vars = array(), $extend)
  703. {
  704. // 分析模板文件名并读取内容
  705. $parseStr = $this->parseTemplateName($tmplPublicName);
  706. // 替换变量
  707. foreach ($vars as $key => $val) {
  708. $parseStr = str_replace('[' . $key . ']', $val, $parseStr);
  709. }
  710. // 再次对包含文件进行模板分析
  711. return $this->parseInclude($parseStr, $extend);
  712. }
  713. /**
  714. * 分析加载的模板文件并读取内容 支持多个模板文件读取
  715. * @access private
  716. * @param string $tmplPublicName 模板文件名
  717. * @return string
  718. */
  719. private function parseTemplateName($templateName)
  720. {
  721. if (substr($templateName, 0, 1) == '$')
  722. //支持加载变量文件名
  723. {
  724. $templateName = $this->get(substr($templateName, 1));
  725. }
  726. $array = explode(',', $templateName);
  727. $parseStr = '';
  728. foreach ($array as $templateName) {
  729. if (empty($templateName)) {
  730. continue;
  731. }
  732. if (false === strpos($templateName, $this->config['template_suffix'])) {
  733. // 解析规则为 模块@主题/控制器/操作
  734. $templateName = T($templateName);
  735. }
  736. // 获取模板文件内容
  737. $parseStr .= file_get_contents($templateName);
  738. }
  739. return $parseStr;
  740. }
  741. }