-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbootstrap.php
More file actions
206 lines (178 loc) 路 7.42 KB
/
Copy pathbootstrap.php
File metadata and controls
206 lines (178 loc) 路 7.42 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
<?php
declare(strict_types=1);
/**
* Ava CMS Bootstrap
*
* Loads composer autoload, configuration, and initializes core services.
* This file is shared by both the web front controller and CLI.
*/
// Prevent direct web access if accidentally exposed
if (php_sapi_name() !== 'cli' && !defined('AVA_START')) {
http_response_code(403);
exit('Direct access denied.');
}
// Ava version (CalVer: YY.M.PATCH - e.g., 26.2.0 = first patch of Feb 2026)
define('AVA_VERSION', '26.8.0');
// Ensure we have a root constant
if (!defined('AVA_ROOT')) {
define('AVA_ROOT', __DIR__);
}
// Composer autoload
$autoloadPath = AVA_ROOT . '/vendor/autoload.php';
if (!file_exists($autoloadPath)) {
$isCli = php_sapi_name() === 'cli';
if ($isCli) {
echo "\n馃憢 Welcome to Ava CMS!\n\n";
echo "Run 'composer install' from your Ava root directory.\n\n";
echo "No SSH? Run it locally and upload the vendor/ folder.\n\n";
echo "More info: https://ava.addy.zone/docs/hosting\n\n";
exit(1);
}
http_response_code(503);
echo <<<'HTML'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ava CMS Setup</title>
<style>
body { font-family: system-ui, sans-serif; max-width: 480px; margin: 4rem auto; padding: 1.5rem; line-height: 1.6; color: #334155; }
h1 { font-size: 1.5rem; margin-bottom: 1rem; }
code { background: #f1f5f9; padding: 0.15rem 0.4rem; border-radius: 0.25rem; font-size: 0.9em; }
a { color: #2563eb; }
.hint { margin-top: 1.5rem; padding-top: 1rem; border-top: 1px solid #e2e8f0; font-size: 0.875rem; color: #64748b; }
</style>
</head>
<body>
<h1>馃憢 Welcome to Ava CMS!</h1>
<p>Run <code>composer install</code> from your Ava root directory.</p>
<p>No SSH? Run it locally and upload the <code>vendor/</code> folder.</p>
<p class="hint">Need help? See the <a href="https://ava.addy.zone/docs/hosting">Hosting Guide</a></p>
</body>
</html>
HTML;
exit;
}
require $autoloadPath;
// Load main configuration
$configPath = AVA_ROOT . '/app/config/ava.php';
if (!file_exists($configPath)) {
die("Configuration file not found: app/config/ava.php\n");
}
$config = require $configPath;
// Configure error handling. Logging is independent from browser debug output so
// production errors remain observable without exposing their details to users.
$errorSettings = \Ava\Support\ErrorSettings::resolve($config['debug'] ?? []);
$debugEnabled = $errorSettings['debug_enabled'];
$displayErrors = $errorSettings['display_errors'];
$logErrors = $errorSettings['log_errors'];
$errorLevel = $errorSettings['level'];
$configuredStorage = $config['paths']['storage'] ?? 'storage';
$configuredStorage = is_string($configuredStorage) && $configuredStorage !== ''
? $configuredStorage
: 'storage';
$errorLog = AVA_ROOT . '/' . trim($configuredStorage, '/\\') . '/logs/error.log';
// Set error reporting level
$errorReporting = match ($errorLevel) {
'all' => E_ALL,
'errors' => E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR,
'none' => 0,
default => E_ALL & ~E_NOTICE & ~E_DEPRECATED,
};
error_reporting($errorReporting);
// Display errors (only in debug mode with display_errors enabled)
ini_set('display_errors', $displayErrors ? '1' : '0');
ini_set('display_startup_errors', $displayErrors ? '1' : '0');
// Honour the configured logging policy even when the host php.ini enables
// logging globally.
ini_set('log_errors', $logErrors ? '1' : '0');
$reportedError = false;
// Log errors to Ava's file
if ($logErrors) {
ini_set('error_log', $errorLog);
// Native PHP errors are written after the custom handler returns. Rotate
// at shutdown only when an error occurred, avoiding work on clean requests.
register_shutdown_function(static function () use ($errorLog, $config, &$reportedError): void {
$lastError = error_get_last();
$fatalTypes = [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR];
$fatalError = $lastError !== null && in_array($lastError['type'], $fatalTypes, true);
if (!$reportedError && !$fatalError) {
return;
}
\Ava\Support\LogRotator::rotateIfNeeded(
$errorLog,
(int) ($config['logs']['max_size'] ?? 10 * 1024 * 1024),
(int) ($config['logs']['max_files'] ?? 3)
);
});
}
// Custom error handler for enhanced logging (only if debugging or logging enabled)
if ($debugEnabled || $logErrors) {
set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) use ($logErrors, $errorLog, &$reportedError) {
// Skip errors suppressed with @
if (!(error_reporting() & $errno)) {
return false;
}
$reportedError = true;
$type = match ($errno) {
E_ERROR, E_USER_ERROR => 'ERROR',
E_WARNING, E_USER_WARNING => 'WARNING',
E_NOTICE, E_USER_NOTICE => 'NOTICE',
E_DEPRECATED, E_USER_DEPRECATED => 'DEPRECATED',
default => 'UNKNOWN',
};
$message = sprintf(
"[%s] %s: %s in %s on line %d",
date('Y-m-d H:i:s'),
$type,
$errstr,
str_replace(AVA_ROOT . '/', '', $errfile),
$errline
);
if ($logErrors) {
@file_put_contents($errorLog, $message . "\n", FILE_APPEND | LOCK_EX);
}
// Let PHP's default handler run if display_errors is on
return false;
});
}
// Exception handler - always registered to show custom error pages
set_exception_handler(function (\Throwable $e) use ($debugEnabled, $displayErrors, $logErrors, $config, $errorLog) {
$message = sprintf(
"[%s] EXCEPTION: %s in %s on line %d\nStack trace:\n%s",
date('Y-m-d H:i:s'),
$e->getMessage(),
str_replace(AVA_ROOT . '/', '', $e->getFile()),
$e->getLine(),
$e->getTraceAsString()
);
if ($logErrors) {
@file_put_contents($errorLog, $message . "\n\n", FILE_APPEND | LOCK_EX);
\Ava\Support\LogRotator::rotateIfNeeded(
$errorLog,
(int) ($config['logs']['max_size'] ?? 10 * 1024 * 1024),
(int) ($config['logs']['max_files'] ?? 3)
);
}
if ($debugEnabled && $displayErrors) {
echo "<pre style='background:#1a1a2e;color:#eee;padding:20px;font-family:monospace;'>";
echo "<strong style='color:#ff6b6b;'>Exception:</strong> " . htmlspecialchars($e->getMessage()) . "\n\n";
echo "<strong>File:</strong> " . htmlspecialchars($e->getFile()) . ":" . $e->getLine() . "\n\n";
echo "<strong>Stack Trace:</strong>\n" . htmlspecialchars($e->getTraceAsString());
echo "</pre>";
} else {
// Show styled error page in production
http_response_code(500);
// Generate a short error reference ID from timestamp
$errorId = $logErrors ? date('ymd-His') . '-' . substr(md5($e->getMessage() . $e->getFile()), 0, 6) : null;
$requestedPath = $_SERVER['REQUEST_URI'] ?? null;
if ($requestedPath) {
$requestedPath = parse_url($requestedPath, PHP_URL_PATH) ?: $requestedPath;
}
echo \Ava\Rendering\ErrorPages::render500($errorId, $requestedPath, $logErrors);
}
exit(1);
});
// Initialize the application and return it
return new Ava\Application($config);