summaryrefslogtreecommitdiff
path: root/src/controller/ajax.php
diff options
context:
space:
mode:
authorpolo <ordipolo@gmx.fr>2025-07-04 10:29:45 +0200
committerpolo <ordipolo@gmx.fr>2025-07-04 10:29:45 +0200
commit3104104e838b7d57de49daba0178b1aefe646548 (patch)
tree8af33f7bb83ccfef04b6e82a0fcd687ef475d455 /src/controller/ajax.php
parent2d7bacce891eab0adb0263d598bfe44418788f42 (diff)
downloadcms-3104104e838b7d57de49daba0178b1aefe646548.zip
formulaire de contact 4
Diffstat (limited to 'src/controller/ajax.php')
-rw-r--r--src/controller/ajax.php15
1 files changed, 9 insertions, 6 deletions
diff --git a/src/controller/ajax.php b/src/controller/ajax.php
index 7529fe6..8a7cb34 100644
--- a/src/controller/ajax.php
+++ b/src/controller/ajax.php
@@ -8,7 +8,7 @@ use PHPMailer\PHPMailer\Exception;
8use App\Entity\Email; 8use App\Entity\Email;
9 9
10// mettre ça ailleurs? 10// mettre ça ailleurs?
11function sendEmail(bool $true_email, string $name = '', string $email = '', string $message = ''): bool 11function sendEmail(string $recipient, bool $true_email, string $name = '', string $email = '', string $message = ''): bool
12{ 12{
13 $mail = new PHPMailer(true); // true => exceptions 13 $mail = new PHPMailer(true); // true => exceptions
14 $mail->CharSet = 'UTF-8'; 14 $mail->CharSet = 'UTF-8';
@@ -35,7 +35,7 @@ function sendEmail(bool $true_email, string $name = '', string $email = '', stri
35 35
36 // Expéditeur et destinataire 36 // Expéditeur et destinataire
37 $mail->setFrom(strtolower(Config::$email_from), Config::$email_from_name); // expéditeur 37 $mail->setFrom(strtolower(Config::$email_from), Config::$email_from_name); // expéditeur
38 $mail->addAddress(strtolower(Config::$email_dest), Config::$email_dest_name); // destinataire 38 $mail->addAddress(strtolower($recipient), Config::$email_dest_name); // destinataire
39 39
40 // Contenu 40 // Contenu
41 $mail->isHTML(true); 41 $mail->isHTML(true);
@@ -65,10 +65,9 @@ if($_SERVER['CONTENT_TYPE'] === 'application/json')
65 $data = file_get_contents('php://input'); 65 $data = file_get_contents('php://input');
66 $json = json_decode($data, true); 66 $json = json_decode($data, true);
67 67
68 // requêtes de tinymce ou touchant aux articles
69 if(isset($_GET['action'])) 68 if(isset($_GET['action']))
70 { 69 {
71 // e-mail envoyé par le formulaire de contact 70 /* -- bloc Formulaire -- */
72 if($_GET['action'] === 'send_email'){ 71 if($_GET['action'] === 'send_email'){
73 $captcha_solution = (isset($_SESSION['captcha']) && is_int($_SESSION['captcha'])) ? $_SESSION['captcha'] : 0; 72 $captcha_solution = (isset($_SESSION['captcha']) && is_int($_SESSION['captcha'])) ? $_SESSION['captcha'] : 0;
74 $captcha_try = isset($json['captcha']) ? Captcha::controlInput($json['captcha']) : 0; 73 $captcha_try = isset($json['captcha']) ? Captcha::controlInput($json['captcha']) : 0;
@@ -77,12 +76,16 @@ if($_SERVER['CONTENT_TYPE'] === 'application/json')
77 $name = htmlspecialchars(trim($json['name'])); 76 $name = htmlspecialchars(trim($json['name']));
78 $email = strtolower(htmlspecialchars(trim($json['email']))); 77 $email = strtolower(htmlspecialchars(trim($json['email'])));
79 $message = htmlspecialchars(trim($json['message'])); 78 $message = htmlspecialchars(trim($json['message']));
79
80 // destinataire = e-mail par défaut dans config.ini OU choisi par l'utilisateur
81 $form_data = $entityManager->find('App\Entity\NodeData', $json['id']);
82 $recipient = $form_data->getData()['email'] ?? Config::$email_dest;
80 83
81 if($captcha_try != 0 && $captcha_solution != 0 && ($captcha_try === $captcha_solution) 84 if($captcha_try != 0 && $captcha_solution != 0 && ($captcha_try === $captcha_solution)
82 && filter_var($email, FILTER_VALIDATE_EMAIL) && isset($json['hidden']) && empty($json['hidden']) 85 && filter_var($email, FILTER_VALIDATE_EMAIL) && isset($json['hidden']) && empty($json['hidden'])
83 && sendEmail(true, $name, $email, $message)) 86 && sendEmail($recipient, true, $name, $email, $message))
84 { 87 {
85 $db_email = new Email(strtolower(Config::$email_from), strtolower(Config::$email_dest), $message); 88 $db_email = new Email(Config::$email_from, Config::$email_dest, $message);
86 $entityManager->persist($db_email); 89 $entityManager->persist($db_email);
87 $entityManager->flush(); 90 $entityManager->flush();
88 echo json_encode(['success' => true]); 91 echo json_encode(['success' => true]);