<?php
/**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
*/
class HTTPInputData
{
- public static function process()
+ /** @var array The $_SERVER variable */
+ protected $server;
+
+ public function __construct(array $server)
+ {
+ $this->server = $server;
+ }
+
+ /**
+ * Process the PHP input stream and creates an array with its content
+ *
+ * @return array|array[]
+ */
+ public function process(): array
{
- $content_parts = explode(';', static::getContentType());
+ $content_parts = explode(';', $this->server['CONTENT_TYPE'] ?? 'application/x-www-form-urlencoded');
$boundary = '';
$encoding = '';
}
if ($content_type == 'multipart/form-data') {
- return self::fetchFromMultipart($boundary);
+ return $this->fetchFromMultipart($boundary);
}
// can be handled by built in PHP functionality
return ['variables' => $variables, 'files' => []];
}
- private static function fetchFromMultipart(string $boundary)
+ private function fetchFromMultipart(string $boundary): array
{
$result = ['variables' => [], 'files' => []];
continue;
}
- $result = self::parseRawHeader($stream, $raw_headers, $boundary, $result);
+ $result = $this->parseRawHeader($stream, $raw_headers, $boundary, $result);
$raw_headers = '';
}
return $result;
}
- private static function parseRawHeader($stream, string $raw_headers, string $boundary, array $result)
+ private function parseRawHeader($stream, string $raw_headers, string $boundary, array $result)
{
$variables = $result['variables'];
$files = $result['files'];
if (strpos($header, ':') === false) {
continue;
}
- list($name, $value) = explode(':', $header, 2);
+ [$name, $value] = explode(':', $header, 2);
$headers[strtolower($name)] = ltrim($value, ' ');
}
$filename = $matches[4] ?? '';
if (!empty($filename)) {
- $files[$name] = self::fetchFileData($stream, $boundary, $headers, $filename);
+ $files[$name] = static::fetchFileData($stream, $boundary, $headers, $filename);
return ['variables' => $variables, 'files' => $files];
} else {
- $variables = self::fetchVariables($stream, $boundary, $name, $variables);
+ $variables = $this->fetchVariables($stream, $boundary, $headers, $name, $variables);
}
return ['variables' => $variables, 'files' => $files];
}
- private static function fetchFileData($stream, string $boundary, array $headers, string $filename)
+ protected function fetchFileData($stream, string $boundary, array $headers, string $filename)
{
$error = UPLOAD_ERR_OK;
$lastLine = null;
while (($chunk = fgets($stream, 8096)) !== false && strpos($chunk, $boundary) !== 0) {
if ($lastLine !== null) {
- if (fwrite($fileHandle, $lastLine) === false) {
+ if (!fwrite($fileHandle, $lastLine)) {
$error = UPLOAD_ERR_CANT_WRITE;
break;
}
}
if ($lastLine !== null && $error !== UPLOAD_ERR_CANT_WRITE) {
- if (fwrite($fileHandle, rtrim($lastLine, "\r\n")) === false) {
+ if (!fwrite($fileHandle, rtrim($lastLine, "\r\n"))) {
$error = UPLOAD_ERR_CANT_WRITE;
}
}
];
}
- private static function fetchVariables($stream, string $boundary, string $name, array $variables)
+ private function fetchVariables($stream, string $boundary, array $headers, string $name, array $variables)
{
$fullValue = '';
$lastLine = null;
$tmp = [];
parse_str($fullValue, $tmp);
- return self::expandVariables(explode('[', $name), $variables, $tmp);
+ return $this->expandVariables(explode('[', $name), $variables, $tmp);
}
- private static function expandVariables(array $names, $variables, array $values)
+ private function expandVariables(array $names, $variables, array $values)
{
if (!is_array($variables)) {
return $values;
if ($name === '') {
$variables[] = reset($values);
} elseif (isset($variables[$name]) && isset($values[$name])) {
- $variables[$name] = self::expandVariables($names, $variables[$name], $values[$name]);
+ $variables[$name] = $this->expandVariables($names, $variables[$name], $values[$name]);
} elseif (isset($values[$name])) {
$variables[$name] = $values[$name];
}
*
* @return false|resource
*/
- protected static function getPhpInputStream()
+ protected function getPhpInputStream()
{
return fopen('php://input', 'rb');
}
*
* @return false|string
*/
- protected static function getPhpInputContent()
+ protected function getPhpInputContent()
{
return file_get_contents('php://input');
}
-
- /**
- * Returns the content type string of the current call
- * Mainly used for test doubling
- *
- * @return false|string
- */
- protected static function getContentType()
- {
- return $_SERVER['CONTENT_TYPE'] ?? 'application/x-www-form-urlencoded';
- }
}