-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.php
More file actions
718 lines (641 loc) · 24 KB
/
Copy pathadmin.php
File metadata and controls
718 lines (641 loc) · 24 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
<?php
session_start();
require_once 'config.php';
require_once 'functions.php';
/**
* Clase AdminController - Maneja toda la lógica del panel de administración
*/
class AdminController {
private $messages = [];
public function __construct() {
$this->processRequests();
}
/**
* Procesa todas las peticiones POST y GET
*/
private function processRequests() {
// Procesar logout
if (isset($_GET['logout'])) {
$this->logout();
}
// Procesar peticiones POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$this->handlePostRequests();
}
}
/**
* Maneja las peticiones POST
*/
private function handlePostRequests() {
$redirect = false;
// Login de administrador
if (isset($_POST['admin_login'])) {
$this->processAdminLogin();
return;
}
// Acciones de administrador (requiere autenticación)
if (!is_admin()) {
$this->addError('Acceso denegado.');
return;
}
// Eliminar post
if (isset($_POST['delete_post'])) {
$redirect = $this->deletePost();
}
// Banear IP
if (isset($_POST['ban_ip'])) {
$redirect = $this->banIp();
}
// Desbanear IP
if (isset($_POST['unban_ip'])) {
$redirect = $this->unbanIp();
}
// Eliminar imagen
if (isset($_POST['delete_image'])) {
$redirect = $this->deleteImage();
}
// Desbloquear post
if (isset($_POST['unlock_post'])) {
$redirect = $this->unlockPost();
}
// Fijar post
if (isset($_POST['pin_post'])) {
$redirect = $this->pinPost();
}
// Desfijar post
if (isset($_POST['unpin_post'])) {
$redirect = $this->unpinPost();
}
// Redirigir si es necesario
if ($redirect) {
$this->redirect('admin.php');
}
}
/**
* Procesa el login de administrador
*/
private function processAdminLogin() {
$password = $_POST['password'] ?? '';
if (empty($password)) {
$this->addError('La contraseña es requerida.');
return;
}
if ($password === ADMIN_PASSWORD) {
if (create_admin_session()) {
$this->addSuccess('Sesión de administrador iniciada correctamente.');
} else {
$this->addError('Error al crear la sesión.');
}
} else {
$this->addError('Contraseña incorrecta.');
}
}
/**
* Cierra la sesión de administrador
*/
private function logout() {
unset($_SESSION['admin_token']);
$this->redirect('admin.php');
}
/**
* Elimina un post
*/
private function deletePost() {
$post_id = (int)($_POST['post_id'] ?? 0);
if ($post_id <= 0) {
$this->addError('ID de post inválido.');
return false;
}
if (delete_post($post_id)) {
$this->addSuccess('Post eliminado correctamente.');
return true;
} else {
$this->addError('Error al eliminar el post.');
return false;
}
}
/**
* Banea una IP
*/
private function banIp() {
$ip_address = clean_input($_POST['ip_address'] ?? '');
$reason = clean_input($_POST['reason'] ?? '');
$duration = isset($_POST['duration']) && !empty($_POST['duration']) ? (int)$_POST['duration'] : null;
if (empty($ip_address)) {
$this->addError('La dirección IP es requerida.');
return false;
}
if (!filter_var($ip_address, FILTER_VALIDATE_IP)) {
$this->addError('Dirección IP inválida.');
return false;
}
if (ban_ip($ip_address, $reason, $duration)) {
$this->addSuccess('IP baneada correctamente.');
return true;
} else {
$this->addError('Error al banear la IP.');
return false;
}
}
/**
* Desbanea una IP
*/
private function unbanIp() {
$ban_id = (int)($_POST['ban_id'] ?? 0);
if ($ban_id <= 0) {
$this->addError('ID de ban inválido.');
return false;
}
if (unban_ip($ban_id)) {
$this->addSuccess('IP desbaneada correctamente.');
return true;
} else {
$this->addError('Error al desbanear la IP.');
return false;
}
}
/**
* Elimina una imagen de un post
*/
private function deleteImage() {
$post_id = (int)($_POST['post_id'] ?? 0);
if ($post_id <= 0) {
$this->addError('ID de post inválido.');
return false;
}
$post = get_post($post_id);
if (!$post) {
$this->addError('Post no encontrado.');
return false;
}
if (empty($post['image_filename'])) {
$this->addError('El post no tiene imagen asociada.');
return false;
}
$image_path = UPLOAD_DIR . $post['image_filename'];
if (file_exists($image_path)) {
unlink($image_path);
}
if (update_post_image($post_id, null)) {
$this->addSuccess('Imagen eliminada correctamente.');
return true;
} else {
$this->addError('Error al eliminar la imagen del post.');
return false;
}
}
/**
* Desbloquea un post
*/
private function unlockPost() {
$post_id = (int)($_POST['post_id'] ?? 0);
if ($post_id <= 0) {
$this->addError('ID de post inválido.');
return false;
}
if (unlock_post($post_id)) {
$this->addSuccess('Post desbloqueado correctamente.');
return true;
} else {
$this->addError('Error al desbloquear el post.');
return false;
}
}
/**
* Fija un post
*/
private function pinPost() {
$post_id = (int)($_POST['post_id'] ?? 0);
if ($post_id <= 0) {
$this->addError('ID de post inválido.');
return false;
}
if (pin_post($post_id)) {
$this->addSuccess('Post fijado correctamente.');
return true;
} else {
$this->addError('Error al fijar el post.');
return false;
}
}
/**
* Desfija un post
*/
private function unpinPost() {
$post_id = (int)($_POST['post_id'] ?? 0);
if ($post_id <= 0) {
$this->addError('ID de post inválido.');
return false;
}
if (unpin_post($post_id)) {
$this->addSuccess('Post desfijado correctamente.');
return true;
} else {
$this->addError('Error al desfijar el post.');
return false;
}
}
/**
* Obtiene todos los datos necesarios para el panel
*/
public function getData() {
return [
'posts' => is_admin() ? get_all_posts() : [],
'bans' => is_admin() ? get_active_bans() : [],
'reports' => is_admin() ? get_all_reports() : []
];
}
/**
* Obtiene los mensajes (errores y éxitos)
*/
public function getMessages() {
return $this->messages;
}
/**
* Añade un mensaje de error
*/
private function addError($message) {
$this->messages['error'] = $message;
}
/**
* Añade un mensaje de éxito
*/
private function addSuccess($message) {
$this->messages['success'] = $message;
}
/**
* Redirige a una página
*/
private function redirect($url) {
header("Location: $url");
exit;
}
}
/**
* Clase AdminView - Maneja la presentación del panel
*/
class AdminView {
private $data;
private $messages;
public function __construct($data, $messages) {
$this->data = $data;
$this->messages = $messages;
}
/**
* Renderiza la página completa
*/
public function render() {
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Panel de Administración - SimpleChan</title>
<link rel="stylesheet" href="assets/css/style.css">
<link rel="stylesheet" href="assets/css/themes.css">
<link rel="stylesheet" href="assets/css/admin.css">
<link id="site-favicon" rel="shortcut icon" href="assets/favicon/favicon.ico" type="image/x-icon">
</head>
<body class="admin">
<?php $this->renderHeader(); ?>
<main>
<?php $this->renderMessages(); ?>
<?php if (!is_admin()): ?>
<?php $this->renderLoginForm(); ?>
<?php else: ?>
<?php $this->renderAdminPanel(); ?>
<?php endif; ?>
</main>
<?php $this->renderFooter(); ?>
<script src="assets/js/script.js"></script>
</body>
</html>
<?php
}
/**
* Renderiza el header
*/
private function renderHeader() {
?>
<header>
<h1>Panel de Administración</h1>
<nav>
<a href="index.php">Volver al Tablón</a>
<a href="admin.php">Recargar</a>
<?php if (is_admin()): ?>
<a href="?logout=1">Cerrar Sesión</a>
<?php endif; ?>
</nav>
</header>
<?php
}
/**
* Renderiza los mensajes de error y éxito
*/
private function renderMessages() {
if (isset($this->messages['error'])) {
echo '<div class="error">' . htmlspecialchars($this->messages['error']) . '</div>';
}
if (isset($this->messages['success'])) {
echo '<div class="success">' . htmlspecialchars($this->messages['success']) . '</div>';
}
}
/**
* Renderiza el formulario de login
*/
private function renderLoginForm() {
?>
<h2>Iniciar Sesión</h2>
<section class="admin-login">
<form method="POST">
<div class="form-group">
<label for="password">Contraseña:</label>
<input type="password" id="password" name="password" required>
</div>
<button type="submit" name="admin_login">Iniciar Sesión</button>
</form>
</section>
<?php
}
/**
* Renderiza el panel de administración
*/
private function renderAdminPanel() {
?>
<!-- Navegación entre secciones -->
<nav class="admin-nav">
<ul>
<h2>Herramientas de Moderación</h2>
<li><a href="#ban-ip" onclick="showSection('ban-ip')">Banear IP</a></li>
<li><a href="#bans-activos" onclick="showSection('bans-activos')">Bans Activos</a></li>
<li><a href="#moderar-posts" onclick="showSection('moderar-posts')">Moderar Posts</a></li>
<li><a href="#reportes-usuarios" onclick="showSection('reportes-usuarios')">Reportes de Usuarios</a></li>
</ul>
</nav>
<?php
$this->renderBanIpSection();
$this->renderActiveBansSection();
$this->renderModeratePostsSection();
$this->renderReportsSection();
}
/**
* Renderiza la sección de baneo de IPs
*/
private function renderBanIpSection() {
?>
<section id="ban-ip" class="admin-section">
<h3>Banear IP</h3>
<form method="POST" class="ban-form">
<div class="form-group">
<label for="ip_address">Dirección IP:</label>
<input type="text" id="ip_address" name="ip_address" required placeholder="192.168.1.1">
</div>
<div class="form-group">
<label for="reason">Razón del ban:</label>
<input type="text" id="reason" name="reason" placeholder="Spam, contenido inapropiado, etc.">
</div>
<div class="form-group">
<label for="duration">Duración (horas, vacío = permanente):</label>
<input type="number" id="duration" name="duration" min="1" placeholder="24">
</div>
<button type="submit" name="ban_ip">Banear IP</button>
</form>
</section>
<?php
}
/**
* Renderiza la sección de bans activos
*/
private function renderActiveBansSection() {
?>
<section id="bans-activos" class="admin-section" style="display:none;">
<h3>Bans Activos</h3>
<?php if (empty($this->data['bans'])): ?>
<p>No hay bans activos.</p>
<?php else: ?>
<table class="admin-table">
<thead>
<tr>
<th>IP</th>
<th>Razón</th>
<th>Fecha</th>
<th>Expira</th>
<th>Acciones</th>
</tr>
</thead>
<tbody>
<?php foreach ($this->data['bans'] as $ban): ?>
<tr>
<td><?php echo htmlspecialchars($ban['ip_address']); ?></td>
<td><?php echo htmlspecialchars($ban['reason']); ?></td>
<td><?php echo date('d/m/Y H:i', strtotime($ban['created_at'])); ?></td>
<td>
<?php
if ($ban['expires_at']) {
echo date('d/m/Y H:i', strtotime($ban['expires_at']));
} else {
echo 'Permanente';
}
?>
</td>
<td>
<form method="POST" style="display: inline;">
<input type="hidden" name="ban_id" value="<?php echo $ban['id']; ?>">
<button type="submit" name="unban_ip" onclick="return confirm('¿Desbanear esta IP?')">Desbanear</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</section>
<?php
}
/**
* Renderiza la sección de moderación de posts
*/
private function renderModeratePostsSection() {
?>
<section id="moderar-posts" class="admin-section" style="display:none;">
<h3>Moderar Posts</h3>
<?php if (empty($this->data['posts'])): ?>
<p>No hay posts.</p>
<?php else: ?>
<div class="posts-admin">
<?php foreach ($this->data['posts'] as $post): ?>
<?php $this->renderPostForModeration($post); ?>
<?php endforeach; ?>
</div>
<?php endif; ?>
</section>
<?php
}
/**
* Renderiza un post individual para moderación
*/
private function renderPostForModeration($post) {
$board = isset($post['board_id']) ? get_board_by_id($post['board_id']) : null;
?>
<div class="post-admin <?php echo $post['is_deleted'] ? 'deleted' : ''; ?>">
<div class="post-header">
<span><b>Tablón:</b> <?php echo htmlspecialchars($board['name'] ?? 'N/A'); ?> -</span>
<span><b>ID:</b> <?php echo $post['id']; ?> -</span>
<?php if ($post['parent_id']): ?>
(Respuesta a: <?php echo $post['parent_id']; ?>)
<?php endif; ?>
<?php if ($post['name'] === 'Administrador'): ?>
<span class="admin-name">Administrador</span>
<?php else: ?>
<?php echo htmlspecialchars($post['name']); ?>
<?php endif; ?>
<span><b>Fecha/Hora:</b> <?php echo date('d/m/Y H:i:s', strtotime($post['created_at'])); ?> -</span>
<span><b>IP:</b> <?php echo htmlspecialchars($post['ip_address']); ?></span>
<?php if ($post['is_deleted']): ?>
<span class="deleted-label">[ELIMINADO]</span>
<?php endif; ?>
</div>
<?php if (!empty($post['subject'])): ?>
<span><b>Asunto:</b> <?php echo htmlspecialchars($post['subject']); ?></span>
<?php endif; ?>
<?php $this->renderPostImage($post); ?>
<div class="post-message">
<?php
// Determinar el post padre para las referencias
$parent_post_id = $post['parent_id'] ? $post['parent_id'] : $post['id'];
echo parse_references($post['message'], true, $parent_post_id);
?>
</div>
<?php $this->renderPostActions($post); ?>
</div>
<?php
}
/**
* Renderiza la imagen de un post
*/
private function renderPostImage($post) {
if (!$post['image_filename']) return;
?>
<div class="post-image">
<?php if (file_exists(UPLOAD_DIR . $post['image_filename'])): ?>
<img src="<?php echo UPLOAD_DIR . $post['image_filename']; ?>" title="<?php echo htmlspecialchars($post['image_filename']); ?>">
<?php else: ?>
<img src="assets/imgs/filedeleted.png" alt="Imagen no disponible">
<?php endif; ?>
</div>
<?php
}
/**
* Renderiza las acciones disponibles para un post
*/
private function renderPostActions($post) {
?>
<div class="post-actions">
<?php if (!$post['is_deleted']): ?>
<form method="POST" style="display: inline;">
<input type="hidden" name="post_id" value="<?php echo $post['id']; ?>">
<button type="submit" name="delete_post" onclick="return confirm('¿Eliminar este post?')">Eliminar Post</button>
</form>
<?php endif; ?>
<?php if ($post['is_locked']): ?>
<form method="POST" style="display: inline;">
<input type="hidden" name="post_id" value="<?php echo $post['id']; ?>">
<button type="submit" name="unlock_post">Desbloquear</button>
</form>
<?php endif; ?>
<?php if ($post['is_pinned']): ?>
<form method="POST" style="display: inline;">
<input type="hidden" name="post_id" value="<?php echo $post['id']; ?>">
<button type="submit" name="unpin_post">Desfijar</button>
</form>
<?php else: ?>
<form method="POST" style="display: inline;">
<input type="hidden" name="post_id" value="<?php echo $post['id']; ?>">
<button type="submit" name="pin_post">Fijar</button>
</form>
<?php endif; ?>
<button type="button" class="btn-ban-ip" onclick="setBanIp('<?php echo htmlspecialchars($post['ip_address']); ?>')">Banear IP</button>
<?php if (!empty($post['image_filename']) && file_exists(UPLOAD_DIR . $post['image_filename'])): ?>
<form method="POST" style="display: inline;">
<input type="hidden" name="post_id" value="<?php echo $post['id']; ?>">
<button type="submit" name="delete_image" onclick="return confirm('¿Eliminar la imagen de este post?')">Eliminar Imagen</button>
</form>
<?php endif; ?>
</div>
<?php
}
/**
* Renderiza la sección de reportes
*/
private function renderReportsSection() {
?>
<section id="reportes-usuarios" class="admin-section" style="display:none;">
<h3>Reportes de Usuarios</h3>
<?php if (empty($this->data['reports'])): ?>
<p>No hay reportes.</p>
<?php else: ?>
<table class="admin-table">
<thead>
<tr>
<th>ID</th>
<th>Post</th>
<th>Motivo</th>
<th>Detalles</th>
<th>Reportado por IP</th>
<th>Fecha</th>
<th>Acciones</th>
</tr>
</thead>
<tbody>
<?php foreach ($this->data['reports'] as $report): ?>
<tr>
<td><?php echo $report['id']; ?></td>
<td>
<a href="index.php#post-<?php echo $report['post_id']; ?>" target="_blank">No. <?php echo $report['post_id']; ?></a>
<?php if (!empty($report['name'])): ?>
<br><small><?php echo htmlspecialchars($report['name']); ?></small>
<?php endif; ?>
<?php if (!empty($report['subject'])): ?>
<br><small><?php echo htmlspecialchars($report['subject']); ?></small>
<?php endif; ?>
<?php if (!empty($report['post_id'])): ?>
<br><small style="color:#888;">IP publicador: <?php echo htmlspecialchars(get_post($report['post_id'])['ip_address'] ?? ''); ?></small>
<?php endif; ?>
</td>
<td><?php echo htmlspecialchars($report['reason']); ?></td>
<td><?php echo htmlspecialchars($report['details']); ?></td>
<td><?php echo htmlspecialchars($report['reporter_ip']); ?></td>
<td><?php echo date('d/m/Y H:i:s', strtotime($report['created_at'])); ?></td>
<td>
<form method="POST" action="admin_actions.php">
<input type="hidden" name="report_id" value="<?php echo $report['id']; ?>">
<button type="submit" name="delete_report" class="btn-delete">Eliminar</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</section>
<?php
}
/**
* Renderiza el footer
*/
private function renderFooter() {
?>
<footer>
<p>© 2025 SimpleChan - Panel de Administración</p>
</footer>
<?php
}
}
// Inicializar la aplicación
$controller = new AdminController();
$data = $controller->getData();
$messages = $controller->getMessages();
$view = new AdminView($data, $messages);
$view->render();
?>