Initial commit: CLEO ERP avec améliorations debug
- Configuration du debug conditionnel pour dev/recette - Fonction debug() globale avec niveaux - Logging des requêtes SQL - Handlers d'exceptions et d'erreurs globaux 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
131
config/init.php
Normal file
131
config/init.php
Normal file
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
$tpsdebut = microtime(true);
|
||||
setlocale(LC_ALL, 'fr_FR');
|
||||
|
||||
define('DS', DIRECTORY_SEPARATOR);
|
||||
define('LIBROOT', dirname(__FILE__));
|
||||
define('ROOT', dirname(LIBROOT));
|
||||
define('CONFROOT', ROOT . DS . 'config');
|
||||
define('CONTROOT', ROOT . DS . 'controllers');
|
||||
define('MODROOT', ROOT . DS . 'models');
|
||||
define('VIEWROOT', ROOT . DS . 'views');
|
||||
define('LAYROOT', VIEWROOT . DS . 'layouts');
|
||||
|
||||
require_once CONFROOT . DS . 'conf.php';
|
||||
$Conf = new Conf();
|
||||
|
||||
define('RESROOT', ROOT . DS . 'pub' . DS . 'res');
|
||||
define('FMKROOT', RESROOT . DS . 'd6');
|
||||
define('BLOCKROOT', FMKROOT . DS . 'blocks');
|
||||
|
||||
//! Chargement de la nouvelle version du d6tools allégée
|
||||
require_once FMKROOT . DS . 'd6_tools.php';
|
||||
|
||||
//! Chargement des fichiers spécifiques au projet
|
||||
require_once FMKROOT . DS . 'lib_cleo.php';
|
||||
|
||||
//! Handler d'exceptions global
|
||||
function exception_handler($exception) {
|
||||
global $Conf;
|
||||
|
||||
$error_data = array(
|
||||
'type' => 'EXCEPTION',
|
||||
'message' => $exception->getMessage(),
|
||||
'file' => $exception->getFile(),
|
||||
'line' => $exception->getLine(),
|
||||
'code' => $exception->getCode(),
|
||||
'trace' => $exception->getTraceAsString()
|
||||
);
|
||||
|
||||
// Logger l'exception
|
||||
if (isset($Conf->_debug_level) && $Conf->_debug_level > 0) {
|
||||
debug($error_data, "UNCAUGHT_EXCEPTION", 1);
|
||||
}
|
||||
|
||||
// Logger dans la table z_logs
|
||||
eLog(0, "Exception non gérée: " . $exception->getMessage() . " dans " . $exception->getFile() . ":" . $exception->getLine());
|
||||
|
||||
// Afficher une erreur propre à l'utilisateur
|
||||
if (isset($Conf->_devIp) && $Conf->_devIp && ini_get('display_errors')) {
|
||||
// En mode dev, afficher les détails
|
||||
echo "<div style='background:#fee; border:2px solid #c00; padding:20px; margin:20px; font-family:monospace;'>";
|
||||
echo "<h2 style='color:#c00;'>Exception non gérée</h2>";
|
||||
echo "<p><strong>Message:</strong> " . htmlspecialchars($exception->getMessage()) . "</p>";
|
||||
echo "<p><strong>Fichier:</strong> " . htmlspecialchars($exception->getFile()) . " ligne " . $exception->getLine() . "</p>";
|
||||
echo "<p><strong>Code:</strong> " . $exception->getCode() . "</p>";
|
||||
echo "<pre style='background:#fff; padding:10px; overflow:auto;'>" . htmlspecialchars($exception->getTraceAsString()) . "</pre>";
|
||||
echo "</div>";
|
||||
} else {
|
||||
// En production, afficher un message générique
|
||||
echo "<div style='text-align:center; padding:50px;'>";
|
||||
echo "<h2>Une erreur est survenue</h2>";
|
||||
echo "<p>Nous nous excusons pour la gêne occasionnée. L'erreur a été enregistrée.</p>";
|
||||
echo "<p><a href='/'>Retour à l'accueil</a></p>";
|
||||
echo "</div>";
|
||||
}
|
||||
}
|
||||
|
||||
//! Handler d'erreurs global
|
||||
function error_handler($errno, $errstr, $errfile, $errline) {
|
||||
global $Conf;
|
||||
|
||||
// Vérifier si l'erreur doit être rapportée selon error_reporting
|
||||
if (!(error_reporting() & $errno)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$error_types = array(
|
||||
E_ERROR => 'ERROR',
|
||||
E_WARNING => 'WARNING',
|
||||
E_PARSE => 'PARSE',
|
||||
E_NOTICE => 'NOTICE',
|
||||
E_CORE_ERROR => 'CORE_ERROR',
|
||||
E_CORE_WARNING => 'CORE_WARNING',
|
||||
E_COMPILE_ERROR => 'COMPILE_ERROR',
|
||||
E_COMPILE_WARNING => 'COMPILE_WARNING',
|
||||
E_USER_ERROR => 'USER_ERROR',
|
||||
E_USER_WARNING => 'USER_WARNING',
|
||||
E_USER_NOTICE => 'USER_NOTICE',
|
||||
E_STRICT => 'STRICT',
|
||||
E_RECOVERABLE_ERROR => 'RECOVERABLE_ERROR',
|
||||
E_DEPRECATED => 'DEPRECATED',
|
||||
E_USER_DEPRECATED => 'USER_DEPRECATED'
|
||||
);
|
||||
|
||||
$error_type = isset($error_types[$errno]) ? $error_types[$errno] : 'UNKNOWN';
|
||||
|
||||
$error_data = array(
|
||||
'type' => $error_type,
|
||||
'errno' => $errno,
|
||||
'message' => $errstr,
|
||||
'file' => $errfile,
|
||||
'line' => $errline
|
||||
);
|
||||
|
||||
// Déterminer le niveau de debug pour ce type d'erreur
|
||||
$debug_level = 4; // Par défaut niveau le plus bas
|
||||
if (in_array($errno, array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR))) {
|
||||
$debug_level = 1; // Erreurs critiques
|
||||
} elseif (in_array($errno, array(E_WARNING, E_CORE_WARNING, E_COMPILE_WARNING, E_USER_WARNING))) {
|
||||
$debug_level = 2; // Warnings
|
||||
} elseif (in_array($errno, array(E_NOTICE, E_USER_NOTICE))) {
|
||||
$debug_level = 3; // Notices
|
||||
}
|
||||
|
||||
// Logger l'erreur
|
||||
if (isset($Conf->_debug_level) && $Conf->_debug_level >= $debug_level) {
|
||||
debug($error_data, "PHP_ERROR", $debug_level);
|
||||
}
|
||||
|
||||
// Pour les erreurs critiques, logger aussi dans z_logs
|
||||
if ($debug_level == 1) {
|
||||
eLog(0, "Erreur PHP $error_type: $errstr dans $errfile:$errline");
|
||||
}
|
||||
|
||||
// Ne pas exécuter le handler d'erreur PHP interne
|
||||
return true;
|
||||
}
|
||||
|
||||
//! Enregistrer les handlers
|
||||
set_exception_handler('exception_handler');
|
||||
set_error_handler('error_handler');
|
||||
Reference in New Issue
Block a user