]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/HTTPInputData.php
Test file uploads
[friendica.git] / src / Util / HTTPInputData.php
index 5b151a2015532631ab6f1bae5b1993be95e8613a..490dda5f66ab9756747ad3e6e736ca746faaf86f 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);
 
@@ -135,13 +135,13 @@ class HTTPInputData
                        $files[$name] = self::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;
 
@@ -186,7 +186,7 @@ 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;
@@ -259,4 +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';
+       }
 }