-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-manager.php
More file actions
executable file
·161 lines (131 loc) · 6.05 KB
/
server-manager.php
File metadata and controls
executable file
·161 lines (131 loc) · 6.05 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
require_once 'config/config.php';
// Solo procesar si es una petición AJAX
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || $_SERVER['HTTP_X_REQUESTED_WITH'] !== 'XMLHttpRequest') {
http_response_code(400);
die('Acceso no autorizado');
}
header('Content-Type: application/json');
$action = $_GET['action'] ?? '';
$response = ['success' => false, 'message' => '', 'data' => null];
try {
switch ($action) {
case 'get_servers':
$response['success'] = true;
$response['data'] = Config::getServers();
break;
case 'get_server':
$serverId = $_GET['server_id'] ?? '';
if (empty($serverId)) {
throw new Exception('ID de servidor requerido');
}
$servers = Config::getServers();
if (!isset($servers[$serverId])) {
throw new Exception('Servidor no encontrado');
}
$response['success'] = true;
$response['data'] = $servers[$serverId];
break;
case 'save_server':
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
throw new Exception('Método no permitido');
}
$input = json_decode(file_get_contents('php://input'), true);
if (!$input) {
throw new Exception('Datos inválidos');
}
$serverId = $input['server_id'] ?? '';
$isNew = $input['is_new'] ?? false;
// Validar datos requeridos
$requiredFields = ['name', 'imap_server', 'imap_port', 'smtp_server', 'smtp_port'];
foreach ($requiredFields as $field) {
if (empty($input[$field])) {
throw new Exception("Campo requerido: $field");
}
}
// Si es nuevo, generar ID único
if ($isNew || empty($serverId)) {
$serverId = $input['domain'] ?? strtolower(preg_replace('/[^a-zA-Z0-9]/', '_', $input['name']));
$serverId = preg_replace('/[^a-zA-Z0-9_]/', '', $serverId);
// Asegurar que el ID sea único
$servers = Config::getServers();
$originalId = $serverId;
$counter = 1;
while (isset($servers[$serverId])) {
$serverId = $originalId . '_' . $counter;
$counter++;
}
}
// Validar que no se edite el servidor default
if ($serverId === 'default' && !$isNew) {
throw new Exception('No se puede editar el servidor por defecto');
}
// Preparar configuración del servidor
$serverConfig = [
'name' => trim($input['name']),
'description' => trim($input['description'] ?? ''),
'imap' => [
'server' => trim($input['imap_server']),
'port' => (int)$input['imap_port'],
'ssl' => $input['imap_ssl'] === 'true' || $input['imap_ssl'] === true
],
'smtp' => [
'server' => trim($input['smtp_server']),
'port' => (int)$input['smtp_port'],
'secure' => trim($input['smtp_secure'])
]
];
// Cargar configuración actual
$configFile = __DIR__ . '/data/servers.json';
$config = json_decode(file_get_contents($configFile), true);
// Añadir/actualizar servidor
$config['servers'][$serverId] = $serverConfig;
// Guardar configuración
if (file_put_contents($configFile, json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE))) {
$response['success'] = true;
$response['message'] = $isNew ? 'Servidor añadido correctamente' : 'Servidor actualizado correctamente';
$response['data'] = ['server_id' => $serverId, 'config' => $serverConfig];
} else {
throw new Exception('Error al guardar la configuración');
}
break;
case 'delete_server':
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
throw new Exception('Método no permitido');
}
$input = json_decode(file_get_contents('php://input'), true);
$serverId = $input['server_id'] ?? '';
if (empty($serverId)) {
throw new Exception('ID de servidor requerido');
}
if ($serverId === 'default') {
throw new Exception('No se puede eliminar el servidor por defecto');
}
// Cargar configuración actual
$configFile = __DIR__ . '/data/servers.json';
$config = json_decode(file_get_contents($configFile), true);
if (!isset($config['servers'][$serverId])) {
throw new Exception('Servidor no encontrado');
}
// Eliminar servidor
unset($config['servers'][$serverId]);
// Si era el servidor por defecto, cambiar al 'default'
if ($config['default_server'] === $serverId) {
$config['default_server'] = 'default';
}
// Guardar configuración
if (file_put_contents($configFile, json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE))) {
$response['success'] = true;
$response['message'] = 'Servidor eliminado correctamente';
} else {
throw new Exception('Error al guardar la configuración');
}
break;
default:
throw new Exception('Acción no válida');
}
} catch (Exception $e) {
$response['message'] = $e->getMessage();
}
echo json_encode($response);
?>