Edit File by line

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