Files
html/api/opus-fix-regex.php
2026-04-17 02:20:01 +02:00

66 lines
2.6 KiB
PHP

<?php
header('Content-Type: application/json');
$R = ['steps'=>[], 'ok'=>false];
$SRC = '/var/www/html/api/wevia-master-api.php';
$TS = date('Ymd-Hi');
$content = file_get_contents($SRC);
$R['steps'][] = ['read' => strlen($content).'B'];
// GOLD
$GOLD = "/opt/wevads/vault/wevia-master-api-REGEXFIX-{$TS}.gold.php";
copy($SRC, $GOLD);
$R['steps'][] = ['gold' => $GOLD];
// Count avant
$before_ss = substr_count($content, '\\\\s');
$R['steps'][] = ['before_4backslash_s_count' => $before_ss];
// Fix : remplacer les '\\\\s' (4-backslash-s dans le source PHP, qui sont dans une string ''→doublés) par '\\s'
// Précisément dans la zone OPUS4-AUTOWIRE-EARLY-v2
$marker_start = '// === OPUS4-AUTOWIRE-EARLY-v2';
$marker_end = '// === OPUS4-AUTOWIRE-EARLY-v2 END ===';
$ps = strpos($content, $marker_start);
$pe = strpos($content, $marker_end);
if ($ps === false || $pe === false) { $R['steps'][] = ['err'=>'markers not found']; die(json_encode($R)); }
$block = substr($content, $ps, $pe - $ps);
$block_fixed = str_replace('\\\\s', '\\s', $block);
$block_fixed = str_replace('\\\\S', '\\S', $block_fixed);
$fixes_applied = substr_count($block, '\\\\s') - substr_count($block_fixed, '\\\\s');
$R['steps'][] = ['fixes_applied' => $fixes_applied];
$newContent = substr($content, 0, $ps) . $block_fixed . substr($content, $pe);
$R['steps'][] = ['before' => strlen($content), 'after' => strlen($newContent)];
// Lint
$TMP = "/tmp/wma-rx-{$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)); }
// Test isolé de la regex fixée
exec('php8.4 -r "' . addslashes('if(preg_match("/^\\s*master\\s+add\\s+intent/i", "master add intent x :: a|b :: echo hi")) echo "OK"; else echo "KO";') . '" 2>&1', $ro);
$R['steps'][] = ['regex_isolated_test' => $ro];
// 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];
// Final lint
exec("php8.4 -l $SRC 2>&1", $flo, $flr);
$R['steps'][] = ['final_lint_rc' => $flr];
// Opcache flush
@opcache_invalidate($SRC, true);
@file_get_contents('http://127.0.0.1/api/opcache-flush.php');
unlink($TMP);
$R['ok'] = ($flr === 0 && $fixes_applied > 0);
echo json_encode($R, JSON_PRETTY_PRINT);