Files
html/api/opus-inject-handler.php
2026-04-17 02:15:01 +02:00

69 lines
2.5 KiB
PHP

<?php
header('Content-Type: application/json');
$R=['steps'=>[],'ok'=>false];
$SRC='/var/www/html/api/wevia-master-api.php';
$MARKER='OPUS4-AUTOWIRE-INCLUDE-v1';
$TS=date('Ymd-Hi');
$content=file_get_contents($SRC);
$R['steps'][]=['read'=>strlen($content).'B'];
// Idempotence
if (strpos($content,$MARKER)!==false){
$R['ok']=true; $R['steps'][]=['already_applied'=>true];
die(json_encode($R,JSON_PRETTY_PRINT));
}
// GOLD
$GOLD="/opt/wevads/vault/wevia-master-api-{$TS}.gold.php";
if (!is_writable('/opt/wevads/vault')) { $GOLD="/tmp/wevia-master-api-{$TS}.gold.php"; }
copy($SRC,$GOLD);
$R['steps'][]=['gold'=>$GOLD,'size'=>filesize($GOLD)];
// Anchor : require_once '/opt/wevia-brain/wevia-strategic-guard.php';
$anchor="require_once '/opt/wevia-brain/wevia-strategic-guard.php';";
$pos=strpos($content,$anchor);
if ($pos===false){ $R['steps'][]=['err'=>'anchor not found']; die(json_encode($R)); }
$INJECT = "\n// === OPUS4-AUTOWIRE-INCLUDE-v1 (17avr) ===\n".
"// Fix cause racine : Wave 128 autowire syntax tombait en LLM fallback.\n".
"// Handler traite \"master add intent\" + \"master list intents\" AVANT strategic-guard.\n".
"@require_once '/opt/wevia-brain/opus4-autowire-handler.php';\n".
"// === OPUS4-AUTOWIRE-INCLUDE-v1 END ===\n";
$newContent=substr($content,0,$pos).$INJECT.substr($content,$pos);
$R['steps'][]=['inject_at'=>$pos,'before'=>strlen($content),'after'=>strlen($newContent)];
// Lint temp
$TMP="/tmp/wma-test-{$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)); }
// Check chattr
exec('lsattr '.escapeshellarg($SRC).' 2>&1',$lsat);
$R['steps'][]=['lsattr_before'=>$lsat];
exec('sudo chattr -i '.escapeshellarg($SRC).' 2>&1',$c1,$rc1);
if ($rc1!==0) exec('chattr -i '.escapeshellarg($SRC).' 2>&1',$c1,$rc1);
$R['steps'][]=['chattr_minus'=>$rc1,'out'=>$c1];
$w=file_put_contents($SRC,$newContent);
$R['steps'][]=['written'=>$w];
exec('sudo chattr +i '.escapeshellarg($SRC).' 2>&1',$c2,$rc2);
if ($rc2!==0) exec('chattr +i '.escapeshellarg($SRC).' 2>&1',$c2,$rc2);
$R['steps'][]=['chattr_plus'=>$rc2];
exec("php8.4 -l $SRC 2>&1",$flo,$flr);
$R['steps'][]=['final_lint_rc'=>$flr,'final_lint'=>$flo];
$final=file_get_contents($SRC);
$R['steps'][]=['marker_present'=>(strpos($final,$MARKER)!==false)];
unlink($TMP);
$R['ok']=($flr===0 && strpos($final,$MARKER)!==false && $w>10000);
echo json_encode($R,JSON_PRETTY_PRINT);