|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00029 require_once($CFG->libdir.'/filelib.php'); 00030 00031 // PLEASE NOTE: we use the simplepie class _unmodified_ 00032 // through the joys of OO. Distros are free to use their stock 00033 // version of this file. 00034 require_once($CFG->libdir.'/simplepie/simplepie.class.php'); 00035 00049 class moodle_simplepie extends SimplePie 00050 { 00057 function __construct($feedurl = null) { 00058 00059 // Use the Moodle class for http requests 00060 $this->file_class = 'moodle_simplepie_file'; 00061 00062 $cachedir = moodle_simplepie::get_cache_directory(); 00063 check_dir_exists($cachedir); 00064 00065 parent::__construct(); 00066 // Match moodle encoding 00067 $this->set_output_encoding('UTF-8'); 00068 00069 // default to a short timeout as most operations will be interactive 00070 $this->set_timeout(2); 00071 00072 // 1 hour default cache 00073 $this->set_cache_location($cachedir); 00074 $this->set_cache_duration(3600); 00075 00076 // init the feed url if passed in constructor 00077 if ($feedurl !== null) { 00078 $this->set_feed_url($feedurl); 00079 $this->init(); 00080 } 00081 } 00082 00088 private static function get_cache_directory() { 00089 global $CFG; 00090 00091 return $CFG->cachedir.'/simplepie/'; 00092 } 00093 00099 public static function reset_cache() { 00100 00101 $cachedir = moodle_simplepie::get_cache_directory(); 00102 00103 return remove_dir($cachedir); 00104 } 00105 } 00106 00115 class moodle_simplepie_file extends SimplePie_File 00116 { 00117 00123 function moodle_simplepie_file($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) { 00124 $this->url = $url; 00125 $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_CURL; 00126 00127 $curl = new curl(); 00128 $curl->setopt( array( 00129 'CURLOPT_HEADER' => true, 00130 'CURLOPT_TIMEOUT' => $timeout, 00131 'CURLOPT_CONNECTTIMEOUT' => $timeout )); 00132 00133 00134 if ($headers !== null) { 00135 // translate simplepie headers to those class curl expects 00136 foreach($headers as $headername => $headervalue){ 00137 $headerstr = "{$headername}: {$headervalue}"; 00138 $curl->setHeader($headerstr); 00139 } 00140 } 00141 00142 $this->headers = $curl->get($url); 00143 00144 if ($curl->error) { 00145 $this->error = 'cURL Error: '.$curl->error; 00146 $this->success = false; 00147 return false; 00148 } 00149 00150 $parser = new SimplePie_HTTP_Parser($this->headers); 00151 00152 if ($parser->parse()) { 00153 $this->headers = $parser->headers; 00154 $this->body = $parser->body; 00155 $this->status_code = $parser->status_code; 00156 00157 00158 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) 00159 { 00160 $this->redirects++; 00161 $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url); 00162 return $this->moodle_simplepie_file($location, $timeout, $redirects, $headers); 00163 } 00164 } 00165 } 00166 }