]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/HTTPInputData.php
Merge pull request #10566 from annando/manage-avatar
[friendica.git] / src / Util / HTTPInputData.php
index 5b151a2015532631ab6f1bae5b1993be95e8613a..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);
 
@@ -132,16 +132,16 @@ 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;
 
@@ -162,7 +162,7 @@ class HTTPInputData
                        $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;
                                        }
@@ -171,7 +171,7 @@ class HTTPInputData
                        }
 
                        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;
                                }
                        }
@@ -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';
+       }
 }