From 8bbc782b9640e5bd06845de96d864e1fcc7bbf04 Mon Sep 17 00:00:00 2001 From: Maurits van der Schee Date: Sat, 13 Oct 2018 08:33:10 +0200 Subject: [PATCH] Add support for file uploads --- README.md | 2 +- api.php | 33 +++++++++++++++---- examples/clients/auth.php/vanilla.html | 2 +- examples/clients/auth0/vanilla.html | 2 +- examples/clients/upload/form.html | 12 +++++++ src/Tqdev/PhpCrudApi/Api.php | 4 +++ .../Middleware/FileUploadMiddleware.php | 9 +++-- src/Tqdev/PhpCrudApi/Request.php | 11 ++++++- 8 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 examples/clients/upload/form.html diff --git a/README.md b/README.md index 32f8c09..12a82ba 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ https://github.com/mevdschee/php-crud-api/tree/v1 Related projects: - - [PHP-API-AUTH](https://github.com/mevdschee/php-api-auth): Authentication add-on supporting JWT for username/password. + - [PHP-API-AUTH](https://github.com/mevdschee/php-api-auth): Single file PHP script that is an authentication provider for PHP-CRUD-API (v2) - [PHP-SP-API](https://github.com/mevdschee/php-sp-api): Single file PHP script that adds a REST API to a SQL database. - [PHP-CRUD-UI](https://github.com/mevdschee/PHP-crud-ui): Single file PHP script that adds a UI to a PHP-CRUD-API (v1) project. - [VUE-CRUD-UI](https://github.com/nlware/vue-crud-ui): Single file Vue.js script that adds a UI to a PHP-CRUD-API (v1) project. diff --git a/api.php b/api.php index 713defa..fe19a7e 100644 --- a/api.php +++ b/api.php @@ -3192,14 +3192,17 @@ class FileUploadMiddleware extends Middleware if (!empty($files)) { $body = $request->getBody(); foreach ($files as $fieldName => $file) { - if (isset($file['error'])) { + if (isset($file['error']) && $file['error']) { return $this->responder->error(ErrorCode::FILE_UPLOAD_FAILED, $fieldName); } foreach ($file as $key => $value) { - if ($key == 'tmp_nam') { + if ($key == 'tmp_name') { $value = base64_encode(file_get_contents($value)); + $key = $fieldName; + } else { + $key = $fieldName . '_' . $key; } - $body[$fieldName . '_' . $key] = $value; + $body->$key = $value; } } $request->setBody($body); @@ -3290,8 +3293,14 @@ class JwtAuthMiddleware extends Middleware } foreach ($requirements as $field => $values) { if (!empty($values)) { - if (!isset($claims[$field]) || !in_array($claims[$field], $values)) { - return array(); + if ($field == 'alg') { + if (!isset($header[$field]) || !in_array($header[$field], $values)) { + return array(); + } + } else { + if (!isset($claims[$field]) || !in_array($claims[$field], $values)) { + return array(); + } } } } @@ -5055,6 +5064,9 @@ class Api case 'jwtAuth': new JwtAuthMiddleware($router, $responder, $properties); break; + case 'fileUpload': + new FileUploadMiddleware($router, $responder, $properties); + break; case 'validation': new ValidationMiddleware($router, $responder, $properties, $reflection); break; @@ -5366,7 +5378,11 @@ class Request private function parseBody(String $body = null) { if (!$body) { - $body = file_get_contents('php://input'); + if (!empty($_FILES)) { + $body = json_encode($_POST); + } else { + $body = file_get_contents('php://input'); + } } $this->body = $body; } @@ -5464,6 +5480,11 @@ class Request } return new Request($method, $path, $query, $headers, $body); } + + public function getUploadedFiles(): array + { + return $_FILES; + } } // file: src/Tqdev/PhpCrudApi/Response.php diff --git a/examples/clients/auth.php/vanilla.html b/examples/clients/auth.php/vanilla.html index a4e2bce..d7a78ef 100644 --- a/examples/clients/auth.php/vanilla.html +++ b/examples/clients/auth.php/vanilla.html @@ -2,7 +2,7 @@