Esempio n. 1
0
 /**
  * Given one backupid and the (FS) final generated file, perform its final storage
  * into Moodle file storage. For stored files it returns the complete file_info object
  *
  * Note: the $filepath is deleted if the backup file is created successfully
  *
  * If you specify the progress monitor, this will start a new progress section
  * to track progress in processing (in case this task takes a long time).
  *
  * @param int $backupid
  * @param string $filepath zip file containing the backup
  * @param \core\progress\base $progress Optional progress monitor
  * @return stored_file if created, null otherwise
  *
  * @throws moodle_exception in case of any problems
  */
 public static function store_backup_file($backupid, $filepath, \core\progress\base $progress = null)
 {
     global $CFG;
     // First of all, get some information from the backup_controller to help us decide
     list($dinfo, $cinfo, $sinfo) = backup_controller_dbops::get_moodle_backup_information($backupid, $progress);
     // Extract useful information to decide
     $hasusers = (bool) $sinfo['users']->value;
     // Backup has users
     $isannon = (bool) $sinfo['anonymize']->value;
     // Backup is anonymised
     $filename = $sinfo['filename']->value;
     // Backup filename
     $backupmode = $dinfo[0]->mode;
     // Backup mode backup::MODE_GENERAL/IMPORT/HUB
     $backuptype = $dinfo[0]->type;
     // Backup type backup::TYPE_1ACTIVITY/SECTION/COURSE
     $userid = $dinfo[0]->userid;
     // User->id executing the backup
     $id = $dinfo[0]->id;
     // Id of activity/section/course (depends of type)
     $courseid = $dinfo[0]->courseid;
     // Id of the course
     $format = $dinfo[0]->format;
     // Type of backup file
     // Quick hack. If for any reason, filename is blank, fix it here.
     // TODO: This hack will be out once MDL-22142 - P26 gets fixed
     if (empty($filename)) {
         $filename = backup_plan_dbops::get_default_backup_filename('moodle2', $backuptype, $id, $hasusers, $isannon);
     }
     // Backups of type IMPORT aren't stored ever
     if ($backupmode == backup::MODE_IMPORT) {
         return null;
     }
     if (!is_readable($filepath)) {
         // we have a problem if zip file does not exist
         throw new coding_exception('backup_helper::store_backup_file() expects valid $filepath parameter');
     }
     // Calculate file storage options of id being backup
     $ctxid = 0;
     $filearea = '';
     $component = '';
     $itemid = 0;
     switch ($backuptype) {
         case backup::TYPE_1ACTIVITY:
             $ctxid = context_module::instance($id)->id;
             $component = 'backup';
             $filearea = 'activity';
             $itemid = 0;
             break;
         case backup::TYPE_1SECTION:
             $ctxid = context_course::instance($courseid)->id;
             $component = 'backup';
             $filearea = 'section';
             $itemid = $id;
             break;
         case backup::TYPE_1COURSE:
             $ctxid = context_course::instance($courseid)->id;
             $component = 'backup';
             $filearea = 'course';
             $itemid = 0;
             break;
     }
     if ($backupmode == backup::MODE_AUTOMATED) {
         // Automated backups have there own special area!
         $filearea = 'automated';
         // If we're keeping the backup only in a chosen path, just move it there now
         // this saves copying from filepool to here later and filling trashdir.
         $config = get_config('backup');
         $dir = $config->backup_auto_destination;
         if ($config->backup_auto_storage == 1 and $dir and is_dir($dir) and is_writable($dir)) {
             $filedest = $dir . '/' . backup_plan_dbops::get_default_backup_filename($format, $backuptype, $courseid, $hasusers, $isannon, !$config->backup_shortname);
             // first try to move the file, if it is not possible copy and delete instead
             if (@rename($filepath, $filedest)) {
                 return null;
             }
             umask($CFG->umaskpermissions);
             if (copy($filepath, $filedest)) {
                 @chmod($filedest, $CFG->filepermissions);
                 // may fail because the permissions may not make sense outside of dataroot
                 unlink($filepath);
                 return null;
             } else {
                 $bc = backup_controller::load_controller($backupid);
                 $bc->log('Attempt to copy backup file to the specified directory using filesystem failed - ', backup::LOG_WARNING, $dir);
                 $bc->destroy();
             }
             // bad luck, try to deal with the file the old way - keep backup in file area if we can not copy to ext system
         }
     }
     // Backups of type HUB (by definition never have user info)
     // are sent to user's "user_tohub" file area. The upload process
     // will be responsible for cleaning that filearea once finished
     if ($backupmode == backup::MODE_HUB) {
         $ctxid = context_user::instance($userid)->id;
         $component = 'user';
         $filearea = 'tohub';
         $itemid = 0;
     }
     // Backups without user info or with the anonymise functionality
     // enabled are sent to user's "user_backup"
     // file area. Maintenance of such area is responsibility of
     // the user via corresponding file manager frontend
     if ($backupmode == backup::MODE_GENERAL && (!$hasusers || $isannon)) {
         $ctxid = context_user::instance($userid)->id;
         $component = 'user';
         $filearea = 'backup';
         $itemid = 0;
     }
     // Let's send the file to file storage, everything already defined
     $fs = get_file_storage();
     $fr = array('contextid' => $ctxid, 'component' => $component, 'filearea' => $filearea, 'itemid' => $itemid, 'filepath' => '/', 'filename' => $filename, 'userid' => $userid, 'timecreated' => time(), 'timemodified' => time());
     // If file already exists, delete if before
     // creating it again. This is BC behaviour - copy()
     // overwrites by default
     if ($fs->file_exists($fr['contextid'], $fr['component'], $fr['filearea'], $fr['itemid'], $fr['filepath'], $fr['filename'])) {
         $pathnamehash = $fs->get_pathname_hash($fr['contextid'], $fr['component'], $fr['filearea'], $fr['itemid'], $fr['filepath'], $fr['filename']);
         $sf = $fs->get_file_by_hash($pathnamehash);
         $sf->delete();
     }
     $file = $fs->create_file_from_pathname($fr, $filepath);
     unlink($filepath);
     return $file;
 }
 /**
  * Loads the backup controller if we are tracking one
  * @return backup_controller|false
  */
 public static final function load_controller($backupid = false)
 {
     // Get the backup id optional param
     if ($backupid) {
         try {
             // Try to load the controller with it.
             // If it fails at this point it is likely because this is the first load
             $controller = backup_controller::load_controller($backupid);
             return $controller;
         } catch (Exception $e) {
             return false;
         }
     }
     return $backupid;
 }