class_HttpResponse.php

Go to the documentation of this file.
00001 <?php
00027 class HttpResponse extends BaseResponse implements Responseable {
00033         protected function __construct () {
00034                 // Call parent constructor
00035                 parent::__construct(__CLASS__);
00036         }
00037 
00044         public final static function createHttpResponse (ManageableApplication $appInstance) {
00045                 // Get a new instance
00046                 $responseInstance = new HttpResponse();
00047 
00048                 // Set the application instance
00049                 $responseInstance->setApplicationInstance($appInstance);
00050 
00051                 // Initialize the template engine here
00052                 $responseInstance->initTemplateEngine($appInstance);
00053 
00054                 // Return the prepared instance
00055                 return $responseInstance;
00056         }
00057 
00064         public final function initTemplateEngine (ManageableApplication $appInstance) {
00065                 $this->setTemplateInstance($this->prepareTemplateInstance($appInstance));
00066         }
00067 
00082         public function addCookie ($cookieName, $cookieValue, $encrypted = false, $expires = null) {
00083                 //* DEBUG: */ echo $cookieName."=".$cookieValue."<br />\n";
00084                 // Are headers already sent?
00085                 if (headers_sent()) {
00086                         // Throw an exception here
00087                         //* DEBUG: */ return;
00088                         throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
00089                 } // END - if
00090 
00091                 // Shall we encrypt the cookie?
00092                 if ($encrypted === true) {
00093                 } // END - if
00094 
00095                 // For slow browsers set the cookie array element first
00096                 $_COOKIE[$cookieName] = $cookieValue;
00097 
00098                 // Get all config entries
00099                 if (is_null($expires)) {
00100                         $expires = (time() + $this->getConfigInstance()->readConfig('cookie_expire'));
00101                 } // END - if
00102 
00103                 $path = $this->getConfigInstance()->readConfig('cookie_path');
00104                 $domain = $this->getConfigInstance()->readConfig('cookie_domain');
00105 
00106                 setcookie($cookieName, $cookieValue, $expires);
00107                 //, $path, $domain, (isset($_SERVER['HTTPS']))
00108                 return;
00109 
00110                 // Now construct the full header
00111                 $cookieString = $cookieName . "=" . $cookieValue . "; ";
00112                 $cookieString .= "expires=" . date("D, d-F-Y H:i:s", $expires) . " GMT";
00113                 // $cookieString .= "; path=".$path."; domain=".$domain;
00114 
00115                 // Set the cookie as a header
00116                 $this->cookies[$cookieName] = $cookieString;
00117         }
00118 
00127         public function redirectToConfiguredUrl ($configEntry) {
00128                 // Is the header not yet sent?
00129                 if (headers_sent()) {
00130                         // Throw an exception here
00131                         throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
00132                 } // END - if
00133 
00134                 // Assign application data
00135                 $this->getTemplateInstance()->assignApplicationData($this->getApplicationInstance());
00136 
00137                 // Get the url from config
00138                 $url = $this->getConfigInstance()->readConfig($configEntry);
00139 
00140                 // Compile the URL
00141                 $url = $this->getTemplateInstance()->compileRawCode($url);
00142 
00143                 // Do we have a "http" in front of the URL?
00144                 if (substr(strtolower($url), 0, 4) != "http") {
00145                         // Is there a / in front of the relative URL?
00146                         if (substr($url, 0, 1) == "/") $url = substr($url, 1);
00147 
00148                         // No, then extend it with our base URL
00149                         $url = $this->getConfigInstance()->readConfig('base_url') . "/" . $url;
00150                 } // END - if
00151 
00152                 // Add redirect header
00153                 $this->addHeader("Location", $url);
00154 
00155                 // Set correct response status
00156                 $this->setResponseStatus("301 Moved Permanently");
00157 
00158                 // Clear the body
00159                 $this->setResponseBody("");
00160 
00161                 // Flush the result
00162                 $this->flushBuffer();
00163 
00164                 // All done here...
00165                 exit();
00166         }
00167 
00174         public function expireCookie ($cookieName) {
00175                 // Is the cookie there?
00176                 if (isset($_COOKIE[$cookieName])) {
00177                         // Then expire it with 20 minutes past
00178                         $this->addCookie($cookieName, "", false, (time() - 1200));
00179 
00180                         // Remove it from array
00181                         unset($_COOKIE[$cookieName]);
00182                 } // END - if
00183         }
00184 
00191         public function refreshCookie ($cookieName) {
00192                 // Only update existing cookies
00193                 if (isset($_COOKIE[$cookieName])) {
00194                         // Update the cookie
00195                         $this->addCookie($cookieName, $_COOKIE[$cookieName], false);
00196                 } // END - if
00197         }
00198 
00204         public function getDefaultCommand () {
00205                 $defaultCommand = $this->getConfigInstance()->readConfig('default_web_command');
00206                 return $defaultCommand;
00207         }
00208 }
00209 
00210 // [EOF]
00211 ?>

Generated on Mon Dec 8 01:06:46 2008 for Ship-Simulator by  doxygen 1.5.6