aboutsummaryrefslogtreecommitdiff
path: root/src/Controller/FileUploadController.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Controller/FileUploadController.php')
-rw-r--r--src/Controller/FileUploadController.php68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/Controller/FileUploadController.php b/src/Controller/FileUploadController.php
new file mode 100644
index 0000000..4627017
--- /dev/null
+++ b/src/Controller/FileUploadController.php
@@ -0,0 +1,68 @@
1<?php
2// src/Controller/FileUploadController.php
3
4declare(strict_types=1);
5
6namespace App\Controller;
7
8use Symfony\Component\HttpFoundation\JsonResponse;
9use App\Service\Security;
10
11class FileUploadController
12{
13 static private function checkFileDownload(array $file): bool
14 {
15 $extensions_white_list = ['pdf', 'rtf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'odt', 'ods', 'odp']; // = extensions_white_list côté javascript
16 $mime_type_white_list = ['application/pdf', 'application/rtf', 'text/rtf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'application/vnd.oasis.opendocument.text', 'application/vnd.oasis.opendocument.spreadsheet', 'application/vnd.oasis.opendocument.presentation'];
17
18 // 1/ extension
19 $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
20 if(!in_array($extension, $extensions_white_list, true)){
21 return false;
22 }
23
24 // 2/ fichier obtenu par HTTP POST (théoriquement inutile si le routeur est solide, mais ça ne mange pas de pain)
25 if(!is_uploaded_file($file['tmp_name'])){
26 return false;
27 }
28
29 // 3/ objet $finfo valide (dépend du paramètre FILEINFO_MIME_TYPE)
30 $finfo = new \finfo(FILEINFO_MIME_TYPE);
31 if($finfo === false){
32 return false;
33 }
34
35 // 4/ contrôle du "vrai" type mime (finfo_file lit les 1ers octets des fichiers pour y trouver des "signatures", très fiable sauf avec les conteneurs: doc, zip...)
36 $real_type = finfo_file($finfo, $file['tmp_name']);
37 return in_array($real_type, $mime_type_white_list, true);
38 }
39
40 static public function fileUploadTinyMce(): JsonResponse
41 {
42 if(isset($_FILES['file'])){
43 $dest = 'user_data/media/';
44 if(!is_dir($dest)){ // Vérifier si le répertoire existe, sinon le créer
45 mkdir($dest, 0755, true);
46 }
47
48 $name = Security::secureFileName(pathinfo($_FILES['file']['name'], PATHINFO_FILENAME)); // retirer caractères spéciaux et changer espaces en underscores
49 $extension = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION));
50 $file_path = $dest . $name . '_' . uniqid() . '.' . $extension; // nom unique
51
52 if(self::checkFileDownload($_FILES['file'])){
53 if(move_uploaded_file($_FILES['file']['tmp_name'], $file_path)){
54 return new JsonResponse(['location' => $file_path]);
55 }
56 else{
57 return new JsonResponse(['message' => 'Erreur enregistrement du fichier.'], JsonResponse::HTTP_INTERNAL_SERVER_ERROR); // code 500
58 }
59 }
60 else{
61 return new JsonResponse(['message' => 'Erreur 400: fichier non valide.'], JsonResponse::HTTP_BAD_REQUEST); // code 400
62 }
63 }
64 else{
65 return new JsonResponse(['message' => 'Erreur 400: Bad Request'], JsonResponse::HTTP_BAD_REQUEST); // code 400
66 }
67 }
68} \ No newline at end of file