diff --git a/backup/converter/moodle1/lib.php b/backup/converter/moodle1/lib.php index c3b47c6f142..4fdf62eebcd 100644 --- a/backup/converter/moodle1/lib.php +++ b/backup/converter/moodle1/lib.php @@ -642,7 +642,7 @@ class moodle1_converter extends base_converter { } foreach ($matches[2] as $match) { $file = str_replace(array('$@FILEPHP@$', '$@SLASH@$', '$@FORCEDOWNLOAD@$'), array('', '/', ''), $match); - $files[] = urldecode($file); + $files[] = rawurldecode($file); } return array_unique($files); @@ -659,9 +659,16 @@ class moodle1_converter extends base_converter { public static function rewrite_filephp_usage($text, array $files) { foreach ($files as $file) { + // Expect URLs properly encoded by default. + $parts = explode('/', $file); + $encoded = implode('/', array_map('rawurlencode', $parts)); + $fileref = '$@FILEPHP@$'.str_replace('/', '$@SLASH@$', $encoded); + $text = str_replace($fileref.'$@FORCEDOWNLOAD@$', '@@PLUGINFILE@@'.$encoded.'?forcedownload=1', $text); + $text = str_replace($fileref, '@@PLUGINFILE@@'.$encoded, $text); + // Add support for URLs without any encoding. $fileref = '$@FILEPHP@$'.str_replace('/', '$@SLASH@$', $file); - $text = str_replace($fileref.'$@FORCEDOWNLOAD@$', '@@PLUGINFILE@@'.$file.'?forcedownload=1', $text); - $text = str_replace($fileref, '@@PLUGINFILE@@'.$file, $text); + $text = str_replace($fileref.'$@FORCEDOWNLOAD@$', '@@PLUGINFILE@@'.$encoded.'?forcedownload=1', $text); + $text = str_replace($fileref, '@@PLUGINFILE@@'.$encoded, $text); } return $text; diff --git a/backup/converter/moodle1/tests/lib_test.php b/backup/converter/moodle1/tests/lib_test.php index 20aca6279c5..bf4935cdd5f 100644 --- a/backup/converter/moodle1/tests/lib_test.php +++ b/backup/converter/moodle1/tests/lib_test.php @@ -443,23 +443,37 @@ as it is parsed from the backup file.

assertTrue(in_array('/pics/news.gif', $files)); $this->assertTrue(in_array('/MANUAL.DOC', $files)); - $text = moodle1_converter::rewrite_filephp_usage($text, array('/pics/news.gif', '/another/file/notused.txt'), $files); + $text = moodle1_converter::rewrite_filephp_usage($text, array('/pics/news.gif', '/another/file/notused.txt')); $this->assertEquals($text, 'This is a text containing links to file.php as it is parsed from the backup file.

Newsdownload image

download manual
'); } public function test_referenced_files_urlencoded() { - // This test covers MDL-36204 + $text = 'This is a text containing links to file.php as it is parsed from the backup file.

Newsno space
-
with urlencoded spaces
'; +
with urlencoded spaces
+Download the full AVI for free! (space and plus encoded) +Download the full AVI for free! (none encoded) +Download the full AVI for free! (only space encoded) +Download the full AVI for free! (only plus)'; $files = moodle1_converter::find_referenced_files($text); $this->assertEquals(gettype($files), 'array'); - $this->assertEquals(2, count($files)); + $this->assertEquals(3, count($files)); $this->assertTrue(in_array('/pics/news.gif', $files)); $this->assertTrue(in_array('/pics/news with spaces.gif', $files)); + $this->assertTrue(in_array('/illegal pics+movies/romeo+juliet.avi', $files)); + + $text = moodle1_converter::rewrite_filephp_usage($text, $files); + $this->assertEquals('This is a text containing links to file.php +as it is parsed from the backup file.

Newsno space
+
with urlencoded spaces
+Download the full AVI for free! (space and plus encoded) +Download the full AVI for free! (none encoded) +Download the full AVI for free! (only space encoded) +Download the full AVI for free! (only plus)', $text); } public function test_question_bank_conversion() {