function SimplePie_File($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false)
{
if (class_exists('idna_convert')) {
$idn = new idna_convert();
$url = $idn->encode($url);
}
$this->url = $url;
$this->useragent = $useragent;
if (preg_match('/^http(s)?:\\/\\//i', $url)) {
if (empty($useragent)) {
$useragent = ini_get('user_agent');
$this->useragent = $useragent;
}
if (!is_array($headers)) {
$headers = array();
}
if (extension_loaded('curl') && version_compare(SimplePie_Misc::get_curl_version(), '7.10.5', '>=') && !$force_fsockopen) {
$this->method = 'curl';
$fp = curl_init();
$headers2 = array();
foreach ($headers as $key => $value) {
$headers2[] = "{$key}: {$value}";
}
curl_setopt($fp, CURLOPT_ENCODING, '');
curl_setopt($fp, CURLOPT_URL, $url);
curl_setopt($fp, CURLOPT_HEADER, 1);
curl_setopt($fp, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($fp, CURLOPT_TIMEOUT, $timeout);
curl_setopt($fp, CURLOPT_REFERER, $url);
curl_setopt($fp, CURLOPT_USERAGENT, $useragent);
curl_setopt($fp, CURLOPT_HTTPHEADER, $headers2);
if (!ini_get('open_basedir') && !ini_get('safe_mode')) {
curl_setopt($fp, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($fp, CURLOPT_MAXREDIRS, $redirects);
}
$this->headers = trim(curl_exec($fp));
if (curl_errno($fp) == 23 || curl_errno($fp) == 61) {
curl_setopt($fp, CURLOPT_ENCODING, 'none');
$this->headers = trim(curl_exec($fp));
}
if (curl_errno($fp)) {
$this->error = 'cURL error ' . curl_errno($fp) . ': ' . curl_error($fp);
$this->success = false;
return false;
}
$info = curl_getinfo($fp);
$this->headers = explode("\r\n\r\n", $this->headers, $info['redirect_count'] + 2);
if (count($this->headers) == $info['redirect_count'] + 1) {
$this->headers = array_pop($this->headers);
$this->body = '';
} else {
$this->body = array_pop($this->headers);
$this->headers = array_pop($this->headers);
}
$this->headers = $this->parse_headers($this->headers);
if (($this->headers['status']['code'] == 301 || $this->headers['status']['code'] == 302 || $this->headers['status']['code'] == 303 || $this->headers['status']['code'] == 307) && !empty($this->headers['location']) && $this->redirects < $redirects) {
$this->redirects++;
return $this->SimplePie_File($this->headers['location'], $timeout, $redirects, $headers, $useragent, $force_fsockopen);
}
} else {
$this->method = 'fsockopen';
$url_parts = parse_url($url);
if (isset($url_parts['scheme']) && strtolower($url_parts['scheme']) == 'https') {
$url_parts['host'] = "ssl://{$url_parts['host']}";
$url_parts['port'] = 443;
}
if (!isset($url_parts['port'])) {
$url_parts['port'] = 80;
}
$this->fp = fsockopen($url_parts['host'], $url_parts['port'], $errno, $errstr, $timeout);
if (!$this->fp) {
$this->error = 'fsockopen error: ' . $errstr;
$this->success = false;
return false;
} else {
stream_set_timeout($this->fp, $timeout);
$get = isset($url_parts['query']) ? "{$url_parts['path']}?{$url_parts['query']}" : $url_parts['path'];
$out = "GET {$get} HTTP/1.0\r\n";
$out .= "Host: {$url_parts['host']}\r\n";
$out .= "User-Agent: {$useragent}\r\n";
if (function_exists('gzinflate')) {
$out .= "Accept-Encoding: gzip,deflate\r\n";
}
if (!empty($url_parts['user']) && !empty($url_parts['pass'])) {
$out .= "Authorization: Basic " . base64_encode("{$url_parts['user']}:{$url_parts['pass']}") . "\r\n";
}
foreach ($headers as $key => $value) {
$out .= "{$key}: {$value}\r\n";
}
$out .= "Connection: Close\r\n\r\n";
fwrite($this->fp, $out);
$info = stream_get_meta_data($this->fp);
$data = '';
while (strpos($data, "\r\n\r\n") === false && !$info['timed_out']) {
$data .= fgets($this->fp, 128);
$info = stream_get_meta_data($this->fp);
}
if (!$info['timed_out']) {
$this->headers = $this->parse_headers($data);
if (($this->headers['status']['code'] == 301 || $this->headers['status']['code'] == 302 || $this->headers['status']['code'] == 303 || $this->headers['status']['code'] == 307) && !empty($this->headers['location']) && $this->redirects < $redirects) {
$this->redirects++;
return $this->SimplePie_File($this->headers['location'], $timeout, $redirects, $headers, $useragent, $force_fsockopen);
}
} else {
$this->close();
$this->error = 'fsocket timed out';
$this->success = false;
return false;
}
}
}
return $this->headers['status']['code'];
} else {
$this->method = 'fopen';
if ($this->fp = fopen($url, 'r')) {
return true;
} else {
$this->error = 'fopen could not open the file';
$this->success = false;
return false;
}
}
}