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  // 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  require_once (__DIR__ .'/../xmlbase.php');
  18  require_once ('cssparser.php');
  19  require_once ('pathutils.php');
  20  
  21  /**
  22   *
  23   * Older version better suited for PHP < 5.2
  24   * @deprecated
  25   * @param mixed $url
  26   * @return boolean
  27   */
  28  function is_url_deprecated($url) {
  29      if (
  30           !preg_match('#^http\\:\\/\\/[a-z0-9\-]+\.([a-z0-9\-]+\.)?[a-z]+#i', $url) &&
  31           !preg_match('#^https\\:\\/\\/[a-z0-9\-]+\.([a-z0-9\-]+\.)?[a-z]+#i', $url) &&
  32           !preg_match('#^ftp\\:\\/\\/[a-z0-9\-]+\.([a-z0-9\-]+\.)?[a-z]+#i', $url)
  33          ) {
  34          $status = false;
  35      } else {
  36          $status = true;
  37      }
  38      return $status;
  39  }
  40  
  41  /**
  42   *
  43   * validates URL
  44   * @param string $url
  45   * @return boolean
  46   */
  47  function is_url($url) {
  48      $result = filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED) !== false;
  49      return $result;
  50  }
  51  
  52  function GetDepFiles($manifestroot, $fname, $folder, &$filenames) {
  53      static $types = array('xhtml' => true, 'html' => true, 'htm' => true);
  54      $extension = strtolower(trim(pathinfo($fname, PATHINFO_EXTENSION)));
  55      $filenames = array();
  56      if (isset($types[$extension])) {
  57          $dcx = new XMLGenericDocument();
  58          $filename = $manifestroot.$folder.$fname;
  59          if (!file_exists($filename)) {
  60              $filename = $manifestroot.DIRECTORY_SEPARATOR.$folder.DIRECTORY_SEPARATOR.$fname;
  61          }
  62          if (file_exists($filename)) {
  63              $res = $dcx->loadHTMLFile($filename);
  64              if ($res) {
  65                  GetDepFilesHTML($manifestroot, $fname, $filenames, $dcx, $folder);
  66              }
  67          }
  68      }
  69  }
  70  
  71  function GetDepFilesHTML($manifestroot, $fname, &$filenames, &$dcx, $folder) {
  72      $dcx->resetXpath();
  73      $nlist         = $dcx->nodeList("//img/@src | //link/@href | //script/@src | //a[not(starts-with(@href,'#'))]/@href");
  74      $css_obj_array = array();
  75      foreach ($nlist as $nl) {
  76          $item       = $folder.$nl->nodeValue;
  77          $path_parts = pathinfo($item);
  78          $fname      = $path_parts['basename'];
  79          $ext        = array_key_exists('extension', $path_parts) ? $path_parts['extension'] : '';
  80          if (!is_url($folder.$nl->nodeValue) && !is_url($nl->nodeValue)) {
  81              $path = $folder.$nl->nodeValue;
  82              $file = fullPath($path, "/");
  83              toNativePath($file);
  84              if (file_exists($manifestroot.DIRECTORY_SEPARATOR.$file)) {
  85                  $filenames[$file] = $file;
  86              }
  87          }
  88          if ($ext == 'css') {
  89              $css = new cssparser();
  90              $css->Parse($dcx->filePath().$nl->nodeValue);
  91              $css_obj_array[$item] = $css;
  92          }
  93      }
  94      $nlist = $dcx->nodeList("//*/@class");
  95      foreach ($nlist as $nl) {
  96          $item = $folder.$nl->nodeValue;
  97          foreach ($css_obj_array as $csskey => $cssobj) {
  98              $bimg  = $cssobj->Get($item, "background-image");
  99              $limg  = $cssobj->Get($item, "list-style-image");
 100              $npath = pathinfo($csskey);
 101              if ((!empty($bimg)) && ($bimg != 'none')) {
 102                  $value             = stripUrl($bimg, $npath['dirname'].'/');
 103                  $filenames[$value] = $value;
 104              } else if ((!empty($limg)) && ($limg != 'none')) {
 105                  $value             = stripUrl($limg, $npath['dirname'].'/');
 106                  $filenames[$value] = $value;
 107              }
 108          }
 109      }
 110      $elems_to_check = array("body", "p", "ul", "h4", "a", "th");
 111      $do_we_have_it  = array();
 112      foreach ($elems_to_check as $elem) {
 113          $do_we_have_it[$elem] = ($dcx->nodeList("//".$elem)->length > 0);
 114      }
 115      foreach ($elems_to_check as $elem) {
 116          if ($do_we_have_it[$elem]) {
 117              foreach ($css_obj_array as $csskey => $cssobj) {
 118                  $sb    = $cssobj->Get($elem, "background-image");
 119                  $sbl   = $cssobj->Get($elem, "list-style-image");
 120                  $npath = pathinfo($csskey);
 121                  if ((!empty($sb)) && ($sb != 'none')) {
 122                      $value             = stripUrl($sb, $npath['dirname'].'/');
 123                      $filenames[$value] = $value;
 124                  } else if ((!empty($sbl)) && ($sbl != 'none')) {
 125                      $value             = stripUrl($sbl, $npath['dirname'].'/');
 126                      $filenames[$value] = $value;
 127                  }
 128              }
 129          }
 130      }
 131  }