mirror of
https://github.com/catalyst/moodle-auth_outage.git
synced 2026-05-16 21:41:31 +02:00
Fix #234 pick up images from inline style background image.
This commit is contained in:
@@ -43,6 +43,15 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class maintenance_static_page_generator {
|
||||
/** PATTERN
|
||||
* The pattern should match the attribute values that
|
||||
* go as 'url(xxxxx)', but make sure 'url(data:xxxxx)' is not
|
||||
* rewritten. Must be case insensitive to match 'URL(xxxxx)'.
|
||||
* It should be possible to specify other background attributes as
|
||||
* 'background: color url(xxxxx) no-repeat'.
|
||||
*/
|
||||
protected const PATTERN = '/url\s*\(\s*[\'"]?(?![\'"]?data:)([^\s\'"]+)[\'"]?\s*\)/i';
|
||||
|
||||
/** @var DOMDocument */
|
||||
protected $dom;
|
||||
|
||||
@@ -83,6 +92,7 @@ class maintenance_static_page_generator {
|
||||
$this->update_link_favicon();
|
||||
$this->update_images();
|
||||
$this->remove_configured_css_selectors();
|
||||
$this->update_inline_background_images();
|
||||
|
||||
$html = $this->dom->saveHTML();
|
||||
if (trim($html) == '') {
|
||||
@@ -150,6 +160,18 @@ class maintenance_static_page_generator {
|
||||
return $matches;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a URL from inline style using regular expressions.
|
||||
*
|
||||
* @param string $style Content of the style attribute
|
||||
* @return array Array containing match
|
||||
*/
|
||||
public function get_url_from_inline_style($style) {
|
||||
preg_match(self::PATTERN, $style, $match);
|
||||
return $match;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks for urls inside filename.
|
||||
*
|
||||
@@ -218,6 +240,31 @@ class maintenance_static_page_generator {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch and fixes all inline background images.
|
||||
*/
|
||||
private function update_inline_background_images() {
|
||||
global $CFG;
|
||||
$xpath = new \DOMXPath($this->dom);
|
||||
$elements = $xpath->query("//*[@style]");
|
||||
|
||||
foreach ($elements as $element) {
|
||||
$style = $element->getAttribute("style");
|
||||
$matches = $this->get_url_from_inline_style($style);
|
||||
if (isset($matches[1])) {
|
||||
// Allow incomplete URLs in style, assume it is from moodle root.
|
||||
if (maintenance_static_page_io::is_url($matches[1])) {
|
||||
$fullurl = $matches[1];
|
||||
} else {
|
||||
$fullurl = (string) new moodle_url($matches[1]);
|
||||
}
|
||||
$newurl = $this->io->generate_file_url($fullurl);
|
||||
$updated = preg_replace(self::PATTERN, ' url('.$newurl.') ', $style);
|
||||
$element->setAttribute('style', $updated);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove from DOM the CSS selectores defined in the plugin settings.
|
||||
*/
|
||||
|
||||
@@ -193,6 +193,60 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
self::assertStringContainsString('www.example.com/moodle/auth/outage/file.php?file=', $generated);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Data provider for test_update_inline_background_images
|
||||
* @return array
|
||||
*/
|
||||
public function test_update_inline_background_images_provider() {
|
||||
return [
|
||||
// Empty string.
|
||||
["", false],
|
||||
// URLs that should be retrieved.
|
||||
["color: #FF00FF; background: lightblue url(/pluginfile.php/1/theme_custom/banner/251298630/0001.png) no-repeat", true],
|
||||
["background: lightblue url(https://www.example.com/moodle/pluginfile.php/1/theme_custom/banner/251298630/0001.png) no-repeat", true],
|
||||
["background:url('https://www.example.com/moodle/pluginfile.php/1/theme_custom/banner/251298630/0001.png')", true],
|
||||
["background-image : url( /pix/help.png);", true],
|
||||
["background-image: url ('/pix/help.png')", true],
|
||||
// URLs that should not be retrieved.
|
||||
["background-image:url(data:image/gif;base64,R0lGODlhYADIAP=)", false],
|
||||
["background-image:url('data:image/gif;base64,R0lGODlhYADIAP=')", false]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests update_inline_background_images() method to update the background images.
|
||||
*
|
||||
* @dataProvider test_update_inline_background_images_provider
|
||||
* @param string $stylecontent Content of the style to test
|
||||
* @param bool $rewrite Flag if URL should be rewritten
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public function test_update_inline_background_images($stylecontent, $rewrite) {
|
||||
global $CFG;
|
||||
$this->resetAfterTest(true);
|
||||
$generator = new maintenance_static_page_generator(new DOMDocument(), new maintenance_static_page_io());
|
||||
|
||||
$html = '<!DOCTYPE html>\n'.
|
||||
'<html><head><title>Title</title></head>'.
|
||||
'<body><div style="'.$stylecontent.'">Content</div></body></html>';
|
||||
|
||||
// Temporarily disable debugging to prevent errors because file does not exist
|
||||
$debuglevel = $CFG->debug;
|
||||
$CFG->debug = '';
|
||||
$generated = $this->generated_page_html($html);
|
||||
// Restore debugging level
|
||||
$CFG->debug = $debuglevel;
|
||||
$matches = $generator->get_url_from_inline_style($stylecontent);
|
||||
if ($rewrite) {
|
||||
self::assertStringNotContainsString($matches[1], $generated);
|
||||
self::assertStringContainsString('www.example.com/moodle/auth/outage/file.php?file=', $generated);
|
||||
self::assertIsArray($matches);
|
||||
} else {
|
||||
self::assertStringContainsString($stylecontent, $generated);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test update preview path to file.php style link.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user