X-Git-Url: https://git.mxchange.org/?p=shipsimu.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fresponse%2Fclass_HttpResponse.php;h=869d358a49acbb44537dbaf09325b7afbdfd3471;hp=bc7693335eb858e09321834c7a58b9a06604a183;hb=a2cc983f5fe910115d25800c258875a3919fb143;hpb=1c3a46c869307f45e3c456254b086503600f8602 diff --git a/inc/classes/main/response/class_HttpResponse.php b/inc/classes/main/response/class_HttpResponse.php index bc76933..869d358 100644 --- a/inc/classes/main/response/class_HttpResponse.php +++ b/inc/classes/main/response/class_HttpResponse.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software + * @copyright Copyright (c) 2007, 2008 Roland Haeder, this is free software * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -35,6 +35,11 @@ class HttpResponse extends BaseFrameworkSystem implements Responseable { */ private $responseHeaders = array(); + /** + * Cookies we shall sent out + */ + private $cookies = array(); + /** * Body of the response */ @@ -43,7 +48,7 @@ class HttpResponse extends BaseFrameworkSystem implements Responseable { /** * Instance of the template engine */ - private $templateEngine = null; + private $templateInstance = null; /** * Fatal resolved messages from filters and so on @@ -59,12 +64,6 @@ class HttpResponse extends BaseFrameworkSystem implements Responseable { // Call parent constructor parent::__construct(__CLASS__); - // Set part description - $this->setObjectDescription("HTTP response"); - - // Create unique ID number - $this->generateUniqueId(); - // Clean up a little $this->removeNumberFormaters(); $this->removeSystemArray(); @@ -143,7 +142,7 @@ class HttpResponse extends BaseFrameworkSystem implements Responseable { /** * Flushs the cached HTTP response to the outer world * - * @param $foce Wether we shall force the output or abort if headers are + * @param $force Wether we shall force the output or abort if headers are * already sent with an exception * @return void * @throws ResponseHeadersAlreadySentException Thrown if headers are @@ -163,15 +162,25 @@ class HttpResponse extends BaseFrameworkSystem implements Responseable { // General header for no caching $this->addHeader('Expired', $now); // rfc2616 - Section 14.21 $this->addHeader('Last-Modified', $now); - $this->addHeader('Cache-Control:', 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0'); // HTTP/1.1 - $this->addHeader('Pragma:', 'no-cache'); // HTTP/1.0 + $this->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0'); // HTTP/1.1 + $this->addHeader('Pragma', 'no-cache'); // HTTP/1.0 // Define the charset to be used //$this->addHeader('Content-Type:', sprintf("text/html; charset=%s", $this->getConfigInstance()->readConfig('header_charset'))); foreach ($this->responseHeaders as $name=>$value) { header("{$name}: {$value}"); - } + } // END - foreach + + // Send cookies out? + if (count($this->cookies) > 0) { + // Send all cookies + $cookieString = implode(" ", $this->cookies); + header("Set-Cookie: {$cookieString}"); + + // Remove them all + $this->cookies = array(); + } // END - if } // Are there some error messages? @@ -198,16 +207,7 @@ class HttpResponse extends BaseFrameworkSystem implements Responseable { * @return void */ public final function initTemplateEngine (ManageableApplication $appInstance) { - $this->templateEngine = $this->prepareTemplateEngine($appInstance); - } - - /** - * Getter for the template engine instance - * - * @return $templateEngine An instance of the used template engine - */ - public final function getTemplateEngine () { - return $this->templateEngine; + $this->setTemplateInstance($this->prepareTemplateInstance($appInstance)); } /** @@ -228,30 +228,49 @@ class HttpResponse extends BaseFrameworkSystem implements Responseable { * @param $cookieName Cookie's name * @param $cookieValue Value to store in the cookie * @param $encrypted Do some extra encryption on the value + * @param $expires Timestamp of expiration (default: configured) * @return void * @throws ResponseHeadersAlreadySentException If headers are already sent + * @todo Encryption of cookie data not yet supported. + * @todo Why are these parameters conflicting? + * @todo If the return statement is removed and setcookie() commented out, + * @todo this will send only one cookie out, the first one. */ - public function addCookie ($cookieName, $cookieValue, $encrypted = false) { + public function addCookie ($cookieName, $cookieValue, $encrypted = false, $expires = null) { + //* DEBUG: */ echo $cookieName."=".$cookieValue."
\n"; // Are headers already sent? if (headers_sent()) { // Throw an exception here + //* DEBUG: */ return; throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT); } // END - if // Shall we encrypt the cookie? if ($encrypted === true) { - // @TODO Encryption of cookie data not yet supported } // END - if - // Set the cookie - setcookie( - $cookieName, - $cookieValue, - $this->getConfigInstance()->readConfig('cookie_expire'), - $this->getConfigInstance()->readConfig('cookie_path'), - $this->getConfigInstance()->readConfig('cookie_domain'), - $this->getConfigInstance()->readConfig('cookie_ssl') - ); + // For slow browsers set the cookie array element first + $_COOKIE[$cookieName] = $cookieValue; + + // Get all config entries + if (is_null($expires)) { + $expires = (time() + $this->getConfigInstance()->readConfig('cookie_expire')); + } // END - if + + $path = $this->getConfigInstance()->readConfig('cookie_path'); + $domain = $this->getConfigInstance()->readConfig('cookie_domain'); + + setcookie($cookieName, $cookieValue, $expires); + //, $path, $domain, (isset($_SERVER['HTTPS'])) + return; + + // Now construct the full header + $cookieString = $cookieName . "=" . $cookieValue . "; "; + $cookieString .= "expires=" . date("D, d-F-Y H:i:s", $expires) . " GMT"; + // $cookieString .= "; path=".$path."; domain=".$domain; + + // Set the cookie as a header + $this->cookies[$cookieName] = $cookieString; } /** @@ -281,9 +300,6 @@ class HttpResponse extends BaseFrameworkSystem implements Responseable { $url = $this->getConfigInstance()->readConfig('base_url') . "/" . $url; } // END - if - // Clean response headers - $this->resetResponseHeaders(); - // Add redirect header $this->addHeader("Location", $url); @@ -299,6 +315,47 @@ class HttpResponse extends BaseFrameworkSystem implements Responseable { // All done here... exit(); } + + /** + * Expires the given cookie if it is set + * + * @param $cookieName Cookie to expire + * @return void + */ + public function expireCookie ($cookieName) { + // Is the cookie there? + if (isset($_COOKIE[$cookieName])) { + // Then expire it with 20 minutes past + $this->addCookie($cookieName, "", false, (time() - 1200)); + + // Remove it from array + unset($_COOKIE[$cookieName]); + } // END - if + } + + /** + * Refreshs a given cookie. This will make the cookie live longer + * + * @param $cookieName Cookie to refresh + * @return void + */ + public function refreshCookie ($cookieName) { + // Only update existing cookies + if (isset($_COOKIE[$cookieName])) { + // Update the cookie + $this->addCookie($cookieName, $_COOKIE[$cookieName], false); + } // END - if + } + + /** + * Getter for default command + * + * @return $defaultCommand Default command for this response + */ + public function getDefaultCommand () { + $defaultCommand = $this->getConfigInstance()->readConfig('default_web_command'); + return $defaultCommand; + } } // [EOF]