PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/phpbb3/functions_template.php

https://bitbucket.org/renaatdemuynck/chamilo
PHP | 818 lines | 510 code | 131 blank | 177 comment | 66 complexity | 326ca8a48d71ad9aa48ec40780e6033b MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT, GPL-2.0
  1. <?php
  2. /**
  3. *
  4. * @package phpBB3
  5. * @version $Id: functions_template.php 10064 2009-08-30 11:15:24Z acydburn $
  6. * @copyright (c) 2005 phpBB Group, sections (c) 2001 ispi of Lincoln Inc
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * @ignore
  12. */
  13. //if (!defined('IN_PHPBB'))
  14. //{
  15. // exit;
  16. //}
  17. /**
  18. * Extension of template class - Functions needed for compiling templates only.
  19. *
  20. * psoTFX, phpBB Development Team - Completion of file caching, decompilation
  21. * routines and implementation of conditionals/keywords and associated changes
  22. *
  23. * The interface was inspired by PHPLib templates, and the template file (formats are
  24. * quite similar)
  25. *
  26. * The keyword/conditional implementation is currently based on sections of code from
  27. * the Smarty templating engine (c) 2001 ispi of Lincoln, Inc. which is released
  28. * (on its own and in whole) under the LGPL. Section 3 of the LGPL states that any code
  29. * derived from an LGPL application may be relicenced under the GPL, this applies
  30. * to this source
  31. *
  32. * DEFINE directive inspired by a request by Cyberalien
  33. *
  34. * @package phpBB3
  35. */
  36. class template_compile
  37. {
  38. var $template;
  39. // Various storage arrays
  40. var $block_names = array();
  41. var $block_else_level = array();
  42. /**
  43. * constuctor
  44. */
  45. function __construct(&$template)
  46. {
  47. $this->template = &$template;
  48. }
  49. /**
  50. * Load template source from file
  51. * @access private
  52. */
  53. function _tpl_load_file($handle, $store_in_db = false)
  54. {
  55. // Try and open template for read
  56. if (! file_exists($this->template->files[$handle]))
  57. {
  58. trigger_error("template->_tpl_load_file(): File {$this->template->files[$handle]} does not exist or is empty", E_USER_ERROR);
  59. }
  60. $this->template->compiled_code[$handle] = $this->compile(trim(@file_get_contents($this->template->files[$handle])));
  61. // Actually compile the code now.
  62. $this->compile_write($handle, $this->template->compiled_code[$handle]);
  63. // Store in database if required...
  64. if ($store_in_db)
  65. {
  66. global $db, $user;
  67. $sql_ary = array('template_id' => $this->template->files_template[$handle],
  68. 'template_filename' => $this->template->filename[$handle], 'template_included' => '',
  69. 'template_mtime' => time(),
  70. 'template_data' => trim(@file_get_contents($this->template->files[$handle])));
  71. $sql = 'INSERT INTO ' . STYLES_TEMPLATE_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
  72. $db->sql_query($sql);
  73. }
  74. }
  75. /**
  76. * Remove any PHP tags that do not belong, these regular expressions are derived from
  77. * the ones that exist in zend_language_scanner.l
  78. * @access private
  79. */
  80. function remove_php_tags(&$code)
  81. {
  82. // This matches the information gathered from the internal PHP lexer
  83. $match = array('#<([\?%])=?.*?\1>#s', '#<script\s+language\s*=\s*(["\']?)php\1\s*>.*?</script\s*>#s',
  84. '#<\?php(?:\r\n?|[ \n\t]).*?\?>#s');
  85. $code = preg_replace($match, '', $code);
  86. }
  87. /**
  88. * The all seeing all doing compile method. Parts are inspired by or directly from Smarty
  89. * @access private
  90. */
  91. function compile($code, $no_echo = false, $echo_var = '')
  92. {
  93. global $config;
  94. if ($echo_var)
  95. {
  96. global $$echo_var;
  97. }
  98. // Remove any "loose" php ... we want to give admins the ability
  99. // to switch on/off PHP for a given template. Allowing unchecked
  100. // php is a no-no. There is a potential issue here in that non-php
  101. // content may be removed ... however designers should use entities
  102. // if they wish to display < and >
  103. $this->remove_php_tags($code);
  104. // dump($code);
  105. // Pull out all block/statement level elements and separate plain text
  106. preg_match_all('#<!-- PHP -->(.*?)<!-- ENDPHP -->#s', $code, $matches);
  107. $php_blocks = $matches[1];
  108. $code = preg_replace('#<!-- PHP -->.*?<!-- ENDPHP -->#s', '<!-- PHP -->', $code);
  109. preg_match_all('#<!-- INCLUDE (\{\$?[A-Z0-9\-_]+\}|[a-zA-Z0-9\_\-\+\./]+) -->#', $code, $matches);
  110. $include_blocks = $matches[1];
  111. $code = preg_replace('#<!-- INCLUDE (?:\{\$?[A-Z0-9\-_]+\}|[a-zA-Z0-9\_\-\+\./]+) -->#', '<!-- INCLUDE -->', $code);
  112. preg_match_all('#<!-- INCLUDEPHP ([a-zA-Z0-9\_\-\+\./]+) -->#', $code, $matches);
  113. $includephp_blocks = $matches[1];
  114. $code = preg_replace('#<!-- INCLUDEPHP [a-zA-Z0-9\_\-\+\./]+ -->#', '<!-- INCLUDEPHP -->', $code);
  115. preg_match_all('#<!-- ([^<].*?) (.*?)? ?-->#', $code, $blocks, PREG_SET_ORDER);
  116. $text_blocks = preg_split('#<!-- [^<].*? (?:.*?)? ?-->#', $code);
  117. for($i = 0, $j = sizeof($text_blocks); $i < $j; $i ++)
  118. {
  119. $this->compile_var_tags($text_blocks[$i]);
  120. }
  121. $compile_blocks = array();
  122. for($curr_tb = 0, $tb_size = sizeof($blocks); $curr_tb < $tb_size; $curr_tb ++)
  123. {
  124. $block_val = &$blocks[$curr_tb];
  125. switch ($block_val[1])
  126. {
  127. case 'BEGIN' :
  128. $this->block_else_level[] = false;
  129. $compile_blocks[] = '<?php ' . $this->compile_tag_block($block_val[2]) . ' ?>';
  130. break;
  131. case 'BEGINELSE' :
  132. $this->block_else_level[sizeof($this->block_else_level) - 1] = true;
  133. $compile_blocks[] = '<?php }} else { ?>';
  134. break;
  135. case 'END' :
  136. array_pop($this->block_names);
  137. $compile_blocks[] = '<?php ' . ((array_pop($this->block_else_level)) ? '}' : '}}') . ' ?>';
  138. break;
  139. case 'IF' :
  140. $compile_blocks[] = '<?php ' . $this->compile_tag_if($block_val[2], false) . ' ?>';
  141. break;
  142. case 'ELSE' :
  143. $compile_blocks[] = '<?php } else { ?>';
  144. break;
  145. case 'ELSEIF' :
  146. $compile_blocks[] = '<?php ' . $this->compile_tag_if($block_val[2], true) . ' ?>';
  147. break;
  148. case 'ENDIF' :
  149. $compile_blocks[] = '<?php } ?>';
  150. break;
  151. case 'DEFINE' :
  152. $compile_blocks[] = '<?php ' . $this->compile_tag_define($block_val[2], true) . ' ?>';
  153. break;
  154. case 'UNDEFINE' :
  155. $compile_blocks[] = '<?php ' . $this->compile_tag_define($block_val[2], false) . ' ?>';
  156. break;
  157. case 'INCLUDE' :
  158. $temp = array_shift($include_blocks);
  159. // Dynamic includes
  160. // Cheap match rather than a full blown regexp, we already know
  161. // the format of the input so just use string manipulation.
  162. if ($temp[0] == '{')
  163. {
  164. $file = false;
  165. if ($temp[1] == '$')
  166. {
  167. $var = substr($temp, 2, - 1);
  168. //$file = $this->template->_tpldata['DEFINE']['.'][$var];
  169. $temp = "\$this->_tpldata['DEFINE']['.']['$var']";
  170. }
  171. else
  172. {
  173. $var = substr($temp, 1, - 1);
  174. //$file = $this->template->_rootref[$var];
  175. $temp = "\$this->_rootref['$var']";
  176. }
  177. }
  178. else
  179. {
  180. $file = $temp;
  181. }
  182. $compile_blocks[] = '<?php ' . $this->compile_tag_include($temp) . ' ?>';
  183. // No point in checking variable includes
  184. if ($file)
  185. {
  186. $this->template->_tpl_include($file, false);
  187. }
  188. break;
  189. case 'INCLUDEPHP' :
  190. $compile_blocks[] = ($config['tpl_allow_php']) ? '<?php ' . $this->compile_tag_include_php(array_shift($includephp_blocks)) . ' ?>' : '';
  191. break;
  192. case 'PHP' :
  193. $compile_blocks[] = ($config['tpl_allow_php']) ? '<?php ' . array_shift($php_blocks) . ' ?>' : '';
  194. break;
  195. default :
  196. $this->compile_var_tags($block_val[0]);
  197. $trim_check = trim($block_val[0]);
  198. $compile_blocks[] = (! $no_echo) ? ((! empty($trim_check)) ? $block_val[0] : '') : ((! empty($trim_check)) ? $block_val[0] : '');
  199. break;
  200. }
  201. }
  202. $template_php = '';
  203. for($i = 0, $size = sizeof($text_blocks); $i < $size; $i ++)
  204. {
  205. $trim_check_text = trim($text_blocks[$i]);
  206. $template_php .= (! $no_echo) ? (($trim_check_text != '') ? $text_blocks[$i] : '') . ((isset($compile_blocks[$i])) ? $compile_blocks[$i] : '') : (($trim_check_text != '') ? $text_blocks[$i] : '') . ((isset($compile_blocks[$i])) ? $compile_blocks[$i] : '');
  207. }
  208. // Remove unused opening/closing tags
  209. $template_php = str_replace(' ?><?php ', ' ', $template_php);
  210. // Now add a newline after each php closing tag which already has a newline
  211. // PHP itself strips a newline if a closing tag is used (this is documented behaviour) and it is mostly not intended by style authors to remove newlines
  212. $template_php = preg_replace('#\?\>([\r\n])#', '?>\1\1', $template_php);
  213. // There will be a number of occasions where we switch into and out of
  214. // PHP mode instantaneously. Rather than "burden" the parser with this
  215. // we'll strip out such occurences, minimising such switching
  216. if ($no_echo)
  217. {
  218. return "\$$echo_var .= '" . $template_php . "'";
  219. }
  220. return $template_php;
  221. }
  222. /**
  223. * Compile variables
  224. * @access private
  225. */
  226. function compile_var_tags(&$text_blocks)
  227. {
  228. // change template varrefs into PHP varrefs
  229. $varrefs = array();
  230. // This one will handle varrefs WITH namespaces
  231. preg_match_all('#\{((?:[a-z0-9\-_]+\.)+)(\$)?([A-Z0-9\-_]+)\}#', $text_blocks, $varrefs, PREG_SET_ORDER);
  232. foreach ($varrefs as $var_val)
  233. {
  234. $namespace = $var_val[1];
  235. $varname = $var_val[3];
  236. $new = $this->generate_block_varref($namespace, $varname, true, $var_val[2]);
  237. $text_blocks = str_replace($var_val[0], $new, $text_blocks);
  238. }
  239. // This will handle the remaining root-level varrefs
  240. // transform vars prefixed by L_ into their language variable pendant if nothing is set within the tpldata array
  241. if (strpos($text_blocks, '{L_') !== false)
  242. {
  243. $text_blocks = preg_replace('#\{L_([A-Z0-9\-_]+)\}#', "<?php echo ((isset(\$this->_rootref['L_\\1'])) ? \$this->_rootref['L_\\1'] : ((isset(\$user->lang['\\1'])) ? \$user->lang['\\1'] : '{ \\1 }')); ?>", $text_blocks);
  244. }
  245. // Handle addslashed language variables prefixed with LA_
  246. // If a template variable already exist, it will be used in favor of it...
  247. if (strpos($text_blocks, '{LA_') !== false)
  248. {
  249. $text_blocks = preg_replace('#\{LA_([A-Z0-9\-_]+)\}#', "<?php echo ((isset(\$this->_rootref['LA_\\1'])) ? \$this->_rootref['LA_\\1'] : ((isset(\$this->_rootref['L_\\1'])) ? addslashes(\$this->_rootref['L_\\1']) : ((isset(\$user->lang['\\1'])) ? addslashes(\$user->lang['\\1']) : '{ \\1 }'))); ?>", $text_blocks);
  250. }
  251. // Handle remaining varrefs
  252. $text_blocks = preg_replace('#\{([A-Z0-9\-_]+)\}#', "<?php echo (isset(\$this->_rootref['\\1'])) ? \$this->_rootref['\\1'] : ''; ?>", $text_blocks);
  253. $text_blocks = preg_replace('#\{\$([A-Z0-9\-_]+)\}#', "<?php echo (isset(\$this->_tpldata['DEFINE']['.']['\\1'])) ? \$this->_tpldata['DEFINE']['.']['\\1'] : ''; ?>", $text_blocks);
  254. return;
  255. }
  256. /**
  257. * Compile blocks
  258. * @access private
  259. */
  260. function compile_tag_block($tag_args)
  261. {
  262. $no_nesting = false;
  263. // Is the designer wanting to call another loop in a loop?
  264. if (strpos($tag_args, '!') === 0)
  265. {
  266. // Count the number if ! occurrences (not allowed in vars)
  267. $no_nesting = substr_count($tag_args, '!');
  268. $tag_args = substr($tag_args, $no_nesting);
  269. }
  270. // Allow for control of looping (indexes start from zero):
  271. // foo(2) : Will start the loop on the 3rd entry
  272. // foo(-2) : Will start the loop two entries from the end
  273. // foo(3,4) : Will start the loop on the fourth entry and end it on the fifth
  274. // foo(3,-4) : Will start the loop on the fourth entry and end it four from last
  275. if (preg_match('#^([^()]*)\(([\-\d]+)(?:,([\-\d]+))?\)$#', $tag_args, $match))
  276. {
  277. $tag_args = $match[1];
  278. if ($match[2] < 0)
  279. {
  280. $loop_start = '($_' . $tag_args . '_count ' . $match[2] . ' < 0 ? 0 : $_' . $tag_args . '_count ' . $match[2] . ')';
  281. }
  282. else
  283. {
  284. $loop_start = '($_' . $tag_args . '_count < ' . $match[2] . ' ? $_' . $tag_args . '_count : ' . $match[2] . ')';
  285. }
  286. if (strlen($match[3]) < 1 || $match[3] == - 1)
  287. {
  288. $loop_end = '$_' . $tag_args . '_count';
  289. }
  290. else
  291. if ($match[3] >= 0)
  292. {
  293. $loop_end = '(' . ($match[3] + 1) . ' > $_' . $tag_args . '_count ? $_' . $tag_args . '_count : ' . ($match[3] + 1) . ')';
  294. }
  295. else //if ($match[3] < -1)
  296. {
  297. $loop_end = '$_' . $tag_args . '_count' . ($match[3] + 1);
  298. }
  299. }
  300. else
  301. {
  302. $loop_start = 0;
  303. $loop_end = '$_' . $tag_args . '_count';
  304. }
  305. $tag_template_php = '';
  306. array_push($this->block_names, $tag_args);
  307. if ($no_nesting !== false)
  308. {
  309. // We need to implode $no_nesting times from the end...
  310. $block = array_slice($this->block_names, - $no_nesting);
  311. }
  312. else
  313. {
  314. $block = $this->block_names;
  315. }
  316. if (sizeof($block) < 2)
  317. {
  318. // Block is not nested.
  319. $tag_template_php = '$_' . $tag_args . "_count = (isset(\$this->_tpldata['$tag_args'])) ? sizeof(\$this->_tpldata['$tag_args']) : 0;";
  320. $varref = "\$this->_tpldata['$tag_args']";
  321. }
  322. else
  323. {
  324. // This block is nested.
  325. // Generate a namespace string for this block.
  326. $namespace = implode('.', $block);
  327. // Get a reference to the data array for this block that depends on the
  328. // current indices of all parent blocks.
  329. $varref = $this->generate_block_data_ref($namespace, false);
  330. // Create the for loop code to iterate over this block.
  331. $tag_template_php = '$_' . $tag_args . '_count = (isset(' . $varref . ')) ? sizeof(' . $varref . ') : 0;';
  332. }
  333. $tag_template_php .= 'if ($_' . $tag_args . '_count) {';
  334. /**
  335. * The following uses foreach for iteration instead of a for loop, foreach is faster but requires PHP to make a copy of the contents of the array which uses more memory
  336. * <code>
  337. * if (!$offset)
  338. * {
  339. * $tag_template_php .= 'foreach (' . $varref . ' as $_' . $tag_args . '_i => $_' . $tag_args . '_val){';
  340. * }
  341. * </code>
  342. */
  343. $tag_template_php .= 'for ($_' . $tag_args . '_i = ' . $loop_start . '; $_' . $tag_args . '_i < ' . $loop_end . '; ++$_' . $tag_args . '_i){';
  344. $tag_template_php .= '$_' . $tag_args . '_val = &' . $varref . '[$_' . $tag_args . '_i];';
  345. return $tag_template_php;
  346. }
  347. /**
  348. * Compile IF tags - much of this is from Smarty with
  349. * some adaptions for our block level methods
  350. * @access private
  351. */
  352. function compile_tag_if($tag_args, $elseif)
  353. {
  354. // Tokenize args for 'if' tag.
  355. preg_match_all('/(?:
  356. "[^"\\\\]*(?:\\\\.[^"\\\\]*)*" |
  357. \'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\' |
  358. [(),] |
  359. [^\s(),]+)/x', $tag_args, $match);
  360. $tokens = $match[0];
  361. $is_arg_stack = array();
  362. for($i = 0, $size = sizeof($tokens); $i < $size; $i ++)
  363. {
  364. $token = &$tokens[$i];
  365. switch ($token)
  366. {
  367. case '!==' :
  368. case '===' :
  369. case '<<' :
  370. case '>>' :
  371. case '|' :
  372. case '^' :
  373. case '&' :
  374. case '~' :
  375. case ')' :
  376. case ',' :
  377. case '+' :
  378. case '-' :
  379. case '*' :
  380. case '/' :
  381. case '@' :
  382. break;
  383. case '==' :
  384. case 'eq' :
  385. $token = '==';
  386. break;
  387. case '!=' :
  388. case '<>' :
  389. case 'ne' :
  390. case 'neq' :
  391. $token = '!=';
  392. break;
  393. case '<' :
  394. case 'lt' :
  395. $token = '<';
  396. break;
  397. case '<=' :
  398. case 'le' :
  399. case 'lte' :
  400. $token = '<=';
  401. break;
  402. case '>' :
  403. case 'gt' :
  404. $token = '>';
  405. break;
  406. case '>=' :
  407. case 'ge' :
  408. case 'gte' :
  409. $token = '>=';
  410. break;
  411. case '&&' :
  412. case 'and' :
  413. $token = '&&';
  414. break;
  415. case '||' :
  416. case 'or' :
  417. $token = '||';
  418. break;
  419. case '!' :
  420. case 'not' :
  421. $token = '!';
  422. break;
  423. case '%' :
  424. case 'mod' :
  425. $token = '%';
  426. break;
  427. case '(' :
  428. array_push($is_arg_stack, $i);
  429. break;
  430. case 'is' :
  431. $is_arg_start = ($tokens[$i - 1] == ')') ? array_pop($is_arg_stack) : $i - 1;
  432. $is_arg = implode(' ', array_slice($tokens, $is_arg_start, $i - $is_arg_start));
  433. $new_tokens = $this->_parse_is_expr($is_arg, array_slice($tokens, $i + 1));
  434. array_splice($tokens, $is_arg_start, sizeof($tokens), $new_tokens);
  435. $i = $is_arg_start;
  436. // no break
  437. default :
  438. if (preg_match('#^((?:[a-z0-9\-_]+\.)+)?(\$)?(?=[A-Z])([A-Z0-9\-_]+)#s', $token, $varrefs))
  439. {
  440. $token = (! empty($varrefs[1])) ? $this->generate_block_data_ref(substr($varrefs[1], 0, - 1), true, $varrefs[2]) . '[\'' . $varrefs[3] . '\']' : (($varrefs[2]) ? '$this->_tpldata[\'DEFINE\'][\'.\'][\'' . $varrefs[3] . '\']' : '$this->_rootref[\'' . $varrefs[3] . '\']');
  441. }
  442. else
  443. if (preg_match('#^\.((?:[a-z0-9\-_]+\.?)+)$#s', $token, $varrefs))
  444. {
  445. // Allow checking if loops are set with .loopname
  446. // It is also possible to check the loop count by doing <!-- IF .loopname > 1 --> for example
  447. $blocks = explode('.', $varrefs[1]);
  448. // If the block is nested, we have a reference that we can grab.
  449. // If the block is not nested, we just go and grab the block from _tpldata
  450. if (sizeof($blocks) > 1)
  451. {
  452. $block = array_pop($blocks);
  453. $namespace = implode('.', $blocks);
  454. $varref = $this->generate_block_data_ref($namespace, true);
  455. // Add the block reference for the last child.
  456. $varref .= "['" . $block . "']";
  457. }
  458. else
  459. {
  460. $varref = '$this->_tpldata';
  461. // Add the block reference for the last child.
  462. $varref .= "['" . $blocks[0] . "']";
  463. }
  464. $token = "sizeof($varref)";
  465. }
  466. else
  467. if (! empty($token))
  468. {
  469. $token = '(' . $token . ')';
  470. }
  471. break;
  472. }
  473. }
  474. // If there are no valid tokens left or only control/compare characters left, we do skip this statement
  475. if (! sizeof($tokens) || str_replace(array(' ', '=', '!', '<', '>', '&', '|', '%', '(', ')'), '', implode('', $tokens)) == '')
  476. {
  477. $tokens = array('false');
  478. }
  479. return (($elseif) ? '} else if (' : 'if (') . (implode(' ', $tokens) . ') { ');
  480. }
  481. /**
  482. * Compile DEFINE tags
  483. * @access private
  484. */
  485. function compile_tag_define($tag_args, $op)
  486. {
  487. preg_match('#^((?:[a-z0-9\-_]+\.)+)?\$(?=[A-Z])([A-Z0-9_\-]*)(?: = (\'?)([^\']*)(\'?))?$#', $tag_args, $match);
  488. if (empty($match[2]) || (! isset($match[4]) && $op))
  489. {
  490. return '';
  491. }
  492. if (! $op)
  493. {
  494. return 'unset(' . (($match[1]) ? $this->generate_block_data_ref(substr($match[1], 0, - 1), true, true) . '[\'' . $match[2] . '\']' : '$this->_tpldata[\'DEFINE\'][\'.\'][\'' . $match[2] . '\']') . ');';
  495. }
  496. // Are we a string?
  497. if ($match[3] && $match[5])
  498. {
  499. $match[4] = str_replace(array('\\\'', '\\\\', '\''), array('\'', '\\', '\\\''), $match[4]);
  500. // Compile reference, we allow template variables in defines...
  501. $match[4] = $this->compile($match[4]);
  502. // Now replace the php code
  503. $match[4] = "'" . str_replace(array('<?php echo ', '; ?>'), array("' . ", " . '"), $match[4]) . "'";
  504. }
  505. else
  506. {
  507. preg_match('#true|false|\.#i', $match[4], $type);
  508. switch (strtolower($type[0]))
  509. {
  510. case 'true' :
  511. case 'false' :
  512. $match[4] = strtoupper($match[4]);
  513. break;
  514. case '.' :
  515. $match[4] = doubleval($match[4]);
  516. break;
  517. default :
  518. $match[4] = intval($match[4]);
  519. break;
  520. }
  521. }
  522. return (($match[1]) ? $this->generate_block_data_ref(substr($match[1], 0, - 1), true, true) . '[\'' . $match[2] . '\']' : '$this->_tpldata[\'DEFINE\'][\'.\'][\'' . $match[2] . '\']') . ' = ' . $match[4] . ';';
  523. }
  524. /**
  525. * Compile INCLUDE tag
  526. * @access private
  527. */
  528. function compile_tag_include($tag_args)
  529. {
  530. // Process dynamic includes
  531. if ($tag_args[0] == '$')
  532. {
  533. return "if (isset($tag_args)) { \$this->_tpl_include($tag_args); }";
  534. }
  535. return "\$this->_tpl_include('$tag_args');";
  536. }
  537. /**
  538. * Compile INCLUDE_PHP tag
  539. * @access private
  540. */
  541. function compile_tag_include_php($tag_args)
  542. {
  543. return "\$this->_php_include('$tag_args');";
  544. }
  545. /**
  546. * parse expression
  547. * This is from Smarty
  548. * @access private
  549. */
  550. function _parse_is_expr($is_arg, $tokens)
  551. {
  552. $expr_end = 0;
  553. $negate_expr = false;
  554. if (($first_token = array_shift($tokens)) == 'not')
  555. {
  556. $negate_expr = true;
  557. $expr_type = array_shift($tokens);
  558. }
  559. else
  560. {
  561. $expr_type = $first_token;
  562. }
  563. switch ($expr_type)
  564. {
  565. case 'even' :
  566. if (@$tokens[$expr_end] == 'by')
  567. {
  568. $expr_end ++;
  569. $expr_arg = $tokens[$expr_end ++];
  570. $expr = "!(($is_arg / $expr_arg) % $expr_arg)";
  571. }
  572. else
  573. {
  574. $expr = "!($is_arg & 1)";
  575. }
  576. break;
  577. case 'odd' :
  578. if (@$tokens[$expr_end] == 'by')
  579. {
  580. $expr_end ++;
  581. $expr_arg = $tokens[$expr_end ++];
  582. $expr = "(($is_arg / $expr_arg) % $expr_arg)";
  583. }
  584. else
  585. {
  586. $expr = "($is_arg & 1)";
  587. }
  588. break;
  589. case 'div' :
  590. if (@$tokens[$expr_end] == 'by')
  591. {
  592. $expr_end ++;
  593. $expr_arg = $tokens[$expr_end ++];
  594. $expr = "!($is_arg % $expr_arg)";
  595. }
  596. break;
  597. }
  598. if ($negate_expr)
  599. {
  600. $expr = "!($expr)";
  601. }
  602. array_splice($tokens, 0, $expr_end, $expr);
  603. return $tokens;
  604. }
  605. /**
  606. * Generates a reference to the given variable inside the given (possibly nested)
  607. * block namespace. This is a string of the form:
  608. * ' . $this->_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['varname'] . '
  609. * It's ready to be inserted into an "echo" line in one of the templates.
  610. * NOTE: expects a trailing "." on the namespace.
  611. * @access private
  612. */
  613. function generate_block_varref($namespace, $varname, $echo = true, $defop = false)
  614. {
  615. // Strip the trailing period.
  616. $namespace = substr($namespace, 0, - 1);
  617. // Get a reference to the data block for this namespace.
  618. $varref = $this->generate_block_data_ref($namespace, true, $defop);
  619. // Prepend the necessary code to stick this in an echo line.
  620. // Append the variable reference.
  621. $varref .= "['$varname']";
  622. $varref = ($echo) ? "<?php echo $varref; ?>" : ((isset($varref)) ? $varref : '');
  623. return $varref;
  624. }
  625. /**
  626. * Generates a reference to the array of data values for the given
  627. * (possibly nested) block namespace. This is a string of the form:
  628. * $this->_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['$childN']
  629. *
  630. * If $include_last_iterator is true, then [$_childN_i] will be appended to the form shown above.
  631. * NOTE: does not expect a trailing "." on the blockname.
  632. * @access private
  633. */
  634. function generate_block_data_ref($blockname, $include_last_iterator, $defop = false)
  635. {
  636. // Get an array of the blocks involved.
  637. $blocks = explode('.', $blockname);
  638. $blockcount = sizeof($blocks) - 1;
  639. // DEFINE is not an element of any referenced variable, we must use _tpldata to access it
  640. if ($defop)
  641. {
  642. $varref = '$this->_tpldata[\'DEFINE\']';
  643. // Build up the string with everything but the last child.
  644. for($i = 0; $i < $blockcount; $i ++)
  645. {
  646. $varref .= "['" . $blocks[$i] . "'][\$_" . $blocks[$i] . '_i]';
  647. }
  648. // Add the block reference for the last child.
  649. $varref .= "['" . $blocks[$blockcount] . "']";
  650. // Add the iterator for the last child if requried.
  651. if ($include_last_iterator)
  652. {
  653. $varref .= '[$_' . $blocks[$blockcount] . '_i]';
  654. }
  655. return $varref;
  656. }
  657. else
  658. if ($include_last_iterator)
  659. {
  660. return '$_' . $blocks[$blockcount] . '_val';
  661. }
  662. else
  663. {
  664. return '$_' . $blocks[$blockcount - 1] . '_val[\'' . $blocks[$blockcount] . '\']';
  665. }
  666. }
  667. /**
  668. * Write compiled file to cache directory
  669. * @access private
  670. */
  671. function compile_write($handle, $data)
  672. {
  673. global $phpEx;
  674. $filename = $this->template->cachepath . str_replace('/', '.', $this->template->filename[$handle]) . '.' . $phpEx;
  675. $data = "<?php if (!defined('IN_PHPBB')) exit;" . ((strpos($data, '<?php') === 0) ? substr($data, 5) : ' ?>' . $data);
  676. if ($fp = @fopen($filename, 'wb'))
  677. {
  678. @flock($fp, LOCK_EX);
  679. @fwrite($fp, $data);
  680. @flock($fp, LOCK_UN);
  681. @fclose($fp);
  682. phpbb_chmod($filename, CHMOD_READ | CHMOD_WRITE);
  683. }
  684. return;
  685. }
  686. }
  687. ?>