/usr/share/php/PHP/Compat/Function/file_get_contents.php is in php-compat 1.6.0a3-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | <?php
define('PHP_COMPAT_FILE_GET_CONTENTS_MAX_REDIRECTS', 5);
/**
* Replace file_get_contents()
*
* @category PHP
* @package PHP_Compat
* @license LGPL - http://www.gnu.org/licenses/lgpl.html
* @copyright 2004-2007 Aidan Lister <aidan@php.net>, Arpad Ray <arpad@php.net>
* @link http://php.net/function.file_get_contents
* @author Aidan Lister <aidan@php.net>
* @author Arpad Ray <arpad@php.net>
* @version $Revision: 280045 $
* @internal resource_context is only supported for PHP 4.3.0+ (stream_context_get_options)
* @since PHP 4.3.0
* @require PHP 4.0.0 (user_error)
*/
function php_compat_file_get_contents($filename, $incpath = false, $resource_context = null, $offset = -1, $maxlen = -1)
{
if (is_resource($resource_context) && function_exists('stream_context_get_options')) {
$opts = stream_context_get_options($resource_context);
}
$colon_pos = strpos($filename, '://');
$wrapper = $colon_pos === false ? 'file' : substr($filename, 0, $colon_pos);
$opts = (empty($opts) || empty($opts[$wrapper])) ? array() : $opts[$wrapper];
$data = false;
switch ($wrapper) {
case 'http':
$max_redirects = (isset($opts[$wrapper]['max_redirects'])
? $opts[$proto]['max_redirects']
: PHP_COMPAT_FILE_GET_CONTENTS_MAX_REDIRECTS);
for ($i = 0; $i < $max_redirects; $i++) {
$data = php_compat_http_get_contents_helper($filename, $opts);
if (is_array($contents)) {
// redirected
$filename = rtrim($contents[1]);
$data = '';
continue;
}
break 2;
}
user_error('redirect limit exceeded', E_USER_WARNING);
return;
case 'ftp':
case 'https':
case 'ftps':
case 'socket':
// tbc
}
if (false === $data) {
if (is_resource($resource_context) && version_compare(PHP_VERSION, '4.3.0', 'ge')) {
$fh = fopen($filename, 'rb', $incpath, $resource_context);
} else {
$fh = fopen($filename, 'rb', $incpath);
}
if (false === $fh) {
user_error('failed to open stream: No such file or directory',
E_USER_WARNING);
return false;
}
clearstatcache();
if ($fsize = @filesize($filename)) {
$data = fread($fh, $fsize);
} else {
$data = '';
while (!feof($fh)) {
$data .= fread($fh, 8192);
}
}
fclose($fh);
}
if ($offset != -1) {
$data = substr($data, $offset);
}
if ($maxlen != -1) {
$data = substr($data, 0, $maxlen);
}
return $data;
}
/**
* Performs HTTP requests
*
* @param string $filename
* the full path to request
* @param array $opts
* an array of stream context options
* @return mixed
* either the contents of the requested path (as a string),
* or an array where $array[1] is the path redirected to.
*/
function php_compat_http_get_contents_helper($filename, $opts)
{
$path = parse_url($filename);
if (!isset($path['host'])) {
return '';
}
$port = isset($path['port']) ? $path['port'] : 80;
$fp = fsockopen($path['host'], $port, $errno, $errstr, 4);
if (!$fp) {
return '';
}
if (!isset($path['path'])) {
$path['path'] = '/';
}
$headers = array(
'Host' => $path['host'],
'Conection' => 'close'
);
// enforce some options (proxy isn't supported)
$opts_defaults = array(
'method' => 'GET',
'header' => null,
'user_agent' => ini_get('user_agent'),
'content' => null,
'request_fulluri' => false
);
foreach ($opts_defaults as $key => $value) {
if (!isset($opts[$key])) {
$opts[$key] = $value;
}
}
$opts['path'] = $opts['request_fulluri'] ? $filename : $path['path'];
// build request
$request = $opts['method'] . ' ' . $opts['path'] . " HTTP/1.0\r\n";
// build headers
if (isset($opts['header'])) {
$optheaders = explode("\r\n", $opts['header']);
for ($i = count($optheaders); $i--;) {
$sep_pos = strpos($optheaders[$i], ': ');
$headers[substr($optheaders[$i], 0, $sep_pos)] = substr($optheaders[$i], $sep_pos + 2);
}
}
foreach ($headers as $key => $value) {
$request .= "$key: $value\r\n";
}
$request .= "\r\n" . $opts['content'];
// make request
fputs($fp, $request);
$response = '';
while (!feof($fp)) {
$response .= fgets($fp, 8192);
}
fclose($fp);
$content_pos = strpos($response, "\r\n\r\n");
$headers = substr($response, 0, $content_pos);
$content = substr($response, $content_pos + 4);
$GLOBALS['http_response_header'] = explode("\r\n", $headers);
// recurse for redirects
if (preg_match('/^Location: (.*)$/mi', $headers, $matches)) {
return $matches;
}
return $content;
}
// Define
if (!function_exists('file_get_contents')) {
function file_get_contents($filename, $incpath = false, $resource_context = null, $offset = -1, $maxlen = -1)
{
return php_compat_file_get_contents($filename, $incpath, $resource_context);
}
}
?>
|