diff options
Diffstat (limited to 'phpBB/includes/template')
-rw-r--r-- | phpBB/includes/template/compile.php | 39 | ||||
-rw-r--r-- | phpBB/includes/template/filter.php | 98 |
2 files changed, 107 insertions, 30 deletions
diff --git a/phpBB/includes/template/compile.php b/phpBB/includes/template/compile.php index fcdaf7abda..76cb3011df 100644 --- a/phpBB/includes/template/compile.php +++ b/phpBB/includes/template/compile.php @@ -33,6 +33,13 @@ class phpbb_template_compile private $filter_params; /** + * Array of default parameters + * + * @var array + */ + private $default_filter_params; + + /** * Constructor. * * @param bool $allow_php Whether PHP code will be allowed in templates (inline PHP code, PHP tag and INCLUDEPHP tag) @@ -44,18 +51,40 @@ class phpbb_template_compile */ public function __construct($allow_php, $style_names, $locator, $phpbb_root_path, $extension_manager = null, $user = null) { - $this->filter_params = array( - 'allow_php' => $allow_php, - 'style_names' => $style_names, - 'locator' => $locator, + $this->filter_params = $this->default_filter_params = array( + 'allow_php' => $allow_php, + 'style_names' => $style_names, + 'locator' => $locator, 'phpbb_root_path' => $phpbb_root_path, 'extension_manager' => $extension_manager, - 'user' => $user, + 'user' => $user, 'template_compile' => $this, + 'cleanup' => true, ); } /** + * Set filter parameters + * + * @param array $params Array of parameters (will be merged onto $this->filter_params) + */ + public function set_filter_params($params) + { + $this->filter_params = array_merge( + $this->filter_params, + $params + ); + } + + /** + * Reset filter parameters to their default settings + */ + public function reset_filter_params() + { + $this->filter_params = $this->default_filter_params; + } + + /** * Compiles template in $source_file and writes compiled template to * cache directory * diff --git a/phpBB/includes/template/filter.php b/phpBB/includes/template/filter.php index c2c100e93e..1c0a56c9f5 100644 --- a/phpBB/includes/template/filter.php +++ b/phpBB/includes/template/filter.php @@ -76,6 +76,14 @@ class phpbb_template_filter extends php_user_filter private $allow_php; /** + * Whether cleanup will be performed on resulting code, see compile() + * (Preserve whitespace) + * + * @var bool + */ + private $cleanup = true; + + /** * Resource locator. * * @var phpbb_template_locator @@ -183,6 +191,7 @@ class phpbb_template_filter extends php_user_filter $this->phpbb_root_path = $this->params['phpbb_root_path']; $this->style_names = $this->params['style_names']; $this->extension_manager = $this->params['extension_manager']; + $this->cleanup = $this->params['cleanup']; if (isset($this->params['user'])) { $this->user = $this->params['user']; @@ -223,41 +232,45 @@ class phpbb_template_filter extends php_user_filter $data = preg_replace('~<!-- ENDPHP -->.*?$~', '', $data); } - /* + if ($this->cleanup) + { + /* - Preserve whitespace. - PHP removes a newline after the closing tag (if it's there). - This is by design: + Preserve whitespace. + PHP removes a newline after the closing tag (if it's there). + This is by design: - http://www.php.net/manual/en/language.basic-syntax.phpmode.php - http://www.php.net/manual/en/language.basic-syntax.instruction-separation.php + http://www.php.net/manual/en/language.basic-syntax.phpmode.php + http://www.php.net/manual/en/language.basic-syntax.instruction-separation.php - Consider the following template: + Consider the following template: - <!-- IF condition --> - some content - <!-- ENDIF --> + <!-- IF condition --> + some content + <!-- ENDIF --> - If we were to simply preserve all whitespace, we could simply - replace all "?>" tags with "?>\n". - Doing that, would add additional newlines to the compiled - template in place of the IF and ENDIF statements. These - newlines are unwanted (and one is conditional). The IF and - ENDIF are usually on their own line for ease of reading. + If we were to simply preserve all whitespace, we could simply + replace all "?>" tags with "?>\n". + Doing that, would add additional newlines to the compiled + template in place of the IF and ENDIF statements. These + newlines are unwanted (and one is conditional). The IF and + ENDIF are usually on their own line for ease of reading. - This replacement preserves newlines only for statements that - are not the only statement on a line. It will NOT preserve - newlines at the end of statements in the above example. - It will preserve newlines in situations like: + This replacement preserves newlines only for statements that + are not the only statement on a line. It will NOT preserve + newlines at the end of statements in the above example. + It will preserve newlines in situations like: - <!-- IF condition -->inline content<!-- ENDIF --> + <!-- IF condition -->inline content<!-- ENDIF --> - */ + */ + + $data = preg_replace('~(?<!^)(<\?php.+(?<!/\*\*/)\?>)$~m', "$1\n", $data); + $data = str_replace('/**/?>', "?>\n", $data); + $data = str_replace('?><?php', '', $data); + } - $data = preg_replace('~(?<!^)(<\?php.+(?<!/\*\*/)\?>)$~m', "$1\n", $data); - $data = str_replace('/**/?>', "?>\n", $data); - $data = str_replace('?><?php', '', $data); return $data; } @@ -335,6 +348,10 @@ class phpbb_template_filter extends php_user_filter return '<?php ' . $this->compile_tag_define($matches[2], false) . ' ?>'; break; + case 'ENDDEFINE': + return '<?php ' . $this->compile_tag_enddefine() . ' ?>'; + break; + case 'INCLUDE': return '<?php ' . $this->compile_tag_include($matches[2]) . ' ?>'; break; @@ -458,6 +475,7 @@ class phpbb_template_filter extends php_user_filter */ private function compile_var_tags(&$text_blocks) { + $is_expr = null; $text_blocks = $this->get_varref($text_blocks, $is_expr); $lang_replaced = $this->compile_language_tags($text_blocks); @@ -839,6 +857,16 @@ class phpbb_template_filter extends php_user_filter $match = array(); preg_match('#^((?:' . self::REGEX_NS . '\.)+)?\$(?=[A-Z])([A-Z0-9_\-]*)(?: = (.*?))?$#', $tag_args, $match); + if (!empty($match[2]) && !isset($match[3]) && $op) + { + // DEFINE tag with ENDDEFINE + $array = "\$_tpldata['DEFINE']['.vars']"; + $code = 'ob_start(); '; + $code .= "if (!isset($array)) { $array = array(); } "; + $code .= "{$array}[] = '{$match[2]}'"; + return $code; + } + if (empty($match[2]) || (!isset($match[3]) && $op)) { return ''; @@ -866,6 +894,20 @@ class phpbb_template_filter extends php_user_filter } /** + * Compile ENDDEFINE tag + * + * @return string compiled template code + */ + private function compile_tag_enddefine() + { + $array = "\$_tpldata['DEFINE']['.vars']"; + $code = "if (!isset($array) || !sizeof($array)) { trigger_error('ENDDEFINE tag without DEFINE in ' . basename(__FILE__), E_USER_ERROR); }"; + $code .= "\$define_var = array_pop($array); "; + $code .= "\$_tpldata['DEFINE']['.'][\$define_var] = ob_get_clean();"; + return $code; + } + + /** * Compile INCLUDE tag * * @param string $tag_args Expression given with INCLUDE in source template @@ -967,8 +1009,14 @@ class phpbb_template_filter extends php_user_filter $all_compiled = ''; foreach ($files as $file) { + $this->template_compile->set_filter_params(array( + 'cleanup' => false, + )); + $compiled = $this->template_compile->compile_file($file); + $this->template_compile->reset_filter_params(); + if ($compiled === false) { if ($this->user) |