From 6eef2aebd7225a44a23dd1c226f601659bdefb09 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Thu, 11 Jul 2013 15:40:28 -0400 Subject: [PATCH 1/6] [ticket/11647] Fix tests for INCLUDEJS PHPBB-11647 --- tests/template/template_includejs_test.php | 18 +++++++++--------- tests/template/templates/includejs.html | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/template/template_includejs_test.php b/tests/template/template_includejs_test.php index ea5c30891b..7616b278d6 100644 --- a/tests/template/template_includejs_test.php +++ b/tests/template/template_includejs_test.php @@ -26,19 +26,19 @@ class phpbb_template_template_includejs_test extends phpbb_template_template_tes ), array( array('TEST' => 2), - '', + '', ), array( array('TEST' => 3), - '', + '', ), array( array('TEST' => 4), - '', + '', ), array( array('TEST' => 5), - '', + '', ), array( array('TEST' => 6), @@ -62,19 +62,19 @@ class phpbb_template_template_includejs_test extends phpbb_template_template_tes ), array( array('TEST' => 11), - '', + '', ), array( array('TEST' => 12), - '', + '', ), array( array('TEST' => 13), - '', + '', ), array( array('TEST' => 14), - '', + '', ), array( array('TEST' => 15), @@ -82,7 +82,7 @@ class phpbb_template_template_includejs_test extends phpbb_template_template_tes ), array( array('TEST' => 16), - '', + '', ), array( array('TEST' => 17), diff --git a/tests/template/templates/includejs.html b/tests/template/templates/includejs.html index 3bcad76af5..2e2c52c857 100644 --- a/tests/template/templates/includejs.html +++ b/tests/template/templates/includejs.html @@ -31,7 +31,7 @@ - + From 4f3ce669f64156d666fee75800648f04fb8e4300 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Thu, 11 Jul 2013 15:41:27 -0400 Subject: [PATCH 2/6] [ticket/11647] New assets handling PHPBB-11647 --- phpBB/includes/template/asset.php | 188 ++++++++++++++++++ .../template/twig/node/includeasset.php | 23 +-- 2 files changed, 197 insertions(+), 14 deletions(-) create mode 100644 phpBB/includes/template/asset.php diff --git a/phpBB/includes/template/asset.php b/phpBB/includes/template/asset.php new file mode 100644 index 0000000000..eae62f4db0 --- /dev/null +++ b/phpBB/includes/template/asset.php @@ -0,0 +1,188 @@ +set_url($url); + } + + /** + * Set URL + * + * @param string $url URL + */ + public function set_url($url) + { + if (version_compare(PHP_VERSION, '5.4.7') < 0 && substr($url, 0, 2) === '//') + { + // Workaround for PHP 5.4.6 and older bug #62844 - add fake scheme and then remove it + $this->components = parse_url('http:' . $url); + if (isset($result['port'])) + { + return; + } + unset($result['scheme']); + $this->components = $result; + return; + } + $this->components = parse_url($url); + } + + /** + * Convert URL components into string + * + * @param array $components URL components + * @return string URL + */ + protected function join_url($components) + { + $path = ''; + if (isset($components['scheme'])) + { + $path = $components['scheme'] === '' ? '//' : $components['scheme'] . '://'; + } + + if (isset($components['user']) || isset($components['pass'])) + { + if ($path === '' && !isset($components['port'])) + { + $path = '//'; + } + $path .= $components['user']; + if (isset($components['pass'])) + { + $path .= ':' . $components['pass']; + } + $path .= '@'; + } + + if (isset($components['host'])) + { + if ($path === '' && !isset($components['port'])) + { + $path = '//'; + } + $path .= $components['host']; + if (isset($components['port'])) + { + $path .= ':' . $components['port']; + } + } + + if (isset($components['path'])) + { + $path .= $components['path']; + } + + if (isset($components['query'])) + { + $path .= '?' . $components['query']; + } + + if (isset($components['fragment'])) + { + $path .= '#' . $components['fragment']; + } + + return $path; + } + + /** + * Get URL + * + * @return string URL + */ + public function get_url() + { + return $this->join_url($this->components); + } + + /** + * Checks if URL is local and relative + * + * @return boolean True if URL is local and relative + */ + public function is_relative() + { + if (empty($this->components) || !isset($this->components['path'])) + { + // Invalid URL + return false; + } + return !isset($this->components['scheme']) && !isset($this->components['host']) && substr($this->components['path'], 0, 1) !== '/'; + } + + /** + * Get path component of current URL + * + * @return string Path + */ + public function get_path() + { + return isset($this->components['path']) ? $this->components['path'] : ''; + } + + /** + * Set path component + * + * @param string $path Path component + * @param boolean $urlencode If true, parts of path should be encoded with rawurlencode() + */ + public function set_path($path, $urlencode = false) + { + if ($urlencode) + { + $paths = explode('/', $path); + foreach ($paths as &$dir) + { + $dir = rawurlencode($dir); + } + $path = implode('/', $paths); + } + $this->components['path'] = $path; + } + + /** + * Add assets_version parameter to URL. + * Parameter will not be added if assets_version already exists in URL + * + * @param string $version Version + */ + public function add_assets_version($version) + { + if (!isset($this->components['query'])) + { + $this->components['query'] = 'assets_version=' . $version; + return; + } + $query = $this->components['query']; + if (!preg_match('/(^|[&;])assets_version=/', $query)) + { + $separator = (strpos($query, '&') === false) && (strpos($query, ';') !== false) && preg_match('/^.*=.*;.*=.*$/', $query) ? ';' : '&'; + $this->components['query'] = $query . $separator . 'assets_version=' . $version; + } + } +} diff --git a/phpBB/includes/template/twig/node/includeasset.php b/phpBB/includes/template/twig/node/includeasset.php index 5abff10e3f..1159b63827 100644 --- a/phpBB/includes/template/twig/node/includeasset.php +++ b/phpBB/includes/template/twig/node/includeasset.php @@ -33,26 +33,21 @@ class phpbb_template_twig_node_includeasset extends Twig_Node ->write("\$asset_file = ") ->subcompile($this->getNode('expr')) ->raw(";\n") - ->write("\$argument_string = \$anchor_string = '';\n") - ->write("if ((\$argument_string_start = strpos(\$asset_file, '?')) !== false) {\n") + ->write("\$asset = new phpbb_template_asset(\$asset_file);\n") + ->write("if (\$asset->is_relative()) {\n") ->indent() - ->write("\$argument_string = substr(\$asset_file, \$argument_string_start);\n") - ->write("\$asset_file = substr(\$asset_file, 0, \$argument_string_start);\n") - ->write("if ((\$anchor_string_start = strpos(\$argument_string, '#')) !== false) {\n") + ->write("\$asset_path = \$asset->get_path();") + ->write("\$local_file = \$this->getEnvironment()->get_phpbb_root_path() . \$asset_path;\n") + ->write("if (!file_exists(\$local_file)) {\n") ->indent() - ->write("\$anchor_string = substr(\$argument_string, \$anchor_string_start);\n") - ->write("\$argument_string = substr(\$argument_string, 0, \$anchor_string_start);\n") + ->write("\$local_file = \$this->getEnvironment()->getLoader()->getCacheKey(\$asset_path);\n") + ->write("\$asset->set_path(\$local_file, true);\n") ->outdent() + ->write("\$asset->add_assets_version(\$this->getEnvironment()->get_phpbb_config()['assets_version']);\n") + ->write("\$asset_file = \$asset->get_url();\n") ->write("}\n") ->outdent() ->write("}\n") - ->write("if (strpos(\$asset_file, '//') !== 0 && strpos(\$asset_file, 'http://') !== 0 && strpos(\$asset_file, 'https://') !== 0 && !file_exists(\$asset_file)) {\n") - ->indent() - ->write("\$asset_file = \$this->getEnvironment()->getLoader()->getCacheKey(\$asset_file);\n") - ->write("\$argument_string .= ((\$argument_string) ? '&' : '?') . 'assets_version={$config['assets_version']}';\n") - ->outdent() - ->write("}\n") - ->write("\$asset_file .= \$argument_string . \$anchor_string;\n") ->write("\$context['definition']->append('{$this->get_definition_name()}', '") ; From 63f1d99cc8971100fed9a74b5ca3e0850c4edcb8 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Thu, 11 Jul 2013 16:33:08 -0400 Subject: [PATCH 3/6] [ticket/11647] Fix invalid variable name PHPBB3-11647 --- phpBB/includes/template/asset.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/phpBB/includes/template/asset.php b/phpBB/includes/template/asset.php index eae62f4db0..f3b18a102a 100644 --- a/phpBB/includes/template/asset.php +++ b/phpBB/includes/template/asset.php @@ -40,12 +40,11 @@ class phpbb_template_asset { // Workaround for PHP 5.4.6 and older bug #62844 - add fake scheme and then remove it $this->components = parse_url('http:' . $url); - if (isset($result['port'])) + if (isset($this->components['port'])) { return; } - unset($result['scheme']); - $this->components = $result; + unset($this->components['scheme']); return; } $this->components = parse_url($url); From 4fbdac0b9e8089574cd3617c6c8294a54dac1abc Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Thu, 11 Jul 2013 16:38:52 -0400 Subject: [PATCH 4/6] [ticket/11647] Use $config for assets_version PHPBB3-11647 --- phpBB/includes/template/twig/node/includeasset.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/includes/template/twig/node/includeasset.php b/phpBB/includes/template/twig/node/includeasset.php index 1159b63827..ae113cadc8 100644 --- a/phpBB/includes/template/twig/node/includeasset.php +++ b/phpBB/includes/template/twig/node/includeasset.php @@ -43,7 +43,7 @@ class phpbb_template_twig_node_includeasset extends Twig_Node ->write("\$local_file = \$this->getEnvironment()->getLoader()->getCacheKey(\$asset_path);\n") ->write("\$asset->set_path(\$local_file, true);\n") ->outdent() - ->write("\$asset->add_assets_version(\$this->getEnvironment()->get_phpbb_config()['assets_version']);\n") + ->write("\$asset->add_assets_version({$config['assets_version']});\n") ->write("\$asset_file = \$asset->get_url();\n") ->write("}\n") ->outdent() From 94fc0cccdc49962e3111e291de8800a0d2d50837 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Thu, 11 Jul 2013 16:56:10 -0400 Subject: [PATCH 5/6] [ticket/11647] Allow custom ports Allow custom port number in schema-relative URLs PHPBB3-11647 --- phpBB/includes/template/asset.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/phpBB/includes/template/asset.php b/phpBB/includes/template/asset.php index f3b18a102a..99ac1f0ca7 100644 --- a/phpBB/includes/template/asset.php +++ b/phpBB/includes/template/asset.php @@ -40,10 +40,6 @@ class phpbb_template_asset { // Workaround for PHP 5.4.6 and older bug #62844 - add fake scheme and then remove it $this->components = parse_url('http:' . $url); - if (isset($this->components['port'])) - { - return; - } unset($this->components['scheme']); return; } From f5c2119e7cd0359614300be9a2bd116965a1ec7c Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Thu, 11 Jul 2013 18:13:48 -0400 Subject: [PATCH 6/6] [ticket/11647] Always use & for URLs Remove code for URLs separated with ; Add test case for mix of & and & in URLs PHPBB3-11647 --- phpBB/includes/template/asset.php | 5 ++--- tests/template/template_includejs_test.php | 14 +++++--------- tests/template/templates/includejs.html | 6 ++---- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/phpBB/includes/template/asset.php b/phpBB/includes/template/asset.php index 99ac1f0ca7..7c322cd971 100644 --- a/phpBB/includes/template/asset.php +++ b/phpBB/includes/template/asset.php @@ -40,7 +40,7 @@ class phpbb_template_asset { // Workaround for PHP 5.4.6 and older bug #62844 - add fake scheme and then remove it $this->components = parse_url('http:' . $url); - unset($this->components['scheme']); + $this->components['scheme'] = ''; return; } $this->components = parse_url($url); @@ -176,8 +176,7 @@ class phpbb_template_asset $query = $this->components['query']; if (!preg_match('/(^|[&;])assets_version=/', $query)) { - $separator = (strpos($query, '&') === false) && (strpos($query, ';') !== false) && preg_match('/^.*=.*;.*=.*$/', $query) ? ';' : '&'; - $this->components['query'] = $query . $separator . 'assets_version=' . $version; + $this->components['query'] = $query . '&assets_version=' . $version; } } } diff --git a/tests/template/template_includejs_test.php b/tests/template/template_includejs_test.php index 7616b278d6..b67fa123a1 100644 --- a/tests/template/template_includejs_test.php +++ b/tests/template/template_includejs_test.php @@ -30,16 +30,12 @@ class phpbb_template_template_includejs_test extends phpbb_template_template_tes ), array( array('TEST' => 3), - '', + '', ), array( array('TEST' => 4), '', ), - array( - array('TEST' => 5), - '', - ), array( array('TEST' => 6), '', @@ -68,10 +64,6 @@ class phpbb_template_template_includejs_test extends phpbb_template_template_tes array('TEST' => 12), '', ), - array( - array('TEST' => 13), - '', - ), array( array('TEST' => 14), '', @@ -88,6 +80,10 @@ class phpbb_template_template_includejs_test extends phpbb_template_template_tes array('TEST' => 17), '', ), + array( + array('TEST' => 18), + '', + ), ); } diff --git a/tests/template/templates/includejs.html b/tests/template/templates/includejs.html index 2e2c52c857..0bcdf1a815 100644 --- a/tests/template/templates/includejs.html +++ b/tests/template/templates/includejs.html @@ -6,8 +6,6 @@ - - @@ -24,8 +22,6 @@ - - @@ -34,5 +30,7 @@ + + {$SCRIPTS}