Fixed spacing in actions/cache/filebackend/filerepo/job folder
[mediawiki.git] / includes / WebResponse.php
blob8e15d7121cfa383c2f23a7db38eeb695e10aa177
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 ) {
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 if( $replace || !isset( $this->headers[$key] ) ) {
120 $this->headers[$key] = $val;
124 if ( $http_response_code !== null ) {
125 $this->code = intval( $http_response_code );
130 * @param $key string
131 * @return string
133 public function getheader( $key ) {
134 if ( isset( $this->headers[$key] ) ) {
135 return $this->headers[$key];
137 return null;
141 * Get the HTTP response code, null if not set
143 * @return Int or null
145 public function getStatusCode() {
146 return $this->code;
150 * @todo document. It just ignore optional parameters.
152 * @param string $name name of cookie
153 * @param string $value value to give cookie
154 * @param int $expire number of seconds til cookie expires (Default: 0)
155 * @param $prefix TODO DOCUMENT (Default: null)
156 * @param $domain TODO DOCUMENT (Default: null)
157 * @param $forceSecure TODO DOCUMENT (Default: null)
159 public function setcookie( $name, $value, $expire = 0, $prefix = null, $domain = null, $forceSecure = null ) {
160 $this->cookies[$name] = $value;
164 * @param $name string
165 * @return string
167 public function getcookie( $name ) {
168 if ( isset( $this->cookies[$name] ) ) {
169 return $this->cookies[$name];
171 return null;