国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1353 lines
45KB

  1. <?php if (!defined('DEDEINC')) exit("Request Error!");
  2. /**
  3. * 模板引擎文件
  4. *
  5. * @version $Id: dedetemplate.class.php 3 15:44 2010年7月6日Z tianya $
  6. * @package DedeBIZ.Libraries
  7. * @copyright Copyright (c) 2020, DedeBIZ.COM
  8. * @license https://www.dedebiz.com/license
  9. * @link https://www.dedebiz.com
  10. */
  11. /**
  12. * 这个函数用于定义任意名称的块使用的接口
  13. * 返回值应是一个二维数组
  14. * 块调用对应的文件为 include/taglib/plus_blockname.php
  15. * ----------------------------------------------------------------
  16. * 由于标记一般存在默认属性,在编写块函数时,应该在块函数中进行给属性赋省缺值处理,如:
  17. * $attlist = "titlelen=30,catalogid=0,modelid=0,flag=,addon=,row=8,ids=,orderby=id,orderway=desc,limit=,subday=0";
  18. * 给属性赋省缺值
  19. * FillAtts($atts,$attlist);
  20. * 处理属性中使用的系统变量 var、global、field 类型(不支持多维数组)
  21. * FillFields($atts,$fields,$refObj);
  22. *
  23. * @access public
  24. * @param array $atts 属性
  25. * @param object $refObj 所属对象
  26. * @param array $fields 字段
  27. * @return string
  28. */
  29. function MakePublicTag($atts = array(), $refObj = '', $fields = array())
  30. {
  31. $atts['tagname'] = preg_replace("/[0-9]{1,}$/", "", $atts['tagname']);
  32. $plusfile = DEDEINC . '/tpllib/plus_' . $atts['tagname'] . '.php';
  33. if (!file_exists($plusfile)) {
  34. if (isset($atts['rstype']) && $atts['rstype'] == 'string') {
  35. return '';
  36. } else {
  37. return array();
  38. }
  39. } else {
  40. include_once($plusfile);
  41. $func = 'plus_' . $atts['tagname'];
  42. return $func($atts, $refObj, $fields);
  43. }
  44. }
  45. /**
  46. * 设定属性的默认值
  47. *
  48. * @access public
  49. * @param array $atts 属性
  50. * @param array $attlist 属性列表
  51. * @return void
  52. */
  53. function FillAtts(&$atts, $attlist)
  54. {
  55. $attlists = explode(',', $attlist);
  56. foreach ($attlists as $att) {
  57. list($k, $v) = explode('=', $att);
  58. if (!isset($atts[$k])) {
  59. $atts[$k] = $v;
  60. }
  61. }
  62. }
  63. /**
  64. * 把上级的fields传递给atts
  65. *
  66. * @access public
  67. * @param array $atts 属性
  68. * @param object $refObj 所属对象
  69. * @param array $fields 字段
  70. * @return string
  71. */
  72. function FillFields(&$atts, &$refObj, &$fields)
  73. {
  74. global $_vars;
  75. foreach ($atts as $k => $v) {
  76. if (preg_match('/^field\./i', $v)) {
  77. $key = preg_replace('/^field\./i', '', $v);
  78. if (isset($fields[$key])) {
  79. $atts[$k] = $fields[$key];
  80. }
  81. } else if (preg_match('/^var\./i', $v)) {
  82. $key = preg_replace('/^var\./i', '', $v);
  83. if (isset($_vars[$key])) {
  84. $atts[$k] = $_vars[$key];
  85. }
  86. } else if (preg_match('/^global\./i', $v)) {
  87. $key = preg_replace('/^global\./i', '', $v);
  88. if (isset($GLOBALS[$key])) {
  89. $atts[$k] = $GLOBALS[$key];
  90. }
  91. }
  92. }
  93. }
  94. /**
  95. * class Tag 标记的数据结构描述
  96. * function C__Tag();
  97. *
  98. * @package Tag
  99. * @subpackage DedeBIZ.Libraries
  100. * @link https://www.dedebiz.com
  101. */
  102. class Tag
  103. {
  104. var $isCompiler = FALSE; //标记是否已被替代,供解析器使用
  105. var $tagName = ""; //标记名称
  106. var $innerText = ""; //标记之间的文本
  107. var $startPos = 0; //标记起始位置
  108. var $endPos = 0; //标记结束位置
  109. var $cAtt = ""; //标记属性描述,即是class TagAttribute
  110. var $tagValue = ""; //标记的值
  111. var $tagID = 0;
  112. /**
  113. * 获取标记的名称和值
  114. *
  115. * @access public
  116. * @return string
  117. */
  118. function GetName()
  119. {
  120. return strtolower($this->tagName);
  121. }
  122. function GetValue()
  123. {
  124. return $this->tagValue;
  125. }
  126. function IsAtt($str)
  127. {
  128. return $this->cAtt->IsAttribute($str);
  129. }
  130. function GetAtt($str)
  131. {
  132. return $this->cAtt->GetAtt($str);
  133. }
  134. /**
  135. * 获取底层模板
  136. *
  137. * @return string
  138. */
  139. function GetinnerText()
  140. {
  141. return $this->innerText;
  142. }
  143. }
  144. /**
  145. * 模板解析器
  146. * function C__DedeTemplate
  147. *
  148. * @package DedeTemplate
  149. * @subpackage DedeBIZ.Libraries
  150. * @link https://www.dedebiz.com
  151. */
  152. class DedeTemplate
  153. {
  154. var $tagMaxLen = 64;
  155. var $charToLow = TRUE;
  156. var $isCache = TRUE;
  157. var $isParse = FALSE;
  158. var $isCompiler = TRUE;
  159. var $templateDir = '';
  160. var $tempMkTime = 0;
  161. var $cacheFile = '';
  162. var $configFile = '';
  163. var $buildFile = '';
  164. var $refDir = '';
  165. var $cacheDir = '';
  166. var $templateFile = '';
  167. var $sourceString = '';
  168. var $cTags = array();
  169. //var $definedVars = array();
  170. var $count = -1;
  171. var $loopNum = 0;
  172. var $refObj = '';
  173. var $makeLoop = 0;
  174. var $tagStartWord = '{dede:';
  175. var $fullTagEndWord = '{/dede:';
  176. var $sTagEndWord = '/}';
  177. var $tagEndWord = '}';
  178. var $tpCfgs = array();
  179. /**
  180. * 析构函数
  181. *
  182. * @access public
  183. * @param string $templatedir 模板目录
  184. * @param string $refDir 所属目录
  185. * @return void
  186. */
  187. function __construct($templatedir = '', $refDir = '')
  188. {
  189. //$definedVars[] = 'var';
  190. //缓存目录
  191. if ($templatedir == '') {
  192. $this->templateDir = DEDEROOT . '/templates';
  193. } else {
  194. $this->templateDir = $templatedir;
  195. }
  196. //模板include目录
  197. if ($refDir == '') {
  198. if (isset($GLOBALS['cfg_df_style'])) {
  199. $this->refDir = $this->templateDir . '/' . $GLOBALS['cfg_df_style'] . '/';
  200. } else {
  201. $this->refDir = $this->templateDir;
  202. }
  203. }
  204. $this->cacheDir = DEDEROOT . $GLOBALS['cfg_tplcache_dir'];
  205. }
  206. //构造函数,兼容PHP4
  207. function DedeTemplate($templatedir = '', $refDir = '')
  208. {
  209. $this->__construct($templatedir, $refDir);
  210. }
  211. /**
  212. * 设定本类自身实例的类引用和使用本类的类实例(如果在类中使用本模板引擎,后一参数一般为$this)
  213. *
  214. * @access public
  215. * @param object $refObj 实例对象
  216. * @return string
  217. */
  218. function SetObject(&$refObj)
  219. {
  220. $this->refObj = $refObj;
  221. }
  222. /**
  223. * 设定Var的键值对
  224. *
  225. * @access public
  226. * @param string $k 键
  227. * @param string $v 值
  228. * @return string
  229. */
  230. function SetVar($k, $v)
  231. {
  232. $GLOBALS['_vars'][$k] = $v;
  233. }
  234. /**
  235. * 设定Var的键值对
  236. *
  237. * @access public
  238. * @param string $k 键
  239. * @param string $v 值
  240. * @return string
  241. */
  242. function Assign($k, $v)
  243. {
  244. $GLOBALS['_vars'][$k] = $v;
  245. }
  246. /**
  247. * 设定数组
  248. *
  249. * @access public
  250. * @param string $k 键
  251. * @param string $v 值
  252. * @return string
  253. */
  254. function SetArray($k, $v)
  255. {
  256. $GLOBALS[$k] = $v;
  257. }
  258. /**
  259. * 设置标记风格
  260. *
  261. * @access public
  262. * @param string $ts 标签开始标记
  263. * @param string $ftend 标签结束标记
  264. * @param string $stend 标签尾部结束标记
  265. * @param string $tend 结束标记
  266. * @return void
  267. */
  268. function SetTagStyle($ts = '{dede:', $ftend = '{/dede:', $stend = '/}', $tend = '}')
  269. {
  270. $this->tagStartWord = $ts;
  271. $this->fullTagEndWord = $ftend;
  272. $this->sTagEndWord = $stend;
  273. $this->tagEndWord = $tend;
  274. }
  275. /**
  276. * 获得模板设定的config值
  277. *
  278. * @access public
  279. * @param string $k 键名
  280. * @return string
  281. */
  282. function GetConfig($k)
  283. {
  284. return (isset($this->tpCfgs[$k]) ? $this->tpCfgs[$k] : '');
  285. }
  286. /**
  287. * 设定模板文件
  288. *
  289. * @access public
  290. * @param string $tmpfile 模板文件
  291. * @return void
  292. */
  293. function LoadTemplate($tmpfile)
  294. {
  295. if (!file_exists($tmpfile)) {
  296. echo " Template Not Found! ";
  297. exit();
  298. }
  299. $tmpfile = preg_replace("/[\\/]{1,}/", "/", $tmpfile);
  300. $tmpfiles = explode('/', $tmpfile);
  301. $tmpfileOnlyName = preg_replace("/(.*)\//", "", $tmpfile);
  302. $this->templateFile = $tmpfile;
  303. $this->refDir = '';
  304. for ($i = 0; $i < count($tmpfiles) - 1; $i++) {
  305. $this->refDir .= $tmpfiles[$i] . '/';
  306. }
  307. if (!is_dir($this->cacheDir)) {
  308. $this->cacheDir = $this->refDir;
  309. }
  310. if ($this->cacheDir != '') {
  311. $this->cacheDir = $this->cacheDir . '/';
  312. }
  313. if (isset($GLOBALS['_DEBUG_CACHE'])) {
  314. $this->cacheDir = $this->refDir;
  315. }
  316. $this->cacheFile = $this->cacheDir . preg_replace("/\.(wml|html|htm|php)$/", "_" . $this->GetEncodeStr($tmpfile) . '.inc', $tmpfileOnlyName);
  317. $this->configFile = $this->cacheDir . preg_replace("/\.(wml|html|htm|php)$/", "_" . $this->GetEncodeStr($tmpfile) . '_config.inc', $tmpfileOnlyName);
  318. //不开启缓存、当缓存文件不存在、及模板为更新的文件的时候才载入模板并进行解析
  319. if (
  320. $this->isCache == FALSE || !file_exists($this->cacheFile)
  321. || filemtime($this->templateFile) > filemtime($this->cacheFile)
  322. ) {
  323. $t1 = ExecTime(); //debug
  324. $fp = fopen($this->templateFile, 'r');
  325. $this->sourceString = fread($fp, filesize($this->templateFile));
  326. fclose($fp);
  327. $this->ParseTemplate();
  328. //模板解析时间
  329. //echo ExecTime() - $t1;
  330. } else {
  331. //如果存在config文件,则载入此文件,该文件用于保存 $this->tpCfgs的内容,以供扩展用途
  332. //模板中用{tag:config name='' value=''/}来设定该值
  333. if (file_exists($this->configFile)) {
  334. include($this->configFile);
  335. }
  336. }
  337. }
  338. /**
  339. * 载入模板字符串
  340. *
  341. * @access public
  342. * @param string $str 模板字符串
  343. * @return void
  344. */
  345. function LoadString($str = '')
  346. {
  347. $this->sourceString = $str;
  348. $hashcode = md5($this->sourceString);
  349. $this->cacheFile = $this->cacheDir . "/string_" . $hashcode . ".inc";
  350. $this->configFile = $this->cacheDir . "/string_" . $hashcode . "_config.inc";
  351. $this->ParseTemplate();
  352. }
  353. /**
  354. * 调用此函数include一个编译后的PHP文件,通常是在最后一个步骤才调用本文件
  355. *
  356. * @access public
  357. * @return string
  358. */
  359. function CacheFile()
  360. {
  361. global $gtmpfile;
  362. $this->WriteCache();
  363. return $this->cacheFile;
  364. }
  365. /**
  366. * 显示内容,由于函数中会重新解压一次$GLOBALS变量,所以在动态页中,应该尽量少用本方法,
  367. * 取代之是直接在程序中 include $tpl->CacheFile(),不过include $tpl->CacheFile()这种方式不能在类或函数内使用
  368. *
  369. * @access public
  370. * @param string
  371. * @return void
  372. */
  373. function Display()
  374. {
  375. global $gtmpfile;
  376. extract($GLOBALS, EXTR_SKIP);
  377. $this->WriteCache();
  378. include $this->cacheFile;
  379. }
  380. /**
  381. * 保存运行后的程序为文件
  382. *
  383. * @access public
  384. * @param string $savefile 保存到的文件目录
  385. * @return void
  386. */
  387. function SaveTo($savefile)
  388. {
  389. extract($GLOBALS, EXTR_SKIP);
  390. $this->WriteCache();
  391. ob_start();
  392. include $this->cacheFile;
  393. $okstr = ob_get_contents();
  394. ob_end_clean();
  395. $fp = @fopen($savefile, "w") or die(" Tag Engine Create File FALSE! ");
  396. fwrite($fp, $okstr);
  397. fclose($fp);
  398. }
  399. // ------------------------------------------------------------------------
  400. /**
  401. * CheckDisabledFunctions
  402. *
  403. * COMMENT : CheckDisabledFunctions : 检查是否存在禁止的函数
  404. *
  405. * @access public
  406. * @param string
  407. * @return bool
  408. */
  409. function CheckDisabledFunctions($str, &$errmsg = '')
  410. {
  411. global $cfg_disable_funs;
  412. $cfg_disable_funs = isset($cfg_disable_funs) ? $cfg_disable_funs : 'phpinfo,eval,exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source,file_put_contents,fsockopen,fopen,fwrite';
  413. // 模板引擎增加disable_functions
  414. if (!defined('DEDEDISFUN')) {
  415. $tokens = token_get_all_nl($str);
  416. $disabled_functions = explode(',', $cfg_disable_funs);
  417. foreach ($tokens as $token) {
  418. if (is_array($token)) {
  419. if ($token[0] = '306' && in_array($token[1], $disabled_functions)) {
  420. $errmsg = 'DedeBIZ Error:function disabled "' . $token[1] . '" <a href="https://www.dedebiz.com/help" target="_blank">more...</a>';
  421. return FALSE;
  422. }
  423. }
  424. }
  425. }
  426. return TRUE;
  427. }
  428. /**
  429. * 解析模板并写缓存文件
  430. *
  431. * @access public
  432. * @param string $ctype 缓存类型
  433. * @return void
  434. */
  435. function WriteCache($ctype = 'all')
  436. {
  437. if (
  438. !file_exists($this->cacheFile) || $this->isCache == FALSE
  439. || (file_exists($this->templateFile) && (filemtime($this->templateFile) > filemtime($this->cacheFile)))
  440. ) {
  441. if (!$this->isParse) {
  442. $this->ParseTemplate();
  443. }
  444. $fp = fopen($this->cacheFile, 'w') or dir("Write Cache File Error! ");
  445. flock($fp, 3);
  446. $result = trim($this->GetResult());
  447. $errmsg = '';
  448. //var_dump($result);exit();
  449. if (!$this->CheckDisabledFunctions($result, $errmsg)) {
  450. fclose($fp);
  451. @unlink($this->cacheFile);
  452. die($errmsg);
  453. }
  454. fwrite($fp, $result);
  455. fclose($fp);
  456. if (count($this->tpCfgs) > 0) {
  457. $fp = fopen($this->configFile, 'w') or dir("Write Config File Error! ");
  458. flock($fp, 3);
  459. fwrite($fp, '<' . '?php' . "\r\n");
  460. foreach ($this->tpCfgs as $k => $v) {
  461. $v = str_replace("\"", "\\\"", $v);
  462. $v = str_replace("\$", "\\\$", $v);
  463. fwrite($fp, "\$this->tpCfgs['$k']=\"$v\";\r\n");
  464. }
  465. fwrite($fp, '?' . '>');
  466. fclose($fp);
  467. }
  468. }
  469. /*
  470. if(!file_exists($this->cacheFile) || $this->isCache==FALSE
  471. || ( file_exists($this->templateFile) && (filemtime($this->templateFile) > filemtime($this->cacheFile)) ) )
  472. {
  473. if($ctype!='config')
  474. {
  475. if(!$this->isParse)
  476. {
  477. $this->ParseTemplate();
  478. }
  479. $fp = fopen($this->cacheFile,'w') or dir("Write Cache File Error! ");
  480. flock($fp,3);
  481. fwrite($fp,trim($this->GetResult()));
  482. fclose($fp);
  483. }
  484. else
  485. {
  486. if(count($this->tpCfgs) > 0)
  487. {
  488. $fp = fopen($this->configFile,'w') or dir("Write Config File Error! ");
  489. flock($fp,3);
  490. fwrite($fp,'<'.'?php'."\r\n");
  491. foreach($this->tpCfgs as $k=>$v)
  492. {
  493. $v = str_replace("\"","\\\"",$v);
  494. $v = str_replace("\$","\\\$",$v);
  495. fwrite($fp,"\$this->tpCfgs['$k']=\"$v\";\r\n");
  496. }
  497. fwrite($fp,'?'.'>');
  498. fclose($fp);
  499. }
  500. }
  501. }
  502. else
  503. {
  504. if($ctype=='config' && count($this->tpCfgs) > 0 )
  505. {
  506. $fp = fopen($this->configFile,'w') or dir("Write Config File Error! ");
  507. flock($fp,3);
  508. fwrite($fp,'<'.'?php'."\r\n");
  509. foreach($this->tpCfgs as $k=>$v)
  510. {
  511. $v = str_replace("\"","\\\"",$v);
  512. $v = str_replace("\$","\\\$",$v);
  513. fwrite($fp,"\$this->tpCfgs['$k']=\"$v\";\r\n");
  514. }
  515. fwrite($fp,'?'.'>');
  516. fclose($fp);
  517. }
  518. }
  519. */
  520. }
  521. /**
  522. * 获得模板文件名的md5字符串
  523. *
  524. * @access public
  525. * @param string $tmpfile 模板文件
  526. * @return string
  527. */
  528. function GetEncodeStr($tmpfile)
  529. {
  530. //$tmpfiles = explode('/',$tmpfile);
  531. $encodeStr = substr(md5($tmpfile), 0, 24);
  532. return $encodeStr;
  533. }
  534. /**
  535. * 解析模板
  536. *
  537. * @access public
  538. * @return void
  539. */
  540. function ParseTemplate()
  541. {
  542. if ($this->makeLoop > 5) {
  543. return;
  544. }
  545. $this->count = -1;
  546. $this->cTags = array();
  547. $this->isParse = TRUE;
  548. $sPos = 0;
  549. $ePos = 0;
  550. $tagStartWord = $this->tagStartWord;
  551. $fullTagEndWord = $this->fullTagEndWord;
  552. $sTagEndWord = $this->sTagEndWord;
  553. $tagEndWord = $this->tagEndWord;
  554. $startWordLen = strlen($tagStartWord);
  555. $sourceLen = strlen($this->sourceString);
  556. if ($sourceLen <= ($startWordLen + 3)) {
  557. return;
  558. }
  559. $cAtt = new TagAttributeParse();
  560. $cAtt->CharToLow = TRUE;
  561. //遍历模板字符串,请取标记及其属性信息
  562. $t = 0;
  563. $preTag = '';
  564. $tswLen = strlen($tagStartWord);
  565. @$cAtt->cAttributes->items = array();
  566. for ($i = 0; $i < $sourceLen; $i++) {
  567. $ttagName = '';
  568. //如果不进行此判断,将无法识别相连的两个标记
  569. if ($i - 1 >= 0) {
  570. $ss = $i - 1;
  571. } else {
  572. $ss = 0;
  573. }
  574. $tagPos = strpos($this->sourceString, $tagStartWord, $ss);
  575. //判断后面是否还有模板标记
  576. if ($tagPos == 0 && ($sourceLen - $i < $tswLen
  577. || substr($this->sourceString, $i, $tswLen) != $tagStartWord)) {
  578. $tagPos = -1;
  579. break;
  580. }
  581. //获取TAG基本信息
  582. for ($j = $tagPos + $startWordLen; $j < $tagPos + $startWordLen + $this->tagMaxLen; $j++) {
  583. if (preg_match("/[ >\/\r\n\t\}\.]/", $this->sourceString[$j])) {
  584. break;
  585. } else {
  586. $ttagName .= $this->sourceString[$j];
  587. }
  588. }
  589. if ($ttagName != '') {
  590. $i = $tagPos + $startWordLen;
  591. $endPos = -1;
  592. //判断 '/}' '{tag:下一标记开始' '{/tag:标记结束' 谁最靠近
  593. $fullTagEndWordThis = $fullTagEndWord . $ttagName . $tagEndWord;
  594. $e1 = strpos($this->sourceString, $sTagEndWord, $i);
  595. $e2 = strpos($this->sourceString, $tagStartWord, $i);
  596. $e3 = strpos($this->sourceString, $fullTagEndWordThis, $i);
  597. $e1 = trim($e1);
  598. $e2 = trim($e2);
  599. $e3 = trim($e3);
  600. $e1 = ($e1 == '' ? '-1' : $e1);
  601. $e2 = ($e2 == '' ? '-1' : $e2);
  602. $e3 = ($e3 == '' ? '-1' : $e3);
  603. if ($e3 == -1) {
  604. //不存在'{/tag:标记'
  605. $endPos = $e1;
  606. $elen = $endPos + strlen($sTagEndWord);
  607. } else if ($e1 == -1) {
  608. //不存在 '/}'
  609. $endPos = $e3;
  610. $elen = $endPos + strlen($fullTagEndWordThis);
  611. }
  612. //同时存在 '/}' 和 '{/tag:标记'
  613. else {
  614. //如果 '/}' 比 '{tag:'、'{/tag:标记' 都要靠近,则认为结束标志是 '/}',否则结束标志为 '{/tag:标记'
  615. if ($e1 < $e2 && $e1 < $e3) {
  616. $endPos = $e1;
  617. $elen = $endPos + strlen($sTagEndWord);
  618. } else {
  619. $endPos = $e3;
  620. $elen = $endPos + strlen($fullTagEndWordThis);
  621. }
  622. }
  623. //如果找不到结束标记,则认为这个标记存在错误
  624. if ($endPos == -1) {
  625. echo "Tpl Character postion $tagPos, '$ttagName' Error!<br />\r\n";
  626. break;
  627. }
  628. $i = $elen;
  629. //分析所找到的标记位置等信息
  630. $attStr = '';
  631. $innerText = '';
  632. $startInner = 0;
  633. for ($j = $tagPos + $startWordLen; $j < $endPos; $j++) {
  634. if ($startInner == 0) {
  635. if ($this->sourceString[$j] == $tagEndWord) {
  636. $startInner = 1;
  637. continue;
  638. } else {
  639. $attStr .= $this->sourceString[$j];
  640. }
  641. } else {
  642. $innerText .= $this->sourceString[$j];
  643. }
  644. }
  645. $ttagName = strtolower($ttagName);
  646. //if、php标记,把整个属性串视为属性
  647. if (preg_match("/^if[0-9]{0,}$/", $ttagName)) {
  648. $cAtt->cAttributes = new TagAttribute();
  649. $cAtt->cAttributes->count = 2;
  650. $cAtt->cAttributes->items['tagname'] = $ttagName;
  651. $cAtt->cAttributes->items['condition'] = preg_replace("/^if[0-9]{0,}[\r\n\t ]/", "", $attStr);
  652. $innerText = preg_replace("/\{else\}/i", '<' . "?php\r\n}\r\nelse{\r\n" . '?' . '>', $innerText);
  653. } else if ($ttagName == 'php') {
  654. $cAtt->cAttributes = new TagAttribute();
  655. $cAtt->cAttributes->count = 2;
  656. $cAtt->cAttributes->items['tagname'] = $ttagName;
  657. $cAtt->cAttributes->items['code'] = '<' . "?php\r\n" . trim(preg_replace(
  658. "/^php[0-9]{0,}[\r\n\t ]/",
  659. "",
  660. $attStr
  661. )) . "\r\n?" . '>';
  662. } else {
  663. //普通标记,解释属性
  664. $cAtt->SetSource($attStr);
  665. }
  666. $this->count++;
  667. $cTag = new Tag();
  668. $cTag->tagName = $ttagName;
  669. $cTag->startPos = $tagPos;
  670. $cTag->endPos = $i;
  671. $cTag->cAtt = $cAtt->cAttributes;
  672. $cTag->isCompiler = FALSE;
  673. $cTag->tagID = $this->count;
  674. $cTag->innerText = $innerText;
  675. $this->cTags[$this->count] = $cTag;
  676. } else {
  677. $i = $tagPos + $startWordLen;
  678. break;
  679. }
  680. } //结束遍历模板字符串
  681. if ($this->count > -1 && $this->isCompiler) {
  682. $this->CompilerAll();
  683. }
  684. }
  685. /**
  686. * 把模板标记转换为PHP代码
  687. *
  688. * @access public
  689. * @return void
  690. */
  691. function CompilerAll()
  692. {
  693. $this->loopNum++;
  694. if ($this->loopNum > 10) {
  695. return; //限制最大递归深度为 10 以防止因标记出错等可能性导致死循环
  696. }
  697. $ResultString = '';
  698. $nextTagEnd = 0;
  699. for ($i = 0; isset($this->cTags[$i]); $i++) {
  700. $ResultString .= substr($this->sourceString, $nextTagEnd, $this->cTags[$i]->startPos - $nextTagEnd);
  701. $ResultString .= $this->CompilerOneTag($this->cTags[$i]);
  702. $nextTagEnd = $this->cTags[$i]->endPos;
  703. }
  704. $slen = strlen($this->sourceString);
  705. if ($slen > $nextTagEnd) {
  706. $ResultString .= substr($this->sourceString, $nextTagEnd, $slen - $nextTagEnd);
  707. }
  708. $this->sourceString = $ResultString;
  709. $this->ParseTemplate();
  710. }
  711. /**
  712. * 获得最终结果
  713. *
  714. * @access public
  715. * @return string
  716. */
  717. function GetResult()
  718. {
  719. if (!$this->isParse) {
  720. $this->ParseTemplate();
  721. }
  722. $addset = '';
  723. $addset .= '<' . '?php' . "\r\n" . 'if(!isset($GLOBALS[\'_vars\'])) $GLOBALS[\'_vars\'] = array(); ' . "\r\n" . '$fields = array();' . "\r\n" . '?' . '>';
  724. return preg_replace("/\?" . ">[ \r\n\t]{0,}<" . "\?php/", "", $addset . $this->sourceString);
  725. }
  726. /**
  727. * 编译单个标记
  728. *
  729. * @access public
  730. * @param string $cTag 标签
  731. * @return string
  732. */
  733. function CompilerOneTag(&$cTag)
  734. {
  735. $cTag->isCompiler = TRUE;
  736. $tagname = $cTag->tagName;
  737. $varname = $cTag->GetAtt('name');
  738. $rsvalue = "";
  739. //用于在模板中设置一个变量以提供作扩展用途
  740. //此变量直接提交到 this->tpCfgs 中,并会生成与模板对应的缓存文件 ***_config.php 文件
  741. if ($tagname == 'config') {
  742. $this->tpCfgs[$varname] = $cTag->GetAtt('value');
  743. } else if ($tagname == 'global') {
  744. $cTag->tagValue = $this->CompilerArrayVar('global', $varname);
  745. if ($cTag->GetAtt('function') != '') {
  746. $cTag->tagValue = $this->CompilerFunction($cTag->GetAtt('function'), $cTag->tagValue);
  747. }
  748. $cTag->tagValue = '<' . '?php echo ' . $cTag->tagValue . '; ?' . '>';
  749. } else if ($tagname == 'cfg') {
  750. $cTag->tagValue = '$GLOBALS[\'cfg_' . $varname . '\']'; //处理函数
  751. if ($cTag->GetAtt('function') != '') {
  752. $cTag->tagValue = $this->CompilerFunction($cTag->GetAtt('function'), $cTag->tagValue);
  753. }
  754. $cTag->tagValue = '<' . '?php echo ' . $cTag->tagValue . '; ?' . '>';
  755. } else if ($tagname == 'name') {
  756. $cTag->tagValue = '$' . $varname; //处理函数
  757. if ($cTag->GetAtt('function') != '') {
  758. $cTag->tagValue = $this->CompilerFunction($cTag->GetAtt('function'), $cTag->tagValue);
  759. }
  760. $cTag->tagValue = '<' . '?php echo ' . $cTag->tagValue . '; ?' . '>';
  761. } else if ($tagname == 'object') {
  762. list($_obs, $_em) = explode('->', $varname);
  763. $cTag->tagValue = "\$GLOBALS['{$_obs}']->{$_em}"; //处理函数
  764. if ($cTag->GetAtt('function') != '') {
  765. $cTag->tagValue = $this->CompilerFunction($cTag->GetAtt('function'), $cTag->tagValue);
  766. }
  767. $cTag->tagValue = '<' . '?php echo ' . $cTag->tagValue . '; ?' . '>';
  768. } else if ($tagname == 'var') {
  769. $cTag->tagValue = $this->CompilerArrayVar('var', $varname);
  770. if ($cTag->GetAtt('function') != '') {
  771. $cTag->tagValue = $this->CompilerFunction($cTag->GetAtt('function'), $cTag->tagValue);
  772. }
  773. // 增加默认空值处理
  774. if ($cTag->GetAtt('default') != '') {
  775. $cTag->tagValue = '<' . '?php echo empty(' . $cTag->tagValue . ')? \'' . addslashes($cTag->GetAtt('default')) . '\':' . $cTag->tagValue . '; ?' . '>';
  776. } else {
  777. $cTag->tagValue = '<' . '?php echo ' . $cTag->tagValue . '; ?' . '>';
  778. }
  779. } else if ($tagname == 'field') {
  780. $cTag->tagValue = '$fields[\'' . $varname . '\']';
  781. if ($cTag->GetAtt('function') != '') {
  782. $cTag->tagValue = $this->CompilerFunction($cTag->GetAtt('function'), $cTag->tagValue);
  783. }
  784. $cTag->tagValue = '<' . '?php echo ' . $cTag->tagValue . '; ?' . '>';
  785. } else if (preg_match("/^key[0-9]{0,}/", $tagname) || preg_match("/^value[0-9]{0,}/", $tagname)) {
  786. if (preg_match("/^value[0-9]{0,}/", $tagname) && $varname != '') {
  787. $cTag->tagValue = '<' . '?php echo ' . $this->CompilerArrayVar($tagname, $varname) . '; ?' . '>';
  788. } else {
  789. $cTag->tagValue = '<' . '?php echo $' . $tagname . '; ?' . '>';
  790. }
  791. } else if (preg_match("/^if[0-9]{0,}$/", $tagname)) {
  792. $cTag->tagValue = $this->CompilerIf($cTag);
  793. } else if ($tagname == 'echo') {
  794. if (trim($cTag->GetInnerText()) == '') $cTag->tagValue = $cTag->GetAtt('code');
  795. else {
  796. $cTag->tagValue = '<' . "?php echo $" . trim($cTag->GetInnerText()) . " ;?" . '>';
  797. }
  798. } else if ($tagname == 'php') {
  799. if (trim($cTag->GetInnerText()) == '') $cTag->tagValue = $cTag->GetAtt('code');
  800. else {
  801. $cTag->tagValue = '<' . "?php\r\n" . trim($cTag->GetInnerText()) . "\r\n?" . '>';
  802. }
  803. }
  804. //遍历数组
  805. else if (preg_match("/^array[0-9]{0,}/", $tagname)) {
  806. $kk = '$key';
  807. $vv = '$value';
  808. if ($cTag->GetAtt('key') != '') {
  809. $kk = '$key' . $cTag->GetAtt('key');
  810. }
  811. if ($cTag->GetAtt('value') != '') {
  812. $vv = '$value' . $cTag->GetAtt('value');
  813. }
  814. $addvar = '';
  815. if (!preg_match("/\(/", $varname)) {
  816. $varname = '$GLOBALS[\'' . $varname . '\']';
  817. } else {
  818. $addvar = "\r\n" . '$myarrs = $pageClass->' . $varname . ";\r\n";
  819. $varname = ' $myarrs ';
  820. }
  821. $rsvalue = '<' . '?php ' . $addvar . ' foreach(' . $varname . ' as ' . $kk . '=>' . $vv . '){ ?' . ">";
  822. $rsvalue .= $cTag->GetInnerText();
  823. $rsvalue .= '<' . '?php } ?' . ">\r\n";
  824. $cTag->tagValue = $rsvalue;
  825. }
  826. //include 文件
  827. else if ($tagname == 'include') {
  828. $filename = $cTag->GetAtt('file');
  829. if ($filename == '') {
  830. $filename = $cTag->GetAtt('filename');
  831. }
  832. $cTag->tagValue = $this->CompilerInclude($filename, FALSE);
  833. if ($cTag->tagValue == 0) $cTag->tagValue = '';
  834. $cTag->tagValue = '<' . '?php include $this->CompilerInclude("' . $filename . '");' . "\r\n" . ' ?' . '>';
  835. } else if ($tagname == 'label') {
  836. $bindFunc = $cTag->GetAtt('bind');
  837. $rsvalue = 'echo ' . $bindFunc . ";\r\n";
  838. $rsvalue = '<' . '?php ' . $rsvalue . ' ?' . ">\r\n";
  839. $cTag->tagValue = $rsvalue;
  840. } else if ($tagname == 'datalist') {
  841. //生成属性数组
  842. foreach ($cTag->cAtt->items as $k => $v) {
  843. $v = $this->TrimAtts($v);
  844. $rsvalue .= '$atts[\'' . $k . '\'] = \'' . str_replace("'", "\\'", $v) . "';\r\n";
  845. }
  846. $rsvalue = '<' . '?php' . "\r\n" . '$atts = array();' . "\r\n" . $rsvalue;
  847. $rsvalue .= '$blockValue = $this->refObj->GetArcList($atts,$this->refObj,$fields); ' . "\r\n";
  848. $rsvalue .= 'if(is_array($blockValue)){' . "\r\n";
  849. $rsvalue .= 'foreach( $blockValue as $key=>$fields )' . "\r\n{\r\n" . '?' . ">";
  850. $rsvalue .= $cTag->GetInnerText();
  851. $rsvalue .= '<' . '?php' . "\r\n}\r\n}" . '?' . '>';
  852. $cTag->tagValue = $rsvalue;
  853. } else if ($tagname == 'pagelist') {
  854. //生成属性数组
  855. foreach ($cTag->cAtt->items as $k => $v) {
  856. $v = $this->TrimAtts($v);
  857. $rsvalue .= '$atts[\'' . $k . '\'] = \'' . str_replace("'", "\\'", $v) . "';\r\n";
  858. }
  859. $rsvalue = '<' . '?php' . "\r\n" . '$atts = array();' . "\r\n" . $rsvalue;
  860. $rsvalue .= ' echo $this->refObj->GetPageList($atts,$this->refObj,$fields); ' . "\r\n" . '?' . ">\r\n";
  861. $cTag->tagValue = $rsvalue;
  862. } else {
  863. $bindFunc = $cTag->GetAtt('bind');
  864. $bindType = $cTag->GetAtt('bindtype');
  865. $rstype = ($cTag->GetAtt('resulttype') == '' ? $cTag->GetAtt('rstype') : $cTag->GetAtt('resulttype'));
  866. $rstype = strtolower($rstype);
  867. //生成属性数组
  868. foreach ($cTag->cAtt->items as $k => $v) {
  869. if (preg_match("/(bind|bindtype)/i", $k)) {
  870. continue;
  871. }
  872. $v = $this->TrimAtts($v);
  873. $rsvalue .= '$atts[\'' . $k . '\'] = \'' . str_replace("'", "\\'", $v) . "';\r\n";
  874. }
  875. $rsvalue = '<' . '?php' . "\r\n" . '$atts = array();' . "\r\n" . $rsvalue;
  876. //绑定到默认函数还是指定函数(datasource属性指定)
  877. if ($bindFunc == '') {
  878. $rsvalue .= '$blockValue = MakePublicTag($atts,$this->refObj,$fields); ' . "\r\n";
  879. } else {
  880. //自定义绑定函数如果不指定 bindtype,则指向$this->refObj->绑定函数名,即是默认指向被引用的类对象
  881. if ($bindType == '') $rsvalue .= '$blockValue = $this->refObj->' . $bindFunc . '($atts,$this->refObj,$fields); ' . "\r\n";
  882. else $rsvalue .= '$blockValue = ' . $bindFunc . '($atts,$this->refObj,$fields); ' . "\r\n";
  883. }
  884. //返回结果类型:默认为 array 是一个二维数组,string 是字符串
  885. if ($rstype == 'string') {
  886. $rsvalue .= 'echo $blockValue;' . "\r\n" . '?' . ">";
  887. } else {
  888. $rsvalue .= 'if(is_array($blockValue) && count($blockValue) > 0){' . "\r\n";
  889. $rsvalue .= 'foreach( $blockValue as $key=>$fields )' . "\r\n{\r\n" . '?' . ">";
  890. $rsvalue .= $cTag->GetInnerText();
  891. $rsvalue .= '<' . '?php' . "\r\n}\r\n}\r\n" . '?' . '>';
  892. }
  893. $cTag->tagValue = $rsvalue;
  894. }
  895. return $cTag->tagValue;
  896. }
  897. /**
  898. * 编译可能为数组的变量
  899. *
  900. * @access public
  901. * @param string $vartype 变量类型
  902. * @param string $varname 变量名称
  903. * @return string
  904. */
  905. function CompilerArrayVar($vartype, $varname)
  906. {
  907. $okvalue = '';
  908. if (!preg_match("/\[/", $varname)) {
  909. if (preg_match("/^value/", $vartype)) {
  910. $varname = $vartype . '.' . $varname;
  911. }
  912. $varnames = explode('.', $varname);
  913. if (isset($varnames[1])) {
  914. $varname = $varnames[0];
  915. for ($i = 1; isset($varnames[$i]); $i++) {
  916. $varname .= "['" . $varnames[$i] . "']";
  917. }
  918. }
  919. }
  920. if (preg_match("/\[/", $varname)) {
  921. $varnames = explode('[', $varname);
  922. $arrend = '';
  923. for ($i = 1; isset($varnames[$i]); $i++) {
  924. $arrend .= '[' . $varnames[$i];
  925. }
  926. if (!preg_match("/[\"']/", $arrend)) {
  927. $arrend = str_replace('[', '', $arrend);
  928. $arrend = str_replace(']', '', $arrend);
  929. $arrend = "['{$arrend}']";
  930. }
  931. if ($vartype == 'var') {
  932. $okvalue = '$GLOBALS[\'_vars\'][\'' . $varnames[0] . '\']' . $arrend;
  933. } else if (preg_match("/^value/", $vartype)) {
  934. $okvalue = '$' . $varnames[0] . $arrend;
  935. } else if ($vartype == 'field') {
  936. $okvalue = '$fields[\'' . $varnames[0] . '\']' . $arrend;
  937. } else {
  938. $okvalue = '$GLOBALS[\'' . $varnames[0] . '\']' . $arrend;
  939. }
  940. } else {
  941. if ($vartype == 'var') {
  942. $okvalue = '$GLOBALS[\'_vars\'][\'' . $varname . '\']';
  943. } else if (preg_match("/^value/", $vartype)) {
  944. $okvalue = '$' . $vartype;
  945. } else if ($vartype == 'field') {
  946. $okvalue = '$' . str_replace($varname);
  947. } else {
  948. $okvalue = '$GLOBALS[\'' . $varname . '\']';
  949. }
  950. }
  951. return $okvalue;
  952. }
  953. /**
  954. * 编译if标记
  955. *
  956. * @access public
  957. * @param string $cTag 标签
  958. * @return string
  959. */
  960. function CompilerIf($cTag)
  961. {
  962. $condition = trim($cTag->GetAtt('condition'));
  963. if ($condition == '') {
  964. $cTag->tagValue = '';
  965. return '';
  966. }
  967. if (version_compare(PHP_VERSION, '5.5.0', '>=')) {
  968. $condition = preg_replace_callback("/((var\.|field\.|cfg\.|global\.|key[0-9]{0,}\.|value[0-9]{0,}\.)[\._a-z0-9]+)/is", "private_rt", $condition);
  969. } else {
  970. $condition = preg_replace("/((var\.|field\.|cfg\.|global\.|key[0-9]{0,}\.|value[0-9]{0,}\.)[\._a-z0-9]+)/ies", "private_rt('\\1')", $condition);
  971. }
  972. $rsvalue = '<' . '?php if(' . $condition . '){ ?' . '>';
  973. $rsvalue .= $cTag->GetInnerText();
  974. $rsvalue .= '<' . '?php } ?' . '>';
  975. return $rsvalue;
  976. }
  977. /**
  978. * 处理block区块传递的atts属性的值
  979. *
  980. * @access public
  981. * @param string $v 值
  982. * @return string
  983. */
  984. function TrimAtts($v)
  985. {
  986. $v = str_replace('<' . '?', '&lt;?', $v);
  987. $v = str_replace('?' . '>', '?&gt;', $v);
  988. return $v;
  989. }
  990. /**
  991. * 函数 function 语法处理
  992. *
  993. * @access public
  994. * @param string $funcstr 函数字符串
  995. * @param string $nvalue 函数值
  996. * @return string
  997. */
  998. function CompilerFunction($funcstr, $nvalue)
  999. {
  1000. $funcstr = str_replace('@quote', '"', $funcstr);
  1001. $funcstr = str_replace('@me', $nvalue, $funcstr);
  1002. return $funcstr;
  1003. }
  1004. /**
  1005. * 引入文件 include 语法处理
  1006. *
  1007. * @access public
  1008. * @param string $filename 文件名
  1009. * @param string $isload 是否载入
  1010. * @return string
  1011. */
  1012. function CompilerInclude($filename, $isload = TRUE)
  1013. {
  1014. $okfile = '';
  1015. if (@file_exists($filename)) {
  1016. $okfile = $filename;
  1017. } else if (@file_exists($this->refDir . $filename)) {
  1018. $okfile = $this->refDir . $filename;
  1019. } else if (@file_exists($this->refDir . "../" . $filename)) {
  1020. $okfile = $this->refDir . "../" . $filename;
  1021. }
  1022. if ($okfile == '') return 0;
  1023. if (!$isload) return 1;
  1024. $itpl = new DedeTemplate($this->templateDir);
  1025. $itpl->isCache = $this->isCache;
  1026. $itpl->SetObject($this->refObj);
  1027. $itpl->LoadTemplate($okfile);
  1028. return $itpl->CacheFile();
  1029. }
  1030. }
  1031. /**
  1032. * class TagAttribute Tag属性集合
  1033. * function C__TagAttribute();
  1034. * 属性的数据描述
  1035. *
  1036. * @package TagAttribute
  1037. * @subpackage DedeBIZ.Libraries
  1038. * @link https://www.dedebiz.com
  1039. */
  1040. class TagAttribute
  1041. {
  1042. var $count = -1;
  1043. var $items = array(); //属性元素的集合
  1044. /**
  1045. * 获得某个属性
  1046. *
  1047. * @access public
  1048. * @param string $str 预处理字符串
  1049. * @return string
  1050. */
  1051. function GetAtt($str)
  1052. {
  1053. if ($str == "") {
  1054. return "";
  1055. }
  1056. if (isset($this->items[$str])) {
  1057. return $this->items[$str];
  1058. } else {
  1059. return "";
  1060. }
  1061. }
  1062. /**
  1063. * 同上
  1064. *
  1065. * @access public
  1066. * @param string $str 预处理字符串
  1067. * @return string
  1068. */
  1069. function GetAttribute($str)
  1070. {
  1071. return $this->GetAtt($str);
  1072. }
  1073. /**
  1074. * 判断属性是否存在
  1075. *
  1076. * @access public
  1077. * @param string $str 预处理字符串
  1078. * @return bool
  1079. */
  1080. function IsAttribute($str)
  1081. {
  1082. if (isset($this->items[$str])) return TRUE;
  1083. else return FALSE;
  1084. }
  1085. /**
  1086. * 获得标记名称
  1087. *
  1088. * @access public
  1089. * @return string
  1090. */
  1091. function GettagName()
  1092. {
  1093. return $this->GetAtt("tagname");
  1094. }
  1095. /**
  1096. * 获得属性个数
  1097. *
  1098. * @access public
  1099. * @return int
  1100. */
  1101. function Getcount()
  1102. {
  1103. return $this->count + 1;
  1104. }
  1105. } //End Class
  1106. /**
  1107. * 属性解析器
  1108. * function C__TagAttributeParse();
  1109. *
  1110. * @package TagAttribute
  1111. * @subpackage DedeBIZ.Libraries
  1112. * @link https://www.dedebiz.com
  1113. */
  1114. class TagAttributeParse
  1115. {
  1116. var $sourceString = "";
  1117. var $sourceMaxSize = 1024;
  1118. var $cAttributes = array();
  1119. var $charToLow = TRUE;
  1120. function SetSource($str = "")
  1121. {
  1122. $this->cAttributes = new TagAttribute();
  1123. $strLen = 0;
  1124. $this->sourceString = trim(preg_replace("/[ \r\n\t\f]{1,}/", " ", $str));
  1125. $strLen = strlen($this->sourceString);
  1126. if ($strLen > 0 && $strLen <= $this->sourceMaxSize) {
  1127. $this->ParseAttribute();
  1128. }
  1129. }
  1130. /**
  1131. * 解析属性
  1132. *
  1133. * @access public
  1134. * @return void
  1135. */
  1136. function ParseAttribute()
  1137. {
  1138. $d = '';
  1139. $tmpatt = '';
  1140. $tmpvalue = '';
  1141. $startdd = -1;
  1142. $ddtag = '';
  1143. $hasAttribute = FALSE;
  1144. $strLen = strlen($this->sourceString);
  1145. $this->cAttributes->items = array();
  1146. // 获得Tag的名称,解析到 cAtt->GetAtt('tagname') 中
  1147. for ($i = 0; $i < $strLen; $i++) {
  1148. if ($this->sourceString[$i] == ' ') {
  1149. $this->cAttributes->count++;
  1150. $tmpvalues = explode('.', $tmpvalue);
  1151. $this->cAttributes->items['tagname'] = ($this->charToLow ? strtolower($tmpvalues[0]) : $tmpvalues[0]);
  1152. if (isset($tmpvalues[2])) {
  1153. $okname = $tmpvalues[1];
  1154. for ($j = 2; isset($tmpvalues[$j]); $j++) {
  1155. $okname .= "['" . $tmpvalues[$j] . "']";
  1156. }
  1157. $this->cAttributes->items['name'] = $okname;
  1158. } else if (isset($tmpvalues[1]) && $tmpvalues[1] != '') {
  1159. $this->cAttributes->items['name'] = $tmpvalues[1];
  1160. }
  1161. $tmpvalue = '';
  1162. $hasAttribute = TRUE;
  1163. break;
  1164. } else {
  1165. $tmpvalue .= $this->sourceString[$i];
  1166. }
  1167. }
  1168. //不存在属性列表的情况
  1169. if (!$hasAttribute) {
  1170. $this->cAttributes->count++;
  1171. $tmpvalues = explode('.', $tmpvalue);
  1172. $this->cAttributes->items['tagname'] = ($this->charToLow ? strtolower($tmpvalues[0]) : $tmpvalues[0]);
  1173. if (isset($tmpvalues[2])) {
  1174. $okname = $tmpvalues[1];
  1175. for ($i = 2; isset($tmpvalues[$i]); $i++) {
  1176. $okname .= "['" . $tmpvalues[$i] . "']";
  1177. }
  1178. $this->cAttributes->items['name'] = $okname;
  1179. } else if (isset($tmpvalues[1]) && $tmpvalues[1] != '') {
  1180. $this->cAttributes->items['name'] = $tmpvalues[1];
  1181. }
  1182. return;
  1183. }
  1184. $tmpvalue = '';
  1185. //如果字符串含有属性值,遍历源字符串,并获得各属性
  1186. for ($i; $i < $strLen; $i++) {
  1187. $d = $this->sourceString[$i];
  1188. //查找属性名称
  1189. if ($startdd == -1) {
  1190. if ($d != '=') {
  1191. $tmpatt .= $d;
  1192. } else {
  1193. if ($this->charToLow) {
  1194. $tmpatt = strtolower(trim($tmpatt));
  1195. } else {
  1196. $tmpatt = trim($tmpatt);
  1197. }
  1198. $startdd = 0;
  1199. }
  1200. }
  1201. //查找属性的限定标志
  1202. else if ($startdd == 0) {
  1203. switch ($d) {
  1204. case ' ':
  1205. break;
  1206. case '\'':
  1207. $ddtag = '\'';
  1208. $startdd = 1;
  1209. break;
  1210. case '"':
  1211. $ddtag = '"';
  1212. $startdd = 1;
  1213. break;
  1214. default:
  1215. $tmpvalue .= $d;
  1216. $ddtag = ' ';
  1217. $startdd = 1;
  1218. break;
  1219. }
  1220. } else if ($startdd == 1) {
  1221. if ($d == $ddtag && (isset($this->sourceString[$i - 1]) && $this->sourceString[$i - 1] != "\\")) {
  1222. $this->cAttributes->count++;
  1223. $this->cAttributes->items[$tmpatt] = trim($tmpvalue);
  1224. $tmpatt = '';
  1225. $tmpvalue = '';
  1226. $startdd = -1;
  1227. } else {
  1228. $tmpvalue .= $d;
  1229. }
  1230. }
  1231. } //for
  1232. //最后一个属性的给值
  1233. if ($tmpatt != '') {
  1234. $this->cAttributes->count++;
  1235. $this->cAttributes->items[$tmpatt] = trim($tmpvalue);
  1236. } //print_r($this->cAttributes->items);
  1237. } // end func
  1238. } //End Class
  1239. /**
  1240. * 私有标签编译,主要用于if标签内的字符串解析
  1241. *
  1242. * @access public
  1243. * @param string $str 需要编译的字符串
  1244. * @return string
  1245. */
  1246. function private_rt($str)
  1247. {
  1248. if (is_array($str)) {
  1249. $arr = explode('.', $str[0]);
  1250. } else {
  1251. $arr = explode('.', $str);
  1252. }
  1253. $rs = '$GLOBALS[\'';
  1254. if ($arr[0] == 'cfg') {
  1255. return $rs . 'cfg_' . $arr[1] . "']";
  1256. } elseif ($arr[0] == 'var') {
  1257. $arr[0] = '_vars';
  1258. $rs .= implode('\'][\'', $arr);
  1259. $rs .= "']";
  1260. return $rs;
  1261. } elseif ($arr[0] == 'global') {
  1262. unset($arr[0]);
  1263. $rs .= implode('\'][\'', $arr);
  1264. $rs .= "']";
  1265. return $rs;
  1266. } else {
  1267. if ($arr[0] == 'field') $arr[0] = 'fields';
  1268. $rs = '$' . $arr[0] . "['";
  1269. unset($arr[0]);
  1270. $rs .= implode('\'][\'', $arr);
  1271. $rs .= "']";
  1272. return $rs;
  1273. }
  1274. }