PHP_Compat
[ class tree: PHP_Compat ] [ index: PHP_Compat ] [ all elements ]

Source for file file_get_contents.php

Documentation is available at file_get_contents.php

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

Documentation generated on Mon, 11 Mar 2019 15:26:35 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.