| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | Prado::using('System.Web.THttpResponseAdapter'); |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | class THttpResponse extends TModule implements ITextWriter |
|---|
| 68 | { |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | private static $HTTP_STATUS_CODES = array( |
|---|
| 73 | 100 => 'Continue', 101 => 'Switching Protocols', |
|---|
| 74 | 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', |
|---|
| 75 | 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect', |
|---|
| 76 | 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Time-out', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Large', 415 => 'Unsupported Media Type', 416 => 'Requested range not satisfiable', 417 => 'Expectation Failed', |
|---|
| 77 | 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Time-out', 505 => 'HTTP Version not supported' |
|---|
| 78 | ); |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | private $_bufferOutput=true; |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | private $_initialized=false; |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | private $_cookies=null; |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | private $_status=200; |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | private $_reason='OK'; |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | private $_htmlWriterType='System.Web.UI.THtmlWriter'; |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | private $_contentType=null; |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | private $_charset=''; |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | private $_adapter; |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | public function __destruct() |
|---|
| 122 | { |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | public function setAdapter(THttpResponseAdapter $adapter) |
|---|
| 131 | { |
|---|
| 132 | $this->_adapter=$adapter; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | public function getAdapter() |
|---|
| 139 | { |
|---|
| 140 | return $this->_adapter; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | public function getHasAdapter() |
|---|
| 147 | { |
|---|
| 148 | return !is_null($this->_adapter); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | public function init($config) |
|---|
| 158 | { |
|---|
| 159 | if($this->_bufferOutput) |
|---|
| 160 | ob_start(); |
|---|
| 161 | $this->_initialized=true; |
|---|
| 162 | $this->getApplication()->setResponse($this); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | public function getCacheExpire() |
|---|
| 169 | { |
|---|
| 170 | return session_cache_expire(); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | public function setCacheExpire($value) |
|---|
| 177 | { |
|---|
| 178 | session_cache_expire(TPropertyValue::ensureInteger($value)); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | public function getCacheControl() |
|---|
| 185 | { |
|---|
| 186 | return session_cache_limiter(); |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | public function setCacheControl($value) |
|---|
| 194 | { |
|---|
| 195 | session_cache_limiter(TPropertyValue::ensureEnum($value,array('none','nocache','private','private_no_expire','public'))); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | public function setContentType($type) |
|---|
| 202 | { |
|---|
| 203 | $this->_contentType = $type; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | public function getContentType() |
|---|
| 210 | { |
|---|
| 211 | return $this->_contentType; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | public function getCharset() |
|---|
| 218 | { |
|---|
| 219 | return $this->_charset; |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | public function setCharset($charset) |
|---|
| 226 | { |
|---|
| 227 | $this->_charset = $charset; |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | public function getBufferOutput() |
|---|
| 234 | { |
|---|
| 235 | return $this->_bufferOutput; |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | public function setBufferOutput($value) |
|---|
| 243 | { |
|---|
| 244 | if($this->_initialized) |
|---|
| 245 | throw new TInvalidOperationException('httpresponse_bufferoutput_unchangeable'); |
|---|
| 246 | else |
|---|
| 247 | $this->_bufferOutput=TPropertyValue::ensureBoolean($value); |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | public function getStatusCode() |
|---|
| 254 | { |
|---|
| 255 | return $this->_status; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | public function setStatusCode($status, $reason=null) |
|---|
| 267 | { |
|---|
| 268 | $status=TPropertyValue::ensureInteger($status); |
|---|
| 269 | if(isset(self::$HTTP_STATUS_CODES[$status])) { |
|---|
| 270 | $this->_reason=self::$HTTP_STATUS_CODES[$status]; |
|---|
| 271 | }else{ |
|---|
| 272 | if($reason===null || $reason==='') { |
|---|
| 273 | throw new TInvalidDataValueException("response_status_reason_missing"); |
|---|
| 274 | } |
|---|
| 275 | $reason=TPropertyValue::ensureString($reason); |
|---|
| 276 | if(strpos($reason, "\r")!=false || strpos($reason, "\n")!=false) { |
|---|
| 277 | throw new TInvalidDataValueException("response_status_reason_barchars"); |
|---|
| 278 | } |
|---|
| 279 | $this->_reason=$reason; |
|---|
| 280 | } |
|---|
| 281 | $this->_status=$status; |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | public function getStatusReason() { |
|---|
| 288 | return $this->_reason; |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | public function getCookies() |
|---|
| 295 | { |
|---|
| 296 | if($this->_cookies===null) |
|---|
| 297 | $this->_cookies=new THttpCookieCollection($this); |
|---|
| 298 | return $this->_cookies; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | public function write($str) |
|---|
| 307 | { |
|---|
| 308 | echo $str; |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | |
|---|
| 320 | public function writeFile($fileName,$content=null,$mimeType=null,$headers=null) |
|---|
| 321 | { |
|---|
| 322 | static $defaultMimeTypes=array( |
|---|
| 323 | 'css'=>'text/css', |
|---|
| 324 | 'gif'=>'image/gif', |
|---|
| 325 | 'jpg'=>'image/jpeg', |
|---|
| 326 | 'jpeg'=>'image/jpeg', |
|---|
| 327 | 'htm'=>'text/html', |
|---|
| 328 | 'html'=>'text/html', |
|---|
| 329 | 'js'=>'javascript/js', |
|---|
| 330 | 'pdf'=>'application/pdf', |
|---|
| 331 | 'xls'=>'application/vnd.ms-excel', |
|---|
| 332 | ); |
|---|
| 333 | |
|---|
| 334 | if($mimeType===null) |
|---|
| 335 | { |
|---|
| 336 | $mimeType='text/plain'; |
|---|
| 337 | if(function_exists('mime_content_type')) |
|---|
| 338 | $mimeType=mime_content_type($fileName); |
|---|
| 339 | else if(($ext=strrchr($fileName,'.'))!==false) |
|---|
| 340 | { |
|---|
| 341 | $ext=substr($ext,1); |
|---|
| 342 | if(isset($defaultMimeTypes[$ext])) |
|---|
| 343 | $mimeType=$defaultMimeTypes[$ext]; |
|---|
| 344 | } |
|---|
| 345 | } |
|---|
| 346 | $fn=basename($fileName); |
|---|
| 347 | $this->sendHttpHeader(); |
|---|
| 348 | if(is_array($headers)) |
|---|
| 349 | { |
|---|
| 350 | foreach($headers as $h) |
|---|
| 351 | header($h); |
|---|
| 352 | } |
|---|
| 353 | else |
|---|
| 354 | { |
|---|
| 355 | header('Pragma: public'); |
|---|
| 356 | header('Expires: 0'); |
|---|
| 357 | header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
|---|
| 358 | } |
|---|
| 359 | header("Content-type: $mimeType"); |
|---|
| 360 | header('Content-Length: '.($content===null?filesize($fileName):strlen($content))); |
|---|
| 361 | header("Content-Disposition: attachment; filename=\"$fn\""); |
|---|
| 362 | header('Content-Transfer-Encoding: binary'); |
|---|
| 363 | if($content===null) |
|---|
| 364 | readfile($fileName); |
|---|
| 365 | else |
|---|
| 366 | echo $content; |
|---|
| 367 | } |
|---|
| 368 | |
|---|
| 369 | |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | public function redirect($url) |
|---|
| 376 | { |
|---|
| 377 | if($this->getHasAdapter()) |
|---|
| 378 | $this->_adapter->httpRedirect($url); |
|---|
| 379 | else |
|---|
| 380 | $this->httpRedirect($url); |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | |
|---|
| 384 | |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | |
|---|
| 389 | public function httpRedirect($url) |
|---|
| 390 | { |
|---|
| 391 | if(!$this->getApplication()->getRequestCompleted()) |
|---|
| 392 | $this->getApplication()->onEndRequest(); |
|---|
| 393 | if($url[0]==='/') |
|---|
| 394 | $url=$this->getRequest()->getBaseUrl().$url; |
|---|
| 395 | header('Location: '.str_replace('&','&',$url)); |
|---|
| 396 | exit(); |
|---|
| 397 | } |
|---|
| 398 | |
|---|
| 399 | |
|---|
| 400 | |
|---|
| 401 | |
|---|
| 402 | |
|---|
| 403 | |
|---|
| 404 | public function reload() |
|---|
| 405 | { |
|---|
| 406 | $this->redirect($this->getRequest()->getRequestUri()); |
|---|
| 407 | } |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | |
|---|
| 412 | public function flush() |
|---|
| 413 | { |
|---|
| 414 | if($this->getHasAdapter()) |
|---|
| 415 | $this->_adapter->flushContent(); |
|---|
| 416 | else |
|---|
| 417 | $this->flushContent(); |
|---|
| 418 | } |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | |
|---|
| 422 | |
|---|
| 423 | |
|---|
| 424 | public function flushContent() |
|---|
| 425 | { |
|---|
| 426 | Prado::trace("Flushing output",'System.Web.THttpResponse'); |
|---|
| 427 | $this->sendHttpHeader(); |
|---|
| 428 | $this->sendContentTypeHeader(); |
|---|
| 429 | if($this->_bufferOutput) |
|---|
| 430 | ob_flush(); |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | |
|---|
| 434 | |
|---|
| 435 | |
|---|
| 436 | protected function sendHttpHeader () |
|---|
| 437 | { |
|---|
| 438 | if (($version=$this->getRequest()->getHttpProtocolVersion())==='') |
|---|
| 439 | header (' ', true, $this->_status); |
|---|
| 440 | else |
|---|
| 441 | header($version.' '.$this->_status.' '.$this->_reason, true, $this->_status); |
|---|
| 442 | } |
|---|
| 443 | |
|---|
| 444 | |
|---|
| 445 | |
|---|
| 446 | |
|---|
| 447 | protected function sendContentTypeHeader() |
|---|
| 448 | { |
|---|
| 449 | $charset=$this->getCharset(); |
|---|
| 450 | if($charset==='' && ($globalization=$this->getApplication()->getGlobalization(false))!==null) |
|---|
| 451 | $charset=$globalization->getCharset(); |
|---|
| 452 | if($charset!=='') |
|---|
| 453 | { |
|---|
| 454 | $contentType=$this->_contentType===null?'text/html':$this->_contentType; |
|---|
| 455 | $this->appendHeader('Content-Type: '.$contentType.';charset='.$charset); |
|---|
| 456 | } |
|---|
| 457 | else if($this->_contentType!==null) |
|---|
| 458 | $this->appendHeader('Content-Type: '.$this->_contentType.';charset=UTF-8'); |
|---|
| 459 | } |
|---|
| 460 | |
|---|
| 461 | |
|---|
| 462 | |
|---|
| 463 | |
|---|
| 464 | |
|---|
| 465 | |
|---|
| 466 | |
|---|
| 467 | public function getContents() |
|---|
| 468 | { |
|---|
| 469 | Prado::trace("Retrieving output",'System.Web.THttpResponse'); |
|---|
| 470 | return $this->_bufferOutput?ob_get_contents():''; |
|---|
| 471 | } |
|---|
| 472 | |
|---|
| 473 | |
|---|
| 474 | |
|---|
| 475 | |
|---|
| 476 | public function clear() |
|---|
| 477 | { |
|---|
| 478 | if($this->_bufferOutput) |
|---|
| 479 | ob_clean(); |
|---|
| 480 | Prado::trace("Clearing output",'System.Web.THttpResponse'); |
|---|
| 481 | } |
|---|
| 482 | |
|---|
| 483 | |
|---|
| 484 | |
|---|
| 485 | |
|---|
| 486 | |
|---|
| 487 | public function appendHeader($value) |
|---|
| 488 | { |
|---|
| 489 | Prado::trace("Sending header '$value'",'System.Web.THttpResponse'); |
|---|
| 490 | header($value); |
|---|
| 491 | } |
|---|
| 492 | |
|---|
| 493 | |
|---|
| 494 | |
|---|
| 495 | |
|---|
| 496 | |
|---|
| 497 | |
|---|
| 498 | |
|---|
| 499 | |
|---|
| 500 | |
|---|
| 501 | |
|---|
| 502 | public function appendLog($message,$messageType=0,$destination='',$extraHeaders='') |
|---|
| 503 | { |
|---|
| 504 | error_log($message,$messageType,$destination,$extraHeaders); |
|---|
| 505 | } |
|---|
| 506 | |
|---|
| 507 | |
|---|
| 508 | |
|---|
| 509 | |
|---|
| 510 | |
|---|
| 511 | |
|---|
| 512 | public function addCookie($cookie) |
|---|
| 513 | { |
|---|
| 514 | $request=$this->getRequest(); |
|---|
| 515 | if($request->getEnableCookieValidation()) |
|---|
| 516 | { |
|---|
| 517 | $value=$this->getApplication()->getSecurityManager()->hashData($cookie->getValue()); |
|---|
| 518 | setcookie($cookie->getName(),$value,$cookie->getExpire(),$cookie->getPath(),$cookie->getDomain(),$cookie->getSecure()); |
|---|
| 519 | } |
|---|
| 520 | else |
|---|
| 521 | setcookie($cookie->getName(),$cookie->getValue(),$cookie->getExpire(),$cookie->getPath(),$cookie->getDomain(),$cookie->getSecure()); |
|---|
| 522 | } |
|---|
| 523 | |
|---|
| 524 | |
|---|
| 525 | |
|---|
| 526 | |
|---|
| 527 | |
|---|
| 528 | |
|---|
| 529 | public function removeCookie($cookie) |
|---|
| 530 | { |
|---|
| 531 | setcookie($cookie->getName(),null,0,$cookie->getPath(),$cookie->getDomain(),$cookie->getSecure()); |
|---|
| 532 | } |
|---|
| 533 | |
|---|
| 534 | |
|---|
| 535 | |
|---|
| 536 | |
|---|
| 537 | public function getHtmlWriterType() |
|---|
| 538 | { |
|---|
| 539 | return $this->_htmlWriterType; |
|---|
| 540 | } |
|---|
| 541 | |
|---|
| 542 | |
|---|
| 543 | |
|---|
| 544 | |
|---|
| 545 | public function setHtmlWriterType($value) |
|---|
| 546 | { |
|---|
| 547 | $this->_htmlWriterType=$value; |
|---|
| 548 | } |
|---|
| 549 | |
|---|
| 550 | |
|---|
| 551 | |
|---|
| 552 | |
|---|
| 553 | |
|---|
| 554 | |
|---|
| 555 | public function createHtmlWriter($type=null) |
|---|
| 556 | { |
|---|
| 557 | if($type===null) |
|---|
| 558 | $type=$this->getHtmlWriterType(); |
|---|
| 559 | if($this->getHasAdapter()) |
|---|
| 560 | return $this->_adapter->createNewHtmlWriter($type, $this); |
|---|
| 561 | else |
|---|
| 562 | return $this->createNewHtmlWriter($type, $this); |
|---|
| 563 | } |
|---|
| 564 | |
|---|
| 565 | |
|---|
| 566 | |
|---|
| 567 | |
|---|
| 568 | |
|---|
| 569 | |
|---|
| 570 | |
|---|
| 571 | public function createNewHtmlWriter($type, $writer) |
|---|
| 572 | { |
|---|
| 573 | return Prado::createComponent($type, $writer); |
|---|
| 574 | } |
|---|
| 575 | } |
|---|
| 576 | |
|---|
| 577 | ?> |
|---|