Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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  /**
  18   * Private imscp module utility functions
  19   *
  20   * @package mod_imscp
  21   * @copyright  2009 Petr Skoda  {@link http://skodak.org}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once("$CFG->dirroot/mod/imscp/lib.php");
  28  require_once("$CFG->libdir/filelib.php");
  29  require_once("$CFG->libdir/resourcelib.php");
  30  
  31  /**
  32   * Print IMSCP content to page.
  33   *
  34   * @param stdClass $imscp module instance.
  35   * @param stdClass $cm course module.
  36   * @param stdClass $course record.
  37   */
  38  function imscp_print_content($imscp, $cm, $course) {
  39      global $PAGE, $CFG;
  40  
  41      $items = array_filter((array) unserialize_array($imscp->structure));
  42  
  43      echo '<div id="imscp_layout">';
  44      echo '<div id="imscp_toc">';
  45      echo '<div id="imscp_tree"><ul>';
  46      foreach ($items as $item) {
  47          echo imscp_htmllize_item($item, $imscp, $cm);
  48      }
  49      echo '</ul></div>';
  50      echo '<div id="imscp_nav" style="display:none">';
  51      echo '<button id="nav_skipprev">&lt;&lt;</button><button id="nav_prev">&lt;</button><button id="nav_up">^</button>';
  52      echo '<button id="nav_next">&gt;</button><button id="nav_skipnext">&gt;&gt;</button>';
  53      echo '</div>';
  54      echo '</div>';
  55      echo '</div>';
  56  
  57      $PAGE->requires->js_init_call('M.mod_imscp.init');
  58  }
  59  
  60  /**
  61   * Internal function - creates htmls structure suitable for YUI tree.
  62   */
  63  function imscp_htmllize_item($item, $imscp, $cm) {
  64      global $CFG;
  65  
  66      if ($item['href']) {
  67          if (preg_match('|^https?://|', $item['href'])) {
  68              $url = $item['href'];
  69          } else {
  70              $context = context_module::instance($cm->id);
  71              $urlbase = "$CFG->wwwroot/pluginfile.php";
  72              $path = '/'.$context->id.'/mod_imscp/content/'.$imscp->revision.'/'.$item['href'];
  73              $url = file_encode_url($urlbase, $path, false);
  74          }
  75          $result = "<li><a href=\"$url\">".$item['title'].'</a>';
  76      } else {
  77          $result = '<li>'.$item['title'];
  78      }
  79      if ($item['subitems']) {
  80          $result .= '<ul>';
  81          foreach ($item['subitems'] as $subitem) {
  82              $result .= imscp_htmllize_item($subitem, $imscp, $cm);
  83          }
  84          $result .= '</ul>';
  85      }
  86      $result .= '</li>';
  87  
  88      return $result;
  89  }
  90  
  91  /**
  92   * Parse an IMS content package's manifest file to determine its structure
  93   * @param object $imscp
  94   * @param object $context
  95   * @return array
  96   */
  97  function imscp_parse_structure($imscp, $context) {
  98      $fs = get_file_storage();
  99  
 100      if (!$manifestfile = $fs->get_file($context->id, 'mod_imscp', 'content', $imscp->revision, '/', 'imsmanifest.xml')) {
 101          return null;
 102      }
 103  
 104      return imscp_parse_manifestfile($manifestfile->get_content(), $imscp, $context);
 105  }
 106  
 107  /**
 108   * Parse the contents of a IMS package's manifest file.
 109   * @param string $manifestfilecontents the contents of the manifest file
 110   * @return array
 111   */
 112  function imscp_parse_manifestfile($manifestfilecontents, $imscp, $context) {
 113      $doc = new DOMDocument();
 114      $oldentities = libxml_disable_entity_loader(true);
 115      if (!$doc->loadXML($manifestfilecontents, LIBXML_NONET)) {
 116          return null;
 117      }
 118      libxml_disable_entity_loader($oldentities);
 119  
 120      // We put this fake URL as base in order to detect path changes caused by xml:base attributes.
 121      $doc->documentURI = 'http://grrr/';
 122  
 123      $xmlorganizations = $doc->getElementsByTagName('organizations');
 124      if (empty($xmlorganizations->length)) {
 125          return null;
 126      }
 127      $default = null;
 128      if ($xmlorganizations->item(0)->attributes->getNamedItem('default')) {
 129          $default = $xmlorganizations->item(0)->attributes->getNamedItem('default')->nodeValue;
 130      }
 131      $xmlorganization = $doc->getElementsByTagName('organization');
 132      if (empty($xmlorganization->length)) {
 133          return null;
 134      }
 135      $organization = null;
 136      foreach ($xmlorganization as $org) {
 137          if (is_null($organization)) {
 138              // Use first if default nor found.
 139              $organization = $org;
 140          }
 141          if (!$org->attributes->getNamedItem('identifier')) {
 142              continue;
 143          }
 144          if ($default === $org->attributes->getNamedItem('identifier')->nodeValue) {
 145              // Found default - use it.
 146              $organization = $org;
 147              break;
 148          }
 149      }
 150  
 151      // Load all resources.
 152      $resources = array();
 153  
 154      $xmlresources = $doc->getElementsByTagName('resource');
 155      foreach ($xmlresources as $res) {
 156          if (!$identifier = $res->attributes->getNamedItem('identifier')) {
 157              continue;
 158          }
 159          $identifier = $identifier->nodeValue;
 160          if ($xmlbase = $res->baseURI) {
 161              // Undo the fake URL, we are interested in relative links only.
 162              $xmlbase = str_replace('http://grrr/', '/', $xmlbase);
 163              $xmlbase = rtrim($xmlbase, '/').'/';
 164          } else {
 165              $xmlbase = '';
 166          }
 167          if (!$href = $res->attributes->getNamedItem('href')) {
 168              // If href not found look for <file href="help.htm"/>.
 169              $fileresources = $res->getElementsByTagName('file');
 170              foreach ($fileresources as $file) {
 171                  $href = $file->getAttribute('href');
 172              }
 173              if (pathinfo($href, PATHINFO_EXTENSION) == 'xml') {
 174                  $href = imscp_recursive_href($href, $imscp, $context);
 175              }
 176              if (empty($href)) {
 177                  continue;
 178              }
 179          } else {
 180              $href = $href->nodeValue;
 181          }
 182          if (strpos($href, 'http://') !== 0) {
 183              $href = $xmlbase.$href;
 184          }
 185          // Item href cleanup - Some packages are poorly done and use \ in urls.
 186          $href = ltrim(strtr($href, "\\", '/'), '/');
 187          $resources[$identifier] = $href;
 188      }
 189  
 190      $items = array();
 191      foreach ($organization->childNodes as $child) {
 192          if ($child->nodeName === 'item') {
 193              if (!$item = imscp_recursive_item($child, 0, $resources)) {
 194                  continue;
 195              }
 196              $items[] = $item;
 197          }
 198      }
 199  
 200      return $items;
 201  }
 202  
 203  function imscp_recursive_href($manifestfilename, $imscp, $context) {
 204      $fs = get_file_storage();
 205  
 206      $dirname = dirname($manifestfilename);
 207      $filename = basename($manifestfilename);
 208  
 209      if ($dirname !== '/') {
 210          $dirname = "/$dirname/";
 211      }
 212  
 213      if (!$manifestfile = $fs->get_file($context->id, 'mod_imscp', 'content', $imscp->revision, $dirname, $filename)) {
 214          return null;
 215      }
 216  
 217      $doc = new DOMDocument();
 218      $oldentities = libxml_disable_entity_loader(true);
 219      if (!$doc->loadXML($manifestfile->get_content(), LIBXML_NONET)) {
 220          return null;
 221      }
 222      libxml_disable_entity_loader($oldentities);
 223  
 224      $xmlresources = $doc->getElementsByTagName('resource');
 225      foreach ($xmlresources as $res) {
 226          if (!$href = $res->attributes->getNamedItem('href')) {
 227              $fileresources = $res->getElementsByTagName('file');
 228              foreach ($fileresources as $file) {
 229                  $href = $file->getAttribute('href');
 230                  if (pathinfo($href, PATHINFO_EXTENSION) == 'xml') {
 231                      $href = imscp_recursive_href($href, $imscp, $context);
 232                  }
 233  
 234                  if (pathinfo($href, PATHINFO_EXTENSION) == 'htm' || pathinfo($href, PATHINFO_EXTENSION) == 'html') {
 235                      return $href;
 236                  }
 237              }
 238          }
 239      }
 240  
 241      return $manifestfilename;
 242  }
 243  
 244  function imscp_recursive_item($xmlitem, $level, $resources) {
 245      $identifierref = '';
 246      if ($identifierref = $xmlitem->attributes->getNamedItem('identifierref')) {
 247          $identifierref = $identifierref->nodeValue;
 248      }
 249  
 250      $title = '?';
 251      $subitems = array();
 252  
 253      foreach ($xmlitem->childNodes as $child) {
 254          if ($child->nodeName === 'title') {
 255              $title = $child->textContent;
 256  
 257          } else if ($child->nodeName === 'item') {
 258              if ($subitem = imscp_recursive_item($child, $level + 1, $resources)) {
 259                  $subitems[] = $subitem;
 260              }
 261          }
 262      }
 263  
 264      return array('href'     => isset($resources[$identifierref]) ? $resources[$identifierref] : '',
 265                   'title'    => $title,
 266                   'level'    => $level,
 267                   'subitems' => $subitems,
 268                  );
 269  }
 270  
 271  /**
 272   * File browsing support class
 273   *
 274   * @copyright  2009 Petr Skoda  {@link http://skodak.org}
 275   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 276   */
 277  class imscp_file_info extends file_info {
 278      protected $course;
 279      protected $cm;
 280      protected $areas;
 281      protected $filearea;
 282  
 283      public function __construct($browser, $course, $cm, $context, $areas, $filearea) {
 284          parent::__construct($browser, $context);
 285          $this->course   = $course;
 286          $this->cm       = $cm;
 287          $this->areas    = $areas;
 288          $this->filearea = $filearea;
 289      }
 290  
 291      /**
 292       * Returns list of standard virtual file/directory identification.
 293       * The difference from stored_file parameters is that null values
 294       * are allowed in all fields
 295       * @return array with keys contextid, filearea, itemid, filepath and filename
 296       */
 297      public function get_params() {
 298          return array('contextid' => $this->context->id,
 299                       'component' => 'mod_imscp',
 300                       'filearea'  => $this->filearea,
 301                       'itemid'    => null,
 302                       'filepath'  => null,
 303                       'filename'  => null);
 304      }
 305  
 306      /**
 307       * Returns localised visible name.
 308       * @return string
 309       */
 310      public function get_visible_name() {
 311          return $this->areas[$this->filearea];
 312      }
 313  
 314      /**
 315       * Can I add new files or directories?
 316       * @return bool
 317       */
 318      public function is_writable() {
 319          return false;
 320      }
 321  
 322      /**
 323       * Is directory?
 324       * @return bool
 325       */
 326      public function is_directory() {
 327          return true;
 328      }
 329  
 330      /**
 331       * Returns list of children.
 332       * @return array of file_info instances
 333       */
 334      public function get_children() {
 335          return $this->get_filtered_children('*', false, true);
 336      }
 337  
 338      /**
 339       * Help function to return files matching extensions or their count
 340       *
 341       * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
 342       * @param bool|int $countonly if false returns the children, if an int returns just the
 343       *    count of children but stops counting when $countonly number of children is reached
 344       * @param bool $returnemptyfolders if true returns items that don't have matching files inside
 345       * @return array|int array of file_info instances or the count
 346       */
 347      private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
 348          global $DB;
 349          $params = array('contextid' => $this->context->id,
 350              'component' => 'mod_imscp',
 351              'filearea' => $this->filearea);
 352          $sql = 'SELECT DISTINCT itemid
 353                      FROM {files}
 354                      WHERE contextid = :contextid
 355                      AND component = :component
 356                      AND filearea = :filearea';
 357          if (!$returnemptyfolders) {
 358              $sql .= ' AND filename <> :emptyfilename';
 359              $params['emptyfilename'] = '.';
 360          }
 361          list($sql2, $params2) = $this->build_search_files_sql($extensions);
 362          $sql .= ' '.$sql2;
 363          $params = array_merge($params, $params2);
 364          if ($countonly !== false) {
 365              $sql .= ' ORDER BY itemid';
 366          }
 367  
 368          $rs = $DB->get_recordset_sql($sql, $params);
 369          $children = array();
 370          foreach ($rs as $record) {
 371              if ($child = $this->browser->get_file_info($this->context, 'mod_imscp', $this->filearea, $record->itemid)) {
 372                  $children[] = $child;
 373                  if ($countonly !== false && count($children) >= $countonly) {
 374                      break;
 375                  }
 376              }
 377          }
 378          $rs->close();
 379          if ($countonly !== false) {
 380              return count($children);
 381          }
 382          return $children;
 383      }
 384  
 385      /**
 386       * Returns list of children which are either files matching the specified extensions
 387       * or folders that contain at least one such file.
 388       *
 389       * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
 390       * @return array of file_info instances
 391       */
 392      public function get_non_empty_children($extensions = '*') {
 393          return $this->get_filtered_children($extensions, false);
 394      }
 395  
 396      /**
 397       * Returns the number of children which are either files matching the specified extensions
 398       * or folders containing at least one such file.
 399       *
 400       * @param string|array $extensions, for example '*' or array('.gif','.jpg')
 401       * @param int $limit stop counting after at least $limit non-empty children are found
 402       * @return int
 403       */
 404      public function count_non_empty_children($extensions = '*', $limit = 1) {
 405          return $this->get_filtered_children($extensions, $limit);
 406      }
 407  
 408      /**
 409       * Returns parent file_info instance
 410       * @return file_info or null for root
 411       */
 412      public function get_parent() {
 413          return $this->browser->get_file_info($this->context);
 414      }
 415  }