[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /* 4 * This file is part of the symfony package. 5 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 11 /** 12 * sfWebResponse class. 13 * 14 * This class manages web reponses. It supports cookies and headers management. 15 * 16 * @package symfony 17 * @subpackage response 18 * @author Fabien Potencier <fabien.potencier@symfony-project.com> 19 * @version SVN: $Id: sfWebResponse.class.php 3250 2007-01-12 20:09:11Z fabien $ 20 */ 21 class sfWebResponse extends sfResponse 22 { 23 protected 24 $cookies = array(), 25 $statusCode = 200, 26 $statusText = 'OK', 27 $statusTexts = array(), 28 $headerOnly = false; 29 30 /** 31 * Initializes this sfWebResponse. 32 * 33 * @param sfContext A sfContext instance 34 * 35 * @return boolean true, if initialization completes successfully, otherwise false 36 * 37 * @throws <b>sfInitializationException</b> If an error occurs while initializing this Response 38 */ 39 public function initialize($context, $parameters = array()) 40 { 41 parent::initialize($context, $parameters); 42 43 if ('HEAD' == $context->getRequest()->getMethodName()) 44 { 45 $this->setHeaderOnly(true); 46 } 47 48 $this->statusTexts = array( 49 '100' => 'Continue', 50 '101' => 'Switching Protocols', 51 '200' => 'OK', 52 '201' => 'Created', 53 '202' => 'Accepted', 54 '203' => 'Non-Authoritative Information', 55 '204' => 'No Content', 56 '205' => 'Reset Content', 57 '206' => 'Partial Content', 58 '300' => 'Multiple Choices', 59 '301' => 'Moved Permanently', 60 '302' => 'Found', 61 '303' => 'See Other', 62 '304' => 'Not Modified', 63 '305' => 'Use Proxy', 64 '306' => '(Unused)', 65 '307' => 'Temporary Redirect', 66 '400' => 'Bad Request', 67 '401' => 'Unauthorized', 68 '402' => 'Payment Required', 69 '403' => 'Forbidden', 70 '404' => 'Not Found', 71 '405' => 'Method Not Allowed', 72 '406' => 'Not Acceptable', 73 '407' => 'Proxy Authentication Required', 74 '408' => 'Request Timeout', 75 '409' => 'Conflict', 76 '410' => 'Gone', 77 '411' => 'Length Required', 78 '412' => 'Precondition Failed', 79 '413' => 'Request Entity Too Large', 80 '414' => 'Request-URI Too Long', 81 '415' => 'Unsupported Media Type', 82 '416' => 'Requested Range Not Satisfiable', 83 '417' => 'Expectation Failed', 84 '500' => 'Internal Server Error', 85 '501' => 'Not Implemented', 86 '502' => 'Bad Gateway', 87 '503' => 'Service Unavailable', 88 '504' => 'Gateway Timeout', 89 '505' => 'HTTP Version Not Supported', 90 ); 91 } 92 93 /** 94 * Sets if the response consist of just HTTP headers. 95 * 96 * @param boolean 97 */ 98 public function setHeaderOnly($value = true) 99 { 100 $this->headerOnly = (boolean) $value; 101 } 102 103 /** 104 * Returns if the response must only consist of HTTP headers. 105 * 106 * @return boolean returns true if, false otherwise 107 */ 108 public function isHeaderOnly() 109 { 110 return $this->headerOnly; 111 } 112 113 /** 114 * Sets a cookie. 115 * 116 * @param string HTTP header name 117 * @param string Value for the cookie 118 * @param string Cookie expiration period 119 * @param string Path 120 * @param string Domain name 121 * @param boolean If secure 122 * @param boolean If uses only HTTP 123 * 124 * @throws <b>sfException</b> If fails to set the cookie 125 */ 126 public function setCookie($name, $value, $expire = null, $path = '/', $domain = '', $secure = false, $httpOnly = false) 127 { 128 if ($expire !== null) 129 { 130 if (is_numeric($expire)) 131 { 132 $expire = (int) $expire; 133 } 134 else 135 { 136 $expire = strtotime($expire); 137 if ($expire === false || $expire == -1) 138 { 139 throw new sfException('Your expire parameter is not valid.'); 140 } 141 } 142 } 143 144 $this->cookies[] = array( 145 'name' => $name, 146 'value' => $value, 147 'expire' => $expire, 148 'path' => $path, 149 'domain' => $domain, 150 'secure' => $secure ? true : false, 151 'httpOnly' => $httpOnly, 152 ); 153 } 154 155 /** 156 * Sets response status code. 157 * 158 * @param string HTTP status code 159 * @param string HTTP status text 160 * 161 */ 162 public function setStatusCode($code, $name = null) 163 { 164 $this->statusCode = $code; 165 $this->statusText = null !== $name ? $name : $this->statusTexts[$code]; 166 } 167 168 /** 169 * Retrieves status code for the current web response. 170 * 171 * @return string Status code 172 */ 173 public function getStatusCode() 174 { 175 return $this->statusCode; 176 } 177 178 /** 179 * Sets a HTTP header. 180 * 181 * @param string HTTP header name 182 * @param string Value 183 * @param boolean Replace for the value 184 * 185 */ 186 public function setHttpHeader($name, $value, $replace = true) 187 { 188 $name = $this->normalizeHeaderName($name); 189 190 if ('Content-Type' == $name) 191 { 192 if ($replace || !$this->getHttpHeader('Content-Type', null)) 193 { 194 $this->setContentType($value); 195 } 196 197 return; 198 } 199 200 if (!$replace) 201 { 202 $current = $this->getParameter($name, '', 'symfony/response/http/headers'); 203 $value = ($current ? $current.', ' : '').$value; 204 } 205 206 $this->setParameter($name, $value, 'symfony/response/http/headers'); 207 } 208 209 /** 210 * Gets HTTP header current value. 211 * 212 * @return array 213 */ 214 public function getHttpHeader($name, $default = null) 215 { 216 return $this->getParameter($this->normalizeHeaderName($name), $default, 'symfony/response/http/headers'); 217 } 218 219 /** 220 * Has a HTTP header. 221 * 222 * @return boolean 223 */ 224 public function hasHttpHeader($name) 225 { 226 return $this->hasParameter($this->normalizeHeaderName($name), 'symfony/response/http/headers'); 227 } 228 229 /** 230 * Sets response content type. 231 * 232 * @param string Content type 233 * 234 */ 235 public function setContentType($value) 236 { 237 // add charset if needed 238 if (false === stripos($value, 'charset')) 239 { 240 $value .= '; charset='.sfConfig::get('sf_charset'); 241 } 242 243 $this->setParameter('Content-Type', $value, 'symfony/response/http/headers'); 244 } 245 246 /** 247 * Gets response content type. 248 * 249 * @return array 250 */ 251 public function getContentType() 252 { 253 return $this->getHttpHeader('Content-Type', 'text/html; charset='.sfConfig::get('sf_charset')); 254 } 255 256 /** 257 * Send HTTP headers and cookies. 258 * 259 */ 260 public function sendHttpHeaders() 261 { 262 // status 263 $status = 'HTTP/1.0 '.$this->statusCode.' '.$this->statusText; 264 header($status); 265 266 if (sfConfig::get('sf_logging_enabled')) 267 { 268 $this->getContext()->getLogger()->info('{sfResponse} send status "'.$status.'"'); 269 } 270 271 // headers 272 foreach ($this->getParameterHolder()->getAll('symfony/response/http/headers') as $name => $value) 273 { 274 header($name.': '.$value); 275 276 if (sfConfig::get('sf_logging_enabled') && $value != '') 277 { 278 $this->getContext()->getLogger()->info('{sfResponse} send header "'.$name.'": "'.$value.'"'); 279 } 280 } 281 282 // cookies 283 foreach ($this->cookies as $cookie) 284 { 285 if (version_compare(phpversion(), '5.2', '>=')) 286 { 287 setrawcookie($cookie['name'], $cookie['value'], $cookie['expire'], $cookie['path'], $cookie['domain'], $cookie['secure'], $cookie['httpOnly']); 288 } 289 else 290 { 291 setrawcookie($cookie['name'], $cookie['value'], $cookie['expire'], $cookie['path'], $cookie['domain'], $cookie['secure']); 292 } 293 294 if (sfConfig::get('sf_logging_enabled')) 295 { 296 $this->getContext()->getLogger()->info('{sfResponse} send cookie "'.$cookie['name'].'": "'.$cookie['value'].'"'); 297 } 298 } 299 } 300 301 /** 302 * Send content for the current web response. 303 * 304 */ 305 public function sendContent() 306 { 307 if (!$this->headerOnly) 308 { 309 parent::sendContent(); 310 } 311 } 312 313 /** 314 * Retrieves a normalized Header. 315 * 316 * @param string Header name 317 * 318 * @return string Normalized header 319 */ 320 protected function normalizeHeaderName($name) 321 { 322 return preg_replace('/\-(.)/e', "'-'.strtoupper('\\1')", strtr(ucfirst(strtolower($name)), '_', '-')); 323 } 324 325 /** 326 * Retrieves a formated date. 327 * 328 * @param string Timestamp 329 * @param string Format type 330 * 331 * @return string Formated date 332 */ 333 public function getDate($timestamp, $type = 'rfc1123') 334 { 335 $type = strtolower($type); 336 337 if ($type == 'rfc1123') 338 { 339 return substr(gmdate('r', $timestamp), 0, -5).'GMT'; 340 } 341 else if ($type == 'rfc1036') 342 { 343 return gmdate('l, d-M-y H:i:s ', $timestamp).'GMT'; 344 } 345 else if ($type == 'asctime') 346 { 347 return gmdate('D M j H:i:s', $timestamp); 348 } 349 else 350 { 351 $error = 'The second getDate() method parameter must be one of: rfc1123, rfc1036 or asctime'; 352 353 throw new sfParameterException($error); 354 } 355 } 356 357 /** 358 * Adds vary to a http header. 359 * 360 * @param string HTTP header 361 */ 362 public function addVaryHttpHeader($header) 363 { 364 $vary = $this->getHttpHeader('Vary'); 365 $currentHeaders = array(); 366 if ($vary) 367 { 368 $currentHeaders = split('/\s*,\s*/', $vary); 369 } 370 $header = $this->normalizeHeaderName($header); 371 372 if (!in_array($header, $currentHeaders)) 373 { 374 $currentHeaders[] = $header; 375 $this->setHttpHeader('Vary', implode(', ', $currentHeaders)); 376 } 377 } 378 379 /** 380 * Adds an control cache http header. 381 * 382 * @param string HTTP header 383 * @param string Value for the http header 384 */ 385 public function addCacheControlHttpHeader($name, $value = null) 386 { 387 $cacheControl = $this->getHttpHeader('Cache-Control'); 388 $currentHeaders = array(); 389 if ($cacheControl) 390 { 391 foreach (split('/\s*,\s*/', $cacheControl) as $tmp) 392 { 393 $tmp = explode('=', $tmp); 394 $currentHeaders[$tmp[0]] = isset($tmp[1]) ? $tmp[1] : null; 395 } 396 } 397 $currentHeaders[strtr(strtolower($name), '_', '-')] = $value; 398 399 $headers = array(); 400 foreach ($currentHeaders as $key => $value) 401 { 402 $headers[] = $key.(null !== $value ? '='.$value : ''); 403 } 404 405 $this->setHttpHeader('Cache-Control', implode(', ', $headers)); 406 } 407 408 /** 409 * Retrieves meta headers for the current web response. 410 * 411 * @return string Meta headers 412 */ 413 public function getHttpMetas() 414 { 415 return $this->getParameterHolder()->getAll('helper/asset/auto/httpmeta'); 416 } 417 418 /** 419 * Adds meta headers to the current web response. 420 * 421 * @param string Key to replace 422 * @param string Value for the replacement 423 * @param boolean Replace or not 424 */ 425 public function addHttpMeta($key, $value, $replace = true) 426 { 427 $key = $this->normalizeHeaderName($key); 428 429 // set HTTP header 430 $this->setHttpHeader($key, $value, $replace); 431 432 if ('Content-Type' == $key) 433 { 434 $value = $this->getContentType(); 435 } 436 437 if (!$replace) 438 { 439 $current = $this->getParameter($key, '', 'helper/asset/auto/httpmeta'); 440 $value = ($current ? $current.', ' : '').$value; 441 } 442 443 $this->setParameter($key, $value, 'helper/asset/auto/httpmeta'); 444 } 445 446 /** 447 * Retrieves all meta headers for the current web response. 448 * 449 * @return array List of meta headers 450 */ 451 public function getMetas() 452 { 453 return $this->getParameterHolder()->getAll('helper/asset/auto/meta'); 454 } 455 456 /** 457 * Adds a meta header to the current web response. 458 * 459 * @param string Name of the header 460 * @param string Meta header to be set 461 * @param boolean true if it's replaceable 462 * @param boolean true for escaping the header 463 */ 464 public function addMeta($key, $value, $replace = true, $escape = true) 465 { 466 $key = strtolower($key); 467 468 if (sfConfig::get('sf_i18n')) 469 { 470 $value = $this->getContext()->getI18N()->__($value); 471 } 472 473 if ($escape) 474 { 475 $value = htmlentities($value, ENT_QUOTES, sfConfig::get('sf_charset')); 476 } 477 478 if ($replace || !$this->getParameter($key, null, 'helper/asset/auto/meta')) 479 { 480 $this->setParameter($key, $value, 'helper/asset/auto/meta'); 481 } 482 } 483 484 /** 485 * Retrieves title for the current web response. 486 * 487 * @return string Title 488 */ 489 public function getTitle() 490 { 491 return $this->getParameter('title', '', 'helper/asset/auto/meta'); 492 } 493 494 /** 495 * Sets title for the current web response. 496 * 497 * @param string Title name 498 * @param boolean true, for escaping the title 499 */ 500 public function setTitle($title, $escape = true) 501 { 502 $this->addMeta('title', $title, true, $escape); 503 } 504 505 /** 506 * Retrieves stylesheets for the current web response. 507 * 508 * @param string Direcotry delimiter 509 * 510 * @return string Stylesheets 511 */ 512 public function getStylesheets($position = '') 513 { 514 return $this->getParameterHolder()->getAll('helper/asset/auto/stylesheet'.($position ? '/'.$position : '')); 515 } 516 517 /** 518 * Adds an stylesheet to the current web response. 519 * 520 * @param string Stylesheet 521 * @param string Direcotry delimiter 522 * @param string Stylesheet options 523 */ 524 public function addStylesheet($css, $position = '', $options = array()) 525 { 526 $this->setParameter($css, $options, 'helper/asset/auto/stylesheet'.($position ? '/'.$position : '')); 527 } 528 529 /** 530 * Retrieves javascript code from the current web response. 531 * 532 * @param string Directory delimiter 533 * 534 * @return string Javascript code 535 */ 536 public function getJavascripts($position = '') 537 { 538 return $this->getParameterHolder()->getAll('helper/asset/auto/javascript'.($position ? '/'.$position : '')); 539 } 540 541 /** 542 * Adds javascript code to the current web response. 543 * 544 * @param string Javascript code 545 * @param string Directory delimiter 546 */ 547 public function addJavascript($js, $position = '') 548 { 549 $this->setParameter($js, $js, 'helper/asset/auto/javascript'.($position ? '/'.$position : '')); 550 } 551 552 /** 553 * Retrieves cookies from the current web response. 554 * 555 * @return array Cookies 556 */ 557 public function getCookies() 558 { 559 $cookies = array(); 560 foreach ($this->cookies as $cookie) 561 { 562 $cookies[$cookie['name']] = $cookie; 563 } 564 565 return $cookies; 566 } 567 568 /** 569 * Retrieves HTTP headers from the current web response. 570 * 571 * @return string HTTP headers 572 */ 573 public function getHttpHeaders() 574 { 575 return $this->getParameterHolder()->getAll('symfony/response/http/headers'); 576 } 577 578 /** 579 * Cleans HTTP headers from the current web response. 580 */ 581 public function clearHttpHeaders() 582 { 583 $this->getParameterHolder()->removeNamespace('symfony/response/http/headers'); 584 } 585 586 /** 587 * Copies a propertie to a new one. 588 * 589 * @param sfResponse Response instance 590 */ 591 public function mergeProperties($response) 592 { 593 $this->parameterHolder = clone $response->getParameterHolder(); 594 } 595 596 /** 597 * Retrieves all objects handlers for the current web response. 598 * 599 * @return array Objects instance 600 */ 601 public function __sleep() 602 { 603 return array('content', 'statusCode', 'statusText', 'parameterHolder'); 604 } 605 606 /** 607 * Reconstructs any result that web response instance needs. 608 */ 609 public function __wakeup() 610 { 611 } 612 613 /** 614 * Executes the shutdown procedure. 615 */ 616 public function shutdown() 617 { 618 } 619 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |