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
24 * Allow programs to request this object from WebRequest::response()
25 * and handle all outputting (or lack of outputting) via it.
31 * Output a HTTP header, wrapper for PHP's
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 );
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;
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
76 $httpOnlySafe = $wgCookieHttpOnly && wfHttpOnlySafe();
79 'setcookie: "' . implode( '", "',
87 $httpOnlySafe ) ) . '"' );
88 setcookie( $prefix . $name,
101 class FauxResponse
extends WebResponse
{
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] );
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).
135 public function getheader( $key ) {
136 $key = strtoupper( $key );
138 if ( isset( $this->headers
[$key] ) ) {
139 return $this->headers
[$key];
145 * Get the HTTP response code, null if not set
147 * @return Int or null
149 public function getStatusCode() {
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
171 public function getcookie( $name ) {
172 if ( isset( $this->cookies
[$name] ) ) {
173 return $this->cookies
[$name];