-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrouter.php
More file actions
121 lines (95 loc) · 4.17 KB
/
router.php
File metadata and controls
121 lines (95 loc) · 4.17 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
<?php
$deactivateSessionControl = "yes";
require_once "classes/EndpointManager.php";
// Obtener la URI completa
$uri = $_SERVER['REQUEST_URI'];
$parsedUri = parse_url($uri);
$path = $parsedUri['path'] ?? '';
$query = $parsedUri['query'] ?? '';
// Ignorar cualquier ruta que no empiece por /apicreator/api
if (!preg_match('#^/apicreator/api(?:/([^?]+))?#', $path, $matches)) {
// Ignorar y salir silenciosamente (Apache servirá el archivo real si existe)
//http_response_code(404);
//echo json_encode(["error" => "Ruta inválida. Debe comenzar con /apicreator/api"]);
//exit;
return;
}
// Capturamos el esquema: todo lo que sigue a /apicreator/api/
$scheme = $matches[1] ?? '';
$scheme = trim($scheme, '/');
// Obtener los endpoints asociados
$manager = new EndpointManager();
$endpoints = $manager->getEndpoints($scheme);
// Eliminar "/apicreator/api" del path para hacer matching con los endpoints
$processedPath = preg_replace('#^/apicreator/api#', '', $path);
$requestUri = $processedPath . ($query !== '' ? "?$query" : '');
// Información de la petición
$method = $_SERVER['REQUEST_METHOD'];
$headers = getallheaders();
$input_mime = $_SERVER["CONTENT_TYPE"] ?? "none";
foreach ($endpoints as $endpoint) {
$endpointUrl = $endpoint['url'];
$parsedEndpoint = parse_url($endpointUrl);
$endpointPath = $parsedEndpoint['path'] ?? '';
$endpointQuery = $parsedEndpoint['query'] ?? '';
// Convertimos {param} en grupos regex
$pathPattern = preg_replace('/\{([a-zA-Z0-9_]+)\}/', '([^/]+)', $endpointPath);
$pathPattern = preg_replace('/([a-zA-Z0-9_]+)\{([a-zA-Z0-9_]+)\}/', '$1([^/]+)', $pathPattern);
$pathPattern = str_replace('/', '\/', $pathPattern);
// Procesar parámetros de query si existen
$queryPattern = '';
if ($endpointQuery !== '') {
$queryParams = explode('&', $endpointQuery);
$queryRegexParts = [];
foreach ($queryParams as $param) {
list($key, $value) = array_pad(explode('=', $param, 2), 2, '');
$value = preg_replace('/\{([a-zA-Z0-9_]+)\}/', '([^&]+)', $value);
$queryRegexParts[] = preg_quote($key, '/') . '=' . $value;
}
$queryPattern = '\\?' . implode('&', $queryRegexParts);
}
$fullPattern = '/^' . $pathPattern . $queryPattern . '$/';
// Comprobamos si hay coincidencia
if (preg_match($fullPattern, $processedPath, $matches)) {
if ($endpoint["method"] !== $method) {
http_response_code(405);
echo json_encode(["error" => "Método no permitido"]);
exit;
}
if ($input_mime !== $endpoint["input_mime"]) {
http_response_code(415);
echo json_encode(["error" => "Formato [$input_mime] no soportado"]);
exit;
}
array_shift($matches); // Eliminar match completo
$_GET["params"] = $matches;
// Ejecutamos el script o devolvemos el valor predefinido(preferencia del valor predefinido)
header("Content-Type: " . $endpoint["output_mime"]);
if(isset($endpoint["return_text"])) {
// TODO: Dependiendo del tipo de salida mime, convertimos el valor predefinido para ajustarlo al mime
// para todos los permitidos
switch ($endpoint["output_mime"]) {
case "application/json":
// Convertir la cadena JSON en un array PHP
$array = json_decode($endpoint["return_text"], true);
// Convertir el array PHP de nuevo en una cadena JSON
$newJsonString = json_encode($array, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
// Mostrar el resultado
echo $newJsonString;
break;
default:
echo $endpoint["return_text"];
break;
}
} elseif(isset($endpoint["script"])) {
include __DIR__ . $endpoint["script"];
}
$manager->incrementHits($endpoint["id"]);
exit;
}
}
// Si no coincide ningún endpoint
http_response_code(404);
echo json_encode(["error" => "Endpoint no encontrado"]);
exit;
?>