diff options
Diffstat (limited to 'src/controller/ajax.php')
-rw-r--r-- | src/controller/ajax.php | 99 |
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 | |||
4 | declare(strict_types=1); | ||
5 | |||
6 | use PHPMailer\PHPMailer\PHPMailer; | ||
7 | use PHPMailer\PHPMailer\Exception; | ||
8 | use App\Entity\Email; | ||
9 | |||
10 | // mettre ça ailleurs? | ||
11 | function 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 | ||
63 | if($_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 | ||