Move MW_LC_VERSION to class constant instead of global define
[mediawiki.git] / includes / WebResponse.php
blobadccf9c7da279624816002b183b1cb71a1f32df0
1 <?php
2 /**
3 * Classes used to send headers and cookies back to the user
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 /**
24 * Allow programs to request this object from WebRequest::response()
25 * and handle all outputting (or lack of outputting) via it.
26 * @ingroup HTTP
28 class WebResponse {
30 /**
31 * Output a HTTP header, wrapper for PHP's header()
32 * @param string $string Header to output
33 * @param bool $replace Replace current similar header
34 * @param null|int $http_response_code Forces the HTTP response code to the specified value.
36 public function header( $string, $replace = true, $http_response_code = null ) {
37 header( $string, $replace, $http_response_code );
40 /**
41 * Set the browser cookie
42 * @param string $name Name of cookie
43 * @param string $value Value to give cookie
44 * @param int|null $expire Unix timestamp (in seconds) when the cookie should expire.
45 * 0 (the default) causes it to expire $wgCookieExpiration seconds from now.
46 * null causes it to be a session cookie.
47 * @param array $options Assoc of additional cookie options:
48 * prefix: string, name prefix ($wgCookiePrefix)
49 * domain: string, cookie domain ($wgCookieDomain)
50 * path: string, cookie path ($wgCookiePath)
51 * secure: bool, secure attribute ($wgCookieSecure)
52 * httpOnly: bool, httpOnly attribute ($wgCookieHttpOnly)
53 * raw: bool, if true uses PHP's setrawcookie() instead of setcookie()
54 * For backwards compatability, if $options is not an array then it and
55 * the following two parameters will be interpreted as values for
56 * 'prefix', 'domain', and 'secure'
57 * @since 1.22 Replaced $prefix, $domain, and $forceSecure with $options
59 public function setcookie( $name, $value, $expire = 0, $options = null ) {
60 global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
61 global $wgCookieSecure, $wgCookieExpiration, $wgCookieHttpOnly;
63 if ( !is_array( $options ) ) {
64 // Backwards compatability
65 $options = array( 'prefix' => $options );
66 if ( func_num_args() >= 5 ) {
67 $options['domain'] = func_get_arg( 4 );
69 if ( func_num_args() >= 6 ) {
70 $options['secure'] = func_get_arg( 5 );
73 $options = array_filter( $options, function ( $a ) {
74 return $a !== null;
75 } ) + array(
76 'prefix' => $wgCookiePrefix,
77 'domain' => $wgCookieDomain,
78 'path' => $wgCookiePath,
79 'secure' => $wgCookieSecure,
80 'httpOnly' => $wgCookieHttpOnly,
81 'raw' => false,
84 if ( $expire === null ) {
85 $expire = 0; // Session cookie
86 } elseif ( $expire == 0 && $wgCookieExpiration != 0 ) {
87 $expire = time() + $wgCookieExpiration;
90 $func = $options['raw'] ? 'setrawcookie' : 'setcookie';
92 if ( wfRunHooks( 'WebResponseSetCookie', array( &$name, &$value, &$expire, $options ) ) ) {
93 wfDebugLog( 'cookie',
94 $func . ': "' . implode( '", "',
95 array(
96 $options['prefix'] . $name,
97 $value,
98 $expire,
99 $options['path'],
100 $options['domain'],
101 $options['secure'],
102 $options['httpOnly'] ) ) . '"' );
104 call_user_func( $func,
105 $options['prefix'] . $name,
106 $value,
107 $expire,
108 $options['path'],
109 $options['domain'],
110 $options['secure'],
111 $options['httpOnly'] );
117 * @ingroup HTTP
119 class FauxResponse extends WebResponse {
120 private $headers;
121 private $cookies;
122 private $code;
125 * Stores a HTTP header
126 * @param string $string Header to output
127 * @param bool $replace Replace current similar header
128 * @param null|int $http_response_code Forces the HTTP response code to the specified value.
130 public function header( $string, $replace = true, $http_response_code = null ) {
131 if ( substr( $string, 0, 5 ) == 'HTTP/' ) {
132 $parts = explode( ' ', $string, 3 );
133 $this->code = intval( $parts[1] );
134 } else {
135 list( $key, $val ) = array_map( 'trim', explode( ":", $string, 2 ) );
137 $key = strtoupper( $key );
139 if ( $replace || !isset( $this->headers[$key] ) ) {
140 $this->headers[$key] = $val;
144 if ( $http_response_code !== null ) {
145 $this->code = intval( $http_response_code );
150 * @param string $key The name of the header to get (case insensitive).
151 * @return string
153 public function getheader( $key ) {
154 $key = strtoupper( $key );
156 if ( isset( $this->headers[$key] ) ) {
157 return $this->headers[$key];
159 return null;
163 * Get the HTTP response code, null if not set
165 * @return int|null
167 public function getStatusCode() {
168 return $this->code;
172 * @todo document. It just ignore optional parameters.
174 * @param string $name Name of cookie
175 * @param string $value Value to give cookie
176 * @param int $expire Number of seconds til cookie expires (Default: 0)
177 * @param array $options Ignored
179 public function setcookie( $name, $value, $expire = 0, $options = null ) {
180 $this->cookies[$name] = $value;
184 * @param string $name
185 * @return string
187 public function getcookie( $name ) {
188 if ( isset( $this->cookies[$name] ) ) {
189 return $this->cookies[$name];
191 return null;