Implement enhanced tracing functionality in inc_fx_trace_compte.php and integrate bootstrap call in inc_start_time.php
This commit introduces new functions for logging and host validation in the account tracing system, improving the ability to track user actions in development and pre-production environments. Additionally, it adds a bootstrap function call in inc_start_time.php to initialize tracing early in the request lifecycle. These changes aim to enhance debugging capabilities and ensure reliable logging across different environments.
This commit is contained in:
@ -1,10 +1,34 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Trace diagnostic /compte + login — dev seulement, ?trace_compte=1
|
||||
* Écrit dans logs/compte-trace.log (dernier arrêt = dernier step avant freeze/redirect).
|
||||
* Trace diagnostic /compte + login — préprod/dev, ?trace_compte=1
|
||||
* Fichier : logs/compte-trace.log (à côté de compte.php)
|
||||
* En-tête réponse : X-Compte-Trace (visible dans F12 → Réseau)
|
||||
*/
|
||||
|
||||
function fxTraceCompteLogPath()
|
||||
{
|
||||
if (defined('APPLICATION_PATH')) {
|
||||
return rtrim(APPLICATION_PATH, '/\\') . '/logs/compte-trace.log';
|
||||
}
|
||||
|
||||
return dirname(__DIR__) . '/logs/compte-trace.log';
|
||||
}
|
||||
|
||||
function fxTraceCompteHostAllowed()
|
||||
{
|
||||
global $vblnEnvironementDev, $vblnEnvironementPreProd;
|
||||
|
||||
if (!empty($vblnEnvironementDev) || !empty($vblnEnvironementPreProd)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$strHost = strtolower($_SERVER['HTTP_HOST'] ?? '');
|
||||
|
||||
return (strpos($strHost, 'ms1inscriptiondev') !== false)
|
||||
|| (strpos($strHost, 'progiweb.net') !== false && strpos($strHost, 'preprod') !== false);
|
||||
}
|
||||
|
||||
function fxTraceCompteEnabled()
|
||||
{
|
||||
static $blnEnabled = null;
|
||||
@ -13,28 +37,41 @@ function fxTraceCompteEnabled()
|
||||
return $blnEnabled;
|
||||
}
|
||||
|
||||
global $vblnEnvironementDev, $vblnEnvironementPreProd;
|
||||
|
||||
if (empty($vblnEnvironementDev) && empty($vblnEnvironementPreProd)) {
|
||||
$blnEnabled = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isset($_GET['trace_compte'])) {
|
||||
$_SESSION['trace_compte'] = ($_GET['trace_compte'] === '1' || $_GET['trace_compte'] === 'on');
|
||||
}
|
||||
|
||||
$blnEnabled = !empty($_SESSION['trace_compte']);
|
||||
$blnEnabled = !empty($_SESSION['trace_compte']) && fxTraceCompteHostAllowed();
|
||||
|
||||
return $blnEnabled;
|
||||
}
|
||||
|
||||
function fxTraceCompteWrite($strLine)
|
||||
{
|
||||
$strPath = fxTraceCompteLogPath();
|
||||
$strDir = dirname($strPath);
|
||||
|
||||
if (!is_dir($strDir)) {
|
||||
@mkdir($strDir, 0755, true);
|
||||
}
|
||||
|
||||
$blnOk = @file_put_contents($strPath, $strLine, FILE_APPEND | LOCK_EX);
|
||||
|
||||
if ($blnOk === false) {
|
||||
@error_log('compte-trace: ' . trim($strLine));
|
||||
}
|
||||
}
|
||||
|
||||
function fxTraceCompte($strStep, $arrExtra = array())
|
||||
{
|
||||
if (!fxTraceCompteEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!headers_sent()) {
|
||||
header('X-Compte-Trace: ' . $strStep);
|
||||
}
|
||||
|
||||
$arrCtx = array(
|
||||
't' => microtime(true),
|
||||
'step' => $strStep,
|
||||
@ -49,22 +86,34 @@ function fxTraceCompte($strStep, $arrExtra = array())
|
||||
$arrCtx = array_merge($arrCtx, $arrExtra);
|
||||
}
|
||||
|
||||
$strLine = date('Y-m-d H:i:s') . ' ' . json_encode($arrCtx, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
|
||||
|
||||
$strDir = dirname(__DIR__) . '/logs';
|
||||
if (!is_dir($strDir)) {
|
||||
@mkdir($strDir, 0755, true);
|
||||
}
|
||||
|
||||
@file_put_contents($strDir . '/compte-trace.log', $strLine, FILE_APPEND | LOCK_EX);
|
||||
|
||||
// Secours si le dossier logs n'est pas inscriptible (permissions serveur).
|
||||
if (!is_file($strDir . '/compte-trace.log')) {
|
||||
@error_log('compte-trace: ' . trim($strLine));
|
||||
}
|
||||
fxTraceCompteWrite(date('Y-m-d H:i:s') . ' ' . json_encode($arrCtx, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n");
|
||||
}
|
||||
|
||||
/** Bootstrap très tôt (inc_start_time) — avant inc_settings. */
|
||||
function fxTraceCompteBootstrap()
|
||||
{
|
||||
if (isset($_GET['trace_compte'])) {
|
||||
$_SESSION['trace_compte'] = ($_GET['trace_compte'] === '1' || $_GET['trace_compte'] === 'on');
|
||||
}
|
||||
|
||||
if (empty($_SESSION['trace_compte'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$strHost = $_SERVER['HTTP_HOST'] ?? '';
|
||||
if (strpos($strHost, 'ms1inscriptiondev') === false && strpos($strHost, 'progiweb') === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!headers_sent()) {
|
||||
header('X-Compte-Trace: bootstrap');
|
||||
}
|
||||
|
||||
fxTraceCompteWrite(date('Y-m-d H:i:s') . ' {"step":"bootstrap","uri":"'
|
||||
. str_replace('"', '', $_SERVER['REQUEST_URI'] ?? '') . '","host":"'
|
||||
. str_replace('"', '', $strHost) . '"}' . "\n");
|
||||
}
|
||||
|
||||
/** Appeler juste avant header('Location:…'); exit; */
|
||||
function fxTraceCompteRedirect($strStep, $strUrl)
|
||||
{
|
||||
fxTraceCompte($strStep, array('redirect_to' => $strUrl));
|
||||
|
||||
@ -26,6 +26,9 @@ ini_set('session.cookie_domain', $cookie_domain);
|
||||
// Démarrez la session
|
||||
session_start();
|
||||
|
||||
require_once __DIR__ . '/inc_fx_trace_compte.php';
|
||||
fxTraceCompteBootstrap();
|
||||
|
||||
|
||||
// Temps de la session = 60 min
|
||||
// MSIN-CON-259
|
||||
|
||||
Reference in New Issue
Block a user