-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_requests.php
More file actions
357 lines (300 loc) · 11.3 KB
/
log_requests.php
File metadata and controls
357 lines (300 loc) · 11.3 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/**
* Plugin Name: Log Requests
* Description: Intercepta REST requests y los guarda en logs diarios usando error_log().
* Version: 1.2.4
*/
define('RLM_LOG_DIR', WP_CONTENT_DIR . '/logs');
add_filter('rest_pre_dispatch', function($result, $server, $request) {
// Solo procesar rutas que contengan "/wp-json/wp"
$route = $request->get_route();
if (!str_starts_with($route, '/wp/v2')) {
$log_data = [
'timestamp' => date('Y-m-d H:i:s'),
'msg' => 'Aqui estoy...',
'user_id' => get_current_user_id(),
'ip' => $_SERVER['REMOTE_ADDR'] ?? null,
];
rlm_log_to_file(json_encode($log_data, JSON_UNESCAPED_UNICODE));
return $result;
}
date_default_timezone_set('America/Mexico_City');
$log_data = [
'timestamp' => date('Y-m-d H:i:s'),
'method' => $request->get_method(),
'route' => $request->get_route(),
'params' => $request->get_params(),
'user_id' => get_current_user_id(),
'ip' => $_SERVER['REMOTE_ADDR'] ?? null,
];
rlm_log_to_file(json_encode($log_data, JSON_UNESCAPED_UNICODE));
return $result;
}, 10, 3);
function rlm_log_to_file($message) {
date_default_timezone_set('America/Mexico_City'); // Establece timezone CDMX
if (!file_exists(RLM_LOG_DIR)) {
mkdir(RLM_LOG_DIR, 0755, true);
}
$log_file = RLM_LOG_DIR . '/rest_' . date('Y-m-d') . '.log';
error_log($message . PHP_EOL, 3, $log_file);
}
add_action('admin_menu', function () {
add_menu_page('REST Logs', 'REST Logs', 'manage_options', 'rest-log-viewer', 'rlm_render_admin_page');
});
function rlm_render_admin_page()
{
date_default_timezone_set('America/Mexico_City'); // Establece timezone CDMX
$days = 7;
$log_files = glob(RLM_LOG_DIR . '/rest_*.log');
if (!$log_files) {
echo '<div class="wrap"><h1>REST Logs</h1><p>No se encontraron archivos de log REST.</p></div>';
return;
}
$log_files = array_reverse($log_files);
$selected = $_POST['log'] ?? basename(reset($log_files));
$log_path = RLM_LOG_DIR . '/' . basename($selected);
// Obtener parámetros de búsqueda y filtros
$search_query = $_GET['search'] ?? '';
$status_filter = $_GET['status_filter'] ?? 'all';
$method_filter = $_GET['method_filter'] ?? 'all';
$user_filter = $_GET['user_filter'] ?? 'all';
if (isset($_POST['clean_log'])) {
file_put_contents($log_path, '');
}
if (isset($_POST['delete_old_logs'])) {
$log_files = glob(RLM_LOG_DIR . '/rest_*.log');
foreach ($log_files as $file) {
if (filemtime($file) < strtotime("-7 days")) {
unlink($file);
}
}
}
if (isset($_POST['download_log'])) {
if (file_exists($log_path)) {
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="' . basename($log_path) . '"');
readfile($log_path);
exit;
}
}
// Leer y filtrar líneas
$lines = file_exists($log_path) ? array_reverse(file($log_path)) : [];
$filtered_lines = filter_log_lines($lines, $search_query, $status_filter, $method_filter, $user_filter);
$paged = max(1, intval($_GET['paged'] ?? 1));
$per_page = 20;
$total = count($filtered_lines);
$offset = ($paged - 1) * $per_page;
$current_lines = array_slice($filtered_lines, $offset, $per_page);
?>
<div class="wrap">
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
<div class="max-w-6xl mx-auto p-6 bg-white shadow rounded">
<h1 class="text-2xl font-bold mb-4">REST Logs</h1>
<form method="post" class="space-y-4 mb-4">
<div class="flex flex-wrap items-center space-x-2">
<label class="font-medium">Seleccionar log:</label>
<select name="log" onchange="this.form.submit()" class="border px-3 py-2 rounded">
<?php foreach ($log_files as $file):
$basename = basename($file);
$selected_attr = ($basename === $selected) ? 'selected' : '';
?>
<option value="<?php echo esc_attr($basename); ?>" <?php echo $selected_attr; ?>>
<?php echo esc_html($basename); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="flex flex-wrap items-center space-x-2 mt-2">
<button name="clean_log" class="bg-yellow-500 hover:bg-yellow-600 text-white font-semibold px-4 py-2 rounded">
Limpiar log actual
</button>
<button name="download_log" class="bg-blue-500 hover:bg-blue-600 text-white font-semibold px-4 py-2 rounded">
Descargar log actual
</button>
<button name="delete_old_logs" class="bg-red-500 hover:bg-red-600 text-white font-semibold px-4 py-2 rounded">
Borrar logs antiguos
</button>
</div>
</form>
<!-- Formulario de búsqueda y filtros -->
<form method="get" class="bg-gray-50 p-4 rounded mb-4">
<input type="hidden" name="page" value="rest-log-viewer">
<input type="hidden" name="log" value="<?php echo esc_attr($selected); ?>">
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
<!-- Campo de búsqueda -->
<div class="col-span-full">
<label class="block text-sm font-medium mb-1">Buscar en logs:</label>
<input type="text" name="search" value="<?php echo esc_attr($search_query); ?>"
placeholder="Buscar por URL, IP, mensaje..."
class="w-full border px-3 py-2 rounded focus:ring-2 focus:ring-blue-500">
</div>
<!-- Filtro por estado -->
<div>
<label class="block text-sm font-medium mb-1">Estado:</label>
<select name="status_filter" class="w-full border px-3 py-2 rounded">
<option value="all" <?php selected($status_filter, 'all'); ?>>Todos</option>
<option value="2xx" <?php selected($status_filter, '2xx'); ?>>2xx (Éxito)</option>
<option value="4xx" <?php selected($status_filter, '4xx'); ?>>4xx (Error cliente)</option>
<option value="5xx" <?php selected($status_filter, '5xx'); ?>>5xx (Error servidor)</option>
</select>
</div>
<!-- Filtro por método -->
<div>
<label class="block text-sm font-medium mb-1">Método:</label>
<select name="method_filter" class="w-full border px-3 py-2 rounded">
<option value="all" <?php selected($method_filter, 'all'); ?>>Todos</option>
<option value="GET" <?php selected($method_filter, 'GET'); ?>>GET</option>
<option value="POST" <?php selected($method_filter, 'POST'); ?>>POST</option>
<option value="PUT" <?php selected($method_filter, 'PUT'); ?>>PUT</option>
<option value="DELETE" <?php selected($method_filter, 'DELETE'); ?>>DELETE</option>
</select>
</div>
<!-- Filtro por usuario -->
<div>
<label class="block text-sm font-medium mb-1">Usuario:</label>
<select name="user_filter" class="w-full border px-3 py-2 rounded">
<option value="all" <?php selected($user_filter, 'all'); ?>>Todos</option>
<option value="admin" <?php selected($user_filter, 'admin'); ?>>admin</option>
<option value="editor" <?php selected($user_filter, 'editor'); ?>>editor</option>
<option value="subscriber" <?php selected($user_filter, 'subscriber'); ?>>subscriber</option>
</select>
</div>
<!-- Botones -->
<div class="col-span-full flex flex-wrap gap-2">
<button type="submit" class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded">
Buscar
</button>
<a href="?page=rest-log-viewer&log=<?php echo esc_attr($selected); ?>"
class="bg-gray-500 hover:bg-gray-600 text-white px-4 py-2 rounded">
Limpiar filtros
</a>
</div>
</div>
</form>
<!-- Resultados -->
<div class="mb-4">
<h2 class="text-lg font-semibold">
Archivo actual: <code class="text-sm bg-gray-100 px-2 py-1 rounded"><?php echo esc_html($selected); ?></code>
<?php if ($search_query || $status_filter !== 'all' || $method_filter !== 'all' || $user_filter !== 'all'): ?>
<span class="text-sm text-gray-600 ml-2">
(<?php echo $total; ?> resultados encontrados)
</span>
<?php endif; ?>
</h2>
</div>
<div class="border rounded bg-gray-50 p-4 overflow-auto max-h-[400px] text-sm font-mono">
<?php if (empty($current_lines)): ?>
<div class="text-gray-500 text-center py-4">No se encontraron resultados</div>
<?php else: ?>
<?php foreach ($current_lines as $line): ?>
<div class="mb-1 border-b border-gray-200 pb-1">
<?php echo highlight_search_terms(esc_html($line), $search_query); ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<?php if ($total > 0): ?>
<div class="mt-4 flex space-x-2">
<?php
$total_pages = ceil($total / $per_page);
$base_url = build_search_url($selected, $search_query, $status_filter, $method_filter, $user_filter);
echo '<div class="mt-4 flex flex-wrap gap-2 items-center">';
if ($paged > 1) {
echo '<a href="' . $base_url . '&paged=' . ($paged - 1) . '" class="px-3 py-1 rounded bg-gray-200 text-gray-700">Anterior</a>';
}
$range = 2;
$start = max(1, $paged - $range);
$end = min($total_pages, $paged + $range);
if ($start > 1) {
echo '<a href="' . $base_url . '&paged=1" class="px-3 py-1 rounded bg-gray-200 text-gray-700">1</a>';
if ($start > 2) {
echo '<span class="px-2">...</span>';
}
}
for ($i = $start; $i <= $end; $i++) {
$active = $i === $paged ? 'bg-blue-600 text-white' : 'bg-gray-200 text-gray-700';
echo '<a href="' . $base_url . '&paged=' . $i . '" class="px-3 py-1 rounded ' . $active . '">' . $i . '</a>';
}
if ($end < $total_pages) {
if ($end < $total_pages - 1) {
echo '<span class="px-2">...</span>';
}
echo '<a href="' . $base_url . '&paged=' . $total_pages . '" class="px-3 py-1 rounded bg-gray-200 text-gray-700">' . $total_pages . '</a>';
}
if ($paged < $total_pages) {
echo '<a href="' . $base_url . '&paged=' . ($paged + 1) . '" class="px-3 py-1 rounded bg-gray-200 text-gray-700">Siguiente</a>';
}
echo '</div>';
?>
</div>
<?php endif; ?>
</div>
</div>
<?php
}
function filter_log_lines($lines, $search_query, $status_filter, $method_filter, $user_filter)
{
$filtered = [];
foreach ($lines as $line) {
$line = trim($line);
if (empty($line)) continue;
// Aplicar búsqueda por texto
if (!empty($search_query)) {
if (stripos($line, $search_query) === false) {
continue;
}
}
// Aplicar filtro por estado HTTP
if ($status_filter !== 'all') {
if (!preg_match('/\b(' . $status_filter[0] . '\d{2})\b/', $line)) {
continue;
}
}
// Aplicar filtro por método HTTP
if ($method_filter !== 'all') {
if (stripos($line, $method_filter) === false) {
continue;
}
}
// Aplicar filtro por usuario
if ($user_filter !== 'all') {
if (stripos($line, $user_filter) === false) {
continue;
}
}
$filtered[] = $line;
}
return $filtered;
}
function highlight_search_terms($text, $search_query)
{
if (empty($search_query)) {
return $text;
}
$highlighted = str_ireplace(
$search_query,
'<span class="bg-yellow-200 px-1 rounded">' . $search_query . '</span>',
$text
);
return $highlighted;
}
function build_search_url($log, $search, $status, $method, $user)
{
$params = [
'page' => 'rest-log-viewer',
'log' => $log
];
if (!empty($search)) {
$params['search'] = $search;
}
if ($status !== 'all') {
$params['status_filter'] = $status;
}
if ($method !== 'all') {
$params['method_filter'] = $method;
}
if ($user !== 'all') {
$params['user_filter'] = $user;
}
return '?' . http_build_query($params);
}