| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Request; |
| 24 |
|
| 25 |
use HttpStatus; |
| 26 |
use MediaWiki\HookContainer\HookRunner; |
| 27 |
use MediaWiki\MainConfigNames; |
| 28 |
use MediaWiki\MediaWikiServices; |
| 29 |
use RuntimeException; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class WebResponse { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
protected static $setCookies = []; |
| 42 |
|
| 43 |
|
| 44 |
protected $disableForPostSend = false; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function disableForPostSend() { |
| 56 |
$this->disableForPostSend = true; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function header( $string, $replace = true, $http_response_code = null ) { |
| 66 |
if ( $this->disableForPostSend ) { |
| 67 |
wfDebugLog( 'header', 'ignored post-send header {header}', 'all', [ |
| 68 |
'header' => $string, |
| 69 |
'replace' => $replace, |
| 70 |
'http_response_code' => $http_response_code, |
| 71 |
'exception' => new RuntimeException( 'Ignored post-send header' ), |
| 72 |
] ); |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
\MediaWiki\Request\HeaderCallback::warnIfHeadersSent(); |
| 77 |
if ( $http_response_code ) { |
| 78 |
header( $string, $replace, $http_response_code ); |
| 79 |
} else { |
| 80 |
header( $string, $replace ); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function getStatusCode() { |
| 90 |
return http_response_code(); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public function getHeader( $key ) { |
| 100 |
foreach ( headers_list() as $header ) { |
| 101 |
[ $name, $val ] = explode( ':', $header, 2 ); |
| 102 |
if ( !strcasecmp( $name, $key ) ) { |
| 103 |
return trim( $val ); |
| 104 |
} |
| 105 |
} |
| 106 |
return null; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public function statusHeader( $code ) { |
| 115 |
if ( $this->disableForPostSend ) { |
| 116 |
wfDebugLog( 'header', 'ignored post-send status header {code}', 'all', [ |
| 117 |
'code' => $code, |
| 118 |
'exception' => new RuntimeException( 'Ignored post-send status header' ), |
| 119 |
] ); |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
HttpStatus::header( $code ); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public function headersSent() { |
| 132 |
return headers_sent(); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function setCookie( $name, $value, $expire = 0, $options = [] ) { |
| 156 |
$services = MediaWikiServices::getInstance(); |
| 157 |
$mainConfig = $services->getMainConfig(); |
| 158 |
$cookiePath = $mainConfig->get( MainConfigNames::CookiePath ); |
| 159 |
$cookiePrefix = $mainConfig->get( MainConfigNames::CookiePrefix ); |
| 160 |
$cookieDomain = $mainConfig->get( MainConfigNames::CookieDomain ); |
| 161 |
$cookieSecure = $mainConfig->get( MainConfigNames::CookieSecure ); |
| 162 |
$cookieExpiration = $mainConfig->get( MainConfigNames::CookieExpiration ); |
| 163 |
$cookieHttpOnly = $mainConfig->get( MainConfigNames::CookieHttpOnly ); |
| 164 |
$options = array_filter( $options, static function ( $a ) { |
| 165 |
return $a !== null; |
| 166 |
} ) + [ |
| 167 |
'prefix' => $cookiePrefix, |
| 168 |
'domain' => $cookieDomain, |
| 169 |
'path' => $cookiePath, |
| 170 |
'secure' => $cookieSecure, |
| 171 |
'httpOnly' => $cookieHttpOnly, |
| 172 |
'raw' => false, |
| 173 |
'sameSite' => '', |
| 174 |
]; |
| 175 |
|
| 176 |
if ( $expire === null ) { |
| 177 |
$expire = 0; |
| 178 |
} elseif ( $expire == 0 && $cookieExpiration != 0 ) { |
| 179 |
$expire = time() + $cookieExpiration; |
| 180 |
} |
| 181 |
|
| 182 |
if ( $this->disableForPostSend ) { |
| 183 |
$prefixedName = $options['prefix'] . $name; |
| 184 |
wfDebugLog( 'cookie', 'ignored post-send cookie {cookie}', 'all', [ |
| 185 |
'cookie' => $prefixedName, |
| 186 |
'data' => [ |
| 187 |
'name' => $prefixedName, |
| 188 |
'value' => (string)$value, |
| 189 |
'expire' => (int)$expire, |
| 190 |
'path' => (string)$options['path'], |
| 191 |
'domain' => (string)$options['domain'], |
| 192 |
'secure' => (bool)$options['secure'], |
| 193 |
'httpOnly' => (bool)$options['httpOnly'], |
| 194 |
'sameSite' => (string)$options['sameSite'] |
| 195 |
], |
| 196 |
'exception' => new RuntimeException( 'Ignored post-send cookie' ), |
| 197 |
] ); |
| 198 |
return; |
| 199 |
} |
| 200 |
|
| 201 |
$hookRunner = new HookRunner( $services->getHookContainer() ); |
| 202 |
if ( !$hookRunner->onWebResponseSetCookie( $name, $value, $expire, $options ) ) { |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
$prefixedName = $options['prefix'] . $name; |
| 209 |
$value = (string)$value; |
| 210 |
$func = $options['raw'] ? 'setrawcookie' : 'setcookie'; |
| 211 |
$setOptions = [ |
| 212 |
'expires' => (int)$expire, |
| 213 |
'path' => (string)$options['path'], |
| 214 |
'domain' => (string)$options['domain'], |
| 215 |
'secure' => (bool)$options['secure'], |
| 216 |
'httponly' => (bool)$options['httpOnly'], |
| 217 |
'samesite' => (string)$options['sameSite'], |
| 218 |
]; |
| 219 |
|
| 220 |
|
| 221 |
$key = "{$prefixedName}\n{$setOptions['domain']}\n{$setOptions['path']}"; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
if ( isset( $_COOKIE[$prefixedName] ) && !array_key_exists( $key, self::$setCookies ) ) { |
| 226 |
self::$setCookies[$key] = []; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
$deleting = ( $value === '' || ( $setOptions['expires'] > 0 && $setOptions['expires'] <= time() ) ); |
| 231 |
|
| 232 |
$logDesc = "$func: \"$prefixedName\", \"$value\", \"" . |
| 233 |
implode( '", "', array_map( 'strval', $setOptions ) ) . '"'; |
| 234 |
$optionsForDeduplication = [ $func, $prefixedName, $value, $setOptions ]; |
| 235 |
|
| 236 |
if ( $deleting && !isset( self::$setCookies[$key] ) ) { |
| 237 |
wfDebugLog( 'cookie', "already deleted $logDesc" ); |
| 238 |
return; |
| 239 |
} elseif ( !$deleting && isset( self::$setCookies[$key] ) && |
| 240 |
self::$setCookies[$key] === $optionsForDeduplication |
| 241 |
) { |
| 242 |
wfDebugLog( 'cookie', "already set $logDesc" ); |
| 243 |
return; |
| 244 |
} |
| 245 |
|
| 246 |
wfDebugLog( 'cookie', $logDesc ); |
| 247 |
if ( $func === 'setrawcookie' ) { |
| 248 |
setrawcookie( $prefixedName, $value, $setOptions ); |
| 249 |
} else { |
| 250 |
setcookie( $prefixedName, $value, $setOptions ); |
| 251 |
} |
| 252 |
self::$setCookies[$key] = $deleting ? null : $optionsForDeduplication; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
public function clearCookie( $name, $options = [] ) { |
| 265 |
$this->setCookie( $name, '', time() - 31_536_000 , $options ); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
public function hasCookies() { |
| 275 |
return (bool)self::$setCookies; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
class_alias( WebResponse::class, 'WebResponse' ); |