Skip to content

Commit

Permalink
MDL-18799 backup & restore - improve file.php handling by using neutr…
Browse files Browse the repository at this point in the history
…al syntax to support any slasharguments combination
  • Loading branch information
stronk7 committed Apr 17, 2009
1 parent 66a2d3b commit 6483c5b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
29 changes: 26 additions & 3 deletions backup/backuplib.php
Expand Up @@ -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}|\/)*))?(?<![,.;])/';
$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.
Expand Down Expand Up @@ -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', '&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) {
Expand Down
14 changes: 14 additions & 0 deletions backup/restorelib.php
Expand Up @@ -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)
// |-------> &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
Expand All @@ -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', '&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
Expand Down

0 comments on commit 6483c5b

Please sign in to comment.