MDL-23979 fixed non standard entities result
[moodle.git] / lib / simplepie / moodle_simplepie.php
CommitLineData
ec00bfc5 1<?php
80d72d56 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>
926987ab 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
80d72d56 25 *
26 * Customised version of SimplePie for Moodle
27 */
28
29require_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.
34require_once($CFG->libdir.'/simplepie/simplepie.inc');
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
926987ab 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
80d72d56 48 */
668fa0d6 49class moodle_simplepie extends SimplePie
80d72d56 50{
6f5e0852 51 /**
926987ab 52 * Constructor - creates an instance of the SimplePie class
53 * with Moodle defaults.
54 *
6f5e0852 55 * @param string $feedurl optional URL of the feed
926987ab 56 */
57 function __construct($feedurl = null) {
80d72d56 58
59 // Use the Moodle class for http requests
668fa0d6 60 $this->file_class = 'moodle_simplepie_file';
80d72d56 61
926987ab 62 $cachedir = moodle_simplepie::get_cache_directory();
63 check_dir_exists($cachedir, true, true);
80d72d56 64
3455494c 65 parent::__construct();
d175fe4c 66 // Match moodle encoding
67 $this->set_output_encoding('UTF-8');
68
69 // default to a short timeout as most operations will be interactive
70 $this->set_timeout(2);
71
72 // 1 hour default cache
3455494c 73 $this->set_cache_location($cachedir);
d175fe4c 74 $this->set_cache_duration(3600);
75
76 // init the feed url if passed in constructor
77 if ($feedurl !== null) {
3455494c
MD
78 $this->set_feed_url($feedurl);
79 $this->init();
d175fe4c 80 }
80d72d56 81 }
926987ab 82
83 /**
84 * Get path for feed cache directory
85 *
86 * @return string absolute path to cache directory
87 */
88 private static function get_cache_directory() {
89 global $CFG;
90
91 return $CFG->dataroot.'/cache/simplepie/';
92 }
93
94 /**
6f5e0852 95 * Reset RSS cache
926987ab 96 *
97 * @return boolean success if cache clear or didn't exist
98 */
99 public static function reset_cache() {
100
101 $cachedir = moodle_simplepie::get_cache_directory();
102
103 return remove_dir($cachedir);
104 }
80d72d56 105}
106
107/**
108 * Moodle Customised version of the SimplePie_File class
109 *
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.
114 */
668fa0d6 115class moodle_simplepie_file extends SimplePie_File
80d72d56 116{
117
118 /**
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.
122 */
926987ab 123 function moodle_simplepie_file($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) {
80d72d56 124 $this->url = $url;
125 $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_CURL;
126
127 $curl = new curl();
6f5e0852 128 $curl->setopt( array(
d175fe4c 129 'CURLOPT_HEADER' => true,
130 'CURLOPT_TIMEOUT' => $timeout,
131 'CURLOPT_CONNECTTIMEOUT' => $timeout ));
132
80d72d56 133
1597e75d 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);
139 }
140 }
141
926987ab 142 $this->headers = $curl->get($url);
143
144 if ($curl->error) {
80d72d56 145 $this->error = 'cURL Error: '.$curl->error;
146 $this->success = false;
147 return false;
148 }
149
926987ab 150 $parser = new SimplePie_HTTP_Parser($this->headers);
80d72d56 151
926987ab 152 if ($parser->parse()) {
80d72d56 153 $this->headers = $parser->headers;
154 $this->body = $parser->body;
155 $this->status_code = $parser->status_code;
156
157
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)
159 {
160 $this->redirects++;
161 $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url);
926987ab 162 return $this->moodle_simplepie_file($location, $timeout, $redirects, $headers);
80d72d56 163 }
164 }
165 }
166}