1 | <?php |
||
27 | final class MultipartStream extends StreamAbstract{ |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | protected string $boundary; |
||
|
|||
33 | |||
34 | /** |
||
35 | * @var bool |
||
36 | */ |
||
37 | protected bool $built = false; |
||
38 | |||
39 | /** |
||
40 | * MultipartStream constructor. |
||
41 | * |
||
42 | * @param array|null $elements [ |
||
43 | * name => string, |
||
44 | * contents => StreamInterface/resource/string, |
||
45 | * headers => array, |
||
46 | * filename => string |
||
47 | * ] |
||
48 | * @param string|null $boundary |
||
49 | */ |
||
50 | public function __construct(array $elements = null, string $boundary = null){ |
||
51 | $this->boundary = $boundary ?? sha1(random_bytes(1024)); |
||
52 | $this->stream = create_stream(); |
||
53 | |||
54 | foreach($elements ?? [] as $element){ |
||
55 | $this->addElement($element); |
||
56 | } |
||
57 | |||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @return string |
||
62 | */ |
||
63 | public function getBoundary():string{ |
||
64 | return $this->boundary; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @return \chillerlan\HTTP\Psr7\MultipartStream |
||
69 | */ |
||
70 | public function build():MultipartStream{ |
||
71 | |||
72 | if(!$this->built){ |
||
73 | $this->stream->write('--'.$this->getBoundary()."--\r\n"); |
||
74 | |||
75 | $this->built = true; |
||
76 | } |
||
77 | |||
78 | $this->stream->rewind(); |
||
79 | |||
80 | return $this; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param array $e |
||
85 | * |
||
86 | * @return \chillerlan\HTTP\Psr7\MultipartStream |
||
87 | */ |
||
88 | public function addElement(array $e):MultipartStream{ |
||
89 | |||
90 | if($this->built){ |
||
91 | throw new RuntimeException('Stream already built'); |
||
92 | } |
||
93 | |||
94 | $e = array_merge(['filename' => null, 'headers' => []], $e); |
||
95 | |||
96 | foreach(['contents', 'name'] as $key){ |
||
97 | if(!isset($e[$key])){ |
||
98 | throw new InvalidArgumentException('A "'.$key.'" element is required'); |
||
99 | } |
||
100 | } |
||
101 | |||
102 | // at this point we assume the string is already the file content and don't guess anymore |
||
103 | $e['contents'] = is_string($e['contents']) |
||
104 | ? create_stream($e['contents']) |
||
105 | : create_stream_from_input($e['contents']); |
||
106 | |||
107 | if(empty($e['filename'])){ |
||
108 | $uri = $e['contents']->getMetadata('uri'); |
||
109 | |||
110 | if(substr($uri, 0, 6) !== 'php://'){ |
||
111 | $e['filename'] = $uri; |
||
112 | } |
||
113 | } |
||
114 | |||
115 | $e = $this->setElementHeaders($e); |
||
116 | |||
117 | $this->stream->write('--'.$this->boundary."\r\n"); |
||
118 | |||
119 | foreach(normalize_message_headers($e['headers']) as $key => $value){ |
||
120 | $this->stream->write($key.': '.$value."\r\n"); |
||
121 | } |
||
122 | |||
123 | $this->stream->write("\r\n".$e['contents']->getContents()."\r\n"); |
||
124 | |||
125 | return $this; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @param array $e |
||
130 | * |
||
131 | * @return array |
||
132 | */ |
||
133 | protected function setElementHeaders(array $e):array{ |
||
134 | $hasFilename = $e['filename'] === '0' || $e['filename']; |
||
135 | |||
136 | // Set a default content-disposition header if none was provided |
||
137 | if(!$this->hasHeader($e['headers'], 'content-disposition')){ |
||
138 | $filename = $hasFilename ? '; filename="'.basename($e['filename']).'"' : ''; |
||
139 | |||
140 | $e['headers']['Content-Disposition'] = 'form-data; name="'.$e['name'].'"'.$filename; |
||
141 | } |
||
142 | |||
143 | // Set a default content-length header if none was provided |
||
144 | if(!$this->hasHeader($e['headers'], 'content-length')){ |
||
145 | $length = $e['contents']->getSize(); |
||
146 | |||
147 | if($length){ |
||
148 | $e['headers']['Content-Length'] = $length; |
||
149 | } |
||
150 | } |
||
151 | |||
152 | // Set a default Content-Type if none was supplied |
||
153 | if(!$this->hasHeader($e['headers'], 'content-type') && $hasFilename){ |
||
154 | $type = MIMETYPES[pathinfo($e['filename'], PATHINFO_EXTENSION)] ?? null; |
||
155 | |||
156 | if($type){ |
||
157 | $e['headers']['Content-Type'] = $type; |
||
158 | } |
||
159 | } |
||
160 | |||
161 | return $e; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @param array $headers |
||
166 | * @param string $key |
||
167 | * |
||
168 | * @return bool |
||
169 | */ |
||
170 | protected function hasHeader(array $headers, string $key):bool{ |
||
171 | $lowercaseHeader = strtolower($key); |
||
172 | |||
173 | foreach($headers as $k => $v){ |
||
174 | if(strtolower($k) === $lowercaseHeader){ |
||
175 | return true; |
||
176 | } |
||
177 | } |
||
178 | |||
179 | return false; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @inheritDoc |
||
184 | */ |
||
185 | public function __toString(){ |
||
186 | return $this->getContents(); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @inheritDoc |
||
191 | */ |
||
192 | public function getSize():?int{ |
||
193 | return $this->stream->getSize() + strlen($this->boundary) + 6; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @inheritDoc |
||
198 | */ |
||
199 | public function isWritable():bool{ |
||
200 | return false; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @inheritDoc |
||
205 | */ |
||
206 | public function write($string):int{ |
||
207 | throw new RuntimeException('Cannot write to a MultipartStream, use MultipartStream::addElement() instead.'); |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @inheritDoc |
||
212 | */ |
||
213 | public function isReadable():bool{ |
||
214 | return true; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @inheritDoc |
||
219 | */ |
||
220 | public function getContents():string{ |
||
221 | return $this->build()->stream->getContents(); |
||
222 | } |
||
223 | |||
224 | } |
||
225 |