summaryrefslogtreecommitdiff
path: root/src/controller/ajax.php
diff options
context:
space:
mode:
authorpolo <ordipolo@gmx.fr>2025-08-02 17:03:42 +0200
committerpolo <ordipolo@gmx.fr>2025-08-02 17:03:42 +0200
commit9934a32f7e02c484d6b122c9af860ab1ca9b2dca (patch)
treef4f8d621a42104246375c0489e19e4673d726279 /src/controller/ajax.php
parent20e1d288035a274b48f0d2d26f547ad15e99761d (diff)
downloadcms-9934a32f7e02c484d6b122c9af860ab1ca9b2dca.zip
réorganisation 2 requêtes "form": séparation routage et contrôleurs avec des fonctions
Diffstat (limited to 'src/controller/ajax.php')
-rw-r--r--src/controller/ajax.php99
1 files changed, 0 insertions, 99 deletions
diff --git a/src/controller/ajax.php b/src/controller/ajax.php
deleted file mode 100644
index 16a6c1f..0000000
--- a/src/controller/ajax.php
+++ /dev/null
@@ -1,99 +0,0 @@
1<?php
2// src/controller/ajax.php
3
4declare(strict_types=1);
5
6use PHPMailer\PHPMailer\PHPMailer;
7use PHPMailer\PHPMailer\Exception;
8use App\Entity\Email;
9
10// mettre ça ailleurs?
11function sendEmail(string $recipient, bool $true_email, string $name = '', string $email = '', string $message = ''): bool
12{
13 $mail = new PHPMailer(true); // true => exceptions
14 $mail->CharSet = 'UTF-8';
15
16 try{
17 // Paramètres du serveur
18 $mail->isSMTP();
19 $mail->Host = Config::$smtp_host;
20 $mail->SMTPAuth = true;
21 $mail->Port = 25;
22
23 if($mail->SMTPAuth){
24 $mail->Username = Config::$smtp_username; // e-mail
25 $mail->Password = Config::$smtp_password;
26 $mail->SMTPSecure = Config::$smtp_secure; // tls (starttls) ou ssl (smtps)
27 if($mail->SMTPSecure === 'tls'){
28 $mail->Port = 587;
29 }
30 elseif($mail->SMTPSecure === 'ssl'){
31 $mail->Port = 465;
32 }
33 }
34 //var_dump($mail->smtpConnect());die; // test de connexion
35
36 // Expéditeur et destinataire
37 $mail->setFrom(strtolower(Config::$email_from), Config::$email_from_name); // expéditeur
38 $mail->addAddress(strtolower($recipient), Config::$email_dest_name); // destinataire
39
40 // Contenu
41 $mail->isHTML(true);
42 if($true_email){
43 $mail->Subject = 'Message envoyé par: ' . $name . ' (' . $email . ') depuis le site web';
44
45 }
46 else{
47 $mail->Subject = "TEST d'un envoi d'e-mail depuis le site web";
48 }
49 $mail->Body = $message;
50 $mail->AltBody = $message;
51
52 $mail->send();
53 return true;
54 }
55 catch(Exception $e){
56 return false;
57 //echo "Le message n'a pas pu être envoyé. Erreur : {$mail->ErrorInfo}";
58 }
59}
60
61
62// détection des requêtes envoyées avec fetch (application/json) et récupération du JSON
63if($_SERVER['CONTENT_TYPE'] === 'application/json')
64{
65 $data = file_get_contents('php://input');
66 $json = json_decode($data, true);
67
68 if(isset($_GET['action']))
69 {
70 /* -- bloc Formulaire -- */
71 if($_GET['action'] === 'send_email'){
72 $captcha_solution = (isset($_SESSION['captcha']) && is_int($_SESSION['captcha'])) ? $_SESSION['captcha'] : 0;
73 $captcha_try = isset($json['captcha']) ? Captcha::controlInput($json['captcha']) : 0;
74
75 // contrôles des entrées
76 $name = htmlspecialchars(trim($json['name']));
77 $email = strtolower(htmlspecialchars(trim($json['email'])));
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;
83
84 if($captcha_try != 0 && $captcha_solution != 0 && ($captcha_try === $captcha_solution)
85 && filter_var($email, FILTER_VALIDATE_EMAIL) && isset($json['hidden']) && empty($json['hidden'])
86 && sendEmail($recipient, true, $name, $email, $message))
87 {
88 $db_email = new Email($email, Config::$email_dest, $message);
89 $entityManager->persist($db_email);
90 $entityManager->flush();
91 echo json_encode(['success' => true]);
92 }
93 else{
94 echo json_encode(['success' => false]);
95 }
96 die;
97 }
98 }
99} \ No newline at end of file