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

Source for file HTTP.php

Documentation is available at HTTP.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Bakken <ssb@fast.no>                                   |
  17. // |          Sterling Hughes <sterling@php.net>                          |
  18. // |          Tomas V.V.Cox <cox@idecnet.com>                             |
  19. // |          Richard Heyes <richard@php.net>                             |
  20. // |          Philippe Jausions <Philippe.Jausions@11abacus.com>          |
  21. // |          Michael Wallner <mike@php.net>                              |
  22. // +----------------------------------------------------------------------+
  23. //
  24. // $Id: HTTP.php,v 1.26 2004/06/10 21:54:53 mike Exp $
  25. //
  26.  
  27. /**
  28.  * HTTP
  29.  * 
  30.  * HTTP utility functions
  31.  * 
  32.  * @package     HTTP
  33.  * @category    HTTP
  34.  * @version     $Revision: 1.26 $
  35.  * @access      public
  36.  */
  37. class HTTP
  38. {
  39.     /**
  40.      * Date
  41.      * 
  42.      * Format a RFC compliant GMT date HTTP header.  This function honors the
  43.      * "y2k_compliance" php.ini directive and formats the GMT date corresponding
  44.      * to either RFC850 or RFC822.
  45.      * 
  46.      * @static
  47.      * @access  public
  48.      * @return  mixed   GMT date string, or false for an invalid $time parameter
  49.      * @param   mixed   $time unix timestamp or date (default = current time)
  50.      */
  51.     function Date($time = null)
  52.     {
  53.         if (!isset($time)) {
  54.             $time time();
  55.         elseif (!is_numeric($time&& (-1 === $time strtotime($time))) {
  56.             return false;
  57.         }
  58.         
  59.         // RFC822 or RFC850
  60.         $format ini_get('y2k_compliance''D, d M Y' 'F, d-D-y';
  61.         
  62.         return gmdate($format .' H:i:s \G\M\T'$time);
  63.     }
  64.  
  65.     /**
  66.      * Negotiate Language
  67.      * 
  68.      * Negotiate language with the user's browser through the Accept-Language
  69.      * HTTP header or the user's host address.  Language codes are generally in
  70.      * the form "ll" for a language spoken in only one country, or "ll-CC" for a
  71.      * language spoken in a particular country.  For example, U.S. English is
  72.      * "en-US", while British English is "en-UK".  Portugese as spoken in
  73.      * Portugal is "pt-PT", while Brazilian Portugese is "pt-BR".
  74.      * 
  75.      * Quality factors in the Accept-Language: header are supported, e.g.:
  76.      *      Accept-Language: en-UK;q=0.7, en-US;q=0.6, no, dk;q=0.8
  77.      * 
  78.      * <code>
  79.      *  require_once 'HTTP.php';
  80.      *  $langs = array(
  81.      *      'en'   => 'locales/en',
  82.      *      'en-US'=> 'locales/en',
  83.      *      'en-UK'=> 'locales/en',
  84.      *      'de'   => 'locales/de',
  85.      *      'de-DE'=> 'locales/de',
  86.      *      'de-AT'=> 'locales/de',
  87.      *  );
  88.      *  $neg = HTTP::negotiateLanguage($langs);
  89.      *  $dir = $langs[$neg];
  90.      * </code>
  91.      * 
  92.      * @static
  93.      * @access  public
  94.      * @return  string  The negotiated language result or the supplied default.
  95.      * @param   array   $supported An associative array of supported languages,
  96.      *                   whose values must evaluate to true.
  97.      * @param   string  $default The default language to use if none is found.
  98.      */
  99.     function negotiateLanguage($supported$default 'en-US')
  100.     {
  101.         $supp = array();
  102.         foreach ($supported as $lang => $isSupported{
  103.             if ($isSupported{
  104.                 $supp[strToLower($lang)$lang;
  105.             }
  106.         }
  107.         
  108.         if (!count($supp)) {
  109.             return $default;
  110.         }
  111.  
  112.         $matches = array();
  113.         if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  114.             foreach (explode(','$_SERVER['HTTP_ACCEPT_LANGUAGE']as $lang{
  115.                 @list($l$qarray_map('strtolower'
  116.                     array_map('trim'explode(';'$lang)));
  117.                 if (isset($supp[$l])) {
  118.                     if (isset($q)) {
  119.                         $matches[$l= (float) str_replace('q='''$q);
  120.                     else {
  121.                         $matches[$l= 1000 - count($matches);
  122.                     }
  123.                 }
  124.             }
  125.         }
  126.  
  127.         if (count($matches)) {
  128.             asort($matchesSORT_NUMERIC);
  129.             return $supp[array_pop(array_keys($matches))];
  130.         }
  131.         
  132.         if (isset($_SERVER['REMOTE_HOST'])) {
  133.             $lang strtolower(array_pop(explode('.'$_SERVER['REMOTE_HOST'])));
  134.             if (isset($supp[$lang])) {
  135.                 return $supp[$lang];
  136.             }
  137.         }
  138.  
  139.         return $default;
  140.     }
  141.  
  142.     /**
  143.      * Head
  144.      * 
  145.      * Sends a "HEAD" HTTP command to a server and returns the headers
  146.      * as an associative array. Example output could be:
  147.      * <code>
  148.      *     Array
  149.      *     (
  150.      *         [response_code] => 200          // The HTTP response code
  151.      *         [response] => HTTP/1.1 200 OK   // The full HTTP response string
  152.      *         [Date] => Fri, 11 Jan 2002 01:41:44 GMT
  153.      *         [Server] => Apache/1.3.20 (Unix) PHP/4.1.1
  154.      *         [X-Powered-By] => PHP/4.1.1
  155.      *         [Connection] => close
  156.      *         [Content-Type] => text/html
  157.      *     )
  158.      * </code>
  159.      * 
  160.      * @see HTTP_Client::head()
  161.      * @see HTTP_Request
  162.      * @static
  163.      * @access  public
  164.      * @return  mixed   Returns associative array of response headers on success
  165.      *                   or PEAR error on failure.
  166.      * @param   string  $url A valid URL, e.g.: http://pear.php.net/credits.php
  167.      * @param   integer $timeout Timeout in seconds (default = 10)
  168.      */
  169.     function head($url$timeout = 10)
  170.     {
  171.         $p parse_url($url);
  172.         if (!isset($p['scheme'])) {
  173.             $p parse_url(HTTP::absoluteURI($url));
  174.         elseif ($p['scheme'!= 'http'{
  175.             return HTTP::raiseError('Unsupported protocol: '$p['scheme']);
  176.         }
  177.  
  178.         $port = isset($p['port']$p['port': 80;
  179.  
  180.         if (!$fp @fsockopen($p['host']$port$eno$estr$timeout)) {
  181.             return HTTP::raiseError("Connection error: $estr ($eno)");
  182.         }
  183.  
  184.         $path  !empty($p['path']$p['path''/';
  185.         $path .= !empty($p['query']'?' $p['query''';
  186.  
  187.         fputs($fp"HEAD $path HTTP/1.0\r\n");
  188.         fputs($fp'Host: ' $p['host'':' $port "\r\n");
  189.         fputs($fp"Connection: close\r\n\r\n");
  190.  
  191.         $response rtrim(fgets($fp4096));
  192.         if (preg_match("|^HTTP/[^\s]*\s(.*?)\s|"$response$status)) {
  193.             $headers['response_code'$status[1];
  194.         }
  195.         $headers['response'$response;
  196.  
  197.         while ($line fgets($fp4096)) {
  198.             if (!trim($line)) {
  199.                 break;
  200.             }
  201.             if (($pos strpos($line':')) !== false{
  202.                 $header substr($line0$pos);
  203.                 $value  trim(substr($line$pos + 1));
  204.                 $headers[$header$value;
  205.             }
  206.         }
  207.         fclose($fp);
  208.         return $headers;
  209.     }
  210.  
  211.     /**
  212.      * Redirect
  213.      * 
  214.      * This function redirects the client.  This is done by issuing
  215.      * a "Location" header and exiting if wanted.
  216.      * 
  217.      * @static
  218.      * @access  public
  219.      * @return  mixed   Returns true on succes (or exits) or false if headers
  220.      *                   have already been sent.
  221.      * @param   string  $url URL where the redirect should go to.
  222.      * @param   bool    $exit Whether to exit immediately after redirection.
  223.      */
  224.     function redirect($url$exit = true)
  225.     {
  226.         if (headers_sent()) {
  227.             return false;
  228.         }
  229.         
  230.         header('Location: 'HTTP::absoluteURI($url));
  231.         
  232.         if ($exit{
  233.             exit;
  234.         }
  235.         return true;
  236.     }
  237.  
  238.     /**
  239.      * Absolute URI
  240.      * 
  241.      * This function returns the absolute URI for the partial URL passed.
  242.      * The current scheme (HTTP/HTTPS), host server, port, current script
  243.      * location are used if necessary to resolve any relative URLs.
  244.      * 
  245.      * Offsets potentially created by PATH_INFO are taken care of to resolve
  246.      * relative URLs to the current script.
  247.      * 
  248.      * You can choose a new protocol while resolving the URI.  This is
  249.      * particularly useful when redirecting a web browser using relative URIs
  250.      * and to switch from HTTP to HTTPS, or vice-versa, at the same time.
  251.      * 
  252.      * @author  Philippe Jausions <Philippe.Jausions@11abacus.com>
  253.      * @static
  254.      * @access  public
  255.      * @return  string  The absolute URI.
  256.      * @param   string  $url Absolute or relative URI the redirect should go to.
  257.      * @param   string  $protocol Protocol to use when redirecting URIs.
  258.      * @param   integer $port A new port number.
  259.      */
  260.     function absoluteURI($url$protocol = null$port = null)
  261.     {
  262.         // Mess around with already absolute URIs
  263.         if (preg_match('!^([a-z0-9]+)://!i'$url)) {
  264.             if (empty($protocol&& empty($port)) {
  265.                 return $url;
  266.             }
  267.             if (!empty($protocol)) {
  268.                 $url $protocol .':'array_pop(explode(':'$url2));
  269.             }
  270.             if (!empty($port)) {
  271.                 $url preg_replace('!^(([a-z0-9]+)://[^/:]+)(:[\d]+)?!i'
  272.                     '\1:'$port$url);
  273.             }
  274.             return $url;
  275.         }
  276.             
  277.         $host 'localhost';
  278.         if (!empty($_SERVER['HTTP_HOST'])) {
  279.             list($hostexplode(':'$_SERVER['HTTP_HOST']);
  280.         elseif (!empty($_SERVER['SERVER_NAME'])) {
  281.             list($hostexplode(':'$_SERVER['SERVER_NAME']);
  282.         }
  283.  
  284.         if (empty($protocol)) {
  285.             $protocol = isset($_SERVER['HTTPS']'https' 'http';
  286.             if (!isset($port|| $port != intval($port)) {
  287.                 $port $_SERVER['SERVER_PORT'];
  288.             }
  289.         }
  290.         
  291.         $server $protocol .'://'$host (isset($port':'$port '');
  292.         
  293.         if ($url{0== '/'{
  294.             return $server $url;
  295.         }
  296.         
  297.         // Check for PATH_INFO
  298.         if (isset($_SERVER['PATH_INFO'])) {
  299.             $path dirname(substr($_SERVER['PHP_SELF']0-strlen($_SERVER['PATH_INFO'])));
  300.         else {
  301.             $path dirname($_SERVER['PHP_SELF']);
  302.         }
  303.         
  304.         return $server strtr($path'\\''/'$url;
  305.     }
  306.  
  307.     /**
  308.      * Raise Error
  309.      * 
  310.      * Lazy raising of PEAR_Errors.
  311.      * 
  312.      * @static
  313.      * @access  protected
  314.      * @return  object PEAR_Error 
  315.      * @param   mixed   $error 
  316.      * @param   int     $code 
  317.      */
  318.     function raiseError($error = null$code = null)
  319.     {
  320.         require_once 'PEAR.php';
  321.         return PEAR::raiseError($error$code);
  322.     }
  323. }
  324. ?>

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