From 1cf5c02c29ec35cf91ca2d6b07cee81c9685fa8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20MARQUET?= Date: Wed, 1 Oct 2025 22:42:50 +0200 Subject: [PATCH] feat(database): Refactor database connection to use environment variables --- docker-compose.yml | 6 +++ index.php | 16 ++++---- src/appli/cntrlApp.php | 80 +++++++++++++++++++++++++++++++++++----- src/appli/cntrlLogin.php | 8 +++- src/appli/utils.php | 32 ++++++++-------- src/metier/User.php | 8 +++- 6 files changed, 115 insertions(+), 35 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 467e91d..589bcf0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,6 +3,12 @@ services: image: ghcr.io/nirij3m/site-comptage-heure:latest ports: - "80:80" + environment: + DBHOST: db + DBPORT: 5432 + DBNAME: bdehours + DBUSER: postgres + DBPASSWORD: Isen44N volumes: - ./:/var/www/html depends_on: diff --git a/index.php b/index.php index 333313f..55ab3fd 100644 --- a/index.php +++ b/index.php @@ -3,15 +3,21 @@ setlocale(LC_TIME, 'fr_FR.utf8','fra'); $method = $_SERVER["REQUEST_METHOD"]; // Récupération de la méthode (GET/POST) $uri = explode("?", $_SERVER["REQUEST_URI"])[0]; // Récupération du contexte (/...) -/* ini_set('display_errors', 1); ini_set('display_startup_errors', 1); -error_reporting(E_ALL);*/ +error_reporting(E_ALL); require_once "src/appli/cntrlLogin.php"; require_once "src/appli/cntrlApp.php"; require_once "src/appli/utils.php"; -$DaoTimeslot = new DaoTimeslot(DBHOST, DBNAME, PORT, USER, PASS); + +$DaoTimeslot = new DaoTimeslot( + getenv('DBHOST') ?: 'localhost', + getenv('DBNAME') ?: 'bdehours', + getenv('DBPORT') ?: 5432, + getenv('DBUSER') ?: 'postgres', + getenv('DBPASS') ?: 'Isen44N' +); $cntrlLogin = new cntrlLogin(); $cntrlApp = new cntrlApp(); $utils = new Utils(); @@ -35,8 +41,4 @@ elseif($method == "POST"){ if($uri == "/admin/validate") $cntrlApp->getValidateResult(); if($uri == "/admin/refuse") $cntrlApp->getRefuseResult(); if($uri == "/admin/historique") $cntrlApp->getSpecificHistoric(); - - } - - diff --git a/src/appli/cntrlApp.php b/src/appli/cntrlApp.php index 8a13e1e..1a96808 100644 --- a/src/appli/cntrlApp.php +++ b/src/appli/cntrlApp.php @@ -11,7 +11,13 @@ class cntrlApp { if(!isset($utils)){ $utils = new Utils(); } - $DaoTimeslot = new DaoTimeslot(DBHOST, DBNAME, PORT, USER, PASS); + $DaoTimeslot = new DaoTimeslot( + getenv('DBHOST') ?: 'localhost', + getenv('DBNAME') ?: 'bdehours', + getenv('DBPORT') ?: 5432, + getenv('DBUSER') ?: 'postgres', + getenv('DBPASS') ?: 'Isen44N' + ); if(session_status() == PHP_SESSION_NONE){ session_start(); } @@ -30,7 +36,13 @@ class cntrlApp { $utils = new Utils(); } - $DaoTimeslot = new DaoTimeslot(DBHOST, DBNAME, PORT, USER, PASS); + $DaoTimeslot = new DaoTimeslot( + getenv('DBHOST') ?: 'localhost', + getenv('DBNAME') ?: 'bdehours', + getenv('DBPORT') ?: 5432, + getenv('DBUSER') ?: 'postgres', + getenv('DBPASS') ?: 'Isen44N' + ); if(session_status() == PHP_SESSION_NONE){ session_start(); } @@ -47,7 +59,13 @@ class cntrlApp { if(!isset($utils)){ $utils = new Utils(); } - $DaoTimeslot = new DaoTimeslot(DBHOST, DBNAME, PORT, USER, PASS); + $DaoTimeslot = new DaoTimeslot( + getenv('DBHOST') ?: 'localhost', + getenv('DBNAME') ?: 'bdehours', + getenv('DBPORT') ?: 5432, + getenv('DBUSER') ?: 'postgres', + getenv('DBPASS') ?: 'Isen44N' + ); $date = $_POST["date"]; $duration = $_POST["duration"]; $description = $_POST["description"]; @@ -66,8 +84,20 @@ class cntrlApp { if(!isset($utils)){ $utils = new Utils(); } - $DaoTimeslot = new DaoTimeslot(DBHOST, DBNAME, PORT, USER, PASS); - $DaoUser = new DaoUser(DBHOST, DBNAME, PORT, USER, PASS); + $DaoTimeslot = new DaoTimeslot( + getenv('DBHOST') ?: 'localhost', + getenv('DBNAME') ?: 'bdehours', + getenv('DBPORT') ?: 5432, + getenv('DBUSER') ?: 'postgres', + getenv('DBPASS') ?: 'Isen44N' + ); + $DaoUser = new DaoUser( + getenv('DBHOST') ?: 'localhost', + getenv('DBNAME') ?: 'bdehours', + getenv('DBPORT') ?: 5432, + getenv('DBUSER') ?: 'postgres', + getenv('DBPASS') ?: 'Isen44N' + ); if(session_status() == PHP_SESSION_NONE){ session_start(); } @@ -86,8 +116,20 @@ class cntrlApp { if(!isset($utils)){ $utils = new Utils(); } - $DaoTimeslot = new DaoTimeslot(DBHOST, DBNAME, PORT, USER, PASS); - $DaoUser = new DaoUser(DBHOST, DBNAME, PORT, USER, PASS); + $DaoTimeslot = new DaoTimeslot( + getenv('DBHOST') ?: 'localhost', + getenv('DBNAME') ?: 'bdehours', + getenv('DBPORT') ?: 5432, + getenv('DBUSER') ?: 'postgres', + getenv('DBPASS') ?: 'Isen44N' + ); + $DaoUser = new DaoUser( + getenv('DBHOST') ?: 'localhost', + getenv('DBNAME') ?: 'bdehours', + getenv('DBPORT') ?: 5432, + getenv('DBUSER') ?: 'postgres', + getenv('DBPASS') ?: 'Isen44N' + ); if(session_status() == PHP_SESSION_NONE){ session_start(); } @@ -118,7 +160,13 @@ class cntrlApp { if(!isset($utils)){ $utils = new Utils(); } - $DaoTimeslot = new DaoTimeslot(DBHOST, DBNAME, PORT, USER, PASS); + $DaoTimeslot = new DaoTimeslot( + getenv('DBHOST') ?: 'localhost', + getenv('DBNAME') ?: 'bdehours', + getenv('DBPORT') ?: 5432, + getenv('DBUSER') ?: 'postgres', + getenv('DBPASS') ?: 'Isen44N' + ); $idDelete = $_POST["idDelete"]; $DaoTimeslot->deleteTimeslotById($idDelete); @@ -134,7 +182,13 @@ class cntrlApp { session_start(); } $idUser = $_SESSION["user"]->getId(); - $DaoTimeslot = new DaoTimeslot(DBHOST, DBNAME, PORT, USER, PASS); + $DaoTimeslot = new DaoTimeslot( + getenv('DBHOST') ?: 'localhost', + getenv('DBNAME') ?: 'bdehours', + getenv('DBPORT') ?: 5432, + getenv('DBUSER') ?: 'postgres', + getenv('DBPASS') ?: 'Isen44N' + ); $DaoTimeslot->validateTimeslot($idValidate, $idUser); $utils->echoSuccess("Horaire validé"); $this->getAdminPage(); @@ -149,7 +203,13 @@ class cntrlApp { session_start(); } $idUser = $_SESSION["user"]->getId(); - $DaoTimeslot = new DaoTimeslot(DBHOST, DBNAME, PORT, USER, PASS); + $DaoTimeslot = new DaoTimeslot( + getenv('DBHOST') ?: 'localhost', + getenv('DBNAME') ?: 'bdehours', + getenv('DBPORT') ?: 5432, + getenv('DBUSER') ?: 'postgres', + getenv('DBPASS') ?: 'Isen44N' + ); $DaoTimeslot->refuseTimeslot($idRefuse, $idUser); $utils->echoSuccess("Horaire refusé"); $this->getAdminPage(); diff --git a/src/appli/cntrlLogin.php b/src/appli/cntrlLogin.php index fa38ebd..6bccb44 100644 --- a/src/appli/cntrlLogin.php +++ b/src/appli/cntrlLogin.php @@ -12,7 +12,13 @@ class cntrlLogin { $utils = new Utils(); $mail = $_POST['mail']; $password = $_POST['password']; - $daoUser = new DaoUser(DBHOST, DBNAME, PORT, USER, PASS); + $daoUser = new DaoUser( + getenv('DBHOST') ?: 'localhost', + getenv('DBNAME') ?: 'bdehours', + getenv('DBPORT') ?: 5432, + getenv('DBUSER') ?: 'postgres', + getenv('DBPASS') ?: 'Isen44N' + ); $id = $daoUser->connectUser($mail, $password); diff --git a/src/appli/utils.php b/src/appli/utils.php index 1defa19..cbf41da 100644 --- a/src/appli/utils.php +++ b/src/appli/utils.php @@ -2,17 +2,16 @@ const PATH_VIEW = "src/view/"; const PATH_CSS = "ressources/css/"; -const DBHOST = "localhost"; -const DBNAME = "bdehours"; -const PORT = 5432; -const USER = "postgres"; -const PASS = "Isen44N"; +$DBHOST = getenv('DBHOST') ?: 'localhost'; +$DBNAME = getenv('DBNAME') ?: 'bdehours'; +$PORT = getenv('DBPORT') ?: 5432; +$USER = getenv('DBUSER') ?: 'postgres'; +$PASS = getenv('DBPASS') ?: 'Isen44N'; require_once "src/dao/DaoUser.php"; require_once "src/metier/User.php"; require_once "src/dao/DaoSpeciality.php"; - class Utils { public function hash_password(string $password) { @@ -37,7 +36,7 @@ class Utils { Erreur!'. $needle . ' - + '; } @@ -66,8 +65,9 @@ class Utils { } public function constructSession($id){ - $DaoUser = new DaoUser(DBHOST, DBNAME, PORT, USER, PASS); - $DaoSpeciality = new DaoSpeciality(DBHOST, DBNAME, PORT, USER, PASS); + global $DBHOST, $DBNAME, $PORT, $USER, $PASS; + $DaoUser = new DaoUser($DBHOST, $DBNAME, $PORT, $USER, $PASS); + $DaoSpeciality = new DaoSpeciality($DBHOST, $DBNAME, $PORT, $USER, $PASS); $result = $DaoUser->getUserById($id); $user = new User($result['id'], $result['name'], $result['surname'], $result['cycle'], $result['mail'], $result['is_admin']); if(session_status() === PHP_SESSION_NONE){ @@ -83,10 +83,13 @@ class Utils { require PATH_VIEW . "vaccueil.php"; } public function convertHoursToDecimal($time){ - $sep = explode(":", $time); - $hours = $sep[0]; - $minutes = round($sep[1] / 60, 2); - return (float) ($hours + $minutes); + $sep = explode(":", $time); + if (count($sep) !== 2 || !is_numeric($sep[1])) { + throw new InvalidArgumentException("Invalid time format. Expected HH:MM."); + } + $hours = $sep[0]; + $minutes = round($sep[1] / 60, 2); + return (float) ($hours + $minutes); } function convertDecimalToHours($dec) { @@ -106,7 +109,4 @@ class Utils { // return the time formatted HH\hMM return (string) ($hours)."h".($minutes); } - - - }; \ No newline at end of file diff --git a/src/metier/User.php b/src/metier/User.php index 5c27c67..24f760b 100644 --- a/src/metier/User.php +++ b/src/metier/User.php @@ -13,7 +13,13 @@ class User { private bool $isAdmin; public function __construct(int $id, string $name, string $surname, string $cycle, string $mail, bool $isAdmin = NULL){ - $DaoSpeciality = new DaoSpeciality(DBHOST, DBNAME, PORT, USER, PASS); + $DaoSpeciality = new DaoSpeciality( + getenv('DBHOST') ?: 'localhost', + getenv('DBNAME') ?: 'bdehours', + getenv('DBPORT') ?: 5432, + getenv('DBUSER') ?: 'postgres', + getenv('DBPASS') ?: 'Isen44N' + ); $this->id = $id; $this->name = $name; $this->surname = $surname;