]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-18799 backup & restore - improve file.php handling by using neutral syntax to...
authorstronk7 <stronk7>
Fri, 17 Apr 2009 23:47:16 +0000 (23:47 +0000)
committerstronk7 <stronk7>
Fri, 17 Apr 2009 23:47:16 +0000 (23:47 +0000)
backup/backuplib.php
backup/restorelib.php

index 2150076a83c3a51229de2cace586c28e8377872b..43234c692754f47ecb4fa85da7ee990bcad62054 100644 (file)
             //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 &amp;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}|\/)*))?(?<![,.;])/';
+        $result = preg_replace_callback($search, 'backup_process_filephp_uses', $result);
+
         foreach ($mypreferences->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.
         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', '&amp;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) {
index 7ab4c01cfc8a0deab01c3d1846f46ac64b1197d6..4f2862943eea7a785ef64f4d1af5e6fcf37a7e02 100644 (file)
@@ -3818,6 +3818,11 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
     //    - $@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)
+    //                          |-------> &amp;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
@@ -3840,6 +3845,15 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
         $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', '&amp;forcedownload=1');
+        }
+        $result = str_replace($search, $replace, $result);
+
         if ($result != $content && debugging()) {                                  //Debug
             if (!defined('RESTORE_SILENTLY')) {
                 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';        //Debug