SimplePie_File

Used for fetching remote files and reading local files.

Defined (1)

The class is defined in the following location(s).

/wp-includes/SimplePie/File.php  
  1. class SimplePie_File 
  2. var $url; 
  3. var $useragent; 
  4. var $success = true; 
  5. var $headers = array(); 
  6. var $body; 
  7. var $status_code; 
  8. var $redirects = 0; 
  9. var $error; 
  10. var $method = SIMPLEPIE_FILE_SOURCE_NONE; 
  11.  
  12. public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) 
  13. if (class_exists('idna_convert')) 
  14. $idn = new idna_convert(); 
  15. $parsed = SimplePie_Misc::parse_url($url); 
  16. $url = SimplePie_Misc::compress_parse_url($parsed['scheme'], $idn->encode($parsed['authority']), $parsed['path'], $parsed['query'], $parsed['fragment']); 
  17. $this->url = $url; 
  18. $this->useragent = $useragent; 
  19. if (preg_match('/^http(s)?:\/\//i', $url)) 
  20. if ($useragent === null) 
  21. $useragent = ini_get('user_agent'); 
  22. $this->useragent = $useragent; 
  23. if (!is_array($headers)) 
  24. $headers = array(); 
  25. if (!$force_fsockopen && function_exists('curl_exec')) 
  26. $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_CURL; 
  27. $fp = curl_init(); 
  28. $headers2 = array(); 
  29. foreach ($headers as $key => $value) 
  30. $headers2[] = "$key: $value"; 
  31. if (version_compare(SimplePie_Misc::get_curl_version(), '7.10.5', '>=')) 
  32. curl_setopt($fp, CURLOPT_ENCODING, ''); 
  33. curl_setopt($fp, CURLOPT_URL, $url); 
  34. curl_setopt($fp, CURLOPT_HEADER, 1); 
  35. curl_setopt($fp, CURLOPT_RETURNTRANSFER, 1); 
  36. curl_setopt($fp, CURLOPT_TIMEOUT, $timeout); 
  37. curl_setopt($fp, CURLOPT_CONNECTTIMEOUT, $timeout); 
  38. curl_setopt($fp, CURLOPT_REFERER, $url); 
  39. curl_setopt($fp, CURLOPT_USERAGENT, $useragent); 
  40. curl_setopt($fp, CURLOPT_HTTPHEADER, $headers2); 
  41. if (!ini_get('open_basedir') && !ini_get('safe_mode') && version_compare(SimplePie_Misc::get_curl_version(), '7.15.2', '>=')) 
  42. curl_setopt($fp, CURLOPT_FOLLOWLOCATION, 1); 
  43. curl_setopt($fp, CURLOPT_MAXREDIRS, $redirects); 
  44.  
  45. $this->headers = curl_exec($fp); 
  46. if (curl_errno($fp) === 23 || curl_errno($fp) === 61) 
  47. curl_setopt($fp, CURLOPT_ENCODING, 'none'); 
  48. $this->headers = curl_exec($fp); 
  49. if (curl_errno($fp)) 
  50. $this->error = 'cURL error ' . curl_errno($fp) . ': ' . curl_error($fp); 
  51. $this->success = false; 
  52. else 
  53. $info = curl_getinfo($fp); 
  54. curl_close($fp); 
  55. $this->headers = explode("\r\n\r\n", $this->headers, $info['redirect_count'] + 1); 
  56. $this->headers = array_pop($this->headers); 
  57. $parser = new SimplePie_HTTP_Parser($this->headers); 
  58. if ($parser->parse()) 
  59. $this->headers = $parser->headers; 
  60. $this->body = $parser->body; 
  61. $this->status_code = $parser->status_code; 
  62. 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) 
  63. $this->redirects++; 
  64. $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url); 
  65. return $this->__construct($location, $timeout, $redirects, $headers, $useragent, $force_fsockopen); 
  66. else 
  67. $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE | SIMPLEPIE_FILE_SOURCE_FSOCKOPEN; 
  68. $url_parts = parse_url($url); 
  69. $socket_host = $url_parts['host']; 
  70. if (isset($url_parts['scheme']) && strtolower($url_parts['scheme']) === 'https') 
  71. $socket_host = "ssl://$url_parts[host]"; 
  72. $url_parts['port'] = 443; 
  73. if (!isset($url_parts['port'])) 
  74. $url_parts['port'] = 80; 
  75. $fp = @fsockopen($socket_host, $url_parts['port'], $errno, $errstr, $timeout); 
  76. if (!$fp) 
  77. $this->error = 'fsockopen error: ' . $errstr; 
  78. $this->success = false; 
  79. else 
  80. stream_set_timeout($fp, $timeout); 
  81. if (isset($url_parts['path'])) 
  82. if (isset($url_parts['query'])) 
  83. $get = "$url_parts[path]?$url_parts[query]"; 
  84. else 
  85. $get = $url_parts['path']; 
  86. else 
  87. $get = '/'; 
  88. $out = "GET $get HTTP/1.1\r\n"; 
  89. $out .= "Host: $url_parts[host]\r\n"; 
  90. $out .= "User-Agent: $useragent\r\n"; 
  91. if (extension_loaded('zlib')) 
  92. $out .= "Accept-Encoding: x-gzip, gzip, deflate\r\n"; 
  93.  
  94. if (isset($url_parts['user']) && isset($url_parts['pass'])) 
  95. $out .= "Authorization: Basic " . base64_encode("$url_parts[user]:$url_parts[pass]") . "\r\n"; 
  96. foreach ($headers as $key => $value) 
  97. $out .= "$key: $value\r\n"; 
  98. $out .= "Connection: Close\r\n\r\n"; 
  99. fwrite($fp, $out); 
  100.  
  101. $info = stream_get_meta_data($fp); 
  102.  
  103. $this->headers = ''; 
  104. while (!$info['eof'] && !$info['timed_out']) 
  105. $this->headers .= fread($fp, 1160); 
  106. $info = stream_get_meta_data($fp); 
  107. if (!$info['timed_out']) 
  108. $parser = new SimplePie_HTTP_Parser($this->headers); 
  109. if ($parser->parse()) 
  110. $this->headers = $parser->headers; 
  111. $this->body = $parser->body; 
  112. $this->status_code = $parser->status_code; 
  113. 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) 
  114. $this->redirects++; 
  115. $location = SimplePie_Misc::absolutize_url($this->headers['location'], $url); 
  116. return $this->__construct($location, $timeout, $redirects, $headers, $useragent, $force_fsockopen); 
  117. if (isset($this->headers['content-encoding'])) 
  118. // Hey, we act dumb elsewhere, so let's do that here too 
  119. switch (strtolower(trim($this->headers['content-encoding'], "\x09\x0A\x0D\x20"))) 
  120. case 'gzip': 
  121. case 'x-gzip': 
  122. $decoder = new SimplePie_gzdecode($this->body); 
  123. if (!$decoder->parse()) 
  124. $this->error = 'Unable to decode HTTP "gzip" stream'; 
  125. $this->success = false; 
  126. else 
  127. $this->body = $decoder->data; 
  128. break; 
  129.  
  130. case 'deflate': 
  131. if (($decompressed = gzinflate($this->body)) !== false) 
  132. $this->body = $decompressed; 
  133. else if (($decompressed = gzuncompress($this->body)) !== false) 
  134. $this->body = $decompressed; 
  135. else if (function_exists('gzdecode') && ($decompressed = gzdecode($this->body)) !== false) 
  136. $this->body = $decompressed; 
  137. else 
  138. $this->error = 'Unable to decode HTTP "deflate" stream'; 
  139. $this->success = false; 
  140. break; 
  141.  
  142. default: 
  143. $this->error = 'Unknown content coding'; 
  144. $this->success = false; 
  145. else 
  146. $this->error = 'fsocket timed out'; 
  147. $this->success = false; 
  148. fclose($fp); 
  149. else 
  150. $this->method = SIMPLEPIE_FILE_SOURCE_LOCAL | SIMPLEPIE_FILE_SOURCE_FILE_GET_CONTENTS; 
  151. if (!$this->body = file_get_contents($url)) 
  152. $this->error = 'file_get_contents could not read the file'; 
  153. $this->success = false;