Edit File by line
/home/barbar84/www/wp-conte.../plugins/updraftp.../vendor/guzzleht.../psr7/src
File: ServerRequest.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace GuzzleHttp\Psr7;
[2] Fix | Delete
[3] Fix | Delete
use InvalidArgumentException;
[4] Fix | Delete
use Psr\Http\Message\ServerRequestInterface;
[5] Fix | Delete
use Psr\Http\Message\StreamInterface;
[6] Fix | Delete
use Psr\Http\Message\UploadedFileInterface;
[7] Fix | Delete
use Psr\Http\Message\UriInterface;
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* Server-side HTTP request
[11] Fix | Delete
*
[12] Fix | Delete
* Extends the Request definition to add methods for accessing incoming data,
[13] Fix | Delete
* specifically server parameters, cookies, matched path parameters, query
[14] Fix | Delete
* string arguments, body parameters, and upload file information.
[15] Fix | Delete
*
[16] Fix | Delete
* "Attributes" are discovered via decomposing the request (and usually
[17] Fix | Delete
* specifically the URI path), and typically will be injected by the application.
[18] Fix | Delete
*
[19] Fix | Delete
* Requests are considered immutable; all methods that might change state are
[20] Fix | Delete
* implemented such that they retain the internal state of the current
[21] Fix | Delete
* message and return a new instance that contains the changed state.
[22] Fix | Delete
*/
[23] Fix | Delete
class ServerRequest extends Request implements ServerRequestInterface
[24] Fix | Delete
{
[25] Fix | Delete
/**
[26] Fix | Delete
* @var array
[27] Fix | Delete
*/
[28] Fix | Delete
private $attributes = [];
[29] Fix | Delete
[30] Fix | Delete
/**
[31] Fix | Delete
* @var array
[32] Fix | Delete
*/
[33] Fix | Delete
private $cookieParams = [];
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* @var array|object|null
[37] Fix | Delete
*/
[38] Fix | Delete
private $parsedBody;
[39] Fix | Delete
[40] Fix | Delete
/**
[41] Fix | Delete
* @var array
[42] Fix | Delete
*/
[43] Fix | Delete
private $queryParams = [];
[44] Fix | Delete
[45] Fix | Delete
/**
[46] Fix | Delete
* @var array
[47] Fix | Delete
*/
[48] Fix | Delete
private $serverParams;
[49] Fix | Delete
[50] Fix | Delete
/**
[51] Fix | Delete
* @var array
[52] Fix | Delete
*/
[53] Fix | Delete
private $uploadedFiles = [];
[54] Fix | Delete
[55] Fix | Delete
/**
[56] Fix | Delete
* @param string $method HTTP method
[57] Fix | Delete
* @param string|UriInterface $uri URI
[58] Fix | Delete
* @param array $headers Request headers
[59] Fix | Delete
* @param string|resource|StreamInterface|null $body Request body
[60] Fix | Delete
* @param string $version Protocol version
[61] Fix | Delete
* @param array $serverParams Typically the $_SERVER superglobal
[62] Fix | Delete
*/
[63] Fix | Delete
public function __construct(
[64] Fix | Delete
$method,
[65] Fix | Delete
$uri,
[66] Fix | Delete
array $headers = [],
[67] Fix | Delete
$body = null,
[68] Fix | Delete
$version = '1.1',
[69] Fix | Delete
array $serverParams = []
[70] Fix | Delete
) {
[71] Fix | Delete
$this->serverParams = $serverParams;
[72] Fix | Delete
[73] Fix | Delete
parent::__construct($method, $uri, $headers, $body, $version);
[74] Fix | Delete
}
[75] Fix | Delete
[76] Fix | Delete
/**
[77] Fix | Delete
* Return an UploadedFile instance array.
[78] Fix | Delete
*
[79] Fix | Delete
* @param array $files A array which respect $_FILES structure
[80] Fix | Delete
*
[81] Fix | Delete
* @return array
[82] Fix | Delete
*
[83] Fix | Delete
* @throws InvalidArgumentException for unrecognized values
[84] Fix | Delete
*/
[85] Fix | Delete
public static function normalizeFiles(array $files)
[86] Fix | Delete
{
[87] Fix | Delete
$normalized = [];
[88] Fix | Delete
[89] Fix | Delete
foreach ($files as $key => $value) {
[90] Fix | Delete
if ($value instanceof UploadedFileInterface) {
[91] Fix | Delete
$normalized[$key] = $value;
[92] Fix | Delete
} elseif (is_array($value) && isset($value['tmp_name'])) {
[93] Fix | Delete
$normalized[$key] = self::createUploadedFileFromSpec($value);
[94] Fix | Delete
} elseif (is_array($value)) {
[95] Fix | Delete
$normalized[$key] = self::normalizeFiles($value);
[96] Fix | Delete
continue;
[97] Fix | Delete
} else {
[98] Fix | Delete
throw new InvalidArgumentException('Invalid value in files specification');
[99] Fix | Delete
}
[100] Fix | Delete
}
[101] Fix | Delete
[102] Fix | Delete
return $normalized;
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
/**
[106] Fix | Delete
* Create and return an UploadedFile instance from a $_FILES specification.
[107] Fix | Delete
*
[108] Fix | Delete
* If the specification represents an array of values, this method will
[109] Fix | Delete
* delegate to normalizeNestedFileSpec() and return that return value.
[110] Fix | Delete
*
[111] Fix | Delete
* @param array $value $_FILES struct
[112] Fix | Delete
*
[113] Fix | Delete
* @return array|UploadedFileInterface
[114] Fix | Delete
*/
[115] Fix | Delete
private static function createUploadedFileFromSpec(array $value)
[116] Fix | Delete
{
[117] Fix | Delete
if (is_array($value['tmp_name'])) {
[118] Fix | Delete
return self::normalizeNestedFileSpec($value);
[119] Fix | Delete
}
[120] Fix | Delete
[121] Fix | Delete
return new UploadedFile(
[122] Fix | Delete
$value['tmp_name'],
[123] Fix | Delete
(int) $value['size'],
[124] Fix | Delete
(int) $value['error'],
[125] Fix | Delete
$value['name'],
[126] Fix | Delete
$value['type']
[127] Fix | Delete
);
[128] Fix | Delete
}
[129] Fix | Delete
[130] Fix | Delete
/**
[131] Fix | Delete
* Normalize an array of file specifications.
[132] Fix | Delete
*
[133] Fix | Delete
* Loops through all nested files and returns a normalized array of
[134] Fix | Delete
* UploadedFileInterface instances.
[135] Fix | Delete
*
[136] Fix | Delete
* @param array $files
[137] Fix | Delete
*
[138] Fix | Delete
* @return UploadedFileInterface[]
[139] Fix | Delete
*/
[140] Fix | Delete
private static function normalizeNestedFileSpec(array $files = [])
[141] Fix | Delete
{
[142] Fix | Delete
$normalizedFiles = [];
[143] Fix | Delete
[144] Fix | Delete
foreach (array_keys($files['tmp_name']) as $key) {
[145] Fix | Delete
$spec = [
[146] Fix | Delete
'tmp_name' => $files['tmp_name'][$key],
[147] Fix | Delete
'size' => $files['size'][$key],
[148] Fix | Delete
'error' => $files['error'][$key],
[149] Fix | Delete
'name' => $files['name'][$key],
[150] Fix | Delete
'type' => $files['type'][$key],
[151] Fix | Delete
];
[152] Fix | Delete
$normalizedFiles[$key] = self::createUploadedFileFromSpec($spec);
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
return $normalizedFiles;
[156] Fix | Delete
}
[157] Fix | Delete
[158] Fix | Delete
/**
[159] Fix | Delete
* Return a ServerRequest populated with superglobals:
[160] Fix | Delete
* $_GET
[161] Fix | Delete
* $_POST
[162] Fix | Delete
* $_COOKIE
[163] Fix | Delete
* $_FILES
[164] Fix | Delete
* $_SERVER
[165] Fix | Delete
*
[166] Fix | Delete
* @return ServerRequestInterface
[167] Fix | Delete
*/
[168] Fix | Delete
public static function fromGlobals()
[169] Fix | Delete
{
[170] Fix | Delete
$method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
[171] Fix | Delete
$headers = getallheaders();
[172] Fix | Delete
$uri = self::getUriFromGlobals();
[173] Fix | Delete
$body = new CachingStream(new LazyOpenStream('php://input', 'r+'));
[174] Fix | Delete
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? str_replace('HTTP/', '', $_SERVER['SERVER_PROTOCOL']) : '1.1';
[175] Fix | Delete
[176] Fix | Delete
$serverRequest = new ServerRequest($method, $uri, $headers, $body, $protocol, $_SERVER);
[177] Fix | Delete
[178] Fix | Delete
return $serverRequest
[179] Fix | Delete
->withCookieParams($_COOKIE)
[180] Fix | Delete
->withQueryParams($_GET)
[181] Fix | Delete
->withParsedBody($_POST)
[182] Fix | Delete
->withUploadedFiles(self::normalizeFiles($_FILES));
[183] Fix | Delete
}
[184] Fix | Delete
[185] Fix | Delete
private static function extractHostAndPortFromAuthority($authority)
[186] Fix | Delete
{
[187] Fix | Delete
$uri = 'http://' . $authority;
[188] Fix | Delete
$parts = parse_url($uri);
[189] Fix | Delete
if (false === $parts) {
[190] Fix | Delete
return [null, null];
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
$host = isset($parts['host']) ? $parts['host'] : null;
[194] Fix | Delete
$port = isset($parts['port']) ? $parts['port'] : null;
[195] Fix | Delete
[196] Fix | Delete
return [$host, $port];
[197] Fix | Delete
}
[198] Fix | Delete
[199] Fix | Delete
/**
[200] Fix | Delete
* Get a Uri populated with values from $_SERVER.
[201] Fix | Delete
*
[202] Fix | Delete
* @return UriInterface
[203] Fix | Delete
*/
[204] Fix | Delete
public static function getUriFromGlobals()
[205] Fix | Delete
{
[206] Fix | Delete
$uri = new Uri('');
[207] Fix | Delete
[208] Fix | Delete
$uri = $uri->withScheme(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http');
[209] Fix | Delete
[210] Fix | Delete
$hasPort = false;
[211] Fix | Delete
if (isset($_SERVER['HTTP_HOST'])) {
[212] Fix | Delete
list($host, $port) = self::extractHostAndPortFromAuthority($_SERVER['HTTP_HOST']);
[213] Fix | Delete
if ($host !== null) {
[214] Fix | Delete
$uri = $uri->withHost($host);
[215] Fix | Delete
}
[216] Fix | Delete
[217] Fix | Delete
if ($port !== null) {
[218] Fix | Delete
$hasPort = true;
[219] Fix | Delete
$uri = $uri->withPort($port);
[220] Fix | Delete
}
[221] Fix | Delete
} elseif (isset($_SERVER['SERVER_NAME'])) {
[222] Fix | Delete
$uri = $uri->withHost($_SERVER['SERVER_NAME']);
[223] Fix | Delete
} elseif (isset($_SERVER['SERVER_ADDR'])) {
[224] Fix | Delete
$uri = $uri->withHost($_SERVER['SERVER_ADDR']);
[225] Fix | Delete
}
[226] Fix | Delete
[227] Fix | Delete
if (!$hasPort && isset($_SERVER['SERVER_PORT'])) {
[228] Fix | Delete
$uri = $uri->withPort($_SERVER['SERVER_PORT']);
[229] Fix | Delete
}
[230] Fix | Delete
[231] Fix | Delete
$hasQuery = false;
[232] Fix | Delete
if (isset($_SERVER['REQUEST_URI'])) {
[233] Fix | Delete
$requestUriParts = explode('?', $_SERVER['REQUEST_URI'], 2);
[234] Fix | Delete
$uri = $uri->withPath($requestUriParts[0]);
[235] Fix | Delete
if (isset($requestUriParts[1])) {
[236] Fix | Delete
$hasQuery = true;
[237] Fix | Delete
$uri = $uri->withQuery($requestUriParts[1]);
[238] Fix | Delete
}
[239] Fix | Delete
}
[240] Fix | Delete
[241] Fix | Delete
if (!$hasQuery && isset($_SERVER['QUERY_STRING'])) {
[242] Fix | Delete
$uri = $uri->withQuery($_SERVER['QUERY_STRING']);
[243] Fix | Delete
}
[244] Fix | Delete
[245] Fix | Delete
return $uri;
[246] Fix | Delete
}
[247] Fix | Delete
[248] Fix | Delete
/**
[249] Fix | Delete
* {@inheritdoc}
[250] Fix | Delete
*/
[251] Fix | Delete
public function getServerParams()
[252] Fix | Delete
{
[253] Fix | Delete
return $this->serverParams;
[254] Fix | Delete
}
[255] Fix | Delete
[256] Fix | Delete
/**
[257] Fix | Delete
* {@inheritdoc}
[258] Fix | Delete
*/
[259] Fix | Delete
public function getUploadedFiles()
[260] Fix | Delete
{
[261] Fix | Delete
return $this->uploadedFiles;
[262] Fix | Delete
}
[263] Fix | Delete
[264] Fix | Delete
/**
[265] Fix | Delete
* {@inheritdoc}
[266] Fix | Delete
*/
[267] Fix | Delete
public function withUploadedFiles(array $uploadedFiles)
[268] Fix | Delete
{
[269] Fix | Delete
$new = clone $this;
[270] Fix | Delete
$new->uploadedFiles = $uploadedFiles;
[271] Fix | Delete
[272] Fix | Delete
return $new;
[273] Fix | Delete
}
[274] Fix | Delete
[275] Fix | Delete
/**
[276] Fix | Delete
* {@inheritdoc}
[277] Fix | Delete
*/
[278] Fix | Delete
public function getCookieParams()
[279] Fix | Delete
{
[280] Fix | Delete
return $this->cookieParams;
[281] Fix | Delete
}
[282] Fix | Delete
[283] Fix | Delete
/**
[284] Fix | Delete
* {@inheritdoc}
[285] Fix | Delete
*/
[286] Fix | Delete
public function withCookieParams(array $cookies)
[287] Fix | Delete
{
[288] Fix | Delete
$new = clone $this;
[289] Fix | Delete
$new->cookieParams = $cookies;
[290] Fix | Delete
[291] Fix | Delete
return $new;
[292] Fix | Delete
}
[293] Fix | Delete
[294] Fix | Delete
/**
[295] Fix | Delete
* {@inheritdoc}
[296] Fix | Delete
*/
[297] Fix | Delete
public function getQueryParams()
[298] Fix | Delete
{
[299] Fix | Delete
return $this->queryParams;
[300] Fix | Delete
}
[301] Fix | Delete
[302] Fix | Delete
/**
[303] Fix | Delete
* {@inheritdoc}
[304] Fix | Delete
*/
[305] Fix | Delete
public function withQueryParams(array $query)
[306] Fix | Delete
{
[307] Fix | Delete
$new = clone $this;
[308] Fix | Delete
$new->queryParams = $query;
[309] Fix | Delete
[310] Fix | Delete
return $new;
[311] Fix | Delete
}
[312] Fix | Delete
[313] Fix | Delete
/**
[314] Fix | Delete
* {@inheritdoc}
[315] Fix | Delete
*/
[316] Fix | Delete
public function getParsedBody()
[317] Fix | Delete
{
[318] Fix | Delete
return $this->parsedBody;
[319] Fix | Delete
}
[320] Fix | Delete
[321] Fix | Delete
/**
[322] Fix | Delete
* {@inheritdoc}
[323] Fix | Delete
*/
[324] Fix | Delete
public function withParsedBody($data)
[325] Fix | Delete
{
[326] Fix | Delete
$new = clone $this;
[327] Fix | Delete
$new->parsedBody = $data;
[328] Fix | Delete
[329] Fix | Delete
return $new;
[330] Fix | Delete
}
[331] Fix | Delete
[332] Fix | Delete
/**
[333] Fix | Delete
* {@inheritdoc}
[334] Fix | Delete
*/
[335] Fix | Delete
public function getAttributes()
[336] Fix | Delete
{
[337] Fix | Delete
return $this->attributes;
[338] Fix | Delete
}
[339] Fix | Delete
[340] Fix | Delete
/**
[341] Fix | Delete
* {@inheritdoc}
[342] Fix | Delete
*/
[343] Fix | Delete
public function getAttribute($attribute, $default = null)
[344] Fix | Delete
{
[345] Fix | Delete
if (false === array_key_exists($attribute, $this->attributes)) {
[346] Fix | Delete
return $default;
[347] Fix | Delete
}
[348] Fix | Delete
[349] Fix | Delete
return $this->attributes[$attribute];
[350] Fix | Delete
}
[351] Fix | Delete
[352] Fix | Delete
/**
[353] Fix | Delete
* {@inheritdoc}
[354] Fix | Delete
*/
[355] Fix | Delete
public function withAttribute($attribute, $value)
[356] Fix | Delete
{
[357] Fix | Delete
$new = clone $this;
[358] Fix | Delete
$new->attributes[$attribute] = $value;
[359] Fix | Delete
[360] Fix | Delete
return $new;
[361] Fix | Delete
}
[362] Fix | Delete
[363] Fix | Delete
/**
[364] Fix | Delete
* {@inheritdoc}
[365] Fix | Delete
*/
[366] Fix | Delete
public function withoutAttribute($attribute)
[367] Fix | Delete
{
[368] Fix | Delete
if (false === array_key_exists($attribute, $this->attributes)) {
[369] Fix | Delete
return $this;
[370] Fix | Delete
}
[371] Fix | Delete
[372] Fix | Delete
$new = clone $this;
[373] Fix | Delete
unset($new->attributes[$attribute]);
[374] Fix | Delete
[375] Fix | Delete
return $new;
[376] Fix | Delete
}
[377] Fix | Delete
}
[378] Fix | Delete
[379] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function