-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.php
More file actions
64 lines (51 loc) · 1.84 KB
/
api.php
File metadata and controls
64 lines (51 loc) · 1.84 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* QuickBox Mail Stack Dashboard API Bridge
*/
header('Content-Type: application/json');
// Security Check: Ensure only authorized requests are processed.
// In the QuickBox ecosystem, this bridge is called by the main dashboard.
// We implement a token-based check for management parity and security.
$env_file = dirname(__DIR__) . '/.env';
$config = [];
if (file_exists($env_file)) {
$lines = file($env_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (strpos(ltrim($line), '#') === 0) continue;
$parts = explode('=', $line, 2);
if (count($parts) === 2) {
$config[trim($parts[0])] = trim($parts[1]);
}
}
}
$api_token = $config['API_TOKEN'] ?? null;
$provided_token = $_SERVER['HTTP_X_API_TOKEN'] ?? $_REQUEST['token'] ?? null;
if (empty($api_token) || $provided_token !== $api_token) {
http_response_code(401);
echo json_encode(['error' => 'Unauthorized: Invalid or missing API token.']);
exit;
}
$command = $_REQUEST['command'] ?? '';
$args = $_REQUEST['args'] ?? [];
if (!is_array($args)) {
$args = $args ? explode(' ', $args) : [];
}
$allowed_commands = ['add', 'del', 'list', 'passwd', 'quota', 'dkim'];
if (!in_array($command, $allowed_commands)) {
http_response_code(400);
echo json_encode(['error' => 'Invalid command']);
exit;
}
$cli_path = '/opt/quickbox/mail-stack/manage-mail.sh';
if (!file_exists($cli_path)) {
$cli_path = dirname(__DIR__) . '/manage-mail.sh';
}
$escaped_args = array_map('escapeshellarg', $args);
$full_command = 'sudo ' . escapeshellarg($cli_path) . ' ' . $command . ' ' . implode(' ', $escaped_args) . ' 2>&1';
exec($full_command, $output, $return_var);
echo json_encode([
'success' => ($return_var === 0),
'command' => $command,
'output' => $output,
'return_code' => $return_var
]);