246 lines
6.3 KiB
PHP
246 lines
6.3 KiB
PHP
<?php
|
|
/**
|
|
* Configuration Globale Application
|
|
* Plateforme Mathématiques
|
|
*/
|
|
|
|
// Définir la racine de l'application
|
|
define('APP_ROOT', dirname(__DIR__));
|
|
|
|
// ================================================
|
|
// ENVIRONNEMENT
|
|
// ================================================
|
|
|
|
define('DEBUG_MODE', true); // Passer à false en production
|
|
define('ENVIRONMENT', 'development'); // development | production
|
|
|
|
// ================================================
|
|
// CHEMINS
|
|
// ================================================
|
|
|
|
define('BASE_URL', '/mathematiques');
|
|
define('ASSETS_URL', BASE_URL . '/assets');
|
|
define('UPLOADS_DIR', APP_ROOT . '/uploads');
|
|
define('LOGS_DIR', APP_ROOT . '/logs');
|
|
define('TEMP_DIR', APP_ROOT . '/temp');
|
|
define('BACKUP_DIR', APP_ROOT . '/backup');
|
|
|
|
// ================================================
|
|
// SESSIONS
|
|
// ================================================
|
|
|
|
define('SESSION_NAME', 'MATH_SESSION');
|
|
define('SESSION_LIFETIME', 3600 * 4); // 4 heures
|
|
define('SESSION_COOKIE_SECURE', false); // true en HTTPS
|
|
define('SESSION_COOKIE_HTTPONLY', true);
|
|
define('SESSION_COOKIE_SAMESITE', 'Lax');
|
|
|
|
// ================================================
|
|
// SÉCURITÉ
|
|
// ================================================
|
|
|
|
define('PASSWORD_MIN_LENGTH', 8);
|
|
define('MAX_LOGIN_ATTEMPTS', 5);
|
|
define('LOCKOUT_TIME', 900); // 15 minutes en secondes
|
|
define('CSRF_TOKEN_NAME', 'csrf_token');
|
|
define('CSRF_TOKEN_LIFETIME', 3600); // 1 heure
|
|
|
|
// ================================================
|
|
// UPLOADS
|
|
// ================================================
|
|
|
|
define('MAX_FILE_SIZE', 50 * 1024 * 1024); // 50 MB
|
|
define('ALLOWED_UPLOAD_TYPES', [
|
|
'json' => 'application/json',
|
|
'csv' => 'text/csv',
|
|
'pdf' => 'application/pdf',
|
|
'jpg' => 'image/jpeg',
|
|
'jpeg' => 'image/jpeg',
|
|
'png' => 'image/png'
|
|
]);
|
|
|
|
// ================================================
|
|
// PAGINATION
|
|
// ================================================
|
|
|
|
define('ITEMS_PER_PAGE', 20);
|
|
define('MAX_PAGINATION_LINKS', 5);
|
|
|
|
// ================================================
|
|
// LOGS
|
|
// ================================================
|
|
|
|
define('LOG_ERRORS', true);
|
|
define('LOG_QUERIES', DEBUG_MODE);
|
|
define('LOG_FILE_ERRORS', LOGS_DIR . '/errors.log');
|
|
define('LOG_FILE_ACCESS', LOGS_DIR . '/access.log');
|
|
|
|
// ================================================
|
|
// CONFIGURATION PHP
|
|
// ================================================
|
|
|
|
// Fuseau horaire
|
|
date_default_timezone_set('Europe/Paris');
|
|
|
|
// Affichage erreurs selon environnement
|
|
if (DEBUG_MODE) {
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
} else {
|
|
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT);
|
|
ini_set('display_errors', 0);
|
|
ini_set('display_startup_errors', 0);
|
|
}
|
|
|
|
// Logs personnalisés
|
|
ini_set('log_errors', LOG_ERRORS);
|
|
ini_set('error_log', LOG_FILE_ERRORS);
|
|
|
|
// Limites PHP
|
|
ini_set('memory_limit', '256M');
|
|
ini_set('max_execution_time', '300');
|
|
ini_set('upload_max_filesize', '50M');
|
|
ini_set('post_max_size', '50M');
|
|
|
|
// ================================================
|
|
// CONFIGURATION SESSIONS
|
|
// ================================================
|
|
|
|
ini_set('session.name', SESSION_NAME);
|
|
ini_set('session.gc_maxlifetime', SESSION_LIFETIME);
|
|
ini_set('session.cookie_lifetime', SESSION_LIFETIME);
|
|
ini_set('session.cookie_secure', SESSION_COOKIE_SECURE);
|
|
ini_set('session.cookie_httponly', SESSION_COOKIE_HTTPONLY);
|
|
ini_set('session.cookie_samesite', SESSION_COOKIE_SAMESITE);
|
|
ini_set('session.use_strict_mode', 1);
|
|
ini_set('session.use_only_cookies', 1);
|
|
|
|
// ================================================
|
|
// AUTOLOAD SIMPLE
|
|
// ================================================
|
|
|
|
spl_autoload_register(function($class) {
|
|
// Chercher dans includes/classes/
|
|
$file = APP_ROOT . '/includes/classes/' . $class . '.php';
|
|
if (file_exists($file)) {
|
|
require_once $file;
|
|
return true;
|
|
}
|
|
|
|
// Chercher dans src/models/
|
|
$file = APP_ROOT . '/src/models/' . $class . '.php';
|
|
if (file_exists($file)) {
|
|
require_once $file;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
|
|
// ================================================
|
|
// FONCTIONS HELPER GLOBALES
|
|
// ================================================
|
|
|
|
/**
|
|
* Échapper HTML
|
|
*/
|
|
function h($string) {
|
|
return htmlspecialchars($string ?? '', ENT_QUOTES, 'UTF-8');
|
|
}
|
|
|
|
/**
|
|
* Redirection
|
|
*/
|
|
function redirect($url) {
|
|
$base = rtrim(BASE_URL, '/');
|
|
$url = ltrim($url, '/');
|
|
header("Location: {$base}/{$url}");
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Générer URL
|
|
*/
|
|
function url($path = '') {
|
|
return BASE_URL . '/' . ltrim($path, '/');
|
|
}
|
|
|
|
/**
|
|
* Vérifier si utilisateur connecté
|
|
* CORRIGÉ : Utilise SessionManager pour synchronisation
|
|
*/
|
|
function isLoggedIn() {
|
|
// Charger SessionManager si pas déjà fait
|
|
if (!class_exists('SessionManager')) {
|
|
require_once __DIR__ . '/session.php';
|
|
}
|
|
return SessionManager::isLoggedIn();
|
|
}
|
|
|
|
/**
|
|
* Obtenir utilisateur courant
|
|
* CORRIGÉ : Utilise SessionManager pour synchronisation
|
|
*/
|
|
function currentUser() {
|
|
// Charger SessionManager si pas déjà fait
|
|
if (!class_exists('SessionManager')) {
|
|
require_once __DIR__ . '/session.php';
|
|
}
|
|
return SessionManager::getUser();
|
|
}
|
|
|
|
/**
|
|
* Vérifier type utilisateur
|
|
*/
|
|
function isEnseignant() {
|
|
if (!isLoggedIn()) return false;
|
|
$user = currentUser();
|
|
return ($user['id_type'] ?? 0) == 1;
|
|
}
|
|
|
|
function isEleve() {
|
|
if (!isLoggedIn()) return false;
|
|
$user = currentUser();
|
|
return in_array($user['id_type'] ?? 0, [2, 3]);
|
|
}
|
|
|
|
function isEleveClasse() {
|
|
if (!isLoggedIn()) return false;
|
|
$user = currentUser();
|
|
return ($user['id_type'] ?? 0) == 2;
|
|
}
|
|
|
|
function isEleveLibre() {
|
|
if (!isLoggedIn()) return false;
|
|
$user = currentUser();
|
|
return ($user['id_type'] ?? 0) == 3;
|
|
}
|
|
|
|
/**
|
|
* Logger un message
|
|
*/
|
|
function logMessage($message, $level = 'INFO') {
|
|
$timestamp = date('Y-m-d H:i:s');
|
|
$log_entry = "[{$timestamp}] [{$level}] {$message}\n";
|
|
error_log($log_entry, 3, LOG_FILE_ACCESS);
|
|
}
|
|
|
|
/**
|
|
* Formater une date
|
|
*/
|
|
function formatDate($date, $format = 'd/m/Y') {
|
|
if (empty($date)) return '';
|
|
$dt = new DateTime($date);
|
|
return $dt->format($format);
|
|
}
|
|
|
|
/**
|
|
* Formater date et heure
|
|
*/
|
|
function formatDateTime($datetime, $format = 'd/m/Y H:i') {
|
|
if (empty($datetime)) return '';
|
|
$dt = new DateTime($datetime);
|
|
return $dt->format($format);
|
|
}
|