first(); if ($config && $config->system_config_value == '1') { $maintenanceMode = true; } } } catch (\Exception $e) { // If DB fails, assume not in maintenance or handle gracefully $maintenanceMode = false; } if (!$maintenanceMode) { return $next($request); } // Inject maintenance mode variable into view try { $view = new View(); $view->getEngine()->addGlobal('is_maintenance_mode', true); } catch (\Throwable $e) { } // Allow access to login route and assets $path = $request->getPathInfo(); $allowedRoutes = ['/login', '/auth/login']; $isAsset = preg_match('/\.(css|js|jpg|jpeg|png|gif|ico|woff|woff2|ttf|svg)$/', $path); if (in_array($path, $allowedRoutes) || $isAsset) { return $next($request); } // Allow Super Admin to bypass if ($this->isSuperAdmin()) { return $next($request); } // Render maintenance page try { $view = new View(); return new Response($view->render('errors/maintenance.twig'), 503); } catch (\Throwable $e) { return new Response('Maintenance Mode', 503); } } /** * Check if current user is Super Admin. * * @return bool */ protected function isSuperAdmin(): bool { if (!isset($_SESSION['user_id'])) { return false; } try { if (class_exists('\Modules\Auth\Models\User')) { $user = \Modules\Auth\Models\User::find($_SESSION['user_id']); return $user && $user->isSuperAdmin(); } } catch (\Exception $e) { return false; } return false; } }