TYPO3 API  SVNRelease
Response.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
00006 *  All rights reserved
00007 *
00008 *  This class is a backport of the corresponding class of FLOW3.
00009 *  All credits go to the v5 team.
00010 *
00011 *  This script is part of the TYPO3 project. The TYPO3 project is
00012 *  free software; you can redistribute it and/or modify
00013 *  it under the terms of the GNU General Public License as published by
00014 *  the Free Software Foundation; either version 2 of the License, or
00015 *  (at your option) any later version.
00016 *
00017 *  The GNU General Public License can be found at
00018 *  http://www.gnu.org/copyleft/gpl.html.
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 
00028 /**
00029  * A web specific response implementation
00030  *
00031  * @package Extbase
00032  * @subpackage MVC\Web
00033  * @version $ID:$
00034  * @scope prototype
00035  * @api
00036  */
00037 class Tx_Extbase_MVC_Web_Response extends Tx_Extbase_MVC_Response {
00038 
00039     /**
00040      * The HTTP headers which will be sent in the response
00041      *
00042      * @var array
00043      */
00044     protected $headers = array();
00045 
00046     /**
00047      * Additional header tags
00048      *
00049      * @var array
00050      */
00051     protected $additionalHeaderData = array();
00052 
00053     /**
00054      * The HTTP status code
00055      *
00056      * @var integer
00057      */
00058     protected $statusCode;
00059 
00060     /**
00061      * The HTTP status message
00062      *
00063      * @var string
00064      */
00065     protected $statusMessage = 'OK';
00066 
00067     /**
00068      * The standardized and other important HTTP Status messages
00069      *
00070      * @var array
00071      */
00072     protected $statusMessages = array(
00073         100 => 'Continue',
00074         101 => 'Switching Protocols',
00075         102 => 'Processing', # RFC 2518
00076         200 => 'OK',
00077         201 => 'Created',
00078         202 => 'Accepted',
00079         203 => 'Non-Authoritative Information',
00080         204 => 'No Content',
00081         205 => 'Reset Content',
00082         206 => 'Partial Content',
00083         207 => 'Multi-Status',
00084         300 => 'Multiple Choices',
00085         301 => 'Moved Permanently',
00086         302 => 'Found',
00087         303 => 'See Other',
00088         304 => 'Not Modified',
00089         305 => 'Use Proxy',
00090         307 => 'Temporary Redirect',
00091         400 => 'Bad Request',
00092         401 => 'Unauthorized',
00093         402 => 'Payment Required',
00094         403 => 'Forbidden',
00095         404 => 'Not Found',
00096         405 => 'Method Not Allowed',
00097         406 => 'Not Acceptable',
00098         407 => 'Proxy Authentication Required',
00099         408 => 'Request Timeout',
00100         409 => 'Conflict',
00101         410 => 'Gone',
00102         411 => 'Length Required',
00103         412 => 'Precondition Failed',
00104         413 => 'Request Entity Too Large',
00105         414 => 'Request-URI Too Long',
00106         415 => 'Unsupported Media Type',
00107         416 => 'Requested Range Not Satisfiable',
00108         417 => 'Expectation Failed',
00109         500 => 'Internal Server Error',
00110         501 => 'Not Implemented',
00111         502 => 'Bad Gateway',
00112         503 => 'Service Unavailable',
00113         504 => 'Gateway Timeout',
00114         505 => 'HTTP Version Not Supported',
00115         507 => 'Insufficient Storage',
00116         509 => 'Bandwidth Limit Exceeded',
00117     );
00118 
00119     /**
00120      * Sets the HTTP status code and (optionally) a customized message.
00121      *
00122      * @param integer $code The status code
00123      * @param string $message If specified, this message is sent instead of the standard message
00124      * @return void
00125      * @throws InvalidArgumentException if the specified status code is not valid
00126      * @api
00127      */
00128     public function setStatus($code, $message = NULL) {
00129         if (!is_int($code)) throw new InvalidArgumentException('The HTTP status code must be of type integer, ' . gettype($code) . ' given.', 1220526013);
00130         if ($message === NULL && !isset($this->statusMessages[$code])) throw new InvalidArgumentException('No message found for HTTP status code "' . $code . '".', 1220526014);
00131 
00132         $this->statusCode = $code;
00133         $this->statusMessage = ($message === NULL) ? $this->statusMessages[$code] : $message;
00134     }
00135 
00136     /**
00137      * Returns status code and status message.
00138      *
00139      * @return string The status code and status message, eg. "404 Not Found"
00140      * @api
00141      */
00142     public function getStatus() {
00143         return $this->statusCode . ' ' . $this->statusMessage;
00144     }
00145 
00146     /**
00147      * Sets the specified HTTP header
00148      *
00149      * @param string $name Name of the header, for example "Location", "Content-Description" etc.
00150      * @param mixed $value The value of the given header
00151      * @param boolean $replaceExistingHeader If a header with the same name should be replaced. Default is TRUE.
00152      * @return void
00153      * @api
00154      */
00155     public function setHeader($name, $value, $replaceExistingHeader = TRUE) {
00156         if (strtoupper(substr($name, 0, 4)) === 'HTTP') throw new InvalidArgumentException('The HTTP status header must be set via setStatus().', 1220541963);
00157         if ($replaceExistingHeader === TRUE || !isset($this->headers[$name])) {
00158             $this->headers[$name] = array($value);
00159         } else {
00160             $this->headers[$name][] = $value;
00161         }
00162     }
00163 
00164     /**
00165      * Returns the HTTP headers - including the status header - of this web response
00166      *
00167      * @return string The HTTP headers
00168      * @api
00169      */
00170     public function getHeaders() {
00171         $preparedHeaders = array();
00172         if ($this->statusCode !== NULL) {
00173             $protocolVersion = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
00174             $statusHeader = $protocolVersion . ' ' . $this->statusCode . ' ' . $this->statusMessage;
00175             $preparedHeaders[] = $statusHeader;
00176         }
00177         foreach ($this->headers as $name => $values) {
00178             foreach ($values as $value) {
00179                 $preparedHeaders[] = $name . ': ' . $value;
00180             }
00181         }
00182         return $preparedHeaders;
00183     }
00184 
00185     /**
00186      * Sends the HTTP headers.
00187      *
00188      * If headers have already been sent, this method fails silently.
00189      *
00190      * @return void
00191      * @api
00192      */
00193     public function sendHeaders() {
00194         if (headers_sent() === TRUE) return;
00195         foreach ($this->getHeaders() as $header) {
00196             header($header);
00197         }
00198     }
00199 
00200     /**
00201      * Renders and sends the whole web response
00202      *
00203      * @return void
00204      * @api
00205      */
00206     public function send() {
00207         $this->sendHeaders();
00208         if ($this->content !== NULL) {
00209             echo $this->getContent();
00210         }
00211     }
00212 
00213     /**
00214      * Adds an additional header data (something like
00215      * '<script src="myext/Resources/JavaScript/my.js" type="text/javascript"></script>'
00216      * )
00217      *
00218      * @param string $additionalHeaderData The value additonal header
00219      * @return void
00220      * @api
00221      */
00222     public function addAdditionalHeaderData($additionalHeaderData) {
00223         if (!is_string($additionalHeaderData)) throw new InvalidArgumentException('The additiona header data must be of type String, ' . gettype($additionalHeaderData) . ' given.', 1237370877);
00224         $this->additionalHeaderData[] = $additionalHeaderData;
00225     }
00226 
00227     /**
00228      * Returns the additional header data
00229      *
00230      * @return array The additional header data
00231      * @api
00232      */
00233     public function getAdditionalHeaderData() {
00234         return $this->additionalHeaderData;
00235     }
00236 
00237 }
00238 ?>