Use $this->getLang() per Nikerabbit on r91648.
[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 */
47 public function setcookie( $name, $value, $expire = 0 ) {
48 global $wgCookiePath, $wgCookiePrefix, $wgCookieDomain;
49 global $wgCookieSecure,$wgCookieExpiration, $wgCookieHttpOnly;
50 if ( $expire == 0 ) {
51 $expire = time() + $wgCookieExpiration;
52 }
53 $httpOnlySafe = wfHttpOnlySafe() && $wgCookieHttpOnly;
54 wfDebugLog( 'cookie',
55 'setcookie: "' . implode( '", "',
56 array(
57 $wgCookiePrefix . $name,
58 $value,
59 $expire,
60 $wgCookiePath,
61 $wgCookieDomain,
62 $wgCookieSecure,
63 $httpOnlySafe ) ) . '"' );
64 setcookie( $wgCookiePrefix . $name,
65 $value,
66 $expire,
67 $wgCookiePath,
68 $wgCookieDomain,
69 $wgCookieSecure,
70 $httpOnlySafe );
71 }
72 }
73
74 /**
75 * @ingroup HTTP
76 */
77 class FauxResponse extends WebResponse {
78 private $headers;
79 private $cookies;
80 private $code;
81
82 /**
83 * Stores a HTTP header
84 * @param $string String: header to output
85 * @param $replace Bool: replace current similar header
86 * @param $http_response_code null|int Forces the HTTP response code to the specified value.
87 */
88 public function header( $string, $replace = true, $http_response_code = null ) {
89 if ( substr( $string, 0, 5 ) == 'HTTP/' ) {
90 $parts = explode( ' ', $string, 3 );
91 $this->code = intval( $parts[1] );
92 } else {
93 list( $key, $val ) = array_map( 'trim', explode( ":", $string, 2 ) );
94
95 if( $replace || !isset( $this->headers[$key] ) ) {
96 $this->headers[$key] = $val;
97 }
98 }
99
100 if ( $http_response_code !== null ) {
101 $this->code = intval( $http_response_code );
102 }
103 }
104
105 /**
106 * @param $key string
107 * @return string
108 */
109 public function getheader( $key ) {
110 if ( isset( $this->headers[$key] ) ) {
111 return $this->headers[$key];
112 }
113 return null;
114 }
115
116 /**
117 * Get the HTTP response code, null if not set
118 *
119 * @return Int or null
120 */
121 public function getStatusCode() {
122 return $this->code;
123 }
124
125 /**
126 * @param $name String: name of cookie
127 * @param $value String: value to give cookie
128 * @param $expire Int: number of seconds til cookie expires
129 */
130 public function setcookie( $name, $value, $expire = 0 ) {
131 $this->cookies[$name] = $value;
132 }
133
134 /**
135 * @param $name string
136 * @return string
137 */
138 public function getcookie( $name ) {
139 if ( isset( $this->cookies[$name] ) ) {
140 return $this->cookies[$name];
141 }
142 return null;
143 }
144 }