Files
html/api/wevia-async-exec.php
2026-04-15 22:26:01 +02:00

33 lines
1.0 KiB
PHP

<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
$dir = '/tmp/wevia-tasks';
if (!is_dir($dir)) mkdir($dir, 0777, true);
if (isset($_GET['poll'])) {
$id = preg_replace('/[^a-z0-9_-]/', '', $_GET['poll']);
$out = "$dir/$id.out";
$flag = "$dir/$id.flag";
if (file_exists($flag)) {
$content = trim(file_get_contents($out));
echo json_encode(['status'=>'done','task_id'=>$id,'content'=>$content]);
} else {
echo json_encode(['status'=>'running','task_id'=>$id]);
}
exit;
}
$jin = json_decode(file_get_contents('php://input'), true);
$cmd = $jin['cmd'] ?? '';
$tool = $jin['tool'] ?? 'async';
if (!$cmd) { echo json_encode(['error'=>'no cmd']); exit; }
$id = 'task_' . bin2hex(random_bytes(5));
$out = "$dir/$id.out";
$flag = "$dir/$id.flag";
$escaped = escapeshellarg($cmd);
exec("nohup bash -c 'timeout 120 bash -c $escaped > $out 2>&1; touch $flag' > /dev/null 2>&1 &");
echo json_encode(['status'=>'launched','task_id'=>$id,'tool'=>$tool]);