-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.php
More file actions
208 lines (187 loc) · 5.27 KB
/
Copy pathbootstrap.php
File metadata and controls
208 lines (187 loc) · 5.27 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
<?php
/**
* Bootstrap File
*
* Initializes the application:
* - Loads environment variables from .env
* - Registers autoloader
* - Loads configuration
* - Starts session
* - Sets up error handling
*/
/**
* Autoloader (must be first to load DotEnv class)
*
* Automatically loads classes using PSR-4 naming convention.
* Maps App\* namespace to app/* directory.
*/
spl_autoload_register(function ($class) {
// Base directory for the namespace prefix
$baseDir = __DIR__ . '/app/';
// Convert namespace to file path
// App\Core\Database => app/Core/Database.php
// App\Models\Employee => app/Models/Employee.php
$file = $baseDir . str_replace('App\\', '', $class) . '.php';
$file = str_replace('\\', DIRECTORY_SEPARATOR, $file);
// If the file exists, require it
if (file_exists($file)) {
require_once $file;
}
});
// Load environment variables from .env file
App\Core\DotEnv::load(__DIR__);
// Load configuration
require_once __DIR__ . '/config/config.php';
// Configure secure session settings for production
if (APP_ENV === 'production') {
ini_set('session.cookie_httponly', 1); // Prevent JavaScript access to session cookie
ini_set('session.cookie_secure', 1); // Only send cookie over HTTPS
ini_set('session.use_strict_mode', 1); // Reject uninitialized session IDs
ini_set('session.cookie_samesite', 'Strict'); // CSRF protection
}
// Start session with custom name
session_name(SESSION_NAME);
session_start();
// Regenerate session ID on login to prevent session fixation
if (!isset($_SESSION['initiated'])) {
session_regenerate_id(true);
$_SESSION['initiated'] = true;
}
/**
* Load Helper Files
*
* Helper files contain global functions that need to be explicitly loaded
*/
require_once __DIR__ . '/app/Helpers/DateHelper.php';
require_once __DIR__ . '/app/Helpers/SecurityHelper.php';
/**
* Error Handler
*
* Custom error handling for better debugging
*/
if (DEBUG_MODE) {
// Development mode - show all errors
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});
} else {
// Production mode - log errors, don't display
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
error_log("Error [$errno]: $errstr in $errfile on line $errline");
return true; // Don't execute PHP's internal error handler
});
}
/**
* Exception Handler
*
* Handle uncaught exceptions gracefully
*/
set_exception_handler(function ($exception) {
error_log("Uncaught exception: " . $exception->getMessage());
if (DEBUG_MODE) {
// Development - show detailed error
echo "<div style='background: #f8d7da; border: 1px solid #f5c6cb; color: #721c24; padding: 20px; margin: 20px; border-radius: 5px;'>";
echo "<h2>An error occurred:</h2>";
echo "<p><strong>Message:</strong> " . $exception->getMessage() . "</p>";
echo "<p><strong>File:</strong> " . $exception->getFile() . " (Line " . $exception->getLine() . ")</p>";
echo "<pre>" . $exception->getTraceAsString() . "</pre>";
echo "</div>";
} else {
// Production - show generic error
echo "<div style='background: #f8d7da; border: 1px solid #f5c6cb; color: #721c24; padding: 20px; margin: 20px; border-radius: 5px;'>";
echo "<h2>An error occurred</h2>";
echo "<p>We're sorry, but something went wrong. Please try again later or contact the system administrator.</p>";
echo "</div>";
}
});
/**
* Helper Functions
*
* Global helper functions available throughout the application
* Wrapped in function_exists() to avoid conflicts with other packages
*/
/**
* Dump and die - for debugging
*
* @param mixed ...$vars Variables to dump
*/
if (!function_exists('dd')) {
function dd(...$vars)
{
echo "<pre style='background: #f8f9fa; border: 2px solid #dee2e6; padding: 15px; margin: 10px; border-radius: 5px;'>";
foreach ($vars as $var) {
var_dump($var);
}
echo "</pre>";
die();
}
}
/**
* Get the root URL of the application
*
* @param string $path Optional path to append
* @return string
*/
if (!function_exists('url')) {
function url($path = '')
{
return APP_URL . '/' . ltrim($path, '/');
}
}
/**
* Get asset URL
*
* @param string $path Path to asset
* @return string
*/
if (!function_exists('asset')) {
function asset($path)
{
return APP_URL . '/assets/' . ltrim($path, '/');
}
}
/**
* Redirect to a URL
*
* @param string $url URL to redirect to
*/
if (!function_exists('redirect')) {
function redirect($url)
{
header("Location: {$url}");
exit;
}
}
/**
* Redirect back to previous page
*/
if (!function_exists('back')) {
function back()
{
redirect($_SERVER['HTTP_REFERER'] ?? 'index.php');
}
}
/**
* Get current page filename
*
* @return string
*/
if (!function_exists('currentPage')) {
function currentPage()
{
return basename($_SERVER['PHP_SELF']);
}
}
/**
* Check if on specific page
*
* @param string $page Page filename
* @return bool
*/
if (!function_exists('isPage')) {
function isPage($page)
{
return currentPage() === $page;
}
}
// Application initialized