From b94dcc42c0127db72dc0abc263605adbb1dd8976 Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Tue, 25 Aug 2015 16:09:57 +0800 Subject: [PATCH] MDL-51179 Atto: Abort autosave if there are more recent changes to the files If the files in the draft area have been modified more recently than the timestamp of the draft - do not restore the files, or you will delete any newer files. We cannot really guess here - merging the 2 lists of files will be wrong. --- lib/editor/atto/autosave-ajax.php | 58 +++++++++++++++++++------------ 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/lib/editor/atto/autosave-ajax.php b/lib/editor/atto/autosave-ajax.php index fa45fd47d16..4e9e8378016 100644 --- a/lib/editor/atto/autosave-ajax.php +++ b/lib/editor/atto/autosave-ajax.php @@ -25,6 +25,7 @@ define('AJAX_SCRIPT', true); require_once(dirname(__FILE__) . '/../../../config.php'); +require_once($CFG->libdir . '/filestorage/file_storage.php'); $contextid = required_param('contextid', PARAM_INT); $elementid = required_param('elementid', PARAM_ALPHANUMEXT); @@ -114,32 +115,43 @@ if ($action === 'save') { $stale = $record->timemodified < $before; require_once($CFG->libdir . '/filelib.php'); - // This function copies all the files in one draft area, to another area (in this case it's - // another draft area). It also rewrites the text to @@PLUGINFILE@@ links. - $newdrafttext = file_save_draft_area_files($record->draftid, - $usercontext->id, - 'user', - 'draft', - $newdraftid, - array(), - $record->drafttext); + $fs = get_file_storage(); + $files = $fs->get_directory_files($usercontext->id, 'user', 'draft', $newdraftid, '/', true, true); - // Final rewrite to the new draft area (convert the @@PLUGINFILES@@ again). - $newdrafttext = file_rewrite_pluginfile_urls($newdrafttext, - 'draftfile.php', - $usercontext->id, - 'user', - 'draft', - $newdraftid); - $record->drafttext = $newdrafttext; + $lastfilemodified = 0; + foreach ($files as $file) { + $lastfilemodified = max($lastfilemodified, $file->get_timemodified()); + } + if ($record->timemodified < $lastfilemodified) { + $stale = true; + } - $record->pageinstance = $pageinstance; - $record->draftid = $newdraftid; - $record->timemodified = time(); - $DB->update_record('editor_atto_autosave', $record); - - // A response means the draft has been restored and here is the auto-saved text. if (!$stale) { + // This function copies all the files in one draft area, to another area (in this case it's + // another draft area). It also rewrites the text to @@PLUGINFILE@@ links. + $newdrafttext = file_save_draft_area_files($record->draftid, + $usercontext->id, + 'user', + 'draft', + $newdraftid, + array(), + $record->drafttext); + + // Final rewrite to the new draft area (convert the @@PLUGINFILES@@ again). + $newdrafttext = file_rewrite_pluginfile_urls($newdrafttext, + 'draftfile.php', + $usercontext->id, + 'user', + 'draft', + $newdraftid); + $record->drafttext = $newdrafttext; + + $record->pageinstance = $pageinstance; + $record->draftid = $newdraftid; + $record->timemodified = time(); + $DB->update_record('editor_atto_autosave', $record); + + // A response means the draft has been restored and here is the auto-saved text. $response['result'] = $record->drafttext; echo json_encode($response); }