/** * Generate a new Cookie object from a cookie string * (for example the value of the Set-Cookie HTTP header) * * @param string $cookieStr * @param Microsoft_Uri_Http|string $ref_uri Reference URI for default values (domain, path) * @return Microsoft_Http_Cookie A new Microsoft_Http_Cookie object or false on failure. */ public static function fromString($cookieStr, $ref_uri = null) { // Set default values if (is_string($ref_uri)) { $ref_uri = Microsoft_Uri_Http::factory($ref_uri); } $name = ''; $value = ''; $domain = ''; $path = ''; $expires = null; $secure = false; $parts = explode(';', $cookieStr); // If first part does not include '=', fail if (strpos($parts[0], '=') === false) { return false; } // Get the name and value of the cookie list($name, $value) = explode('=', trim(array_shift($parts)), 2); $name = trim($name); $value = urldecode(trim($value)); // Set default domain and path if ($ref_uri instanceof Microsoft_Uri_Http) { $domain = $ref_uri->getHost(); $path = $ref_uri->getPath(); $path = substr($path, 0, strrpos($path, '/')); } // Set other cookie parameters foreach ($parts as $part) { $part = trim($part); if (strtolower($part) == 'secure') { $secure = true; continue; } $keyValue = explode('=', $part, 2); if (count($keyValue) == 2) { list($k, $v) = $keyValue; switch (strtolower($k)) { case 'expires': if (($expires = strtotime($v)) === false) { /** * The expiration is past Tue, 19 Jan 2038 03:14:07 UTC * the maximum for 32-bit signed integer. Microsoft_Date * can get around that limit. * * @see Microsoft_Date */ require_once 'Microsoft/Date.php'; $expireDate = new Microsoft_Date($v); $expires = $expireDate->getTimestamp(); } break; case 'path': $path = $v; break; case 'domain': $domain = $v; break; default: break; } } } if ($name !== '') { return new self($name, $value, $domain, $expires, $path, $secure); } else { return false; } }