160 lines
4.5 KiB
PHP
160 lines
4.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
use MintyPHP\Firewall;
|
||
|
|
use MintyPHP\Session;
|
||
|
|
use MintyPHP\Debugger;
|
||
|
|
use MintyPHP\Analyzer;
|
||
|
|
use MintyPHP\Router;
|
||
|
|
use MintyPHP\DB;
|
||
|
|
use MintyPHP\Buffer;
|
||
|
|
use MintyPHP\I18n;
|
||
|
|
use MintyPHP\Http\Request;
|
||
|
|
use MintyPHP\Http\LocaleResolver;
|
||
|
|
use MintyPHP\Http\AccessControl;
|
||
|
|
use MintyPHP\Http\AssetDetector;
|
||
|
|
|
||
|
|
// Change directory to project root
|
||
|
|
chdir(__DIR__ . '/..');
|
||
|
|
// Load the libraries
|
||
|
|
require 'vendor/autoload.php';
|
||
|
|
// Load the config parameters
|
||
|
|
require 'config/config.php';
|
||
|
|
// Load the routes
|
||
|
|
require 'config/router.php';
|
||
|
|
// Register shortcut functions
|
||
|
|
require 'lib/Support/helpers.php';
|
||
|
|
|
||
|
|
// Start the firewall
|
||
|
|
Firewall::start();
|
||
|
|
|
||
|
|
// Start the session
|
||
|
|
Session::start();
|
||
|
|
|
||
|
|
// Initialize default locale from tenant/app settings
|
||
|
|
$defaultLocale = appDefaultLocale();
|
||
|
|
if ($defaultLocale !== null) {
|
||
|
|
I18n::$defaultLocale = $defaultLocale;
|
||
|
|
if (!defined('APP_LOCALES') || in_array($defaultLocale, APP_LOCALES, true)) {
|
||
|
|
if (I18n::$locale === '' || !in_array(I18n::$locale, APP_LOCALES, true)) {
|
||
|
|
I18n::$locale = $defaultLocale;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Auto-login from remember-me cookie
|
||
|
|
if (empty($_SESSION['user']['id'])) {
|
||
|
|
\MintyPHP\Service\RememberMeService::autoLoginFromCookie();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Parse request URI and resolve locale
|
||
|
|
$originalUri = $_SERVER['REQUEST_URI'] ?? '';
|
||
|
|
$_SERVER['MINTY_ORIGINAL_URI'] = $originalUri;
|
||
|
|
|
||
|
|
$localeResolver = new LocaleResolver();
|
||
|
|
$parsedUri = $localeResolver->parseUri($originalUri);
|
||
|
|
$pathWithoutLocale = $parsedUri['pathWithoutLocale'];
|
||
|
|
$requestedLocale = $parsedUri['locale'];
|
||
|
|
$_SERVER['MINTY_LOCALE'] = $requestedLocale;
|
||
|
|
|
||
|
|
// Rewrite REQUEST_URI without locale segment for router
|
||
|
|
if ($parsedUri['hadLocaleInUrl']) {
|
||
|
|
$_SERVER['REQUEST_URI'] = $localeResolver->buildUriWithoutLocale(
|
||
|
|
$pathWithoutLocale,
|
||
|
|
$parsedUri['query']
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Resolve effective locale from URL > Session > Cookie > Default
|
||
|
|
I18n::$locale = $localeResolver->resolveLocale(
|
||
|
|
$requestedLocale,
|
||
|
|
$_SESSION['user']['locale'] ?? null,
|
||
|
|
$_COOKIE['locale'] ?? null
|
||
|
|
);
|
||
|
|
|
||
|
|
$isAssetRequest = AssetDetector::isAssetRequest($pathWithoutLocale);
|
||
|
|
|
||
|
|
// Refresh tenant context if stale or missing (skip for assets)
|
||
|
|
if (!empty($_SESSION['user']['id']) && !$isAssetRequest) {
|
||
|
|
$userId = (int) ($_SESSION['user']['id'] ?? 0);
|
||
|
|
if ($userId > 0) {
|
||
|
|
$ttl = defined('TENANT_SNAPSHOT_TTL') ? (int) TENANT_SNAPSHOT_TTL : 120;
|
||
|
|
$lastRefresh = (int) ($_SESSION['tenant_snapshot_at'] ?? 0);
|
||
|
|
$needsRefresh = empty($_SESSION['current_tenant']) || empty($_SESSION['available_tenants']);
|
||
|
|
if ($ttl > 0 && (time() - $lastRefresh) >= $ttl) {
|
||
|
|
$needsRefresh = true;
|
||
|
|
}
|
||
|
|
if ($needsRefresh) {
|
||
|
|
\MintyPHP\Service\AuthService::loadTenantDataIntoSession($userId);
|
||
|
|
$_SESSION['tenant_snapshot_at'] = time();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($requestedLocale === '' && !$isAssetRequest) {
|
||
|
|
$method = strtoupper((string) ($_SERVER['REQUEST_METHOD'] ?? 'GET'));
|
||
|
|
if (in_array($method, ['GET', 'HEAD'], true)) {
|
||
|
|
$target = Request::withLocale($pathWithoutLocale, I18n::$locale ?? I18n::$defaultLocale);
|
||
|
|
$target .= $parsedUri['query'] !== '' ? '?' . $parsedUri['query'] : '';
|
||
|
|
Router::redirect($target);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Guard: restrict non-public pages to logged-in users
|
||
|
|
$accessControl = new AccessControl();
|
||
|
|
$accessControl->requireAuthOrRedirect($pathWithoutLocale, !empty($_SESSION['user']['id']));
|
||
|
|
|
||
|
|
// Analyze the PHP code
|
||
|
|
if (Debugger::$enabled) {
|
||
|
|
Analyzer::execute();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Load the action into body
|
||
|
|
ob_start();
|
||
|
|
if (Router::getTemplateAction()) {
|
||
|
|
require Router::getTemplateAction();
|
||
|
|
}
|
||
|
|
if (ob_get_contents()) {
|
||
|
|
ob_end_flush();
|
||
|
|
trigger_error('MintyPHP template action"' . Router::getTemplateAction() . '" should not send output. Error raised ', E_USER_WARNING);
|
||
|
|
} else {
|
||
|
|
ob_end_clean();
|
||
|
|
}
|
||
|
|
|
||
|
|
ob_start();
|
||
|
|
if (Router::getAction()) {
|
||
|
|
extract(Router::getParameters());
|
||
|
|
require Router::getAction();
|
||
|
|
}
|
||
|
|
if (ob_get_contents()) {
|
||
|
|
ob_end_flush();
|
||
|
|
trigger_error('MintyPHP action "' . Router::getAction() . '" should not send output. Error raised ', E_USER_WARNING);
|
||
|
|
} else {
|
||
|
|
ob_end_clean();
|
||
|
|
}
|
||
|
|
|
||
|
|
// End the session
|
||
|
|
Session::end();
|
||
|
|
|
||
|
|
// Close the database connection
|
||
|
|
DB::close();
|
||
|
|
|
||
|
|
if (Router::getTemplateView()) {
|
||
|
|
Buffer::start('html');
|
||
|
|
if (Router::getView()) {
|
||
|
|
require Router::getView();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Show developer toolbar
|
||
|
|
if (Debugger::$enabled) {
|
||
|
|
Debugger::toolbar();
|
||
|
|
}
|
||
|
|
|
||
|
|
Buffer::end('html');
|
||
|
|
// Load body into template
|
||
|
|
require Router::getTemplateView();
|
||
|
|
} else { // Handle the 'none' template case
|
||
|
|
if (Router::getView()) {
|
||
|
|
require Router::getView();
|
||
|
|
}
|
||
|
|
}
|