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.5 2006/06/02 20:09:04 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.5 $
  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.
  60.             $cookies = array();
  61.             foreach ($this->_cookies as $cookie{
  62.                 if ($this->_domainMatch($url->host$cookie['domain']&& (0 === strpos($url->path$cookie['path']))
  63.                     && (empty($cookie['secure']|| $url->protocol == 'https')) {
  64.                     $cookies[$cookie['name']][strlen($cookie['path'])$cookie['value'];
  65.                 }
  66.             }
  67.             // cookies with longer paths go first
  68.             foreach ($cookies as $name => $values{
  69.                 krsort($values);
  70.                 foreach ($values as $value{
  71.                     $request->addCookie($name$value);
  72.                 }
  73.             }
  74.         }
  75.         return true;
  76.     }
  77.  
  78.  
  79.    /**
  80.     * Explicitly adds cookie to the list
  81.     * 
  82.     * @param array An array representing cookie, this function expects all of the array's
  83.     *               fields to be set
  84.     * @access public
  85.     */
  86.     function addCookie($cookie)
  87.     {
  88.         $hash $this->_makeHash($cookie['name']$cookie['domain']$cookie['path']);
  89.         $this->_cookies[$hash$cookie;
  90.     }
  91.  
  92.  
  93.    /**
  94.     * Updates cookie list from HTTP server response
  95.     *
  96.     * @access public
  97.     * @param object An HTTP_Request object with sendRequest() already done
  98.     */
  99.     function updateCookies(&$request)
  100.     {
  101.         if (false !== ($cookies $request->getResponseCookies())) {
  102.             $url =$request->_url;
  103.             foreach ($cookies as $cookie{
  104.                 // use the current domain by default
  105.                 if (!isset($cookie['domain'])) {
  106.                     $cookie['domain'$url->host;
  107.                 }
  108.                 // use the path to the current page by default
  109.                 if (empty($cookie['path'])) {
  110.                     $cookie['path'= DIRECTORY_SEPARATOR == dirname($url->path)'/'dirname($url->path);
  111.                 }
  112.                 // check if the domains match
  113.                 if ($this->_domainMatch($url->host$cookie['domain'])) {
  114.                     $hash $this->_makeHash($cookie['name']$cookie['domain']$cookie['path']);
  115.                     // if value is empty or the time is in the past the cookie is deleted, else added
  116.                     if (strlen($cookie['value'])
  117.                         && (!isset($cookie['expires']|| (strtotime($cookie['expires']time()))) {
  118.                         $this->_cookies[$hash$cookie;
  119.                     elseif (isset($this->_cookies[$hash])) {
  120.                         unset($this->_cookies[$hash]);
  121.                     }
  122.                 }
  123.             }
  124.         }
  125.     }
  126.  
  127.  
  128.    /**
  129.     * Generates a key for the $_cookies array.
  130.     * 
  131.     * The cookies is uniquely identified by its name, domain and path.
  132.     * Thus we cannot make f.e. an associative array with name as a key, we should
  133.     * generate a key from these 3 values.
  134.     * 
  135.     * @access private
  136.     * @param string    Cookie name
  137.     * @param string    Cookie domain
  138.     * @param string    Cookie path
  139.     * @return string   a key
  140.     */
  141.     function _makeHash($name$domain$path)
  142.     {
  143.         return md5($name "\r\n" $domain "\r\n" $path);
  144.     }
  145.  
  146.  
  147.    /**
  148.     * Checks whether a cookie domain matches a request host.
  149.     * 
  150.     * Cookie domain can begin with a dot, it also must contain at least
  151.     * two dots.
  152.     * 
  153.     * @access private
  154.     * @param string     request host
  155.     * @param string     cookie domain
  156.     * @return bool      match success
  157.     */
  158.     function _domainMatch($requestHost$cookieDomain)
  159.     {
  160.         if ('.' != $cookieDomain{0}{
  161.             return $requestHost == $cookieDomain;
  162.         elseif (substr_count($cookieDomain'.'< 2{
  163.             return false;
  164.         else {
  165.             return substr('.'$requestHoststrlen($cookieDomain)) == $cookieDomain;
  166.         }
  167.     }
  168.  
  169.  
  170.    /**
  171.     * Clears the $_cookies array
  172.     *
  173.     * @access public
  174.     */
  175.     function reset()
  176.     {
  177.         $this->_cookies = array();
  178.     }
  179.  
  180.  
  181.    /**
  182.     * Magic serialization function, removes session cookies
  183.     */
  184.     function __sleep()
  185.     {
  186.         foreach ($this->_cookies as $hash => $cookie{
  187.             if (empty($cookie['expires'])) {
  188.                 unset($this->_cookies[$hash]);
  189.             }
  190.         }
  191.         return array('_cookies');
  192.     }
  193.  
  194.  
  195.    /**
  196.     * Magic unserialization function, purges expired cookies
  197.     */
  198.     function __wakeup()
  199.     {
  200.         foreach ($this->_cookies as $hash => $cookie{
  201.             if (strtotime($cookie['expires']time()) {
  202.                 unset($this->_cookies[$hash]);
  203.             }
  204.         }
  205.     }
  206. }
  207. ?>

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