class_ImageResponse.php

Go to the documentation of this file.
00001 <?php
00027 class ImageResponse extends BaseResponse implements Responseable {
00031         private $imageInstance = null;
00032 
00038         protected function __construct () {
00039                 // Call parent constructor
00040                 parent::__construct(__CLASS__);
00041         }
00042 
00049         public final static function createImageResponse (ManageableApplication $appInstance) {
00050                 // Get a new instance
00051                 $responseInstance = new ImageResponse();
00052 
00053                 // Set the application instance
00054                 $responseInstance->setApplicationInstance($appInstance);
00055 
00056                 // Initialize the template engine here
00057                 $responseInstance->initTemplateEngine($appInstance);
00058 
00059                 // Return the prepared instance
00060                 return $responseInstance;
00061         }
00062 
00069         public final function initTemplateEngine (ManageableApplication $appInstance) {
00070                 // Get config instance
00071                 $cfg = $this->getConfigInstance();
00072 
00073                 // Set new template engine
00074                 $cfg->setConfigEntry('template_class'         , $cfg->readConfig('image_template_class'));
00075                 $cfg->setConfigEntry('raw_template_extension' , ".img");
00076                 $cfg->setConfigEntry('code_template_extension', ".itp");
00077                 $cfg->setConfigEntry('tpl_base_path'          , "templates/images/");
00078                 $cfg->setConfigEntry('code_template_type'     , "image");
00079 
00080                 // Get a prepared instance
00081                 $this->setTemplateInstance($this->prepareTemplateInstance($appInstance));
00082         }
00083 
00098         public function addCookie ($cookieName, $cookieValue, $encrypted = false, $expires = null) {
00099                 // Are headers already sent?
00100                 if (headers_sent()) {
00101                         // Throw an exception here
00102                         throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
00103                 } // END - if
00104 
00105                 // Shall we encrypt the cookie?
00106                 if ($encrypted === true) {
00107                 } // END - if
00108 
00109                 // For slow browsers set the cookie array element first
00110                 $_COOKIE[$cookieName] = $cookieValue;
00111 
00112                 // Get all config entries
00113                 if (is_null($expires)) {
00114                         $expires = (time() + $this->getConfigInstance()->readConfig('cookie_expire'));
00115                 } // END - if
00116 
00117                 $path = $this->getConfigInstance()->readConfig('cookie_path');
00118                 $domain = $this->getConfigInstance()->readConfig('cookie_domain');
00119 
00120                 setcookie($cookieName, $cookieValue, $expires);
00121                 //, $path, $domain, (isset($_SERVER['HTTPS']))
00122                 return;
00123 
00124                 // Now construct the full header
00125                 $cookieString = $cookieName . "=" . $cookieValue . "; ";
00126                 $cookieString .= "expires=" . date("D, d-F-Y H:i:s", $expires) . " GMT";
00127                 // $cookieString .= "; path=".$path."; domain=".$domain;
00128 
00129                 // Set the cookie as a header
00130                 $this->cookies[$cookieName] = $cookieString;
00131         }
00132 
00141         public function redirectToConfiguredUrl ($configEntry) {
00142                 // Is the header not yet sent?
00143                 if (headers_sent()) {
00144                         // Throw an exception here
00145                         throw new ResponseHeadersAlreadySentException($this, self::EXCEPTION_HEADERS_ALREADY_SENT);
00146                 } // END - if
00147 
00148                 // Assign application data
00149                 $this->getTemplateInstance()->assignApplicationData($this->getApplicationInstance());
00150 
00151                 // Get the url from config
00152                 $url = $this->getConfigInstance()->readConfig($configEntry);
00153 
00154                 // Compile the URL
00155                 $url = $this->getTemplateInstance()->compileRawCode($url);
00156 
00157                 // Do we have a "http" in front of the URL?
00158                 if (substr(strtolower($url), 0, 4) != "http") {
00159                         // Is there a / in front of the relative URL?
00160                         if (substr($url, 0, 1) == "/") $url = substr($url, 1);
00161 
00162                         // No, then extend it with our base URL
00163                         $url = $this->getConfigInstance()->readConfig('base_url') . "/" . $url;
00164                 } // END - if
00165 
00166                 // Add redirect header
00167                 $this->addHeader("Location", $url);
00168 
00169                 // Set correct response status
00170                 $this->setResponseStatus("301 Moved Permanently");
00171 
00172                 // Clear the body
00173                 $this->setResponseBody("");
00174 
00175                 // Flush the result
00176                 $this->flushBuffer();
00177 
00178                 // All done here...
00179                 exit();
00180         }
00181 
00189         public function flushBuffer ($force = false) {
00190                 // Finish the image
00191                 $this->getImageInstance()->finishImage();
00192 
00193                 // Get image content
00194                 $content = $this->getImageInstance()->getContent();
00195 
00196                 // Set it as response body
00197                 $this->setResponseBody($content);
00198 
00199                 // Set content type
00200                 $this->addHeader('Content-type', "image/".$this->getImageInstance()->getImageType());
00201 
00202                 // Call parent method
00203                 parent::flushBuffer($force);
00204         }
00205 
00212         public function expireCookie ($cookieName) {
00213                 // Is the cookie there?
00214                 if (isset($_COOKIE[$cookieName])) {
00215                         // Then expire it with 20 minutes past
00216                         $this->addCookie($cookieName, "", false, (time() - 1200));
00217 
00218                         // Remove it from array
00219                         unset($_COOKIE[$cookieName]);
00220                 } // END - if
00221         }
00222 
00229         public function refreshCookie ($cookieName) {
00230                 // Only update existing cookies
00231                 if (isset($_COOKIE[$cookieName])) {
00232                         // Update the cookie
00233                         $this->addCookie($cookieName, $_COOKIE[$cookieName], false);
00234                 } // END - if
00235         }
00236 
00243         public final function setImageInstance (BaseImage $imageInstance) {
00244                 $this->imageInstance = $imageInstance;
00245         }
00246 
00252         public final function getImageInstance () {
00253                 return $this->imageInstance;
00254         }
00255 
00261         public function getDefaultCommand () {
00262                 $defaultCommand = $this->getConfigInstance()->readConfig('default_image_command');
00263                 return $defaultCommand;
00264         }
00265 }
00266 
00267 // [EOF]
00268 ?>

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