From 5d87ee20d0d34ba676b10b5f67f4251428e1f2f7 Mon Sep 17 00:00:00 2001 From: polo Date: Fri, 2 Jan 2026 23:28:41 +0100 Subject: =?UTF-8?q?upload=20images=20et=20documents=20par=20copier-coller?= =?UTF-8?q?=20et=20glisser-d=C3=A9poser,=20format=20whitelist=20par=20exte?= =?UTF-8?q?nsions=20c=C3=B4t=C3=A9=20JS,=20erreur=20chemin=20traduction,?= =?UTF-8?q?=20ajout=20format=20RTF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/index.php | 258 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 166 insertions(+), 92 deletions(-) (limited to 'public') diff --git a/public/index.php b/public/index.php index 2cd0c78..f46b384 100644 --- a/public/index.php +++ b/public/index.php @@ -72,7 +72,8 @@ function curlDownloadImage(string $url, int $maxRetries = 3, int $timeout = 10): return false; // échec après trois tentatives } -function sanitizeFileName(string $filename): string { +function sanitizeFileName(string $filename): string +{ $filename = preg_replace('/[^a-zA-Z0-9_-]/', '_', $filename); // ne garder que les lettres, chiffres, tirets et underscores $filename = preg_replace('/_+/', '_', $filename); // doublons d'underscores return trim($filename, '_'); @@ -80,8 +81,8 @@ function sanitizeFileName(string $filename): string { function checkFileDownload(array $file): bool { - $extensions_white_list = ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'odt', 'ods', 'odp']; - $mime_type_white_list = ['application/pdf', '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']; + $extensions_white_list = ['pdf', 'rtf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'odt', 'ods', 'odp']; // = extensions_white_list côté javascript + $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']; // 1/ extension $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); @@ -93,18 +94,15 @@ function checkFileDownload(array $file): bool if(!is_uploaded_file($file['tmp_name'])){ return false; } - - $finfo = finfo_open(FILEINFO_MIME_TYPE); - + // 3/ objet $finfo valide (dépend du paramètre FILEINFO_MIME_TYPE) + $finfo = new finfo(FILEINFO_MIME_TYPE); if($finfo === false){ return false; } - $real_type = finfo_file($finfo, $file['tmp_name']); - finfo_close($finfo); - // 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...) + $real_type = finfo_file($finfo, $file['tmp_name']); return in_array($real_type, $mime_type_white_list, true); } @@ -113,9 +111,9 @@ if(isset($_GET['action']) && $_GET['action'] == 'editor_submit'){ $data = file_get_contents('php://input'); $json = json_decode($data, true); - if(json_last_error() === JSON_ERROR_NONE) { + if(json_last_error() === JSON_ERROR_NONE){ // Traitement côté serveur - $articleId = $json['id']; + $article_id = $json['id']; $content = $json['content']; // retour au client @@ -135,7 +133,7 @@ elseif(isset($_GET['action']) && $_GET['action'] == 'delete_article'){ $success = true; // retour au client - if($success) { + if($success){ echo json_encode(['success' => true]); } else { @@ -145,8 +143,6 @@ elseif(isset($_GET['action']) && $_GET['action'] == 'delete_article'){ } die; } - - elseif(isset($_GET['action']) && in_array($_GET['action'], ['upload_image', 'upload_image_url', 'upload_image_base64'])){ $dest = 'images/'; if(!is_dir($dest)){ @@ -222,8 +218,6 @@ elseif(isset($_GET['action']) && in_array($_GET['action'], ['upload_image', 'upl } die; } - - elseif(isset($_GET['action']) && $_GET['action'] == 'upload_file'){ if(isset($_FILES['file'])){ $dest = 'media/'; @@ -275,15 +269,15 @@ elseif(isset($_GET['action']) && $_GET['action'] == 'upload_file'){ -- cgit v1.2.3