Edit File by line
/home/barbar84/www/wp-conte.../plugins/updraftp.../vendor/guzzleht.../psr7/src
File: Utils.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace GuzzleHttp\Psr7;
[2] Fix | Delete
[3] Fix | Delete
use Psr\Http\Message\RequestInterface;
[4] Fix | Delete
use Psr\Http\Message\ServerRequestInterface;
[5] Fix | Delete
use Psr\Http\Message\StreamInterface;
[6] Fix | Delete
use Psr\Http\Message\UriInterface;
[7] Fix | Delete
[8] Fix | Delete
final class Utils
[9] Fix | Delete
{
[10] Fix | Delete
/**
[11] Fix | Delete
* Remove the items given by the keys, case insensitively from the data.
[12] Fix | Delete
*
[13] Fix | Delete
* @param iterable<string> $keys
[14] Fix | Delete
*
[15] Fix | Delete
* @return array
[16] Fix | Delete
*/
[17] Fix | Delete
public static function caselessRemove($keys, array $data)
[18] Fix | Delete
{
[19] Fix | Delete
$result = [];
[20] Fix | Delete
[21] Fix | Delete
foreach ($keys as &$key) {
[22] Fix | Delete
$key = strtolower($key);
[23] Fix | Delete
}
[24] Fix | Delete
[25] Fix | Delete
foreach ($data as $k => $v) {
[26] Fix | Delete
if (!in_array(strtolower($k), $keys)) {
[27] Fix | Delete
$result[$k] = $v;
[28] Fix | Delete
}
[29] Fix | Delete
}
[30] Fix | Delete
[31] Fix | Delete
return $result;
[32] Fix | Delete
}
[33] Fix | Delete
[34] Fix | Delete
/**
[35] Fix | Delete
* Copy the contents of a stream into another stream until the given number
[36] Fix | Delete
* of bytes have been read.
[37] Fix | Delete
*
[38] Fix | Delete
* @param StreamInterface $source Stream to read from
[39] Fix | Delete
* @param StreamInterface $dest Stream to write to
[40] Fix | Delete
* @param int $maxLen Maximum number of bytes to read. Pass -1
[41] Fix | Delete
* to read the entire stream.
[42] Fix | Delete
*
[43] Fix | Delete
* @throws \RuntimeException on error.
[44] Fix | Delete
*/
[45] Fix | Delete
public static function copyToStream(StreamInterface $source, StreamInterface $dest, $maxLen = -1)
[46] Fix | Delete
{
[47] Fix | Delete
$bufferSize = 8192;
[48] Fix | Delete
[49] Fix | Delete
if ($maxLen === -1) {
[50] Fix | Delete
while (!$source->eof()) {
[51] Fix | Delete
if (!$dest->write($source->read($bufferSize))) {
[52] Fix | Delete
break;
[53] Fix | Delete
}
[54] Fix | Delete
}
[55] Fix | Delete
} else {
[56] Fix | Delete
$remaining = $maxLen;
[57] Fix | Delete
while ($remaining > 0 && !$source->eof()) {
[58] Fix | Delete
$buf = $source->read(min($bufferSize, $remaining));
[59] Fix | Delete
$len = strlen($buf);
[60] Fix | Delete
if (!$len) {
[61] Fix | Delete
break;
[62] Fix | Delete
}
[63] Fix | Delete
$remaining -= $len;
[64] Fix | Delete
$dest->write($buf);
[65] Fix | Delete
}
[66] Fix | Delete
}
[67] Fix | Delete
}
[68] Fix | Delete
[69] Fix | Delete
/**
[70] Fix | Delete
* Copy the contents of a stream into a string until the given number of
[71] Fix | Delete
* bytes have been read.
[72] Fix | Delete
*
[73] Fix | Delete
* @param StreamInterface $stream Stream to read
[74] Fix | Delete
* @param int $maxLen Maximum number of bytes to read. Pass -1
[75] Fix | Delete
* to read the entire stream.
[76] Fix | Delete
*
[77] Fix | Delete
* @return string
[78] Fix | Delete
*
[79] Fix | Delete
* @throws \RuntimeException on error.
[80] Fix | Delete
*/
[81] Fix | Delete
public static function copyToString(StreamInterface $stream, $maxLen = -1)
[82] Fix | Delete
{
[83] Fix | Delete
$buffer = '';
[84] Fix | Delete
[85] Fix | Delete
if ($maxLen === -1) {
[86] Fix | Delete
while (!$stream->eof()) {
[87] Fix | Delete
$buf = $stream->read(1048576);
[88] Fix | Delete
// Using a loose equality here to match on '' and false.
[89] Fix | Delete
if ($buf == null) {
[90] Fix | Delete
break;
[91] Fix | Delete
}
[92] Fix | Delete
$buffer .= $buf;
[93] Fix | Delete
}
[94] Fix | Delete
return $buffer;
[95] Fix | Delete
}
[96] Fix | Delete
[97] Fix | Delete
$len = 0;
[98] Fix | Delete
while (!$stream->eof() && $len < $maxLen) {
[99] Fix | Delete
$buf = $stream->read($maxLen - $len);
[100] Fix | Delete
// Using a loose equality here to match on '' and false.
[101] Fix | Delete
if ($buf == null) {
[102] Fix | Delete
break;
[103] Fix | Delete
}
[104] Fix | Delete
$buffer .= $buf;
[105] Fix | Delete
$len = strlen($buffer);
[106] Fix | Delete
}
[107] Fix | Delete
[108] Fix | Delete
return $buffer;
[109] Fix | Delete
}
[110] Fix | Delete
[111] Fix | Delete
/**
[112] Fix | Delete
* Calculate a hash of a stream.
[113] Fix | Delete
*
[114] Fix | Delete
* This method reads the entire stream to calculate a rolling hash, based
[115] Fix | Delete
* on PHP's `hash_init` functions.
[116] Fix | Delete
*
[117] Fix | Delete
* @param StreamInterface $stream Stream to calculate the hash for
[118] Fix | Delete
* @param string $algo Hash algorithm (e.g. md5, crc32, etc)
[119] Fix | Delete
* @param bool $rawOutput Whether or not to use raw output
[120] Fix | Delete
*
[121] Fix | Delete
* @return string Returns the hash of the stream
[122] Fix | Delete
*
[123] Fix | Delete
* @throws \RuntimeException on error.
[124] Fix | Delete
*/
[125] Fix | Delete
public static function hash(StreamInterface $stream, $algo, $rawOutput = false)
[126] Fix | Delete
{
[127] Fix | Delete
$pos = $stream->tell();
[128] Fix | Delete
[129] Fix | Delete
if ($pos > 0) {
[130] Fix | Delete
$stream->rewind();
[131] Fix | Delete
}
[132] Fix | Delete
[133] Fix | Delete
$ctx = hash_init($algo);
[134] Fix | Delete
while (!$stream->eof()) {
[135] Fix | Delete
hash_update($ctx, $stream->read(1048576));
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
$out = hash_final($ctx, (bool) $rawOutput);
[139] Fix | Delete
$stream->seek($pos);
[140] Fix | Delete
[141] Fix | Delete
return $out;
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
/**
[145] Fix | Delete
* Clone and modify a request with the given changes.
[146] Fix | Delete
*
[147] Fix | Delete
* This method is useful for reducing the number of clones needed to mutate
[148] Fix | Delete
* a message.
[149] Fix | Delete
*
[150] Fix | Delete
* The changes can be one of:
[151] Fix | Delete
* - method: (string) Changes the HTTP method.
[152] Fix | Delete
* - set_headers: (array) Sets the given headers.
[153] Fix | Delete
* - remove_headers: (array) Remove the given headers.
[154] Fix | Delete
* - body: (mixed) Sets the given body.
[155] Fix | Delete
* - uri: (UriInterface) Set the URI.
[156] Fix | Delete
* - query: (string) Set the query string value of the URI.
[157] Fix | Delete
* - version: (string) Set the protocol version.
[158] Fix | Delete
*
[159] Fix | Delete
* @param RequestInterface $request Request to clone and modify.
[160] Fix | Delete
* @param array $changes Changes to apply.
[161] Fix | Delete
*
[162] Fix | Delete
* @return RequestInterface
[163] Fix | Delete
*/
[164] Fix | Delete
public static function modifyRequest(RequestInterface $request, array $changes)
[165] Fix | Delete
{
[166] Fix | Delete
if (!$changes) {
[167] Fix | Delete
return $request;
[168] Fix | Delete
}
[169] Fix | Delete
[170] Fix | Delete
$headers = $request->getHeaders();
[171] Fix | Delete
[172] Fix | Delete
if (!isset($changes['uri'])) {
[173] Fix | Delete
$uri = $request->getUri();
[174] Fix | Delete
} else {
[175] Fix | Delete
// Remove the host header if one is on the URI
[176] Fix | Delete
if ($host = $changes['uri']->getHost()) {
[177] Fix | Delete
$changes['set_headers']['Host'] = $host;
[178] Fix | Delete
[179] Fix | Delete
if ($port = $changes['uri']->getPort()) {
[180] Fix | Delete
$standardPorts = ['http' => 80, 'https' => 443];
[181] Fix | Delete
$scheme = $changes['uri']->getScheme();
[182] Fix | Delete
if (isset($standardPorts[$scheme]) && $port != $standardPorts[$scheme]) {
[183] Fix | Delete
$changes['set_headers']['Host'] .= ':' . $port;
[184] Fix | Delete
}
[185] Fix | Delete
}
[186] Fix | Delete
}
[187] Fix | Delete
$uri = $changes['uri'];
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
if (!empty($changes['remove_headers'])) {
[191] Fix | Delete
$headers = self::caselessRemove($changes['remove_headers'], $headers);
[192] Fix | Delete
}
[193] Fix | Delete
[194] Fix | Delete
if (!empty($changes['set_headers'])) {
[195] Fix | Delete
$headers = self::caselessRemove(array_keys($changes['set_headers']), $headers);
[196] Fix | Delete
$headers = $changes['set_headers'] + $headers;
[197] Fix | Delete
}
[198] Fix | Delete
[199] Fix | Delete
if (isset($changes['query'])) {
[200] Fix | Delete
$uri = $uri->withQuery($changes['query']);
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
if ($request instanceof ServerRequestInterface) {
[204] Fix | Delete
$new = (new ServerRequest(
[205] Fix | Delete
isset($changes['method']) ? $changes['method'] : $request->getMethod(),
[206] Fix | Delete
$uri,
[207] Fix | Delete
$headers,
[208] Fix | Delete
isset($changes['body']) ? $changes['body'] : $request->getBody(),
[209] Fix | Delete
isset($changes['version'])
[210] Fix | Delete
? $changes['version']
[211] Fix | Delete
: $request->getProtocolVersion(),
[212] Fix | Delete
$request->getServerParams()
[213] Fix | Delete
))
[214] Fix | Delete
->withParsedBody($request->getParsedBody())
[215] Fix | Delete
->withQueryParams($request->getQueryParams())
[216] Fix | Delete
->withCookieParams($request->getCookieParams())
[217] Fix | Delete
->withUploadedFiles($request->getUploadedFiles());
[218] Fix | Delete
[219] Fix | Delete
foreach ($request->getAttributes() as $key => $value) {
[220] Fix | Delete
$new = $new->withAttribute($key, $value);
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
return $new;
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
return new Request(
[227] Fix | Delete
isset($changes['method']) ? $changes['method'] : $request->getMethod(),
[228] Fix | Delete
$uri,
[229] Fix | Delete
$headers,
[230] Fix | Delete
isset($changes['body']) ? $changes['body'] : $request->getBody(),
[231] Fix | Delete
isset($changes['version'])
[232] Fix | Delete
? $changes['version']
[233] Fix | Delete
: $request->getProtocolVersion()
[234] Fix | Delete
);
[235] Fix | Delete
}
[236] Fix | Delete
[237] Fix | Delete
/**
[238] Fix | Delete
* Read a line from the stream up to the maximum allowed buffer length.
[239] Fix | Delete
*
[240] Fix | Delete
* @param StreamInterface $stream Stream to read from
[241] Fix | Delete
* @param int|null $maxLength Maximum buffer length
[242] Fix | Delete
*
[243] Fix | Delete
* @return string
[244] Fix | Delete
*/
[245] Fix | Delete
public static function readLine(StreamInterface $stream, $maxLength = null)
[246] Fix | Delete
{
[247] Fix | Delete
$buffer = '';
[248] Fix | Delete
$size = 0;
[249] Fix | Delete
[250] Fix | Delete
while (!$stream->eof()) {
[251] Fix | Delete
// Using a loose equality here to match on '' and false.
[252] Fix | Delete
if (null == ($byte = $stream->read(1))) {
[253] Fix | Delete
return $buffer;
[254] Fix | Delete
}
[255] Fix | Delete
$buffer .= $byte;
[256] Fix | Delete
// Break when a new line is found or the max length - 1 is reached
[257] Fix | Delete
if ($byte === "\n" || ++$size === $maxLength - 1) {
[258] Fix | Delete
break;
[259] Fix | Delete
}
[260] Fix | Delete
}
[261] Fix | Delete
[262] Fix | Delete
return $buffer;
[263] Fix | Delete
}
[264] Fix | Delete
[265] Fix | Delete
/**
[266] Fix | Delete
* Create a new stream based on the input type.
[267] Fix | Delete
*
[268] Fix | Delete
* Options is an associative array that can contain the following keys:
[269] Fix | Delete
* - metadata: Array of custom metadata.
[270] Fix | Delete
* - size: Size of the stream.
[271] Fix | Delete
*
[272] Fix | Delete
* This method accepts the following `$resource` types:
[273] Fix | Delete
* - `Psr\Http\Message\StreamInterface`: Returns the value as-is.
[274] Fix | Delete
* - `string`: Creates a stream object that uses the given string as the contents.
[275] Fix | Delete
* - `resource`: Creates a stream object that wraps the given PHP stream resource.
[276] Fix | Delete
* - `Iterator`: If the provided value implements `Iterator`, then a read-only
[277] Fix | Delete
* stream object will be created that wraps the given iterable. Each time the
[278] Fix | Delete
* stream is read from, data from the iterator will fill a buffer and will be
[279] Fix | Delete
* continuously called until the buffer is equal to the requested read size.
[280] Fix | Delete
* Subsequent read calls will first read from the buffer and then call `next`
[281] Fix | Delete
* on the underlying iterator until it is exhausted.
[282] Fix | Delete
* - `object` with `__toString()`: If the object has the `__toString()` method,
[283] Fix | Delete
* the object will be cast to a string and then a stream will be returned that
[284] Fix | Delete
* uses the string value.
[285] Fix | Delete
* - `NULL`: When `null` is passed, an empty stream object is returned.
[286] Fix | Delete
* - `callable` When a callable is passed, a read-only stream object will be
[287] Fix | Delete
* created that invokes the given callable. The callable is invoked with the
[288] Fix | Delete
* number of suggested bytes to read. The callable can return any number of
[289] Fix | Delete
* bytes, but MUST return `false` when there is no more data to return. The
[290] Fix | Delete
* stream object that wraps the callable will invoke the callable until the
[291] Fix | Delete
* number of requested bytes are available. Any additional bytes will be
[292] Fix | Delete
* buffered and used in subsequent reads.
[293] Fix | Delete
*
[294] Fix | Delete
* @param resource|string|int|float|bool|StreamInterface|callable|\Iterator|null $resource Entity body data
[295] Fix | Delete
* @param array $options Additional options
[296] Fix | Delete
*
[297] Fix | Delete
* @return StreamInterface
[298] Fix | Delete
*
[299] Fix | Delete
* @throws \InvalidArgumentException if the $resource arg is not valid.
[300] Fix | Delete
*/
[301] Fix | Delete
public static function streamFor($resource = '', array $options = [])
[302] Fix | Delete
{
[303] Fix | Delete
if (is_scalar($resource)) {
[304] Fix | Delete
$stream = self::tryFopen('php://temp', 'r+');
[305] Fix | Delete
if ($resource !== '') {
[306] Fix | Delete
fwrite($stream, $resource);
[307] Fix | Delete
fseek($stream, 0);
[308] Fix | Delete
}
[309] Fix | Delete
return new Stream($stream, $options);
[310] Fix | Delete
}
[311] Fix | Delete
[312] Fix | Delete
switch (gettype($resource)) {
[313] Fix | Delete
case 'resource':
[314] Fix | Delete
/*
[315] Fix | Delete
* The 'php://input' is a special stream with quirks and inconsistencies.
[316] Fix | Delete
* We avoid using that stream by reading it into php://temp
[317] Fix | Delete
*/
[318] Fix | Delete
$metaData = \stream_get_meta_data($resource);
[319] Fix | Delete
if (isset($metaData['uri']) && $metaData['uri'] === 'php://input') {
[320] Fix | Delete
$stream = self::tryFopen('php://temp', 'w+');
[321] Fix | Delete
fwrite($stream, stream_get_contents($resource));
[322] Fix | Delete
fseek($stream, 0);
[323] Fix | Delete
$resource = $stream;
[324] Fix | Delete
}
[325] Fix | Delete
return new Stream($resource, $options);
[326] Fix | Delete
case 'object':
[327] Fix | Delete
if ($resource instanceof StreamInterface) {
[328] Fix | Delete
return $resource;
[329] Fix | Delete
} elseif ($resource instanceof \Iterator) {
[330] Fix | Delete
return new PumpStream(function () use ($resource) {
[331] Fix | Delete
if (!$resource->valid()) {
[332] Fix | Delete
return false;
[333] Fix | Delete
}
[334] Fix | Delete
$result = $resource->current();
[335] Fix | Delete
$resource->next();
[336] Fix | Delete
return $result;
[337] Fix | Delete
}, $options);
[338] Fix | Delete
} elseif (method_exists($resource, '__toString')) {
[339] Fix | Delete
return Utils::streamFor((string) $resource, $options);
[340] Fix | Delete
}
[341] Fix | Delete
break;
[342] Fix | Delete
case 'NULL':
[343] Fix | Delete
return new Stream(self::tryFopen('php://temp', 'r+'), $options);
[344] Fix | Delete
}
[345] Fix | Delete
[346] Fix | Delete
if (is_callable($resource)) {
[347] Fix | Delete
return new PumpStream($resource, $options);
[348] Fix | Delete
}
[349] Fix | Delete
[350] Fix | Delete
throw new \InvalidArgumentException('Invalid resource type: ' . gettype($resource));
[351] Fix | Delete
}
[352] Fix | Delete
[353] Fix | Delete
/**
[354] Fix | Delete
* Safely opens a PHP stream resource using a filename.
[355] Fix | Delete
*
[356] Fix | Delete
* When fopen fails, PHP normally raises a warning. This function adds an
[357] Fix | Delete
* error handler that checks for errors and throws an exception instead.
[358] Fix | Delete
*
[359] Fix | Delete
* @param string $filename File to open
[360] Fix | Delete
* @param string $mode Mode used to open the file
[361] Fix | Delete
*
[362] Fix | Delete
* @return resource
[363] Fix | Delete
*
[364] Fix | Delete
* @throws \RuntimeException if the file cannot be opened
[365] Fix | Delete
*/
[366] Fix | Delete
public static function tryFopen($filename, $mode)
[367] Fix | Delete
{
[368] Fix | Delete
$ex = null;
[369] Fix | Delete
set_error_handler(function () use ($filename, $mode, &$ex) {
[370] Fix | Delete
$ex = new \RuntimeException(sprintf(
[371] Fix | Delete
'Unable to open "%s" using mode "%s": %s',
[372] Fix | Delete
$filename,
[373] Fix | Delete
$mode,
[374] Fix | Delete
func_get_args()[1]
[375] Fix | Delete
));
[376] Fix | Delete
[377] Fix | Delete
return true;
[378] Fix | Delete
});
[379] Fix | Delete
[380] Fix | Delete
try {
[381] Fix | Delete
$handle = fopen($filename, $mode);
[382] Fix | Delete
} catch (\Throwable $e) {
[383] Fix | Delete
$ex = new \RuntimeException(sprintf(
[384] Fix | Delete
'Unable to open "%s" using mode "%s": %s',
[385] Fix | Delete
$filename,
[386] Fix | Delete
$mode,
[387] Fix | Delete
$e->getMessage()
[388] Fix | Delete
), 0, $e);
[389] Fix | Delete
}
[390] Fix | Delete
[391] Fix | Delete
restore_error_handler();
[392] Fix | Delete
[393] Fix | Delete
if ($ex) {
[394] Fix | Delete
/** @var $ex \RuntimeException */
[395] Fix | Delete
throw $ex;
[396] Fix | Delete
}
[397] Fix | Delete
[398] Fix | Delete
return $handle;
[399] Fix | Delete
}
[400] Fix | Delete
[401] Fix | Delete
/**
[402] Fix | Delete
* Returns a UriInterface for the given value.
[403] Fix | Delete
*
[404] Fix | Delete
* This function accepts a string or UriInterface and returns a
[405] Fix | Delete
* UriInterface for the given value. If the value is already a
[406] Fix | Delete
* UriInterface, it is returned as-is.
[407] Fix | Delete
*
[408] Fix | Delete
* @param string|UriInterface $uri
[409] Fix | Delete
*
[410] Fix | Delete
* @return UriInterface
[411] Fix | Delete
*
[412] Fix | Delete
* @throws \InvalidArgumentException
[413] Fix | Delete
*/
[414] Fix | Delete
public static function uriFor($uri)
[415] Fix | Delete
{
[416] Fix | Delete
if ($uri instanceof UriInterface) {
[417] Fix | Delete
return $uri;
[418] Fix | Delete
}
[419] Fix | Delete
[420] Fix | Delete
if (is_string($uri)) {
[421] Fix | Delete
return new Uri($uri);
[422] Fix | Delete
}
[423] Fix | Delete
[424] Fix | Delete
throw new \InvalidArgumentException('URI must be a string or UriInterface');
[425] Fix | Delete
}
[426] Fix | Delete
}
[427] Fix | Delete
[428] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function