From 7dcf2efe8d6f368c592e767a11fd4b7b60370a0f Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Mon, 19 Sep 2016 09:53:26 -0400 Subject: [PATCH] Parse URLs in theme.config.php js/css correctly. - Resolves VUFIND-1200. --- .../View/Helper/HeadThemeResources.php | 27 ++++++++++++++++--- .../View/Helper/HeadThemeResourcesTest.php | 19 +++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/module/VuFindTheme/src/VuFindTheme/View/Helper/HeadThemeResources.php b/module/VuFindTheme/src/VuFindTheme/View/Helper/HeadThemeResources.php index 078398183ba..acb1a40d449 100644 --- a/module/VuFindTheme/src/VuFindTheme/View/Helper/HeadThemeResources.php +++ b/module/VuFindTheme/src/VuFindTheme/View/Helper/HeadThemeResources.php @@ -68,6 +68,27 @@ class HeadThemeResources extends \Zend\View\Helper\AbstractHelper $this->addScripts(); } + /** + * Given a colon-delimited configuration string, break it apart, making sure + * that URLs in the first position are not inappropriately split. + * + * @param string $current Setting to parse + * + * @return array + */ + protected function parseSetting($current) + { + $parts = explode(':', $current); + // Special case: don't explode URLs: + if (($parts[0] === 'http' || $parts[0] === 'https') + && '//' === substr($parts[1], 0, 2) + ) { + $protocol = array_shift($parts); + $parts[0] = $protocol . ':' . $parts[0]; + } + return $parts; + } + /** * Add meta tags to header. * @@ -101,7 +122,7 @@ class HeadThemeResources extends \Zend\View\Helper\AbstractHelper // Load CSS (make sure we prepend them in the appropriate order; theme // resources should load before extras added by individual templates): foreach (array_reverse($this->container->getCss()) as $current) { - $parts = explode(':', $current); + $parts = $this->parseSetting($current); $headLink()->prependStylesheet( trim($parts[0]), isset($parts[1]) ? trim($parts[1]) : 'all', @@ -112,7 +133,7 @@ class HeadThemeResources extends \Zend\View\Helper\AbstractHelper // Compile and load LESS (make sure we prepend them in the appropriate order // theme resources should load before extras added by individual templates): foreach (array_reverse($this->container->getLessCss()) as $current) { - $parts = explode(':', $current); + $parts = $this->parseSetting($current); $headLink()->prependStylesheet( $headLink()->addLessStylesheet(trim($parts[0])), isset($parts[1]) ? trim($parts[1]) : 'all', @@ -141,7 +162,7 @@ class HeadThemeResources extends \Zend\View\Helper\AbstractHelper // Load Javascript (same ordering considerations as CSS, above): $headScript = $this->getView()->plugin('headscript'); foreach (array_reverse($this->container->getJs()) as $current) { - $parts = explode(':', $current); + $parts = $this->parseSetting($current); $headScript()->prependFile( trim($parts[0]), 'text/javascript', diff --git a/module/VuFindTheme/tests/unit-tests/src/VuFindTest/View/Helper/HeadThemeResourcesTest.php b/module/VuFindTheme/tests/unit-tests/src/VuFindTest/View/Helper/HeadThemeResourcesTest.php index 8a5f7d06670..67615920c12 100644 --- a/module/VuFindTheme/tests/unit-tests/src/VuFindTest/View/Helper/HeadThemeResourcesTest.php +++ b/module/VuFindTheme/tests/unit-tests/src/VuFindTest/View/Helper/HeadThemeResourcesTest.php @@ -51,6 +51,25 @@ class HeadThemeResourcesTest extends \VuFindTest\Unit\TestCase $helper->__invoke(); } + /** + * Test configuration parsing. + * + * @return void + */ + public function testConfigParsing() + { + $helper = new HeadThemeResources($this->getResourceContainer()); + $tests = [ + 'foo:bar:baz' => ['foo', 'bar', 'baz'], + 'http://foo/bar:baz:xyzzy' => ['http://foo/bar', 'baz', 'xyzzy'] + ]; + foreach ($tests as $test => $expected) { + $this->assertEquals( + $expected, $this->callMethod($helper, 'parseSetting', [$test]) + ); + } + } + /** * Get a populated resource container for testing. * -- GitLab