77 lines
3.1 KiB
PHP
77 lines
3.1 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);
|
|
$R['steps'][] = ['read' => strlen($content).'B'];
|
|
|
|
// GOLD
|
|
$GOLD = "/opt/wevads/vault/weval-chatbot-api-STREAMFIX-{$TS}.gold.php";
|
|
copy($SRC, $GOLD);
|
|
$R['steps'][] = ['gold' => $GOLD];
|
|
|
|
// Target la ligne dans ma zone OPUS4-AUTOWIRE-CHATBOT-v1
|
|
$start_marker = 'OPUS4-AUTOWIRE-CHATBOT-v1 (17avr 02h35)';
|
|
$end_marker = 'OPUS4-AUTOWIRE-CHATBOT-v1 END';
|
|
$ps = strpos($content, $start_marker);
|
|
$pe = strpos($content, $end_marker);
|
|
if ($ps === false || $pe === false) { $R['steps'][]=['err'=>'markers absent']; die(json_encode($R)); }
|
|
|
|
$block = substr($content, $ps, $pe - $ps);
|
|
|
|
// Remplace la source du raw : $GLOBALS["_wevia_raw"] ?? file_get_contents("php://input")
|
|
// est déjà tentée, mais si les 2 sont vides ($GLOBALS défini MAIS vide), on fallback sur $_POST
|
|
// Meilleur fix : DOUBLE JSON read si premier vide
|
|
$old = '$__opus4_raw = $GLOBALS["_wevia_raw"] ?? file_get_contents("php://input");';
|
|
$new = '$__opus4_raw = $GLOBALS["_wevia_raw"] ?? (isset($_wevia_raw) ? $_wevia_raw : file_get_contents("php://input"));';
|
|
|
|
// Aussi: normaliser via $_POST si JSON vide
|
|
// Et tracer TOUJOURS qu'on est passé par là pour diagnostic
|
|
$trace_inject = '@file_put_contents("/var/log/weval/opus4-autowire.log", date("c") . " CHATBOT_REACHED raw_len=" . strlen($__opus4_raw) . " msg=" . substr($__opus4_msg, 0, 40) . "\n", FILE_APPEND);';
|
|
|
|
$block_new = str_replace($old, $new, $block);
|
|
|
|
// Injecte trace line juste apres la définition de $__opus4_msg
|
|
$trace_anchor = '$__opus4_msg = mb_strtolower(trim($__opus4_in["message"] ?? ($_POST["message"] ?? "")));';
|
|
$trace_new = $trace_anchor . "\n " . $trace_inject;
|
|
$block_new = str_replace($trace_anchor, $trace_new, $block_new);
|
|
|
|
$R['steps'][] = [
|
|
'stream_fix_applied' => ($block !== $block_new),
|
|
'block_before_len' => strlen($block),
|
|
'block_after_len' => strlen($block_new)
|
|
];
|
|
|
|
$newContent = substr($content, 0, $ps) . $block_new . substr($content, $pe);
|
|
|
|
// Lint
|
|
$TMP = "/tmp/wc-stream-{$TS}.php";
|
|
file_put_contents($TMP, $newContent);
|
|
exec("php8.4 -l $TMP 2>&1", $lo, $lr);
|
|
$R['steps'][] = ['lint_rc' => $lr, 'lint' => $lo];
|
|
if ($lr !== 0) { unlink($TMP); $R['steps'][]=['err'=>'LINT FAIL']; die(json_encode($R)); }
|
|
|
|
// Write
|
|
exec('sudo chattr -i '.escapeshellarg($SRC).' 2>&1', $c1, $rc1);
|
|
if ($rc1 !== 0) exec('chattr -i '.escapeshellarg($SRC).' 2>&1', $c1, $rc1);
|
|
$w = file_put_contents($SRC, $newContent);
|
|
exec('sudo chattr +i '.escapeshellarg($SRC).' 2>&1', $c2, $rc2);
|
|
if ($rc2 !== 0) exec('chattr +i '.escapeshellarg($SRC).' 2>&1', $c2, $rc2);
|
|
$R['steps'][] = ['written' => $w, 'chattr_plus' => $rc2];
|
|
|
|
exec("php8.4 -l $SRC 2>&1", $flo, $flr);
|
|
$R['steps'][] = ['final_lint_rc' => $flr];
|
|
|
|
@opcache_invalidate($SRC, true);
|
|
@opcache_reset();
|
|
|
|
// Prepare log dir
|
|
@mkdir('/var/log/weval', 0755, true);
|
|
@file_put_contents('/var/log/weval/opus4-autowire.log', date('c')." FIX_STREAM applied\n", FILE_APPEND);
|
|
|
|
unlink($TMP);
|
|
$R['ok'] = ($flr === 0);
|
|
echo json_encode($R, JSON_PRETTY_PRINT);
|