diff options
| author | polo <ordipolo@gmx.fr> | 2026-01-12 17:16:41 +0100 |
|---|---|---|
| committer | polo <ordipolo@gmx.fr> | 2026-01-12 17:16:41 +0100 |
| commit | 505cab0210e83bf62085b665abad0698a8d31352 (patch) | |
| tree | 6e66d20d2041bae3f800ee99fdde1957c7bb9f51 /src | |
| parent | dfed030ca6edf832397d273fbb0ad4b0b5d35877 (diff) | |
| download | cms-505cab0210e83bf62085b665abad0698a8d31352.tar.gz cms-505cab0210e83bf62085b665abad0698a8d31352.tar.bz2 cms-505cab0210e83bf62085b665abad0698a8d31352.zip | |
upload de fichiers (JS + contrôleur + htaccess), collage de fichiers dans l'éditeur (JS), amélioration utilisation de Imagick (ImageUploadController)
Diffstat (limited to 'src')
| -rw-r--r-- | src/Captcha.php | 6 | ||||
| -rw-r--r-- | src/controller/FileUploadController.php | 67 | ||||
| -rw-r--r-- | src/controller/HeadFootController.php | 5 | ||||
| -rw-r--r-- | src/controller/ImageUploadController.php | 186 | ||||
| -rw-r--r-- | src/router.php | 7 |
5 files changed, 181 insertions, 90 deletions
diff --git a/src/Captcha.php b/src/Captcha.php index f60031a..a0c7a54 100644 --- a/src/Captcha.php +++ b/src/Captcha.php | |||
| @@ -10,12 +10,10 @@ class Captcha | |||
| 10 | { | 10 | { |
| 11 | private int $a; | 11 | private int $a; |
| 12 | private int $b; | 12 | private int $b; |
| 13 | private int $solution; | ||
| 14 | 13 | ||
| 15 | public function __construct(){ | 14 | public function __construct(){ |
| 16 | $this->a = rand(2, 9); | 15 | $this->a = rand(2, 9); |
| 17 | $this->b = rand(2, 9); | 16 | $this->b = rand(2, 9); |
| 18 | $this->solution = $this->a * $this->b; | ||
| 19 | } | 17 | } |
| 20 | 18 | ||
| 21 | public function getA(): string | 19 | public function getA(): string |
| @@ -28,7 +26,7 @@ class Captcha | |||
| 28 | } | 26 | } |
| 29 | public function getSolution(): int | 27 | public function getSolution(): int |
| 30 | { | 28 | { |
| 31 | return $this->solution; | 29 | return ($this->a * $this->b); |
| 32 | } | 30 | } |
| 33 | 31 | ||
| 34 | private function toLettersFrench(int $number): string | 32 | private function toLettersFrench(int $number): string |
| @@ -45,6 +43,8 @@ class Captcha | |||
| 45 | default => '', // erreur | 43 | default => '', // erreur |
| 46 | }; | 44 | }; |
| 47 | } | 45 | } |
| 46 | |||
| 47 | // (à déplacer dans FormValidation?) | ||
| 48 | static public function controlInput(string $input = '0'): int | 48 | static public function controlInput(string $input = '0'): int |
| 49 | { | 49 | { |
| 50 | // un POST est une chaîne qu'on doit convertir en nombre dans deux conditions: | 50 | // un POST est une chaîne qu'on doit convertir en nombre dans deux conditions: |
diff --git a/src/controller/FileUploadController.php b/src/controller/FileUploadController.php new file mode 100644 index 0000000..f53f5c2 --- /dev/null +++ b/src/controller/FileUploadController.php | |||
| @@ -0,0 +1,67 @@ | |||
| 1 | <?php | ||
| 2 | // src/controller/FileUploadController.php | ||
| 3 | |||
| 4 | declare(strict_types=1); | ||
| 5 | |||
| 6 | class FileUploadController | ||
| 7 | { | ||
| 8 | static public function checkFileDownload(array $file): bool | ||
| 9 | { | ||
| 10 | $extensions_white_list = ['pdf', 'rtf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'odt', 'ods', 'odp']; // = extensions_white_list côté javascript | ||
| 11 | $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']; | ||
| 12 | |||
| 13 | // 1/ extension | ||
| 14 | $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); | ||
| 15 | if(!in_array($extension, $extensions_white_list, true)){ | ||
| 16 | return false; | ||
| 17 | } | ||
| 18 | |||
| 19 | // 2/ fichier obtenu par HTTP POST (théoriquement inutile si le routeur est solide, mais ça ne mange pas de pain) | ||
| 20 | if(!is_uploaded_file($file['tmp_name'])){ | ||
| 21 | return false; | ||
| 22 | } | ||
| 23 | |||
| 24 | // 3/ objet $finfo valide (dépend du paramètre FILEINFO_MIME_TYPE) | ||
| 25 | $finfo = new finfo(FILEINFO_MIME_TYPE); | ||
| 26 | if($finfo === false){ | ||
| 27 | return false; | ||
| 28 | } | ||
| 29 | |||
| 30 | // 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...) | ||
| 31 | $real_type = finfo_file($finfo, $file['tmp_name']); | ||
| 32 | return in_array($real_type, $mime_type_white_list, true); | ||
| 33 | } | ||
| 34 | |||
| 35 | static public function fileUploadTinyMce(): void | ||
| 36 | { | ||
| 37 | if(isset($_FILES['file'])){ | ||
| 38 | $dest = 'user_data/media/'; | ||
| 39 | if(!is_dir($dest)){ // Vérifier si le répertoire existe, sinon le créer | ||
| 40 | mkdir($dest, 0755, true); | ||
| 41 | } | ||
| 42 | |||
| 43 | $name = Security::secureFileName(pathinfo($_FILES['file']['name'], PATHINFO_FILENAME)); // retirer caractères spéciaux et changer espaces en underscores | ||
| 44 | $extension = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION)); | ||
| 45 | $file_path = $dest . $name . '_' . uniqid() . '.' . $extension; // nom unique | ||
| 46 | |||
| 47 | if(self::checkFileDownload($_FILES['file'])){ | ||
| 48 | if(move_uploaded_file($_FILES['file']['tmp_name'], $file_path)){ | ||
| 49 | echo json_encode(['location' => $file_path]); | ||
| 50 | } | ||
| 51 | else{ | ||
| 52 | http_response_code(500); | ||
| 53 | echo json_encode(['message' => 'Erreur enregistrement du fichier.']); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | else{ | ||
| 57 | http_response_code(400); | ||
| 58 | echo json_encode(['message' => 'Erreur 400: fichier non valide.']); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | else{ | ||
| 62 | http_response_code(400); | ||
| 63 | echo json_encode(['message' => 'Erreur 400: Bad Request']); | ||
| 64 | } | ||
| 65 | die; | ||
| 66 | } | ||
| 67 | } \ No newline at end of file | ||
diff --git a/src/controller/HeadFootController.php b/src/controller/HeadFootController.php index de7ae53..83e27a8 100644 --- a/src/controller/HeadFootController.php +++ b/src/controller/HeadFootController.php | |||
| @@ -4,7 +4,6 @@ | |||
| 4 | declare(strict_types=1); | 4 | declare(strict_types=1); |
| 5 | 5 | ||
| 6 | use App\Entity\NodeData; | 6 | use App\Entity\NodeData; |
| 7 | use App\Entity\NodeDataAsset; | ||
| 8 | use App\Entity\Asset; | 7 | use App\Entity\Asset; |
| 9 | use App\Entity\AssetEmployment; | 8 | use App\Entity\AssetEmployment; |
| 10 | use Doctrine\ORM\EntityManager; | 9 | use Doctrine\ORM\EntityManager; |
| @@ -82,7 +81,7 @@ class HeadFootController | |||
| 82 | } | 81 | } |
| 83 | 82 | ||
| 84 | /* -- écriture du fichier sur le disque -- */ | 83 | /* -- écriture du fichier sur le disque -- */ |
| 85 | if(!ImageUploadController::imagickCleanImage(file_get_contents($file['tmp_name']), Asset::USER_PATH . $name, $extension)){ // recréer l’image pour la nettoyer | 84 | if(!ImageUploadController::imagickCleanAndWriteImage(file_get_contents($file['tmp_name']), Asset::USER_PATH . $name, $extension)){ // recréer l’image pour la nettoyer |
| 86 | http_response_code(500); | 85 | http_response_code(500); |
| 87 | echo json_encode(['success' => false, 'message' => 'Erreur image non valide.', 'format' => $extension]); | 86 | echo json_encode(['success' => false, 'message' => 'Erreur image non valide.', 'format' => $extension]); |
| 88 | } | 87 | } |
| @@ -146,6 +145,4 @@ class HeadFootController | |||
| 146 | } | 145 | } |
| 147 | die; | 146 | die; |
| 148 | } | 147 | } |
| 149 | |||
| 150 | //static public function uploadImage(EntityManager $entityManager, array $request_params): void | ||
| 151 | } \ No newline at end of file | 148 | } \ No newline at end of file |
diff --git a/src/controller/ImageUploadController.php b/src/controller/ImageUploadController.php index 77f0a47..7943116 100644 --- a/src/controller/ImageUploadController.php +++ b/src/controller/ImageUploadController.php | |||
| @@ -5,17 +5,40 @@ declare(strict_types=1); | |||
| 5 | 5 | ||
| 6 | class ImageUploadController | 6 | class ImageUploadController |
| 7 | { | 7 | { |
| 8 | static public function imagickCleanImage(string $image_data, string $local_path, string $format = 'jpeg'): bool // "string" parce que file_get_contents... | 8 | const ALLOWED_EXTENSIONS = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'tif']; |
| 9 | |||
| 10 | static public function imagickCleanAndWriteImage(string $image_data, string $local_path): bool // "string" parce que file_get_contents... | ||
| 9 | { | 11 | { |
| 12 | $format = strtolower(pathinfo($local_path)['extension']); | ||
| 10 | try{ | 13 | try{ |
| 11 | $imagick = new Imagick(); | 14 | $imagick = new Imagick(); |
| 12 | $imagick->readImageBlob($image_data); | 15 | $imagick->readImageBlob($image_data); |
| 13 | $imagick->stripImage(); // nettoyage métadonnées | 16 | $imagick->stripImage(); // nettoyage métadonnées |
| 14 | $imagick->setImageFormat($format); | 17 | //$imagick->setImageFormat($format); // inutile, writeImage force la conversion |
| 15 | if($format === 'jpeg'){ | 18 | |
| 16 | $imagick->setImageCompression(Imagick::COMPRESSION_JPEG); | 19 | // compression |
| 17 | $imagick->setImageCompressionQuality(85); // optionnel | 20 | switch($format){ |
| 21 | case 'jpeg': // particularité du switch, si 'jpeg' le test de 'jpg' est ignoré et on va jusqu'au break | ||
| 22 | case 'jpg': | ||
| 23 | $imagick->setImageCompression(Imagick::COMPRESSION_JPEG); | ||
| 24 | $imagick->setImageCompressionQuality(85); | ||
| 25 | break; | ||
| 26 | case 'webp': | ||
| 27 | $imagick->setImageCompression(Imagick::COMPRESSION_WEBP); | ||
| 28 | $imagick->setImageCompressionQuality(85); | ||
| 29 | break; | ||
| 30 | case 'png': | ||
| 31 | $imagick->setImageCompression(Imagick::COMPRESSION_ZIP); | ||
| 32 | $imagick->setImageCompressionQuality(7); // 9 est sans perte | ||
| 33 | break; | ||
| 34 | case 'tiff': | ||
| 35 | $imagick->setImageCompression(Imagick::COMPRESSION_LZW); // LZW est sans perte | ||
| 36 | break; | ||
| 18 | } | 37 | } |
| 38 | |||
| 39 | // enregistrement | ||
| 40 | // writeImage utilise l'extension du fichier et ignore le format détecté | ||
| 41 | // imagemagick est à l'origine une appli console, elle considère que l'extension montre l'intention de | ||
| 19 | $imagick->writeImage($local_path); // enregistrement | 42 | $imagick->writeImage($local_path); // enregistrement |
| 20 | $imagick->clear(); | 43 | $imagick->clear(); |
| 21 | $imagick->destroy(); | 44 | $imagick->destroy(); |
| @@ -25,7 +48,7 @@ class ImageUploadController | |||
| 25 | return false; | 48 | return false; |
| 26 | } | 49 | } |
| 27 | } | 50 | } |
| 28 | static public function curlDownloadImage(string $url, $maxRetries = 3, $timeout = 10): string|false | 51 | static public function curlDownloadImage(string $url, int $maxRetries = 3, int $timeout = 10): string|false |
| 29 | { | 52 | { |
| 30 | $attempt = 0; | 53 | $attempt = 0; |
| 31 | $imageData = false; | 54 | $imageData = false; |
| @@ -43,8 +66,6 @@ class ImageUploadController | |||
| 43 | $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | 66 | $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 44 | //$curlError = curl_error($ch); | 67 | //$curlError = curl_error($ch); |
| 45 | 68 | ||
| 46 | curl_close($ch); | ||
| 47 | |||
| 48 | if($imageData !== false && $httpCode >= 200 && $httpCode < 300){ | 69 | if($imageData !== false && $httpCode >= 200 && $httpCode < 300){ |
| 49 | return $imageData; | 70 | return $imageData; |
| 50 | } | 71 | } |
| @@ -59,42 +80,45 @@ class ImageUploadController | |||
| 59 | // téléchargement par le plugin (bouton "insérer une image") | 80 | // téléchargement par le plugin (bouton "insérer une image") |
| 60 | static public function imageUploadTinyMce(): void | 81 | static public function imageUploadTinyMce(): void |
| 61 | { | 82 | { |
| 62 | if(isset($_FILES['file'])){ | 83 | if(!isset($_FILES['file'])){ |
| 63 | $file = $_FILES['file']; | 84 | http_response_code(400); |
| 64 | $dest = 'user_data/images/'; | ||
| 65 | $dest_mini = 'user_data/images-mini/'; | ||
| 66 | |||
| 67 | // Vérifier si les répertoires existent, sinon les créer | ||
| 68 | if(!is_dir($dest)){ | ||
| 69 | mkdir($dest, 0777, true); | ||
| 70 | } | ||
| 71 | if(!is_dir($dest_mini)){ | ||
| 72 | mkdir($dest_mini, 0777, true); | ||
| 73 | } | ||
| 74 | |||
| 75 | $allowed_extensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'tif']; | ||
| 76 | $name = Security::secureFileName(pathinfo($file['name'], PATHINFO_FILENAME)); | ||
| 77 | $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); | ||
| 78 | if(!in_array($extension, $allowed_extensions) || $extension === 'jpg'){ | ||
| 79 | $extension = 'jpeg'; | ||
| 80 | } | ||
| 81 | $file_path = uniqid($dest . $name . '_') . '.' . $extension; | ||
| 82 | |||
| 83 | // créer une miniature de l'image | ||
| 84 | // | ||
| 85 | |||
| 86 | if(self::imagickCleanImage(file_get_contents($file['tmp_name']), $file_path, $extension)){ // recréer l’image pour la nettoyer | ||
| 87 | echo json_encode(['location' => $file_path]); // renvoyer l'URL de l'image téléchargée | ||
| 88 | } | ||
| 89 | else{ | ||
| 90 | http_response_code(500); | ||
| 91 | echo json_encode(['message' => 'Erreur image non valide', 'format' => $extension]); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | else{ | ||
| 95 | http_response_code(400); | ||
| 96 | echo json_encode(['message' => 'Erreur 400: Bad Request']); | 85 | echo json_encode(['message' => 'Erreur 400: Bad Request']); |
| 97 | } | 86 | } |
| 87 | if(!is_uploaded_file($_FILES['file']['tmp_name'])) { | ||
| 88 | http_response_code(400); | ||
| 89 | echo json_encode(['message' => "Le fichier n'a pas été téléchargé correctement."]); | ||
| 90 | die; | ||
| 91 | } | ||
| 92 | |||
| 93 | $dest = 'user_data/images/'; | ||
| 94 | $dest_mini = 'user_data/images-mini/'; | ||
| 95 | |||
| 96 | // Vérifier si les répertoires existent, sinon les créer | ||
| 97 | if(!is_dir($dest)){ | ||
| 98 | mkdir($dest, 0755, true); | ||
| 99 | } | ||
| 100 | if(!is_dir($dest_mini)){ | ||
| 101 | mkdir($dest_mini, 0755, true); | ||
| 102 | } | ||
| 103 | |||
| 104 | $name = Security::secureFileName(pathinfo($_FILES['file']['name'], PATHINFO_FILENAME)); | ||
| 105 | $extension = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION)); | ||
| 106 | $image_data = file_get_contents($_FILES['file']['tmp_name']); | ||
| 107 | if(!in_array($extension, self::ALLOWED_EXTENSIONS)){ | ||
| 108 | $extension = 'jpeg'; | ||
| 109 | } | ||
| 110 | $local_path = uniqid($dest . $name . '_') . '.' . $extension; | ||
| 111 | |||
| 112 | // créer une miniature de l'image | ||
| 113 | // | ||
| 114 | |||
| 115 | if(self::imagickCleanAndWriteImage($image_data, $local_path)){ // recréer l’image pour la nettoyer | ||
| 116 | echo json_encode(['location' => $local_path]); // renvoyer l'URL de l'image téléchargée | ||
| 117 | } | ||
| 118 | else{ | ||
| 119 | http_response_code(500); | ||
| 120 | echo json_encode(['message' => 'Erreur image non valide']); | ||
| 121 | } | ||
| 98 | die; | 122 | die; |
| 99 | } | 123 | } |
| 100 | 124 | ||
| @@ -103,40 +127,39 @@ class ImageUploadController | |||
| 103 | { | 127 | { |
| 104 | $json = json_decode(file_get_contents('php://input'), true); | 128 | $json = json_decode(file_get_contents('php://input'), true); |
| 105 | 129 | ||
| 106 | if(isset($json['image_url'])){ | 130 | if(!isset($json['image_url'])){ |
| 107 | $image_data = self::curlDownloadImage($json['image_url']); // téléchargement de l’image par le serveur avec cURL au lieu de file_get_contents | 131 | http_response_code(400); |
| 108 | $dest = 'user_data/images/'; | 132 | echo json_encode(['message' => "Erreur 400: Bad Request"]); |
| 109 | 133 | die; | |
| 110 | if(!is_dir($dest)) { // Vérifier si le répertoire existe, sinon le créer | ||
| 111 | mkdir($dest, 0777, true); | ||
| 112 | } | ||
| 113 | |||
| 114 | if($image_data === false){ | ||
| 115 | http_response_code(400); | ||
| 116 | echo json_encode(['message' => "Erreur, le serveur n'a pas réussi à télécharger l'image."]); | ||
| 117 | die; | ||
| 118 | } | ||
| 119 | |||
| 120 | $allowed_extensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'tif']; | ||
| 121 | $url_path = parse_url($json['image_url'], PHP_URL_PATH); | ||
| 122 | $name = Security::secureFileName(pathinfo($url_path, PATHINFO_FILENAME)); | ||
| 123 | $extension = strtolower(pathinfo($url_path, PATHINFO_EXTENSION)); | ||
| 124 | if(!in_array($extension, $allowed_extensions) || $extension === 'jpg'){ | ||
| 125 | $extension = 'jpeg'; | ||
| 126 | } | ||
| 127 | $local_path = uniqid($dest . $name . '_') . '.' . $extension; | ||
| 128 | |||
| 129 | if(self::imagickCleanImage($image_data, $local_path, $extension)){ // recréer l’image pour la nettoyer | ||
| 130 | echo json_encode(['location' => $local_path]); // nouvelle adresse | ||
| 131 | } | ||
| 132 | else{ | ||
| 133 | http_response_code(500); | ||
| 134 | echo json_encode(['message' => 'Erreur image non valide', 'format' => $extension]); | ||
| 135 | } | ||
| 136 | } | ||
| 137 | else{ | ||
| 138 | echo json_encode(['message' => 'Erreur 400: Bad Request']); | ||
| 139 | } | 134 | } |
| 135 | |||
| 136 | $image_data = self::curlDownloadImage($json['image_url']); // téléchargement de l’image par le serveur avec cURL au lieu de file_get_contents | ||
| 137 | if(!$image_data){ | ||
| 138 | http_response_code(400); | ||
| 139 | echo json_encode(['message' => "Erreur, le serveur n'a pas réussi à télécharger l'image."]); | ||
| 140 | die; | ||
| 141 | } | ||
| 142 | |||
| 143 | $dest = 'user_data/images/'; | ||
| 144 | if(!is_dir($dest)) { // Vérifier si le répertoire existe, sinon le créer | ||
| 145 | mkdir($dest, 0755, true); | ||
| 146 | } | ||
| 147 | |||
| 148 | $url_path = parse_url($json['image_url'], PHP_URL_PATH); | ||
| 149 | $name = Security::secureFileName(pathinfo($url_path, PATHINFO_FILENAME)); | ||
| 150 | $extension = strtolower(pathinfo($url_path, PATHINFO_EXTENSION)); | ||
| 151 | if(!in_array($extension, self::ALLOWED_EXTENSIONS) || $extension === 'jpg'){ | ||
| 152 | $extension = 'jpeg'; | ||
| 153 | } | ||
| 154 | $local_path = uniqid($dest . $name . '_') . '.' . $extension; | ||
| 155 | |||
| 156 | if(self::imagickCleanAndWriteImage($image_data, $local_path)){ // recréer l’image pour la nettoyer | ||
| 157 | echo json_encode(['location' => $local_path]); // nouvelle adresse | ||
| 158 | } | ||
| 159 | else{ | ||
| 160 | http_response_code(500); | ||
| 161 | echo json_encode(['message' => 'Erreur image non valide', 'format' => $extension]); | ||
| 162 | } | ||
| 140 | die; | 163 | die; |
| 141 | } | 164 | } |
| 142 | 165 | ||
| @@ -147,7 +170,7 @@ class ImageUploadController | |||
| 147 | $dest = 'user_data/images/'; | 170 | $dest = 'user_data/images/'; |
| 148 | 171 | ||
| 149 | if(!is_dir($dest)){ | 172 | if(!is_dir($dest)){ |
| 150 | mkdir($dest, 0777, true); | 173 | mkdir($dest, 0755, true); |
| 151 | } | 174 | } |
| 152 | 175 | ||
| 153 | // détection de data:image/ et de ;base64, et capture du format dans $type | 176 | // détection de data:image/ et de ;base64, et capture du format dans $type |
| @@ -157,22 +180,21 @@ class ImageUploadController | |||
| 157 | die; | 180 | die; |
| 158 | } | 181 | } |
| 159 | 182 | ||
| 160 | $allowed_extensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'tif']; | 183 | $extension = strtolower($type[1]); // dans (\w+) |
| 161 | $extension = strtolower($type[1]); | 184 | if(!in_array($extension, self::ALLOWED_EXTENSIONS)){ |
| 162 | if(!in_array($extension, $allowed_extensions) || $extension === 'jpg'){ | ||
| 163 | $extension = 'jpeg'; | 185 | $extension = 'jpeg'; |
| 164 | } | 186 | } |
| 165 | 187 | ||
| 188 | $name = 'pasted_image'; | ||
| 166 | $image_data = base64_decode(substr($json['image_base64'], strpos($json['image_base64'], ',') + 1)); // découpe la chaine à la virgule puis convertit en binaire | 189 | $image_data = base64_decode(substr($json['image_base64'], strpos($json['image_base64'], ',') + 1)); // découpe la chaine à la virgule puis convertit en binaire |
| 167 | if($image_data === false){ | 190 | if($image_data === false){ |
| 168 | http_response_code(400); | 191 | http_response_code(400); |
| 169 | echo json_encode(['message' => 'Décodage base64 invalide']); | 192 | echo json_encode(['message' => 'Décodage base64 invalide']); |
| 170 | die; | 193 | die; |
| 171 | } | 194 | } |
| 172 | 195 | $local_path = uniqid($dest . $name . '_') . '.' . $extension; | |
| 173 | $local_path = uniqid($dest . 'pasted_image_') . '.' . $extension; | ||
| 174 | 196 | ||
| 175 | if(self::imagickCleanImage($image_data, $local_path)){ | 197 | if(self::imagickCleanAndWriteImage($image_data, $local_path)){ |
| 176 | echo json_encode(['location' => $local_path]); | 198 | echo json_encode(['location' => $local_path]); |
| 177 | } | 199 | } |
| 178 | else{ | 200 | else{ |
diff --git a/src/router.php b/src/router.php index 7348f2b..ff219da 100644 --- a/src/router.php +++ b/src/router.php | |||
| @@ -231,12 +231,17 @@ elseif($request->getMethod() === 'POST'){ | |||
| 231 | } | 231 | } |
| 232 | } | 232 | } |
| 233 | 233 | ||
| 234 | // upload d'image dans tinymce avec le plugin (bouton "insérer une image" de l'éditeur) | 234 | // upload avec FormData |
| 235 | elseif(strpos($_SERVER['CONTENT_TYPE'], 'multipart/form-data') !== false) | 235 | elseif(strpos($_SERVER['CONTENT_TYPE'], 'multipart/form-data') !== false) |
| 236 | { | 236 | { |
| 237 | // dans tinymce avec le plugin (bouton "insérer une image" de l'éditeur ou glisser-déposer) | ||
| 237 | if($request->query->has('action') && $request->query->get('action') === 'upload_image_tinymce'){ | 238 | if($request->query->has('action') && $request->query->get('action') === 'upload_image_tinymce'){ |
| 238 | ImageUploadController::imageUploadTinyMce(); | 239 | ImageUploadController::imageUploadTinyMce(); |
| 239 | } | 240 | } |
| 241 | // dans tinymce, des quatre méthodes: bouton "link", drag & drop, html, base64 | ||
| 242 | elseif($request->query->has('action') && $request->query->get('action') === 'upload_file_tinymce'){ | ||
| 243 | FileUploadController::fileUploadTinyMce(); | ||
| 244 | } | ||
| 240 | elseif($request->query->has('head_foot_image')){ | 245 | elseif($request->query->has('head_foot_image')){ |
| 241 | HeadFootController::uploadAsset($entityManager, $request->query->get('head_foot_image')); | 246 | HeadFootController::uploadAsset($entityManager, $request->query->get('head_foot_image')); |
| 242 | } | 247 | } |
