<?php
/**
 * Simple extension for the Zend Framework Zend_Controller_Response_Http.
 *
 * @package MKLib
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */
 
/**
 * Extension for the Zend_Controller_Response_Http, added support for the
 * cookies.
 *
 * Be aware, that when we redirecting via some redirector front controller
 * plugin, response object isn't observed about it and is destroyed.
 *
 * @package MKLib
 * @author Miroslav Kubelik (koubel@volny.cz)
 * @copyright Copyright (c) 2007-2009 Miroslav Kubelik
 * @license http://opensource.org/licenses/gpl-license.php GNU GPL
 */
class MKLib_HttpResponse extends Zend_Controller_Response_Http
{
    /**
     * False cookie domain flag, must be used for domain like localhost or
     * domains with one dot.
     */
    const FALSE_DOMAIN = "00";

    /**
     * Response cookies
     * @var array
     */
    private $_cookies = array();

    /**
     * Adds cookie into response.
     * @param Zend_Http_Cookie $cookie new cookie
     */
    public function addCookie(Zend_Http_Cookie $cookie)
    {
        if (!$this->_cookieExist($cookie)) {
            $this->_cookies[] = $cookie;
        }
    }

    /**
     * Removes particular cookie if is already registred.
     * @param Zend_Http_Cookie $cookie cookie for a removing
     */
    public function removeCookie(Zend_Http_Cookie $cookie)
    {
        foreach($this->_cookies as $index => $cookieVal) {
            if (self::_isEqualCookie($cookieVal, $cookie)) {
                unset($this->_cookies[$index]);
                return;
            }
        }
    }

    /**
     * Checks if particular cookie is already in registred in the response.
     * @param Zend_Http_Cookie $testCookie tested cookie
     */
    private function _cookieExist(Zend_Http_Cookie $testCookie)
    {
        foreach($this->_cookies as $cookie) {
            if (self::_isEqualCookie($cookie, $testCookie)) return true;
        }
        return false;
    }

    /**
     * Checks cookie equivalency.
     * Cookies are equivalent when have same
     *  - name
     *  - value
     *  - domain
     */
    private static function _isEqualCookie(Zend_Http_Cookie $cookieA, Zend_Http_Cookie $cookieB)
    {
        return ($cookieA->getName() == $cookieB->getName() 
            && $cookieA->getValue() == $cookieB->getValue() 
            && $cookieA->getDomain() == $cookieB->getDomain()
        );
    }

    /**
     * Overloaded method, is invoked when the response is send into client.
     * Be aware, that when we redirecting via some redirector front controller
     * plugin, response object isn't observed about this and is destroyed. When we
     * can send some output (mostly cookies, we need call methods by hand)
     */
    public function sendResponse()
    {
        $this->sendCookies();
        parent::sendResponse();
    }

    /**
     * Sends cookies registred with the response. Uses PHP setCookie function.
     */
    public function sendCookies()
    {
        foreach($this->_cookies as $cookie) {
            $domain = $cookie->getDomain() == self::FALSE_DOMAIN ? false : $cookie->getDomain();
            setCookie($cookie->getName(), $cookie->getValue(), 
                $cookie->getExpiryTime(), $cookie->getPath(), 
                $domain, $cookie->isSecure()
            );
        }
    }
}
