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

/lib/template_enc.php

http://wowroster.googlecode.com/
PHP | 519 lines | 397 code | 46 blank | 76 comment | 33 complexity | d13a2bca980c45743d45b13ae7d9158d MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * WoWRoster..net WoWRoster
  4. *
  5. * Template Encoder
  6. * Modified from the EQDkp Project
  7. *
  8. *
  9. * @copyright 2002-2011 WoWRoster.net
  10. * @license http://www.gnu.org/licenses/gpl.html Licensed under the GNU General Public License v3.
  11. * @version SVN: $Id: template_enc.php 2222 2010-12-05 10:05:37Z c.treyce@gmail.com $
  12. * @link http://www.wowroster.net
  13. * @since File available since Release 1.8.0
  14. * @package WoWRoster
  15. * @subpackage Template
  16. */
  17. if ( !defined('IN_ROSTER') )
  18. {
  19. exit('Detected invalid access to this file!');
  20. }
  21. /**
  22. * Template Encoder
  23. *
  24. * @package WoWRoster
  25. * @subpackage Template
  26. */
  27. class RosterTplEncode
  28. {
  29. // The all seeing all doing compile method. Parts are inspired by or directly
  30. // from Smarty
  31. function compile( $code , $no_echo = false , $echo_var = '' )
  32. {
  33. // Remove any "loose" php ... we want to give admins the ability
  34. // to switch on/off PHP for a given template. Allowing unchecked
  35. // php is a no-no. There is a potential issue here in that non-php
  36. // content may be removed ... however designers should use entities
  37. // if they wish to display < and >
  38. $match_php_tags = array('#\<\?php .*?\?\>#is', '#\<\script language="php"\>.*?\<\/script\>#is', '#\<\?.*?\?\>#s', '#\<%.*?%\>#s');
  39. $code = preg_replace($match_php_tags, '', $code);
  40. // Pull out all block/statement level elements and seperate plain text
  41. preg_match_all('#<!-- PHP -->(.*?)<!-- ENDPHP -->#s', $code, $matches);
  42. $php_blocks = $matches[1];
  43. $code = preg_replace('#<!-- PHP -->(.*?)<!-- ENDPHP -->#s', '<!-- PHP -->', $code);
  44. preg_match_all('#<!-- INCLUDE ([a-zA-Z0-9\_\-\+\.\/]+?) -->#', $code, $matches);
  45. $include_blocks = $matches[1];
  46. $code = preg_replace('#<!-- INCLUDE ([a-zA-Z0-9\_\-\+\.\/]+?) -->#', '<!-- INCLUDE -->', $code);
  47. preg_match_all('#<!-- INCLUDEPHP ([a-zA-Z0-9\_\-\+\.\\\\\/]+?) -->#', $code, $matches);
  48. $includephp_blocks = $matches[1];
  49. $code = preg_replace('#<!-- INCLUDEPHP ([a-zA-Z0-9\_\-\+\.\/]+?) -->#', '<!-- INCLUDEPHP -->', $code);
  50. preg_match_all('#<!-- (.*?) (.*?)?[ ]?-->#s', $code, $blocks);
  51. $text_blocks = preg_split('#<!-- (.*?) (.*?)?[ ]?-->#s', $code);
  52. if( $text_blocks[count($text_blocks)-1] == '' )
  53. {
  54. unset($text_blocks[count($text_blocks)-1]);
  55. }
  56. for( $i = 0; $i < count($text_blocks); $i++ )
  57. {
  58. RosterTplEncode::compile_var_tags($text_blocks[$i]);
  59. }
  60. $compile_blocks = array();
  61. $block_else_level = array();
  62. $this->block_names = array();
  63. for( $curr_tb = 0; $curr_tb < count($text_blocks); $curr_tb++ )
  64. {
  65. if( isset($blocks[1][$curr_tb]) )
  66. {
  67. switch ($blocks[1][$curr_tb])
  68. {
  69. case 'BEGIN':
  70. $block_else_level[] = false;
  71. $compile_blocks[] = '<?php ' . RosterTplEncode::compile_tag_block($blocks[2][$curr_tb]) . ' ?>';
  72. break;
  73. case 'BEGINELSE':
  74. $block_else_level[count($block_else_level) - 1] = true;
  75. $compile_blocks[] = '<?php }} else { ?>';
  76. break;
  77. case 'END':
  78. array_pop($this->block_names);
  79. $compile_blocks[] = '<?php ' . ((array_pop($block_else_level)) ? '}' : '}}') . ' ?>';
  80. break;
  81. case 'IF':
  82. $compile_blocks[] = '<?php ' . RosterTplEncode::compile_tag_if($blocks[2][$curr_tb], false) . ' ?>';
  83. break;
  84. case 'ELSE':
  85. $compile_blocks[] = '<?php } else { ?>';
  86. break;
  87. case 'ELSEIF':
  88. $compile_blocks[] = '<?php ' . RosterTplEncode::compile_tag_if($blocks[2][$curr_tb], true) . ' ?>';
  89. break;
  90. case 'ENDIF':
  91. $compile_blocks[] = '<?php } ?>';
  92. break;
  93. case 'DEFINE':
  94. $compile_blocks[] = '<?php ' . RosterTplEncode::compile_tag_define($blocks[2][$curr_tb], true) . ' ?>';
  95. break;
  96. case 'UNDEFINE':
  97. $compile_blocks[] = '<?php ' . RosterTplEncode::compile_tag_define($blocks[2][$curr_tb], false) . ' ?>';
  98. break;
  99. case 'INCLUDE':
  100. $temp = array_shift($include_blocks);
  101. $compile_blocks[] = '<?php $this->_tpl_include(\'' . $temp . "'); ?>";
  102. $this->_tpl_include($temp, false);
  103. break;
  104. case 'INCLUDEPHP':
  105. /* $compile_blocks[] = "<?php include('" . $this->root . '/' . array_shift($includephp_blocks) . "'); ?>"; */
  106. break;
  107. case 'PHP':
  108. $compile_blocks[] = '<?php ' . array_shift($php_blocks) . ' ?>';
  109. break;
  110. case 'TRANSLATE':
  111. $params = explode(' ', $blocks[2][$curr_tb]);
  112. $key = $params[0];
  113. $compile_blocks[] = '<?php if( isset($roster->locale->act[\'' . $key . '\']) ) { echo $roster->locale->act[\'' . $key . '\']; ' . '} else { echo \'{ TRANSLATE ' . $key . ' }\'; trigger_error(\'Missing translation { ' . $key . ' }\', E_USER_NOTICE); } ?>';
  114. break;
  115. case 'TRANSLATE_F':
  116. $params = explode(' ', $blocks[2][$curr_tb]);
  117. $key = array_shift($params);
  118. $args = '';
  119. $extra = '';
  120. foreach( $params as $param )
  121. {
  122. if( preg_match('#^(([a-z0-9\-_]+?\.)+?)(\$)?([A-Z0-9\-_]+?)$#', $param, $varref) )
  123. {
  124. $namespace = $varref[1];
  125. $varname = $varref[4];
  126. $isdefine = $varref[3];
  127. $args .= ',' . RosterTplEncode::generate_block_varref($namespace, $varname, false, $isdefine);
  128. }
  129. else
  130. {
  131. $args .= ',( isset($roster->locale->act[\'' . $param . '\']) ? $roster->locale->act[\'' . $param . '\'] : ( isset($this->_tpldata[\'.\'][0][\'' . $param . '\']) ? $this->_tpldata[\'.\'][0][\'' . $param . '\'] : \'{ TRANSLATE ' . $param . ' }\' ))';
  132. }
  133. }
  134. $compile_blocks[] = '<?php if( isset($roster->locale->act[\'' . $key . '\']) ) { echo sprintf( $roster->locale->act[\'' . $key . '\']' . $args . '); ' . '} else { echo \'{ TRANSLATE_F ' . $key . ' }\'; trigger_error(\'Missing translation { ' . $key . ' }\', E_USER_NOTICE); } ?>';
  135. break;
  136. default:
  137. RosterTplEncode::compile_var_tags($blocks[0][$curr_tb]);
  138. $trim_check = trim($blocks[0][$curr_tb]);
  139. $compile_blocks[] = (!$no_echo) ? ((!empty($trim_check)) ? $blocks[0][$curr_tb] : '') : ((!empty($trim_check)) ? $blocks[0][$curr_tb] : '');
  140. break;
  141. }
  142. }
  143. }
  144. $template_php = '<?php global $roster; ?>';
  145. for( $i = 0; $i < count($text_blocks); $i++ )
  146. {
  147. $trim_check_text = trim($text_blocks[$i]);
  148. $trim_check_block = isset($compile_blocks[$i]) ? trim($compile_blocks[$i]) : '';
  149. $template_php .= (!$no_echo) ? ((!empty($trim_check_text)) ? $text_blocks[$i] : '') . ((!empty($compile_blocks[$i])) ? $compile_blocks[$i] : '') : ((!empty($trim_check_text)) ? $text_blocks[$i] : '') . ((!empty($compile_blocks[$i])) ? $compile_blocks[$i] : '');
  150. }
  151. // There will be a number of occassions where we switch into and out of
  152. // PHP mode instantaneously. Rather than "burden" the parser with this
  153. // we'll strip out such occurences, minimising such switching
  154. $template_php = str_replace(' ?><?php ', '', $template_php);
  155. return (!$no_echo) ? $template_php : "\$$echo_var .= '" . $template_php . "'";
  156. }
  157. function compile_var_tags( &$text_blocks )
  158. {
  159. // change template varrefs into PHP varrefs
  160. $varrefs = array();
  161. // This one will handle varrefs WITH namespaces
  162. preg_match_all('#\{(([a-z0-9\-_]+?\.)+?)(\$)?([A-Z0-9\-_]+?)\}#', $text_blocks, $varrefs);
  163. for( $j = 0; $j < count($varrefs[1]); $j++ )
  164. {
  165. $namespace = $varrefs[1][$j];
  166. $varname = $varrefs[4][$j];
  167. $new = RosterTplEncode::generate_block_varref($namespace, $varname, true, $varrefs[3][$j]);
  168. $text_blocks = str_replace($varrefs[0][$j], $new, $text_blocks);
  169. }
  170. // This will handle the remaining root-level varrefs
  171. $text_blocks = preg_replace('#\{L_([a-z0-9\-_]*?)\}#is', "<?php echo ((isset(\$this->_tpldata['.'][0]['L_\\1'])) ? \$this->_tpldata['.'][0]['L_\\1'] : '{ '.ucfirst(strtolower(str_replace('_', ' ', '\\1'))).' }'); ?>", $text_blocks);
  172. $text_blocks = preg_replace('#\{([a-z0-9\-_]*?)\}#is', "<?php echo \$this->_tpldata['.'][0]['\\1']; ?>", $text_blocks);
  173. $text_blocks = preg_replace('#\{\$([a-z0-9\-_]*?)\}#is', "<?php echo \$this->_tpldata['DEFINE']['.']['\\1']; ?>", $text_blocks);
  174. return;
  175. }
  176. function compile_tag_block( $tag_args )
  177. {
  178. // Allow for control of looping (indexes start from zero):
  179. // foo(2) : Will start the loop on the 3rd entry
  180. // foo(-2) : Will start the loop two entries from the end
  181. // foo(3,4) : Will start the loop on the fourth entry and end it on the fourth
  182. // foo(3,-4) : Will start the loop on the fourth entry and end it four from last
  183. if( preg_match('#^(.*?)\(([\-0-9]+)(,([\-0-9]+))?\)$#', $tag_args, $match) )
  184. {
  185. $tag_args = $match[1];
  186. $loop_start = ($match[2] < 0) ? '$_' . $tag_args . '_count ' . ($match[2] - 1) : $match[2];
  187. $loop_end = isset($match[4]) ? (($match[4] < 0) ? '$_' . $tag_args . '_count ' . $match[4] : ($match[4] + 1)) : '$_' . $tag_args . '_count';
  188. }
  189. else
  190. {
  191. $loop_start = 0;
  192. $loop_end = '$_' . $tag_args . '_count';
  193. }
  194. $tag_template_php = '';
  195. array_push($this->block_names, $tag_args);
  196. if( count($this->block_names) < 2 )
  197. {
  198. // Block is not nested.
  199. $tag_template_php = '$_' . $tag_args . "_count = (isset(\$this->_tpldata['$tag_args'])) ? count(\$this->_tpldata['$tag_args']) : 0;";
  200. }
  201. else
  202. {
  203. // This block is nested.
  204. // Generate a namespace string for this block.
  205. $namespace = implode('.', $this->block_names);
  206. // Get a reference to the data array for this block that depends on the
  207. // current indices of all parent blocks.
  208. $varref = RosterTplEncode::generate_block_data_ref($namespace, false, false);
  209. // Create the for loop code to iterate over this block.
  210. $tag_template_php = '$_' . $tag_args . '_count = (isset(' . $varref . ')) ? count(' . $varref . ') : 0;';
  211. }
  212. $tag_template_php .= 'if ($_' . $tag_args . '_count) {';
  213. $tag_template_php .= 'for ($this->_' . $tag_args . '_i = ' . $loop_start . '; $this->_' . $tag_args . '_i < ' . $loop_end . '; $this->_' . $tag_args . '_i++){';
  214. return $tag_template_php;
  215. }
  216. //
  217. // Compile IF tags - much of this is from Smarty with
  218. // some adaptions for our block level methods
  219. //
  220. function compile_tag_if( $tag_args , $elseif )
  221. {
  222. /* Tokenize args for 'if' tag. */
  223. preg_match_all('/(?:
  224. "[^"\\\\]*(?:\\\\.[^"\\\\]*)*" |
  225. \'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\' |
  226. [(),] |
  227. [^\s(),]+)/x', $tag_args, $match);
  228. $tokens = $match[0];
  229. $is_arg_stack = array();
  230. for( $i = 0; $i < count($tokens); $i++ )
  231. {
  232. $token = &$tokens[$i];
  233. switch( $token )
  234. {
  235. case '!':
  236. case '%':
  237. case '!==':
  238. case '==':
  239. case '===':
  240. case '>':
  241. case '<':
  242. case '!=':
  243. case '<>':
  244. case '<<':
  245. case '>>':
  246. case '<=':
  247. case '>=':
  248. case '&&':
  249. case '||':
  250. case '|':
  251. case '^':
  252. case '&':
  253. case '~':
  254. case ')':
  255. case ',':
  256. case '+':
  257. case '-':
  258. case '*':
  259. case '/':
  260. case '@':
  261. break;
  262. case 'eq':
  263. $token = '==';
  264. break;
  265. case 'ne':
  266. case 'neq':
  267. $token = '!=';
  268. break;
  269. case 'lt':
  270. $token = '<';
  271. break;
  272. case 'le':
  273. case 'lte':
  274. $token = '<=';
  275. break;
  276. case 'gt':
  277. $token = '>';
  278. break;
  279. case 'ge':
  280. case 'gte':
  281. $token = '>=';
  282. break;
  283. case 'and':
  284. $token = '&&';
  285. break;
  286. case 'or':
  287. $token = '||';
  288. break;
  289. case 'not':
  290. $token = '!';
  291. break;
  292. case 'mod':
  293. $token = '%';
  294. break;
  295. case '(':
  296. array_push($is_arg_stack, $i);
  297. break;
  298. case 'is':
  299. $is_arg_start = ($tokens[$i-1] == ')') ? array_pop($is_arg_stack) : $i-1;
  300. $is_arg = implode(' ', array_slice($tokens, $is_arg_start, $i - $is_arg_start));
  301. $new_tokens = RosterTplEncode::_parse_is_expr($is_arg, array_slice($tokens, $i+1));
  302. array_splice($tokens, $is_arg_start, count($tokens), $new_tokens);
  303. $i = $is_arg_start;
  304. default:
  305. if( preg_match('#^(([a-z0-9\-_]+?\.)+?)?(\$)?([A-Z]+[A-Z0-9\-_]+)$#s', $token, $varrefs) )
  306. {
  307. $token = (!empty($varrefs[1])) ? RosterTplEncode::generate_block_data_ref(substr($varrefs[1], 0, -1), true, $varrefs[3]) . '[\'' . $varrefs[4] . '\']' : (($varrefs[3]) ? '$this->_tpldata[\'DEFINE\'][\'.\'][\'' . $varrefs[4] . '\']' : '$this->_tpldata[\'.\'][0][\'' . $varrefs[4] . '\']');
  308. }
  309. break;
  310. }
  311. }
  312. return (($elseif) ? '} elseif (' : 'if (') . (implode(' ', $tokens) . ') { ');
  313. }
  314. function compile_tag_define( $tag_args , $option )
  315. {
  316. preg_match('#^(([a-z0-9\-_]+?\.)+?)?\$([A-Z][A-Z0-9_\-]*?) = (\'?)(.*?)(\'?)$#', $tag_args, $match);
  317. if( empty($match[3]) || empty($match[5]) )
  318. {
  319. trigger_error("'$tag_args' is no proper &lt;!-- DEFINE --&gt; format.", E_USER_WARNING);
  320. return;
  321. }
  322. // Are we a string?
  323. if( $match[4] && $match[6] )
  324. {
  325. // Can't use addslashes as we don't know what value has magic_quotes_sybase.
  326. $match[5] = "'" . addcslashes(str_replace(array('\\\'', '\\\\'), array('\'', '\\'), $match[5]), "\0..\37\177\\\"'") . "'"; //"
  327. }
  328. else
  329. {
  330. preg_match('#(true|false|\.)#i', $match[5], $type);
  331. switch( strtolower($type[1]) )
  332. {
  333. case 'true':
  334. case 'false':
  335. $match[5] = strtoupper($match[5]);
  336. break;
  337. case '.':
  338. $match[5] = doubleval($match[5]);
  339. break;
  340. default:
  341. $match[5] = intval($match[5]);
  342. break;
  343. }
  344. }
  345. if( $option )
  346. {
  347. return (($match[1]) ? RosterTplEncode::generate_block_data_ref(substr($match[1], 0, -1), true, true) . '[\'' . $match[3] . '\']' : '$this->_tpldata[\'DEFINE\'][\'.\'][\'' . $match[3] . '\']') . ' = ' . $match[5] . ';';
  348. }
  349. else
  350. {
  351. return 'unset(' . (($match[1]) ? RosterTplEncode::generate_block_data_ref(substr($match[1], 0, -1), true, true) . '[\'' . $match[3] . '\']' : '$this->_tpldata[\'DEFINE\'][\'.\'][\'' . $match[3] . '\']') . ');';
  352. }
  353. }
  354. // This is from Smarty
  355. function _parse_is_expr( $is_arg , $tokens )
  356. {
  357. $expr_end = 0;
  358. $negate_expr = false;
  359. if( ($first_token = array_shift($tokens)) == 'not' )
  360. {
  361. $negate_expr = true;
  362. $expr_type = array_shift($tokens);
  363. }
  364. else
  365. {
  366. $expr_type = $first_token;
  367. }
  368. switch( $expr_type )
  369. {
  370. case 'even':
  371. if( $tokens[$expr_end] == 'by' )
  372. {
  373. ++$expr_end;
  374. $expr_arg = $tokens[$expr_end++];
  375. $expr = "!(($is_arg / $expr_arg) % $expr_arg)";
  376. }
  377. else
  378. {
  379. $expr = "!($is_arg % 2)";
  380. }
  381. break;
  382. case 'odd':
  383. if( $tokens[$expr_end] == 'by' )
  384. {
  385. ++$expr_end;
  386. $expr_arg = $tokens[$expr_end++];
  387. $expr = "(($is_arg / $expr_arg) % $expr_arg)";
  388. }
  389. else
  390. {
  391. $expr = "($is_arg % 2)";
  392. }
  393. break;
  394. case 'div':
  395. if( $tokens[$expr_end] == 'by' )
  396. {
  397. ++$expr_end;
  398. $expr_arg = $tokens[$expr_end++];
  399. $expr = "!($is_arg % $expr_arg)";
  400. }
  401. break;
  402. default:
  403. break;
  404. }
  405. if( $negate_expr )
  406. {
  407. $expr = "!($expr)";
  408. }
  409. array_splice($tokens, 0, $expr_end, $expr);
  410. return $tokens;
  411. }
  412. /**
  413. * Generates a reference to the given variable inside the given (possibly nested)
  414. * block namespace. This is a string of the form:
  415. * ' . $this->_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['varname'] . '
  416. * It's ready to be inserted into an "echo" line in one of the templates.
  417. * NOTE: expects a trailing "." on the namespace.
  418. */
  419. function generate_block_varref( $namespace , $varname , $echo = true , $defop = false )
  420. {
  421. # Strip the trailing period.
  422. $namespace = substr($namespace, 0, strlen($namespace) - 1);
  423. # Get a reference to the data block for this namespace.
  424. $varref = RosterTplEncode::generate_block_data_ref($namespace, true, $defop);
  425. # Append the variable reference.
  426. $varref .= "['$varname']";
  427. return (($echo) ? "<?php echo $varref; ?>" : $varref);
  428. }
  429. /**
  430. * Generates a reference to the array of data values for the given
  431. * (possibly nested) block namespace. This is a string of the form:
  432. * $this->_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['$childN']
  433. *
  434. * If $include_last_iterator is true, then [$_childN_i] will be appended to the form shown above.
  435. * NOTE: does not expect a trailing "." on the blockname.
  436. */
  437. function generate_block_data_ref( $blockname , $include_last_iterator , $defop )
  438. {
  439. # Get an array of the blocks involved.
  440. $blocks = explode('.', $blockname);
  441. $blockcount = count($blocks) - 1;
  442. $varref = '$this->_tpldata' . (($defop) ? '[\'DEFINE\']' : '');
  443. # Build up the string with everything but the last child.
  444. for( $i = 0; $i < $blockcount; $i++ )
  445. {
  446. $varref .= "['" . $blocks[$i] . "'][\$this->_" . $blocks[$i] . '_i]';
  447. }
  448. # Add the block reference for the last child.
  449. $varref .= "['" . $blocks[$blockcount] . "']";
  450. # Add the iterator for the last child if requried.
  451. if ($include_last_iterator)
  452. {
  453. $varref .= '[$this->_' . $blocks[$blockcount] . '_i]';
  454. }
  455. return $varref;
  456. }
  457. function compile_write( &$handle , &$data )
  458. {
  459. $filename = preg_replace('/\//', '#', $this->filename[$handle]);
  460. $filename = $this->cachepath . $filename . '.inc';
  461. if(is_writeable(dirname($filename)))
  462. {
  463. file_writer($filename, $data);
  464. }
  465. return;
  466. }
  467. }