MDL-19972 reverting the previous commit
[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{
926987ab 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 */
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
926987ab 65 parent::__construct($feedurl, $cachedir);
80d72d56 66 parent::set_output_encoding('UTF-8');
67 }
926987ab 68
69 /**
70 * Get path for feed cache directory
71 *
72 * @return string absolute path to cache directory
73 */
74 private static function get_cache_directory() {
75 global $CFG;
76
77 return $CFG->dataroot.'/cache/simplepie/';
78 }
79
80 /**
81 * Reset RSS cache
82 *
83 * @return boolean success if cache clear or didn't exist
84 */
85 public static function reset_cache() {
86
87 $cachedir = moodle_simplepie::get_cache_directory();
88
89 return remove_dir($cachedir);
90 }
80d72d56 91}
92
93/**
94 * Moodle Customised version of the SimplePie_File class
95 *
96 * This class extends the stock SimplePie class
97 * in order to utilise Moodles own curl class for making
98 * http requests. By using the moodle curl class
99 * we ensure that the correct proxy configuration is used.
100 */
668fa0d6 101class moodle_simplepie_file extends SimplePie_File
80d72d56 102{
103
104 /**
105 * The contructor is a copy of the stock simplepie File class which has
106 * been modifed to add in use the Moodle curl class rather than php curl
107 * functions.
108 */
926987ab 109 function moodle_simplepie_file($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) {
80d72d56 110 $this->url = $url;
111 $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_CURL;
112
113 $curl = new curl();
114 $curl->setopt(array('CURLOPT_HEADER'=>true));
115
926987ab 116 $this->headers = $curl->get($url);
117
118 if ($curl->error) {
80d72d56 119 $this->error = 'cURL Error: '.$curl->error;
120 $this->success = false;
121 return false;
122 }
123
926987ab 124 $parser = new SimplePie_HTTP_Parser($this->headers);
80d72d56 125
926987ab 126 if ($parser->parse()) {
80d72d56 127 $this->headers = $parser->headers;
128 $this->body = $parser->body;
129 $this->status_code = $parser->status_code;
130
131
132 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)
133 {
134 $this->redirects++;
135 $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url);
926987ab 136 return $this->moodle_simplepie_file($location, $timeout, $redirects, $headers);
80d72d56 137 }
138 }
139 }
140}