Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * @package moodlecore
  20   * @subpackage backup-controller
  21   * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  /**
  26   * Class implementing the controller of any restore process
  27   *
  28   * This final class is in charge of controlling all the restore architecture, for any
  29   * type of backup.
  30   *
  31   * TODO: Finish phpdocs
  32   */
  33  class restore_controller extends base_controller {
  34  
  35      protected $tempdir;   // Directory under $CFG->backuptempdir awaiting restore
  36      protected $restoreid; // Unique identificator for this restore
  37  
  38      protected $courseid; // courseid where restore is going to happen
  39  
  40      protected $type;   // Type of backup (activity, section, course)
  41      protected $format; // Format of backup (moodle, imscc)
  42      protected $interactive; // yes/no
  43      protected $mode;   // Purpose of the backup (default settings)
  44      protected $userid; // user id executing the restore
  45      protected $operation; // Type of operation (backup/restore)
  46      protected $target;    // Restoring to new/existing/current_adding/_deleting
  47      protected $samesite;  // Are we restoring to the same site where the backup was generated
  48  
  49      protected $status; // Current status of the controller (created, planned, configured...)
  50      protected $precheck;    // Results of the execution of restore prechecks
  51  
  52      protected $info;   // Information retrieved from backup contents
  53      /** @var restore_plan */
  54      protected $plan;   // Restore execution plan
  55  
  56      /**
  57       * Immediate/delayed execution type.
  58       * @var integer
  59       */
  60      protected $execution;
  61      protected $executiontime; // epoch time when we want the restore to be executed (requires cron to run)
  62  
  63      protected $checksum; // Cache @checksumable results for lighter @is_checksum_correct() uses
  64  
  65      /** @var int Number of restore_controllers that are currently executing */
  66      protected static $executing = 0;
  67  
  68      /**
  69       * Constructor.
  70       *
  71       * If you specify a progress monitor, this will be used to report progress
  72       * while loading the plan, as well as for future use. (You can change it
  73       * for a different one later using set_progress.)
  74       *
  75       * @param string $tempdir Directory under $CFG->backuptempdir awaiting restore
  76       * @param int $courseid Course id where restore is going to happen
  77       * @param bool $interactive backup::INTERACTIVE_YES[true] or backup::INTERACTIVE_NO[false]
  78       * @param int $mode backup::MODE_[ GENERAL | HUB | IMPORT | SAMESITE ]
  79       * @param int $userid
  80       * @param int $target backup::TARGET_[ NEW_COURSE | CURRENT_ADDING | CURRENT_DELETING | EXISTING_ADDING | EXISTING_DELETING ]
  81       * @param \core\progress\base $progress Optional progress monitor
  82       * @param bool $releasesession Should release the session? backup::RELEASESESSION_YES or backup::RELEASESESSION_NO
  83       */
  84      public function __construct($tempdir, $courseid, $interactive, $mode, $userid, $target,
  85              \core\progress\base $progress = null, $releasesession = backup::RELEASESESSION_NO) {
  86          $this->tempdir = $tempdir;
  87          $this->courseid = $courseid;
  88          $this->interactive = $interactive;
  89          $this->mode = $mode;
  90          $this->userid = $userid;
  91          $this->target = $target;
  92          $this->releasesession = $releasesession;
  93  
  94          // Apply some defaults
  95          $this->type = '';
  96          $this->format = backup::FORMAT_UNKNOWN;
  97          $this->operation = backup::OPERATION_RESTORE;
  98          $this->executiontime = 0;
  99          $this->samesite = false;
 100          $this->checksum = '';
 101          $this->precheck = null;
 102  
 103          // Apply current backup version and release if necessary
 104          backup_controller_dbops::apply_version_and_release();
 105  
 106          // Check courseid is correct
 107          restore_check::check_courseid($this->courseid);
 108  
 109          // Check user is correct
 110          restore_check::check_user($this->userid);
 111  
 112          // Calculate unique $restoreid
 113          $this->calculate_restoreid();
 114  
 115          // Default logger chain (based on interactive/execution)
 116          $this->logger = backup_factory::get_logger_chain($this->interactive, $this->execution, $this->restoreid);
 117  
 118          // Set execution based on backup mode.
 119          if ($mode == backup::MODE_ASYNC || $mode == backup::MODE_COPY) {
 120              $this->execution = backup::EXECUTION_DELAYED;
 121          } else {
 122              $this->execution = backup::EXECUTION_INMEDIATE;
 123          }
 124  
 125          // By default there is no progress reporter unless you specify one so it
 126          // can be used during loading of the plan.
 127          if ($progress) {
 128              $this->progress = $progress;
 129          } else {
 130              $this->progress = new \core\progress\none();
 131          }
 132          $this->progress->start_progress('Constructing restore_controller');
 133  
 134          // Instantiate the output_controller singleton and active it if interactive and immediate.
 135          $oc = output_controller::get_instance();
 136          if ($this->interactive == backup::INTERACTIVE_YES && $this->execution == backup::EXECUTION_INMEDIATE) {
 137              $oc->set_active(true);
 138          }
 139  
 140          $this->log('instantiating restore controller', backup::LOG_INFO, $this->restoreid);
 141  
 142          // Set initial status
 143          $this->set_status(backup::STATUS_CREATED);
 144  
 145          // Calculate original restore format
 146          $this->format = backup_general_helper::detect_backup_format($tempdir);
 147  
 148          // If format is not moodle2, set to conversion needed
 149          if ($this->format !== backup::FORMAT_MOODLE) {
 150              $this->set_status(backup::STATUS_REQUIRE_CONV);
 151  
 152          // Else, format is moodle2, load plan, apply security and set status based on interactivity
 153          } else {
 154              // Load plan
 155              $this->load_plan();
 156  
 157              // Apply all default settings (based on type/format/mode).
 158              $this->apply_defaults();
 159  
 160              // Perform all initial security checks and apply (2nd param) them to settings automatically
 161              restore_check::check_security($this, true);
 162  
 163              if ($this->interactive == backup::INTERACTIVE_YES) {
 164                  $this->set_status(backup::STATUS_SETTING_UI);
 165              } else {
 166                  $this->set_status(backup::STATUS_NEED_PRECHECK);
 167              }
 168          }
 169  
 170          // Tell progress monitor that we finished loading.
 171          $this->progress->end_progress();
 172      }
 173  
 174      /**
 175       * Clean structures used by the restore_controller
 176       *
 177       * This method clean various structures used by the restore_controller,
 178       * destroying them in an ordered way, so their memory will be gc properly
 179       * by PHP (mainly circular references).
 180       *
 181       * Note that, while it's not mandatory to execute this method, it's highly
 182       * recommended to do so, specially in scripts performing multiple operations
 183       * (like the automated backups) or the system will run out of memory after
 184       * a few dozens of backups)
 185       */
 186      public function destroy() {
 187          // Only need to destroy circulars under the plan. Delegate to it.
 188          $this->plan->destroy();
 189          // Loggers may have also chained references, destroy them. Also closing resources when needed.
 190          $this->logger->destroy();
 191      }
 192  
 193      public function finish_ui() {
 194          if ($this->status != backup::STATUS_SETTING_UI) {
 195              throw new restore_controller_exception('cannot_finish_ui_if_not_setting_ui');
 196          }
 197          $this->set_status(backup::STATUS_NEED_PRECHECK);
 198      }
 199  
 200      public function process_ui_event() {
 201  
 202          // Perform security checks throwing exceptions (2nd param) if something is wrong
 203          restore_check::check_security($this, false);
 204      }
 205  
 206      public function set_status($status) {
 207          // Note: never save_controller() with the object info after STATUS_EXECUTING or the whole controller,
 208          // containing all the steps will be sent to DB. 100% (monster) useless.
 209          $this->log('setting controller status to', backup::LOG_DEBUG, $status);
 210          // TODO: Check it's a correct status.
 211          $this->status = $status;
 212          // Ensure that, once set to backup::STATUS_AWAITING | STATUS_NEED_PRECHECK, controller is stored in DB.
 213          // Also save if executing so we can better track progress.
 214          if ($status == backup::STATUS_AWAITING || $status == backup::STATUS_NEED_PRECHECK || $status == backup::STATUS_EXECUTING) {
 215              $this->save_controller();
 216              $tbc = self::load_controller($this->restoreid);
 217              $this->logger = $tbc->logger; // wakeup loggers
 218              $tbc->plan->destroy(); // Clean plan controller structures, keeping logger alive.
 219  
 220          } else if ($status == backup::STATUS_FINISHED_OK) {
 221              // If the operation has ended without error (backup::STATUS_FINISHED_OK)
 222              // proceed by cleaning the object from database. MDL-29262.
 223              $this->save_controller(false, true);
 224          } else if ($status == backup::STATUS_FINISHED_ERR) {
 225              // If the operation has ended with an error save the controller
 226              // preserving the object in the database. We may want it for debugging.
 227              $this->save_controller();
 228          }
 229      }
 230  
 231      public function set_execution($execution, $executiontime = 0) {
 232          $this->log('setting controller execution', backup::LOG_DEBUG);
 233          // TODO: Check valid execution mode.
 234          // TODO: Check time in future.
 235          // TODO: Check time = 0 if immediate.
 236          $this->execution = $execution;
 237          $this->executiontime = $executiontime;
 238  
 239          // Default logger chain (based on interactive/execution)
 240          $this->logger = backup_factory::get_logger_chain($this->interactive, $this->execution, $this->restoreid);
 241      }
 242  
 243  // checksumable interface methods
 244  
 245      public function calculate_checksum() {
 246          // Reset current checksum to take it out from calculations!
 247          $this->checksum = '';
 248          // Init checksum
 249          $tempchecksum = md5('tempdir-'    . $this->tempdir .
 250                              'restoreid-'  . $this->restoreid .
 251                              'courseid-'   . $this->courseid .
 252                              'type-'       . $this->type .
 253                              'format-'     . $this->format .
 254                              'interactive-'. $this->interactive .
 255                              'mode-'       . $this->mode .
 256                              'userid-'     . $this->userid .
 257                              'target-'     . $this->target .
 258                              'samesite-'   . $this->samesite .
 259                              'operation-'  . $this->operation .
 260                              'status-'     . $this->status .
 261                              'precheck-'   . backup_general_helper::array_checksum_recursive(array($this->precheck)) .
 262                              'execution-'  . $this->execution .
 263                              'plan-'       . backup_general_helper::array_checksum_recursive(array($this->plan)) .
 264                              'info-'       . backup_general_helper::array_checksum_recursive(array($this->info)) .
 265                              'logger-'     . backup_general_helper::array_checksum_recursive(array($this->logger)));
 266          $this->log('calculating controller checksum', backup::LOG_DEBUG, $tempchecksum);
 267          return $tempchecksum;
 268      }
 269  
 270      public function is_checksum_correct($checksum) {
 271          return $this->checksum === $checksum;
 272      }
 273  
 274      public function get_tempdir() {
 275          return $this->tempdir;
 276      }
 277  
 278      public function get_restoreid() {
 279          return $this->restoreid;
 280      }
 281  
 282      public function get_type() {
 283          return $this->type;
 284      }
 285  
 286      public function get_operation() {
 287          return $this->operation;
 288      }
 289  
 290      public function get_courseid() {
 291          return $this->courseid;
 292      }
 293  
 294      public function get_format() {
 295          return $this->format;
 296      }
 297  
 298      public function get_interactive() {
 299          return $this->interactive;
 300      }
 301  
 302      public function get_mode() {
 303          return $this->mode;
 304      }
 305  
 306      public function get_userid() {
 307          return $this->userid;
 308      }
 309  
 310      public function get_target() {
 311          return $this->target;
 312      }
 313  
 314      public function is_samesite() {
 315          return $this->samesite;
 316      }
 317  
 318      public function get_status() {
 319          return $this->status;
 320      }
 321  
 322      public function get_execution() {
 323          return $this->execution;
 324      }
 325  
 326      public function get_executiontime() {
 327          return $this->executiontime;
 328      }
 329  
 330      /**
 331       * Returns the restore plan
 332       * @return restore_plan
 333       */
 334      public function get_plan() {
 335          return $this->plan;
 336      }
 337      /**
 338       * Gets the value for the requested setting
 339       *
 340       * @param string $name
 341       * @param bool $default
 342       * @return mixed
 343       */
 344      public function get_setting_value($name, $default = false) {
 345          try {
 346              return $this->get_plan()->get_setting($name)->get_value();
 347          } catch (Exception $e) {
 348              debugging('Failed to find the setting: '.$name, DEBUG_DEVELOPER);
 349              return $default;
 350          }
 351      }
 352  
 353      /**
 354       * For debug only. Get a simple test display of all the settings.
 355       *
 356       * @return string
 357       */
 358      public function debug_display_all_settings_values(): string {
 359          return $this->get_plan()->debug_display_all_settings_values();
 360      }
 361  
 362      public function get_info() {
 363          return $this->info;
 364      }
 365  
 366      public function execute_plan() {
 367          // Basic/initial prevention against time/memory limits
 368          core_php_time_limit::raise(1 * 60 * 60); // 1 hour for 1 course initially granted
 369          raise_memory_limit(MEMORY_EXTRA);
 370  
 371          // Release the session so other tabs in the same session are not blocked.
 372          if ($this->get_releasesession() === backup::RELEASESESSION_YES) {
 373              \core\session\manager::write_close();
 374          }
 375  
 376          // Do course cleanup precheck, if required. This was originally in restore_ui. Moved to handle async backup/restore.
 377          if ($this->get_target() == backup::TARGET_CURRENT_DELETING || $this->get_target() == backup::TARGET_EXISTING_DELETING) {
 378              $options = array();
 379              $options['keep_roles_and_enrolments'] = $this->get_setting_value('keep_roles_and_enrolments');
 380              $options['keep_groups_and_groupings'] = $this->get_setting_value('keep_groups_and_groupings');
 381              $options['userid'] = $this->userid;
 382              restore_dbops::delete_course_content($this->get_courseid(), $options);
 383          }
 384          // If this is not a course restore or single activity restore (e.g. duplicate), inform the plan we are not
 385          // including all the activities for sure. This will affect any
 386          // task/step executed conditionally to stop processing information
 387          // for section and activity restore. MDL-28180.
 388          if ($this->get_type() !== backup::TYPE_1COURSE && $this->get_type() !== backup::TYPE_1ACTIVITY) {
 389              $this->log('notifying plan about excluded activities by type', backup::LOG_DEBUG);
 390              $this->plan->set_excluding_activities();
 391          }
 392          self::$executing++;
 393          try {
 394              $this->plan->execute();
 395          } catch (Exception $e) {
 396              self::$executing--;
 397              throw $e;
 398          }
 399          self::$executing--;
 400      }
 401  
 402      /**
 403       * Checks whether restore is currently executing. Certain parts of code that
 404       * is called during restore, but not directly part of the restore system, may
 405       * need to behave differently during restore (e.g. do not bother resetting a
 406       * cache because we know it will be reset at end of operation).
 407       *
 408       * @return bool True if any restore is currently executing
 409       */
 410      public static function is_executing() {
 411          return self::$executing > 0;
 412      }
 413  
 414      /**
 415       * Execute the restore prechecks to detect any problem before proceed with restore
 416       *
 417       * This function checks various parts of the restore (versions, users, roles...)
 418       * returning true if everything was ok or false if any warning/error was detected.
 419       * Any warning/error is returned by the get_precheck_results() method.
 420       * Note: if any problem is found it will, automatically, drop all the restore temp
 421       * tables as far as the next step is to inform about the warning/errors. If no problem
 422       * is found, then default behaviour is to keep the temp tables so, in the same request
 423       * restore will be executed, saving a lot of checks to be executed again.
 424       * Note: If for any reason (UI to show after prechecks...) you want to force temp tables
 425       * to be dropped always, you can pass true to the $droptemptablesafter parameter
 426       */
 427      public function execute_precheck($droptemptablesafter = false) {
 428          if (is_array($this->precheck)) {
 429              throw new restore_controller_exception('precheck_alredy_executed', $this->status);
 430          }
 431          if ($this->status != backup::STATUS_NEED_PRECHECK) {
 432              throw new restore_controller_exception('cannot_precheck_wrong_status', $this->status);
 433          }
 434          // Basic/initial prevention against time/memory limits
 435          core_php_time_limit::raise(1 * 60 * 60); // 1 hour for 1 course initially granted
 436          raise_memory_limit(MEMORY_EXTRA);
 437          $this->precheck = restore_prechecks_helper::execute_prechecks($this, $droptemptablesafter);
 438          if (!array_key_exists('errors', $this->precheck)) { // No errors, can be executed
 439              $this->set_status(backup::STATUS_AWAITING);
 440          }
 441          if (empty($this->precheck)) { // No errors nor warnings, return true
 442              return true;
 443          }
 444          return false;
 445      }
 446  
 447      public function get_results() {
 448          return $this->plan->get_results();
 449      }
 450  
 451      /**
 452       * Returns true if the prechecks have been executed
 453       * @return bool
 454       */
 455      public function precheck_executed() {
 456          return (is_array($this->precheck));
 457      }
 458  
 459      public function get_precheck_results() {
 460          if (!is_array($this->precheck)) {
 461              throw new restore_controller_exception('precheck_not_executed');
 462          }
 463          return $this->precheck;
 464      }
 465  
 466      /**
 467       * Save controller information
 468       *
 469       * @param bool $includeobj to decide if the object itself must be updated (true) or no (false)
 470       * @param bool $cleanobj to decide if the object itself must be cleaned (true) or no (false)
 471       */
 472      public function save_controller($includeobj = true, $cleanobj = false) {
 473          // Going to save controller to persistent storage, calculate checksum for later checks and save it
 474          // TODO: flag the controller as NA. Any operation on it should be forbidden util loaded back
 475          $this->log('saving controller to db', backup::LOG_DEBUG);
 476          if ($includeobj ) {  // Only calculate checksum if we are going to include the object.
 477              $this->checksum = $this->calculate_checksum();
 478          }
 479          restore_controller_dbops::save_controller($this, $this->checksum, $includeobj, $cleanobj);
 480      }
 481  
 482      public static function load_controller($restoreid) {
 483          // Load controller from persistent storage
 484          // TODO: flag the controller as available. Operations on it can continue
 485          $controller = restore_controller_dbops::load_controller($restoreid);
 486          $controller->log('loading controller from db', backup::LOG_DEBUG);
 487          return $controller;
 488      }
 489  
 490      /**
 491       * class method to provide pseudo random unique "correct" tempdir names
 492       */
 493      public static function get_tempdir_name($courseid = 0, $userid = 0) {
 494          // Current epoch time + courseid + userid + random bits
 495          return md5(time() . '-' . $courseid . '-'. $userid . '-'. random_string(20));
 496      }
 497  
 498      /**
 499       * Converts from current format to backup::MOODLE format
 500       */
 501      public function convert() {
 502          global $CFG;
 503          require_once($CFG->dirroot . '/backup/util/helper/convert_helper.class.php');
 504  
 505          // Basic/initial prevention against time/memory limits
 506          core_php_time_limit::raise(1 * 60 * 60); // 1 hour for 1 course initially granted
 507          raise_memory_limit(MEMORY_EXTRA);
 508          $this->progress->start_progress('Backup format conversion');
 509  
 510          if ($this->status != backup::STATUS_REQUIRE_CONV) {
 511              throw new restore_controller_exception('cannot_convert_not_required_status');
 512          }
 513  
 514          $this->log('backup format conversion required', backup::LOG_INFO);
 515  
 516          // Run conversion to the proper format
 517          if (!convert_helper::to_moodle2_format($this->get_tempdir(), $this->format, $this->get_logger())) {
 518              // todo - unable to find the conversion path, what to do now?
 519              // throwing the exception as a temporary solution
 520              throw new restore_controller_exception('unable_to_find_conversion_path');
 521          }
 522  
 523          $this->log('backup format conversion successful', backup::LOG_INFO);
 524  
 525          // If no exceptions were thrown, then we are in the proper format
 526          $this->format = backup::FORMAT_MOODLE;
 527  
 528          // Load plan, apply security and set status based on interactivity
 529          $this->load_plan();
 530  
 531          // Perform all initial security checks and apply (2nd param) them to settings automatically
 532          restore_check::check_security($this, true);
 533  
 534          if ($this->interactive == backup::INTERACTIVE_YES) {
 535              $this->set_status(backup::STATUS_SETTING_UI);
 536          } else {
 537              $this->set_status(backup::STATUS_NEED_PRECHECK);
 538          }
 539          $this->progress->end_progress();
 540      }
 541  
 542      /**
 543       * Do the necessary copy preparation actions.
 544       * This method should only be called once the backup of a copy operation is completed.
 545       *
 546       * @throws restore_controller_exception
 547       */
 548      public function prepare_copy(): void {
 549          // Check that we are in the correct mode.
 550          if ($this->mode != backup::MODE_COPY) {
 551              throw new restore_controller_exception('cannot_prepare_copy_wrong_mode');
 552          }
 553  
 554          $this->progress->start_progress('Prepare Copy');
 555  
 556          // If no exceptions were thrown, then we are in the proper format.
 557          $this->format = backup::FORMAT_MOODLE;
 558  
 559          // Load plan, apply security and set status based on interactivity.
 560          $this->load_plan();
 561  
 562          $this->set_status(backup::STATUS_NEED_PRECHECK);
 563          $this->progress->end_progress();
 564      }
 565  
 566  // Protected API starts here
 567  
 568      protected function calculate_restoreid() {
 569          // Current epoch time + tempdir + courseid + interactive + mode + userid + target + operation + random bits
 570          $this->restoreid = md5(time() . '-' . $this->tempdir . '-' . $this->courseid . '-'. $this->interactive . '-' .
 571                                 $this->mode . '-' . $this->userid . '-'. $this->target . '-' . $this->operation . '-' .
 572                                 random_string(20));
 573      }
 574  
 575      protected function load_plan() {
 576          // First of all, we need to introspect the moodle_backup.xml file
 577          // in order to detect all the required stuff. So, create the
 578          // monster $info structure where everything will be defined
 579          $this->log('loading backup info', backup::LOG_DEBUG);
 580          $this->info = backup_general_helper::get_backup_information($this->tempdir);
 581  
 582          // Set the controller type to the one found in the information
 583          $this->type = $this->info->type;
 584  
 585          // Set the controller samesite flag as needed
 586          $this->samesite = backup_general_helper::backup_is_samesite($this->info);
 587  
 588          // Now we load the plan that will be configured following the
 589          // information provided by the $info
 590          $this->log('loading controller plan', backup::LOG_DEBUG);
 591          $this->plan = new restore_plan($this);
 592          $this->plan->build(); // Build plan for this controller
 593          $this->set_status(backup::STATUS_PLANNED);
 594      }
 595  
 596      /**
 597       * Apply defaults from the global admin settings
 598       */
 599      protected function apply_defaults() {
 600          $this->log('applying restore defaults', backup::LOG_DEBUG);
 601          restore_controller_dbops::apply_config_defaults($this);
 602          $this->set_status(backup::STATUS_CONFIGURED);
 603      }
 604  }
 605  
 606  /*
 607   * Exception class used by all the @restore_controller stuff
 608   */
 609  class restore_controller_exception extends backup_exception {
 610  
 611      public function __construct($errorcode, $a=NULL, $debuginfo=null) {
 612          parent::__construct($errorcode, $a, $debuginfo);
 613      }
 614  }