[], '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); $R['steps'][] = ['read' => strlen($content).'B']; if (strpos($content, $MARKER) !== false) { $R['ok']=true; $R['steps'][]=['already']=1; die(json_encode($R)); } // GOLD $GOLD = "/opt/wevads/vault/wevia-master-router-ETHICA-{$TS}.gold.php"; copy($SRC, $GOLD); $R['steps'][] = ['gold' => $GOLD, 'size' => filesize($GOLD)]; // Remplacement ciblé et idempotent $old = ' // MASTER-WIRED INTENT: ethica_count if (preg_match(\'/\\b(clients ethica|ethica count|ethica hcp)\\b/iu\', $msg)) { $_out = @shell_exec("timeout 10 cat /etc/hostname 2>&1 | head -c 1500"); return array_merge($base, [\'content\' => "ethica_count (auto-wired):\\n" . trim((string)$_out)]); }'; $new = ' // MASTER-WIRED INTENT: ethica_count OPUS5-ETHICA-COUNT-FIX-v1 if (preg_match(\'/\\b(clients ethica|ethica count|ethica hcp|combien ethica|ethica combien|hcp total count|total hcps|combien de hcp|combien medecins ethica)\\b/iu\', $msg)) { $_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"); $_out = trim((string)$_out); if (is_numeric($_out)) $_out = number_format((int)$_out, 0, \'.\', \',\') . \' HCPs LIVE (ethica.medecins_real)\'; return array_merge($base, [\'content\' => "ethica_count (auto-wired):\\n" . $_out]); }'; $newContent = str_replace($old, $new, $content, $cnt); $R['steps'][] = ['replacements' => $cnt]; if ($cnt === 0) { $R['steps'][]=['err'=>'old block not matched - check indentation']; die(json_encode($R)); } // Lint $TMP = "/tmp/wmr-{$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', $lsa); $R['steps'][] = ['lsattr' => $lsa]; 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('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]; @opcache_invalidate($SRC, true); @opcache_reset(); unlink($TMP); $R['ok'] = ($flr === 0 && $w > 100000); echo json_encode($R, JSON_PRETTY_PRINT);