Merge branch 'MDL-32657-master-1' of git://git.luns.net.uk/moodle
[moodle.git] / lib / simplepie / moodle_simplepie.php
blob54e2d07ee3ffdc53d41b13898f314d5746510ff7
1 <?php
3 /**
4 * Moodle - Modular Object-Oriented Dynamic Learning Environment
5 * http://moodle.org
6 * Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
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.
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.
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/>.
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
26 * Customised version of SimplePie for Moodle
29 require_once($CFG->libdir.'/filelib.php');
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/simplepie.class.php');
36 /**
37 * Moodle Customised version of the SimplePie class
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.
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
49 class moodle_simplepie extends SimplePie
51 /**
52 * Constructor - creates an instance of the SimplePie class
53 * with Moodle defaults.
55 * @param string $feedurl optional URL of the feed
57 function __construct($feedurl = null) {
59 // Use the Moodle class for http requests
60 $this->file_class = 'moodle_simplepie_file';
62 $cachedir = moodle_simplepie::get_cache_directory();
63 check_dir_exists($cachedir);
65 parent::__construct();
66 // Match moodle encoding
67 $this->set_output_encoding('UTF-8');
69 // default to a short timeout as most operations will be interactive
70 $this->set_timeout(2);
72 // 1 hour default cache
73 $this->set_cache_location($cachedir);
74 $this->set_cache_duration(3600);
76 // init the feed url if passed in constructor
77 if ($feedurl !== null) {
78 $this->set_feed_url($feedurl);
79 $this->init();
83 /**
84 * Get path for feed cache directory
86 * @return string absolute path to cache directory
88 private static function get_cache_directory() {
89 global $CFG;
91 return $CFG->cachedir.'/simplepie/';
94 /**
95 * Reset RSS cache
97 * @return boolean success if cache clear or didn't exist
99 public static function reset_cache() {
101 $cachedir = moodle_simplepie::get_cache_directory();
103 return remove_dir($cachedir);
108 * Moodle Customised version of the SimplePie_File class
110 * This class extends the stock SimplePie class
111 * in order to utilise Moodles own curl class for making
112 * http requests. By using the moodle curl class
113 * we ensure that the correct proxy configuration is used.
115 class moodle_simplepie_file extends SimplePie_File
119 * The contructor is a copy of the stock simplepie File class which has
120 * been modifed to add in use the Moodle curl class rather than php curl
121 * functions.
123 function moodle_simplepie_file($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) {
124 $this->url = $url;
125 $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_CURL;
127 $curl = new curl();
128 $curl->setopt( array(
129 'CURLOPT_HEADER' => true,
130 'CURLOPT_TIMEOUT' => $timeout,
131 'CURLOPT_CONNECTTIMEOUT' => $timeout ));
134 if ($headers !== null) {
135 // translate simplepie headers to those class curl expects
136 foreach($headers as $headername => $headervalue){
137 $headerstr = "{$headername}: {$headervalue}";
138 $curl->setHeader($headerstr);
142 $this->headers = $curl->get($url);
144 if ($curl->error) {
145 $this->error = 'cURL Error: '.$curl->error;
146 $this->success = false;
147 return false;
150 $parser = new SimplePie_HTTP_Parser($this->headers);
152 if ($parser->parse()) {
153 $this->headers = $parser->headers;
154 $this->body = $parser->body;
155 $this->status_code = $parser->status_code;
158 if (($this->status_code == 300 || $this->status_code == 301 || $this->status_code == 302 || $this->status_code == 303 || $this->status_code == 307 || $this->status_code > 307 && $this->status_code < 400) && isset($this->headers['location']) && $this->redirects < $redirects)
160 $this->redirects++;
161 $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url);
162 return $this->moodle_simplepie_file($location, $timeout, $redirects, $headers);