3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Util;
25 * Derived from the work of Reid Johnson <https://codereview.stackexchange.com/users/4020/reid-johnson>
26 * @see https://codereview.stackexchange.com/questions/69882/parsing-multipart-form-data-in-php-for-put-requests
30 /** @var array The $_SERVER variable */
33 public function __construct(array $server)
35 $this->server = $server;
39 * Process the PHP input stream and creates an array with its content
41 * @return array|array[]
43 public function process(): array
45 $content_parts = explode(';', $this->server['CONTENT_TYPE'] ?? 'application/x-www-form-urlencoded');
50 $content_type = array_shift($content_parts);
52 foreach ($content_parts as $part) {
53 if (strpos($part, 'boundary') !== false) {
54 $part = explode('=', $part, 2);
55 if (!empty($part[1])) {
56 $boundary = '--' . $part[1];
58 } elseif (strpos($part, 'charset') !== false) {
59 $part = explode('=', $part, 2);
60 if (!empty($part[1])) {
64 if ($boundary !== '' && $encoding !== '') {
69 if ($content_type == 'multipart/form-data') {
70 return $this->fetchFromMultipart($boundary);
73 // can be handled by built in PHP functionality
74 $content = static::getPhpInputContent();
76 $variables = json_decode($content, true);
78 if (empty($variables)) {
79 parse_str($content, $variables);
82 return ['variables' => $variables, 'files' => []];
85 private function fetchFromMultipart(string $boundary): array
87 $result = ['variables' => [], 'files' => []];
89 $stream = static::getPhpInputStream();
91 $sanity = fgets($stream, strlen($boundary) + 5);
93 // malformed file, boundary should be first item
94 if (rtrim($sanity) !== $boundary) {
100 while (($chunk = fgets($stream)) !== false) {
101 if ($chunk === $boundary) {
105 if (!empty(trim($chunk))) {
106 $raw_headers .= $chunk;
110 $result = $this->parseRawHeader($stream, $raw_headers, $boundary, $result);
120 private function parseRawHeader($stream, string $raw_headers, string $boundary, array $result)
122 $variables = $result['variables'];
123 $files = $result['files'];
127 foreach (explode("\r\n", $raw_headers) as $header) {
128 if (strpos($header, ':') === false) {
131 [$name, $value] = explode(':', $header, 2);
133 $headers[strtolower($name)] = ltrim($value, ' ');
136 if (!isset($headers['content-disposition'])) {
137 return ['variables' => $variables, 'files' => $files];
140 if (!preg_match('/^(.+); *name="([^"]+)"(; *filename="([^"]+)")?/', $headers['content-disposition'], $matches)) {
141 return ['variables' => $variables, 'files' => $files];
145 $filename = $matches[4] ?? '';
147 if (!empty($filename)) {
148 $files[$name] = static::fetchFileData($stream, $boundary, $headers, $filename);
149 return ['variables' => $variables, 'files' => $files];
151 $variables = $this->fetchVariables($stream, $boundary, $headers, $name, $variables);
154 return ['variables' => $variables, 'files' => $files];
157 protected function fetchFileData($stream, string $boundary, array $headers, string $filename)
159 $error = UPLOAD_ERR_OK;
161 if (isset($headers['content-type'])) {
162 $tmp = explode(';', $headers['content-type']);
164 $contentType = $tmp[0];
166 $contentType = 'unknown';
169 $tmpnam = tempnam(ini_get('upload_tmp_dir'), 'php');
170 $fileHandle = fopen($tmpnam, 'wb');
172 if ($fileHandle === false) {
173 $error = UPLOAD_ERR_CANT_WRITE;
176 while (($chunk = fgets($stream, 8096)) !== false && strpos($chunk, $boundary) !== 0) {
177 if ($lastLine !== null) {
178 if (!fwrite($fileHandle, $lastLine)) {
179 $error = UPLOAD_ERR_CANT_WRITE;
186 if ($lastLine !== null && $error !== UPLOAD_ERR_CANT_WRITE) {
187 if (!fwrite($fileHandle, rtrim($lastLine, "\r\n"))) {
188 $error = UPLOAD_ERR_CANT_WRITE;
195 'type' => $contentType,
196 'tmp_name' => $tmpnam,
198 'size' => filesize($tmpnam)
202 private function fetchVariables($stream, string $boundary, array $headers, string $name, array $variables)
207 while (($chunk = fgets($stream)) !== false && strpos($chunk, $boundary) !== 0) {
208 if ($lastLine !== null) {
209 $fullValue .= $lastLine;
215 if ($lastLine !== null) {
216 $fullValue .= rtrim($lastLine, "\r\n");
219 if (isset($headers['content-type'])) {
222 foreach (explode(';', $headers['content-type']) as $part) {
223 if (strpos($part, 'charset') !== false) {
224 $part = explode($part, '=', 2);
225 if (isset($part[1])) {
226 $encoding = $part[1];
232 if ($encoding !== '' && strtoupper($encoding) !== 'UTF-8' && strtoupper($encoding) !== 'UTF8') {
233 $tmp = mb_convert_encoding($fullValue, 'UTF-8', $encoding);
234 if ($tmp !== false) {
240 $fullValue = $name . '=' . $fullValue;
243 parse_str($fullValue, $tmp);
245 return $this->expandVariables(explode('[', $name), $variables, $tmp);
248 private function expandVariables(array $names, $variables, array $values)
250 if (!is_array($variables)) {
254 $name = rtrim(array_shift($names), ']');
256 $name = $name . '=p';
259 parse_str($name, $tmp);
261 $tmp = array_keys($tmp);
266 $variables[] = reset($values);
267 } elseif (isset($variables[$name]) && isset($values[$name])) {
268 $variables[$name] = $this->expandVariables($names, $variables[$name], $values[$name]);
269 } elseif (isset($values[$name])) {
270 $variables[$name] = $values[$name];
277 * Returns the current PHP input stream
278 * Mainly used for test doubling
280 * @return false|resource
282 protected function getPhpInputStream()
284 return fopen('php://input', 'rb');
288 * Returns the content of the current PHP input
289 * Mainly used for test doubling
291 * @return false|string
293 protected function getPhpInputContent()
295 return file_get_contents('php://input');