diff --git a/classes/local/controllers/maintenance_static_page_generator.php b/classes/local/controllers/maintenance_static_page_generator.php index e9e20e3..d58e87b 100644 --- a/classes/local/controllers/maintenance_static_page_generator.php +++ b/classes/local/controllers/maintenance_static_page_generator.php @@ -138,6 +138,17 @@ class maintenance_static_page_generator { } } + /** + * Retrieves all URLs from file content using regular expressions. + * + * @param string $contents Content of the file + * @return array Array of all matches in multi-dimensional array + */ + public function get_urls_from_stylesheet($contents) { + preg_match_all('#url\([\'"]?(?!data:)([^\'"\)]+)#', $contents, $matches); + return $matches; + } + /** * Checks for urls inside filename. * @@ -148,15 +159,15 @@ class maintenance_static_page_generator { global $CFG; $contents = file_get_contents($filename); - if (!preg_match_all('#url\([\'"]?([^\'"\)]+)#', $contents, $matches)) { - return; - } + $matches = $this->get_urls_from_stylesheet($contents); + foreach ($matches[1] as $originalurl) { // Allow incomplete URLs in CSS, assume it is from moodle root. if (maintenance_static_page_io::is_url($originalurl)) { $fullurl = $originalurl; } else if ($originalurl[0] == '/') { - $fullurl = $CFG->wwwroot.$originalurl; + $rooturl = parse_url($CFG->wwwroot); + $fullurl = $rooturl['scheme'].'://'.$rooturl['host'].$originalurl; } else { $fullurl = $baseref.'/'.$originalurl; } diff --git a/classes/local/outagelib.php b/classes/local/outagelib.php index cf8ebbd..8a8b496 100644 --- a/classes/local/outagelib.php +++ b/classes/local/outagelib.php @@ -64,7 +64,7 @@ class outagelib { curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); // It is localhost, time to connect is enough. - curl_setopt($curl, CURLOPT_TIMEOUT, 15); // It is localhost, time to fetch index is enough. + curl_setopt($curl, CURLOPT_TIMEOUT, 60); $contents = curl_exec($curl); $mime = curl_getinfo($curl, CURLINFO_CONTENT_TYPE); curl_close($curl); diff --git a/tests/phpunit/local/controllers/fixtures/withurls-quoted.css b/tests/phpunit/local/controllers/fixtures/withurls-quoted.css index 012773c..884fef7 100644 --- a/tests/phpunit/local/controllers/fixtures/withurls-quoted.css +++ b/tests/phpunit/local/controllers/fixtures/withurls-quoted.css @@ -3,5 +3,5 @@ a { } div { - background-image: url('/auth/outage/tests/phpunit/local/controllers/fixtures/catalyst.png'); + background-image: url('/moodle/auth/outage/tests/phpunit/local/controllers/fixtures/catalyst.png'); } \ No newline at end of file diff --git a/tests/phpunit/local/controllers/fixtures/withurls.css b/tests/phpunit/local/controllers/fixtures/withurls.css index 7e1e985..c5b57fa 100644 --- a/tests/phpunit/local/controllers/fixtures/withurls.css +++ b/tests/phpunit/local/controllers/fixtures/withurls.css @@ -3,5 +3,5 @@ a { } div { - background-image: url(/auth/outage/tests/phpunit/local/controllers/fixtures/catalyst.png); + background-image: url(/moodle/auth/outage/tests/phpunit/local/controllers/fixtures/catalyst.png); } \ No newline at end of file diff --git a/tests/phpunit/local/controllers/maintenance_static_page_test.php b/tests/phpunit/local/controllers/maintenance_static_page_test.php index 4176ee5..f632284 100644 --- a/tests/phpunit/local/controllers/maintenance_static_page_test.php +++ b/tests/phpunit/local/controllers/maintenance_static_page_test.php @@ -25,6 +25,7 @@ use auth_outage\local\controllers\maintenance_static_page; use auth_outage\local\controllers\maintenance_static_page_io; +use auth_outage\local\controllers\maintenance_static_page_generator; use auth_outage\task\update_static_page; defined('MOODLE_INTERNAL') || die(); @@ -101,7 +102,7 @@ class maintenance_static_page_test extends auth_outage_base_testcase { $page->generate(); // Check for css file. - self::assertFileExists($page->get_io()->get_resources_folder().'/622ef6e83acfcb274cdf37bdb3bffa0923f9a7ad.dGV4dC9wbGFpbg'); + self::assertFileExists($page->get_io()->get_resources_folder().'/d8643101d96b093e642b15544e4d1f7815b5ba55.dGV4dC9wbGFpbg'); // Check for catalyst.png file referenced in url(..) of css. self::assertFileExists($page->get_io()->get_resources_folder().'/ff7f7f87a26a908fc72930eaefb6b57306361d16.aW1hZ2UvcG5n'); @@ -116,7 +117,7 @@ class maintenance_static_page_test extends auth_outage_base_testcase { $page->generate(); // Check for css file. - self::assertFileExists($page->get_io()->get_resources_folder().'/1d84b6d321fef780237f84834b7316c079221a31.dGV4dC9wbGFpbg'); + self::assertFileExists($page->get_io()->get_resources_folder().'/9fe2374b03953e1949d54ab750be2d8706891c03.dGV4dC9wbGFpbg'); // Check for catalyst.png file referenced in url(..) of css. self::assertFileExists($page->get_io()->get_resources_folder().'/ff7f7f87a26a908fc72930eaefb6b57306361d16.aW1hZ2UvcG5n'); @@ -395,4 +396,45 @@ class maintenance_static_page_test extends auth_outage_base_testcase { self::assertContains('', $generated); } + + /** + * Data provider for test_get_urls_from_stylesheet + * @return array + */ + public function test_get_urls_from_stylesheet_provider() { + return [ + // Empty string. + ["", 0], + // URLs that should be retrieved. + ["background:url(/theme/image.php/_s/boost/core/1581292565/t/expanded)", 1], + ["background:url('/theme/image.php/_s/boost/core/1581292565/t/expanded')", 1], + ["src:url(\"/theme/font.php/boost/core/1581292565/fontawesome-webfont.eot?#iefix&v=4.7.0\")", 1], + ["background-image:url(pix/vline-rtl.gif)", 1], + // URLs that should not be retrieved. + ["background-image:url(data:image/gif;base64,R0lGODlhYADIAP=)", 0], + ["background-image:url('data:image/gif;base64,R0lGODlhYADIAP=')", 0], + ["background-image:url(\"data:image/svg+xml;charset=utf8,%3Csvg xmlns=\'http://www.w3.org/2000/svg\'\")", 0], + // Combination of URLs used above. + ["background-image:url(pix/vline-rtl.gif) background:url(/theme/image.php/_s/boost/core/158/t/expanded)", 2], + ["background-image:url(data:image/gif;base64,R0lG=)src:url(\"/theme/font.php/fontawesome-webfont.eot\")", 1], + ]; + } + + /** + * Tests get_urls_from_stylesheet() method to get all appropriate URLS from the file. + * + * @dataProvider test_get_urls_from_stylesheet_provider + * @param string $filecontent Content of the file + * @param int $count Expected quantity of found URLs + * @throws coding_exception + */ + public function test_get_urls_from_stylesheet($filecontent, $count) { + $this->resetAfterTest(true); + $generator = new maintenance_static_page_generator(new DOMDocument(), new maintenance_static_page_io()); + $matches = $generator->get_urls_from_stylesheet($filecontent); + + self::assertInternalType('array', $matches); + self::assertCount(2, $matches); + self::assertCount($count, $matches[1]); + } }