Merge pull request #183 from catalyst/issue179

Fix Issue179
This commit is contained in:
golenkovm
2020-02-10 15:57:00 +11:00
committed by GitHub
5 changed files with 62 additions and 9 deletions

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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');
}

View File

@@ -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);
}

View File

@@ -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('<meta http-equiv="refresh" content="5">', $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]);
}
}