| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace MediaWiki\Request; |
| 25 |
|
| 26 |
use MediaWiki\Config\Config; |
| 27 |
use MediaWiki\MainConfigNames; |
| 28 |
use MediaWiki\MediaWikiServices; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class FauxResponse extends WebResponse { |
| 34 |
|
| 35 |
private $headers; |
| 36 |
|
| 37 |
private $cookies = []; |
| 38 |
|
| 39 |
private $code; |
| 40 |
|
| 41 |
|
| 42 |
private $cookieConfig = null; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function header( $string, $replace = true, $http_response_code = null ) { |
| 51 |
if ( $this->disableForPostSend ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
if ( str_starts_with( $string, 'HTTP/' ) ) { |
| 56 |
$parts = explode( ' ', $string, 3 ); |
| 57 |
$this->code = intval( $parts[1] ); |
| 58 |
} else { |
| 59 |
[ $key, $val ] = array_map( 'trim', explode( ":", $string, 2 ) ); |
| 60 |
|
| 61 |
$key = strtoupper( $key ); |
| 62 |
|
| 63 |
if ( $replace || !isset( $this->headers[$key] ) ) { |
| 64 |
$this->headers[$key] = $val; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
if ( $http_response_code ) { |
| 69 |
$this->code = intval( $http_response_code ); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function statusHeader( $code ) { |
| 78 |
$this->code = intval( $code ); |
| 79 |
} |
| 80 |
|
| 81 |
public function headersSent() { |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function getHeader( $key ) { |
| 90 |
$key = strtoupper( $key ); |
| 91 |
|
| 92 |
return $this->headers[$key] ?? null; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
public function getHeaders(): array { |
| 97 |
return $this->headers ?? []; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function getStatusCode() { |
| 106 |
return $this->code; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
private function getCookieConfig(): Config { |
| 113 |
if ( !$this->cookieConfig ) { |
| 114 |
$this->cookieConfig = MediaWikiServices::getInstance()->getMainConfig(); |
| 115 |
} |
| 116 |
return $this->cookieConfig; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public function setCookieConfig( Config $cookieConfig ): void { |
| 123 |
$this->cookieConfig = $cookieConfig; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public function setCookie( $name, $value, $expire = 0, $options = [] ) { |
| 133 |
if ( $this->disableForPostSend ) { |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
$cookieConfig = $this->getCookieConfig(); |
| 138 |
$cookiePath = $cookieConfig->get( MainConfigNames::CookiePath ); |
| 139 |
$cookiePrefix = $cookieConfig->get( MainConfigNames::CookiePrefix ); |
| 140 |
$cookieDomain = $cookieConfig->get( MainConfigNames::CookieDomain ); |
| 141 |
$cookieSecure = $cookieConfig->get( MainConfigNames::CookieSecure ); |
| 142 |
$cookieExpiration = $cookieConfig->get( MainConfigNames::CookieExpiration ); |
| 143 |
$cookieHttpOnly = $cookieConfig->get( MainConfigNames::CookieHttpOnly ); |
| 144 |
$options = array_filter( $options, static function ( $a ) { |
| 145 |
return $a !== null; |
| 146 |
} ) + [ |
| 147 |
'prefix' => $cookiePrefix, |
| 148 |
'domain' => $cookieDomain, |
| 149 |
'path' => $cookiePath, |
| 150 |
'secure' => $cookieSecure, |
| 151 |
'httpOnly' => $cookieHttpOnly, |
| 152 |
'raw' => false, |
| 153 |
]; |
| 154 |
|
| 155 |
if ( $expire === null ) { |
| 156 |
$expire = 0; |
| 157 |
} elseif ( $expire == 0 && $cookieExpiration != 0 ) { |
| 158 |
$expire = time() + $cookieExpiration; |
| 159 |
} |
| 160 |
|
| 161 |
$this->cookies[$options['prefix'] . $name] = [ |
| 162 |
'value' => (string)$value, |
| 163 |
'expire' => (int)$expire, |
| 164 |
'path' => (string)$options['path'], |
| 165 |
'domain' => (string)$options['domain'], |
| 166 |
'secure' => (bool)$options['secure'], |
| 167 |
'httpOnly' => (bool)$options['httpOnly'], |
| 168 |
'raw' => (bool)$options['raw'], |
| 169 |
]; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public function getCookie( $name ) { |
| 177 |
if ( isset( $this->cookies[$name] ) ) { |
| 178 |
return $this->cookies[$name]['value']; |
| 179 |
} |
| 180 |
return null; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public function getCookieData( $name ) { |
| 188 |
return $this->cookies[$name] ?? null; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public function getCookies() { |
| 195 |
return $this->cookies; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public function hasCookies() { |
| 202 |
return count( $this->cookies ) > 0; |
| 203 |
} |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
class_alias( FauxResponse::class, 'FauxResponse' ); |