feat(database): Refactor database connection to use environment variables

This commit is contained in:
2025-10-01 22:42:50 +02:00
parent ed01893346
commit 1cf5c02c29
6 changed files with 115 additions and 35 deletions

View File

@@ -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:

View File

@@ -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();
}

View File

@@ -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();

View File

@@ -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);

View File

@@ -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 {
<i class="bi-exclamation-octagon-fill"></i>
<strong class="mx-2">Erreur!</strong>'. $needle . '
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
</div>
</div>
';
}
@@ -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);
}
};

View File

@@ -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;