-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.php
More file actions
78 lines (62 loc) · 1.88 KB
/
Copy pathindex.php
File metadata and controls
78 lines (62 loc) · 1.88 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
<?php
$envFile = __DIR__ . '/.env';
if (file_exists($envFile)) {
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (strpos(trim($line), '#') === 0) {
continue;
}
list($key, $value) = explode('=', $line, 2);
$key = trim($key);
$value = trim($value, "\"'");
if (!getenv($key)) {
putenv("{$key}={$value}");
}
}
}
date_default_timezone_set('Asia/Ho_Chi_Minh');
session_start();
// Load Composer's autoloader if it exists
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}
spl_autoload_register(function ($class) {
$paths = [
__DIR__ . '/core/' . $class . '.php',
__DIR__ . '/controllers/' . $class . '.php',
__DIR__ . '/models/' . $class . '.php',
];
foreach ($paths as $path) {
if (file_exists($path)) {
require_once $path;
return;
}
}
});
$config = require __DIR__ . '/config/config.php';
$controller = $_GET['controller'] ?? 'dashboard';
$action = $_GET['action'] ?? 'index';
ImpersonationMiddleware::handle();
$publicRoutes = [
'auth' => ['login', 'forgotPassword'],
];
$isAuthenticated = !empty($_SESSION['user']);
if (!$isAuthenticated) {
if (!isset($publicRoutes[$controller]) || !in_array($action, $publicRoutes[$controller], true)) {
header('Location: ?controller=auth&action=login');
exit;
}
}
$controllerClass = ucfirst($controller) . 'Controller';
if (!class_exists($controllerClass)) {
http_response_code(404);
echo '<h1>404 - Controller not found</h1>';
exit;
}
$controllerInstance = new $controllerClass();
if (!method_exists($controllerInstance, $action)) {
http_response_code(404);
echo '<h1>404 - Action not found</h1>';
exit;
}
$controllerInstance->$action();