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

Source for file Cookie.php

Documentation is available at Cookie.php

  1. <?php
  2.  
  3. /**
  4.  * Zend Framework
  5.  *
  6.  * LICENSE
  7.  *
  8.  * This source file is subject to the new BSD license that is bundled
  9.  * with this package in the file LICENSE.txt.
  10.  * It is also available through the world-wide-web at this URL:
  11.  * http://framework.zend.com/license/new-bsd
  12.  * If you did not receive a copy of the license and are unable to
  13.  * obtain it through the world-wide-web, please send an email
  14.  * to license@zend.com so we can send you a copy immediately.
  15.  *
  16.  * @category   Microsoft
  17.  * @package    Microsoft_Http
  18.  * @subpackage Cookie
  19.  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  20.  * @version    $Id: Cookie.php 17131 2009-07-26 10:03:39Z shahar $
  21.  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  22.  */
  23.  
  24. /**
  25.  * @see Microsoft_AutoLoader
  26.  */
  27. require_once dirname(__FILE__) . '/../AutoLoader.php';
  28.  
  29.  
  30. /**
  31.  * Microsoft_Http_Cookie is a class describing an HTTP cookie and all it's parameters.
  32.  *
  33.  * Microsoft_Http_Cookie is a class describing an HTTP cookie and all it's parameters. The
  34.  * class also enables validating whether the cookie should be sent to the server in
  35.  * a specified scenario according to the request URI, the expiry time and whether
  36.  * session cookies should be used or not. Generally speaking cookies should be
  37.  * contained in a Cookiejar object, or instantiated manually and added to an HTTP
  38.  * request.
  39.  *
  40.  * See http://wp.netscape.com/newsref/std/cookie_spec.html for some specs.
  41.  *
  42.  * @category   Microsoft
  43.  * @package    Microsoft_Http
  44.  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  45.  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  46.  */
  47. {
  48.     /**
  49.      * Cookie name
  50.      *
  51.      * @var string 
  52.      */
  53.     protected $name;
  54.  
  55.     /**
  56.      * Cookie value
  57.      *
  58.      * @var string 
  59.      */
  60.     protected $value;
  61.  
  62.     /**
  63.      * Cookie expiry date
  64.      *
  65.      * @var int 
  66.      */
  67.     protected $expires;
  68.  
  69.     /**
  70.      * Cookie domain
  71.      *
  72.      * @var string 
  73.      */
  74.     protected $domain;
  75.  
  76.     /**
  77.      * Cookie path
  78.      *
  79.      * @var string 
  80.      */
  81.     protected $path;
  82.  
  83.     /**
  84.      * Whether the cookie is secure or not
  85.      *
  86.      * @var boolean 
  87.      */
  88.     protected $secure;
  89.  
  90.     /**
  91.      * Cookie object constructor
  92.      *
  93.      * @todo Add validation of each one of the parameters (legal domain, etc.)
  94.      *
  95.      * @param string $name 
  96.      * @param string $value 
  97.      * @param string $domain 
  98.      * @param int $expires 
  99.      * @param string $path 
  100.      * @param bool $secure 
  101.      */
  102.     public function __construct($name, $value, $domain, $expires = null, $path = null, $secure = false)
  103.     {
  104.         if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
  105.             require_once 'Microsoft/Http/Exception.php';
  106.             throw new Microsoft_Http_Exception("Cookie name cannot contain these characters: =,; \\t\\r\\n\\013\\014 ({$name})");
  107.         }
  108.  
  109.         if (! $this->name = (string) $name) {
  110.             require_once 'Microsoft/Http/Exception.php';
  111.             throw new Microsoft_Http_Exception('Cookies must have a name');
  112.         }
  113.  
  114.         if (! $this->domain = (string) $domain) {
  115.             require_once 'Microsoft/Http/Exception.php';
  116.             throw new Microsoft_Http_Exception('Cookies must have a domain');
  117.         }
  118.  
  119.         $this->value = (string) $value;
  120.         $this->expires = ($expires === null ? null : (int) $expires);
  121.         $this->path = ($path ? $path : '/');
  122.         $this->secure = $secure;
  123.     }
  124.  
  125.     /**
  126.      * Get Cookie name
  127.      *
  128.      * @return string 
  129.      */
  130.     public function getName()
  131.     {
  132.         return $this->name;
  133.     }
  134.  
  135.     /**
  136.      * Get cookie value
  137.      *
  138.      * @return string 
  139.      */
  140.     public function getValue()
  141.     {
  142.         return $this->value;
  143.     }
  144.  
  145.     /**
  146.      * Get cookie domain
  147.      *
  148.      * @return string 
  149.      */
  150.     public function getDomain()
  151.     {
  152.         return $this->domain;
  153.     }
  154.  
  155.     /**
  156.      * Get the cookie path
  157.      *
  158.      * @return string 
  159.      */
  160.     public function getPath()
  161.     {
  162.         return $this->path;
  163.     }
  164.  
  165.     /**
  166.      * Get the expiry time of the cookie, or null if no expiry time is set
  167.      *
  168.      * @return int|null
  169.      */
  170.     public function getExpiryTime()
  171.     {
  172.         return $this->expires;
  173.     }
  174.  
  175.     /**
  176.      * Check whether the cookie should only be sent over secure connections
  177.      *
  178.      * @return boolean 
  179.      */
  180.     public function isSecure()
  181.     {
  182.         return $this->secure;
  183.     }
  184.  
  185.     /**
  186.      * Check whether the cookie has expired
  187.      *
  188.      * Always returns false if the cookie is a session cookie (has no expiry time)
  189.      *
  190.      * @param int $now Timestamp to consider as "now"
  191.      * @return boolean 
  192.      */
  193.     public function isExpired($now = null)
  194.     {
  195.         if ($now === null) $now = time();
  196.         if (is_int($this->expires) && $this->expires < $now) {
  197.             return true;
  198.         } else {
  199.             return false;
  200.         }
  201.     }
  202.  
  203.     /**
  204.      * Check whether the cookie is a session cookie (has no expiry time set)
  205.      *
  206.      * @return boolean 
  207.      */
  208.     public function isSessionCookie()
  209.     {
  210.         return ($this->expires === null);
  211.     }
  212.  
  213.     /**
  214.      * Checks whether the cookie should be sent or not in a specific scenario
  215.      *
  216.      * @param string|Microsoft_Uri_Http$uri URI to check against (secure, domain, path)
  217.      * @param boolean $matchSessionCookies Whether to send session cookies
  218.      * @param int $now Override the current time when checking for expiry time
  219.      * @return boolean 
  220.      */
  221.     public function match($uri, $matchSessionCookies = true, $now = null)
  222.     {
  223.         if (is_string ($uri)) {
  224.             $uri = Microsoft_Uri_Http::factory($uri);
  225.         }
  226.  
  227.         // Make sure we have a valid Microsoft_Uri_Http object
  228.         if (! ($uri->valid() && ($uri->getScheme() == 'http' || $uri->getScheme() =='https'))) {
  229.             require_once 'Microsoft/Http/Exception.php';
  230.             throw new Microsoft_Http_Exception('Passed URI is not a valid HTTP or HTTPS URI');
  231.         }
  232.  
  233.         // Check that the cookie is secure (if required) and not expired
  234.         if ($this->secure && $uri->getScheme() != 'https') return false;
  235.         if ($this->isExpired($now)) return false;
  236.         if ($this->isSessionCookie() && ! $matchSessionCookies) return false;
  237.  
  238.         // Check if the domain matches
  239.         if (! self::matchCookieDomain($this->getDomain(), $uri->getHost())) {
  240.             return false;
  241.         }
  242.  
  243.         // Check that path matches using prefix match
  244.         if (! self::matchCookiePath($this->getPath(), $uri->getPath())) {
  245.             return false;
  246.         }
  247.  
  248.         // If we didn't die until now, return true.
  249.         return true;
  250.     }
  251.  
  252.     /**
  253.      * Get the cookie as a string, suitable for sending as a "Cookie" header in an
  254.      * HTTP request
  255.      *
  256.      * @return string 
  257.      */
  258.     public function __toString()
  259.     {
  260.         return $this->name . '=' . urlencode($this->value) . ';';
  261.     }
  262.  
  263.     /**
  264.      * Generate a new Cookie object from a cookie string
  265.      * (for example the value of the Set-Cookie HTTP header)
  266.      *
  267.      * @param string $cookieStr 
  268.      * @param Microsoft_Uri_Http|string$ref_uri Reference URI for default values (domain, path)
  269.      * @return Microsoft_Http_Cookie A new Microsoft_Http_Cookie object or false on failure.
  270.      */
  271.     public static function fromString($cookieStr, $ref_uri = null)
  272.     {
  273.         // Set default values
  274.         if (is_string($ref_uri)) {
  275.             $ref_uri = Microsoft_Uri_Http::factory($ref_uri);
  276.         }
  277.  
  278.         $name    = '';
  279.         $value   = '';
  280.         $domain  = '';
  281.         $path    = '';
  282.         $expires = null;
  283.         $secure  = false;
  284.         $parts   = explode(';', $cookieStr);
  285.  
  286.         // If first part does not include '=', fail
  287.         if (strpos($parts[0], '=') === false) return false;
  288.  
  289.         // Get the name and value of the cookie
  290.         list($name, $value) = explode('=', trim(array_shift($parts)), 2);
  291.         $name  = trim($name);
  292.         $value = urldecode(trim($value));
  293.  
  294.         // Set default domain and path
  295.         if ($ref_uri instanceof Microsoft_Uri_Http) {
  296.             $domain = $ref_uri->getHost();
  297.             $path = $ref_uri->getPath();
  298.             $path = substr($path, 0, strrpos($path, '/'));
  299.         }
  300.  
  301.         // Set other cookie parameters
  302.         foreach ($parts as $part) {
  303.             $part = trim($part);
  304.             if (strtolower($part) == 'secure') {
  305.                 $secure = true;
  306.                 continue;
  307.             }
  308.  
  309.             $keyValue = explode('=', $part, 2);
  310.             if (count($keyValue) == 2) {
  311.                 list($k, $v) = $keyValue;
  312.                 switch (strtolower($k))    {
  313.                     case 'expires':
  314.                         if(($expires = strtotime($v)) === false) {
  315.                             /**
  316.                              * The expiration is past Tue, 19 Jan 2038 03:14:07 UTC
  317.                              * the maximum for 32-bit signed integer. Microsoft_Date
  318.                              * can get around that limit.
  319.                              *
  320.                              * @see Microsoft_Date
  321.                              */
  322.                             require_once 'Microsoft/Date.php';
  323.  
  324.                             $expireDate = new Microsoft_Date($v);
  325.                             $expires = $expireDate->getTimestamp();
  326.                         }
  327.                         break;
  328.  
  329.                     case 'path':
  330.                         $path = $v;
  331.                         break;
  332.  
  333.                     case 'domain':
  334.                         $domain = $v;
  335.                         break;
  336.  
  337.                     default:
  338.                         break;
  339.                 }
  340.             }
  341.         }
  342.  
  343.         if ($name !== '') {
  344.             return new self($name, $value, $domain, $expires, $path, $secure);
  345.         } else {
  346.             return false;
  347.         }
  348.     }
  349.  
  350.     /**
  351.      * Check if a cookie's domain matches a host name.
  352.      *
  353.      * Used by Microsoft_Http_Cookie and Microsoft_Http_CookieJar for cookie matching
  354.      *
  355.      * @param  string $cookieDomain 
  356.      * @param  string $host 
  357.      *
  358.      * @return boolean 
  359.      */
  360.     public static function matchCookieDomain($cookieDomain, $host)
  361.     {
  362.         if (! $cookieDomain) {
  363.             require_once 'Microsoft/Http/Exception.php';
  364.             throw new Microsoft_Http_Exception("\$cookieDomain is expected to be a cookie domain");
  365.         }
  366.  
  367.         if (! $host) {
  368.             require_once 'Microsoft/Http/Exception.php';
  369.             throw new Microsoft_Http_Exception("\$host is expected to be a host name");
  370.         }
  371.  
  372.         $cookieDomain = strtolower($cookieDomain);
  373.         $host = strtolower($host);
  374.  
  375.         if ($cookieDomain[0] == '.') {
  376.             $cookieDomain = substr($cookieDomain, 1);
  377.         }
  378.  
  379.         // Check for either exact match or suffix match
  380.         return ($cookieDomain == $host ||
  381.                 preg_match("/\.$cookieDomain$/", $host));
  382.     }
  383.  
  384.     /**
  385.      * Check if a cookie's path matches a URL path
  386.      *
  387.      * Used by Microsoft_Http_Cookie and Microsoft_Http_CookieJar for cookie matching
  388.      *
  389.      * @param  string $cookiePath 
  390.      * @param  string $path 
  391.      * @return boolean 
  392.      */
  393.     public static function matchCookiePath($cookiePath, $path)
  394.     {
  395.         if (! $cookiePath) {
  396.             require_once 'Microsoft/Http/Exception.php';
  397.             throw new Microsoft_Http_Exception("\$cookiePath is expected to be a cookie path");
  398.         }
  399.  
  400.         if (! $path) {
  401.             require_once 'Microsoft/Http/Exception.php';
  402.             throw new Microsoft_Http_Exception("\$path is expected to be a host name");
  403.         }
  404.  
  405.         return (strpos($path, $cookiePath) === 0);
  406.     }
  407. }

Documentation generated on Sat, 03 Dec 2011 13:59:18 +0100 by phpDocumentor 1.4.3