Index: Http.php =================================================================== --- Http.php (revision 13607) +++ Http.php (working copy) @@ -22,7 +22,15 @@ /** Zend_Controller_Response_Abstract */ require_once 'Zend/Controller/Response/Abstract.php'; +/** Zend_Http_CookieJar */ +require_once 'Zend/Http/CookieJar.php'; +/** Zend_Http_Cookie */ +require_once 'Zend/Http/Cookie.php'; + +/** Zend_Controller_Front */ +require_once 'Zend_Controller/Front.php'; + /** * Zend_Controller_Response_Http * @@ -34,4 +42,141 @@ */ class Zend_Controller_Response_Http extends Zend_Controller_Response_Abstract { + /** + * cookieJar attached to the response + * + * @var Zend_Http_CookieJar + */ + protected $_cookieJar; + + /** + * Constructor + * Creates the cookieJar if it doesn't exist + */ + public function __construct() + { + $this->_cookieJar = $this->getCookieJar(); + } + + /** + * Retrives the cookieJar + * + * @return Zend_Htpp_CookieJar + */ + public function getCookieJar() + { + if ($this->_cookieJar == null) { + $this->_cookieJar = new Zend_Http_CookieJar(); + } + return $this->_cookieJar; + } + + /** + * Sets a cookieJar + * + * @param Zend_Http_CookieJar $cookieJar + * @return Zend_Controller_Response_Http + */ + public function setCookieJar(Zend_Http_CookieJar $cookieJar) + { + $this->_cookieJar = $cookieJar; + return $this; + } + + /** + * Send all headers to SAPI layer + * Appends cookies to the headers then send them + * + * @return Zend_Controller_Response_Http + */ + public function sendHeaders() + { + $this->_sendCookies(); + return parent::sendHeaders(); + } + + /** + * Adds cookies to the response object + * + * @param array $cookies + */ + public function setCookies(array $cookies) + { + foreach ($cookie as $c => $v) { + if (is_string($c)) { + $this->setCookie($c, $v); + } else { + $this->setCookie($v); + } + } + } + + /** + * Adds one cookie to the response object + * + * @param Zend_Http_Cookie|string $cookie + * @param string $value + * @return Zend_Controller_Response_Http + */ + public function setCookie($cookie, $value = null, $strict = false) + { + if ($value !== null) { + $value = addslashes(urlencode($value)); + } + + $front = Zend_Controller_Front::getInstance(); + + if ($cookie instanceof Zend_Http_Cookie) { + if ($cookie->getDomain() != $front->getRequest()->getHttpHost()) { + throw new Zend_Controller_Response_Http('Cookie domain differs from actual domain'); + } + } elseif (is_string($cookie)) { + + $cookie = $front->cookieFactory($cookie, $value); + $path = null; + if ($strict) { + $path = $front->getRequest()->getRequestUri(); + } + $cookie = $front->cookieFactory($cookie, $value, null, $path); + } else{ + throw new Zend_Controller_Response_Http('Invalid cookie value provided'); + } + $this->_cookieJar->addCookie($cookie); + return $this; + } + + /** + * Appends the cookies in the cookiejar + * to raw headers + * + */ + protected function _sendCookies() + { + if (!$this->_cookieJar->isEmpty()) { + $cookstr = $this->_cookieJar->getAllCookies(Zend_Http_CookieJar::COOKIE_STRING_CONCAT); + $this->_headersRaw[] = "Cookie: {$cookstr}"; + } + } + + /** + * Tells if the response has some cookies + * in it + * + * @return bool + */ + public function hasCookie() + { + return !$this->_cookieJar->isEmpty(); + } + + /** + * Reset the response cookies + * + * @return Zend_Controller_Http_Response + */ + public function resetCookies() + { + $this->_cookieJar->reset(); + return $this; + } }