Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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   * Defines backup_xml_transformer class
  20   *
  21   * @package     core_backup
  22   * @subpackage  moodle2
  23   * @category    backup
  24   * @copyright   2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  25   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  // Cache for storing link encoders, so that we don't need to call
  31  // register_link_encoders each time backup_xml_transformer is constructed
  32  // TODO MDL-25290 replace global with MUC code.
  33  global $LINKS_ENCODERS_CACHE;
  34  
  35  $LINKS_ENCODERS_CACHE = array();
  36  
  37  /**
  38   * Class implementing the @xml_contenttransformed logic to be applied in moodle2 backups
  39   *
  40   * TODO: Finish phpdocs
  41   */
  42  class backup_xml_transformer extends xml_contenttransformer {
  43  
  44      private $absolute_links_encoders; // array of static methods to be called in order to
  45                                        // perform the encoding of absolute links to all the
  46                                        // contents sent to xml
  47      private $courseid;                // courseid this content belongs to
  48      private $unicoderegexp;           // to know if the site supports unicode regexp
  49  
  50      public function __construct($courseid) {
  51          $this->absolute_links_encoders = array();
  52          $this->courseid = $courseid;
  53          // Check if we support unicode modifiers in regular expressions
  54          $this->unicoderegexp = @preg_match('/\pL/u', 'a'); // This will fail silently, returning false,
  55                                                             // if regexp libraries don't support unicode
  56          // Register all the available content link encoders
  57          $this->absolute_links_encoders = $this->register_link_encoders();
  58      }
  59  
  60      public function process($content) {
  61  
  62          // Array or object, debug and try our best recursively, shouldn't happen but...
  63          if (is_array($content)) {
  64              debugging('Backup XML transformer should not process arrays but plain content only', DEBUG_DEVELOPER);
  65              foreach($content as $key => $plaincontent) {
  66                  $content[$key] = $this->process($plaincontent);
  67              }
  68              return $content;
  69          } else if (is_object($content)) {
  70              debugging('Backup XML transformer should not process objects but plain content only', DEBUG_DEVELOPER);
  71              foreach((array)$content as $key => $plaincontent) {
  72                  $content[$key] = $this->process($plaincontent);
  73              }
  74              return (object)$content;
  75          }
  76  
  77          if (is_null($content)) {  // Some cases we know we can skip complete processing
  78              return '$@NULL@$';
  79          } else if ($content === '') {
  80              return '';
  81          } else if (is_numeric($content)) {
  82              return $content;
  83          } else if (strlen($content) < 32) { // Impossible to have one link in 32cc
  84              return $content;                // (http://10.0.0.1/file.php/1/1.jpg, http://10.0.0.1/mod/url/view.php?id=)
  85          }
  86  
  87          $content = $this->process_filephp_links($content); // Replace all calls to file.php by $@FILEPHP@$ in a normalised way
  88  
  89          // Replace all calls to h5p/embed.php by $@H5PEMBED@$.
  90          $content = $this->process_h5pembedphp_links($content);
  91  
  92          $content = $this->encode_absolute_links($content); // Pass the content against all the found encoders
  93  
  94          return $content;
  95      }
  96  
  97      private function process_filephp_links($content) {
  98          global $CFG;
  99  
 100          if (strpos($content, 'file.php') === false) { // No file.php, nothing to convert
 101              return $content;
 102          }
 103  
 104          //First, we check for every call to file.php inside the course
 105          $search = array($CFG->wwwroot.'/file.php/' . $this->courseid,
 106                          $CFG->wwwroot.'/file.php?file=/' . $this->courseid,
 107                          $CFG->wwwroot.'/file.php?file=%2f' . $this->courseid,
 108                          $CFG->wwwroot.'/file.php?file=%2F' . $this->courseid);
 109          $replace = array('$@FILEPHP@$', '$@FILEPHP@$', '$@FILEPHP@$', '$@FILEPHP@$');
 110          $content = str_replace($search, $replace, $content);
 111  
 112          // Now we look for any '$@FILEPHP@$' URLs, replacing:
 113          //     - slashes and %2F by $@SLASH@$
 114          //     - &forcedownload=1 &amp;forcedownload=1 and ?forcedownload=1 by $@FORCEDOWNLOAD@$
 115          // This way, backup contents will be neutral and independent of slasharguments configuration. MDL-18799
 116          // Based in $this->unicoderegexp, decide the regular expression to use
 117          if ($this->unicoderegexp) { //We can use unicode modifiers
 118              $search = '/(\$@FILEPHP@\$)((?:(?:\/|%2f|%2F))(?:(?:\([-;:@#&=\pL0-9\$~_.+!*\',]*?\))|[-;:@#&=\pL0-9\$~_.+!*\',]|%[a-fA-F0-9]{2}|\/)*)?(\?(?:(?:(?:\([-;:@#&=\pL0-9\$~_.+!*\',]*?\))|[-;:@#&=?\pL0-9\$~_.+!*\',]|%[a-fA-F0-9]{2}|\/)*))?(?<![,.;])/u';
 119          } else { //We cannot ue unicode modifiers
 120              $search = '/(\$@FILEPHP@\$)((?:(?:\/|%2f|%2F))(?:(?:\([-;:@#&=a-zA-Z0-9\$~_.+!*\',]*?\))|[-;:@#&=a-zA-Z0-9\$~_.+!*\',]|%[a-fA-F0-9]{2}|\/)*)?(\?(?:(?:(?:\([-;:@#&=a-zA-Z0-9\$~_.+!*\',]*?\))|[-;:@#&=?a-zA-Z0-9\$~_.+!*\',]|%[a-fA-F0-9]{2}|\/)*))?(?<![,.;])/';
 121          }
 122          $content = preg_replace_callback($search, array('backup_xml_transformer', 'process_filephp_uses'), $content);
 123  
 124          return $content;
 125      }
 126  
 127      /**
 128       * Replace all calls to /h5p/embed.php by $@H5PEMBED@$
 129       * to allow restore the /h5p/embed.php url in
 130       * other domains.
 131       *
 132       * @param  string $content
 133       * @return string
 134       */
 135      private function process_h5pembedphp_links($content) {
 136          global $CFG;
 137  
 138          // No /h5p/embed.php, nothing to convert.
 139          if (strpos($content, '/h5p/embed.php') === false) {
 140              return $content;
 141          }
 142  
 143          return str_replace($CFG->wwwroot.'/h5p/embed.php', '$@H5PEMBED@$', $content);
 144      }
 145  
 146      private function encode_absolute_links($content) {
 147          foreach ($this->absolute_links_encoders as $classname => $methodname) {
 148              $content = call_user_func(array($classname, $methodname), $content);
 149          }
 150          return $content;
 151      }
 152  
 153      static private function process_filephp_uses($matches) {
 154  
 155          // Replace slashes (plain and encoded) and forcedownload=1 parameter
 156          $search = array('/', '%2f', '%2F', '?forcedownload=1', '&forcedownload=1', '&amp;forcedownload=1');
 157          $replace = array('$@SLASH@$', '$@SLASH@$', '$@SLASH@$', '$@FORCEDOWNLOAD@$', '$@FORCEDOWNLOAD@$', '$@FORCEDOWNLOAD@$');
 158  
 159          $result = $matches[1] . (isset($matches[2]) ? str_replace($search, $replace, $matches[2]) : '') . (isset($matches[3]) ? str_replace($search, $replace, $matches[3]) : '');
 160  
 161          return $result;
 162      }
 163  
 164      /**
 165       * Register all available content link encoders
 166       *
 167       * @return array encoder
 168       * @todo MDL-25290 replace LINKS_ENCODERS_CACHE global with MUC code
 169       */
 170      private function register_link_encoders() {
 171          global $LINKS_ENCODERS_CACHE;
 172          // If encoder is linked, then return cached encoder.
 173          if (!empty($LINKS_ENCODERS_CACHE)) {
 174              return $LINKS_ENCODERS_CACHE;
 175          }
 176  
 177          $encoders = array();
 178  
 179          // Add the course encoder
 180          $encoders['backup_course_task'] = 'encode_content_links';
 181  
 182          // Add the module ones. Each module supporting moodle2 backups MUST have it
 183          $mods = core_component::get_plugin_list('mod');
 184          foreach ($mods as $mod => $moddir) {
 185              if (plugin_supports('mod', $mod, FEATURE_BACKUP_MOODLE2) && class_exists('backup_' . $mod . '_activity_task')) {
 186                  $encoders['backup_' . $mod . '_activity_task'] = 'encode_content_links';
 187              }
 188          }
 189  
 190          // Add the block encoders
 191          $blocks = core_component::get_plugin_list('block');
 192          foreach ($blocks as $block => $blockdir) {
 193              if (class_exists('backup_' . $block . '_block_task')) {
 194                  $encoders['backup_' . $block . '_block_task'] = 'encode_content_links';
 195              }
 196          }
 197  
 198          // Add the course format encodes
 199          // TODO: Same than blocks, need to know how courseformats are going to handle backup
 200          //       (1.9 was based in backuplib function, see code)
 201  
 202          // Add local encodes
 203          // TODO: Any interest? 1.9 never had that.
 204  
 205          $LINKS_ENCODERS_CACHE = $encoders;
 206          return $encoders;
 207      }
 208  }