4
0

File.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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-2016, 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. * @copyright 2004-2016 Ryan Parman, Geoffrey Sneddon, Ryan McCue
  37. * @author Ryan Parman
  38. * @author Geoffrey Sneddon
  39. * @author Ryan McCue
  40. * @link http://simplepie.org/ SimplePie
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. */
  43. /**
  44. * Used for fetching remote files and reading local files
  45. *
  46. * Supports HTTP 1.0 via cURL or fsockopen, with spotty HTTP 1.1 support
  47. *
  48. * This class can be overloaded with {@see SimplePie::set_file_class()}
  49. *
  50. * @package SimplePie
  51. * @subpackage HTTP
  52. * @todo Move to properly supporting RFC2616 (HTTP/1.1)
  53. */
  54. class SimplePie_File
  55. {
  56. var $url;
  57. var $useragent;
  58. var $success = true;
  59. var $headers = array();
  60. var $body;
  61. var $status_code;
  62. var $redirects = 0;
  63. var $error;
  64. var $method = SIMPLEPIE_FILE_SOURCE_NONE;
  65. var $permanent_url;
  66. public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false, $curl_options = array(), $syslog_enabled = SIMPLEPIE_SYSLOG)
  67. {
  68. if (class_exists('idna_convert'))
  69. {
  70. $idn = new idna_convert();
  71. $parsed = SimplePie_Misc::parse_url($url);
  72. $url = SimplePie_Misc::compress_parse_url($parsed['scheme'], $idn->encode($parsed['authority']), $parsed['path'], $parsed['query'], $parsed['fragment']);
  73. }
  74. $this->url = $url;
  75. $this->permanent_url = $url;
  76. $this->useragent = $useragent;
  77. if (preg_match('/^http(s)?:\/\//i', $url))
  78. {
  79. if ($syslog_enabled)
  80. {
  81. syslog(LOG_INFO, 'SimplePie GET ' . SimplePie_Misc::url_remove_credentials($url)); //FreshRSS
  82. }
  83. if ($useragent === null)
  84. {
  85. $useragent = ini_get('user_agent');
  86. $this->useragent = $useragent;
  87. }
  88. if (!is_array($headers))
  89. {
  90. $headers = array();
  91. }
  92. if (!$force_fsockopen && function_exists('curl_exec'))
  93. {
  94. $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_CURL;
  95. $fp = curl_init();
  96. $headers2 = array();
  97. foreach ($headers as $key => $value)
  98. {
  99. $headers2[] = "$key: $value";
  100. }
  101. if (version_compare(SimplePie_Misc::get_curl_version(), '7.10.5', '>='))
  102. {
  103. curl_setopt($fp, CURLOPT_ENCODING, '');
  104. }
  105. curl_setopt($fp, CURLOPT_URL, $url);
  106. curl_setopt($fp, CURLOPT_HEADER, 1);
  107. curl_setopt($fp, CURLOPT_RETURNTRANSFER, 1);
  108. curl_setopt($fp, CURLOPT_FAILONERROR, 1);
  109. curl_setopt($fp, CURLOPT_TIMEOUT, $timeout);
  110. curl_setopt($fp, CURLOPT_CONNECTTIMEOUT, $timeout);
  111. curl_setopt($fp, CURLOPT_REFERER, $url);
  112. curl_setopt($fp, CURLOPT_USERAGENT, $useragent);
  113. curl_setopt($fp, CURLOPT_HTTPHEADER, $headers2);
  114. if (!ini_get('open_basedir') && !ini_get('safe_mode') && version_compare(SimplePie_Misc::get_curl_version(), '7.15.2', '>='))
  115. {
  116. curl_setopt($fp, CURLOPT_FOLLOWLOCATION, 1);
  117. curl_setopt($fp, CURLOPT_MAXREDIRS, $redirects);
  118. }
  119. foreach ($curl_options as $curl_param => $curl_value) {
  120. curl_setopt($fp, $curl_param, $curl_value);
  121. }
  122. $this->headers = curl_exec($fp);
  123. if (curl_errno($fp) === 23 || curl_errno($fp) === 61)
  124. {
  125. curl_setopt($fp, CURLOPT_ENCODING, 'none');
  126. $this->headers = curl_exec($fp);
  127. }
  128. if (curl_errno($fp))
  129. {
  130. $this->error = 'cURL error ' . curl_errno($fp) . ': ' . curl_error($fp);
  131. $this->success = false;
  132. }
  133. else
  134. {
  135. // Use the updated url provided by curl_getinfo after any redirects.
  136. if ($info = curl_getinfo($fp)) {
  137. $this->url = $info['url'];
  138. }
  139. curl_close($fp);
  140. $this->headers = SimplePie_HTTP_Parser::prepareHeaders($this->headers, $info['redirect_count'] + 1);
  141. $parser = new SimplePie_HTTP_Parser($this->headers);
  142. if ($parser->parse())
  143. {
  144. $this->headers = $parser->headers;
  145. $this->body = trim($parser->body);
  146. $this->status_code = $parser->status_code;
  147. 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)
  148. {
  149. $this->redirects++;
  150. $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url);
  151. $previousStatusCode = $this->status_code;
  152. $this->__construct($location, $timeout, $redirects, $headers, $useragent, $force_fsockopen);
  153. $this->permanent_url = ($previousStatusCode == 301) ? $location : $url;
  154. return;
  155. }
  156. }
  157. }
  158. }
  159. else
  160. {
  161. $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_FSOCKOPEN;
  162. $url_parts = parse_url($url);
  163. $socket_host = $url_parts['host'];
  164. if (isset($url_parts['scheme']) && strtolower($url_parts['scheme']) === 'https')
  165. {
  166. $socket_host = "ssl://$url_parts[host]";
  167. $url_parts['port'] = 443;
  168. }
  169. if (!isset($url_parts['port']))
  170. {
  171. $url_parts['port'] = 80;
  172. }
  173. $fp = @fsockopen($socket_host, $url_parts['port'], $errno, $errstr, $timeout);
  174. if (!$fp)
  175. {
  176. $this->error = 'fsockopen error: ' . $errstr;
  177. $this->success = false;
  178. }
  179. else
  180. {
  181. stream_set_timeout($fp, $timeout);
  182. if (isset($url_parts['path']))
  183. {
  184. if (isset($url_parts['query']))
  185. {
  186. $get = "$url_parts[path]?$url_parts[query]";
  187. }
  188. else
  189. {
  190. $get = $url_parts['path'];
  191. }
  192. }
  193. else
  194. {
  195. $get = '/';
  196. }
  197. $out = "GET $get HTTP/1.1\r\n";
  198. $out .= "Host: $url_parts[host]\r\n";
  199. $out .= "User-Agent: $useragent\r\n";
  200. if (extension_loaded('zlib'))
  201. {
  202. $out .= "Accept-Encoding: x-gzip,gzip,deflate\r\n";
  203. }
  204. if (isset($url_parts['user']) && isset($url_parts['pass']))
  205. {
  206. $out .= "Authorization: Basic " . base64_encode("$url_parts[user]:$url_parts[pass]") . "\r\n";
  207. }
  208. foreach ($headers as $key => $value)
  209. {
  210. $out .= "$key: $value\r\n";
  211. }
  212. $out .= "Connection: Close\r\n\r\n";
  213. fwrite($fp, $out);
  214. $info = stream_get_meta_data($fp);
  215. $this->headers = '';
  216. while (!$info['eof'] && !$info['timed_out'])
  217. {
  218. $this->headers .= fread($fp, 1160);
  219. $info = stream_get_meta_data($fp);
  220. }
  221. if (!$info['timed_out'])
  222. {
  223. $parser = new SimplePie_HTTP_Parser($this->headers);
  224. if ($parser->parse())
  225. {
  226. $this->headers = $parser->headers;
  227. $this->body = $parser->body;
  228. $this->status_code = $parser->status_code;
  229. 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)
  230. {
  231. $this->redirects++;
  232. $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url);
  233. $previousStatusCode = $this->status_code;
  234. $this->__construct($location, $timeout, $redirects, $headers, $useragent, $force_fsockopen);
  235. $this->permanent_url = ($previousStatusCode == 301) ? $location : $url;
  236. return;
  237. }
  238. if (isset($this->headers['content-encoding']))
  239. {
  240. // Hey, we act dumb elsewhere, so let's do that here too
  241. switch (strtolower(trim($this->headers['content-encoding'], "\x09\x0A\x0D\x20")))
  242. {
  243. case 'gzip':
  244. case 'x-gzip':
  245. $decoder = new SimplePie_gzdecode($this->body);
  246. if (!$decoder->parse())
  247. {
  248. $this->error = 'Unable to decode HTTP "gzip" stream';
  249. $this->success = false;
  250. }
  251. else
  252. {
  253. $this->body = trim($decoder->data);
  254. }
  255. break;
  256. case 'deflate':
  257. if (($decompressed = gzinflate($this->body)) !== false)
  258. {
  259. $this->body = $decompressed;
  260. }
  261. else if (($decompressed = gzuncompress($this->body)) !== false)
  262. {
  263. $this->body = $decompressed;
  264. }
  265. else if (function_exists('gzdecode') && ($decompressed = gzdecode($this->body)) !== false)
  266. {
  267. $this->body = $decompressed;
  268. }
  269. else
  270. {
  271. $this->error = 'Unable to decode HTTP "deflate" stream';
  272. $this->success = false;
  273. }
  274. break;
  275. default:
  276. $this->error = 'Unknown content coding';
  277. $this->success = false;
  278. }
  279. }
  280. }
  281. }
  282. else
  283. {
  284. $this->error = 'fsocket timed out';
  285. $this->success = false;
  286. }
  287. fclose($fp);
  288. }
  289. }
  290. }
  291. else
  292. {
  293. $this->method = SIMPLEPIE_FILE_SOURCE_LOCAL | SIMPLEPIE_FILE_SOURCE_FILE_GET_CONTENTS;
  294. if (empty($url) || !($this->body = trim(file_get_contents($url))))
  295. {
  296. $this->error = 'file_get_contents could not read the file';
  297. $this->success = false;
  298. }
  299. }
  300. }
  301. }