Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 
1441 ariadna 29
use SimplePie\SimplePie;
30
use SimplePie\File;
31
use SimplePie\Sanitize;
32
use SimplePie\HTTP\Parser;
33
use SimplePie\Misc;
34
 
1 efrain 35
require_once($CFG->libdir.'/filelib.php');
36
 
37
/**
38
 * Moodle Customised version of the SimplePie class
39
 *
40
 * This class extends the stock SimplePie class
41
 * in order to make sensible configuration choices,
42
 * such as using the Moodle cache directory and
43
 * curl functions/proxy config for making http
44
 * requests in line with moodle configuration.
45
 *
46
 * @copyright 2009 Dan Poltawski <talktodan@gmail.com>
47
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48
 * @since     Moodle 2.0
49
 */
1441 ariadna 50
class moodle_simplepie extends SimplePie {
1 efrain 51
    /**
52
     * Constructor - creates an instance of the SimplePie class
53
     * with Moodle defaults.
54
     *
55
     * @param string $feedurl optional URL of the feed
56
     * @param int $timeout how many seconds requests should wait for server response
57
     */
58
    public function __construct($feedurl = null, $timeout = 2) {
1441 ariadna 59
        $cachedir = self::get_cache_directory();
1 efrain 60
        check_dir_exists($cachedir);
61
 
62
        parent::__construct();
63
 
64
        // Use the Moodle class for http requests
65
        $registry = $this->get_registry();
1441 ariadna 66
        $registry->register(File::class, 'moodle_simplepie_file', true);
1 efrain 67
 
68
        // Use html purifier for text cleaning.
1441 ariadna 69
        $registry->register(Sanitize::class, 'moodle_simplepie_sanitize', true);
1 efrain 70
        $this->sanitize = new moodle_simplepie_sanitize();
71
 
72
        // Match moodle encoding
73
        $this->set_output_encoding('UTF-8');
74
 
75
        // default to a short timeout as most operations will be interactive
76
        $this->set_timeout($timeout);
77
 
78
        // 1 hour default cache
79
        $this->set_cache_location($cachedir);
80
        $this->set_cache_duration(3600);
81
 
82
        // init the feed url if passed in constructor
83
        if ($feedurl !== null) {
84
            $this->set_feed_url($feedurl);
85
            $this->init();
86
        }
87
    }
88
 
89
    /**
90
     * Get path for feed cache directory
91
     *
92
     * @return string absolute path to cache directory
93
     */
94
    private static function get_cache_directory() {
95
        global $CFG;
96
 
97
        return $CFG->cachedir.'/simplepie/';
98
    }
99
 
100
    /**
101
     * Reset RSS cache
102
     *
103
     * @return boolean success if cache clear or didn't exist
104
     */
105
    public static function reset_cache() {
106
 
1441 ariadna 107
        $cachedir = self::get_cache_directory();
1 efrain 108
 
109
        return remove_dir($cachedir);
110
    }
111
}
112
 
113
/**
114
 * Moodle Customised version of the SimplePie_File class
115
 *
116
 * This class extends the stock SimplePie class
117
 * in order to utilise Moodles own curl class for making
118
 * http requests. By using the moodle curl class
119
 * we ensure that the correct proxy configuration is used.
120
 */
1441 ariadna 121
class moodle_simplepie_file extends File {
1 efrain 122
 
123
    /**
124
     * The constructor is a copy of the stock simplepie File class which has
125
     * been modified to add in use the Moodle curl class rather than php curl
126
     * functions.
127
     */
128
    public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) {
129
        $this->url = $url;
1441 ariadna 130
        $this->method = SimplePie::FILE_SOURCE_REMOTE | SimplePie::FILE_SOURCE_CURL;
1 efrain 131
 
132
        $curl = new curl();
133
        $curl->setopt( array(
134
                'CURLOPT_HEADER' => true,
135
                'CURLOPT_TIMEOUT' => $timeout,
136
                'CURLOPT_CONNECTTIMEOUT' => $timeout ));
137
 
138
        if ($headers !== null) {
139
            // translate simplepie headers to those class curl expects
1441 ariadna 140
            foreach ($headers as $headername => $headervalue) {
1 efrain 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
 
1441 ariadna 154
        $parser = new Parser($this->headers);
1 efrain 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++;
1441 ariadna 166
                $location = Misc::absolutize_url($this->headers['location'], $url);
1 efrain 167
                return $this->__construct($location, $timeout, $redirects, $headers);
168
            }
169
        }
170
    }
171
}
172
 
173
 
174
/**
175
 * Customised feed sanitization using HTMLPurifier.
176
 */
1441 ariadna 177
class moodle_simplepie_sanitize extends \SimplePie\Sanitize {
1 efrain 178
    public function sanitize($data, $type, $base = '') {
179
        $data = trim($data);
180
 
181
        if ($data === '') {
182
            return '';
183
        }
184
 
1441 ariadna 185
        if ($type & SimplePie::CONSTRUCT_BASE64) {
1 efrain 186
            $data = base64_decode($data);
187
        }
188
 
1441 ariadna 189
        if ($type & SimplePie::CONSTRUCT_MAYBE_HTML) {
1 efrain 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]*'
1441 ariadna 191
                    . SimplePie::PCRE_HTML_ATTRIBUTE . '>)/', $data)) {
192
                $type |= SimplePie::CONSTRUCT_HTML;
1 efrain 193
            } else {
1441 ariadna 194
                $type |= SimplePie::CONSTRUCT_TEXT;
1 efrain 195
            }
196
        }
197
 
1441 ariadna 198
        if ($type & SimplePie::CONSTRUCT_IRI) {
1 efrain 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
 
1441 ariadna 206
        if ($type & (SimplePie::CONSTRUCT_TEXT | SimplePie::CONSTRUCT_IRI)) {
1 efrain 207
            $data = htmlspecialchars($data, ENT_COMPAT, 'UTF-8');
208
        }
209
 
210
        $data = purify_html($data);
211
 
212
        if ($this->remove_div) {
1441 ariadna 213
            $data = preg_replace('/^<div' . SimplePie::PCRE_XML_ATTRIBUTE . '>/', '', $data);
1 efrain 214
            $data = preg_replace('/<\/div>$/', '', $data);
215
        } else {
1441 ariadna 216
            $data = preg_replace('/^<div' . SimplePie::PCRE_XML_ATTRIBUTE . '>/', '<div>', $data);
1 efrain 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
}