diff --git a/backup/moodle2/backup_xml_transformer.class.php b/backup/moodle2/backup_xml_transformer.class.php index eb5b1766fa7..e6461658512 100644 --- a/backup/moodle2/backup_xml_transformer.class.php +++ b/backup/moodle2/backup_xml_transformer.class.php @@ -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); diff --git a/backup/moodle2/tests/backup_xml_transformer_test.php b/backup/moodle2/tests/backup_xml_transformer_test.php index 55af1763597..554aa0be8fb 100644 --- a/backup/moodle2/tests/backup_xml_transformer_test.php +++ b/backup/moodle2/tests/backup_xml_transformer_test.php @@ -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'), ); } diff --git a/backup/util/helper/restore_structure_parser_processor.class.php b/backup/util/helper/restore_structure_parser_processor.class.php index add8d855053..9fc1f6e4d27 100644 --- a/backup/util/helper/restore_structure_parser_processor.class.php +++ b/backup/util/helper/restore_structure_parser_processor.class.php @@ -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 = '&forcedownload=1'; + if (strpos($cdata, '$@FILEPHP@$') !== false) { + // We need to convert $@FILEPHP@$. + if ($CFG->slasharguments) { + $slash = '/'; + $forcedownload = '?forcedownload=1'; + } else { + $slash = '%2F'; + $forcedownload = '&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; } /** diff --git a/backup/util/helper/tests/restore_structure_parser_processor_test.php b/backup/util/helper/tests/restore_structure_parser_processor_test.php index 069163ad8bd..ea5ab2cc0e2 100644 --- a/backup/util/helper/tests/restore_structure_parser_processor_test.php +++ b/backup/util/helper/tests/restore_structure_parser_processor_test.php @@ -107,6 +107,11 @@ class restore_structure_parser_processor_test extends advanced_testcase { "Image", false ), + array( + "", + "", + true + ), ); }