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