Adding SimplePie feed parsing library MDL-7946, MDL-13932
[moodle.git] / lib / simplepie / moodle_simplepie.php
CommitLineData
80d72d56 1<?php // $id$
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
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
42 * curl functions/proxy config for making http
43 * requests.
44 */
45class Moodle_SimplePie extends SimplePie
46{
47 function __construct($feed_url = null){
48 global $CFG;
49
50 // Use the Moodle class for http requests
51 $this->file_class = 'Moodle_SimplePie_File';
52
53 // Use sensible cache directory
54 $cachedir = $CFG->dataroot.'/cache/simplepie/';
55 if (!file_exists($cachedir)){
56 mkdir($cachedir, 0777, true);
57 }
58
59 parent::__construct($feed_url, $cachedir);
60 parent::set_output_encoding('UTF-8');
61 }
62}
63
64/**
65 * Moodle Customised version of the SimplePie_File class
66 *
67 * This class extends the stock SimplePie class
68 * in order to utilise Moodles own curl class for making
69 * http requests. By using the moodle curl class
70 * we ensure that the correct proxy configuration is used.
71 */
72class Moodle_SimplePie_File extends SimplePie_File
73{
74
75 /**
76 * The contructor is a copy of the stock simplepie File class which has
77 * been modifed to add in use the Moodle curl class rather than php curl
78 * functions.
79 */
80 function Moodle_SimplePie_File($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false)
81 {
82 $this->url = $url;
83 $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_CURL;
84
85 $curl = new curl();
86 $curl->setopt(array('CURLOPT_HEADER'=>true));
87
88 try{
89 $this->headers = $curl->get($url);
90 }catch(moodle_exception $e){
91 $this->error = 'cURL Error: '.$curl->error;
92 $this->success = false;
93 return false;
94 }
95
96 $parser =& new SimplePie_HTTP_Parser($this->headers);
97
98 if ($parser->parse()){
99 $this->headers = $parser->headers;
100 $this->body = $parser->body;
101 $this->status_code = $parser->status_code;
102
103
104 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)
105 {
106 $this->redirects++;
107 $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url);
108 return $this->SimplePie_File($location, $timeout, $redirects, $headers);
109 }
110 }
111 }
112}