Edit File by line
/home/barbar84/www/wp-conte.../plugins/worker/src/MWP/Stream
File: Base64EncodedStream.php
<?php
[0] Fix | Delete
/*
[1] Fix | Delete
* This file is part of the ManageWP Worker plugin.
[2] Fix | Delete
*
[3] Fix | Delete
* (c) ManageWP LLC <contact@managewp.com>
[4] Fix | Delete
*
[5] Fix | Delete
* For the full copyright and license information, please view the LICENSE
[6] Fix | Delete
* file that was distributed with this source code.
[7] Fix | Delete
*/
[8] Fix | Delete
[9] Fix | Delete
class MWP_Stream_Base64EncodedStream extends MWP_Stream_Decorator
[10] Fix | Delete
{
[11] Fix | Delete
[12] Fix | Delete
/** @var MWP_Stream_Interface */
[13] Fix | Delete
private $buffer;
[14] Fix | Delete
[15] Fix | Delete
const BASE64_BLOCK_SIZE = 4;
[16] Fix | Delete
const ORIGIN_BLOCK_SIZE = 3;
[17] Fix | Delete
[18] Fix | Delete
public function __construct(MWP_Stream_Interface $stream)
[19] Fix | Delete
{
[20] Fix | Delete
parent::__construct($stream);
[21] Fix | Delete
$this->buffer = new MWP_Stream_Buffer();
[22] Fix | Delete
}
[23] Fix | Delete
[24] Fix | Delete
public function eof()
[25] Fix | Delete
{
[26] Fix | Delete
return $this->buffer->eof() && $this->getStream()->eof();
[27] Fix | Delete
}
[28] Fix | Delete
[29] Fix | Delete
public function read($length)
[30] Fix | Delete
{
[31] Fix | Delete
$readFromBuffer = $this->buffer->read($length);
[32] Fix | Delete
if (strlen($readFromBuffer) === $length) {
[33] Fix | Delete
return $readFromBuffer;
[34] Fix | Delete
}
[35] Fix | Delete
[36] Fix | Delete
$remaining = $length - strlen($readFromBuffer);
[37] Fix | Delete
[38] Fix | Delete
// Calculate the approximate length required to read so that the base64 encoded stream does not have padding.
[39] Fix | Delete
// base64 is calculated for blocks of 3 input characters resulting in 4 output characters.
[40] Fix | Delete
// strlen(base64_encode($str)) ==> strlen($str) * 4 / 3
[41] Fix | Delete
//
[42] Fix | Delete
// This leads to:
[43] Fix | Delete
//
[44] Fix | Delete
// strlen($str) ==> strlen(base64_encode($str)) * 3 / 4
[45] Fix | Delete
//
[46] Fix | Delete
// Meaning, to read $length characters from the base64 encoded string, read 3/4 of $length from the original stream.
[47] Fix | Delete
// $length is first rounded to the first larger number divisible by 4 since base64 encoded strings come in blocks of 4 characters.
[48] Fix | Delete
$closestGroupLength = $remaining + (self::BASE64_BLOCK_SIZE - $remaining % self::BASE64_BLOCK_SIZE);
[49] Fix | Delete
$read = $closestGroupLength * self::ORIGIN_BLOCK_SIZE / self::BASE64_BLOCK_SIZE;
[50] Fix | Delete
$this->buffer->write(base64_encode($this->getStream()->read($read)));
[51] Fix | Delete
[52] Fix | Delete
return $readFromBuffer.$this->buffer->read($remaining);
[53] Fix | Delete
}
[54] Fix | Delete
}
[55] Fix | Delete
[56] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function