Files
html/api/opus-add-trace.php
2026-04-17 02:25:01 +02:00

37 lines
1.4 KiB
PHP

<?php
header('Content-Type: application/json');
$R = ['steps'=>[], 'ok'=>false];
$SRC = '/var/www/weval/wevia-ia/weval-chatbot-api.php';
$TS = date('Ymd-Hi');
$content = file_get_contents($SRC);
if (strpos($content, 'OPUS4-TRACE-V1') !== false) {
$R['already'] = true; die(json_encode($R));
}
// Ajoute trace tout au début - juste après <?php
$old = '<?php /* ACTIVE: Fullscreen chatbot backend';
$new = '<?php @file_put_contents("/var/log/weval/opus4-trace.log", date("c")." CHATBOT_OPENED uri=".($_SERVER["REQUEST_URI"]??"?")."\n", FILE_APPEND); /* OPUS4-TRACE-V1 ACTIVE: Fullscreen chatbot backend';
$newContent = str_replace($old, $new, $content, $count);
$R['replacements'] = $count;
if ($count === 0) { $R['err'] = 'anchor not found'; die(json_encode($R)); }
// Lint
$TMP = "/tmp/wc-trace-{$TS}.php";
file_put_contents($TMP, $newContent);
exec("php8.4 -l $TMP 2>&1", $lo, $lr);
if ($lr !== 0) { unlink($TMP); $R['err']='LINT'; $R['lint']=$lo; die(json_encode($R)); }
// GOLD + write
copy($SRC, "/opt/wevads/vault/weval-chatbot-TRACE-{$TS}.gold.php");
exec('chattr -i '.escapeshellarg($SRC).' 2>&1');
$w = file_put_contents($SRC, $newContent);
exec('chattr +i '.escapeshellarg($SRC).' 2>&1');
exec("php8.4 -l $SRC 2>&1", $flo, $flr);
@opcache_invalidate($SRC, true);
@opcache_reset();
unlink($TMP);
$R['ok'] = ($flr === 0); $R['written'] = $w; $R['final_lint'] = $flr;
echo json_encode($R, JSON_PRETTY_PRINT);