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