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

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