Files
ms1inscription-v5/php/inc_fx_trace_compte.php
stephan 29c411a23c 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.
2026-07-07 13:32:25 -04:00

121 lines
3.2 KiB
PHP

<?php
/**
* 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;
if ($blnEnabled !== null) {
return $blnEnabled;
}
if (isset($_GET['trace_compte'])) {
$_SESSION['trace_compte'] = ($_GET['trace_compte'] === '1' || $_GET['trace_compte'] === 'on');
}
$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,
'uri' => $_SERVER['REQUEST_URI'] ?? '',
'method' => $_SERVER['REQUEST_METHOD'] ?? '',
'com_id' => $_SESSION['com_id'] ?? null,
'usa_id' => $_SESSION['usa_id'] ?? null,
'redirect_after_login' => $_SESSION['redirect_after_login'] ?? null,
);
if (!empty($arrExtra)) {
$arrCtx = array_merge($arrCtx, $arrExtra);
}
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");
}
function fxTraceCompteRedirect($strStep, $strUrl)
{
fxTraceCompte($strStep, array('redirect_to' => $strUrl));
}