PluginProbe
Media Cloud Sync / 1.4.2
Media Cloud Sync v1.4.2
1.4.2 1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 All 36 releases
← All changes | includes/sdk/s3/GuzzleHttp/Psr7/MultipartStream.php +32 -27 1.1.0 → 1.4.2 View file →
@@ -1,6 +1,7 @@
1 1 <?php
2 2
3 +declare (strict_types=1);
3 4 namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7;
4 5
5 6 use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface;
6 7 /**
@@ -5,15 +6,16 @@
5 6 use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface;
6 7 /**
7 8 * Stream that when read returns bytes for a streaming multipart or
8 9 * multipart/form-data stream.
9 - *
10 - * @final
11 10 */
12 -class MultipartStream implements StreamInterface
11 +final class MultipartStream implements StreamInterface
13 12 {
14 13 use StreamDecoratorTrait;
14 + /** @var string */
15 15 private $boundary;
16 + /** @var StreamInterface */
17 + private $stream;
16 18 /**
17 19 * @param array $elements Array of associative arrays, each containing a
18 20 * required "name" key mapping to the form field,
19 21 * name, a required "contents" key mapping to a
@@ -24,30 +26,27 @@
24 26 * @param string $boundary You can optionally provide a specific boundary
25 27 *
26 28 * @throws \InvalidArgumentException
27 29 */
28 - public function __construct(array $elements = [], $boundary = null)
30 + public function __construct(array $elements = [], ?string $boundary = null)
29 31 {
30 - $this->boundary = $boundary ?: \sha1(\uniqid('', \true));
32 + $this->boundary = $boundary ?: \bin2hex(\random_bytes(20));
31 33 $this->stream = $this->createStream($elements);
32 34 }
33 - /**
34 - * Get the boundary
35 - *
36 - * @return string
37 - */
38 - public function getBoundary()
35 + public function getBoundary() : string
39 36 {
40 37 return $this->boundary;
41 38 }
42 - public function isWritable()
39 + public function isWritable() : bool
43 40 {
44 41 return \false;
45 42 }
46 43 /**
47 44 * Get the headers needed before transferring the content of a POST file
45 + *
46 + * @param string[] $headers
48 47 */
49 - private function getHeaders(array $headers)
48 + private function getHeaders(array $headers) : string
50 49 {
51 50 $str = '';
52 51 foreach ($headers as $key => $value) {
53 52 $str .= "{$key}: {$value}\r\n";
@@ -56,12 +55,15 @@
56 55 }
57 56 /**
58 57 * Create the aggregate stream that will be used to upload the POST data
59 58 */
60 - protected function createStream(array $elements)
59 + protected function createStream(array $elements = []) : StreamInterface
61 60 {
62 61 $stream = new AppendStream();
63 62 foreach ($elements as $element) {
63 + if (!\is_array($element)) {
64 + throw new \UnexpectedValueException('An array is expected');
65 + }
64 66 $this->addElement($stream, $element);
65 67 }
66 68 // Add the trailing boundary with CRLF
67 69 $stream->addStream(Utils::streamFor("--{$this->boundary}--\r\n"));
@@ -66,9 +68,9 @@
66 68 // Add the trailing boundary with CRLF
67 69 $stream->addStream(Utils::streamFor("--{$this->boundary}--\r\n"));
68 70 return $stream;
69 71 }
70 - private function addElement(AppendStream $stream, array $element)
72 + private function addElement(AppendStream $stream, array $element) : void
71 73 {
72 74 foreach (['contents', 'name'] as $key) {
73 75 if (!\array_key_exists($key, $element)) {
74 76 throw new \InvalidArgumentException("A '{$key}' key is required");
@@ -76,29 +78,31 @@
76 78 }
77 79 $element['contents'] = Utils::streamFor($element['contents']);
78 80 if (empty($element['filename'])) {
79 81 $uri = $element['contents']->getMetadata('uri');
80 - if (\substr($uri, 0, 6) !== 'php://') {
82 + if ($uri && \is_string($uri) && \substr($uri, 0, 6) !== 'php://' && \substr($uri, 0, 7) !== 'data://') {
81 83 $element['filename'] = $uri;
82 84 }
83 85 }
84 - list($body, $headers) = $this->createElement($element['name'], $element['contents'], isset($element['filename']) ? $element['filename'] : null, isset($element['headers']) ? $element['headers'] : []);
86 + [$body, $headers] = $this->createElement($element['name'], $element['contents'], $element['filename'] ?? null, $element['headers'] ?? []);
85 87 $stream->addStream(Utils::streamFor($this->getHeaders($headers)));
86 88 $stream->addStream($body);
87 89 $stream->addStream(Utils::streamFor("\r\n"));
88 90 }
89 91 /**
90 - * @return array
92 + * @param string[] $headers
93 + *
94 + * @return array{0: StreamInterface, 1: string[]}
91 95 */
92 - private function createElement($name, StreamInterface $stream, $filename, array $headers)
96 + private function createElement(string $name, StreamInterface $stream, ?string $filename, array $headers) : array
93 97 {
94 98 // Set a default content-disposition header if one was no provided
95 - $disposition = $this->getHeader($headers, 'content-disposition');
99 + $disposition = self::getHeader($headers, 'content-disposition');
96 100 if (!$disposition) {
97 101 $headers['Content-Disposition'] = $filename === '0' || $filename ? \sprintf('form-data; name="%s"; filename="%s"', $name, \basename($filename)) : "form-data; name=\"{$name}\"";
98 102 }
99 103 // Set a default content-length header if one was no provided
100 - $length = $this->getHeader($headers, 'content-length');
104 + $length = self::getHeader($headers, 'content-length');
101 105 if (!$length) {
102 106 if ($length = $stream->getSize()) {
103 107 $headers['Content-Length'] = (string) $length;
104 108 }
@@ -103,21 +107,22 @@
103 107 $headers['Content-Length'] = (string) $length;
104 108 }
105 109 }
106 110 // Set a default Content-Type if one was not supplied
107 - $type = $this->getHeader($headers, 'content-type');
111 + $type = self::getHeader($headers, 'content-type');
108 112 if (!$type && ($filename === '0' || $filename)) {
109 - if ($type = MimeType::fromFilename($filename)) {
110 - $headers['Content-Type'] = $type;
111 - }
113 + $headers['Content-Type'] = MimeType::fromFilename($filename) ?? 'application/octet-stream';
112 114 }
113 115 return [$stream, $headers];
114 116 }
115 - private function getHeader(array $headers, $key)
117 + /**
118 + * @param string[] $headers
119 + */
120 + private static function getHeader(array $headers, string $key) : ?string
116 121 {
117 122 $lowercaseHeader = \strtolower($key);
118 123 foreach ($headers as $k => $v) {
119 - if (\strtolower($k) === $lowercaseHeader) {
124 + if (\strtolower((string) $k) === $lowercaseHeader) {
120 125 return $v;
121 126 }
122 127 }
123 128 return null;