From 29a5db25ec21a8b349195d01bdd0cfea09814653 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sun, 7 Apr 2013 19:12:04 +0300 Subject: [PATCH 1/3] [ticket/11482] Implementation of advanced DEFINE tag Implementation of advanced DEFINE tag and ENDDEFINE PHPBB3-11482 --- phpBB/includes/template/filter.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/phpBB/includes/template/filter.php b/phpBB/includes/template/filter.php index 9e8ad2fef0..a3894905e5 100644 --- a/phpBB/includes/template/filter.php +++ b/phpBB/includes/template/filter.php @@ -329,6 +329,10 @@ class phpbb_template_filter extends php_user_filter return 'compile_tag_define($matches[2], false) . ' ?>'; break; + case 'ENDDEFINE': + return 'compile_tag_enddefine() . ' ?>'; + break; + case 'INCLUDE': return 'compile_tag_include($matches[2]) . ' ?>'; break; @@ -833,6 +837,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 ''; @@ -859,6 +873,20 @@ class phpbb_template_filter extends php_user_filter return (($match[1]) ? $this->generate_block_data_ref(substr($match[1], 0, -1), true, true) . '[\'' . $match[2] . '\']' : '$_tpldata[\'DEFINE\'][\'.\'][\'' . $match[2] . '\']') . ' = ' . $parsed_statement . ';'; } + /** + * 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 * From 5e8d92b0a84cf6ffbaefe1af5f2efd947e25aa1a Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Wed, 10 Apr 2013 09:00:34 +0300 Subject: [PATCH 2/3] [ticket/11482] Unit tests for advanced DEFINE Unit tests for advanced DEFINE and ENDDEFINE PHPBB3-11482 --- tests/template/template_test.php | 14 ++++++++++++++ tests/template/templates/define.html | 2 -- tests/template/templates/define_advanced.html | 12 ++++++++++++ tests/template/templates/define_include2.html | 11 +++++++++++ tests/template/templates/define_unclosed.html | 2 ++ 5 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 tests/template/templates/define_advanced.html create mode 100644 tests/template/templates/define_include2.html create mode 100644 tests/template/templates/define_unclosed.html diff --git a/tests/template/template_test.php b/tests/template/template_test.php index 56cc7a9de5..a3c0b69123 100644 --- a/tests/template/template_test.php +++ b/tests/template/template_test.php @@ -132,6 +132,20 @@ class phpbb_template_template_test extends phpbb_template_template_test_case array(), "xyz\nabc\nabc\nbar\nbar\nabc", ), + array( + 'define_advanced.html', + array(), + array('loop' => array(array(), array(), array(), array(), array(), array(), array()), 'test' => array(array()), 'test.deep' => array(array()), 'test.deep.defines' => array(array())), + array(), + "abc\nzxc\ncde\nbcd", + ), + array( + 'define_unclosed.html', + array(), + array(), + array(), + "test", + ), array( 'expressions.html', array(), diff --git a/tests/template/templates/define.html b/tests/template/templates/define.html index 4459fffbe0..4e6d0ee793 100644 --- a/tests/template/templates/define.html +++ b/tests/template/templates/define.html @@ -7,5 +7,3 @@ {$VALUE} {$VALUE} - - diff --git a/tests/template/templates/define_advanced.html b/tests/template/templates/define_advanced.html new file mode 100644 index 0000000000..83467a5b4b --- /dev/null +++ b/tests/template/templates/define_advanced.html @@ -0,0 +1,12 @@ + +abc + +{$VALUE} + +bcd + + +cde + + +{$INCLUDED_VALUE3} diff --git a/tests/template/templates/define_include2.html b/tests/template/templates/define_include2.html new file mode 100644 index 0000000000..874f3e1852 --- /dev/null +++ b/tests/template/templates/define_include2.html @@ -0,0 +1,11 @@ + +zxc + + +qwe + +{$INCLUDED_VALUE1} + +{$VALUE2} +{$VALUE1} + diff --git a/tests/template/templates/define_unclosed.html b/tests/template/templates/define_unclosed.html new file mode 100644 index 0000000000..1c975eab2b --- /dev/null +++ b/tests/template/templates/define_unclosed.html @@ -0,0 +1,2 @@ + +test From 8567aaed324eb87856ee6274f8330c52beecd0a3 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Wed, 10 Apr 2013 20:12:03 +0300 Subject: [PATCH 3/3] [ticket/11482] Use double quotes for code Use double quotes for code to avoid excessive escaping PHPBB3-11482 --- phpBB/includes/template/filter.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/phpBB/includes/template/filter.php b/phpBB/includes/template/filter.php index a3894905e5..5b0957280d 100644 --- a/phpBB/includes/template/filter.php +++ b/phpBB/includes/template/filter.php @@ -840,10 +840,10 @@ class phpbb_template_filter extends php_user_filter if (!empty($match[2]) && !isset($match[3]) && $op) { // DEFINE tag with ENDDEFINE - $array = '$_tpldata[\'DEFINE\'][\'.vars\']'; + $array = "\$_tpldata['DEFINE']['.vars']"; $code = 'ob_start(); '; - $code .= 'if (!isset(' . $array . ')) { ' . $array . ' = array(); } '; - $code .= $array . '[] = \'' . $match[2] . '\''; + $code .= "if (!isset($array)) { $array = array(); } "; + $code .= "{$array}[] = '{$match[2]}'"; return $code; } @@ -880,10 +880,10 @@ class phpbb_template_filter extends php_user_filter */ 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();'; + $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; }