-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
79 lines (69 loc) · 3.95 KB
/
Copy pathindex.php
File metadata and controls
79 lines (69 loc) · 3.95 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
<?php
session_start();
require_once __DIR__ . '/core/helpers.php';
require_once __DIR__ . '/core/database.php';
// Pembuatan token keamanan pertama kali
if (empty($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
// Validasi Keamanan CSRF Otomatis untuk setiap Request POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!isset($_POST['csrf_token']) || $_POST['csrf_token'] !== $_SESSION['csrf_token']) {
http_response_code(403);
echo "<div style='font-family:sans-serif; padding:30px; background:#fff5f5; color:#c53030; max-width:500px; margin:50px auto; border-radius:8px; border:1px solid #feb2b2;'>";
echo "<h2 style='margin-top:0;'>Akses Ditolak (CSRF Token Invalid)</h2>";
echo "<p>Formulir Anda tidak valid atau telah kedaluwarsa. Silakan kembali ke halaman sebelumnya, segarkan (refresh) browser Anda, dan coba kirim ulang.</p>";
echo "</div>";
exit;
}
}
try {
$routes = require __DIR__ . '/config/routes.php';
$url = '/' . rtrim($_GET['url'] ?? '', '/');
$file_target = null;
foreach ($routes as $route_path => $file) {
$pattern = preg_replace('/\{[a-zA-Z0-9_]+\}/', '([a-zA-Z0-9_]+)', $route_path);
$pattern = '#^' . $pattern . '$#';
if (preg_match($pattern, $url, $matches)) {
$file_target = __DIR__ . '/' . $file;
if (isset($matches[1])) {
$_GET['id'] = $matches[1];
}
break;
}
}
// Fitur Auto Routing: Jika tidak ada di config/routes.php, cari file otomatis di /pages/
if (!$file_target) {
$url_path = ltrim($url, '/');
if (empty($url_path)) $url_path = 'index';
$auto_file = __DIR__ . '/pages/' . $url_path . '.php';
if (file_exists($auto_file)) {
$file_target = $auto_file;
}
}
if ($file_target && file_exists($file_target)) {
require $file_target;
} else {
http_response_code(404);
echo "<div style='font-family:sans-serif; text-align:center; padding:50px;'><h1>404 - Halaman Tidak Ditemukan</h1><p>File halaman tidak ada di folder pages.</p></div>";
}
} catch (Throwable $e) {
// Sistem Penangkap Error Berbahasa Indonesia
http_response_code(500);
$pesan = $e->getMessage();
// Analisis error secara cerdas untuk memberikan petunjuk solusi
$solusi = "Periksa kembali penulisan sintaks atau logika kode Anda.";
if (strpos($pesan, "no such table") !== false || strpos($pesan, "doesn't exist") !== false) {
$solusi = "Tabel database yang Anda panggil belum dibuat. Silakan akses alamat <strong>domain.com/migrate</strong> untuk membuat tabel secara otomatis.";
} elseif (strpos($pesan, "Call to undefined function") !== false) {
$solusi = "Anda memanggil fungsi bawaan yang tidak terdaftar. Periksa apakah nama fungsinya salah ketik (typo).";
} elseif (strpos($pesan, "Access denied for user") !== false || strpos($pesan, "Connection refused") !== false) {
$solusi = "Gagal terhubung ke database MySQL. Periksa kembali pengaturan username, password, dan nama database Anda di dalam file <strong>config.php</strong>.";
}
echo "<div style='font-family:sans-serif; padding:30px; background:#fffaf0; color:#dd6b20; max-width:650px; margin:50px auto; border-radius:8px; border:1px solid #fbd38d; box-shadow: 0 4px 6px rgba(0,0,0,0.05);'>";
echo "<h2 style='color:#c05621; margin-top:0;'>Ups, Ada Sedikit Masalah pada Kode Anda!</h2>";
echo "<p><strong>Pesan Masalah:</strong> <br><code style='background:#fff5f5; color:#c53030; padding:3px 6px; border-radius:4px; display:inline-block; margin-top:5px;'>$pesan</code></p>";
echo "<div style='background:#fff; padding:15px; border-radius:6px; border-left:4px solid #dd6b20; margin:20px 0;'><strong>💡 Petunjuk Solusi:</strong><br>$solusi</div>";
echo "<small style='color:#a0aec0;'>Lokasi file: " . $e->getFile() . " (Baris ke-" . $e->getLine() . ")</small>";
echo "</div>";
}