| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Rest; |
| 4 |
|
| 5 |
use GuzzleHttp\Psr7\LazyOpenStream; |
| 6 |
use GuzzleHttp\Psr7\ServerRequest; |
| 7 |
use GuzzleHttp\Psr7\Uri; |
| 8 |
use InvalidArgumentException; |
| 9 |
use MediaWiki\Request\WebRequest; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class RequestFromGlobals extends RequestBase { |
| 18 |
|
| 19 |
private $uri; |
| 20 |
|
| 21 |
private $protocol; |
| 22 |
|
| 23 |
private $uploadedFiles; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public function __construct( $params = [] ) { |
| 30 |
parent::__construct( $params['cookiePrefix'] ?? '' ); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public function getMethod() { |
| 36 |
|
| 37 |
|
| 38 |
return strtoupper( $_SERVER['REQUEST_METHOD'] ?? 'GET' ); |
| 39 |
} |
| 40 |
|
| 41 |
public function getUri() { |
| 42 |
if ( $this->uri === null ) { |
| 43 |
$requestUrl = WebRequest::getGlobalRequestURL(); |
| 44 |
|
| 45 |
try { |
| 46 |
$uriInstance = new Uri( $requestUrl ); |
| 47 |
} catch ( InvalidArgumentException $e ) { |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
$uriInstance = ( |
| 56 |
new Uri( '//HOST:80' . $requestUrl ) |
| 57 |
)->withScheme( '' )->withHost( '' )->withPort( null ); |
| 58 |
} |
| 59 |
$this->uri = $uriInstance; |
| 60 |
} |
| 61 |
return $this->uri; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public function getProtocolVersion() { |
| 67 |
if ( $this->protocol === null ) { |
| 68 |
$serverProtocol = $_SERVER['SERVER_PROTOCOL'] ?? ''; |
| 69 |
$prefix = 'HTTP/'; |
| 70 |
if ( str_starts_with( $serverProtocol, $prefix ) ) { |
| 71 |
$this->protocol = substr( $serverProtocol, strlen( $prefix ) ); |
| 72 |
} else { |
| 73 |
$this->protocol = '1.1'; |
| 74 |
} |
| 75 |
} |
| 76 |
return $this->protocol; |
| 77 |
} |
| 78 |
|
| 79 |
protected function initHeaders() { |
| 80 |
$this->setHeaders( getallheaders() ); |
| 81 |
} |
| 82 |
|
| 83 |
public function getBody() { |
| 84 |
return new LazyOpenStream( 'php://input', 'r' ); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function getServerParams() { |
| 90 |
return $_SERVER; |
| 91 |
} |
| 92 |
|
| 93 |
public function getCookieParams() { |
| 94 |
return $_COOKIE; |
| 95 |
} |
| 96 |
|
| 97 |
public function getQueryParams() { |
| 98 |
return $_GET; |
| 99 |
} |
| 100 |
|
| 101 |
public function getUploadedFiles() { |
| 102 |
$this->uploadedFiles ??= ServerRequest::normalizeFiles( $_FILES ); |
| 103 |
return $this->uploadedFiles; |
| 104 |
} |
| 105 |
|
| 106 |
public function getPostParams() { |
| 107 |
return $_POST; |
| 108 |
} |
| 109 |
|
| 110 |
} |