cvs: pear /PHP_Compat/Compat/Function file_get_contents.php
| From: | Arpad Ray | Date: | Mon, 02 Jan 2006 19:30:12 +0000 |
| Subject: | cvs: pear /PHP_Compat/Compat/Function file_get_contents.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-37267@lists.php.net to get a copy of this message | ||
arpad Mon Jan 2 19:30:12 2006 UTC
Modified files:
/pear/PHP_Compat/Compat/Function file_get_contents.php
Log:
initial stream context support (http)
http://cvs.php.net/viewcvs.cgi/pear/PHP_Compat/Compat/Function/file_get_contents.php?r1=1.22&r2=1.23&diff_format=u
Index: pear/PHP_Compat/Compat/Function/file_get_contents.php
diff -u pear/PHP_Compat/Compat/Function/file_get_contents.php:1.22
pear/PHP_Compat/Compat/Function/file_get_contents.php:1.23
--- pear/PHP_Compat/Compat/Function/file_get_contents.php:1.22 Mon Dec 19 23:45:29 2005
+++ pear/PHP_Compat/Compat/Function/file_get_contents.php Mon Jan 2 19:30:12 2006
@@ -13,10 +13,12 @@
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Aidan Lister <aidan@php.net> |
+// | Arpad Ray <arpad@php.net> |
// +----------------------------------------------------------------------+
//
-// $Id: file_get_contents.php,v 1.22 2005/12/19 23:45:29 aidan Exp $
+// $Id: file_get_contents.php,v 1.23 2006/01/02 19:30:12 arpad Exp $
+define('PHP_COMPAT_FILE_GET_CONTENTS_MAX_REDIRECTS', 5);
/**
* Replace file_get_contents()
@@ -25,15 +27,48 @@
* @package PHP_Compat
* @link http://php.net/function.file_get_contents
* @author Aidan Lister <aidan@php.net>
- * @version $Revision: 1.22 $
- * @internal resource_context is not supported
+ * @author Arpad Ray <arpad@php.net>
+ * @version $Revision: 1.23 $
+ * @internal resource_context is only supported for PHP 4.3.0+ (stream_context_get_options)
* @since PHP 5
* @require PHP 4.0.0 (user_error)
*/
function php_compat_file_get_contents($filename, $incpath = false, $resource_context = null)
{
+ 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];
+
+ 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++) {
+ $contents = php_compat_http_get_contents_helper($filename, $opts);
+ if (is_array($contents)) {
+ // redirected
+ $filename = rtrim($contents[1]);
+ $contents = '';
+ continue;
+ }
+ return $contents;
+ }
+ user_error('redirect limit exceeded', E_USER_WARNING);
+ return;
+ case 'ftp':
+ case 'https':
+ case 'ftps':
+ case 'socket':
+ // tbc
+ }
+
if (false === $fh = fopen($filename, 'rb', $incpath)) {
- user_error('file_get_contents() failed to open stream: No such file or
directory',
+ user_error('failed to open stream: No such file or directory',
E_USER_WARNING);
return false;
}
@@ -52,11 +87,94 @@
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 '';
+ }
+ $fp = fsockopen($path['host'], 80, $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");
+
+
+ // recurse for redirects
+ if (preg_match('/^Location: (.*)$/mi', $response, $matches)) {
+ return $matches;
+ }
+ return ($content_pos != -1 ? substr($response, $content_pos + 4) : $response);
+}
+
+function php_compat_ftp_get_contents_helper($filename, $opts)
+{
+}
-// Define
if (!function_exists('file_get_contents')) {
function file_get_contents($filename, $incpath = false, $resource_context = null)
{
return php_compat_file_get_contents($filename, $incpath, $resource_context);
}
}
+
+?>
\ No newline at end of file