diff --git a/controllers/connect.php b/controllers/connect.php index 3660a50..2ce7f2b 100755 --- a/controllers/connect.php +++ b/controllers/connect.php @@ -55,6 +55,7 @@ $_SESSION['connect'] = true; $_SESSION['admin'] = $user['admin']; $_SESSION['email'] = $user['email']; + $_SESSION['transfer'] = $user['transfer']; $_SESSION['csrf'] = str_shuffle(uniqid().uniqid()); header('Location: ' . $this->generateUrl('')); return true; diff --git a/controllers/internalConsole.php b/controllers/internalConsole.php index 7829201..325946a 100755 --- a/controllers/internalConsole.php +++ b/controllers/internalConsole.php @@ -20,6 +20,11 @@ ), 'optionals' => array(), ), + 'sendTransfers' => array( + 'description' => 'Cette commande permet d\'envoyer par mails les sms à transférés.', + 'requireds' => [], + 'optionals' => [], + ), ); $message = "Vous êtes ici dans l'aide de la console.\n"; @@ -274,6 +279,9 @@ die(7); } + //On insert le SMS dans le tableau des sms à envoyer par mail + $db->insertIntoTable('transfers', ['content' => $content]); + //Chaque commande sera executée. foreach ($found_commands as $command_name => $command) { @@ -286,4 +294,45 @@ sleep(2); } } + + /** + * Cette fonction permet d'envoyer par mail les sms à transférer + */ + public function sendTransfers () + { + global $db; + + $transfers = $db->getFromTableWhere('transfers', ['progress' => false]); + + $ids_transfers = []; + $ids_receiveds = []; + foreach ($transfers as $transfer) + { + $ids_transfers[] = $transfer['id']; + $ids_receiveds[] = $transfer['id_received']; + } + + $db->updateProgressTransfersIn($ids_transfers, true); + + $receiveds = $db->getReceivedsIn($ids_receiveds); + + $users = $db->getFromTableWhere('users', ['transfer' => true]); + + foreach ($users as $user) + { + foreach ($receiveds as $received) + { + echo "Transfer d'un SMS du " . $received['send_by'] . " à l'email " . $user['email']; + $to = $user['email']; + $subject = '[RaspiSMS] - Transfert d\'un SMS du ' . $received['send_by']; + $message = " + Le numéro " . $received['send_by'] . " vous a envoyé un SMS\n + -----------------------------------------------------------\n" . $received['content']; + + $ok = mail($to, $subject, $message); + + echo " ... " . ($ok ? 'OK' : 'KO') . "\n"; + } + } + } } diff --git a/controllers/profile.php b/controllers/profile.php index 377c6ad..a81ac72 100755 --- a/controllers/profile.php +++ b/controllers/profile.php @@ -35,7 +35,7 @@ if (!internalTools::verifyCSRF($csrf)) { $_SESSION['errormessage'] = 'Jeton CSRF invalide !'; - header('Location: ' . $this->generateUrl('profile', 'showAll')); + header('Location: ' . $this->generateUrl('profile')); return false; } @@ -45,7 +45,7 @@ if (empty($_POST['password']) || empty($_POST['verif_password']) || $_POST['password'] != $_POST['verif_password']) { $_SESSION['errormessage'] = 'Les mots de passe ne correspondent pas.'; - header('Location: ' . $this->generateUrl('profile', 'show')); + header('Location: ' . $this->generateUrl('profile')); return false; } @@ -55,12 +55,52 @@ if (!$db->updateTableWhere('users', ['password' => $password], ['id' => $user[0]['id']])) { $_SESSION['errormessage'] = 'Impossible de mettre à jour le mot de passe.'; - header('Location: ' . $this->generateUrl('profile', 'show')); + header('Location: ' . $this->generateUrl('profile')); return false; } $_SESSION['successmessage'] = 'Les données ont été mises à jour.'; - header('Location: ' . $this->generateUrl('profile', 'show')); + header('Location: ' . $this->generateUrl('profile')); + return true; + } + + /** + * Cette fonction change la valeur du champ "transfer" de l'utilisateur + * @param $csrf : Le jeton CSRF + * @param string $_POST['transfer'] : Le nouveau transfer de l'utilisateur + * @return void; + */ + public function changeTransfer($csrf) + { + //On vérifie que le jeton csrf est bon + if (!internalTools::verifyCSRF($csrf)) + { + $_SESSION['errormessage'] = 'Jeton CSRF invalide !'; + header('Location: ' . $this->generateUrl('profile')); + return false; + } + + //Creation de l'object de base de données + global $db; + + if (!isset($_POST['transfer'])) + { + $_SESSION['errormessage'] = 'Vous devez renseigner un valeur'; + header('Location: ' . $this->generateUrl('profile')); + return false; + } + + $transfer = (boolean)$_POST['transfer']; + if (!$db->updateTableWhere('users', ['transfer' => $transfer], ['email' => $_SESSION['email']])) + { + $_SESSION['errormessage'] = 'Impossible de mettre les données à jour.'; + header('Location: ' . $this->generateUrl('profile')); + return false; + } + + $_SESSION['transfer'] = $transfer; + $_SESSION['successmessage'] = 'Les données ont été mises à jour.'; + header('Location: ' . $this->generateUrl('profile')); return true; } @@ -77,7 +117,7 @@ if (!internalTools::verifyCSRF($csrf)) { $_SESSION['errormessage'] = 'Jeton CSRF invalide !'; - header('Location: ' . $this->generateUrl('profile', 'showAll')); + header('Location: ' . $this->generateUrl('profile')); return false; } @@ -88,7 +128,7 @@ if (empty($_POST['mail']) || empty($_POST['verif_mail']) || $_POST['mail'] != $_POST['verif_mail']) { $_SESSION['errormessage'] = 'Les e-mails ne correspondent pas.'; - header('Location: ' . $this->generateUrl('profile', 'show', array( + header('Location: ' . $this->generateUrl('profile', array( 'errormessage' => 'Les e-mails ne correspondent pas.' ))); return false; @@ -99,7 +139,7 @@ if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $_SESSION['errormessage'] = 'L\'adresse e-mail est invalide.'; - header('Location: ' . $this->generateUrl('profile', 'show')); + header('Location: ' . $this->generateUrl('profile')); return false; } @@ -108,13 +148,13 @@ if (!$db->updateTableWhere('users', ['email' => $email], ['id' => $user[0]['id']])) { $_SESSION['errormessage'] = 'Cette adresse e-mail est déjà utilisée.'; - header('Location: ' . $this->generateUrl('profile', 'show')); + header('Location: ' . $this->generateUrl('profile')); return false; } $_SESSION['email'] = $email; $_SESSION['successmessage'] = 'Les données ont été mises à jour.'; - header('Location: ' . $this->generateUrl('profile', 'show')); + header('Location: ' . $this->generateUrl('profile')); return true; } @@ -130,7 +170,7 @@ if (!internalTools::verifyCSRF($csrf)) { $_SESSION['errormessage'] = 'Jeton CSRF invalide !'; - header('Location: ' . $this->generateUrl('profile', 'showAll')); + header('Location: ' . $this->generateUrl('profile')); return false; } @@ -141,14 +181,14 @@ if (empty($_POST['delete_account'])) { $_SESSION['errormessage'] = 'Le compte n\'a pas été supprimé'; - header('Location: ' . $this->generateUrl('profile', 'show')); + header('Location: ' . $this->generateUrl('profile')); return false; } if (!$db->deleteFromTableWhere('users', ['email' => $_SESSION['email']])) { $_SESSION['errormessage'] = 'Impossible de supprime le compte'; - header('Location: ' . $this->generateUrl('profile', 'show')); + header('Location: ' . $this->generateUrl('profile')); return false; } diff --git a/createDatabase.sql b/createDatabase.sql index 381c048..4310c08 100755 --- a/createDatabase.sql +++ b/createDatabase.sql @@ -3,7 +3,7 @@ CREATE DATABASE IF NOT EXISTS raspisms; USE raspisms; -CREATE TABLE receiveds +CREATE TABLE IF NOT EXISTS receiveds ( id INT NOT NULL AUTO_INCREMENT, at DATETIME NOT NULL, @@ -13,7 +13,7 @@ CREATE TABLE receiveds PRIMARY KEY (id) ); -CREATE TABLE sendeds +CREATE TABLE IF NOT EXISTS sendeds ( id INT NOT NULL AUTO_INCREMENT, at DATETIME NOT NULL, @@ -22,7 +22,7 @@ CREATE TABLE sendeds PRIMARY KEY (id) ); -CREATE TABLE scheduleds +CREATE TABLE IF NOT EXISTS scheduleds ( id INT NOT NULL AUTO_INCREMENT, at DATETIME NOT NULL, @@ -31,7 +31,7 @@ CREATE TABLE scheduleds PRIMARY KEY (id) ); -CREATE TABLE contacts +CREATE TABLE IF NOT EXISTS contacts ( id INT NOT NULL AUTO_INCREMENT, @@ -41,7 +41,7 @@ CREATE TABLE contacts UNIQUE (name) ); -CREATE TABLE groups +CREATE TABLE IF NOT EXISTS groups ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(100) NOT NULL, @@ -49,7 +49,7 @@ CREATE TABLE groups UNIQUE (name) ); -CREATE TABLE groups_contacts +CREATE TABLE IF NOT EXISTS groups_contacts ( id INT NOT NULL AUTO_INCREMENT, id_group INT NOT NULL, @@ -59,7 +59,7 @@ CREATE TABLE groups_contacts FOREIGN KEY (id_contact) REFERENCES contacts (id) ON DELETE CASCADE ON UPDATE CASCADE ); -CREATE TABLE scheduleds_contacts +CREATE TABLE IF NOT EXISTS scheduleds_contacts ( id INT NOT NULL AUTO_INCREMENT, id_scheduled INT NOT NULL, @@ -69,7 +69,7 @@ CREATE TABLE scheduleds_contacts FOREIGN KEY (id_contact) REFERENCES contacts (id) ON DELETE CASCADE ON UPDATE CASCADE ); -CREATE TABLE scheduleds_groups +CREATE TABLE IF NOT EXISTS scheduleds_groups ( id INT NOT NULL AUTO_INCREMENT, id_scheduled INT NOT NULL, @@ -79,7 +79,7 @@ CREATE TABLE scheduleds_groups FOREIGN KEY (id_group) REFERENCES groups (id) ON DELETE CASCADE ON UPDATE CASCADE ); -CREATE TABLE scheduleds_numbers +CREATE TABLE IF NOT EXISTS scheduleds_numbers ( id INT NOT NULL AUTO_INCREMENT, id_scheduled INT NOT NULL, @@ -88,7 +88,7 @@ CREATE TABLE scheduleds_numbers FOREIGN KEY (id_scheduled) REFERENCES scheduleds (id) ON DELETE CASCADE ON UPDATE CASCADE ); -CREATE TABLE commands +CREATE TABLE IF NOT EXISTS commands ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(25) NOT NULL, @@ -98,7 +98,7 @@ CREATE TABLE commands UNIQUE (name) ); -CREATE TABLE events +CREATE TABLE IF NOT EXISTS events ( id INT NOT NULL AUTO_INCREMENT, type VARCHAR(25) NOT NULL, @@ -107,13 +107,22 @@ CREATE TABLE events PRIMARY KEY (id) ); -CREATE TABLE users +CREATE TABLE IF NOT EXISTS users ( id INT NOT NULL AUTO_INCREMENT, email VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, - admin BOOLEAN NOT NULL, + admin BOOLEAN NOT NULL DEFAULT FALSE, + transfer BOOLEAN NOT NULL DEFAULT FALSE, PRIMARY KEY (id), UNIQUE (email) ); +CREATE TABLE IF NOT EXISTS transfers +( + id INT NOT NULL AUTO_INCREMENT, + id_received INT NOT NULL, + progress BOOLEAN NOT NULL DEFAULT 0, + PRIMARY KEY (id), + FOREIGN KEY (id_received) REFERENCES receiveds (id) ON DELETE CASCADE ON UPDATE CASCADE +); diff --git a/model/DataBase.php b/model/DataBase.php index 5557006..d97b91b 100755 --- a/model/DataBase.php +++ b/model/DataBase.php @@ -139,6 +139,30 @@ return $this->runQuery($query, $params); } + /** + * Récupère les receiveds dont l'id fait partie de la liste fournie + * @param array $receiveds_ids = Tableau des id des receiveds voulus + * @return array : Retourne un tableau avec les receiveds adaptés + */ + public function getReceivedsIn($receiveds_ids) + { + $query = " + SELECT * + FROM receiveds + WHERE id "; + + //On génère la clause IN et les paramètres adaptés depuis le tableau des id + $generted_in = $this->generateInFromArray($receiveds_ids); + $query .= $generted_in['QUERY']; + $params = $generted_in['PARAMS']; + + return $this->runQuery($query, $params); + } + + /***********************************/ + /* PARTIE DES REQUETES DISCUSSIONS */ + /***********************************/ + /** * Récupère les SMS reçus groupé par numéro et trié par date * @return array : Le tablea avec les sms et la date @@ -712,4 +736,28 @@ return $this->runQuery($query, $params, self::ROWCOUNT); } + /*********************************/ + /* PARTIE DES REQUETES TRANSFERS */ + /*********************************/ + + /** + * Change le statut des tranfers dont l'id est fourni dans $transfers_id + * @param array $transfers_ids = Tableau des id des transfers voulus + * @return int : Retourne le nombre de lignes mises à jour + */ + public function updateProgressTransfersIn($transfers_ids, $progress) + { + $query = " + UPDATE transfers + SET progress = :progress + WHERE id "; + + //On génère la clause IN et les paramètres adaptés depuis le tableau des id + $generted_in = $this->generateInFromArray($transfers_ids); + $query .= $generted_in['QUERY']; + $params = $generted_in['PARAMS']; + $params['progress'] = (boolean)$progress; + + return $this->runQuery($query, $params, self::ROWCOUNT); + } } diff --git a/templates/profile/default.php b/templates/profile/default.php index aae5483..6e6018d 100755 --- a/templates/profile/default.php +++ b/templates/profile/default.php @@ -64,6 +64,23 @@ +