MDL-20700 coding style cleanup - cvs keywords removed, closign php tag removed, trail...
[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
d175fe4c 65 parent::__construct(null, $cachedir);
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
73 $this->set_cache_duration(3600);
74
75 // init the feed url if passed in constructor
76 if ($feedurl !== null) {
77 $this->set_feed_url($feedurl);
78 $this->init();
79 }
80d72d56 80 }
926987ab 81
82 /**
83 * Get path for feed cache directory
84 *
85 * @return string absolute path to cache directory
86 */
87 private static function get_cache_directory() {
88 global $CFG;
89
90 return $CFG->dataroot.'/cache/simplepie/';
91 }
92
93 /**
94 * Reset RSS cache
95 *
96 * @return boolean success if cache clear or didn't exist
97 */
98 public static function reset_cache() {
99
100 $cachedir = moodle_simplepie::get_cache_directory();
101
102 return remove_dir($cachedir);
103 }
80d72d56 104}
105
106/**
107 * Moodle Customised version of the SimplePie_File class
108 *
109 * This class extends the stock SimplePie class
110 * in order to utilise Moodles own curl class for making
111 * http requests. By using the moodle curl class
112 * we ensure that the correct proxy configuration is used.
113 */
668fa0d6 114class moodle_simplepie_file extends SimplePie_File
80d72d56 115{
116
117 /**
118 * The contructor is a copy of the stock simplepie File class which has
119 * been modifed to add in use the Moodle curl class rather than php curl
120 * functions.
121 */
926987ab 122 function moodle_simplepie_file($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) {
80d72d56 123 $this->url = $url;
124 $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_CURL;
125
126 $curl = new curl();
d175fe4c 127 $curl->setopt( array(
128 'CURLOPT_HEADER' => true,
129 'CURLOPT_TIMEOUT' => $timeout,
130 'CURLOPT_CONNECTTIMEOUT' => $timeout ));
131
80d72d56 132
1597e75d 133 if ($headers !== null) {
134 // translate simplepie headers to those class curl expects
135 foreach($headers as $headername => $headervalue){
136 $headerstr = "{$headername}: {$headervalue}";
137 $curl->setHeader($headerstr);
138 }
139 }
140
926987ab 141 $this->headers = $curl->get($url);
142
143 if ($curl->error) {
80d72d56 144 $this->error = 'cURL Error: '.$curl->error;
145 $this->success = false;
146 return false;
147 }
148
926987ab 149 $parser = new SimplePie_HTTP_Parser($this->headers);
80d72d56 150
926987ab 151 if ($parser->parse()) {
80d72d56 152 $this->headers = $parser->headers;
153 $this->body = $parser->body;
154 $this->status_code = $parser->status_code;
155
156
157 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)
158 {
159 $this->redirects++;
160 $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url);
926987ab 161 return $this->moodle_simplepie_file($location, $timeout, $redirects, $headers);
80d72d56 162 }
163 }
164 }
165}