Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   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-helper
  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  require_once($CFG->dirroot.'/backup/util/xml/parser/processors/grouped_parser_processor.class.php');
  26  
  27  /**
  28   * helper implementation of grouped_parser_processor that will
  29   * support the process of all the moodle2 backup files, with
  30   * complete specs about what to load (grouped or no), dispatching
  31   * to corresponding methods and basic decoding of contents
  32   * (NULLs and legacy file.php uses)
  33   *
  34   * TODO: Complete phpdocs
  35   */
  36  class restore_structure_parser_processor extends grouped_parser_processor {
  37  
  38      protected $courseid; // Course->id we are restoring to
  39      protected $step;     // @restore_structure_step using this processor
  40  
  41      public function __construct($courseid, $step) {
  42          $this->courseid = $courseid;
  43          $this->step     = $step;
  44          parent::__construct();
  45      }
  46  
  47      /**
  48       * Provide NULL and legacy file.php uses decoding
  49       */
  50      public function process_cdata($cdata) {
  51          global $CFG;
  52          if ($cdata === '$@NULL@$') {  // Some cases we know we can skip complete processing
  53              return null;
  54          } else if ($cdata === '') {
  55              return '';
  56          } else if (is_numeric($cdata)) {
  57              return $cdata;
  58          } else if (strlen($cdata ?? '') < 32) {
  59              // Impossible to have one link in 32cc.
  60              // (http://10.0.0.1/file.php/1/1.jpg, http://10.0.0.1/mod/url/view.php?id=).
  61              return $cdata;
  62          }
  63  
  64          if (strpos($cdata, '$@FILEPHP@$') !== false) {
  65              // We need to convert $@FILEPHP@$.
  66              if ($CFG->slasharguments) {
  67                  $slash = '/';
  68                  $forcedownload = '?forcedownload=1';
  69              } else {
  70                  $slash = '%2F';
  71                  $forcedownload = '&amp;forcedownload=1';
  72              }
  73  
  74              // We have to remove trailing slashes, otherwise file URLs will be restored with an extra slash.
  75              $basefileurl = rtrim(moodle_url::make_legacyfile_url($this->courseid, null)->out(true), $slash);
  76              // Decode file.php calls.
  77              $search = array ("$@FILEPHP@$");
  78              $replace = array($basefileurl);
  79              $result = str_replace($search, $replace, $cdata);
  80  
  81              // Now $@SLASH@$ and $@FORCEDOWNLOAD@$ MDL-18799.
  82              $search = array('$@SLASH@$', '$@FORCEDOWNLOAD@$');
  83              $replace = array($slash, $forcedownload);
  84  
  85              $cdata = str_replace($search, $replace, $result);
  86          }
  87  
  88          if (strpos($cdata, '$@H5PEMBED@$') !== false) {
  89              // We need to convert $@H5PEMBED@$.
  90              // Decode embed.php calls.
  91              $cdata = str_replace('$@H5PEMBED@$', $CFG->wwwroot.'/h5p/embed.php', $cdata);
  92          }
  93  
  94          return $cdata;
  95      }
  96  
  97      /**
  98       * Override this method so we'll be able to skip
  99       * dispatching some well-known chunks, like the
 100       * ones being 100% part of subplugins stuff. Useful
 101       * for allowing development without having all the
 102       * possible restore subplugins defined
 103       */
 104      protected function postprocess_chunk($data) {
 105  
 106          // Iterate over all the data tags, if any of them is
 107          // not 'subplugin_XXXX' or has value, then it's a valid chunk,
 108          // pass it to standard (parent) processing of chunks.
 109          foreach ($data['tags'] as $key => $value) {
 110              if (trim($value) !== '' || strpos($key, 'subplugin_') !== 0) {
 111                  parent::postprocess_chunk($data);
 112                  return;
 113              }
 114          }
 115          // Arrived here, all the tags correspond to sublplugins and are empty,
 116          // skip the chunk, and debug_developer notice
 117          $this->chunks--; // not counted
 118          debugging('Missing support on restore for ' . clean_param($data['path'], PARAM_PATH) .
 119                    ' subplugin (' . implode(', ', array_keys($data['tags'])) .')', DEBUG_DEVELOPER);
 120      }
 121  
 122      protected function dispatch_chunk($data) {
 123          $this->step->process($data);
 124      }
 125  
 126      protected function notify_path_start($path) {
 127          // nothing to do
 128      }
 129  
 130      protected function notify_path_end($path) {
 131          // nothing to do
 132      }
 133  }