[ Index ] |
|
Code source de PRADO 3.0.6 |
1 <?php 2 /** 3 * THttpResponse class 4 * 5 * @author Qiang Xue <qiang.xue@gmail.com> 6 * @link http://www.pradosoft.com/ 7 * @copyright Copyright © 2005 PradoSoft 8 * @license http://www.pradosoft.com/license/ 9 * @version $Id: THttpResponse.php 1508 2006-11-25 20:42:54Z xue $ 10 * @package System.Web 11 */ 12 13 /** 14 * THttpResponse class 15 * 16 * THttpResponse implements the mechanism for sending output to client users. 17 * 18 * To output a string to client, use {@link write()}. By default, the output is 19 * buffered until {@link flush()} is called or the application ends. The output in 20 * the buffer can also be cleaned by {@link clear()}. To disable output buffering, 21 * set BufferOutput property to false. 22 * 23 * To send cookies to client, use {@link getCookies()}. 24 * To redirect client browser to a new URL, use {@link redirect()}. 25 * To send a file to client, use {@link writeFile()}. 26 * 27 * By default, THttpResponse is registered with {@link TApplication} as the 28 * response module. It can be accessed via {@link TApplication::getResponse()}. 29 * 30 * THttpResponse may be configured in application configuration file as follows 31 * 32 * <module id="response" CacheExpire="20" CacheControl="nocache" BufferOutput="true" /> 33 * 34 * where {@link getCacheExpire CacheExpire}, {@link getCacheControl CacheControl} 35 * and {@link getBufferOutput BufferOutput} are optional properties of THttpResponse. 36 * 37 * THttpResponse sends charset header if either {@link setCharset() Charset} 38 * or {@link TGlobalization::setCharset() TGlobalization.Charset} is set. 39 * 40 * @author Qiang Xue <qiang.xue@gmail.com> 41 * @version $Id: THttpResponse.php 1508 2006-11-25 20:42:54Z xue $ 42 * @package System.Web 43 * @since 3.0 44 */ 45 class THttpResponse extends TModule implements ITextWriter 46 { 47 /** 48 * @var boolean whether to buffer output 49 */ 50 private $_bufferOutput=true; 51 /** 52 * @var boolean if the application is initialized 53 */ 54 private $_initialized=false; 55 /** 56 * @var THttpCookieCollection list of cookies to return 57 */ 58 private $_cookies=null; 59 /** 60 * @var integer response status code 61 */ 62 private $_status=200; 63 /** 64 * @var string HTML writer type 65 */ 66 private $_htmlWriterType='System.Web.UI.THtmlWriter'; 67 /** 68 * @var string content type 69 */ 70 private $_contentType=null; 71 /** 72 * @var string character set, e.g. UTF-8 73 */ 74 private $_charset=''; 75 76 /** 77 * Destructor. 78 * Flushes any existing content in buffer. 79 */ 80 public function __destruct() 81 { 82 if($this->_bufferOutput) 83 @ob_end_flush(); 84 } 85 86 /** 87 * Initializes the module. 88 * This method is required by IModule and is invoked by application. 89 * It starts output buffer if it is enabled. 90 * @param TXmlElement module configuration 91 */ 92 public function init($config) 93 { 94 if($this->_bufferOutput) 95 ob_start(); 96 $this->_initialized=true; 97 $this->getApplication()->setResponse($this); 98 } 99 100 /** 101 * @return integer time-to-live for cached session pages in minutes, this has no effect for nocache limiter. Defaults to 180. 102 */ 103 public function getCacheExpire() 104 { 105 return session_cache_expire(); 106 } 107 108 /** 109 * @param integer time-to-live for cached session pages in minutes, this has no effect for nocache limiter. 110 */ 111 public function setCacheExpire($value) 112 { 113 session_cache_expire(TPropertyValue::ensureInteger($value)); 114 } 115 116 /** 117 * @return string cache control method to use for session pages 118 */ 119 public function getCacheControl() 120 { 121 return session_cache_limiter(); 122 } 123 124 /** 125 * @param string cache control method to use for session pages. Valid values 126 * include none/nocache/private/private_no_expire/public 127 */ 128 public function setCacheControl($value) 129 { 130 session_cache_limiter(TPropertyValue::ensureEnum($value,array('none','nocache','private','private_no_expire','public'))); 131 } 132 133 /** 134 * @return string content type, default is text/html 135 */ 136 public function setContentType($type) 137 { 138 $this->_contentType = $type; 139 } 140 141 /** 142 * @return string current content type 143 */ 144 public function getContentType() 145 { 146 return $this->_contentType; 147 } 148 149 /** 150 * @return string output charset. 151 */ 152 public function getCharset() 153 { 154 return $this->_charset; 155 } 156 157 /** 158 * @param string output charset. 159 */ 160 public function setCharset($charset) 161 { 162 $this->_charset = $charset; 163 } 164 165 /** 166 * @return boolean whether to enable output buffer 167 */ 168 public function getBufferOutput() 169 { 170 return $this->_bufferOutput; 171 } 172 173 /** 174 * @param boolean whether to enable output buffer 175 * @throws TInvalidOperationException if session is started already 176 */ 177 public function setBufferOutput($value) 178 { 179 if($this->_initialized) 180 throw new TInvalidOperationException('httpresponse_bufferoutput_unchangeable'); 181 else 182 $this->_bufferOutput=TPropertyValue::ensureBoolean($value); 183 } 184 185 /** 186 * @return integer HTTP status code, defaults to 200 187 */ 188 public function getStatusCode() 189 { 190 return $this->_status; 191 } 192 193 /** 194 * @param integer HTTP status code 195 */ 196 public function setStatusCode($status) 197 { 198 $this->_status=TPropertyValue::ensureInteger($status); 199 } 200 201 /** 202 * @return THttpCookieCollection list of output cookies 203 */ 204 public function getCookies() 205 { 206 if($this->_cookies===null) 207 $this->_cookies=new THttpCookieCollection($this); 208 return $this->_cookies; 209 } 210 211 /** 212 * Outputs a string. 213 * It may not be sent back to user immediately if output buffer is enabled. 214 * @param string string to be output 215 */ 216 public function write($str) 217 { 218 echo $str; 219 } 220 221 /** 222 * Sends a file back to user. 223 * Make sure not to output anything else after calling this method. 224 * @param string file name 225 * @param string content to be set. If null, the content will be read from the server file pointed to by $fileName. 226 * @param string mime type of the content. 227 * @param array list of headers to be sent 228 * @throws TInvalidDataValueException if the file cannot be found 229 */ 230 public function writeFile($fileName,$content=null,$mimeType=null,$headers=null) 231 { 232 static $defaultMimeTypes=array( 233 'css'=>'text/css', 234 'gif'=>'image/gif', 235 'jpg'=>'image/jpeg', 236 'jpeg'=>'image/jpeg', 237 'htm'=>'text/html', 238 'html'=>'text/html', 239 'js'=>'javascript/js' 240 ); 241 242 if($mimeType===null) 243 { 244 $mimeType='text/plain'; 245 if(function_exists('mime_content_type')) 246 $mimeType=mime_content_type($fileName); 247 else if(($ext=strrchr($fileName,'.'))!==false) 248 { 249 $ext=substr($ext,1); 250 if(isset($defaultMimeTypes[$ext])) 251 $mimeType=$defaultMimeTypes[$ext]; 252 } 253 } 254 $fn=basename($fileName); 255 if(is_array($headers)) 256 { 257 foreach($headers as $h) 258 header($h); 259 } 260 else 261 { 262 header('Pragma: public'); 263 header('Expires: 0'); 264 header('Cache-Component: must-revalidate, post-check=0, pre-check=0'); 265 } 266 header("Content-type: $mimeType"); 267 header('Content-Length: '.($content===null?filesize($fileName):strlen($content))); 268 header("Content-Disposition: attachment; filename=\"$fn\""); 269 header('Content-Transfer-Encoding: binary'); 270 if($content===null) 271 readfile($fileName); 272 else 273 echo $content; 274 } 275 276 /** 277 * Redirects the browser to the specified URL. 278 * The current application will be terminated after this method is invoked. 279 * @param string URL to be redirected to. If the URL is a relative one, the base URL of 280 * the current request will be inserted at the beginning. 281 */ 282 public function redirect($url) 283 { 284 if(!$this->getApplication()->getRequestCompleted()) 285 $this->getApplication()->onEndRequest(); 286 if($url[0]==='/') 287 $url=$this->getRequest()->getBaseUrl().$url; 288 header('Location: '.str_replace('&','&',$url)); 289 exit(); 290 } 291 292 /** 293 * Reloads the current page. 294 * The effect of this method call is the same as user pressing the 295 * refresh button on his browser (without post data). 296 **/ 297 public function reload() 298 { 299 $this->redirect($this->getRequest()->getRequestUri()); 300 } 301 302 /** 303 * Outputs the buffered content, sends content-type and charset header. 304 */ 305 public function flush() 306 { 307 Prado::trace("Flushing output",'System.Web.THttpResponse'); 308 $this->sendContentTypeHeader(); 309 if($this->_bufferOutput) 310 ob_flush(); 311 } 312 313 /** 314 * Sends content type header if charset is not empty. 315 */ 316 protected function sendContentTypeHeader() 317 { 318 $charset=$this->getCharset(); 319 if($charset==='' && ($globalization=$this->getApplication()->getGlobalization(false))!==null) 320 $charset=$globalization->getCharset(); 321 if($charset!=='') 322 { 323 $contentType=$this->_contentType===null?'text/html':$this->_contentType; 324 $this->appendHeader('Content-Type: '.$contentType.';charset='.$charset); 325 } 326 else if($this->_contentType!==null) 327 $this->appendHeader('Content-Type: '.$this->_contentType.';charset=UTF-8'); 328 } 329 330 /** 331 * Returns the content in the output buffer. 332 * The buffer will NOT be cleared after calling this method. 333 * Use {@link clear()} is you want to clear the buffer. 334 * @return string output that is in the buffer. 335 */ 336 public function getContents() 337 { 338 Prado::trace("Retrieving output",'System.Web.THttpResponse'); 339 return $this->_bufferOutput?ob_get_contents():''; 340 } 341 342 /** 343 * Clears any existing buffered content. 344 */ 345 public function clear() 346 { 347 if($this->_bufferOutput) 348 ob_clean(); 349 Prado::trace("Clearing output",'System.Web.THttpResponse'); 350 } 351 352 /** 353 * Sends a header. 354 * @param string header 355 */ 356 public function appendHeader($value) 357 { 358 Prado::trace("Sending header '$value'",'System.Web.THttpResponse'); 359 header($value); 360 } 361 362 /** 363 * Writes a log message into error log. 364 * This method is simple wrapper of PHP function error_log. 365 * @param string The error message that should be logged 366 * @param integer where the error should go 367 * @param string The destination. Its meaning depends on the message parameter as described above 368 * @param string The extra headers. It's used when the message parameter is set to 1. This message type uses the same internal function as mail() does. 369 * @see http://us2.php.net/manual/en/function.error-log.php 370 */ 371 public function appendLog($message,$messageType=0,$destination='',$extraHeaders='') 372 { 373 error_log($message,$messageType,$destination,$extraHeaders); 374 } 375 376 /** 377 * Sends a cookie. 378 * Do not call this method directly. Operate with the result of {@link getCookies} instead. 379 * @param THttpCookie cook to be sent 380 */ 381 public function addCookie($cookie) 382 { 383 $request=$this->getRequest(); 384 if($request->getEnableCookieValidation()) 385 { 386 $value=$this->getApplication()->getSecurityManager()->hashData($cookie->getValue()); 387 setcookie($cookie->getName(),$value,$cookie->getExpire(),$cookie->getPath(),$cookie->getDomain(),$cookie->getSecure()); 388 } 389 else 390 setcookie($cookie->getName(),$cookie->getValue(),$cookie->getExpire(),$cookie->getPath(),$cookie->getDomain(),$cookie->getSecure()); 391 } 392 393 /** 394 * Deletes a cookie. 395 * Do not call this method directly. Operate with the result of {@link getCookies} instead. 396 * @param THttpCookie cook to be deleted 397 */ 398 public function removeCookie($cookie) 399 { 400 setcookie($cookie->getName(),null,0,$cookie->getPath(),$cookie->getDomain(),$cookie->getSecure()); 401 } 402 403 /** 404 * @return string the type of HTML writer to be used, defaults to THtmlWriter 405 */ 406 public function getHtmlWriterType() 407 { 408 return $this->_htmlWriterType; 409 } 410 411 /** 412 * @param string the type of HTML writer to be used, may be the class name or the namespace 413 */ 414 public function setHtmlWriterType($value) 415 { 416 $this->_htmlWriterType=$value; 417 } 418 419 /** 420 * Creates a new instance of HTML writer. 421 * If the type of the HTML writer is not supplied, {@link getHtmlWriterType HtmlWriterType} will be assumed. 422 * @param string type of the HTML writer to be created. If null, {@link getHtmlWriterType HtmlWriterType} will be assumed. 423 */ 424 public function createHtmlWriter($type=null) 425 { 426 if($type===null) 427 $type=$this->_htmlWriterType; 428 return Prado::createComponent($type,$this); 429 } 430 } 431 432 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 21:07:04 2007 | par Balluche grâce à PHPXref 0.7 |