(bug 29277) MediaWiki:Filepage.css not loaded on foreignwiki itself. Fixup to r68904...
[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
81 /**
82 * Stores a HTTP header
83 * @param $string String: header to output
84 * @param $replace Bool: replace current similar header
85 * @param $http_response_code null|int Forces the HTTP response code to the specified value.
86 */
87 public function header( $string, $replace = true, $http_response_code = null ) {
88 list( $key, $val ) = explode( ":", $string, 2 );
89
90 if( $replace || !isset( $this->headers[$key] ) ) {
91 $this->headers[$key] = $val;
92 }
93 }
94
95 /**
96 * @param $key string
97 * @return string
98 */
99 public function getheader( $key ) {
100 return $this->headers[$key];
101 }
102
103 /**
104 * @param $name String: name of cookie
105 * @param $value String: value to give cookie
106 * @param $expire Int: number of seconds til cookie expires
107 */
108 public function setcookie( $name, $value, $expire = 0 ) {
109 $this->cookies[$name] = $value;
110 }
111
112 /**
113 * @param $name string
114 * @return string
115 */
116 public function getcookie( $name ) {
117 if ( isset( $this->cookies[$name] ) ) {
118 return $this->cookies[$name];
119 }
120 return null;
121 }
122 }