From 6483c5b005730ab2b37e0e74a6ebddcfc9d87c8d Mon Sep 17 00:00:00 2001 From: stronk7 Date: Fri, 17 Apr 2009 23:42:01 +0000 Subject: [PATCH] MDL-18799 backup & restore - improve file.php handling by using neutral syntax to support any slasharguments combination --- backup/backuplib.php | 29 ++++++++++++++++++++++++++--- backup/restorelib.php | 14 ++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/backup/backuplib.php b/backup/backuplib.php index aaec9a94bbef3..0f0b48c685d2c 100644 --- a/backup/backuplib.php +++ b/backup/backuplib.php @@ -2332,15 +2332,23 @@ function backup_encode_absolute_links($content) { //We are in manual backups so global preferences must exist!! $mypreferences = $preferences; } - //First, we check for every call to file.php inside the course $search = array($CFG->wwwroot.'/file.php/'.$mypreferences->backup_course, - $CFG->wwwroot.'/file.php?file=/'.$mypreferences->backup_course); + $CFG->wwwroot.'/file.php?file=/'.$mypreferences->backup_course, + $CFG->wwwroot.'/file.php?file=%2f'.$mypreferences->backup_course, + $CFG->wwwroot.'/file.php?file=%2F'.$mypreferences->backup_course); - $replace = array('$@FILEPHP@$','$@FILEPHP@$'); + $replace = array('$@FILEPHP@$', '$@FILEPHP@$', '$@FILEPHP@$', '$@FILEPHP@$'); $result = str_replace($search,$replace,$content); + // Now we look for any '$@FILEPHP@$' URLs, replacing: + // - slashes and %2F by $@SLASH@$ + // - &forcedownload=1 &forcedownload=1 and ?forcedownload=1 by $@FORCEDOWNLOAD@$ + // This way, backup contents will be neutral and independent of slasharguments configuration. MDL-18799 + $search = '/(\$@FILEPHP@\$)((?:(?:\/|%2f|%2F))(?:(?:\([-;:@#&=\pL0-9\$~_.+!*\',]*?\))|[-;:@#&=\pL0-9\$~_.+!*\',]|%[a-fA-F0-9]{2}|\/)*)?(\?(?:(?:(?:\([-;:@#&=\pL0-9\$~_.+!*\',]*?\))|[-;:@#&=?\pL0-9\$~_.+!*\',]|%[a-fA-F0-9]{2}|\/)*))?(?mods as $name => $info) { /// We only include the corresponding backuplib.php if it hasn't been included before /// This will save some load under PHP5. MDL-8700. @@ -2401,6 +2409,21 @@ function backup_encode_absolute_links($content) { return $result; } + /** + * Callback preg_replace function used by backup_encode_absolute_links() + * to process $@FILEPHP@$ URLs to get slasharguments independent URLs + */ + function backup_process_filephp_uses($matches) { + + // Replace slashes (plain and encoded) and forcedownload=1 parameter + $search = array('/', '%2f', '%2F', '?forcedownload=1', '&forcedownload=1', '&forcedownload=1'); + $replace = array('$@SLASH@$', '$@SLASH@$', '$@SLASH@$', '$@FORCEDOWNLOAD@$', '$@FORCEDOWNLOAD@$', '$@FORCEDOWNLOAD@$'); + + $result = $matches[1] . (isset($matches[2]) ? str_replace($search, $replace, $matches[2]) : '') . (isset($matches[3]) ? str_replace($search, $replace, $matches[3]) : ''); + + return $result; + } + //This function copies all the needed files under the "user" directory to the "user_files" //directory under temp/backup function backup_copy_user_files ($preferences) { diff --git a/backup/restorelib.php b/backup/restorelib.php index 79eaf17b7f9aa..708f7d831b0f3 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -3741,6 +3741,11 @@ function restore_create_events($restore,$xml_file) { // - $@FILEPHP@$ ---|------------> $CFG->wwwroot/file.php/courseid (slasharguments on) // |------------> $CFG->wwwroot/file.php?file=/courseid (slasharguments off) // + // - $@SLASH@$ --|---------------> / (slasharguments on) + // |---------------> %2F (slasharguments off) + // + // - $@FORCEDOWNLOAD@$ --|-------> ?forcedownload=1 (slasharguments on) + // |-------> &forcedownload=1(slasharguments off) //Note: Inter-activities linking is being implemented as a final //step in the restore execution, because we need to have it //finished to know all the oldid, newid equivaleces @@ -3764,6 +3769,15 @@ function restore_decode_absolute_links($content) { $replace = array(get_file_url($restore->course_id)); $result = str_replace($search,$replace,$content); + //Now $@SLASH@$ and $@FORCEDOWNLOAD@$ MDL-18799 + $search = array('$@SLASH@$', '$@FORCEDOWNLOAD@$'); + if ($CFG->slasharguments) { + $replace = array('/', '?forcedownload=1'); + } else { + $replace = array('%2F', '&forcedownload=1'); + } + $result = str_replace($search, $replace, $result); + if ($result != $content && debugging()) { //Debug if (!defined('RESTORE_SILENTLY')) { echo '

'.s($content).'
changed to
'.s($result).'

'; //Debug