Edit File by line
/home/barbar84/www/wp-conte.../plugins/updraftp.../vendor/guzzleht.../psr7/src
File: MultipartStream.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace GuzzleHttp\Psr7;
[2] Fix | Delete
[3] Fix | Delete
use Psr\Http\Message\StreamInterface;
[4] Fix | Delete
[5] Fix | Delete
/**
[6] Fix | Delete
* Stream that when read returns bytes for a streaming multipart or
[7] Fix | Delete
* multipart/form-data stream.
[8] Fix | Delete
*
[9] Fix | Delete
* @final
[10] Fix | Delete
*/
[11] Fix | Delete
class MultipartStream implements StreamInterface
[12] Fix | Delete
{
[13] Fix | Delete
use StreamDecoratorTrait;
[14] Fix | Delete
[15] Fix | Delete
private $boundary;
[16] Fix | Delete
[17] Fix | Delete
/**
[18] Fix | Delete
* @param array $elements Array of associative arrays, each containing a
[19] Fix | Delete
* required "name" key mapping to the form field,
[20] Fix | Delete
* name, a required "contents" key mapping to a
[21] Fix | Delete
* StreamInterface/resource/string, an optional
[22] Fix | Delete
* "headers" associative array of custom headers,
[23] Fix | Delete
* and an optional "filename" key mapping to a
[24] Fix | Delete
* string to send as the filename in the part.
[25] Fix | Delete
* @param string $boundary You can optionally provide a specific boundary
[26] Fix | Delete
*
[27] Fix | Delete
* @throws \InvalidArgumentException
[28] Fix | Delete
*/
[29] Fix | Delete
public function __construct(array $elements = [], $boundary = null)
[30] Fix | Delete
{
[31] Fix | Delete
$this->boundary = $boundary ?: sha1(uniqid('', true));
[32] Fix | Delete
$this->stream = $this->createStream($elements);
[33] Fix | Delete
}
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* Get the boundary
[37] Fix | Delete
*
[38] Fix | Delete
* @return string
[39] Fix | Delete
*/
[40] Fix | Delete
public function getBoundary()
[41] Fix | Delete
{
[42] Fix | Delete
return $this->boundary;
[43] Fix | Delete
}
[44] Fix | Delete
[45] Fix | Delete
public function isWritable()
[46] Fix | Delete
{
[47] Fix | Delete
return false;
[48] Fix | Delete
}
[49] Fix | Delete
[50] Fix | Delete
/**
[51] Fix | Delete
* Get the headers needed before transferring the content of a POST file
[52] Fix | Delete
*/
[53] Fix | Delete
private function getHeaders(array $headers)
[54] Fix | Delete
{
[55] Fix | Delete
$str = '';
[56] Fix | Delete
foreach ($headers as $key => $value) {
[57] Fix | Delete
$str .= "{$key}: {$value}\r\n";
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
return "--{$this->boundary}\r\n" . trim($str) . "\r\n\r\n";
[61] Fix | Delete
}
[62] Fix | Delete
[63] Fix | Delete
/**
[64] Fix | Delete
* Create the aggregate stream that will be used to upload the POST data
[65] Fix | Delete
*/
[66] Fix | Delete
protected function createStream(array $elements)
[67] Fix | Delete
{
[68] Fix | Delete
$stream = new AppendStream();
[69] Fix | Delete
[70] Fix | Delete
foreach ($elements as $element) {
[71] Fix | Delete
$this->addElement($stream, $element);
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
// Add the trailing boundary with CRLF
[75] Fix | Delete
$stream->addStream(Utils::streamFor("--{$this->boundary}--\r\n"));
[76] Fix | Delete
[77] Fix | Delete
return $stream;
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
private function addElement(AppendStream $stream, array $element)
[81] Fix | Delete
{
[82] Fix | Delete
foreach (['contents', 'name'] as $key) {
[83] Fix | Delete
if (!array_key_exists($key, $element)) {
[84] Fix | Delete
throw new \InvalidArgumentException("A '{$key}' key is required");
[85] Fix | Delete
}
[86] Fix | Delete
}
[87] Fix | Delete
[88] Fix | Delete
$element['contents'] = Utils::streamFor($element['contents']);
[89] Fix | Delete
[90] Fix | Delete
if (empty($element['filename'])) {
[91] Fix | Delete
$uri = $element['contents']->getMetadata('uri');
[92] Fix | Delete
if (substr($uri, 0, 6) !== 'php://') {
[93] Fix | Delete
$element['filename'] = $uri;
[94] Fix | Delete
}
[95] Fix | Delete
}
[96] Fix | Delete
[97] Fix | Delete
list($body, $headers) = $this->createElement(
[98] Fix | Delete
$element['name'],
[99] Fix | Delete
$element['contents'],
[100] Fix | Delete
isset($element['filename']) ? $element['filename'] : null,
[101] Fix | Delete
isset($element['headers']) ? $element['headers'] : []
[102] Fix | Delete
);
[103] Fix | Delete
[104] Fix | Delete
$stream->addStream(Utils::streamFor($this->getHeaders($headers)));
[105] Fix | Delete
$stream->addStream($body);
[106] Fix | Delete
$stream->addStream(Utils::streamFor("\r\n"));
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
/**
[110] Fix | Delete
* @return array
[111] Fix | Delete
*/
[112] Fix | Delete
private function createElement($name, StreamInterface $stream, $filename, array $headers)
[113] Fix | Delete
{
[114] Fix | Delete
// Set a default content-disposition header if one was no provided
[115] Fix | Delete
$disposition = $this->getHeader($headers, 'content-disposition');
[116] Fix | Delete
if (!$disposition) {
[117] Fix | Delete
$headers['Content-Disposition'] = ($filename === '0' || $filename)
[118] Fix | Delete
? sprintf(
[119] Fix | Delete
'form-data; name="%s"; filename="%s"',
[120] Fix | Delete
$name,
[121] Fix | Delete
basename($filename)
[122] Fix | Delete
)
[123] Fix | Delete
: "form-data; name=\"{$name}\"";
[124] Fix | Delete
}
[125] Fix | Delete
[126] Fix | Delete
// Set a default content-length header if one was no provided
[127] Fix | Delete
$length = $this->getHeader($headers, 'content-length');
[128] Fix | Delete
if (!$length) {
[129] Fix | Delete
if ($length = $stream->getSize()) {
[130] Fix | Delete
$headers['Content-Length'] = (string) $length;
[131] Fix | Delete
}
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
// Set a default Content-Type if one was not supplied
[135] Fix | Delete
$type = $this->getHeader($headers, 'content-type');
[136] Fix | Delete
if (!$type && ($filename === '0' || $filename)) {
[137] Fix | Delete
if ($type = MimeType::fromFilename($filename)) {
[138] Fix | Delete
$headers['Content-Type'] = $type;
[139] Fix | Delete
}
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
return [$stream, $headers];
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
private function getHeader(array $headers, $key)
[146] Fix | Delete
{
[147] Fix | Delete
$lowercaseHeader = strtolower($key);
[148] Fix | Delete
foreach ($headers as $k => $v) {
[149] Fix | Delete
if (strtolower($k) === $lowercaseHeader) {
[150] Fix | Delete
return $v;
[151] Fix | Delete
}
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
return null;
[155] Fix | Delete
}
[156] Fix | Delete
}
[157] Fix | Delete
[158] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function