赛亿官网

Template.php 45KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 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. use think\exception\TemplateNotFoundException;
  13. /**
  14. * ThinkPHP分离出来的模板引擎
  15. * 支持XML标签和普通标签的模板解析
  16. * 编译型模板引擎 支持动态缓存
  17. */
  18. class Template
  19. {
  20. // 模板变量
  21. protected $data = [];
  22. // 引擎配置
  23. protected $config = [
  24. 'view_path' => '', // 模板路径
  25. 'view_base' => '',
  26. 'view_suffix' => 'html', // 默认模板文件后缀
  27. 'view_depr' => DS,
  28. 'cache_suffix' => 'php', // 默认模板缓存后缀
  29. 'tpl_deny_func_list' => 'echo,exit', // 模板引擎禁用函数
  30. 'tpl_deny_php' => false, // 默认模板引擎是否禁用PHP原生代码
  31. 'tpl_begin' => '{', // 模板引擎普通标签开始标记
  32. 'tpl_end' => '}', // 模板引擎普通标签结束标记
  33. 'strip_space' => false, // 是否去除模板文件里面的html空格与换行
  34. 'tpl_cache' => true, // 是否开启模板编译缓存,设为false则每次都会重新编译
  35. 'compile_type' => 'file', // 模板编译类型
  36. 'cache_prefix' => '', // 模板缓存前缀标识,可以动态改变
  37. 'cache_time' => 0, // 模板缓存有效期 0 为永久,(以数字为值,单位:秒)
  38. 'layout_on' => false, // 布局模板开关
  39. 'layout_name' => 'layout', // 布局模板入口文件
  40. 'layout_item' => '{__CONTENT__}', // 布局模板的内容替换标识
  41. 'taglib_begin' => '{', // 标签库标签开始标记
  42. 'taglib_end' => '}', // 标签库标签结束标记
  43. 'taglib_load' => true, // 是否使用内置标签库之外的其它标签库,默认自动检测
  44. 'taglib_build_in' => 'cx', // 内置标签库名称(标签使用不必指定标签库名称),以逗号分隔 注意解析顺序
  45. 'taglib_pre_load' => '', // 需要额外加载的标签库(须指定标签库名称),多个以逗号分隔
  46. 'display_cache' => false, // 模板渲染缓存
  47. 'cache_id' => '', // 模板缓存ID
  48. 'tpl_replace_string' => [],
  49. 'tpl_var_identify' => 'array', // .语法变量识别,array|object|'', 为空时自动识别
  50. ];
  51. private $literal = [];
  52. private $includeFile = []; // 记录所有模板包含的文件路径及更新时间
  53. protected $storage;
  54. /**
  55. * 构造函数
  56. * @access public
  57. */
  58. public function __construct(array $config = [])
  59. {
  60. $this->config['cache_path'] = TEMP_PATH;
  61. $this->config = array_merge($this->config, $config);
  62. $this->config['taglib_begin'] = $this->stripPreg($this->config['taglib_begin']);
  63. $this->config['taglib_end'] = $this->stripPreg($this->config['taglib_end']);
  64. $this->config['tpl_begin'] = $this->stripPreg($this->config['tpl_begin']);
  65. $this->config['tpl_end'] = $this->stripPreg($this->config['tpl_end']);
  66. // 初始化模板编译存储器
  67. $type = $this->config['compile_type'] ? $this->config['compile_type'] : 'File';
  68. $class = false !== strpos($type, '\\') ? $type : '\\think\\template\\driver\\' . ucwords($type);
  69. $this->storage = new $class();
  70. }
  71. /**
  72. * 字符串替换 避免正则混淆
  73. * @access private
  74. * @param string $str
  75. * @return string
  76. */
  77. private function stripPreg($str)
  78. {
  79. return str_replace(
  80. ['{', '}', '(', ')', '|', '[', ']', '-', '+', '*', '.', '^', '?'],
  81. ['\{', '\}', '\(', '\)', '\|', '\[', '\]', '\-', '\+', '\*', '\.', '\^', '\?'],
  82. $str);
  83. }
  84. /**
  85. * 模板变量赋值
  86. * @access public
  87. * @param mixed $name
  88. * @param mixed $value
  89. * @return void
  90. */
  91. public function assign($name, $value = '')
  92. {
  93. if (is_array($name)) {
  94. $this->data = array_merge($this->data, $name);
  95. } else {
  96. $this->data[$name] = $value;
  97. }
  98. }
  99. /**
  100. * 模板引擎参数赋值
  101. * @access public
  102. * @param mixed $name
  103. * @param mixed $value
  104. */
  105. public function __set($name, $value)
  106. {
  107. $this->config[$name] = $value;
  108. }
  109. /**
  110. * 模板引擎配置项
  111. * @access public
  112. * @param array|string $config
  113. * @return void|array
  114. */
  115. public function config($config)
  116. {
  117. if (is_array($config)) {
  118. $this->config = array_merge($this->config, $config);
  119. } elseif (isset($this->config[$config])) {
  120. return $this->config[$config];
  121. } else {
  122. return;
  123. }
  124. }
  125. /**
  126. * 模板变量获取
  127. * @access public
  128. * @param string $name 变量名
  129. * @return mixed
  130. */
  131. public function get($name = '')
  132. {
  133. if ('' == $name) {
  134. return $this->data;
  135. } else {
  136. $data = $this->data;
  137. foreach (explode('.', $name) as $key => $val) {
  138. if (isset($data[$val])) {
  139. $data = $data[$val];
  140. } else {
  141. $data = null;
  142. break;
  143. }
  144. }
  145. return $data;
  146. }
  147. }
  148. /**
  149. * 渲染模板文件
  150. * @access public
  151. * @param string $template 模板文件
  152. * @param array $vars 模板变量
  153. * @param array $config 模板参数
  154. * @return void
  155. */
  156. public function fetch($template, $vars = [], $config = [])
  157. {
  158. if ($vars) {
  159. $this->data = $vars;
  160. }
  161. if ($config) {
  162. $this->config($config);
  163. }
  164. if (!empty($this->config['cache_id']) && $this->config['display_cache']) {
  165. // 读取渲染缓存
  166. $cacheContent = Cache::get($this->config['cache_id']);
  167. if (false !== $cacheContent) {
  168. echo $cacheContent;
  169. return;
  170. }
  171. }
  172. $template = $this->parseTemplateFile($template);
  173. if ($template) {
  174. $cacheFile = $this->config['cache_path'] . $this->config['cache_prefix'] . md5($template) . '.' . ltrim($this->config['cache_suffix'], '.');
  175. if (!$this->checkCache($cacheFile)) {
  176. // 缓存无效 重新模板编译
  177. $content = file_get_contents($template);
  178. $this->compiler($content, $cacheFile);
  179. }
  180. // 页面缓存
  181. ob_start();
  182. ob_implicit_flush(0);
  183. // 读取编译存储
  184. $this->storage->read($cacheFile, $this->data);
  185. // 获取并清空缓存
  186. $content = ob_get_clean();
  187. if (!empty($this->config['cache_id']) && $this->config['display_cache']) {
  188. // 缓存页面输出
  189. Cache::set($this->config['cache_id'], $content, $this->config['cache_time']);
  190. }
  191. echo $content;
  192. }
  193. }
  194. /**
  195. * 渲染模板内容
  196. * @access public
  197. * @param string $content 模板内容
  198. * @param array $vars 模板变量
  199. * @param array $config 模板参数
  200. * @return void
  201. */
  202. public function display($content, $vars = [], $config = [])
  203. {
  204. if ($vars) {
  205. $this->data = $vars;
  206. }
  207. if ($config) {
  208. $this->config($config);
  209. }
  210. $cacheFile = $this->config['cache_path'] . $this->config['cache_prefix'] . md5($content) . '.' . ltrim($this->config['cache_suffix'], '.');
  211. if (!$this->checkCache($cacheFile)) {
  212. // 缓存无效 模板编译
  213. $this->compiler($content, $cacheFile);
  214. }
  215. // 读取编译存储
  216. $this->storage->read($cacheFile, $this->data);
  217. }
  218. /**
  219. * 设置布局
  220. * @access public
  221. * @param mixed $name 布局模板名称 false 则关闭布局
  222. * @param string $replace 布局模板内容替换标识
  223. * @return object
  224. */
  225. public function layout($name, $replace = '')
  226. {
  227. if (false === $name) {
  228. // 关闭布局
  229. $this->config['layout_on'] = false;
  230. } else {
  231. // 开启布局
  232. $this->config['layout_on'] = true;
  233. // 名称必须为字符串
  234. if (is_string($name)) {
  235. $this->config['layout_name'] = $name;
  236. }
  237. if (!empty($replace)) {
  238. $this->config['layout_item'] = $replace;
  239. }
  240. }
  241. return $this;
  242. }
  243. /**
  244. * 检查编译缓存是否有效
  245. * 如果无效则需要重新编译
  246. * @access private
  247. * @param string $cacheFile 缓存文件名
  248. * @return boolean
  249. */
  250. private function checkCache($cacheFile)
  251. {
  252. // 未开启缓存功能
  253. if (!$this->config['tpl_cache']) {
  254. return false;
  255. }
  256. // 缓存文件不存在
  257. if (!is_file($cacheFile)) {
  258. return false;
  259. }
  260. // 读取缓存文件失败
  261. if (!$handle = @fopen($cacheFile, "r")) {
  262. return false;
  263. }
  264. // 读取第一行
  265. preg_match('/\/\*(.+?)\*\//', fgets($handle), $matches);
  266. if (!isset($matches[1])) {
  267. return false;
  268. }
  269. $includeFile = unserialize($matches[1]);
  270. if (!is_array($includeFile)) {
  271. return false;
  272. }
  273. // 检查模板文件是否有更新
  274. foreach ($includeFile as $path => $time) {
  275. if (is_file($path) && filemtime($path) > $time) {
  276. // 模板文件如果有更新则缓存需要更新
  277. return false;
  278. }
  279. }
  280. // 检查编译存储是否有效
  281. return $this->storage->check($cacheFile, $this->config['cache_time']);
  282. }
  283. /**
  284. * 检查编译缓存是否存在
  285. * @access public
  286. * @param string $cacheId 缓存的id
  287. * @return boolean
  288. */
  289. public function isCache($cacheId)
  290. {
  291. if ($cacheId && $this->config['display_cache']) {
  292. // 缓存页面输出
  293. return Cache::has($cacheId);
  294. }
  295. return false;
  296. }
  297. /**
  298. * 编译模板文件内容
  299. * @access private
  300. * @param string $content 模板内容
  301. * @param string $cacheFile 缓存文件名
  302. * @return void
  303. */
  304. private function compiler(&$content, $cacheFile)
  305. {
  306. // 判断是否启用布局
  307. if ($this->config['layout_on']) {
  308. if (false !== strpos($content, '{__NOLAYOUT__}')) {
  309. // 可以单独定义不使用布局
  310. $content = str_replace('{__NOLAYOUT__}', '', $content);
  311. } else {
  312. // 读取布局模板
  313. $layoutFile = $this->parseTemplateFile($this->config['layout_name']);
  314. if ($layoutFile) {
  315. // 替换布局的主体内容
  316. $content = str_replace($this->config['layout_item'], $content, file_get_contents($layoutFile));
  317. }
  318. }
  319. } else {
  320. $content = str_replace('{__NOLAYOUT__}', '', $content);
  321. }
  322. // 模板解析
  323. $this->parse($content);
  324. if ($this->config['strip_space']) {
  325. /* 去除html空格与换行 */
  326. $find = ['~>\s+<~', '~>(\s+\n|\r)~'];
  327. $replace = ['><', '>'];
  328. $content = preg_replace($find, $replace, $content);
  329. }
  330. // 优化生成的php代码
  331. $content = preg_replace('/\?>\s*<\?php\s(?!echo\b)/s', '', $content);
  332. // 模板过滤输出
  333. $replace = $this->config['tpl_replace_string'];
  334. $content = str_replace(array_keys($replace), array_values($replace), $content);
  335. // 添加安全代码及模板引用记录
  336. $content = '<?php if (!defined(\'THINK_PATH\')) exit(); /*' . serialize($this->includeFile) . '*/ ?>' . "\n" . $content;
  337. // 编译存储
  338. $this->storage->write($cacheFile, $content);
  339. $this->includeFile = [];
  340. return;
  341. }
  342. /**
  343. * 模板解析入口
  344. * 支持普通标签和TagLib解析 支持自定义标签库
  345. * @access public
  346. * @param string $content 要解析的模板内容
  347. * @return void
  348. */
  349. public function parse(&$content)
  350. {
  351. // 内容为空不解析
  352. if (empty($content)) {
  353. return;
  354. }
  355. // 替换literal标签内容
  356. $this->parseLiteral($content);
  357. // 解析继承
  358. $this->parseExtend($content);
  359. // 解析布局
  360. $this->parseLayout($content);
  361. // 检查include语法
  362. $this->parseInclude($content);
  363. // 替换包含文件中literal标签内容
  364. $this->parseLiteral($content);
  365. // 检查PHP语法
  366. $this->parsePhp($content);
  367. // 获取需要引入的标签库列表
  368. // 标签库只需要定义一次,允许引入多个一次
  369. // 一般放在文件的最前面
  370. // 格式:<taglib name="html,mytag..." />
  371. // 当TAGLIB_LOAD配置为true时才会进行检测
  372. if ($this->config['taglib_load']) {
  373. $tagLibs = $this->getIncludeTagLib($content);
  374. if (!empty($tagLibs)) {
  375. // 对导入的TagLib进行解析
  376. foreach ($tagLibs as $tagLibName) {
  377. $this->parseTagLib($tagLibName, $content);
  378. }
  379. }
  380. }
  381. // 预先加载的标签库 无需在每个模板中使用taglib标签加载 但必须使用标签库XML前缀
  382. if ($this->config['taglib_pre_load']) {
  383. $tagLibs = explode(',', $this->config['taglib_pre_load']);
  384. foreach ($tagLibs as $tag) {
  385. $this->parseTagLib($tag, $content);
  386. }
  387. }
  388. // 内置标签库 无需使用taglib标签导入就可以使用 并且不需使用标签库XML前缀
  389. $tagLibs = explode(',', $this->config['taglib_build_in']);
  390. foreach ($tagLibs as $tag) {
  391. $this->parseTagLib($tag, $content, true);
  392. }
  393. // 解析普通模板标签 {$tagName}
  394. $this->parseTag($content);
  395. // 还原被替换的Literal标签
  396. $this->parseLiteral($content, true);
  397. return;
  398. }
  399. /**
  400. * 检查PHP语法
  401. * @access private
  402. * @param string $content 要解析的模板内容
  403. * @return void
  404. * @throws \think\Exception
  405. */
  406. private function parsePhp(&$content)
  407. {
  408. // 短标签的情况要将<?标签用echo方式输出 否则无法正常输出xml标识
  409. $content = preg_replace('/(<\?(?!php|=|$))/i', '<?php echo \'\\1\'; ?>' . "\n", $content);
  410. // PHP语法检查
  411. if ($this->config['tpl_deny_php'] && false !== strpos($content, '<?php')) {
  412. throw new Exception('not allow php tag', 11600);
  413. }
  414. return;
  415. }
  416. /**
  417. * 解析模板中的布局标签
  418. * @access private
  419. * @param string $content 要解析的模板内容
  420. * @return void
  421. */
  422. private function parseLayout(&$content)
  423. {
  424. // 读取模板中的布局标签
  425. if (preg_match($this->getRegex('layout'), $content, $matches)) {
  426. // 替换Layout标签
  427. $content = str_replace($matches[0], '', $content);
  428. // 解析Layout标签
  429. $array = $this->parseAttr($matches[0]);
  430. if (!$this->config['layout_on'] || $this->config['layout_name'] != $array['name']) {
  431. // 读取布局模板
  432. $layoutFile = $this->parseTemplateFile($array['name']);
  433. if ($layoutFile) {
  434. $replace = isset($array['replace']) ? $array['replace'] : $this->config['layout_item'];
  435. // 替换布局的主体内容
  436. $content = str_replace($replace, $content, file_get_contents($layoutFile));
  437. }
  438. }
  439. } else {
  440. $content = str_replace('{__NOLAYOUT__}', '', $content);
  441. }
  442. return;
  443. }
  444. /**
  445. * 解析模板中的include标签
  446. * @access private
  447. * @param string $content 要解析的模板内容
  448. * @return void
  449. */
  450. private function parseInclude(&$content)
  451. {
  452. $regex = $this->getRegex('include');
  453. $func = function ($template) use (&$func, &$regex, &$content) {
  454. if (preg_match_all($regex, $template, $matches, PREG_SET_ORDER)) {
  455. foreach ($matches as $match) {
  456. $array = $this->parseAttr($match[0]);
  457. $file = $array['file'];
  458. unset($array['file']);
  459. // 分析模板文件名并读取内容
  460. $parseStr = $this->parseTemplateName($file);
  461. foreach ($array as $k => $v) {
  462. // 以$开头字符串转换成模板变量
  463. if (0 === strpos($v, '$')) {
  464. $v = $this->get(substr($v, 1));
  465. }
  466. $parseStr = str_replace('[' . $k . ']', $v, $parseStr);
  467. }
  468. $content = str_replace($match[0], $parseStr, $content);
  469. // 再次对包含文件进行模板分析
  470. $func($parseStr);
  471. }
  472. unset($matches);
  473. }
  474. };
  475. // 替换模板中的include标签
  476. $func($content);
  477. return;
  478. }
  479. /**
  480. * 解析模板中的extend标签
  481. * @access private
  482. * @param string $content 要解析的模板内容
  483. * @return void
  484. */
  485. private function parseExtend(&$content)
  486. {
  487. $regex = $this->getRegex('extend');
  488. $array = $blocks = $baseBlocks = [];
  489. $extend = '';
  490. $func = function ($template) use (&$func, &$regex, &$array, &$extend, &$blocks, &$baseBlocks) {
  491. if (preg_match($regex, $template, $matches)) {
  492. if (!isset($array[$matches['name']])) {
  493. $array[$matches['name']] = 1;
  494. // 读取继承模板
  495. $extend = $this->parseTemplateName($matches['name']);
  496. // 递归检查继承
  497. $func($extend);
  498. // 取得block标签内容
  499. $blocks = array_merge($blocks, $this->parseBlock($template));
  500. return;
  501. }
  502. } else {
  503. // 取得顶层模板block标签内容
  504. $baseBlocks = $this->parseBlock($template, true);
  505. if (empty($extend)) {
  506. // 无extend标签但有block标签的情况
  507. $extend = $template;
  508. }
  509. }
  510. };
  511. $func($content);
  512. if (!empty($extend)) {
  513. if ($baseBlocks) {
  514. $children = [];
  515. foreach ($baseBlocks as $name => $val) {
  516. $replace = $val['content'];
  517. if (!empty($children[$name])) {
  518. // 如果包含有子block标签
  519. foreach ($children[$name] as $key) {
  520. $replace = str_replace($baseBlocks[$key]['begin'] . $baseBlocks[$key]['content'] . $baseBlocks[$key]['end'], $blocks[$key]['content'], $replace);
  521. }
  522. }
  523. if (isset($blocks[$name])) {
  524. // 带有{__block__}表示与所继承模板的相应标签合并,而不是覆盖
  525. $replace = str_replace(['{__BLOCK__}', '{__block__}'], $replace, $blocks[$name]['content']);
  526. if (!empty($val['parent'])) {
  527. // 如果不是最顶层的block标签
  528. $parent = $val['parent'];
  529. if (isset($blocks[$parent])) {
  530. $blocks[$parent]['content'] = str_replace($blocks[$name]['begin'] . $blocks[$name]['content'] . $blocks[$name]['end'], $replace, $blocks[$parent]['content']);
  531. }
  532. $blocks[$name]['content'] = $replace;
  533. $children[$parent][] = $name;
  534. continue;
  535. }
  536. } elseif (!empty($val['parent'])) {
  537. // 如果子标签没有被继承则用原值
  538. $children[$val['parent']][] = $name;
  539. $blocks[$name] = $val;
  540. }
  541. if (!$val['parent']) {
  542. // 替换模板中的顶级block标签
  543. $extend = str_replace($val['begin'] . $val['content'] . $val['end'], $replace, $extend);
  544. }
  545. }
  546. }
  547. $content = $extend;
  548. unset($blocks, $baseBlocks);
  549. }
  550. return;
  551. }
  552. /**
  553. * 替换页面中的literal标签
  554. * @access private
  555. * @param string $content 模板内容
  556. * @param boolean $restore 是否为还原
  557. * @return void
  558. */
  559. private function parseLiteral(&$content, $restore = false)
  560. {
  561. $regex = $this->getRegex($restore ? 'restoreliteral' : 'literal');
  562. if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER)) {
  563. if (!$restore) {
  564. $count = count($this->literal);
  565. // 替换literal标签
  566. foreach ($matches as $match) {
  567. $this->literal[] = substr($match[0], strlen($match[1]), -strlen($match[2]));
  568. $content = str_replace($match[0], "<!--###literal{$count}###-->", $content);
  569. $count++;
  570. }
  571. } else {
  572. // 还原literal标签
  573. foreach ($matches as $match) {
  574. $content = str_replace($match[0], $this->literal[$match[1]], $content);
  575. }
  576. // 清空literal记录
  577. $this->literal = [];
  578. }
  579. unset($matches);
  580. }
  581. return;
  582. }
  583. /**
  584. * 获取模板中的block标签
  585. * @access private
  586. * @param string $content 模板内容
  587. * @param boolean $sort 是否排序
  588. * @return array
  589. */
  590. private function parseBlock(&$content, $sort = false)
  591. {
  592. $regex = $this->getRegex('block');
  593. $result = [];
  594. if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) {
  595. $right = $keys = [];
  596. foreach ($matches as $match) {
  597. if (empty($match['name'][0])) {
  598. if (count($right) > 0) {
  599. $tag = array_pop($right);
  600. $start = $tag['offset'] + strlen($tag['tag']);
  601. $length = $match[0][1] - $start;
  602. $result[$tag['name']] = [
  603. 'begin' => $tag['tag'],
  604. 'content' => substr($content, $start, $length),
  605. 'end' => $match[0][0],
  606. 'parent' => count($right) ? end($right)['name'] : '',
  607. ];
  608. $keys[$tag['name']] = $match[0][1];
  609. }
  610. } else {
  611. // 标签头压入栈
  612. $right[] = [
  613. 'name' => $match[2][0],
  614. 'offset' => $match[0][1],
  615. 'tag' => $match[0][0],
  616. ];
  617. }
  618. }
  619. unset($right, $matches);
  620. if ($sort) {
  621. // 按block标签结束符在模板中的位置排序
  622. array_multisort($keys, $result);
  623. }
  624. }
  625. return $result;
  626. }
  627. /**
  628. * 搜索模板页面中包含的TagLib库
  629. * 并返回列表
  630. * @access private
  631. * @param string $content 模板内容
  632. * @return array|null
  633. */
  634. private function getIncludeTagLib(&$content)
  635. {
  636. // 搜索是否有TagLib标签
  637. if (preg_match($this->getRegex('taglib'), $content, $matches)) {
  638. // 替换TagLib标签
  639. $content = str_replace($matches[0], '', $content);
  640. return explode(',', $matches['name']);
  641. }
  642. return;
  643. }
  644. /**
  645. * TagLib库解析
  646. * @access public
  647. * @param string $tagLib 要解析的标签库
  648. * @param string $content 要解析的模板内容
  649. * @param boolean $hide 是否隐藏标签库前缀
  650. * @return void
  651. */
  652. public function parseTagLib($tagLib, &$content, $hide = false)
  653. {
  654. if (false !== strpos($tagLib, '\\')) {
  655. // 支持指定标签库的命名空间
  656. $className = $tagLib;
  657. $tagLib = substr($tagLib, strrpos($tagLib, '\\') + 1);
  658. } else {
  659. $className = '\\think\\template\\taglib\\' . ucwords($tagLib);
  660. }
  661. $tLib = new $className($this);
  662. $tLib->parseTag($content, $hide ? '' : $tagLib);
  663. return;
  664. }
  665. /**
  666. * 分析标签属性
  667. * @access public
  668. * @param string $str 属性字符串
  669. * @param string $name 不为空时返回指定的属性名
  670. * @return array
  671. */
  672. public function parseAttr($str, $name = null)
  673. {
  674. $regex = '/\s+(?>(?P<name>[\w-]+)\s*)=(?>\s*)([\"\'])(?P<value>(?:(?!\\2).)*)\\2/is';
  675. $array = [];
  676. if (preg_match_all($regex, $str, $matches, PREG_SET_ORDER)) {
  677. foreach ($matches as $match) {
  678. $array[$match['name']] = $match['value'];
  679. }
  680. unset($matches);
  681. }
  682. if (!empty($name) && isset($array[$name])) {
  683. return $array[$name];
  684. } else {
  685. return $array;
  686. }
  687. }
  688. /**
  689. * 模板标签解析
  690. * 格式: {TagName:args [|content] }
  691. * @access private
  692. * @param string $content 要解析的模板内容
  693. * @return void
  694. */
  695. private function parseTag(&$content)
  696. {
  697. $regex = $this->getRegex('tag');
  698. if (preg_match_all($regex, $content, $matches, PREG_SET_ORDER)) {
  699. foreach ($matches as $match) {
  700. $str = stripslashes($match[1]);
  701. $flag = substr($str, 0, 1);
  702. switch ($flag) {
  703. case '$':
  704. // 解析模板变量 格式 {$varName}
  705. // 是否带有?号
  706. if (false !== $pos = strpos($str, '?')) {
  707. $array = preg_split('/([!=]={1,2}|(?<!-)[><]={0,1})/', substr($str, 0, $pos), 2, PREG_SPLIT_DELIM_CAPTURE);
  708. $name = $array[0];
  709. $this->parseVar($name);
  710. $this->parseVarFunction($name);
  711. $str = trim(substr($str, $pos + 1));
  712. $this->parseVar($str);
  713. $first = substr($str, 0, 1);
  714. if (strpos($name, ')')) {
  715. // $name为对象或是自动识别,或者含有函数
  716. if (isset($array[1])) {
  717. $this->parseVar($array[2]);
  718. $name .= $array[1] . $array[2];
  719. }
  720. switch ($first) {
  721. case '?':
  722. $str = '<?php echo (' . $name . ') ? ' . $name . ' : ' . substr($str, 1) . '; ?>';
  723. break;
  724. case '=':
  725. $str = '<?php if(' . $name . ') echo ' . substr($str, 1) . '; ?>';
  726. break;
  727. default:
  728. $str = '<?php echo ' . $name . '?' . $str . '; ?>';
  729. }
  730. } else {
  731. if (isset($array[1])) {
  732. $this->parseVar($array[2]);
  733. $_name = ' && ' . $name . $array[1] . $array[2];
  734. } else {
  735. $_name = '';
  736. }
  737. // $name为数组
  738. switch ($first) {
  739. case '?':
  740. // {$varname??'xxx'} $varname有定义则输出$varname,否则输出xxx
  741. $str = '<?php echo isset(' . $name . ')' . $_name . ' ? ' . $name . ' : ' . substr($str, 1) . '; ?>';
  742. break;
  743. case '=':
  744. // {$varname?='xxx'} $varname为真时才输出xxx
  745. $str = '<?php if(!empty(' . $name . ')' . $_name . ') echo ' . substr($str, 1) . '; ?>';
  746. break;
  747. case ':':
  748. // {$varname?:'xxx'} $varname为真时输出$varname,否则输出xxx
  749. $str = '<?php echo !empty(' . $name . ')' . $_name . '?' . $name . $str . '; ?>';
  750. break;
  751. default:
  752. if (strpos($str, ':')) {
  753. // {$varname ? 'a' : 'b'} $varname为真时输出a,否则输出b
  754. $str = '<?php echo !empty(' . $name . ')' . $_name . '?' . $str . '; ?>';
  755. } else {
  756. $str = '<?php echo ' . $_name . '?' . $str . '; ?>';
  757. }
  758. }
  759. }
  760. } else {
  761. $this->parseVar($str);
  762. $this->parseVarFunction($str);
  763. $str = '<?php echo ' . $str . '; ?>';
  764. }
  765. break;
  766. case ':':
  767. // 输出某个函数的结果
  768. $str = substr($str, 1);
  769. $this->parseVar($str);
  770. $str = '<?php echo ' . $str . '; ?>';
  771. break;
  772. case '~':
  773. // 执行某个函数
  774. $str = substr($str, 1);
  775. $this->parseVar($str);
  776. $str = '<?php ' . $str . '; ?>';
  777. break;
  778. case '-':
  779. case '+':
  780. // 输出计算
  781. $this->parseVar($str);
  782. $str = '<?php echo ' . $str . '; ?>';
  783. break;
  784. case '/':
  785. // 注释标签
  786. $flag2 = substr($str, 1, 1);
  787. if ('/' == $flag2 || ('*' == $flag2 && substr(rtrim($str), -2) == '*/')) {
  788. $str = '';
  789. }
  790. break;
  791. default:
  792. // 未识别的标签直接返回
  793. $str = $this->config['tpl_begin'] . $str . $this->config['tpl_end'];
  794. break;
  795. }
  796. $content = str_replace($match[0], $str, $content);
  797. }
  798. unset($matches);
  799. }
  800. return;
  801. }
  802. /**
  803. * 模板变量解析,支持使用函数
  804. * 格式: {$varname|function1|function2=arg1,arg2}
  805. * @access public
  806. * @param string $varStr 变量数据
  807. * @return void
  808. */
  809. public function parseVar(&$varStr)
  810. {
  811. $varStr = trim($varStr);
  812. if (preg_match_all('/\$[a-zA-Z_](?>\w*)(?:[:\.][0-9a-zA-Z_](?>\w*))+/', $varStr, $matches, PREG_OFFSET_CAPTURE)) {
  813. static $_varParseList = [];
  814. while ($matches[0]) {
  815. $match = array_pop($matches[0]);
  816. //如果已经解析过该变量字串,则直接返回变量值
  817. if (isset($_varParseList[$match[0]])) {
  818. $parseStr = $_varParseList[$match[0]];
  819. } else {
  820. if (strpos($match[0], '.')) {
  821. $vars = explode('.', $match[0]);
  822. $first = array_shift($vars);
  823. if ('$Think' == $first) {
  824. // 所有以Think.打头的以特殊变量对待 无需模板赋值就可以输出
  825. $parseStr = $this->parseThinkVar($vars);
  826. } elseif ('$Request' == $first) {
  827. // 获取Request请求对象参数
  828. $method = array_shift($vars);
  829. if (!empty($vars)) {
  830. $params = implode('.', $vars);
  831. if ('true' != $params) {
  832. $params = '\'' . $params . '\'';
  833. }
  834. } else {
  835. $params = '';
  836. }
  837. $parseStr = '\think\Request::instance()->' . $method . '(' . $params . ')';
  838. } else {
  839. switch ($this->config['tpl_var_identify']) {
  840. case 'array': // 识别为数组
  841. $parseStr = $first . '[\'' . implode('\'][\'', $vars) . '\']';
  842. break;
  843. case 'obj': // 识别为对象
  844. $parseStr = $first . '->' . implode('->', $vars);
  845. break;
  846. default: // 自动判断数组或对象
  847. $parseStr = '(is_array(' . $first . ')?' . $first . '[\'' . implode('\'][\'', $vars) . '\']:' . $first . '->' . implode('->', $vars) . ')';
  848. }
  849. }
  850. } else {
  851. $parseStr = str_replace(':', '->', $match[0]);
  852. }
  853. $_varParseList[$match[0]] = $parseStr;
  854. }
  855. $varStr = substr_replace($varStr, $parseStr, $match[1], strlen($match[0]));
  856. }
  857. unset($matches);
  858. }
  859. return;
  860. }
  861. /**
  862. * 对模板中使用了函数的变量进行解析
  863. * 格式 {$varname|function1|function2=arg1,arg2}
  864. * @access public
  865. * @param string $varStr 变量字符串
  866. * @return void
  867. */
  868. public function parseVarFunction(&$varStr)
  869. {
  870. if (false == strpos($varStr, '|')) {
  871. return;
  872. }
  873. static $_varFunctionList = [];
  874. $_key = md5($varStr);
  875. //如果已经解析过该变量字串,则直接返回变量值
  876. if (isset($_varFunctionList[$_key])) {
  877. $varStr = $_varFunctionList[$_key];
  878. } else {
  879. $varArray = explode('|', $varStr);
  880. // 取得变量名称
  881. $name = array_shift($varArray);
  882. // 对变量使用函数
  883. $length = count($varArray);
  884. // 取得模板禁止使用函数列表
  885. $template_deny_funs = explode(',', $this->config['tpl_deny_func_list']);
  886. for ($i = 0; $i < $length; $i++) {
  887. $args = explode('=', $varArray[$i], 2);
  888. // 模板函数过滤
  889. $fun = trim($args[0]);
  890. switch ($fun) {
  891. case 'default': // 特殊模板函数
  892. if (false === strpos($name, '(')) {
  893. $name = '(isset(' . $name . ') && (' . $name . ' !== \'\')?' . $name . ':' . $args[1] . ')';
  894. } else {
  895. $name = '(' . $name . ' ?: ' . $args[1] . ')';
  896. }
  897. break;
  898. default: // 通用模板函数
  899. if (!in_array($fun, $template_deny_funs)) {
  900. if (isset($args[1])) {
  901. if (strstr($args[1], '###')) {
  902. $args[1] = str_replace('###', $name, $args[1]);
  903. $name = "$fun($args[1])";
  904. } else {
  905. $name = "$fun($name,$args[1])";
  906. }
  907. } else {
  908. if (!empty($args[0])) {
  909. $name = "$fun($name)";
  910. }
  911. }
  912. }
  913. }
  914. }
  915. $_varFunctionList[$_key] = $name;
  916. $varStr = $name;
  917. }
  918. return;
  919. }
  920. /**
  921. * 特殊模板变量解析
  922. * 格式 以 $Think. 打头的变量属于特殊模板变量
  923. * @access public
  924. * @param array $vars 变量数组
  925. * @return string
  926. */
  927. public function parseThinkVar($vars)
  928. {
  929. $type = strtoupper(trim(array_shift($vars)));
  930. $param = implode('.', $vars);
  931. if ($vars) {
  932. switch ($type) {
  933. case 'SERVER':
  934. $parseStr = '\\think\\Request::instance()->server(\'' . $param . '\')';
  935. break;
  936. case 'GET':
  937. $parseStr = '\\think\\Request::instance()->get(\'' . $param . '\')';
  938. break;
  939. case 'POST':
  940. $parseStr = '\\think\\Request::instance()->post(\'' . $param . '\')';
  941. break;
  942. case 'COOKIE':
  943. $parseStr = '\\think\\Cookie::get(\'' . $param . '\')';
  944. break;
  945. case 'SESSION':
  946. $parseStr = '\\think\\Session::get(\'' . $param . '\')';
  947. break;
  948. case 'ENV':
  949. $parseStr = '\\think\\Request::instance()->env(\'' . $param . '\')';
  950. break;
  951. case 'REQUEST':
  952. $parseStr = '\\think\\Request::instance()->request(\'' . $param . '\')';
  953. break;
  954. case 'CONST':
  955. $parseStr = strtoupper($param);
  956. break;
  957. case 'LANG':
  958. $parseStr = '\\think\\Lang::get(\'' . $param . '\')';
  959. break;
  960. case 'CONFIG':
  961. $parseStr = '\\think\\Config::get(\'' . $param . '\')';
  962. break;
  963. default:
  964. $parseStr = '\'\'';
  965. break;
  966. }
  967. } else {
  968. switch ($type) {
  969. case 'NOW':
  970. $parseStr = "date('Y-m-d g:i a',time())";
  971. break;
  972. case 'VERSION':
  973. $parseStr = 'THINK_VERSION';
  974. break;
  975. case 'LDELIM':
  976. $parseStr = '\'' . ltrim($this->config['tpl_begin'], '\\') . '\'';
  977. break;
  978. case 'RDELIM':
  979. $parseStr = '\'' . ltrim($this->config['tpl_end'], '\\') . '\'';
  980. break;
  981. default:
  982. if (defined($type)) {
  983. $parseStr = $type;
  984. } else {
  985. $parseStr = '';
  986. }
  987. }
  988. }
  989. return $parseStr;
  990. }
  991. /**
  992. * 分析加载的模板文件并读取内容 支持多个模板文件读取
  993. * @access private
  994. * @param string $templateName 模板文件名
  995. * @return string
  996. */
  997. private function parseTemplateName($templateName)
  998. {
  999. $array = explode(',', $templateName);
  1000. $parseStr = '';
  1001. foreach ($array as $templateName) {
  1002. if (empty($templateName)) {
  1003. continue;
  1004. }
  1005. if (0 === strpos($templateName, '$')) {
  1006. //支持加载变量文件名
  1007. $templateName = $this->get(substr($templateName, 1));
  1008. }
  1009. $template = $this->parseTemplateFile($templateName);
  1010. if ($template) {
  1011. // 获取模板文件内容
  1012. $parseStr .= file_get_contents($template);
  1013. }
  1014. }
  1015. return $parseStr;
  1016. }
  1017. /**
  1018. * 解析模板文件名
  1019. * @access private
  1020. * @param string $template 文件名
  1021. * @return string|false
  1022. */
  1023. private function parseTemplateFile($template)
  1024. {
  1025. if ('' == pathinfo($template, PATHINFO_EXTENSION)) {
  1026. if (strpos($template, '@')) {
  1027. list($module, $template) = explode('@', $template);
  1028. }
  1029. if (0 !== strpos($template, '/')) {
  1030. $template = str_replace(['/', ':'], $this->config['view_depr'], $template);
  1031. } else {
  1032. $template = str_replace(['/', ':'], $this->config['view_depr'], substr($template, 1));
  1033. }
  1034. if ($this->config['view_base']) {
  1035. $module = isset($module) ? $module : Request::instance()->module();
  1036. $path = $this->config['view_base'] . ($module ? $module . DS : '');
  1037. } else {
  1038. $path = isset($module) ? APP_PATH . $module . DS . basename($this->config['view_path']) . DS : $this->config['view_path'];
  1039. }
  1040. $template = $path . $template . '.' . ltrim($this->config['view_suffix'], '.');
  1041. }
  1042. if (is_file($template)) {
  1043. // 记录模板文件的更新时间
  1044. $this->includeFile[$template] = filemtime($template);
  1045. return $template;
  1046. } else {
  1047. throw new TemplateNotFoundException('template not exists:' . $template, $template);
  1048. }
  1049. }
  1050. /**
  1051. * 按标签生成正则
  1052. * @access private
  1053. * @param string $tagName 标签名
  1054. * @return string
  1055. */
  1056. private function getRegex($tagName)
  1057. {
  1058. $regex = '';
  1059. if ('tag' == $tagName) {
  1060. $begin = $this->config['tpl_begin'];
  1061. $end = $this->config['tpl_end'];
  1062. if (strlen(ltrim($begin, '\\')) == 1 && strlen(ltrim($end, '\\')) == 1) {
  1063. $regex = $begin . '((?:[\$]{1,2}[a-wA-w_]|[\:\~][\$a-wA-w_]|[+]{2}[\$][a-wA-w_]|[-]{2}[\$][a-wA-w_]|\/[\*\/])(?>[^' . $end . ']*))' . $end;
  1064. } else {
  1065. $regex = $begin . '((?:[\$]{1,2}[a-wA-w_]|[\:\~][\$a-wA-w_]|[+]{2}[\$][a-wA-w_]|[-]{2}[\$][a-wA-w_]|\/[\*\/])(?>(?:(?!' . $end . ').)*))' . $end;
  1066. }
  1067. } else {
  1068. $begin = $this->config['taglib_begin'];
  1069. $end = $this->config['taglib_end'];
  1070. $single = strlen(ltrim($begin, '\\')) == 1 && strlen(ltrim($end, '\\')) == 1 ? true : false;
  1071. switch ($tagName) {
  1072. case 'block':
  1073. if ($single) {
  1074. $regex = $begin . '(?:' . $tagName . '\b(?>(?:(?!name=).)*)\bname=([\'\"])(?P<name>[\$\w\-\/\.]+)\\1(?>[^' . $end . ']*)|\/' . $tagName . ')' . $end;
  1075. } else {
  1076. $regex = $begin . '(?:' . $tagName . '\b(?>(?:(?!name=).)*)\bname=([\'\"])(?P<name>[\$\w\-\/\.]+)\\1(?>(?:(?!' . $end . ').)*)|\/' . $tagName . ')' . $end;
  1077. }
  1078. break;
  1079. case 'literal':
  1080. if ($single) {
  1081. $regex = '(' . $begin . $tagName . '\b(?>[^' . $end . ']*)' . $end . ')';
  1082. $regex .= '(?:(?>[^' . $begin . ']*)(?>(?!' . $begin . '(?>' . $tagName . '\b[^' . $end . ']*|\/' . $tagName . ')' . $end . ')' . $begin . '[^' . $begin . ']*)*)';
  1083. $regex .= '(' . $begin . '\/' . $tagName . $end . ')';
  1084. } else {
  1085. $regex = '(' . $begin . $tagName . '\b(?>(?:(?!' . $end . ').)*)' . $end . ')';
  1086. $regex .= '(?:(?>(?:(?!' . $begin . ').)*)(?>(?!' . $begin . '(?>' . $tagName . '\b(?>(?:(?!' . $end . ').)*)|\/' . $tagName . ')' . $end . ')' . $begin . '(?>(?:(?!' . $begin . ').)*))*)';
  1087. $regex .= '(' . $begin . '\/' . $tagName . $end . ')';
  1088. }
  1089. break;
  1090. case 'restoreliteral':
  1091. $regex = '<!--###literal(\d+)###-->';
  1092. break;
  1093. case 'include':
  1094. $name = 'file';
  1095. case 'taglib':
  1096. case 'layout':
  1097. case 'extend':
  1098. if (empty($name)) {
  1099. $name = 'name';
  1100. }
  1101. if ($single) {
  1102. $regex = $begin . $tagName . '\b(?>(?:(?!' . $name . '=).)*)\b' . $name . '=([\'\"])(?P<name>[\$\w\-\/\.\:@,\\\\]+)\\1(?>[^' . $end . ']*)' . $end;
  1103. } else {
  1104. $regex = $begin . $tagName . '\b(?>(?:(?!' . $name . '=).)*)\b' . $name . '=([\'\"])(?P<name>[\$\w\-\/\.\:@,\\\\]+)\\1(?>(?:(?!' . $end . ').)*)' . $end;
  1105. }
  1106. break;
  1107. }
  1108. }
  1109. return '/' . $regex . '/is';
  1110. }
  1111. }