]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/HTTPInputData.php
Fixed max value check, improved request value fetching
[friendica.git] / src / Util / HTTPInputData.php
index e3b48cf58dacb0e738d9dabfebb931d4beacbace..9382e65b3055c225e670a71b433d725d7b0c9b52 100644 (file)
@@ -1,6 +1,6 @@
 <?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
  *
@@ -27,9 +27,22 @@ namespace Friendica\Util;
  */
 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(';', $_SERVER['CONTENT_TYPE'] ?? 'application/x-www-form-urlencoded');
+               $content_parts = explode(';', $this->server['CONTENT_TYPE'] ?? 'application/x-www-form-urlencoded');
 
                $boundary = '';
                $encoding = '';
@@ -54,13 +67,13 @@ class HTTPInputData
                }
 
                if ($content_type == 'multipart/form-data') {
-                       return self::fetchFromMultipart($boundary);
+                       return $this->fetchFromMultipart($boundary);
                }
 
                // can be handled by built in PHP functionality
-               $content = file_get_contents('php://input');
+               $content = static::getPhpInputContent();
 
-               $variables = json_decode($content);
+               $variables = json_decode($content, true);
 
                if (empty($variables)) {
                        parse_str($content, $variables);
@@ -69,11 +82,11 @@ class HTTPInputData
                return ['variables' => $variables, 'files' => []];
        }
 
-       private static function fetchFromMultipart(string $boundary)
+       private function fetchFromMultipart(string $boundary): array
        {
                $result = ['variables' => [], 'files' => []];
 
-               $stream = fopen('php://input', 'rb');
+               $stream = static::getPhpInputStream();
 
                $sanity = fgets($stream, strlen($boundary) + 5);
 
@@ -94,7 +107,8 @@ class HTTPInputData
                                continue;
                        }
 
-                       $result = self::parseRawHeader($stream, $raw_headers, $boundary, $result);
+                       $result = $this->parseRawHeader($stream, $raw_headers, $boundary, $result);
+
                        $raw_headers = '';
                }
 
@@ -103,7 +117,7 @@ class HTTPInputData
                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'];
@@ -114,7 +128,8 @@ class HTTPInputData
                        if (strpos($header, ':') === false) {
                                continue;
                        }
-                       list($name, $value) = explode(':', $header, 2);
+                       [$name, $value] = explode(':', $header, 2);
+
                        $headers[strtolower($name)] = ltrim($value, ' ');
                }
 
@@ -130,21 +145,22 @@ class HTTPInputData
                $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;
 
                if (isset($headers['content-type'])) {
                        $tmp = explode(';', $headers['content-type']);
+
                        $contentType = $tmp[0];
                } else {
                        $contentType = 'unknown';
@@ -156,10 +172,10 @@ class HTTPInputData
                if ($fileHandle === false) {
                        $error = UPLOAD_ERR_CANT_WRITE;
                } else {
-                       $lastLine = NULL;
+                       $lastLine = null;
                        while (($chunk = fgets($stream, 8096)) !== false && strpos($chunk, $boundary) !== 0) {
-                               if ($lastLine !== NULL) {
-                                       if (fwrite($fileHandle, $lastLine) === false) {
+                               if ($lastLine !== null) {
+                                       if (!fwrite($fileHandle, $lastLine)) {
                                                $error = UPLOAD_ERR_CANT_WRITE;
                                                break;
                                        }
@@ -167,8 +183,8 @@ class HTTPInputData
                                $lastLine = $chunk;
                        }
 
-                       if ($lastLine !== NULL && $error !== UPLOAD_ERR_CANT_WRITE) {
-                               if (fwrite($fileHandle, rtrim($lastLine, "\r\n")) === false) {
+                       if ($lastLine !== null && $error !== UPLOAD_ERR_CANT_WRITE) {
+                               if (!fwrite($fileHandle, rtrim($lastLine, "\r\n"))) {
                                        $error = UPLOAD_ERR_CANT_WRITE;
                                }
                        }
@@ -183,20 +199,20 @@ class HTTPInputData
                ];
        }
 
-       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;
+               $lastLine  = null;
 
                while (($chunk = fgets($stream)) !== false && strpos($chunk, $boundary) !== 0) {
-                       if ($lastLine !== NULL) {
+                       if ($lastLine !== null) {
                                $fullValue .= $lastLine;
                        }
 
                        $lastLine = $chunk;
                }
 
-               if ($lastLine !== NULL) {
+               if ($lastLine !== null) {
                        $fullValue .= rtrim($lastLine, "\r\n");
                }
 
@@ -214,10 +230,10 @@ class HTTPInputData
                        }
 
                        if ($encoding !== '' && strtoupper($encoding) !== 'UTF-8' && strtoupper($encoding) !== 'UTF8') {
-                                       $tmp = mb_convert_encoding($fullValue, 'UTF-8', $encoding);
-                                       if ($tmp !== false) {
-                                               $fullValue = $tmp;
-                                       }
+                               $tmp = mb_convert_encoding($fullValue, 'UTF-8', $encoding);
+                               if ($tmp !== false) {
+                                       $fullValue = $tmp;
+                               }
                        }
                }
 
@@ -226,10 +242,10 @@ class HTTPInputData
                $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;
@@ -249,12 +265,33 @@ class HTTPInputData
                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 $variables;
        }
+
+       /**
+        * Returns the current PHP input stream
+        * Mainly used for test doubling
+        *
+        * @return false|resource
+        */
+       protected function getPhpInputStream()
+       {
+               return fopen('php://input', 'rb');
+       }
+
+       /**
+        * Returns the content of the current PHP input
+        * Mainly used for test doubling
+        *
+        * @return false|string
+        */
+       protected function getPhpInputContent()
+       {
+               return file_get_contents('php://input');
+       }
 }
-?>