From c98ddbd9cb6c63f7effd45829e1ca9ff78a3a6b2 Mon Sep 17 00:00:00 2001
From: filippo.bertilotti <filippobertilotti@gmail.com>
Date: Fri, 03 May 2024 10:47:19 +0200
Subject: [PATCH] aggiunta specifica restituzione tipi alle functions

---
 htdocs/contact.php |  101 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 100 insertions(+), 1 deletions(-)

diff --git a/htdocs/contact.php b/htdocs/contact.php
index 47749df..1f5505c 100644
--- a/htdocs/contact.php
+++ b/htdocs/contact.php
@@ -5,7 +5,106 @@
 
     $smarty->setTemplateDir('../ihtml');
     $smarty->setCompileDir('../compile');
+
+    $errorMsgs = [];
+    $validazioneOk = true;
+
+    $number = $_POST['number'] ?? '';
+    $email = $_POST['email'] ?? '';
+    $msg = '';
+
+    /**
+     *  Inserisce i dati trasmessi dal form ($email e $number) nella tabella
+     */
+    function insertDataOnTable(mysqli $mysql, string $email, string $number): bool { 
+        $esitoInserimento = false;
+        $query = "INSERT INTO Contact (email, telefono) VALUES ('$email', '$number');";
+        try {
+            $mysql->query($query);
+            $esitoInserimento = true;
+        } catch (Exception $e) {
+            echo $e->getMessage();
+        }
+        return $esitoInserimento;
+    }
+
+
+    /**
+     *  controlla se ci sono errori sulla digitazione dei dati messi nel form
+     */
+    function controlloErrori(string $email, string $number, array &$errorMsgs): bool {
+        if(!is_numeric($number) || strlen($number) != 10) {
+            $errorMsgs['number'] = 'Numero di telefono non corretto';
+        }else {
+            //$errorMsgs['number'] = '';
+        }
     
+        if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
+            $errorMsgs['email'] = 'Email non valida';
+        } else {
+            //$errorMsgs['email'] = '';
+        }
+
+        $validazioneOk = count($errorMsgs) == 0;
+
+        return $validazioneOk;
+    }
+
+    /**
+     *  controlla se nelle 24 ore prima non sia stata messa nel campo email la stessa mail per evitarne lo spam
+     */
+    function controlloSpamEmail(mysqli $connection, string $email): bool {
+        $spam = false;
+        $query = "SELECT email, dtm_richiesta FROM Contact WHERE email = '$email' AND dtm_richiesta BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) AND NOW();";
+        
+        
+        $result = $connection->query($query);
+        
+        $rowcount=mysqli_num_rows($result);
+        
+        if ($rowcount > 0) {
+            $spam = true;
+        }
+
+        return $spam;
+    }
+    
+
+
+    if($_SERVER['REQUEST_METHOD'] === 'POST') {
+        $validazioneOk = controlloErrori($email, $number, $errorMsgs);
+
+        if($validazioneOk) {
+            try {
+                $conn = new mysqli('127.0.0.1', 'root', '', 'contact_db');
+            } catch (Exception $e) {
+                die($e->getMessage());
+            }
+
+            if(controlloSpamEmail($conn, $email)) {
+                $smarty->display('avviso.tpl');
+                exit;
+            }
+            
+            $esitoInserimento = insertDataOnTable($conn, $email, $number);
+            
+
+            if($esitoInserimento == true) {
+                $smarty->display('conferma.tpl');
+                exit;
+            }
+        }
+    }
+    
+    
+
+    $smarty->assign('cellNumber', $number);
+    $smarty->assign('email', $email);
+    $smarty->assign('formMsg', $msg);
+    
+    $smarty->assign('errorMsgs', $errorMsgs);
+
     $smarty->display('contact.tpl');
-    echo "contact";
+  
+    
 ?>
\ No newline at end of file

--
Gitblit v1.8.0