Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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  require_once $CFG->dirroot .'/backup/cc/cc_lib/xmlbase.php';
  18  require_once  'cssparser.php';
  19  require_once  'pathutils.php';
  20  
  21  
  22  
  23  function is_url($url) {
  24      if (
  25           !preg_match('#^http\\:\\/\\/[a-z0-9\-]+\.([a-z0-9\-]+\.)?[a-z]+#i', $url) &&
  26           !preg_match('#^https\\:\\/\\/[a-z0-9\-]+\.([a-z0-9\-]+\.)?[a-z]+#i', $url) &&
  27           !preg_match('#^ftp\\:\\/\\/[a-z0-9\-]+\.([a-z0-9\-]+\.)?[a-z]+#i', $url)
  28          ) {
  29          $status = false;
  30      } else {
  31          $status = true;
  32      }
  33      return $status;
  34  }
  35  
  36  function GetDepFiles($manifestroot, $fname,$folder,&$filenames) {
  37  
  38      $extension      = end(explode('.',$fname));
  39      $filenames      = array();
  40      $dcx            = new XMLGenericDocument();
  41      $result         = true;
  42  
  43      switch ($extension){
  44          case 'xml':
  45                   $result = @$dcx->loadXMLFile($manifestroot.$folder.$fname);
  46                   if (!$result) {
  47                      $result = @$dcx->loadXMLFile($manifestroot.DIRECTORY_SEPARATOR.$folder.DIRECTORY_SEPARATOR.$fname);
  48                   }
  49                   GetDepFilesXML($manifestroot, $fname,$filenames,$dcx, $folder);
  50              break;
  51          case 'html':
  52          case 'htm':
  53                   $result = @$dcx->loadHTMLFile($manifestroot.$folder.$fname);
  54                   if (!$result) {
  55                      $result = @$dcx->loadHTMLFile($manifestroot.DIRECTORY_SEPARATOR.$folder.DIRECTORY_SEPARATOR.$fname);
  56                   }
  57                   GetDepFilesHTML($manifestroot, $fname,$filenames,$dcx, $folder);
  58              break;
  59      }
  60      return $result;
  61  }
  62  
  63  
  64  
  65  function GetDepFilesXML ($manifestroot, $fname,&$filenames,&$dcx, $folder){
  66          $nlist = $dcx->nodeList("//img/@src | //attachments/attachment/@href  | //link/@href | //script/@src");
  67          $css_obj_array = array();
  68          foreach ($nlist as $nl) {
  69              $item = $nl->nodeValue;
  70              $path_parts = pathinfo($item);
  71              $fname = $path_parts['basename'];
  72              $ext   = array_key_exists('extension',$path_parts) ? $path_parts['extension'] : '';
  73              if (!is_url($nl->nodeValue)) {
  74                //$file =   $folder.$nl->nodeValue; // DEPENDERA SI SE QUIERE Q SEA RELATIVO O ABSOLUTO
  75                $file =   $nl->nodeValue;
  76                toNativePath($file);
  77                $filenames[]=$file;
  78              }
  79          }
  80          $dcx->registerNS('qti','http://www.imsglobal.org/xsd/imscc/ims_qtiasiv1p2.xsd');
  81          $dcx->resetXpath();
  82          $nlist = $dcx->nodeList("//qti:mattext | //text");
  83          $dcx2 = new XMLGenericDocument();
  84          foreach ($nlist as $nl) {
  85              if ($dcx2->loadString($nl->nodeValue)){
  86                  GetDepFilesHTML($manifestroot,$fname,$filenames,$dcx2,$folder);
  87              }
  88          }
  89  }
  90  
  91  
  92  
  93  function GetDepFilesHTML ($manifestroot, $fname,&$filenames,&$dcx, $folder){
  94          $dcx->resetXpath();
  95          $nlist = $dcx->nodeList("//img/@src | //link/@href | //script/@src | //a[not(starts-with(@href,'#'))]/@href");
  96          $css_obj_array=array();
  97          foreach ($nlist as $nl) {
  98              $item = $nl->nodeValue;
  99              $path_parts = pathinfo($item);
 100              $fname = $path_parts['basename'];
 101              $ext   = array_key_exists('extension',$path_parts) ? $path_parts['extension'] : '';
 102              if (!is_url($folder.$nl->nodeValue) && !is_url($nl->nodeValue)) {
 103                $path = $nl->nodeValue;
 104                //$file = fullPath($path,"/");
 105                toNativePath($path);
 106                $filenames[]= $path;
 107              }
 108              if ($ext == 'css') {
 109                  $css = new cssparser();
 110                  $css->Parse($dcx->filePath().$nl->nodeValue);
 111                  $css_obj_array[$item]=$css;
 112              }
 113          }
 114          $nlist = $dcx->nodeList("//*/@class");
 115          foreach ($nlist as $nl) {
 116              $item = $nl->nodeValue;
 117              foreach ($css_obj_array as $csskey => $cssobj) {
 118                  $bimg = $cssobj->Get($item,"background-image");
 119                  $limg = $cssobj->Get($item,"list-style-image");
 120                  $npath = pathinfo($csskey);
 121                  if ((!empty($bimg))&& ($bimg != 'none')) {
 122                      $filenames[] = stripUrl($bimg,$npath['dirname'].'/');
 123                  } else
 124                  if ((!empty($limg))&& ($limg != 'none')) {
 125                      $filenames[] = stripUrl($limg,$npath['dirname'].'/');
 126                  }
 127              }
 128          }
 129          $elems_to_check = array("body","p","ul","h4","a","th");
 130          $do_we_have_it = array();
 131          foreach ($elems_to_check as $elem) {
 132              $do_we_have_it[$elem]=($dcx->nodeList("//".$elem)->length > 0);
 133          }
 134          foreach ($elems_to_check as $elem) {
 135              if ($do_we_have_it[$elem]) {
 136                  foreach ($css_obj_array as $csskey => $cssobj) {
 137                      $sb = $cssobj->Get($elem, "background-image");
 138                      $sbl = $cssobj->Get($elem,"list-style-image");
 139                      $npath = pathinfo($csskey);
 140                      if ((!empty($sb)) && ($sb != 'none')) {
 141                          $filenames[] = stripUrl($sb,$npath['dirname'].'/');
 142                      } else
 143                      if ((!empty($sbl)) && ($sbl != 'none')) {
 144                          $filenames[] = stripUrl($sbl,$npath['dirname'].'/');
 145                      }
 146                  }
 147              }
 148          }
 149  }