/** * Get a specific cookie according to a URI and name * * @param Zend_Uri_Http|string $uri The uri (domain and path) to match * @param string $cookie_name The cookie's name * @param int $ret_as Whether to return cookies as objects of Zend_Http_Cookie or as strings * @return Zend_Http_Cookie|string */ public function getCookie($uri, $cookie_name, $ret_as = self::COOKIE_OBJECT) { if (is_string($uri)) { if (($parsed = parse_url($uri)) !== false) { $uri = Sabel_Http_Uri::fromArray($parsed); } } if (!$uri instanceof Sabel_Http_Uri) { $message = __METHOD__ . "() Invalid URI specified."; throw new Sabel_Exception_Runtime($message); } // Get correct cookie path $path = $uri->path; $path = substr($path, 0, strrpos($path, "/")); if (!$path) { $path = "/"; } if (isset($this->cookies[$uri->host][$path][$cookie_name])) { $cookie = $this->cookies[$uri->host][$path][$cookie_name]; switch ($ret_as) { case self::COOKIE_OBJECT: return $cookie; case self::COOKIE_STRING_ARRAY: case self::COOKIE_STRING_CONCAT: return $cookie->__toString(); default: $message = "Invalid value passed for \$ret_as: {$ret_as}"; throw new Sabel_Exception_Runtime($message); } } else { return false; } }
/** * Set the URI for the next request * * @param Zend_Uri_Http|string $uri * @return Zend_Http_Client * @throws Zend_Http_Client_Exception */ public function setUri($uri) { if (is_string($uri)) { if (($parsed = parse_url($uri)) !== false) { $uri = Sabel_Http_Uri::fromArray($parsed); } } if (!$uri instanceof Sabel_Http_Uri) { $message = __METHOD__ . "() Passed parameter is not a valid URI."; throw new Sabel_Exception_InvalidArgument($message); } if ($uri->scheme !== "http" && $uri->scheme !== "https") { $message = __METHOD__ . "() Passed parameter is not a valid HTTP URI."; throw new Sabel_Exception_InvalidArgument($message); } if (empty($uri->port)) { $uri->port = $uri->scheme === "http" ? 80 : 443; } $this->uri = $uri; return $this; }
/** * Generate a new Cookie object from a cookie string * (for example the value of the Set-Cookie HTTP header) * * @param string $cookieStr * @param Zend_Uri_Http|string $ref_uri Reference URI for default values (domain, path) * @return Zend_Http_Cookie A new Zend_Http_Cookie object or false on failure. */ public static function fromString($cookieStr, $ref_uri = null) { if (is_string($ref_uri)) { if (($parsed = parse_url($ref_uri)) !== false) { $ref_uri = Sabel_Http_Uri::fromArray($parsed); } } $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 Sabel_Http_Uri) { $domain = $ref_uri->host; $path = $ref_uri->path; $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. Zend_Date * can get around that limit. * * @see Zend_Date */ //require_once 'Zend/Date.php'; //$expireDate = new Zend_Date($v); //$expires = $expireDate->getTimestamp(); } break; case "path": $path = $v; break; case "domain": $domain = $v; break; } } } if ($name !== "") { return new self($name, $value, $domain, $expires, $path, $secure); } else { return false; } }