Merge branch 'MDL-66593-master' of git://github.com/cescobedo/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2019-10-30 11:19:40 +01:00
commit 9554d68ddf
4 changed files with 57 additions and 19 deletions

View File

@ -85,6 +85,10 @@ class backup_xml_transformer extends xml_contenttransformer {
}
$content = $this->process_filephp_links($content); // Replace all calls to file.php by $@FILEPHP@$ in a normalised way
// Replace all calls to h5p/embed.php by $@H5PEMBED@$.
$content = $this->process_h5pembedphp_links($content);
$content = $this->encode_absolute_links($content); // Pass the content against all the found encoders
return $content;
@ -120,6 +124,25 @@ class backup_xml_transformer extends xml_contenttransformer {
return $content;
}
/**
* Replace all calls to /h5p/embed.php by $@H5PEMBED@$
* to allow restore the /h5p/embed.php url in
* other domains.
*
* @param string $content
* @return string
*/
private function process_h5pembedphp_links($content) {
global $CFG;
// No /h5p/embed.php, nothing to convert.
if (strpos($content, '/h5p/embed.php') === false) {
return $content;
}
return str_replace($CFG->wwwroot.'/h5p/embed.php', '$@H5PEMBED@$', $content);
}
private function encode_absolute_links($content) {
foreach ($this->absolute_links_encoders as $classname => $methodname) {
$content = call_user_func(array($classname, $methodname), $content);

View File

@ -76,6 +76,7 @@ class backup_xml_transformer_testcase extends advanced_testcase {
array('http://test.test/file.php?file=%2F2', 'http://test.test/file.php?file=%2F2'),
array('http://test.test/file.php?file=%2F1%2F1.jpg', '$@FILEPHP@$$@SLASH@$1.jpg'),
array('http://test.test/file.php?file=%2F1%2F%2F1.jpg', '$@FILEPHP@$$@SLASH@$$@SLASH@$1.jpg'),
array('http://test.test/h5p/embed.php?url=testurl', '$@H5PEMBED@$?url=testurl'),
);
}

View File

@ -57,30 +57,39 @@ class restore_structure_parser_processor extends grouped_parser_processor {
return $cdata;
} else if (strlen($cdata) < 32) { // Impossible to have one link in 32cc
return $cdata; // (http://10.0.0.1/file.php/1/1.jpg, http://10.0.0.1/mod/url/view.php?id=)
} else if (strpos($cdata, '$@FILEPHP@$') === false) { // No $@FILEPHP@$, nothing to convert
return $cdata;
}
if ($CFG->slasharguments) {
$slash = '/';
$forcedownload = '?forcedownload=1';
} else {
$slash = '%2F';
$forcedownload = '&amp;forcedownload=1';
if (strpos($cdata, '$@FILEPHP@$') !== false) {
// We need to convert $@FILEPHP@$.
if ($CFG->slasharguments) {
$slash = '/';
$forcedownload = '?forcedownload=1';
} else {
$slash = '%2F';
$forcedownload = '&amp;forcedownload=1';
}
// We have to remove trailing slashes, otherwise file URLs will be restored with an extra slash.
$basefileurl = rtrim(moodle_url::make_legacyfile_url($this->courseid, null)->out(true), $slash);
// Decode file.php calls.
$search = array ("$@FILEPHP@$");
$replace = array($basefileurl);
$result = str_replace($search, $replace, $cdata);
// Now $@SLASH@$ and $@FORCEDOWNLOAD@$ MDL-18799.
$search = array('$@SLASH@$', '$@FORCEDOWNLOAD@$');
$replace = array($slash, $forcedownload);
$cdata = str_replace($search, $replace, $result);
}
// We have to remove trailing slashes, otherwise file URLs will be restored with an extra slash.
$basefileurl = rtrim(moodle_url::make_legacyfile_url($this->courseid, null)->out(true), $slash);
// Decode file.php calls
$search = array ("$@FILEPHP@$");
$replace = array($basefileurl);
$result = str_replace($search, $replace, $cdata);
if (strpos($cdata, '$@H5PEMBED@$') !== false) {
// We need to convert $@H5PEMBED@$.
// Decode embed.php calls.
$cdata = str_replace('$@H5PEMBED@$', $CFG->wwwroot.'/h5p/embed.php', $cdata);
}
// Now $@SLASH@$ and $@FORCEDOWNLOAD@$ MDL-18799
$search = array('$@SLASH@$', '$@FORCEDOWNLOAD@$');
$replace = array($slash, $forcedownload);
return str_replace($search, $replace, $result);
return $cdata;
}
/**

View File

@ -107,6 +107,11 @@ class restore_structure_parser_processor_test extends advanced_testcase {
"<a href='http://test.test/file.php?file=%2F1%2F1.jpg&amp;forcedownload=1'>Image</a>",
false
),
array(
"<iframe src='$@H5PEMBED@$?url=testurl'></iframe>",
"<iframe src='http://test.test/h5p/embed.php?url=testurl'></iframe>",
true
),
);
}