Merge "Update outdated references to maintenance/ in documentation comments."
[mediawiki.git] / includes / WebResponse.php
blobc2193fbd84c261b01ab2364c964c2af42246aabf
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
32 * header()
33 * @param string $string header to output
34 * @param bool $replace replace current similar header
35 * @param $http_response_code null|int Forces the HTTP response code to the specified value.
37 public function header( $string, $replace = true, $http_response_code = null ) {
38 header( $string, $replace, $http_response_code );
41 /**
42 * Set the browser cookie
43 * @param string $name name of cookie
44 * @param string $value value to give cookie
45 * @param int $expire Unix timestamp (in seconds) when the cookie should expire.
46 * 0 (the default) causes it to expire $wgCookieExpiration seconds from now.
47 * @param string $prefix Prefix to use, if not $wgCookiePrefix (use '' for no prefix)
48 * @param string $domain Cookie domain to use, if not $wgCookieDomain
49 * @param $forceSecure Bool:
50 * true: force the cookie to be set with the secure attribute
51 * false: force the cookie to be set without the secure attribute
52 * null: use the value from $wgCookieSecure
54 public function setcookie( $name, $value, $expire = 0, $prefix = null, $domain = null, $forceSecure = null ) {
55 global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
56 global $wgCookieSecure, $wgCookieExpiration, $wgCookieHttpOnly;
57 if ( $expire == 0 && $wgCookieExpiration != 0 ) {
58 $expire = time() + $wgCookieExpiration;
60 if ( $prefix === null ) {
61 $prefix = $wgCookiePrefix;
63 if ( $domain === null ) {
64 $domain = $wgCookieDomain;
67 if ( is_null( $forceSecure ) ) {
68 $secureCookie = $wgCookieSecure;
69 } else {
70 $secureCookie = $forceSecure;
73 // Mark the cookie as httpOnly if $wgCookieHttpOnly is true,
74 // unless the requesting user-agent is known to have trouble with
75 // httpOnly cookies.
76 $httpOnlySafe = $wgCookieHttpOnly && wfHttpOnlySafe();
78 wfDebugLog( 'cookie',
79 'setcookie: "' . implode( '", "',
80 array(
81 $prefix . $name,
82 $value,
83 $expire,
84 $wgCookiePath,
85 $domain,
86 $secureCookie,
87 $httpOnlySafe ) ) . '"' );
88 setcookie( $prefix . $name,
89 $value,
90 $expire,
91 $wgCookiePath,
92 $domain,
93 $secureCookie,
94 $httpOnlySafe );
98 /**
99 * @ingroup HTTP
101 class FauxResponse extends WebResponse {
102 private $headers;
103 private $cookies;
104 private $code;
107 * Stores a HTTP header
108 * @param string $string header to output
109 * @param bool $replace replace current similar header
110 * @param $http_response_code null|int Forces the HTTP response code to the specified value.
112 public function header( $string, $replace = true, $http_response_code = null ) {
113 if ( substr( $string, 0, 5 ) == 'HTTP/' ) {
114 $parts = explode( ' ', $string, 3 );
115 $this->code = intval( $parts[1] );
116 } else {
117 list( $key, $val ) = array_map( 'trim', explode( ":", $string, 2 ) );
119 $key = strtoupper( $key );
121 if ( $replace || !isset( $this->headers[$key] ) ) {
122 $this->headers[$key] = $val;
126 if ( $http_response_code !== null ) {
127 $this->code = intval( $http_response_code );
132 * @param string $key The name of the header to get (case insensitive).
133 * @return string
135 public function getheader( $key ) {
136 $key = strtoupper( $key );
138 if ( isset( $this->headers[$key] ) ) {
139 return $this->headers[$key];
141 return null;
145 * Get the HTTP response code, null if not set
147 * @return Int or null
149 public function getStatusCode() {
150 return $this->code;
154 * @todo document. It just ignore optional parameters.
156 * @param string $name name of cookie
157 * @param string $value value to give cookie
158 * @param int $expire number of seconds til cookie expires (Default: 0)
159 * @param $prefix TODO DOCUMENT (Default: null)
160 * @param $domain TODO DOCUMENT (Default: null)
161 * @param $forceSecure TODO DOCUMENT (Default: null)
163 public function setcookie( $name, $value, $expire = 0, $prefix = null, $domain = null, $forceSecure = null ) {
164 $this->cookies[$name] = $value;
168 * @param $name string
169 * @return string
171 public function getcookie( $name ) {
172 if ( isset( $this->cookies[$name] ) ) {
173 return $this->cookies[$name];
175 return null;