From e1c5528fff22cbd1b30ff83894c18101fc152892 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mudr=C3=A1k?= Date: Wed, 21 Nov 2012 20:56:32 +0100 Subject: [PATCH] MDL-36204 Improve moodle1 conversion of embedded files URL of files referenced in HTML fields via $@FILEPHP$@ are usually encoded if they were linked using the HTML editor. So if they contain spaces or plus signs in the name, their URL is encoded to use %20 and %2B respectively. Sometimes, for example when the HTML was edited manually in 1.9 without the editor, non-encoded file names may be put into the HTML text. Such a link used to work in 1.9 so we add an explicit support for it, too. However, we do not support partially encoded URLs (e.g. only spaces are encoded while plus signs are not). Such links are unmodified and will rely on Legacy files support. --- backup/converter/moodle1/lib.php | 13 ++++++++--- .../converter/moodle1/simpletest/testlib.php | 22 +++++++++++++++---- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/backup/converter/moodle1/lib.php b/backup/converter/moodle1/lib.php index 02256e4bbe1..457d03cfaa9 100644 --- a/backup/converter/moodle1/lib.php +++ b/backup/converter/moodle1/lib.php @@ -641,7 +641,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); @@ -658,9 +658,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/simpletest/testlib.php b/backup/converter/moodle1/simpletest/testlib.php index f61be995da6..87321c84f09 100644 --- a/backup/converter/moodle1/simpletest/testlib.php +++ b/backup/converter/moodle1/simpletest/testlib.php @@ -429,23 +429,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->assertEqual($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->assertIsA($files, 'array'); - $this->assertEqual(2, count($files)); + $this->assertEqual(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->assertEqual('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() {