web/lib/Zend/Http/Header/SetCookie.php
changeset 1230 68c69c656a2c
parent 808 6b6c2214f778
equal deleted inserted replaced
1229:5a6b6e770365 1230:68c69c656a2c
    15  *
    15  *
    16  * @category   Zend
    16  * @category   Zend
    17  * @package    Zend_Http
    17  * @package    Zend_Http
    18  * @subpackage Header
    18  * @subpackage Header
    19  * @version    $Id$
    19  * @version    $Id$
    20  * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
    20  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    21  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    21  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    22  */
    22  */
    23 
    23 
    24 /**
    24 /**
    25  * @see Zend_Http_Header_Exception_InvalidArgumentException
    25  * @see Zend_Http_Header_Exception_InvalidArgumentException
    39  *
    39  *
    40  * @todo Implement proxy settings
    40  * @todo Implement proxy settings
    41  * @category   Zend
    41  * @category   Zend
    42  * @package    Zend_Http
    42  * @package    Zend_Http
    43  * @subpackage Header
    43  * @subpackage Header
    44  * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
    44  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    45  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    45  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    46  */
    46  */
    47 class Zend_Http_Header_SetCookie
    47 class Zend_Http_Header_SetCookie
    48 {
    48 {
    49 
    49 
    61      */
    61      */
    62     protected $value = null;
    62     protected $value = null;
    63 
    63 
    64     /**
    64     /**
    65      * Version
    65      * Version
    66      * 
    66      *
    67      * @var integer
    67      * @var integer
    68      */
    68      */
    69     protected $version = null;
    69     protected $version = null;
    70     
    70 
    71     /**
    71     /**
    72      * Max Age
    72      * Max Age
    73      * 
    73      *
    74      * @var integer
    74      * @var integer
    75      */
    75      */
    76     protected $maxAge = null;
    76     protected $maxAge = null;
    77     
    77 
    78     /**
    78     /**
    79      * Cookie expiry date
    79      * Cookie expiry date
    80      *
    80      *
    81      * @var int
    81      * @var int
    82      */
    82      */
   137                     list($headerKey, $headerValue) = preg_split('#=\s*#', $keyValue, 2);
   137                     list($headerKey, $headerValue) = preg_split('#=\s*#', $keyValue, 2);
   138                 } else {
   138                 } else {
   139                     $headerKey = $keyValue;
   139                     $headerKey = $keyValue;
   140                     $headerValue = null;
   140                     $headerValue = null;
   141                 }
   141                 }
   142                 
   142 
   143                 // First K=V pair is always the cookie name and value
   143                 // First K=V pair is always the cookie name and value
   144                 if ($header->getName() === NULL) {
   144                 if ($header->getName() === NULL) {
   145                     $header->setName($headerKey);
   145                     $header->setName($headerKey);
   146                     $header->setValue($headerValue);
   146                     $header->setValue($headerValue);
   147                     continue;
   147                     continue;
   214         }
   214         }
   215 
   215 
   216         if ($secure) {
   216         if ($secure) {
   217             $this->setSecure($secure);
   217             $this->setSecure($secure);
   218         }
   218         }
   219         
   219 
   220         if ($httponly) {
   220         if ($httponly) {
   221             $this->setHttponly($httponly);
   221             $this->setHttponly($httponly);
   222         }
   222         }
   223     }
   223     }
   224 
   224 
   237     public function getFieldValue()
   237     public function getFieldValue()
   238     {
   238     {
   239         if ($this->getName() == '') {
   239         if ($this->getName() == '') {
   240             throw new Zend_Http_Header_Exception_RuntimeException('A cookie name is required to generate a field value for this cookie');
   240             throw new Zend_Http_Header_Exception_RuntimeException('A cookie name is required to generate a field value for this cookie');
   241         }
   241         }
   242         
   242 
   243         $value = $this->getValue();
   243         $value = $this->getValue();
   244         if (strpos($value,'"')!==false) {
   244         if (strpos($value,'"')!==false) {
   245             $value = '"'.urlencode(str_replace('"', '', $value)).'"';
   245             $value = '"'.urlencode(str_replace('"', '', $value)).'"';
   246         } else {
   246         } else {
   247             $value = urlencode($value);
   247             $value = urlencode($value);
   250 
   250 
   251         $version = $this->getVersion();
   251         $version = $this->getVersion();
   252         if ($version!==null) {
   252         if ($version!==null) {
   253             $fieldValue .= '; Version=' . $version;
   253             $fieldValue .= '; Version=' . $version;
   254         }
   254         }
   255         
   255 
   256         $maxAge = $this->getMaxAge();
   256         $maxAge = $this->getMaxAge();
   257         if ($maxAge!==null) {
   257         if ($maxAge!==null) {
   258             $fieldValue .= '; Max-Age=' . $maxAge;
   258             $fieldValue .= '; Max-Age=' . $maxAge;
   259         }
   259         }
   260         
   260 
   261         $expires = $this->getExpires();
   261         $expires = $this->getExpires();
   262         if ($expires) {
   262         if ($expires) {
   263             $fieldValue .= '; Expires=' . $expires;
   263             $fieldValue .= '; Expires=' . $expires;
   264         }
   264         }
   265 
   265 
   323         return $this->value;
   323         return $this->value;
   324     }
   324     }
   325 
   325 
   326     /**
   326     /**
   327      * Set version
   327      * Set version
   328      * 
   328      *
   329      * @param integer $version
   329      * @param integer $version
   330      */
   330      */
   331     public function setVersion($version)
   331     public function setVersion($version)
   332     {
   332     {
   333         if (!is_int($version)) {
   333         if (!is_int($version)) {
   334             throw new Zend_Http_Header_Exception_InvalidArgumentException('Invalid Version number specified');
   334             throw new Zend_Http_Header_Exception_InvalidArgumentException('Invalid Version number specified');
   335         }
   335         }
   336         $this->version = $version;
   336         $this->version = $version;
   337     }
   337     }
   338     
   338 
   339     /**
   339     /**
   340      * Get version
   340      * Get version
   341      * 
   341      *
   342      * @return integer
   342      * @return integer
   343      */
   343      */
   344     public function getVersion()
   344     public function getVersion()
   345     {
   345     {
   346         return $this->version;
   346         return $this->version;
   347     }
   347     }
   348     
   348 
   349     /**
   349     /**
   350      * Set Max-Age
   350      * Set Max-Age
   351      * 
   351      *
   352      * @param integer $maxAge
   352      * @param integer $maxAge
   353      */
   353      */
   354     public function setMaxAge($maxAge)
   354     public function setMaxAge($maxAge)
   355     {
   355     {
   356         if (!is_int($maxAge) || ($maxAge<0)) {
   356         if (!is_int($maxAge) || ($maxAge<0)) {
   357             throw new Zend_Http_Header_Exception_InvalidArgumentException('Invalid Max-Age number specified');
   357             throw new Zend_Http_Header_Exception_InvalidArgumentException('Invalid Max-Age number specified');
   358         }
   358         }
   359         $this->maxAge = $maxAge;
   359         $this->maxAge = $maxAge;
   360     }
   360     }
   361     
   361 
   362     /**
   362     /**
   363      * Get Max-Age
   363      * Get Max-Age
   364      * 
   364      *
   365      * @return integer
   365      * @return integer
   366      */
   366      */
   367     public function getMaxAge()
   367     public function getMaxAge()
   368     {
   368     {
   369         return $this->maxAge;
   369         return $this->maxAge;
   502     public function isValidForRequest($requestDomain, $path, $isSecure = false)
   502     public function isValidForRequest($requestDomain, $path, $isSecure = false)
   503     {
   503     {
   504         if ($this->getDomain() && (strrpos($requestDomain, $this->getDomain()) !== false)) {
   504         if ($this->getDomain() && (strrpos($requestDomain, $this->getDomain()) !== false)) {
   505             return false;
   505             return false;
   506         }
   506         }
   507         
   507 
   508         if ($this->getPath() && (strpos($path, $this->getPath()) !== 0)) {
   508         if ($this->getPath() && (strpos($path, $this->getPath()) !== 0)) {
   509             return false;
   509             return false;
   510         }
   510         }
   511         
   511 
   512         if ($this->secure && $this->isSecure()!==$isSecure) {
   512         if ($this->secure && $this->isSecure()!==$isSecure) {
   513             return false;
   513             return false;
   514         }
   514         }
   515         
   515 
   516         return true;
   516         return true;
   517 
   517 
   518     }
   518     }
   519 
   519 
   520     public function toString()
   520     public function toString()
   521     {
   521     {
   522         return $this->getFieldName() . ': ' . $this->getFieldValue();
   522         return $this->getFieldName() . ': ' . $this->getFieldValue();
   523     }
   523     }
   524     
   524 
   525     public function __toString()
   525     public function __toString()
   526     {
   526     {
   527         return $this->toString();
   527         return $this->toString();
   528     }
   528     }
   529 
   529 
   540             $headerLine .= ', ' . $header->getFieldValue();
   540             $headerLine .= ', ' . $header->getFieldValue();
   541         }
   541         }
   542         return $headerLine;
   542         return $headerLine;
   543     }
   543     }
   544 
   544 
   545     
   545 
   546 }
   546 }