PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

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