From ddc9f9afe4e2f26d11c5ff82997b0d5c8c315d2a Mon Sep 17 00:00:00 2001 From: marco <marco@unistra.fr> Date: Fri, 21 Mar 2025 10:00:51 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Create=20login=20&=20traduction=20s?= =?UTF-8?q?ystem?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin.php | 20 ++++++++++++++++++++ assets/js/getRecipe.js | 2 +- assets/locales/en.php | 23 +++++++++++++++++++--- assets/locales/fr.php | 7 +++++++ assets/locales/trad.php | 7 ++++++- assets/php/Database.php | 5 +++-- assets/php/Security.php | 41 ++++++++++++++++++++++++++++++++++++++++ assets/php/db.sqlite | Bin 12288 -> 16384 bytes assets/php/login.php | 11 +++++++++++ assets/php/navbar.php | 34 ++++++++++++++++++++++++++++----- assets/php/session.php | 5 +++++ assets/php/signIn.php | 20 ++++++++++++++++++++ index.php | 2 ++ lang.php | 13 +++++++++++++ login.php | 31 ++++++++++++++++++++++++++++++ logout.php | 6 ++++++ 16 files changed, 215 insertions(+), 12 deletions(-) create mode 100644 admin.php create mode 100644 assets/php/Security.php create mode 100644 assets/php/login.php create mode 100644 assets/php/session.php create mode 100644 assets/php/signIn.php create mode 100644 lang.php create mode 100644 login.php create mode 100644 logout.php diff --git a/admin.php b/admin.php new file mode 100644 index 0000000..d863b00 --- /dev/null +++ b/admin.php @@ -0,0 +1,20 @@ +<?php +require_once 'assets/php/session.php'; + +if (!$isLogged) { + header('Location: /login.php'); + die(); +} +?> + +<!doctype html> +<html lang="fr"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>Document</title> +</head> +<body> +<h1>Page d'admin</h1> +</body> +</html> diff --git a/assets/js/getRecipe.js b/assets/js/getRecipe.js index 1512a8a..6212f63 100644 --- a/assets/js/getRecipe.js +++ b/assets/js/getRecipe.js @@ -30,4 +30,4 @@ const loadData = async () => { main.appendChild(p); }; -loadData(); \ No newline at end of file +loadData(); diff --git a/assets/locales/en.php b/assets/locales/en.php index cafeda5..d6d93bd 100644 --- a/assets/locales/en.php +++ b/assets/locales/en.php @@ -1,10 +1,27 @@ <?php $t = [ + 'create' => [ + 'form' => [ + 'thumbnail' => 'Thumbnail', + 'title' => 'Title', + 'description' => 'Description', + ], + ], + 'core' => [ + 'save' => 'Save', + ], + 'login' => [ + 'email' => 'Email', + 'login' => 'Login', + 'passphrase' => 'Passphrase', + ], 'nav' => [ + 'admin' => 'Admin', 'home' => 'Home', - 'create' => 'Create a recipe', + 'create' => 'Create recipe', 'login' => 'Login', - 'about' => 'About' - ] + 'logout' => 'Logout', + 'about' => 'About me' + ], ]; diff --git a/assets/locales/fr.php b/assets/locales/fr.php index db7c5ba..b3e6920 100644 --- a/assets/locales/fr.php +++ b/assets/locales/fr.php @@ -11,10 +11,17 @@ $t = [ 'core' => [ 'save' => 'Enregistrer', ], + 'login' => [ + 'email' => 'Courriel', + 'login' => 'Se connecter', + 'passphrase' => 'Phrase de passe', + ], 'nav' => [ + 'admin' => 'Admin', 'home' => 'Accueil', 'create' => 'Créer sa recette', 'login' => 'Se connecter', + 'logout' => 'Se déconnecter', 'about' => 'À propos' ], ]; diff --git a/assets/locales/trad.php b/assets/locales/trad.php index efe7a04..821c72c 100644 --- a/assets/locales/trad.php +++ b/assets/locales/trad.php @@ -1,3 +1,8 @@ <?php -require_once 'assets/locales/fr.php'; \ No newline at end of file +if (array_key_exists('lang', $_COOKIE) && $_COOKIE['lang'] === 'en') { + require_once 'assets/locales/en.php'; +} else { + require_once 'assets/locales/fr.php'; +} + diff --git a/assets/php/Database.php b/assets/php/Database.php index 7f3669c..6a86425 100644 --- a/assets/php/Database.php +++ b/assets/php/Database.php @@ -1,11 +1,12 @@ <?php -class Database { +class Database +{ protected PDO $db; public function __construct() { - $this->db = new PDO('sqlite:' . __DIR__ . '/db.sqlite'); + $this->db = new PDO('sqlite:' . $_SERVER['DOCUMENT_ROOT'] . '/assets/php/db.sqlite'); $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); } diff --git a/assets/php/Security.php b/assets/php/Security.php new file mode 100644 index 0000000..3dfb481 --- /dev/null +++ b/assets/php/Security.php @@ -0,0 +1,41 @@ +<?php + +require_once 'Database.php'; + +class Security extends Database +{ + public function __construct() + { + parent::__construct(); + + $this->db->exec('CREATE TABLE IF NOT EXISTS user ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + email VARCHAR(255) NOT NULL UNIQUE, + passphrase VARCHAR(255) NOT NULL +)'); + } + + public function signIn(string $email, string $passphrase) + { + $statement = $this->db->prepare("INSERT INTO user ('email', 'passphrase') VALUES (:email, :passphrase)"); + + $statement->bindValue(':email', $email); + $statement->bindValue(':passphrase', $passphrase); + + $statement->execute(); + } + + public function login(string $email, string $passphrase): bool + { + $statement = $this->db->prepare('SELECT id, passphrase FROM user WHERE email=:email'); + $statement->bindValue(':email', $email); + $statement->execute(); + $data = $statement->fetch(); + + if (!$data) return false; + + $_SESSION['id'] = $data['id']; + + return password_verify($passphrase, $data['passphrase']); + } +} \ No newline at end of file diff --git a/assets/php/db.sqlite b/assets/php/db.sqlite index b75635b4ea953daaf6ec0d3e1ce5e5396923694e..9c247a5621f538bd5a5e4a4b547179a2c6417921 100644 GIT binary patch delta 339 zcmZojXlP)ZAT7wnz`(!)#4x}#QO8)Ai$SlflNTt&%xBHOuf=zR&w69wNnXuHDQ0$Y zQBlSw@sh-(oYd0d)FKdOaSn2I3~^Nmadh%=RRD=hj^sCVPR&is%uxt)4085x4AL+% zHPuw`^AAz*3-$5Q;ZgvCg2dwDf{dcX;#BOaxHLE4;L{Q0VB&wp!2gB+)n-A3d;Cfq zOw6K;AWN9I7?~v*i&B#_3sPBl`5!Sb^7k|F_w)B}78GdaFAQX2WzcP8%}p#y&UYxy z%PcM_O4Lg$QZcGjF*H_5Hmh<m%QveGa4}BFi83iL4bwLb@HZ`ROic_avCJ`ZGY-!* kw{UjLa0{-=FZZi7b1ZZ#1%`;6t}MEVhDOFFre@|A00_BS@Bjb+ delta 84 zcmZo@U~EX3AT7woz`(!^#4x}(QO6i4sF%#h3lw7FyTQP(#dl+~pg;!S<{NxEf^3ZZ eUl{nmY!+0w$v<&|AP*NKvm|3tYI0^lDhmK)$Pq9A diff --git a/assets/php/login.php b/assets/php/login.php new file mode 100644 index 0000000..b90c32e --- /dev/null +++ b/assets/php/login.php @@ -0,0 +1,11 @@ +<?php +session_start(); // Cette fonction DOIT TOUJOURS être appelée avant toutes les lignes de HTML + +require_once 'Security.php'; + +$security = new Security(); +$isLogged = $security->login($_POST['email'], $_POST['passphrase']); + +$_SESSION['isLogged'] = 'true'; + +header('Location: /'); diff --git a/assets/php/navbar.php b/assets/php/navbar.php index bbbf571..73fddbe 100644 --- a/assets/php/navbar.php +++ b/assets/php/navbar.php @@ -14,15 +14,39 @@ <?= $t['nav']['create'] ?> </a> </li> - <li> - <a href="#"> - <?= $t['nav']['login'] ?> - </a> - </li> + <?php if ($isLogged) : ?> + <li> + <a href="/logout.php"> + <?= $t['nav']['logout'] ?> + </a> + </li> + <?php else: ?> + <li> + <a href="/login.php"> + <?= $t['nav']['login'] ?> + </a> + </li> + <?php endif; ?> <li> <a href="#"> <?= $t['nav']['about'] ?> </a> </li> + <?php if ($isLogged) : ?> + <li> + <a href="/admin.php"> + <?= $t['nav']['admin'] ?> + </a> + </li> + <?php endif; ?> + + <div> + <menu> + <li> + <a href="/lang.php?lang=fr">🇫🇷</a> + <a href="/lang.php?lang=en">🇬🇧</a> + </li> + </menu> + </div> </menu> </nav> \ No newline at end of file diff --git a/assets/php/session.php b/assets/php/session.php new file mode 100644 index 0000000..5fce6f8 --- /dev/null +++ b/assets/php/session.php @@ -0,0 +1,5 @@ +<?php + +session_start(); + +$isLogged = $_SESSION['isLogged'] === 'true'; \ No newline at end of file diff --git a/assets/php/signIn.php b/assets/php/signIn.php new file mode 100644 index 0000000..0c5a978 --- /dev/null +++ b/assets/php/signIn.php @@ -0,0 +1,20 @@ +<?php + +require_once 'Security.php'; + + +$email = htmlspecialchars($_POST['email']); +$passphrase = password_hash( + $_POST['passphrase'], + PASSWORD_DEFAULT, [ + 'salt' => 'aB9cD3eF5gH7', + 'cost' => 13, + ] +); + + +// verif ici + +$security = new Security(); +$security->signIn($email, $passphrase); + diff --git a/index.php b/index.php index fcce970..ee212a4 100644 --- a/index.php +++ b/index.php @@ -1,4 +1,5 @@ <?php +require_once 'assets/php/session.php'; require_once 'assets/locales/trad.php'; ?> @@ -23,5 +24,6 @@ include_once 'assets/php/navbar.php'; <img src="/assets/img/meat.png" alt="De la viande" aria-hidden="true"/> <img src="/assets/img/logo_main.svg" alt=""/> </header> + </body> </html> \ No newline at end of file diff --git a/lang.php b/lang.php new file mode 100644 index 0000000..166813a --- /dev/null +++ b/lang.php @@ -0,0 +1,13 @@ +<?php + +setcookie( + 'lang', + $_GET['lang'], + time() + 3600 * 24 * 365, + '/', + '', + true, + true, +); + +header('Location: /'); \ No newline at end of file diff --git a/login.php b/login.php new file mode 100644 index 0000000..50f8f4b --- /dev/null +++ b/login.php @@ -0,0 +1,31 @@ +<?php +require_once 'assets/locales/trad.php'; +?> + +<!doctype html> +<html lang="fr"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>Document</title> +</head> +<body> +<form action="/assets/php/login.php" method="post"> + <label> + <?= $t['login']['email'] ?> + + <input type="email" name="email"/> + </label> + + <label> + <?= $t['login']['passphrase'] ?> + + <input type="password" name="passphrase"/> + </label> + + <button> + <?= $t['login']['login'] ?> + </button> +</form> +</body> +</html> \ No newline at end of file diff --git a/logout.php b/logout.php new file mode 100644 index 0000000..c20b65c --- /dev/null +++ b/logout.php @@ -0,0 +1,6 @@ +<?php + +session_start(); +session_destroy(); + +header('Location: /'); \ No newline at end of file -- GitLab