PageRenderTime 35ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/phpBB/includes/functions_template.php

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