}
// can be handled by built in PHP functionality
- $content = file_get_contents('php://input');
+ $content = static::getPhpInputContent();
$variables = json_decode($content);
{
$result = ['variables' => [], 'files' => []];
- $stream = fopen('php://input', 'rb');
+ $stream = static::getPhpInputStream();
$sanity = fgets($stream, strlen($boundary) + 5);
}
$result = self::parseRawHeader($stream, $raw_headers, $boundary, $result);
+
$raw_headers = '';
}
continue;
}
list($name, $value) = explode(':', $header, 2);
+
$headers[strtolower($name)] = ltrim($value, ' ');
}
if (isset($headers['content-type'])) {
$tmp = explode(';', $headers['content-type']);
+
$contentType = $tmp[0];
} else {
$contentType = 'unknown';
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 ($lastLine !== null) {
if (fwrite($fileHandle, $lastLine) === false) {
$error = UPLOAD_ERR_CANT_WRITE;
break;
$lastLine = $chunk;
}
- if ($lastLine !== NULL && $error !== UPLOAD_ERR_CANT_WRITE) {
+ if ($lastLine !== null && $error !== UPLOAD_ERR_CANT_WRITE) {
if (fwrite($fileHandle, rtrim($lastLine, "\r\n")) === false) {
$error = UPLOAD_ERR_CANT_WRITE;
}
private static function fetchVariables($stream, string $boundary, 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");
}
}
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;
+ }
}
}
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');
+ }
}
-?>