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

Source for file CookieManager.php

Documentation is available at CookieManager.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at                              |
  11. // | http://www.php.net/license/3_0.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. // | Author: Alexey Borzov <avb@php.net>                                  |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: CookieManager.php,v 1.3 2004/04/10 10:04:52 avb Exp $
  20.  
  21. /**
  22.  * This class is used to store cookies and pass them between HTTP requests.
  23.  * 
  24.  * @package HTTP_Client
  25.  * @author  Alexey Borzov <avb@php.net>
  26.  * @version $Revision: 1.3 $
  27.  */
  28. {
  29.    /**
  30.     * An array containing cookie values
  31.     * @var array 
  32.     */
  33.     var $_cookies = array();
  34.  
  35.  
  36.    /**
  37.     * Constructor
  38.     * 
  39.     * @access public
  40.     */
  41.     function HTTP_Client_CookieManager()
  42.     {
  43.         // abstract
  44.     }
  45.  
  46.  
  47.    /**
  48.     * Adds cookies to the request
  49.     * 
  50.     * @access public
  51.     * @param object An HTTP_Request object
  52.     */
  53.     function passCookies(&$request)
  54.     {
  55.         if (!empty($this->_cookies)) {
  56.             $url =$request->_url;
  57.             // We do not check cookie's "expires" field, as we do not store deleted
  58.             // cookies in the array and our client does not work long enough for other
  59.             // cookies to expire. If some kind of persistence is added to this object,
  60.             // then expiration should be checked upon loading and session cookies should
  61.             // be cleared on saving.
  62.             $cookies = array();
  63.             foreach ($this->_cookies as $cookie{
  64.                 if ($this->_domainMatch($url->host$cookie['domain']&& (0 === strpos($url->path$cookie['path']))
  65.                     && (empty($cookie['secure']|| $url->protocol == 'https')) {
  66.                     $cookies[$cookie['name']][strlen($cookie['path'])$cookie['value'];
  67.                 }
  68.             }
  69.             // cookies with longer paths go first
  70.             foreach ($cookies as $name => $values{
  71.                 krsort($values);
  72.                 foreach ($values as $value{
  73.                     $request->addCookie($name$value);
  74.                 }
  75.             }
  76.         }
  77.         return true;
  78.     }
  79.  
  80.  
  81.    /**
  82.     * Explicitly adds cookie to the list
  83.     * 
  84.     * @param array An array representing cookie, this function expects all of the array's
  85.     *               fields to be set
  86.     * @access public
  87.     */
  88.     function addCookie($cookie)
  89.     {
  90.         $hash $this->_makeHash($cookie['name']$cookie['domain']$cookie['path']);
  91.         $this->_cookies[$hash$cookie;
  92.     }
  93.  
  94.  
  95.    /**
  96.     * Updates cookie list from HTTP server response
  97.     *
  98.     * @access public
  99.     * @param object An HTTP_Request object with sendRequest() already done
  100.     */
  101.     function updateCookies(&$request)
  102.     {
  103.         if (false !== ($cookies $request->getResponseCookies())) {
  104.             $url =$request->_url;
  105.             foreach ($cookies as $cookie{
  106.                 // use the current domain by default
  107.                 if (!isset($cookie['domain'])) {
  108.                     $cookie['domain'$url->host;
  109.                 }
  110.                 // use the path to the current page by default
  111.                 if (!isset($cookie['path'])) {
  112.                     $cookie['path'= DIRECTORY_SEPARATOR == dirname($url->path)'/'dirname($url->path);
  113.                 }
  114.                 // check if the domains match
  115.                 if ($this->_domainMatch($url->host$cookie['domain'])) {
  116.                     $hash $this->_makeHash($cookie['name']$cookie['domain']$cookie['path']);
  117.                     // if value is empty or the time is in the past the cookie is deleted, else added
  118.                     if (strlen($cookie['value'])
  119.                         && (!isset($cookie['expires']|| (strtotime($cookie['expires']time()))) {
  120.                         $this->_cookies[$hash$cookie;
  121.                     elseif (isset($this->_cookies[$hash])) {
  122.                         unset($this->_cookies[$hash]);
  123.                     }
  124.                 }
  125.             }
  126.         }
  127.     }
  128.  
  129.  
  130.    /**
  131.     * Generates a key for the $_cookies array.
  132.     * 
  133.     * The cookies is uniquely identified by its name, domain and path.
  134.     * Thus we cannot make f.e. an associative array with name as a key, we should
  135.     * generate a key from these 3 values.
  136.     * 
  137.     * @access private
  138.     * @param string    Cookie name
  139.     * @param string    Cookie domain
  140.     * @param string    Cookie path
  141.     * @return string   a key
  142.     */
  143.     function _makeHash($name$domain$path)
  144.     {
  145.         return md5($name "\r\n" $domain "\r\n" $path);
  146.     }
  147.  
  148.  
  149.    /**
  150.     * Checks whether a cookie domain matches a request host.
  151.     * 
  152.     * Cookie domain can begin with a dot, it also must contain at least
  153.     * two dots.
  154.     * 
  155.     * @access private
  156.     * @param string     request host
  157.     * @param string     cookie domain
  158.     * @return bool      match success
  159.     */
  160.     function _domainMatch($requestHost$cookieDomain)
  161.     {
  162.         if ('.' != $cookieDomain{0}{
  163.             return $requestHost == $cookieDomain;
  164.         elseif (substr_count($cookieDomain'.'< 2{
  165.             return false;
  166.         else {
  167.             return substr('.'$requestHoststrlen($cookieDomain)) == $cookieDomain;
  168.         }
  169.     }
  170.  
  171.  
  172.    /**
  173.     * Clears the $_cookies array
  174.     *
  175.     * @access public
  176.     */
  177.     function reset()
  178.     {
  179.         $this->_cookies = array();
  180.     }
  181. }
  182. ?>

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