Files
html/api/opus-patch-ethica.php

72 lines
2.4 KiB
PHP

<?php
header('Content-Type: application/json');
$R = ['steps'=>[], 'ok'=>false];
$SRC = '/opt/wevia-brain/wevia-master-router.php';
$TS = date('Ymd-Hi');
$MARKER = 'OPUS5_ETHICA_COUNT_FIX_v1';
$content = file_get_contents($SRC);
if (strpos($content, $MARKER) !== false) {
$R['ok']=true; $R['already']=true; die(json_encode($R));
}
$GOLD = "/opt/wevads/vault/wevia-master-router-ETHICA-{$TS}.gold.php";
copy($SRC, $GOLD);
$R['steps'][] = ['gold'=>$GOLD];
// Target simple : dans le bloc ethica_count, remplacer la commande hostname par query DB
// Le bloc ethica_count fait le cat /etc/hostname. J'injecte le marker + change la cmd.
// Approach : find the specific "ethica_count" comment line and the next shell_exec line
$lines = explode("\n", $content);
$patched = false;
$inblock = false;
for ($i = 0; $i < count($lines); $i++) {
if (strpos($lines[$i], 'MASTER-WIRED INTENT: ethica_count') !== false) {
$inblock = true;
$lines[$i] .= ' // ' . $MARKER;
continue;
}
if ($inblock && strpos($lines[$i], 'cat /etc/hostname') !== false) {
$lines[$i] = ' $_out = @shell_exec("timeout 10 env PGPASSWORD=admin123 psql -h 10.1.0.3 -U admin -d adx_system -tAc \"SELECT COUNT(*) FROM ethica.medecins_real\" 2>&1");';
$patched = true;
$inblock = false;
break;
}
if ($inblock && strpos($lines[$i], '}') !== false && strpos($lines[$i], 'shell_exec') === false) {
// end of block without find
$inblock = false;
}
}
$R['steps'][] = ['patched'=>$patched];
if (!$patched) { $R['err']='pattern not found'; die(json_encode($R)); }
$newContent = implode("\n", $lines);
// Lint
$TMP = "/tmp/wmr-s-{$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['err']='LINT FAIL'; die(json_encode($R)); }
// Write
exec('chattr -i '.escapeshellarg($SRC).' 2>&1', $c1, $rc1);
$R['steps'][] = ['chattr_minus'=>$rc1];
$w = @file_put_contents($SRC, $newContent);
$R['steps'][] = ['written'=>$w];
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'=>$flr];
@opcache_invalidate($SRC, true);
@opcache_reset();
unlink($TMP);
$R['ok'] = ($flr === 0 && $w > 100000);
echo json_encode($R, JSON_PRETTY_PRINT);