]>
Commit | Line | Data |
---|---|---|
ec397236 NL |
1 | <?php |
2 | /** | |
3 | * SimplePie | |
4 | * | |
5 | * A PHP-Based RSS and Atom Feed Framework. | |
6 | * Takes the hard work out of managing a complete RSS/Atom solution. | |
7 | * | |
8 | * Copyright (c) 2004-2009, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors | |
9 | * All rights reserved. | |
10 | * | |
11 | * Redistribution and use in source and binary forms, with or without modification, are | |
12 | * permitted provided that the following conditions are met: | |
13 | * | |
14 | * * Redistributions of source code must retain the above copyright notice, this list of | |
15 | * conditions and the following disclaimer. | |
16 | * | |
17 | * * Redistributions in binary form must reproduce the above copyright notice, this list | |
18 | * of conditions and the following disclaimer in the documentation and/or other materials | |
19 | * provided with the distribution. | |
20 | * | |
21 | * * Neither the name of the SimplePie Team nor the names of its contributors may be used | |
22 | * to endorse or promote products derived from this software without specific prior | |
23 | * written permission. | |
24 | * | |
25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS | |
26 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY | |
27 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS | |
28 | * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
32 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
33 | * POSSIBILITY OF SUCH DAMAGE. | |
34 | * | |
35 | * @package SimplePie | |
36 | * @version 1.3-dev | |
37 | * @copyright 2004-2010 Ryan Parman, Geoffrey Sneddon, Ryan McCue | |
38 | * @author Ryan Parman | |
39 | * @author Geoffrey Sneddon | |
40 | * @author Ryan McCue | |
41 | * @link http://simplepie.org/ SimplePie | |
42 | * @license http://www.opensource.org/licenses/bsd-license.php BSD License | |
43 | * @todo phpDoc comments | |
44 | */ | |
45 | ||
46 | ||
47 | /** | |
48 | * @todo Move to properly supporting RFC2616 (HTTP/1.1) | |
49 | */ | |
50 | class SimplePie_File | |
51 | { | |
52 | var $url; | |
53 | var $useragent; | |
54 | var $success = true; | |
55 | var $headers = array(); | |
56 | var $body; | |
57 | var $status_code; | |
58 | var $redirects = 0; | |
59 | var $error; | |
60 | var $method = SIMPLEPIE_FILE_SOURCE_NONE; | |
61 | ||
62 | public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) | |
63 | { | |
64 | if (class_exists('idna_convert')) | |
65 | { | |
66 | $idn = new idna_convert(); | |
67 | $parsed = SimplePie_Misc::parse_url($url); | |
68 | $url = SimplePie_Misc::compress_parse_url($parsed['scheme'], $idn->encode($parsed['authority']), $parsed['path'], $parsed['query'], $parsed['fragment']); | |
69 | } | |
70 | $this->url = $url; | |
71 | $this->useragent = $useragent; | |
72 | if (preg_match('/^http(s)?:\/\//i', $url)) | |
73 | { | |
74 | if ($useragent === null) | |
75 | { | |
76 | $useragent = ini_get('user_agent'); | |
77 | $this->useragent = $useragent; | |
78 | } | |
79 | if (!is_array($headers)) | |
80 | { | |
81 | $headers = array(); | |
82 | } | |
83 | if (!$force_fsockopen && function_exists('curl_exec')) | |
84 | { | |
85 | $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_CURL; | |
86 | $fp = curl_init(); | |
87 | $headers2 = array(); | |
88 | foreach ($headers as $key => $value) | |
89 | { | |
90 | $headers2[] = "$key: $value"; | |
91 | } | |
92 | if (version_compare(SimplePie_Misc::get_curl_version(), '7.10.5', '>=')) | |
93 | { | |
94 | curl_setopt($fp, CURLOPT_ENCODING, ''); | |
95 | } | |
96 | curl_setopt($fp, CURLOPT_URL, $url); | |
97 | curl_setopt($fp, CURLOPT_HEADER, 1); | |
98 | curl_setopt($fp, CURLOPT_RETURNTRANSFER, 1); | |
99 | curl_setopt($fp, CURLOPT_TIMEOUT, $timeout); | |
100 | curl_setopt($fp, CURLOPT_CONNECTTIMEOUT, $timeout); | |
101 | curl_setopt($fp, CURLOPT_REFERER, $url); | |
102 | curl_setopt($fp, CURLOPT_USERAGENT, $useragent); | |
103 | curl_setopt($fp, CURLOPT_HTTPHEADER, $headers2); | |
104 | if (!ini_get('open_basedir') && !ini_get('safe_mode') && version_compare(SimplePie_Misc::get_curl_version(), '7.15.2', '>=')) | |
105 | { | |
106 | curl_setopt($fp, CURLOPT_FOLLOWLOCATION, 1); | |
107 | curl_setopt($fp, CURLOPT_MAXREDIRS, $redirects); | |
108 | } | |
109 | ||
110 | $this->headers = curl_exec($fp); | |
111 | if (curl_errno($fp) === 23 || curl_errno($fp) === 61) | |
112 | { | |
113 | curl_setopt($fp, CURLOPT_ENCODING, 'none'); | |
114 | $this->headers = curl_exec($fp); | |
115 | } | |
116 | if (curl_errno($fp)) | |
117 | { | |
118 | $this->error = 'cURL error ' . curl_errno($fp) . ': ' . curl_error($fp); | |
119 | $this->success = false; | |
120 | } | |
121 | else | |
122 | { | |
123 | $info = curl_getinfo($fp); | |
124 | curl_close($fp); | |
125 | $this->headers = explode("\r\n\r\n", $this->headers, $info['redirect_count'] + 1); | |
126 | $this->headers = array_pop($this->headers); | |
127 | $parser = new SimplePie_HTTP_Parser($this->headers); | |
128 | if ($parser->parse()) | |
129 | { | |
130 | $this->headers = $parser->headers; | |
131 | $this->body = $parser->body; | |
132 | $this->status_code = $parser->status_code; | |
133 | if ((in_array($this->status_code, array(300, 301, 302, 303, 307)) || $this->status_code > 307 && $this->status_code < 400) && isset($this->headers['location']) && $this->redirects < $redirects) | |
134 | { | |
135 | $this->redirects++; | |
136 | $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url); | |
137 | return $this->__construct($location, $timeout, $redirects, $headers, $useragent, $force_fsockopen); | |
138 | } | |
139 | } | |
140 | } | |
141 | } | |
142 | else | |
143 | { | |
144 | $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_FSOCKOPEN; | |
145 | $url_parts = parse_url($url); | |
146 | $socket_host = $url_parts['host']; | |
147 | if (isset($url_parts['scheme']) && strtolower($url_parts['scheme']) === 'https') | |
148 | { | |
149 | $socket_host = "ssl://$url_parts[host]"; | |
150 | $url_parts['port'] = 443; | |
151 | } | |
152 | if (!isset($url_parts['port'])) | |
153 | { | |
154 | $url_parts['port'] = 80; | |
155 | } | |
156 | $fp = @fsockopen($socket_host, $url_parts['port'], $errno, $errstr, $timeout); | |
157 | if (!$fp) | |
158 | { | |
159 | $this->error = 'fsockopen error: ' . $errstr; | |
160 | $this->success = false; | |
161 | } | |
162 | else | |
163 | { | |
164 | stream_set_timeout($fp, $timeout); | |
165 | if (isset($url_parts['path'])) | |
166 | { | |
167 | if (isset($url_parts['query'])) | |
168 | { | |
169 | $get = "$url_parts[path]?$url_parts[query]"; | |
170 | } | |
171 | else | |
172 | { | |
173 | $get = $url_parts['path']; | |
174 | } | |
175 | } | |
176 | else | |
177 | { | |
178 | $get = '/'; | |
179 | } | |
180 | $out = "GET $get HTTP/1.1\r\n"; | |
181 | $out .= "Host: $url_parts[host]\r\n"; | |
182 | $out .= "User-Agent: $useragent\r\n"; | |
183 | if (extension_loaded('zlib')) | |
184 | { | |
185 | $out .= "Accept-Encoding: x-gzip,gzip,deflate\r\n"; | |
186 | } | |
187 | ||
188 | if (isset($url_parts['user']) && isset($url_parts['pass'])) | |
189 | { | |
190 | $out .= "Authorization: Basic " . base64_encode("$url_parts[user]:$url_parts[pass]") . "\r\n"; | |
191 | } | |
192 | foreach ($headers as $key => $value) | |
193 | { | |
194 | $out .= "$key: $value\r\n"; | |
195 | } | |
196 | $out .= "Connection: Close\r\n\r\n"; | |
197 | fwrite($fp, $out); | |
198 | ||
199 | $info = stream_get_meta_data($fp); | |
200 | ||
201 | $this->headers = ''; | |
202 | while (!$info['eof'] && !$info['timed_out']) | |
203 | { | |
204 | $this->headers .= fread($fp, 1160); | |
205 | $info = stream_get_meta_data($fp); | |
206 | } | |
207 | if (!$info['timed_out']) | |
208 | { | |
209 | $parser = new SimplePie_HTTP_Parser($this->headers); | |
210 | if ($parser->parse()) | |
211 | { | |
212 | $this->headers = $parser->headers; | |
213 | $this->body = $parser->body; | |
214 | $this->status_code = $parser->status_code; | |
215 | if ((in_array($this->status_code, array(300, 301, 302, 303, 307)) || $this->status_code > 307 && $this->status_code < 400) && isset($this->headers['location']) && $this->redirects < $redirects) | |
216 | { | |
217 | $this->redirects++; | |
218 | $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url); | |
219 | return $this->__construct($location, $timeout, $redirects, $headers, $useragent, $force_fsockopen); | |
220 | } | |
221 | if (isset($this->headers['content-encoding'])) | |
222 | { | |
223 | // Hey, we act dumb elsewhere, so let's do that here too | |
224 | switch (strtolower(trim($this->headers['content-encoding'], "\x09\x0A\x0D\x20"))) | |
225 | { | |
226 | case 'gzip': | |
227 | case 'x-gzip': | |
228 | $decoder = new SimplePie_gzdecode($this->body); | |
229 | if (!$decoder->parse()) | |
230 | { | |
231 | $this->error = 'Unable to decode HTTP "gzip" stream'; | |
232 | $this->success = false; | |
233 | } | |
234 | else | |
235 | { | |
236 | $this->body = $decoder->data; | |
237 | } | |
238 | break; | |
239 | ||
240 | case 'deflate': | |
241 | if (($body = gzuncompress($this->body)) === false) | |
242 | { | |
243 | if (($body = gzinflate($this->body)) === false) | |
244 | { | |
245 | $this->error = 'Unable to decode HTTP "deflate" stream'; | |
246 | $this->success = false; | |
247 | } | |
248 | } | |
249 | $this->body = $body; | |
250 | break; | |
251 | ||
252 | default: | |
253 | $this->error = 'Unknown content coding'; | |
254 | $this->success = false; | |
255 | } | |
256 | } | |
257 | } | |
258 | } | |
259 | else | |
260 | { | |
261 | $this->error = 'fsocket timed out'; | |
262 | $this->success = false; | |
263 | } | |
264 | fclose($fp); | |
265 | } | |
266 | } | |
267 | } | |
268 | else | |
269 | { | |
270 | $this->method = SIMPLEPIE_FILE_SOURCE_LOCAL | SIMPLEPIE_FILE_SOURCE_FILE_GET_CONTENTS; | |
271 | if (!$this->body = file_get_contents($url)) | |
272 | { | |
273 | $this->error = 'file_get_contents could not read the file'; | |
274 | $this->success = false; | |
275 | } | |
276 | } | |
277 | } | |
278 | } |