-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdelete_endpoint.php
More file actions
120 lines (98 loc) · 3.64 KB
/
delete_endpoint.php
File metadata and controls
120 lines (98 loc) · 3.64 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
<?php
function eliminarScriptYCarpetaSiVacia($scheme, $script) {
$basePath = __DIR__ . '/endpoints';
$folderPath = $basePath . '/' . $scheme;
$scriptPath = $folderPath . '/' . $script;
// Resultado
$resultado = [
'archivo_eliminado' => false,
'carpeta_eliminada' => false,
'mensajes' => []
];
// Eliminar el archivo
if (file_exists($scriptPath)) {
if (unlink($scriptPath)) {
$resultado['archivo_eliminado'] = true;
$resultado['mensajes'][] = "Archivo eliminado: $scriptPath";
} else {
$resultado['mensajes'][] = "Error al eliminar el archivo: $scriptPath";
}
} else {
$resultado['mensajes'][] = "El archivo no existe: $scriptPath";
}
// Verificar y eliminar carpeta si está vacía
if (is_dir($folderPath)) {
$files = array_diff(scandir($folderPath), ['.', '..']);
if (empty($files)) {
if (rmdir($folderPath)) {
$resultado['carpeta_eliminada'] = true;
$resultado['mensajes'][] = "Esquema eliminado: $folderPath";
} else {
$resultado['mensajes'][] = "Error al eliminar el esquema: $folderPath";
}
} else {
$resultado['mensajes'][] = "El esquema '$folderPath' tiene mas elementos.";
}
} else {
$resultado['mensajes'][] = "El esquema no existe: $folderPath";
}
return $resultado;
}
include './session_time_controller.php';
if (!isset($_SESSION['api_creator_user_session']) && $_SESSION['api_creator_user_session'] != "OK") {
echo json_encode(["status" => "error", "message" => "Sesión expirada."]);
exit();
}
// Creamos la Base de datos de API Creator, si no existe, y controlamos login
require_once "./connection.php";
$pdoApiCreator = getDatabaseConnection($databaseFile);
// Leer ID del POST
$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
if ($id <= 0) {
echo json_encode(["status" => "error", "message" => "ID inválido."]);
exit;
}
// Consultamos los datos para eliminar el script
$registro = [];
try {
// Preparar la consulta
$stmt = $pdoApiCreator->prepare("SELECT * FROM endpoints WHERE id = :id");
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
// Obtener el resultado
$registro = $stmt->fetch(PDO::FETCH_ASSOC);
closeStmt($stmt);
if (!$registro) {
echo json_encode(["status" => "error", "message" => "ID inválido."]);
exit;
}
} catch (PDOException $e) {
closeStmt($stmt);
echo json_encode(["status" => "error", "message" => "Error al recuperar el registro: " . $e->getMessage()]);
exit;
}
$msgFileDelete = "";
if ($registro) {
$scheme = $registro['scheme'];
$script = $registro['script'];
$resultado = eliminarScriptYCarpetaSiVacia($scheme, $script);
if ($resultado['archivo_eliminado'] || $resultado['carpeta_eliminada']) {
$msgFileDelete = "Script eliminado correctamente.";
} else {
$msgFileDelete = "El Script no se ha podido eliminar.";
}
// Ejecutar DELETE
try {
$stmt = $pdoApiCreator->prepare("DELETE FROM endpoints WHERE id = :id");
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
} catch (PDOException $e) {
closeStmt($stmt);
closeConnection($pdoApiCreator);
echo json_encode(["status" => "error", "message" => "$msgFileDelete Error al eliminar el endpoint: " . $e->getMessage()]);
}
closeStmt($stmt);
closeConnection($pdoApiCreator);
echo json_encode(["status" => "success", "message" => "$msgFileDelete Endpoint eliminado correctamente."]);
}
?>