Example #1
0
 /**
  * Generate a new Cookie object from a cookie string
  * (for example the value of the Set-Cookie HTTP header)
  *
  * @static
  * @throws Zend_Http_Header_Exception_InvalidArgumentException
  * @param  $headerLine
  * @param  bool $bypassHeaderFieldName
  * @return array|SetCookie
  */
 public static function fromString($headerLine, $bypassHeaderFieldName = false)
 {
     list($name, $value) = explode(': ', $headerLine, 2);
     // check to ensure proper header type for this factory
     if (strtolower($name) !== 'set-cookie') {
         throw new Zend_Http_Header_Exception_InvalidArgumentException('Invalid header line for Set-Cookie string: "' . $name . '"');
     }
     $multipleHeaders = preg_split('#(?<!Sun|Mon|Tue|Wed|Thu|Fri|Sat),\\s*#', $value);
     $headers = array();
     foreach ($multipleHeaders as $headerLine) {
         $header = new self();
         $keyValuePairs = preg_split('#;\\s*#', $headerLine);
         foreach ($keyValuePairs as $keyValue) {
             if (strpos($keyValue, '=')) {
                 list($headerKey, $headerValue) = preg_split('#=\\s*#', $keyValue, 2);
             } else {
                 $headerKey = $keyValue;
                 $headerValue = null;
             }
             // First K=V pair is always the cookie name and value
             if ($header->getName() === NULL) {
                 $header->setName($headerKey);
                 $header->setValue($headerValue);
                 continue;
             }
             // Process the remanining elements
             switch (str_replace(array('-', '_'), '', strtolower($headerKey))) {
                 case 'expires':
                     $header->setExpires($headerValue);
                     break;
                 case 'domain':
                     $header->setDomain($headerValue);
                     break;
                 case 'path':
                     $header->setPath($headerValue);
                     break;
                 case 'secure':
                     $header->setSecure(true);
                     break;
                 case 'httponly':
                     $header->setHttponly(true);
                     break;
                 case 'version':
                     $header->setVersion((int) $headerValue);
                     break;
                 case 'maxage':
                     $header->setMaxAge((int) $headerValue);
                     break;
                 default:
                     // Intentionally omitted
             }
         }
         $headers[] = $header;
     }
     return count($headers) == 1 ? array_pop($headers) : $headers;
 }
Example #2
0
 /**
  * Creates a cookie object from a string
  * 
  * @param   string                      The cookie string
  * @return  Woops_Http_Cookie           The cookie object
  * @throws  Woops_Http_Cookie_Exception If the cookie string cannot be parsed
  */
 public static function createCookieObject($str)
 {
     // Finds the position of the first '=' character
     $equal = strpos($str, '=');
     // Checks for the '=' character
     if (!$equal) {
         // Invalid cookie - No '=' character
         throw new Woops_Http_Cookie_Exception('Invalid cookie: \'' . $str . '\'', Woops_Http_Cookie_Exception::EXCEPTION_BAD_COOKIE);
     }
     // Gets the cookie's name
     $name = trim(substr($str, 0, $equal));
     // Gets the cookie options
     $options = trim(substr($str, $equal + 1));
     // Gets the cookie's options' parts
     $parts = explode(';', $options);
     // Gets the cookie valie
     $value = trim(array_shift($parts));
     // Creates the cookie object
     $cookie = new self($name, $value);
     // Process each part
     foreach ($parts as $part) {
         // Position of the '=' character
         $equal = strpos($part, '=');
         // Checks for the '=' character
         if (!$equal) {
             // Option without a value
             $name = trim($part);
         } else {
             // Gets the name and the value of the option
             $name = trim(substr($part, 0, $equal));
             $value = trim(substr($part, $equal + 1));
         }
         // Checks the option name
         switch ($name) {
             // Expiration date
             case 'expires':
                 // Sets the expiration date (as a timestamp)
                 $cookie->setExpires(strtotime($value));
                 break;
                 // Path
             // Path
             case 'path':
                 // Sets the cookie's path
                 $cookie->setPath($value);
                 break;
                 // Domain
             // Domain
             case 'domain':
                 // Sets the cookie's domain
                 $cookie->setDomain($value);
                 break;
                 // Secure option
             // Secure option
             case 'secure':
                 // The cookie is secure
                 $cookie->setSecure(true);
                 break;
                 // HTTP only option
             // HTTP only option
             case 'HttpOnly':
                 // The cookie is accessible only through the HTTP protocol
                 $cookie->setHttpOnly(true);
                 break;
                 // Unknown option
             // Unknown option
             default:
                 break;
         }
     }
     // Returns the cookie object
     return $cookie;
 }