MediaWiki REL1_27
WebResponse.php
Go to the documentation of this file.
1<?php
29
33 protected static $setCookies = [];
34
41 public function header( $string, $replace = true, $http_response_code = null ) {
42 header( $string, $replace, $http_response_code );
43 }
44
51 public function getHeader( $key ) {
52 foreach ( headers_list() as $header ) {
53 list( $name, $val ) = explode( ':', $header, 2 );
54 if ( !strcasecmp( $name, $key ) ) {
55 return trim( $val );
56 }
57 }
58 return null;
59 }
60
66 public function statusHeader( $code ) {
68 }
69
75 public function headersSent() {
76 return headers_sent();
77 }
78
98 public function setCookie( $name, $value, $expire = 0, $options = [] ) {
101
102 if ( !is_array( $options ) ) {
103 // Backwards compatibility
104 $options = [ 'prefix' => $options ];
105 if ( func_num_args() >= 5 ) {
106 $options['domain'] = func_get_arg( 4 );
107 }
108 if ( func_num_args() >= 6 ) {
109 $options['secure'] = func_get_arg( 5 );
110 }
111 }
112 $options = array_filter( $options, function ( $a ) {
113 return $a !== null;
114 } ) + [
115 'prefix' => $wgCookiePrefix,
116 'domain' => $wgCookieDomain,
117 'path' => $wgCookiePath,
118 'secure' => $wgCookieSecure,
119 'httpOnly' => $wgCookieHttpOnly,
120 'raw' => false,
121 ];
122
123 if ( $expire === null ) {
124 $expire = 0; // Session cookie
125 } elseif ( $expire == 0 && $wgCookieExpiration != 0 ) {
126 $expire = time() + $wgCookieExpiration;
127 }
128
129 $func = $options['raw'] ? 'setrawcookie' : 'setcookie';
130
131 if ( Hooks::run( 'WebResponseSetCookie', [ &$name, &$value, &$expire, &$options ] ) ) {
132 $cookie = $options['prefix'] . $name;
133 $data = [
134 'name' => (string)$cookie,
135 'value' => (string)$value,
136 'expire' => (int)$expire,
137 'path' => (string)$options['path'],
138 'domain' => (string)$options['domain'],
139 'secure' => (bool)$options['secure'],
140 'httpOnly' => (bool)$options['httpOnly'],
141 ];
142
143 // Per RFC 6265, key is name + domain + path
144 $key = "{$data['name']}\n{$data['domain']}\n{$data['path']}";
145
146 // If this cookie name was in the request, fake an entry in
147 // self::$setCookies for it so the deleting check works right.
148 if ( isset( $_COOKIE[$cookie] ) && !array_key_exists( $key, self::$setCookies ) ) {
149 self::$setCookies[$key] = [];
150 }
151
152 // PHP deletes if value is the empty string; also, a past expiry is deleting
153 $deleting = ( $data['value'] === '' || $data['expire'] > 0 && $data['expire'] <= time() );
154
155 if ( $deleting && !isset( self::$setCookies[$key] ) ) { // isset( null ) is false
156 wfDebugLog( 'cookie', 'already deleted ' . $func . ': "' . implode( '", "', $data ) . '"' );
157 } elseif ( !$deleting && isset( self::$setCookies[$key] ) &&
158 self::$setCookies[$key] === [ $func, $data ]
159 ) {
160 wfDebugLog( 'cookie', 'already set ' . $func . ': "' . implode( '", "', $data ) . '"' );
161 } else {
162 wfDebugLog( 'cookie', $func . ': "' . implode( '", "', $data ) . '"' );
163 if ( call_user_func_array( $func, array_values( $data ) ) ) {
164 self::$setCookies[$key] = $deleting ? null : [ $func, $data ];
165 }
166 }
167 }
168 }
169
179 public function clearCookie( $name, $options = [] ) {
180 $this->setCookie( $name, '', time() - 31536000 /* 1 year */, $options );
181 }
182
189 public function hasCookies() {
190 return (bool)self::$setCookies;
191 }
192}
193
198 private $headers;
199 private $cookies = [];
200 private $code;
201
208 public function header( $string, $replace = true, $http_response_code = null ) {
209 if ( substr( $string, 0, 5 ) == 'HTTP/' ) {
210 $parts = explode( ' ', $string, 3 );
211 $this->code = intval( $parts[1] );
212 } else {
213 list( $key, $val ) = array_map( 'trim', explode( ":", $string, 2 ) );
214
215 $key = strtoupper( $key );
216
217 if ( $replace || !isset( $this->headers[$key] ) ) {
218 $this->headers[$key] = $val;
219 }
220 }
221
222 if ( $http_response_code !== null ) {
223 $this->code = intval( $http_response_code );
224 }
225 }
226
231 public function statusHeader( $code ) {
232 $this->code = intval( $code );
233 }
234
235 public function headersSent() {
236 return false;
237 }
238
243 public function getHeader( $key ) {
244 $key = strtoupper( $key );
245
246 if ( isset( $this->headers[$key] ) ) {
247 return $this->headers[$key];
248 }
249 return null;
250 }
251
257 public function getStatusCode() {
258 return $this->code;
259 }
260
267 public function setCookie( $name, $value, $expire = 0, $options = [] ) {
270
271 if ( !is_array( $options ) ) {
272 // Backwards compatibility
273 $options = [ 'prefix' => $options ];
274 if ( func_num_args() >= 5 ) {
275 $options['domain'] = func_get_arg( 4 );
276 }
277 if ( func_num_args() >= 6 ) {
278 $options['secure'] = func_get_arg( 5 );
279 }
280 }
281 $options = array_filter( $options, function ( $a ) {
282 return $a !== null;
283 } ) + [
284 'prefix' => $wgCookiePrefix,
285 'domain' => $wgCookieDomain,
286 'path' => $wgCookiePath,
287 'secure' => $wgCookieSecure,
288 'httpOnly' => $wgCookieHttpOnly,
289 'raw' => false,
290 ];
291
292 if ( $expire === null ) {
293 $expire = 0; // Session cookie
294 } elseif ( $expire == 0 && $wgCookieExpiration != 0 ) {
295 $expire = time() + $wgCookieExpiration;
296 }
297
298 $this->cookies[$options['prefix'] . $name] = [
299 'value' => (string)$value,
300 'expire' => (int)$expire,
301 'path' => (string)$options['path'],
302 'domain' => (string)$options['domain'],
303 'secure' => (bool)$options['secure'],
304 'httpOnly' => (bool)$options['httpOnly'],
305 'raw' => (bool)$options['raw'],
306 ];
307 }
308
313 public function getCookie( $name ) {
314 if ( isset( $this->cookies[$name] ) ) {
315 return $this->cookies[$name]['value'];
316 }
317 return null;
318 }
319
324 public function getCookieData( $name ) {
325 if ( isset( $this->cookies[$name] ) ) {
326 return $this->cookies[$name];
327 }
328 return null;
329 }
330
334 public function getCookies() {
335 return $this->cookies;
336 }
337}
$wgCookieExpiration
Default cookie lifetime, in seconds.
$wgCookieHttpOnly
Set authentication cookies to HttpOnly to prevent access by JavaScript, in browsers that support this...
$wgCookiePath
Set this variable if you want to restrict cookies to a certain path within the domain specified by $w...
$wgCookieDomain
Set to set an explicit domain on the login cookies eg, "justthis.domain.org" or "....
$wgCookieSecure
Whether the "secure" flag should be set on the cookie.
$wgCookiePrefix
Cookies generated by MediaWiki have names starting with this prefix.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
header( $string, $replace=true, $http_response_code=null)
Stores a HTTP header.
statusHeader( $code)
setCookie( $name, $value, $expire=0, $options=[])
getStatusCode()
Get the HTTP response code, null if not set.
headersSent()
Test if headers have been sent.
getCookie( $name)
getCookieData( $name)
static header( $code)
Output an HTTP status code header.
Allow programs to request this object from WebRequest::response() and handle all outputting (or lack ...
setCookie( $name, $value, $expire=0, $options=[])
Set the browser cookie.
hasCookies()
Checks whether this request is performing cookie operations.
getHeader( $key)
Get a response header.
statusHeader( $code)
Output an HTTP status code header.
header( $string, $replace=true, $http_response_code=null)
Output an HTTP header, wrapper for PHP's header()
clearCookie( $name, $options=[])
Unset a browser cookie.
headersSent()
Test if headers have been sent.
static array $setCookies
Used to record set cookies, because PHP's setcookie() will happily send an identical Set-Cookie to th...
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
it sets a lot of them automatically from cookies
Definition design.txt:93
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add headers
Definition design.txt:18
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
and how to run hooks for an and one after Each event has a preferably in CamelCase For ArticleDelete hook A clump of code and data that should be run when an event happens This can be either a function and a chunk of or an object and a method hook function The function part of a third party developers and local administrators to define code that will be run at certain points in the mainline code
Definition hooks.txt:28
the array() calling protocol came about after MediaWiki 1.4rc1.
This code would result in ircNotify being run twice when an article is and once for brion Hooks can return three possible true was required This is the default since MediaWiki *some string
Definition hooks.txt:183
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition hooks.txt:1042
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:314
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition hooks.txt:847
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37