/WeBid/trunk/includes/functions_template.php

https://github.com/cipiceuca25/webid · PHP · 758 lines · 478 code · 113 blank · 167 comment · 59 complexity · 58cbb3e0b93da07f75cd5fbfe3f41670 MD5 · raw file

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