Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 24... Línea 24...
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 *
25
 *
26
 * Customised version of SimplePie for Moodle
26
 * Customised version of SimplePie for Moodle
27
 */
27
 */
Línea 28... Línea 28...
28
 
28
 
-
 
29
use SimplePie\SimplePie;
-
 
30
use SimplePie\File;
-
 
31
use SimplePie\Sanitize;
-
 
32
use SimplePie\HTTP\Parser;
Línea 29... Línea -...
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
use SimplePie\Misc;
Línea 33... Línea 34...
33
// version of this file.
34
 
34
require_once($CFG->libdir.'/simplepie/autoloader.php');
35
require_once($CFG->libdir.'/filelib.php');
35
 
36
 
36
/**
37
/**
Línea 44... Línea 45...
44
 *
45
 *
45
 * @copyright 2009 Dan Poltawski <talktodan@gmail.com>
46
 * @copyright 2009 Dan Poltawski <talktodan@gmail.com>
46
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47
 * @since     Moodle 2.0
48
 * @since     Moodle 2.0
48
 */
49
 */
49
class moodle_simplepie extends SimplePie\SimplePie {
50
class moodle_simplepie extends SimplePie {
50
    /**
51
    /**
51
     * Constructor - creates an instance of the SimplePie class
52
     * Constructor - creates an instance of the SimplePie class
52
     * with Moodle defaults.
53
     * with Moodle defaults.
53
     *
54
     *
54
     * @param string $feedurl optional URL of the feed
55
     * @param string $feedurl optional URL of the feed
55
     * @param int $timeout how many seconds requests should wait for server response
56
     * @param int $timeout how many seconds requests should wait for server response
56
     */
57
     */
57
    public function __construct($feedurl = null, $timeout = 2) {
58
    public function __construct($feedurl = null, $timeout = 2) {
58
        $cachedir = moodle_simplepie::get_cache_directory();
59
        $cachedir = self::get_cache_directory();
59
        check_dir_exists($cachedir);
60
        check_dir_exists($cachedir);
Línea 60... Línea 61...
60
 
61
 
Línea 61... Línea 62...
61
        parent::__construct();
62
        parent::__construct();
62
 
63
 
63
        // Use the Moodle class for http requests
64
        // Use the Moodle class for http requests
Línea 64... Línea 65...
64
        $registry = $this->get_registry();
65
        $registry = $this->get_registry();
65
        $registry->register(SimplePie\File::class, 'moodle_simplepie_file', true);
66
        $registry->register(File::class, 'moodle_simplepie_file', true);
66
 
67
 
Línea 67... Línea 68...
67
        // Use html purifier for text cleaning.
68
        // Use html purifier for text cleaning.
68
        $registry->register(SimplePie\Sanitize::class, 'moodle_simplepie_sanitize', true);
69
        $registry->register(Sanitize::class, 'moodle_simplepie_sanitize', true);
Línea 101... Línea 102...
101
     *
102
     *
102
     * @return boolean success if cache clear or didn't exist
103
     * @return boolean success if cache clear or didn't exist
103
     */
104
     */
104
    public static function reset_cache() {
105
    public static function reset_cache() {
Línea 105... Línea 106...
105
 
106
 
Línea 106... Línea 107...
106
        $cachedir = moodle_simplepie::get_cache_directory();
107
        $cachedir = self::get_cache_directory();
107
 
108
 
108
        return remove_dir($cachedir);
109
        return remove_dir($cachedir);
Línea 115... Línea 116...
115
 * This class extends the stock SimplePie class
116
 * This class extends the stock SimplePie class
116
 * in order to utilise Moodles own curl class for making
117
 * in order to utilise Moodles own curl class for making
117
 * http requests. By using the moodle curl class
118
 * http requests. By using the moodle curl class
118
 * we ensure that the correct proxy configuration is used.
119
 * we ensure that the correct proxy configuration is used.
119
 */
120
 */
120
class moodle_simplepie_file extends SimplePie\File {
121
class moodle_simplepie_file extends File {
Línea 121... Línea 122...
121
 
122
 
122
    /**
123
    /**
123
     * The constructor is a copy of the stock simplepie File class which has
124
     * 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
     * been modified to add in use the Moodle curl class rather than php curl
125
     * functions.
126
     * functions.
126
     */
127
     */
127
    public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) {
128
    public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) {
128
        $this->url = $url;
129
        $this->url = $url;
Línea 129... Línea 130...
129
        $this->method = SimplePie\SimplePie::FILE_SOURCE_REMOTE | SimplePie\SimplePie::FILE_SOURCE_CURL;
130
        $this->method = SimplePie::FILE_SOURCE_REMOTE | SimplePie::FILE_SOURCE_CURL;
130
 
131
 
131
        $curl = new curl();
132
        $curl = new curl();
132
        $curl->setopt( array(
133
        $curl->setopt( array(
133
                'CURLOPT_HEADER' => true,
134
                'CURLOPT_HEADER' => true,
Línea 134... Línea -...
134
                'CURLOPT_TIMEOUT' => $timeout,
-
 
135
                'CURLOPT_CONNECTTIMEOUT' => $timeout ));
135
                'CURLOPT_TIMEOUT' => $timeout,
136
 
136
                'CURLOPT_CONNECTTIMEOUT' => $timeout ));
137
 
137
 
138
        if ($headers !== null) {
138
        if ($headers !== null) {
139
            // translate simplepie headers to those class curl expects
139
            // translate simplepie headers to those class curl expects
140
            foreach($headers as $headername => $headervalue){
140
            foreach ($headers as $headername => $headervalue) {
141
                $headerstr = "{$headername}: {$headervalue}";
141
                $headerstr = "{$headername}: {$headervalue}";
Línea 149... Línea 149...
149
            $this->error = 'cURL Error: '.$curl->error;
149
            $this->error = 'cURL Error: '.$curl->error;
150
            $this->success = false;
150
            $this->success = false;
151
            return false;
151
            return false;
152
        }
152
        }
Línea 153... Línea 153...
153
 
153
 
Línea 154... Línea 154...
154
        $parser = new SimplePie\HTTP\Parser($this->headers);
154
        $parser = new Parser($this->headers);
155
 
155
 
156
        if ($parser->parse()) {
156
        if ($parser->parse()) {
157
            $this->headers = $parser->headers;
157
            $this->headers = $parser->headers;
Línea 161... Línea 161...
161
 
161
 
162
            if (($this->status_code == 300 || $this->status_code == 301 || $this->status_code == 302 || $this->status_code == 303
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)
163
                    || $this->status_code == 307 || $this->status_code > 307 && $this->status_code < 400)
164
                    && isset($this->headers['location']) && $this->redirects < $redirects) {
164
                    && isset($this->headers['location']) && $this->redirects < $redirects) {
165
                $this->redirects++;
165
                $this->redirects++;
166
                $location = SimplePie\Misc::absolutize_url($this->headers['location'], $url);
166
                $location = Misc::absolutize_url($this->headers['location'], $url);
167
                return $this->__construct($location, $timeout, $redirects, $headers);
167
                return $this->__construct($location, $timeout, $redirects, $headers);
168
            }
168
            }
169
        }
169
        }
170
    }
170
    }
Línea 171... Línea 171...
171
}
171
}
172
 
172
 
173
 
173
 
174
/**
174
/**
175
 * Customised feed sanitization using HTMLPurifier.
175
 * Customised feed sanitization using HTMLPurifier.
176
 */
176
 */
Línea 177... Línea 177...
177
class moodle_simplepie_sanitize extends SimplePie\Sanitize {
177
class moodle_simplepie_sanitize extends \SimplePie\Sanitize {
178
    public function sanitize($data, $type, $base = '') {
178
    public function sanitize($data, $type, $base = '') {
179
        $data = trim($data);
179
        $data = trim($data);
Línea 180... Línea 180...
180
 
180
 
181
        if ($data === '') {
181
        if ($data === '') {
182
            return '';
182
            return '';
Línea 183... Línea 183...
183
        }
183
        }
184
 
184
 
185
        if ($type & SimplePie\SimplePie::CONSTRUCT_BASE64){
185
        if ($type & SimplePie::CONSTRUCT_BASE64) {
186
            $data = base64_decode($data);
186
            $data = base64_decode($data);
187
        }
187
        }
188
 
188
 
189
        if ($type & SimplePie\SimplePie::CONSTRUCT_MAYBE_HTML) {
189
        if ($type & 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]*'
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]*'
Línea 191... Línea 191...
191
                    . SimplePie\SimplePie::PCRE_HTML_ATTRIBUTE . '>)/', $data)) {
191
                    . SimplePie::PCRE_HTML_ATTRIBUTE . '>)/', $data)) {
192
                $type |= SimplePie\SimplePie::CONSTRUCT_HTML;
192
                $type |= SimplePie::CONSTRUCT_HTML;
193
            } else {
193
            } else {
194
                $type |= SimplePie\SimplePie::CONSTRUCT_TEXT;
194
                $type |= SimplePie::CONSTRUCT_TEXT;
195
            }
195
            }
196
        }
196
        }
197
 
197
 
Línea 198... Línea 198...
198
        if ($type & SimplePie\SimplePie::CONSTRUCT_IRI) {
198
        if ($type & SimplePie::CONSTRUCT_IRI) {
199
            $absolute = $this->registry->call('Misc', 'absolutize_url', array($data, $base));
199
            $absolute = $this->registry->call('Misc', 'absolutize_url', array($data, $base));
200
            if ($absolute !== false) {
200
            if ($absolute !== false) {
Línea 201... Línea 201...
201
                $data = $absolute;
201
                $data = $absolute;
Línea 202... Línea 202...
202
            }
202
            }
203
            $data = clean_param($data, PARAM_URL);
203
            $data = clean_param($data, PARAM_URL);
204
        }
204
        }
205
 
205
 
206
        if ($type & (SimplePie\SimplePie::CONSTRUCT_TEXT | SimplePie\SimplePie::CONSTRUCT_IRI)) {
206
        if ($type & (SimplePie::CONSTRUCT_TEXT | SimplePie::CONSTRUCT_IRI)) {
207
            $data = htmlspecialchars($data, ENT_COMPAT, 'UTF-8');
207
            $data = htmlspecialchars($data, ENT_COMPAT, 'UTF-8');
Línea 208... Línea 208...
208
        }
208
        }
209
 
209
 
210
        $data = purify_html($data);
210
        $data = purify_html($data);