📄 functions_template.php
字号:
<?php/** ** @package phpBB3* @version $Id: functions_template.php,v 1.14 2006/04/29 17:19:24 acydburn Exp $* @copyright (c) 2005 phpBB Group, sections (c) 2001 ispi of Lincoln Inc* @license http://opensource.org/licenses/gpl-license.php GNU Public License **//*** @package phpBB3** Extension of template class - Functions needed for compiling templates only.** psoTFX - Completion of file caching, decompilation routines and implementation of* conditionals/keywords and associated changes** phpBB Development Team - further additions and fixes** The interface was inspired by PHPLib templates, and the template file (formats are* quite similar)** The keyword/conditional implementation is currently based on sections of code from* the Smarty templating engine (c) 2001 ispi of Lincoln, Inc. which is released* (on its own and in whole) under the LGPL. Section 3 of the LGPL states that any code* derived from an LGPL application may be relicenced under the GPL, this applies* to this source* * DEFINE directive inspired by a request by Cyberalien*/class template_compile{ var $template; // Various storage arrays var $block_names = array(); var $block_else_level = array(); /** * constuctor */ function template_compile(&$template) { $this->template = &$template; } /** * Load template source from file * @private */ function _tpl_load_file($handle) { // Try and open template for read if (!file_exists($this->template->files[$handle])) { trigger_error("template->_tpl_load_file(): File {$this->template->files[$handle]} does not exist or is empty", E_USER_ERROR); } $this->template->compiled_code[$handle] = $this->compile(trim(@file_get_contents($this->template->files[$handle]))); // Actually compile the code now. if($this->template->cache) $this->compile_write($handle, $this->template->compiled_code[$handle]); } /** * The all seeing all doing compile method. Parts are inspired by or directly from Smarty * @private */ function compile($code, $no_echo = false, $echo_var = '') { global $config; if ($echo_var) { global $$echo_var; } // Remove any "loose" php ... we want to give admins the ability // to switch on/off PHP for a given template. Allowing unchecked // php is a no-no. There is a potential issue here in that non-php // content may be removed ... however designers should use entities // if they wish to display < and > $match_php_tags = array('#\<\?php .*?\?\>#is', '#\<\script language="php"\>.*?\<\/script\>#is', '#\<\?.*?\?\>#s', '#\<%.*?%\>#s'); $code = preg_replace($match_php_tags, '', $code); // Pull out all block/statement level elements and seperate plain text preg_match_all('#<!-- PHP -->(.*?)<!-- ENDPHP -->#s', $code, $matches); $php_blocks = $matches[1]; $code = preg_replace('#<!-- PHP -->(.*?)<!-- ENDPHP -->#s', '<!-- PHP -->', $code); preg_match_all('#<!-- INCLUDE ([a-zA-Z0-9\_\-\+\.]+?) -->#', $code, $matches); $include_blocks = $matches[1]; $code = preg_replace('#<!-- INCLUDE ([a-zA-Z0-9\_\-\+\.]+?) -->#', '<!-- INCLUDE -->', $code); preg_match_all('#<!-- INCLUDEPHP ([a-zA-Z0-9\_\-\+\.\\\\]+?) -->#', $code, $matches); $includephp_blocks = $matches[1]; $code = preg_replace('#<!-- INCLUDEPHP ([a-zA-Z0-9\_\-\+\.]+?) -->#', '<!-- INCLUDEPHP -->', $code); preg_match_all('#<!-- (.*?) (.*?)?[ ]?-->#', $code, $blocks); $text_blocks = preg_split('#<!-- (.*?) (.*?)?[ ]?-->#', $code); for ($i = 0, $j = sizeof($text_blocks); $i < $j; $i++) { $this->compile_var_tags($text_blocks[$i]); } $compile_blocks = array(); for ($curr_tb = 0, $tb_size = sizeof($text_blocks); $curr_tb < $tb_size; $curr_tb++) { if (!isset($blocks[1][$curr_tb])) { $blocks[1][$curr_tb] = ''; } switch ($blocks[1][$curr_tb]) { case 'BEGIN': $this->block_else_level[] = false; $compile_blocks[] = '<?php ' . $this->compile_tag_block($blocks[2][$curr_tb]) . ' ?>'; break; case 'BEGINELSE': $this->block_else_level[sizeof($this->block_else_level) - 1] = true; $compile_blocks[] = '<?php }} else { ?>'; break; case 'END': array_pop($this->block_names); $compile_blocks[] = '<?php ' . ((array_pop($this->block_else_level)) ? '}' : '}}') . ' ?>'; break; case 'IF': $compile_blocks[] = '<?php ' . $this->compile_tag_if($blocks[2][$curr_tb], false) . ' ?>'; break; case 'ELSE': $compile_blocks[] = '<?php } else { ?>'; break; case 'ELSEIF': $compile_blocks[] = '<?php ' . $this->compile_tag_if($blocks[2][$curr_tb], true) . ' ?>'; break; case 'ENDIF': $compile_blocks[] = '<?php } ?>'; break; case 'DEFINE': $compile_blocks[] = '<?php ' . $this->compile_tag_define($blocks[2][$curr_tb], true) . ' ?>'; break; case 'UNDEFINE': $compile_blocks[] = '<?php ' . $this->compile_tag_define($blocks[2][$curr_tb], false) . ' ?>'; break; case 'INCLUDE': $temp = array_shift($include_blocks); $compile_blocks[] = '<?php ' . $this->compile_tag_include($temp) . ' ?>'; $this->template->_tpl_include($temp, false); break; case 'INCLUDEPHP': $compile_blocks[] = $this->template->allow_php ? '<?php ' . $this->compile_tag_include_php(array_shift($includephp_blocks)) . ' ?>' : ''; break; case 'PHP': $compile_blocks[] = $this->template->allow_php ? '<?php ' . array_shift($php_blocks) . ' ?>' : ''; break; default: $this->compile_var_tags($blocks[0][$curr_tb]); $trim_check = trim($blocks[0][$curr_tb]); $compile_blocks[] = (!$no_echo) ? ((!empty($trim_check)) ? $blocks[0][$curr_tb] : '') : ((!empty($trim_check)) ? $blocks[0][$curr_tb] : ''); break; } } $template_php = ''; for ($i = 0, $size = sizeof($text_blocks); $i < $size; $i++) { $trim_check_text = trim($text_blocks[$i]); $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] : ''); } // There will be a number of occassions where we switch into and out of // PHP mode instantaneously. Rather than "burden" the parser with this // we'll strip out such occurences, minimising such switching $template_php = str_replace(' ?><?php ', '', $template_php); return (!$no_echo) ? $template_php : "\$$echo_var .= '" . $template_php . "'"; } /** * Compile variables * @private */ function compile_var_tags(&$text_blocks) { // change template varrefs into PHP varrefs $varrefs = array(); // This one will handle varrefs WITH namespaces preg_match_all('#\{((?:[a-zA-Z0-9\-_]+\.)+)(\$)?([a-zA-Z0-9\-_]+)\}#', $text_blocks, $varrefs); for ($j = 0, $size = sizeof($varrefs[1]); $j < $size; $j++) { $namespace = $varrefs[1][$j]; $varname = $varrefs[3][$j]; $new = $this->generate_block_varref($namespace, $varname, true, $varrefs[2][$j]); $text_blocks = str_replace($varrefs[0][$j], $new, $text_blocks); } // This will handle the remaining root-level varrefs // transform vars prefixed by L_ into their language variable pendant if nothing is set within the tpldata array $text_blocks = preg_replace('#\{L_([a-z0-9\-_]*)\}#is', "<?php echo ((isset(\$this->_tpldata['.'][0]['L_\\1'])) ? \$this->_tpldata['.'][0]['L_\\1'] : ((isset(\$user->lang['\\1'])) ? \$user->lang['\\1'] : '{ ' . ucfirst(strtolower(str_replace('_', ' ', '\\1'))) . ' }')); ?>", $text_blocks); // Handle addslashed language variables prefixed with LA_ // If a template variable already exist, it will be used in favor of it... $text_blocks = preg_replace('#\{LA_([a-z0-9\-_]*)\}#is', "<?php echo ((isset(\$this->_tpldata['.'][0]['LA_\\1'])) ? \$this->_tpldata['.'][0]['LA_\\1'] : ((isset(\$this->_tpldata['.'][0]['L_\\1'])) ? addslashes(\$this->_tpldata['.'][0]['L_\\1']) : ((isset(\$user->lang['\\1'])) ? addslashes(\$user->lang['\\1']) : '{ ' . ucfirst(strtolower(str_replace('_', ' ', '\\1'))) . ' }'))); ?>", $text_blocks); // Handle remaining varrefs $text_blocks = preg_replace('#\{([a-z0-9\-_]*)\}#is', "<?php echo (isset(\$this->_tpldata['.'][0]['\\1'])) ? \$this->_tpldata['.'][0]['\\1'] : ''; ?>", $text_blocks); $text_blocks = preg_replace('#\{\$([a-z0-9\-_]*)\}#is', "<?php echo (isset(\$this->_tpldata['DEFINE']['.']['\\1'])) ? \$this->_tpldata['DEFINE']['.']['\\1'] : ''; ?>", $text_blocks); return; } /** * Compile blocks * @private */ function compile_tag_block($tag_args) { $no_nesting = false; // Is the designer wanting to call another loop in a loop? if (strpos($tag_args, '!') === 0) { // Count the number if ! occurrences (not allowed in vars) $no_nesting = substr_count($tag_args, '!'); $tag_args = substr($tag_args, $no_nesting); } // Allow for control of looping (indexes start from zero): // foo(2) : Will start the loop on the 3rd entry // foo(-2) : Will start the loop two entries from the end // foo(3,4) : Will start the loop on the fourth entry and end it on the fifth // foo(3,-4) : Will start the loop on the fourth entry and end it four from last if (preg_match('#^([^()]*)\(([\-\d]+)(?:,([\-\d]+))?\)$#', $tag_args, $match)) { $tag_args = $match[1]; if ($match[2] < 0) { $loop_start = '($_' . $tag_args . '_count ' . $match[2] . ' < 0 ? 0 : $_' . $tag_args . '_count ' . $match[2] . ')'; } else { $loop_start = '($_' . $tag_args . '_count < ' . $match[2] . ' ? $_' . $tag_args . '_count : ' . $match[2] . ')'; } if (strlen($match[3]) < 1 || $match[3] == -1) { $loop_end = '$_' . $tag_args . '_count'; } else if ($match[3] >= 0) { $loop_end = '(' . ($match[3] + 1) . ' > $_' . $tag_args . '_count ? $_' . $tag_args . '_count : ' . ($match[3] + 1) . ')'; } else //if ($match[3] < -1) { $loop_end = '$_' . $tag_args . '_count' . ($match[3] + 1); } } else { $loop_start = 0; $loop_end = '$_' . $tag_args . '_count'; } $tag_template_php = ''; array_push($this->block_names, $tag_args); if (sizeof($this->block_names) < 2) { // Block is not nested. $tag_template_php = '$_' . $tag_args . "_count = (isset(\$this->_tpldata['$tag_args'])) ? sizeof(\$this->_tpldata['$tag_args']) : 0;"; } else { // This block is nested. // Generate a namespace string for this block. if ($no_nesting !== false) { // We need to implode $no_nesting times from the end... $namespace = implode('.', array_slice($this->block_names, -$no_nesting)); } else { $namespace = implode('.', $this->block_names); } // Get a reference to the data array for this block that depends on the // current indices of all parent blocks. $varref = $this->generate_block_data_ref($namespace, false); // Create the for loop code to iterate over this block. $tag_template_php = '$_' . $tag_args . '_count = (isset(' . $varref . ')) ? sizeof(' . $varref . ') : 0;'; } $tag_template_php .= 'if ($_' . $tag_args . '_count) {'; $tag_template_php .= 'for ($this->_' . $tag_args . '_i = ' . $loop_start . '; $this->_' . $tag_args . '_i < ' . $loop_end . '; $this->_' . $tag_args . '_i++){'; return $tag_template_php; } /** * Compile IF tags - much of this is from Smarty with * some adaptions for our block level methods * @private */ function compile_tag_if($tag_args, $elseif) { // Tokenize args for 'if' tag. preg_match_all('/(?: "[^"\\\\]*(?:\\\\.[^"\\\\]*)*" | \'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\' | [(),] | [^\s(),]+)/x', $tag_args, $match); $tokens = $match[0]; $is_arg_stack = array(); //echo ($tag_args);print_r($tokens);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -