Verified Commit b4342f7d authored by Cyril Vazquez's avatar Cyril Vazquez
Browse files

Parse multipart/form-data from superglobal $_POST and $_FILES

Manage multiple files in nested structures (PHP strange behaviour)
Use parsed body in kernel
parent 92f75771
Pipeline #18830 failed with stages
in 18 seconds
......@@ -154,8 +154,7 @@ class PresentationKernel
if (!is_null($this->request->body)) {
switch ($this->request->contentType) {
case 'url':
$contents = stream_get_contents($this->request->body);
$bodyArguments = \core\Encoding\url::decode($contents);
$bodyArguments = $this->request->body;
break;
case 'json':
......
......@@ -52,14 +52,7 @@ class HttpRequest
$this->getAuthentication();
$this->body = fopen('php://temp', 'w+');
$input = fopen('php://input', 'r');
$length = stream_copy_to_stream($input, $this->body);
rewind($this->body);
if ($length == 0) {
$this->body = null;
}
$this->getBody();
$this->query = urldecode($_SERVER['QUERY_STRING']);
......@@ -177,13 +170,65 @@ class HttpRequest
return $this->queryType;
}
protected function parseUrlBody()
protected function getBody()
{
if ($this->contentType == 'url') {
$this->body = array_merge_recursive($_POST, $this->getFiles());
} else {
$this->body = fopen('php://temp', 'w+');
$input = fopen('php://input', 'r');
$length = stream_copy_to_stream($input, $this->body);
rewind($this->body);
if ($length == 0) {
$this->body = null;
}
}
}
protected function getFiles()
{
if ($this->type == 'url' && !empty($this->body)) {
$bodyArguments = array();
\parse_str($this->body, $bodyArguments);
$this->arguments = array_merge($bodyArguments, $this->arguments);
$files = [];
$keys = ['name', 'type', 'tmp_name', 'error', 'size'];
foreach ($_FILES as $name => $item) {
// Standard form-data files at root level
if (is_string($item['name'])) {
$files[$name] = $item;
continue;
}
// Merged array of file values
// root-level name =>
// 'name' => array of subfile names
// 'type' => array of subfile types
// ...
foreach ($item['name'] as $fname => $value) {
// 2nd level files
if (is_string($value)) {
$file = [];
foreach ($keys as $key) {
$file[$key] = $item[$key][$fname];
}
$files[$name][$fname] = $file;
continue;
}
// 3rd level : multiple files with one name
if (is_array($value)) {
foreach ($value as $index => $svalue) {
$file = [];
foreach ($keys as $key) {
$file[$key] = $item[$key][$fname][$index];
}
$files[$name][$fname][$index] = $file;
}
}
}
}
return $files;
}
protected function parseUrl()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment