Merge "[JobQueue] Added support for approximate FIFO job queues."
[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 a HTTP header, wrapper for PHP's
32 * header()
33 * @param $string String: header to output
34 * @param $replace Bool: replace current similar header
35 * @param $http_response_code null|int Forces the HTTP response code to the specified value.
36 */
37 public function header( $string, $replace = true, $http_response_code = null ) {
38 header( $string, $replace, $http_response_code );
39 }
40
41 /**
42 * Set the browser cookie
43 * @param $name String: name of cookie
44 * @param $value String: value to give cookie
45 * @param $expire Int: number of seconds til cookie expires
46 * @param $prefix String: Prefix to use, if not $wgCookiePrefix (use '' for no prefix)
47 * @param $domain String: Cookie domain to use, if not $wgCookieDomain
48 * @param $forceSecure Bool:
49 * true: force the cookie to be set with the secure attribute
50 * false: force the cookie to be set without the secure attribute
51 * null: use the value from $wgCookieSecure
52 */
53 public function setcookie( $name, $value, $expire = 0, $prefix = null, $domain = null, $forceSecure = null ) {
54 global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
55 global $wgCookieSecure,$wgCookieExpiration, $wgCookieHttpOnly;
56 if ( $expire == 0 ) {
57 $expire = time() + $wgCookieExpiration;
58 }
59 if( $prefix === null ) {
60 $prefix = $wgCookiePrefix;
61 }
62 if( $domain === null ) {
63 $domain = $wgCookieDomain;
64 }
65
66 if ( is_null( $forceSecure ) ) {
67 $secureCookie = $wgCookieSecure;
68 } else {
69 $secureCookie = $forceSecure;
70 }
71
72 $httpOnlySafe = wfHttpOnlySafe() && $wgCookieHttpOnly;
73 wfDebugLog( 'cookie',
74 'setcookie: "' . implode( '", "',
75 array(
76 $prefix . $name,
77 $value,
78 $expire,
79 $wgCookiePath,
80 $domain,
81 $secureCookie,
82 $httpOnlySafe ) ) . '"' );
83 setcookie( $prefix . $name,
84 $value,
85 $expire,
86 $wgCookiePath,
87 $domain,
88 $secureCookie,
89 $httpOnlySafe );
90 }
91 }
92
93 /**
94 * @ingroup HTTP
95 */
96 class FauxResponse extends WebResponse {
97 private $headers;
98 private $cookies;
99 private $code;
100
101 /**
102 * Stores a HTTP header
103 * @param $string String: header to output
104 * @param $replace Bool: replace current similar header
105 * @param $http_response_code null|int Forces the HTTP response code to the specified value.
106 */
107 public function header( $string, $replace = true, $http_response_code = null ) {
108 if ( substr( $string, 0, 5 ) == 'HTTP/' ) {
109 $parts = explode( ' ', $string, 3 );
110 $this->code = intval( $parts[1] );
111 } else {
112 list( $key, $val ) = array_map( 'trim', explode( ":", $string, 2 ) );
113
114 if( $replace || !isset( $this->headers[$key] ) ) {
115 $this->headers[$key] = $val;
116 }
117 }
118
119 if ( $http_response_code !== null ) {
120 $this->code = intval( $http_response_code );
121 }
122 }
123
124 /**
125 * @param $key string
126 * @return string
127 */
128 public function getheader( $key ) {
129 if ( isset( $this->headers[$key] ) ) {
130 return $this->headers[$key];
131 }
132 return null;
133 }
134
135 /**
136 * Get the HTTP response code, null if not set
137 *
138 * @return Int or null
139 */
140 public function getStatusCode() {
141 return $this->code;
142 }
143
144 /**
145 * @todo document. It just ignore optional parameters.
146 *
147 * @param $name String: name of cookie
148 * @param $value String: value to give cookie
149 * @param $expire Int: number of seconds til cookie expires (Default: 0)
150 * @param $prefix TODO DOCUMENT (Default: null)
151 * @param $domain TODO DOCUMENT (Default: null)
152 * @param $forceSecure TODO DOCUMENT (Default: null)
153 */
154 public function setcookie( $name, $value, $expire = 0, $prefix = null, $domain = null, $forceSecure = null ) {
155 $this->cookies[$name] = $value;
156 }
157
158 /**
159 * @param $name string
160 * @return string
161 */
162 public function getcookie( $name ) {
163 if ( isset( $this->cookies[$name] ) ) {
164 return $this->cookies[$name];
165 }
166 return null;
167 }
168 }