PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/simplepie/library/SimplePie/File.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 306 lines | 244 code | 8 blank | 54 comment | 51 complexity | 5332ef9e6d0f523fc4f1c7d1eaf42af4 MD5 | raw file
  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())
  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 ($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_FAILONERROR, 1);
  105. curl_setopt($fp, CURLOPT_TIMEOUT, $timeout);
  106. curl_setopt($fp, CURLOPT_CONNECTTIMEOUT, $timeout);
  107. curl_setopt($fp, CURLOPT_REFERER, $url);
  108. curl_setopt($fp, CURLOPT_USERAGENT, $useragent);
  109. curl_setopt($fp, CURLOPT_HTTPHEADER, $headers2);
  110. if (!ini_get('open_basedir') && !ini_get('safe_mode') && version_compare(SimplePie_Misc::get_curl_version(), '7.15.2', '>='))
  111. {
  112. curl_setopt($fp, CURLOPT_FOLLOWLOCATION, 1);
  113. curl_setopt($fp, CURLOPT_MAXREDIRS, $redirects);
  114. }
  115. foreach ($curl_options as $curl_param => $curl_value) {
  116. curl_setopt($fp, $curl_param, $curl_value);
  117. }
  118. $this->headers = curl_exec($fp);
  119. if (curl_errno($fp) === 23 || curl_errno($fp) === 61)
  120. {
  121. curl_setopt($fp, CURLOPT_ENCODING, 'none');
  122. $this->headers = curl_exec($fp);
  123. }
  124. if (curl_errno($fp))
  125. {
  126. $this->error = 'cURL error ' . curl_errno($fp) . ': ' . curl_error($fp);
  127. $this->success = false;
  128. }
  129. else
  130. {
  131. // Use the updated url provided by curl_getinfo after any redirects.
  132. if ($info = curl_getinfo($fp)) {
  133. $this->url = $info['url'];
  134. }
  135. curl_close($fp);
  136. $this->headers = explode("\r\n\r\n", $this->headers, $info['redirect_count'] + 1);
  137. $this->headers = array_pop($this->headers);
  138. $parser = new SimplePie_HTTP_Parser($this->headers);
  139. if ($parser->parse())
  140. {
  141. $this->headers = $parser->headers;
  142. $this->body = trim($parser->body);
  143. $this->status_code = $parser->status_code;
  144. 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)
  145. {
  146. $this->redirects++;
  147. $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url);
  148. $previousStatusCode = $this->status_code;
  149. $this->__construct($location, $timeout, $redirects, $headers, $useragent, $force_fsockopen);
  150. $this->permanent_url = ($previousStatusCode == 301) ? $location : $url;
  151. return;
  152. }
  153. }
  154. }
  155. }
  156. else
  157. {
  158. $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_FSOCKOPEN;
  159. $url_parts = parse_url($url);
  160. $socket_host = $url_parts['host'];
  161. if (isset($url_parts['scheme']) && strtolower($url_parts['scheme']) === 'https')
  162. {
  163. $socket_host = "ssl://$url_parts[host]";
  164. $url_parts['port'] = 443;
  165. }
  166. if (!isset($url_parts['port']))
  167. {
  168. $url_parts['port'] = 80;
  169. }
  170. $fp = @fsockopen($socket_host, $url_parts['port'], $errno, $errstr, $timeout);
  171. if (!$fp)
  172. {
  173. $this->error = 'fsockopen error: ' . $errstr;
  174. $this->success = false;
  175. }
  176. else
  177. {
  178. stream_set_timeout($fp, $timeout);
  179. if (isset($url_parts['path']))
  180. {
  181. if (isset($url_parts['query']))
  182. {
  183. $get = "$url_parts[path]?$url_parts[query]";
  184. }
  185. else
  186. {
  187. $get = $url_parts['path'];
  188. }
  189. }
  190. else
  191. {
  192. $get = '/';
  193. }
  194. $out = "GET $get HTTP/1.1\r\n";
  195. $out .= "Host: $url_parts[host]\r\n";
  196. $out .= "User-Agent: $useragent\r\n";
  197. if (extension_loaded('zlib'))
  198. {
  199. $out .= "Accept-Encoding: x-gzip,gzip,deflate\r\n";
  200. }
  201. if (isset($url_parts['user']) && isset($url_parts['pass']))
  202. {
  203. $out .= "Authorization: Basic " . base64_encode("$url_parts[user]:$url_parts[pass]") . "\r\n";
  204. }
  205. foreach ($headers as $key => $value)
  206. {
  207. $out .= "$key: $value\r\n";
  208. }
  209. $out .= "Connection: Close\r\n\r\n";
  210. fwrite($fp, $out);
  211. $info = stream_get_meta_data($fp);
  212. $this->headers = '';
  213. while (!$info['eof'] && !$info['timed_out'])
  214. {
  215. $this->headers .= fread($fp, 1160);
  216. $info = stream_get_meta_data($fp);
  217. }
  218. if (!$info['timed_out'])
  219. {
  220. $parser = new SimplePie_HTTP_Parser($this->headers);
  221. if ($parser->parse())
  222. {
  223. $this->headers = $parser->headers;
  224. $this->body = $parser->body;
  225. $this->status_code = $parser->status_code;
  226. 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)
  227. {
  228. $this->redirects++;
  229. $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url);
  230. $previousStatusCode = $this->status_code;
  231. $this->__construct($location, $timeout, $redirects, $headers, $useragent, $force_fsockopen);
  232. $this->permanent_url = ($previousStatusCode == 301) ? $location : $url;
  233. return;
  234. }
  235. if (isset($this->headers['content-encoding']))
  236. {
  237. // Hey, we act dumb elsewhere, so let's do that here too
  238. switch (strtolower(trim($this->headers['content-encoding'], "\x09\x0A\x0D\x20")))
  239. {
  240. case 'gzip':
  241. case 'x-gzip':
  242. $decoder = new SimplePie_gzdecode($this->body);
  243. if (!$decoder->parse())
  244. {
  245. $this->error = 'Unable to decode HTTP "gzip" stream';
  246. $this->success = false;
  247. }
  248. else
  249. {
  250. $this->body = trim($decoder->data);
  251. }
  252. break;
  253. case 'deflate':
  254. if (($decompressed = gzinflate($this->body)) !== false)
  255. {
  256. $this->body = $decompressed;
  257. }
  258. else if (($decompressed = gzuncompress($this->body)) !== false)
  259. {
  260. $this->body = $decompressed;
  261. }
  262. else if (function_exists('gzdecode') && ($decompressed = gzdecode($this->body)) !== false)
  263. {
  264. $this->body = $decompressed;
  265. }
  266. else
  267. {
  268. $this->error = 'Unable to decode HTTP "deflate" stream';
  269. $this->success = false;
  270. }
  271. break;
  272. default:
  273. $this->error = 'Unknown content coding';
  274. $this->success = false;
  275. }
  276. }
  277. }
  278. }
  279. else
  280. {
  281. $this->error = 'fsocket timed out';
  282. $this->success = false;
  283. }
  284. fclose($fp);
  285. }
  286. }
  287. }
  288. else
  289. {
  290. $this->method = SIMPLEPIE_FILE_SOURCE_LOCAL | SIMPLEPIE_FILE_SOURCE_FILE_GET_CONTENTS;
  291. if (empty($url) || !($this->body = trim(file_get_contents($url))))
  292. {
  293. $this->error = 'file_get_contents could not read the file';
  294. $this->success = false;
  295. }
  296. }
  297. }
  298. }