]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/HTTPInputData.php
Tests ...
[friendica.git] / src / Util / HTTPInputData.php
index e3b48cf58dacb0e738d9dabfebb931d4beacbace..d22c3894d40d0bff9552c2b18e384c6130a2e0fc 100644 (file)
@@ -29,7 +29,7 @@ class HTTPInputData
 {
        public static function process()
        {
-               $content_parts = explode(';', $_SERVER['CONTENT_TYPE'] ?? 'application/x-www-form-urlencoded');
+               $content_parts = explode(';', static::getContentType());
 
                $boundary = '';
                $encoding = '';
@@ -58,9 +58,9 @@ class HTTPInputData
                }
 
                // 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);
@@ -73,7 +73,7 @@ class HTTPInputData
        {
                $result = ['variables' => [], 'files' => []];
 
-               $stream = fopen('php://input', 'rb');
+               $stream = static::getPhpInputStream();
 
                $sanity = fgets($stream, strlen($boundary) + 5);
 
@@ -95,6 +95,7 @@ class HTTPInputData
                        }
 
                        $result = self::parseRawHeader($stream, $raw_headers, $boundary, $result);
+
                        $raw_headers = '';
                }
 
@@ -115,6 +116,7 @@ class HTTPInputData
                                continue;
                        }
                        list($name, $value) = explode(':', $header, 2);
+
                        $headers[strtolower($name)] = ltrim($value, ' ');
                }
 
@@ -130,21 +132,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 = self::fetchVariables($stream, $boundary, $headers, $name, $variables);
                }
 
                return ['variables' => $variables, 'files' => $files];
        }
 
-       private static function fetchFileData($stream, string $boundary, array $headers, string $filename)
+       protected static 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 +159,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 +170,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 +186,20 @@ class HTTPInputData
                ];
        }
 
-       private static function fetchVariables($stream, string $boundary, string $name, array $variables)
+       private static 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 +217,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;
+                               }
                        }
                }
 
@@ -256,5 +259,37 @@ class HTTPInputData
 
                return $variables;
        }
+
+       /**
+        * Returns the current PHP input stream
+        * Mainly used for test doubling
+        *
+        * @return false|resource
+        */
+       protected static function getPhpInputStream()
+       {
+               return fopen('php://input', 'rb');
+       }
+
+       /**
+        * Returns the content of the current PHP input
+        * Mainly used for test doubling
+        *
+        * @return false|string
+        */
+       protected static 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';
+       }
 }
-?>