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 cc_resource extends entities {
  27  
  28      public function generate_node () {
  29  
  30          cc2moodle::log_action('Creating Resource mods');
  31  
  32          $response = '';
  33          $sheet_mod_resource = cc2moodle::loadsheet(SHEET_COURSE_SECTIONS_SECTION_MODS_MOD_RESOURCE);
  34  
  35          if (!empty(cc2moodle::$instances['instances'][MOODLE_TYPE_RESOURCE])) {
  36              foreach (cc2moodle::$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 = cc2moodle::newx_path(cc2moodle::$manifest, cc2moodle::$namespaces);
  54  
  55          if ($instance['common_cartriedge_type'] == cc2moodle::CC_TYPE_WEBCONTENT || $instance['common_cartriedge_type'] == cc2moodle::CC_TYPE_ASSOCIATED_CONTENT) {
  56              $resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/@href');
  57              $resource = !empty($resource->item(0)->nodeValue) ? $resource->item(0)->nodeValue : '';
  58  
  59              if (empty($resource)) {
  60  
  61                  unset($resource);
  62  
  63                  $resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/imscc:file/@href');
  64                  $resource = !empty($resource->item(0)->nodeValue) ? $resource->item(0)->nodeValue : '';
  65  
  66              }
  67  
  68              if (!empty($resource)) {
  69                  $link = $resource;
  70              }
  71          }
  72  
  73          if ($instance['common_cartriedge_type'] == cc2moodle::CC_TYPE_WEBLINK) {
  74  
  75              $external_resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/imscc:file/@href')->item(0)->nodeValue;
  76  
  77              if ($external_resource) {
  78  
  79                  $resource = $this->load_xml_resource(cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $external_resource);
  80  
  81                  if (!empty($resource)) {
  82                      $xpath = cc2moodle::newx_path($resource, cc2moodle::getresourcens());
  83                      $resource = $xpath->query('//url/@href');
  84                      if ($resource->length > 0) {
  85                          $rawlink = $resource->item(0)->nodeValue;
  86                          if (!validateUrlSyntax($rawlink, 's+')) {
  87                              $changed = rawurldecode($rawlink);
  88                              if (validateUrlSyntax($changed, 's+')) {
  89                                  $link = $changed;
  90                              } else {
  91                                  $link = 'http://invalidurldetected/';
  92                              }
  93                          } else {
  94                              $link = $rawlink;
  95                          }
  96                      }
  97                  }
  98              }
  99          }
 100  
 101          $find_tags = array('[#mod_instance#]',
 102                             '[#mod_name#]',
 103                             '[#mod_type#]',
 104                             '[#mod_reference#]',
 105                             '[#mod_summary#]',
 106                             '[#mod_alltext#]',
 107                             '[#mod_options#]',
 108                             '[#date_now#]');
 109  
 110          $mod_type      = 'file';
 111          $mod_options   = 'objectframe';
 112          $mod_reference = $link;
 113          //detected if we are dealing with html file
 114          if (!empty($link) && ($instance['common_cartriedge_type'] == cc2moodle::CC_TYPE_WEBCONTENT)) {
 115              $ext = strtolower(pathinfo($link, PATHINFO_EXTENSION));
 116              if (in_array($ext, array('html', 'htm', 'xhtml'))) {
 117                  $mod_type = 'html';
 118                  //extract the content of the file
 119                  $rootpath = realpath(cc112moodle::$path_to_manifest_folder);
 120                  $htmlpath = realpath($rootpath . DIRECTORY_SEPARATOR . $link);
 121                  $dirpath  = dirname($htmlpath);
 122                  if (file_exists($htmlpath)) {
 123                      $fcontent = file_get_contents($htmlpath);
 124                      $mod_alltext = clean_param($this->prepare_content($fcontent), PARAM_CLEANHTML);
 125                      $mod_reference = '';
 126                      $mod_options = '';
 127                      //TODO: try to handle embedded resources
 128                      /**
 129                      * images, linked static resources, applets, videos
 130                      */
 131                      $doc = new DOMDocument();
 132                      $cdir = getcwd();
 133                      chdir($dirpath);
 134                      try {
 135                          if (!empty($mod_alltext) && $doc->loadHTML($mod_alltext)) {
 136                              $xpath = new DOMXPath($doc);
 137                              $attributes = array('href', 'src', 'background', 'archive', 'code');
 138                              $qtemplate = "//*[@##][not(contains(@##,'://'))]/@##";
 139                              $query = '';
 140                              foreach ($attributes as $attrname) {
 141                                  if (!empty($query)) {
 142                                      $query .= " | ";
 143                                  }
 144                                  $query .= str_replace('##', $attrname, $qtemplate);
 145                              }
 146                              $list = $xpath->query($query);
 147                              $searches = array();
 148                              $replaces = array();
 149                              foreach ($list as $resrc) {
 150                                  $rpath = $resrc->nodeValue;
 151                                  $rtp = realpath($rpath);
 152                                  if (($rtp !== false) && is_file($rtp)) {
 153                                      //file is there - we are in business
 154                                      $strip = str_replace("\\", "/", str_ireplace($rootpath, '', $rtp));
 155                                      $encoded_file = '$@FILEPHP@$'.str_replace('/', '$@SLASH@$', $strip);
 156                                      $searches[] = $resrc->nodeValue;
 157                                      $replaces[] = $encoded_file;
 158                                  }
 159                              }
 160                              $mod_alltext = str_replace($searches, $replaces, $mod_alltext);
 161                          }
 162                      } catch (Exception $e) {
 163                          //silence the complaints
 164                      }
 165                      chdir($cdir);
 166                      $mod_alltext = self::safexml($mod_alltext);
 167                  }
 168              }
 169          }
 170  
 171          $replace_values = array($instance['instance'],
 172                                  self::safexml($instance['title']),
 173                                  $mod_type,
 174                                  $mod_reference,
 175                                  '',
 176                                  $mod_alltext,
 177                                  $mod_options,
 178                                  time());
 179  
 180  
 181          return str_replace($find_tags, $replace_values, $sheet_mod_resource);
 182      }
 183  }