File.php 10.0 KB

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