Parse duplicate-defaultsort error message
[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 bool $replace 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 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
53 */
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;
59 }
60 if ( $prefix === null ) {
61 $prefix = $wgCookiePrefix;
62 }
63 if ( $domain === null ) {
64 $domain = $wgCookieDomain;
65 }
66
67 if ( is_null( $forceSecure ) ) {
68 $secureCookie = $wgCookieSecure;
69 } else {
70 $secureCookie = $forceSecure;
71 }
72
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();
77
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 );
95 }
96 }
97
98 /**
99 * @ingroup HTTP
100 */
101 class FauxResponse extends WebResponse {
102 private $headers;
103 private $cookies;
104 private $code;
105
106 /**
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.
111 */
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 ) );
118
119 $key = strtoupper( $key );
120
121 if ( $replace || !isset( $this->headers[$key] ) ) {
122 $this->headers[$key] = $val;
123 }
124 }
125
126 if ( $http_response_code !== null ) {
127 $this->code = intval( $http_response_code );
128 }
129 }
130
131 /**
132 * @param string $key The name of the header to get (case insensitive).
133 * @return string
134 */
135 public function getheader( $key ) {
136 $key = strtoupper( $key );
137
138 if ( isset( $this->headers[$key] ) ) {
139 return $this->headers[$key];
140 }
141 return null;
142 }
143
144 /**
145 * Get the HTTP response code, null if not set
146 *
147 * @return Int or null
148 */
149 public function getStatusCode() {
150 return $this->code;
151 }
152
153 /**
154 * @todo document. It just ignore optional parameters.
155 *
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)
162 */
163 public function setcookie( $name, $value, $expire = 0, $prefix = null, $domain = null, $forceSecure = null ) {
164 $this->cookies[$name] = $value;
165 }
166
167 /**
168 * @param $name string
169 * @return string
170 */
171 public function getcookie( $name ) {
172 if ( isset( $this->cookies[$name] ) ) {
173 return $this->cookies[$name];
174 }
175 return null;
176 }
177 }