1
0
mirror of https://github.com/php/web-php.git synced 2026-03-23 23:02:13 +01:00

Remove fetch_contents and fetch_header (#566)

This commit is contained in:
Kamil Tekiela
2022-06-27 22:49:22 +01:00
committed by GitHub
parent 139d12d908
commit a6c79eca2e

View File

@@ -162,152 +162,6 @@ function header_nocache()
}
}
// Compatibility function to fetch data from external source
function fetch_contents($url, $headers = false) {
$terrors_setting = ini_set('track_errors', true);
// A mysterious and elusive bug on us3 allows this to work, but the other methods do not
// So for now, use Curl first (curl++)
if(function_exists('curl_exec')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, (int)$headers);
curl_setopt($ch, CURLOPT_USERAGENT, "php.net");
$data = curl_exec($ch);
curl_close($ch);
if ($headers) {
$array = explode("\n", $data);
foreach($array as $line) {
if (strlen($line)<=2) {
return $header;
} else {
$header[] = $line;
}
}
}
if (!$data) {
return array(
'ERROR_NOTE' => 'Unable to find a way to retrieve data with curl',
'ERROR_LAST' => $php_errormsg,
);
}
return $data;
}
if(function_exists('file_get_contents') && ini_get('allow_url_fopen')) {
$context = null;
$opts = array('user_agent' => 'php.net');
if (version_compare(PHP_VERSION, '5.1.0', '>')) {
$context = stream_context_get_default(array('http' => $opts));
}
elseif (version_compare(PHP_VERSION, '5.0.0', '>')) {
$context = stream_context_create(array('http' => $opts));
}
if ($context) {
$data = @file_get_contents($url, false, $context);
} else {
$data = @file_get_contents($url, false);
}
if ($headers) {
return $http_response_header;
}
if (!$data) {
return array(
'ERROR_NOTE' => 'Unable to find a way to retrieve data with file_get_contents',
'ERROR_LAST' => $php_errormsg,
'URL' => $url,
);
}
return $data;
}
$array = parse_url($url);
if(function_exists('fsockopen') && $fd = @fsockopen($array["host"], 80, $errno, $errstr, 15)) {
$data = "";
$header = array();
$body = false;
$path = $array["path"];
if (isset($array["query"])) {
$path .= "?" . $array["query"];
}
fputs($fd,"GET {$path} HTTP/1.0\r\n");
fputs($fd,"Host: {$array["host"]}\r\n");
fputs($fd,"User-Agent: php.net\r\n");
fputs($fd,"Connection: close\r\n\r\n");
while($line = fgets($fd)) {
if($body) {
$data .= $line;
} elseif(strlen($line)<=2) {
$body = true;
} else {
$header[] = $line;
}
}
fclose($fd);
if ($headers) {
return $header;
}
if (!$data) {
return array(
'ERROR_NOTE' => 'Unable to find a way to retrieve data with fsockopen',
'ERROR_LAST' => $php_errormsg,
);
}
return $data;
}
// TODO: Log if we get here
// Redirect to www.php.net ?
return array(
'ERROR_NOTE' => 'Unable to find a way to retrieve data',
'ERROR_LAST' => $php_errormsg,
);
}
// Compatibility function to fetch headers from external source
function fetch_header($url, $header) {
$headers = array();
if(function_exists("get_headers") && ini_get('allow_url_fopen')) {
$headers = get_headers($url, 1);
} else {
$data = fetch_contents($url, true);
if (isset($data["ERROR_NOTE"])) {
return null;
}
foreach($data as $line) {
if (($pos = strpos($line, ":")) !== false) {
$headers[substr($line, 0, $pos)] = trim(substr($line, $pos+1));
} else {
$headers[0] = trim($line);
}
}
}
if (!is_array($headers) || empty($headers)) {
return null;
}
if (is_string($header)) {
$header = strtolower($header);
$headers = array_change_key_case($headers, CASE_LOWER);
}
return isset($headers[$header]) ? $headers[$header] : null;
}
function get_available_sqlites() {
$allsqlites = array(1 => 'sqlite', 2 => 'sqlite3', 4 => 'pdo_sqlite', 8 => 'pdo_sqlite2');