1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43:
44:
45: 46: 47: 48: 49: 50: 51: 52: 53: 54:
55: class SimplePie_File
56: {
57: var $url;
58: var $useragent;
59: var $success = true;
60: var $headers = array();
61: var $body;
62: var $status_code;
63: var $redirects = 0;
64: var $error;
65: var $method = SIMPLEPIE_FILE_SOURCE_NONE;
66:
67: public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false)
68: {
69: if (class_exists('idna_convert'))
70: {
71: $idn = new idna_convert();
72: $parsed = SimplePie_Misc::parse_url($url);
73: $url = SimplePie_Misc::compress_parse_url($parsed['scheme'], $idn->encode($parsed['authority']), $parsed['path'], $parsed['query'], $parsed['fragment']);
74: }
75: $this->url = $url;
76: $this->useragent = $useragent;
77: if (preg_match('/^http(s)?:\/\//i', $url))
78: {
79: if ($useragent === null)
80: {
81: $useragent = ini_get('user_agent');
82: $this->useragent = $useragent;
83: }
84: if (!is_array($headers))
85: {
86: $headers = array();
87: }
88: if (!$force_fsockopen && function_exists('curl_exec'))
89: {
90: $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_CURL;
91: $fp = curl_init();
92: $headers2 = array();
93: foreach ($headers as $key => $value)
94: {
95: $headers2[] = "$key: $value";
96: }
97: if (version_compare(SimplePie_Misc::get_curl_version(), '7.10.5', '>='))
98: {
99: curl_setopt($fp, CURLOPT_ENCODING, '');
100: }
101: curl_setopt($fp, CURLOPT_URL, $url);
102: curl_setopt($fp, CURLOPT_HEADER, 1);
103: curl_setopt($fp, CURLOPT_RETURNTRANSFER, 1);
104: curl_setopt($fp, CURLOPT_TIMEOUT, $timeout);
105: curl_setopt($fp, CURLOPT_CONNECTTIMEOUT, $timeout);
106: curl_setopt($fp, CURLOPT_REFERER, $url);
107: curl_setopt($fp, CURLOPT_USERAGENT, $useragent);
108: curl_setopt($fp, CURLOPT_HTTPHEADER, $headers2);
109: if (!ini_get('open_basedir') && !ini_get('safe_mode') && version_compare(SimplePie_Misc::get_curl_version(), '7.15.2', '>='))
110: {
111: curl_setopt($fp, CURLOPT_FOLLOWLOCATION, 1);
112: curl_setopt($fp, CURLOPT_MAXREDIRS, $redirects);
113: }
114:
115: $this->headers = curl_exec($fp);
116: if (curl_errno($fp) === 23 || curl_errno($fp) === 61)
117: {
118: curl_setopt($fp, CURLOPT_ENCODING, 'none');
119: $this->headers = curl_exec($fp);
120: }
121: if (curl_errno($fp))
122: {
123: $this->error = 'cURL error ' . curl_errno($fp) . ': ' . curl_error($fp);
124: $this->success = false;
125: }
126: else
127: {
128: $info = curl_getinfo($fp);
129: curl_close($fp);
130: $this->headers = explode("\r\n\r\n", $this->headers, $info['redirect_count'] + 1);
131: $this->headers = array_pop($this->headers);
132: $parser = new SimplePie_HTTP_Parser($this->headers);
133: if ($parser->parse())
134: {
135: $this->headers = $parser->headers;
136: $this->body = $parser->body;
137: $this->status_code = $parser->status_code;
138: 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)
139: {
140: $this->redirects++;
141: $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url);
142: return $this->__construct($location, $timeout, $redirects, $headers, $useragent, $force_fsockopen);
143: }
144: }
145: }
146: }
147: else
148: {
149: $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_FSOCKOPEN;
150: $url_parts = parse_url($url);
151: $socket_host = $url_parts['host'];
152: if (isset($url_parts['scheme']) && strtolower($url_parts['scheme']) === 'https')
153: {
154: $socket_host = "ssl://$url_parts[host]";
155: $url_parts['port'] = 443;
156: }
157: if (!isset($url_parts['port']))
158: {
159: $url_parts['port'] = 80;
160: }
161: $fp = @fsockopen($socket_host, $url_parts['port'], $errno, $errstr, $timeout);
162: if (!$fp)
163: {
164: $this->error = 'fsockopen error: ' . $errstr;
165: $this->success = false;
166: }
167: else
168: {
169: stream_set_timeout($fp, $timeout);
170: if (isset($url_parts['path']))
171: {
172: if (isset($url_parts['query']))
173: {
174: $get = "$url_parts[path]?$url_parts[query]";
175: }
176: else
177: {
178: $get = $url_parts['path'];
179: }
180: }
181: else
182: {
183: $get = '/';
184: }
185: $out = "GET $get HTTP/1.1\r\n";
186: $out .= "Host: $url_parts[host]\r\n";
187: $out .= "User-Agent: $useragent\r\n";
188: if (extension_loaded('zlib'))
189: {
190: $out .= "Accept-Encoding: x-gzip,gzip,deflate\r\n";
191: }
192:
193: if (isset($url_parts['user']) && isset($url_parts['pass']))
194: {
195: $out .= "Authorization: Basic " . base64_encode("$url_parts[user]:$url_parts[pass]") . "\r\n";
196: }
197: foreach ($headers as $key => $value)
198: {
199: $out .= "$key: $value\r\n";
200: }
201: $out .= "Connection: Close\r\n\r\n";
202: fwrite($fp, $out);
203:
204: $info = stream_get_meta_data($fp);
205:
206: $this->headers = '';
207: while (!$info['eof'] && !$info['timed_out'])
208: {
209: $this->headers .= fread($fp, 1160);
210: $info = stream_get_meta_data($fp);
211: }
212: if (!$info['timed_out'])
213: {
214: $parser = new SimplePie_HTTP_Parser($this->headers);
215: if ($parser->parse())
216: {
217: $this->headers = $parser->headers;
218: $this->body = $parser->body;
219: $this->status_code = $parser->status_code;
220: 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)
221: {
222: $this->redirects++;
223: $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url);
224: return $this->__construct($location, $timeout, $redirects, $headers, $useragent, $force_fsockopen);
225: }
226: if (isset($this->headers['content-encoding']))
227: {
228:
229: switch (strtolower(trim($this->headers['content-encoding'], "\x09\x0A\x0D\x20")))
230: {
231: case 'gzip':
232: case 'x-gzip':
233: $decoder = new SimplePie_gzdecode($this->body);
234: if (!$decoder->parse())
235: {
236: $this->error = 'Unable to decode HTTP "gzip" stream';
237: $this->success = false;
238: }
239: else
240: {
241: $this->body = $decoder->data;
242: }
243: break;
244:
245: case 'deflate':
246: if (($body = gzuncompress($this->body)) === false)
247: {
248: if (($body = gzinflate($this->body)) === false)
249: {
250: $this->error = 'Unable to decode HTTP "deflate" stream';
251: $this->success = false;
252: }
253: }
254: $this->body = $body;
255: break;
256:
257: default:
258: $this->error = 'Unknown content coding';
259: $this->success = false;
260: }
261: }
262: }
263: }
264: else
265: {
266: $this->error = 'fsocket timed out';
267: $this->success = false;
268: }
269: fclose($fp);
270: }
271: }
272: }
273: else
274: {
275: $this->method = SIMPLEPIE_FILE_SOURCE_LOCAL | SIMPLEPIE_FILE_SOURCE_FILE_GET_CONTENTS;
276: if (!$this->body = file_get_contents($url))
277: {
278: $this->error = 'file_get_contents could not read the file';
279: $this->success = false;
280: }
281: }
282: }
283: }
284: