Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]

   1  <?php
   2  
   3  /**
   4   * Moodle - Modular Object-Oriented Dynamic Learning Environment
   5   *          http://moodle.org
   6   * Copyright (C) 1999 onwards Martin Dougiamas  http://dougiamas.com
   7   *
   8   * This program is free software: you can redistribute it and/or modify
   9   * it under the terms of the GNU General Public License as published by
  10   * the Free Software Foundation, either version 2 of the License, or
  11   * (at your option) any later version.
  12   *
  13   * This program is distributed in the hope that it will be useful,
  14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16   * GNU General Public License for more details.
  17   *
  18   * You should have received a copy of the GNU General Public License
  19   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  20   *
  21   * @package    moodle
  22   * @subpackage lib
  23   * @author     Dan Poltawski <talktodan@gmail.com>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   *
  26   * Customised version of SimplePie for Moodle
  27   */
  28  
  29  require_once($CFG->libdir.'/filelib.php');
  30  
  31  // PLEASE NOTE: we use the simplepie class _unmodified_
  32  // through the joys of OO. Distros are free to use their stock
  33  // version of this file.
  34  require_once($CFG->libdir.'/simplepie/autoloader.php');
  35  
  36  /**
  37   * Moodle Customised version of the SimplePie class
  38   *
  39   * This class extends the stock SimplePie class
  40   * in order to make sensible configuration choices,
  41   * such as using the Moodle cache directory and
  42   * curl functions/proxy config for making http
  43   * requests in line with moodle configuration.
  44   *
  45   * @copyright 2009 Dan Poltawski <talktodan@gmail.com>
  46   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  47   * @since     Moodle 2.0
  48   */
  49  class moodle_simplepie extends SimplePie\SimplePie {
  50      /**
  51       * Constructor - creates an instance of the SimplePie class
  52       * with Moodle defaults.
  53       *
  54       * @param string $feedurl optional URL of the feed
  55       * @param int $timeout how many seconds requests should wait for server response
  56       */
  57      public function __construct($feedurl = null, $timeout = 2) {
  58          $cachedir = moodle_simplepie::get_cache_directory();
  59          check_dir_exists($cachedir);
  60  
  61          parent::__construct();
  62  
  63          // Use the Moodle class for http requests
  64          $registry = $this->get_registry();
  65          $registry->register(SimplePie\File::class, 'moodle_simplepie_file', true);
  66  
  67          // Use html purifier for text cleaning.
  68          $registry->register(SimplePie\Sanitize::class, 'moodle_simplepie_sanitize', true);
  69          $this->sanitize = new moodle_simplepie_sanitize();
  70  
  71          // Match moodle encoding
  72          $this->set_output_encoding('UTF-8');
  73  
  74          // default to a short timeout as most operations will be interactive
  75          $this->set_timeout($timeout);
  76  
  77          // 1 hour default cache
  78          $this->set_cache_location($cachedir);
  79          $this->set_cache_duration(3600);
  80  
  81          // init the feed url if passed in constructor
  82          if ($feedurl !== null) {
  83              $this->set_feed_url($feedurl);
  84              $this->init();
  85          }
  86      }
  87  
  88      /**
  89       * Get path for feed cache directory
  90       *
  91       * @return string absolute path to cache directory
  92       */
  93      private static function get_cache_directory() {
  94          global $CFG;
  95  
  96          return $CFG->cachedir.'/simplepie/';
  97      }
  98  
  99      /**
 100       * Reset RSS cache
 101       *
 102       * @return boolean success if cache clear or didn't exist
 103       */
 104      public static function reset_cache() {
 105  
 106          $cachedir = moodle_simplepie::get_cache_directory();
 107  
 108          return remove_dir($cachedir);
 109      }
 110  }
 111  
 112  /**
 113   * Moodle Customised version of the SimplePie_File class
 114   *
 115   * This class extends the stock SimplePie class
 116   * in order to utilise Moodles own curl class for making
 117   * http requests. By using the moodle curl class
 118   * we ensure that the correct proxy configuration is used.
 119   */
 120  class moodle_simplepie_file extends SimplePie\File {
 121  
 122      /**
 123       * The constructor is a copy of the stock simplepie File class which has
 124       * been modified to add in use the Moodle curl class rather than php curl
 125       * functions.
 126       */
 127      public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) {
 128          $this->url = $url;
 129          $this->method = SimplePie\SimplePie::FILE_SOURCE_REMOTE | SimplePie\SimplePie::FILE_SOURCE_CURL;
 130  
 131          $curl = new curl();
 132          $curl->setopt( array(
 133                  'CURLOPT_HEADER' => true,
 134                  'CURLOPT_TIMEOUT' => $timeout,
 135                  'CURLOPT_CONNECTTIMEOUT' => $timeout ));
 136  
 137  
 138          if ($headers !== null) {
 139              // translate simplepie headers to those class curl expects
 140              foreach($headers as $headername => $headervalue){
 141                  $headerstr = "{$headername}: {$headervalue}";
 142                  $curl->setHeader($headerstr);
 143              }
 144          }
 145  
 146          $this->headers = curl::strip_double_headers($curl->get($url));
 147  
 148          if ($curl->error) {
 149              $this->error = 'cURL Error: '.$curl->error;
 150              $this->success = false;
 151              return false;
 152          }
 153  
 154          $parser = new SimplePie\HTTP\Parser($this->headers);
 155  
 156          if ($parser->parse()) {
 157              $this->headers = $parser->headers;
 158              $this->body = trim($parser->body);
 159              $this->status_code = $parser->status_code;
 160  
 161  
 162              if (($this->status_code == 300 || $this->status_code == 301 || $this->status_code == 302 || $this->status_code == 303
 163                      || $this->status_code == 307 || $this->status_code > 307 && $this->status_code < 400)
 164                      && isset($this->headers['location']) && $this->redirects < $redirects) {
 165                  $this->redirects++;
 166                  $location = SimplePie\Misc::absolutize_url($this->headers['location'], $url);
 167                  return $this->__construct($location, $timeout, $redirects, $headers);
 168              }
 169          }
 170      }
 171  }
 172  
 173  
 174  /**
 175   * Customised feed sanitization using HTMLPurifier.
 176   */
 177  class moodle_simplepie_sanitize extends SimplePie\Sanitize {
 178      public function sanitize($data, $type, $base = '') {
 179          $data = trim($data);
 180  
 181          if ($data === '') {
 182              return '';
 183          }
 184  
 185          if ($type & SimplePie\SimplePie::CONSTRUCT_BASE64){
 186              $data = base64_decode($data);
 187          }
 188  
 189          if ($type & SimplePie\SimplePie::CONSTRUCT_MAYBE_HTML) {
 190              if (preg_match('/(&(#(x[0-9a-fA-F]+|[0-9]+)|[a-zA-Z0-9]+)|<\/[A-Za-z][^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3E]*'
 191                      . SimplePie\SimplePie::PCRE_HTML_ATTRIBUTE . '>)/', $data)) {
 192                  $type |= SimplePie\SimplePie::CONSTRUCT_HTML;
 193              } else {
 194                  $type |= SimplePie\SimplePie::CONSTRUCT_TEXT;
 195              }
 196          }
 197  
 198          if ($type & SimplePie\SimplePie::CONSTRUCT_IRI) {
 199              $absolute = $this->registry->call('Misc', 'absolutize_url', array($data, $base));
 200              if ($absolute !== false) {
 201                  $data = $absolute;
 202              }
 203              $data = clean_param($data, PARAM_URL);
 204          }
 205  
 206          if ($type & (SimplePie\SimplePie::CONSTRUCT_TEXT | SimplePie\SimplePie::CONSTRUCT_IRI)) {
 207              $data = htmlspecialchars($data, ENT_COMPAT, 'UTF-8');
 208          }
 209  
 210          $data = purify_html($data);
 211  
 212          if ($this->remove_div) {
 213              $data = preg_replace('/^<div' . SimplePie\SimplePie::PCRE_XML_ATTRIBUTE . '>/', '', $data);
 214              $data = preg_replace('/<\/div>$/', '', $data);
 215          } else {
 216              $data = preg_replace('/^<div' . SimplePie\SimplePie::PCRE_XML_ATTRIBUTE . '>/', '<div>', $data);
 217          }
 218  
 219          if ($this->output_encoding !== 'UTF-8') {
 220              core_text::convert($data, 'UTF-8', $this->output_encoding);
 221          }
 222  
 223          return $data;
 224      }
 225  }