Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  /**
  17   * @package   moodlecore
  18   * @subpackage backup-imscc
  19   * @copyright 2009 Mauro Rondinelli (mauro.rondinelli [AT] uvcms.com)
  20   * @copyright 2011 Darko Miletic (dmiletic@moodlerooms.com)
  21   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
  25  
  26  class cc11_resource extends entities11 {
  27  
  28      public function generate_node () {
  29  
  30          cc112moodle::log_action('Creating Resource mods');
  31  
  32          $response = '';
  33          $sheet_mod_resource = cc112moodle::loadsheet(SHEET_COURSE_SECTIONS_SECTION_MODS_MOD_RESOURCE);
  34  
  35          if (!empty(cc112moodle::$instances['instances'][MOODLE_TYPE_RESOURCE])) {
  36              foreach (cc112moodle::$instances['instances'][MOODLE_TYPE_RESOURCE] as $instance) {
  37                  $response .= $this->create_node_course_modules_mod_resource($sheet_mod_resource, $instance);
  38              }
  39          }
  40  
  41          return $response;
  42  
  43      }
  44  
  45      private function create_node_course_modules_mod_resource ($sheet_mod_resource, $instance) {
  46          global $CFG;
  47  
  48          require_once($CFG->libdir.'/validateurlsyntax.php');
  49  
  50          $link = '';
  51          $mod_alltext = '';
  52          $mod_summary = '';
  53          $xpath = cc112moodle::newx_path(cc112moodle::$manifest, cc112moodle::$namespaces);
  54  
  55          if ($instance['common_cartriedge_type'] == cc112moodle::CC_TYPE_WEBCONTENT || $instance['common_cartriedge_type'] == cc112moodle::CC_TYPE_ASSOCIATED_CONTENT) {
  56              $resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/@href');
  57              if ($resource->length > 0) {
  58                  $resource = !empty($resource->item(0)->nodeValue) ? $resource->item(0)->nodeValue : '';
  59              } else {
  60                  $resource = '';
  61              }
  62  
  63              if (empty($resource)) {
  64  
  65                  unset($resource);
  66  
  67                  $resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/imscc:file/@href');
  68                  if ($resource->length > 0) {
  69                      $resource = !empty($resource->item(0)->nodeValue) ? $resource->item(0)->nodeValue : '';
  70                  } else {
  71                      $resource = '';
  72                  }
  73  
  74              }
  75  
  76              if (!empty($resource)) {
  77                  $link = $resource;
  78              }
  79          }
  80  
  81          if ($instance['common_cartriedge_type'] == cc112moodle::CC_TYPE_WEBLINK) {
  82  
  83              $external_resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/imscc:file/@href')->item(0)->nodeValue;
  84  
  85              if ($external_resource) {
  86  
  87                  $resource = $this->load_xml_resource(cc112moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $external_resource);
  88  
  89                  if (!empty($resource)) {
  90                      $xpath = cc112moodle::newx_path($resource, cc112moodle::$resourcens);
  91                      $resource = $xpath->query('/wl:webLink/wl:url/@href');
  92                      if ($resource->length > 0) {
  93                          $rawlink = $resource->item(0)->nodeValue;
  94                          if (!validateUrlSyntax($rawlink, 's+')) {
  95                              $changed = rawurldecode($rawlink);
  96                              if (validateUrlSyntax($changed, 's+')) {
  97                                  $link = $changed;
  98                              } else {
  99                                  $link = 'http://invalidurldetected/';
 100                              }
 101                          } else {
 102                              $link = htmlspecialchars(trim($rawlink), ENT_COMPAT, 'UTF-8', false);
 103                          }
 104                      }
 105                  }
 106              }
 107          }
 108  
 109          $find_tags = array('[#mod_instance#]',
 110                             '[#mod_name#]',
 111                             '[#mod_type#]',
 112                             '[#mod_reference#]',
 113                             '[#mod_summary#]',
 114                             '[#mod_alltext#]',
 115                             '[#mod_options#]',
 116                             '[#date_now#]');
 117  
 118          $mod_type      = 'file';
 119          $mod_options   = 'objectframe';
 120          $mod_reference = $link;
 121          //detected if we are dealing with html file
 122          if (!empty($link) && ($instance['common_cartriedge_type'] == cc112moodle::CC_TYPE_WEBCONTENT)) {
 123              $ext = strtolower(pathinfo($link, PATHINFO_EXTENSION));
 124              if (in_array($ext, array('html', 'htm', 'xhtml'))) {
 125                  $mod_type = 'html';
 126                  //extract the content of the file
 127                  $rootpath = realpath(cc112moodle::$path_to_manifest_folder);
 128                  $htmlpath = realpath($rootpath . DIRECTORY_SEPARATOR . $link);
 129                  $dirpath  = dirname($htmlpath);
 130                  if (file_exists($htmlpath)) {
 131                      $fcontent = file_get_contents($htmlpath);
 132                      $mod_alltext = clean_param($this->prepare_content($fcontent), PARAM_CLEANHTML);
 133                      $mod_reference = '';
 134                      $mod_options = '';
 135                      /**
 136                       * try to handle embedded resources
 137                       * images, linked static resources, applets, videos
 138                       */
 139                      $doc = new DOMDocument();
 140                      $cdir = getcwd();
 141                      chdir($dirpath);
 142                      try {
 143                          $doc->loadHTML($mod_alltext);
 144                          $xpath = new DOMXPath($doc);
 145                          $attributes = array('href', 'src', 'background', 'archive', 'code');
 146                          $qtemplate = "//*[@##][not(contains(@##,'://'))]/@##";
 147                          $query = '';
 148                          foreach ($attributes as $attrname) {
 149                              if (!empty($query)) {
 150                                  $query .= " | ";
 151                              }
 152                              $query .= str_replace('##', $attrname, $qtemplate);
 153                          }
 154                          $list = $xpath->query($query);
 155                          $searches = array();
 156                          $replaces = array();
 157                          foreach ($list as $resrc) {
 158                              $rpath = $resrc->nodeValue;
 159                              $rtp = realpath($rpath);
 160                              if (($rtp !== false) && is_file($rtp)) {
 161                                  //file is there - we are in business
 162                                  $strip = str_replace("\\", "/", str_ireplace($rootpath, '', $rtp));
 163                                  $encoded_file = '$@FILEPHP@$'.str_replace('/', '$@SLASH@$', $strip);
 164                                  $searches[] = $resrc->nodeValue;
 165                                  $replaces[] = $encoded_file;
 166                              }
 167                          }
 168                          $mod_alltext = str_replace($searches, $replaces, $mod_alltext);
 169                      } catch (Exception $e) {
 170                          //silence the complaints
 171                      }
 172                      chdir($cdir);
 173                      $mod_alltext = self::safexml($mod_alltext);
 174                  }
 175              }
 176          }
 177  
 178          $replace_values = array($instance['instance'],
 179                                  self::safexml($instance['title']),
 180                                  $mod_type,
 181                                  $mod_reference,
 182                                  '',
 183                                  $mod_alltext,
 184                                  $mod_options,
 185                                  time());
 186  
 187          return str_replace($find_tags, $replace_values, $sheet_mod_resource);
 188      }
 189  }