36 lines
1.3 KiB
PHP
36 lines
1.3 KiB
PHP
<?php
|
|
// OPUS5 count_pattern - autonomous grep endpoint for WEVIA
|
|
header('Content-Type: application/json');
|
|
$pattern = $_GET['p'] ?? $_POST['pattern'] ?? '';
|
|
$dir = $_GET['d'] ?? '/var/www/html,/opt/wevia-brain';
|
|
$ext = $_GET['ext'] ?? 'html,php,json';
|
|
|
|
if (empty($pattern) || !preg_match('/^[a-zA-Z0-9_\-\.\/]{1,40}$/', $pattern)) {
|
|
echo json_encode(['error'=>'pattern required, alphanum+_-./max40']);
|
|
exit;
|
|
}
|
|
|
|
$dirs = explode(',', $dir);
|
|
$exts = explode(',', $ext);
|
|
$results = ['pattern'=>$pattern, 'matches'=>[], 'total_files'=>0, 'total_occurrences'=>0];
|
|
|
|
foreach ($dirs as $d) {
|
|
$d = trim($d);
|
|
if (!is_dir($d)) continue;
|
|
foreach ($exts as $e) {
|
|
$cmd = "timeout 5 grep -rc " . escapeshellarg($pattern) . " " . escapeshellarg($d) . " --include='*." . escapeshellarg(trim($e)) . "' 2>/dev/null";
|
|
$out = @shell_exec($cmd);
|
|
if (!$out) continue;
|
|
foreach (explode("\n", trim($out)) as $line) {
|
|
if (!trim($line)) continue;
|
|
if (!preg_match('/^(.+):(\d+)$/', $line, $m)) continue;
|
|
$count = (int)$m[2];
|
|
if ($count === 0) continue;
|
|
$results['matches'][] = ['file' => $m[1], 'count' => $count];
|
|
$results['total_files']++;
|
|
$results['total_occurrences'] += $count;
|
|
}
|
|
}
|
|
}
|
|
echo json_encode($results, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
|