Follow-up r88054: register the file if a hook changed the target file.
[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 $match = array();
90 if ( substr( $string, 0, 5 ) == 'HTTP/' ) {
91 $parts = explode( ' ', $string, 3 );
92 $this->code = intval( $parts[1] );
93 } else {
94 list( $key, $val ) = explode( ":", $string, 2 );
95
96 if( $replace || !isset( $this->headers[$key] ) ) {
97 $this->headers[$key] = $val;
98 }
99 }
100
101 if ( $http_response_code !== null ) {
102 $this->code = intval( $http_response_code );
103 }
104 }
105
106 /**
107 * @param $key string
108 * @return string
109 */
110 public function getheader( $key ) {
111 return $this->headers[$key];
112 }
113
114 /**
115 * Get the HTTP response code, null if not set
116 *
117 * @return Int or null
118 */
119 public function getStatusCode() {
120 return $this->code;
121 }
122
123 /**
124 * @param $name String: name of cookie
125 * @param $value String: value to give cookie
126 * @param $expire Int: number of seconds til cookie expires
127 */
128 public function setcookie( $name, $value, $expire = 0 ) {
129 $this->cookies[$name] = $value;
130 }
131
132 /**
133 * @param $name string
134 * @return string
135 */
136 public function getcookie( $name ) {
137 if ( isset( $this->cookies[$name] ) ) {
138 return $this->cookies[$name];
139 }
140 return null;
141 }
142 }