Switch some HTMLForms in special pages to OOUI
[lhc/web/wiklou.git] / includes / WebResponse.php
1 <?php
2 /**
3 * Classes used to send headers and cookies back to the user
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
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
27 */
28 class WebResponse {
29
30 /**
31 * Output an 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.
35 */
36 public function header( $string, $replace = true, $http_response_code = null ) {
37 header( $string, $replace, $http_response_code );
38 }
39
40 /**
41 * Get a response header
42 * @param string $key The name of the header to get (case insensitive).
43 * @return string|null The header value (if set); null otherwise.
44 * @since 1.25
45 */
46 public function getHeader( $key ) {
47 foreach ( headers_list() as $header ) {
48 list( $name, $val ) = explode( ':', $header, 2 );
49 if ( !strcasecmp( $name, $key ) ) {
50 return trim( $val );
51 }
52 }
53 return null;
54 }
55
56 /**
57 * Output an HTTP status code header
58 * @since 1.26
59 * @param int $code Status code
60 */
61 public function statusHeader( $code ) {
62 HttpStatus::header( $code );
63 }
64
65 /**
66 * Set the browser cookie
67 * @param string $name The name of the cookie.
68 * @param string $value The value to be stored in the cookie.
69 * @param int|null $expire Unix timestamp (in seconds) when the cookie should expire.
70 * 0 (the default) causes it to expire $wgCookieExpiration seconds from now.
71 * null causes it to be a session cookie.
72 * @param array $options Assoc of additional cookie options:
73 * prefix: string, name prefix ($wgCookiePrefix)
74 * domain: string, cookie domain ($wgCookieDomain)
75 * path: string, cookie path ($wgCookiePath)
76 * secure: bool, secure attribute ($wgCookieSecure)
77 * httpOnly: bool, httpOnly attribute ($wgCookieHttpOnly)
78 * raw: bool, if true uses PHP's setrawcookie() instead of setcookie()
79 * For backwards compatibility, if $options is not an array then it and
80 * the following two parameters will be interpreted as values for
81 * 'prefix', 'domain', and 'secure'
82 * @since 1.22 Replaced $prefix, $domain, and $forceSecure with $options
83 */
84 public function setcookie( $name, $value, $expire = 0, $options = array() ) {
85 global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
86 global $wgCookieSecure, $wgCookieExpiration, $wgCookieHttpOnly;
87
88 if ( !is_array( $options ) ) {
89 // Backwards compatibility
90 $options = array( 'prefix' => $options );
91 if ( func_num_args() >= 5 ) {
92 $options['domain'] = func_get_arg( 4 );
93 }
94 if ( func_num_args() >= 6 ) {
95 $options['secure'] = func_get_arg( 5 );
96 }
97 }
98 $options = array_filter( $options, function ( $a ) {
99 return $a !== null;
100 } ) + array(
101 'prefix' => $wgCookiePrefix,
102 'domain' => $wgCookieDomain,
103 'path' => $wgCookiePath,
104 'secure' => $wgCookieSecure,
105 'httpOnly' => $wgCookieHttpOnly,
106 'raw' => false,
107 );
108
109 if ( $expire === null ) {
110 $expire = 0; // Session cookie
111 } elseif ( $expire == 0 && $wgCookieExpiration != 0 ) {
112 $expire = time() + $wgCookieExpiration;
113 }
114
115 $func = $options['raw'] ? 'setrawcookie' : 'setcookie';
116
117 if ( Hooks::run( 'WebResponseSetCookie', array( &$name, &$value, &$expire, $options ) ) ) {
118 wfDebugLog( 'cookie',
119 $func . ': "' . implode( '", "',
120 array(
121 $options['prefix'] . $name,
122 $value,
123 $expire,
124 $options['path'],
125 $options['domain'],
126 $options['secure'],
127 $options['httpOnly'] ) ) . '"' );
128
129 call_user_func( $func,
130 $options['prefix'] . $name,
131 $value,
132 $expire,
133 $options['path'],
134 $options['domain'],
135 $options['secure'],
136 $options['httpOnly'] );
137 }
138 }
139 }
140
141 /**
142 * @ingroup HTTP
143 */
144 class FauxResponse extends WebResponse {
145 private $headers;
146 private $cookies;
147 private $code;
148
149 /**
150 * Stores a HTTP header
151 * @param string $string Header to output
152 * @param bool $replace Replace current similar header
153 * @param null|int $http_response_code Forces the HTTP response code to the specified value.
154 */
155 public function header( $string, $replace = true, $http_response_code = null ) {
156 if ( substr( $string, 0, 5 ) == 'HTTP/' ) {
157 $parts = explode( ' ', $string, 3 );
158 $this->code = intval( $parts[1] );
159 } else {
160 list( $key, $val ) = array_map( 'trim', explode( ":", $string, 2 ) );
161
162 $key = strtoupper( $key );
163
164 if ( $replace || !isset( $this->headers[$key] ) ) {
165 $this->headers[$key] = $val;
166 }
167 }
168
169 if ( $http_response_code !== null ) {
170 $this->code = intval( $http_response_code );
171 }
172 }
173
174 /**
175 * @since 1.26
176 * @param int $code Status code
177 */
178 public function statusHeader( $code ) {
179 $this->code = intval( $code );
180 }
181
182 /**
183 * @param string $key The name of the header to get (case insensitive).
184 * @return string|null The header value (if set); null otherwise.
185 */
186 public function getHeader( $key ) {
187 $key = strtoupper( $key );
188
189 if ( isset( $this->headers[$key] ) ) {
190 return $this->headers[$key];
191 }
192 return null;
193 }
194
195 /**
196 * Get the HTTP response code, null if not set
197 *
198 * @return int|null
199 */
200 public function getStatusCode() {
201 return $this->code;
202 }
203
204 /**
205 * @param string $name The name of the cookie.
206 * @param string $value The value to be stored in the cookie.
207 * @param int|null $expire Ignored in this faux subclass.
208 * @param array $options Ignored in this faux subclass.
209 */
210 public function setcookie( $name, $value, $expire = 0, $options = array() ) {
211 $this->cookies[$name] = $value;
212 }
213
214 /**
215 * @param string $name
216 * @return string|null
217 */
218 public function getcookie( $name ) {
219 if ( isset( $this->cookies[$name] ) ) {
220 return $this->cookies[$name];
221 }
222 return null;
223 }
224 }