From 138ea701851b2c215290e08bd9a6bc18a1792420 Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Tue, 20 Jan 2026 22:44:47 +0200 Subject: [PATCH] Updates core code to use App(db) --- app/classes/ratelimiter.php | 11 +++++----- app/classes/user.php | 4 +++- app/core/Maintenance.php | 22 ++++++++++++-------- app/core/PluginManager.php | 40 +++++++++++++++++++++++++------------ app/pages/login.php | 3 ++- 5 files changed, 52 insertions(+), 28 deletions(-) diff --git a/app/classes/ratelimiter.php b/app/classes/ratelimiter.php index 058f245..2948137 100644 --- a/app/classes/ratelimiter.php +++ b/app/classes/ratelimiter.php @@ -1,10 +1,10 @@ database = $database; - $this->db = $database->getConnection(); + public function __construct($logger = null) { + $db = App::db(); + // Extract PDO connection from Database object + $this->db = ($db instanceof PDO) ? $db : $db->getConnection(); + // Initialize logger (plugin Log if present or NullLogger otherwise) if ($logger !== null) { $this->logger = $logger; diff --git a/app/classes/user.php b/app/classes/user.php index eadb843..44ca4f1 100644 --- a/app/classes/user.php +++ b/app/classes/user.php @@ -1,5 +1,7 @@ rateLimiter = new RateLimiter($database); + $this->rateLimiter = new RateLimiter(); $this->twoFactorAuth = new TwoFactorAuthentication($database); } diff --git a/app/core/Maintenance.php b/app/core/Maintenance.php index f63752c..badeaab 100644 --- a/app/core/Maintenance.php +++ b/app/core/Maintenance.php @@ -2,6 +2,8 @@ namespace App\Core; +use App\App; + class Maintenance { // Keep it simple: store the flag within the app directory @@ -13,10 +15,11 @@ class Maintenance return true; } // Prefer DB settings if available in the current request - if (isset($GLOBALS['db'])) { + $db = App::db(); + if ($db) { try { require_once __DIR__ . '/Settings.php'; - $settings = new Settings($GLOBALS['db']); + $settings = new Settings($db); return $settings->get('maintenance_enabled', '0') === '1'; } catch (\Throwable $e) { // fall back to file flag @@ -27,10 +30,11 @@ class Maintenance public static function enable(string $message = ''): bool { - if (isset($GLOBALS['db'])) { + $db = App::db(); + if ($db) { try { require_once __DIR__ . '/Settings.php'; - $settings = new Settings($GLOBALS['db']); + $settings = new Settings($db); $ok1 = $settings->set('maintenance_enabled', '1'); $ok2 = $settings->set('maintenance_message', $message); return $ok1 && $ok2; @@ -48,10 +52,11 @@ class Maintenance public static function disable(): bool { - if (isset($GLOBALS['db'])) { + $db = App::db(); + if ($db) { try { require_once __DIR__ . '/Settings.php'; - $settings = new Settings($GLOBALS['db']); + $settings = new Settings($db); $ok1 = $settings->set('maintenance_enabled', '0'); // keep last message for reference, optional to clear return $ok1; @@ -74,10 +79,11 @@ class Maintenance if ($envMsg) { return trim($envMsg); } - if (isset($GLOBALS['db'])) { + $db = App::db(); + if ($db) { try { require_once __DIR__ . '/Settings.php'; - $settings = new Settings($GLOBALS['db']); + $settings = new Settings($db); return (string)$settings->get('maintenance_message', ''); } catch (\Throwable $e) { // ignore and fall back to file flag diff --git a/app/core/PluginManager.php b/app/core/PluginManager.php index 2a70d62..57c5d54 100644 --- a/app/core/PluginManager.php +++ b/app/core/PluginManager.php @@ -167,9 +167,9 @@ class PluginManager return false; } - // Use global DB and get PDO connection - $db = $GLOBALS['db']; - $pdo = $db->getConnection(); + // Use App API to get database connection + $db = \App\App::db(); + $pdo = ($db instanceof \PDO) ? $db : $db->getConnection(); try { // Update or insert plugin setting in database @@ -213,9 +213,15 @@ class PluginManager return false; } - // Use global DB and get PDO connection - $db = $GLOBALS['db']; - $pdo = $db->getConnection(); + // Use App API to get database connection + $db = \App\App::db(); + + // If database unavailable, fallback to manifest + if (!$db) { + return self::$catalog[$plugin]['meta']['enabled'] ?? false; + } + + $pdo = ($db instanceof \PDO) ? $db : $db->getConnection(); try { $stmt = $pdo->prepare('SELECT `value` FROM settings WHERE `key` = :key LIMIT 1'); @@ -226,7 +232,8 @@ class PluginManager return $result && $result['value'] === '1'; } catch (\PDOException $e) { app_log('error', 'PluginManager::isEnabled failed for ' . $plugin . ': ' . $e->getMessage(), ['scope' => 'plugin']); - return false; + // Fallback to manifest on database error + return self::$catalog[$plugin]['meta']['enabled'] ?? false; } } @@ -276,36 +283,43 @@ class PluginManager return false; } - global $db; - if (!$db instanceof PDO) { + $db = \App\App::db(); + if (!$db) { app_log('error', 'PluginManager::purge: Database connection not available', ['scope' => 'plugin']); return false; } + $pdo = ($db instanceof \PDO) ? $db : $db->getConnection(); try { // First disable the plugin self::setEnabled($plugin, false); // Remove plugin settings - $stmt = $db->prepare('DELETE FROM settings WHERE `key` LIKE :pattern'); + $stmt = $pdo->prepare('DELETE FROM settings WHERE `key` LIKE :pattern'); $stmt->execute([':pattern' => 'plugin_enabled_' . $plugin]); // Drop plugin-specific tables (user_pro_* tables for this plugin) - $stmt = $db->prepare('SHOW TABLES LIKE "user_pro_%"'); + $stmt = $pdo->prepare('SHOW TABLES LIKE "user_pro_%"'); $stmt->execute(); - $tables = $stmt->fetchAll(PDO::FETCH_COLUMN, 0); + $tables = $stmt->fetchAll(\PDO::FETCH_COLUMN, 0); + // Disable foreign key checks temporarily to allow table drops + $pdo->exec('SET FOREIGN_KEY_CHECKS=0'); + foreach ($tables as $table) { // Check if this table belongs to the plugin by checking its migration file $migrationFile = self::$catalog[$plugin]['path'] . '/migrations/create_' . $plugin . '_tables.sql'; if (file_exists($migrationFile)) { $migrationContent = file_get_contents($migrationFile); if (strpos($migrationContent, $table) !== false) { - $db->exec("DROP TABLE IF EXISTS `$table`"); + $pdo->exec("DROP TABLE IF EXISTS `$table`"); app_log('info', 'PluginManager::purge: Dropped table ' . $table . ' for plugin ' . $plugin, ['scope' => 'plugin']); } } } + + // Re-enable foreign key checks + $pdo->exec('SET FOREIGN_KEY_CHECKS=1'); app_log('info', 'PluginManager::purge: Successfully purged plugin ' . $plugin, ['scope' => 'plugin']); return true; diff --git a/app/pages/login.php b/app/pages/login.php index b582ebb..a384cdd 100644 --- a/app/pages/login.php +++ b/app/pages/login.php @@ -23,7 +23,8 @@ try { // Initialize RateLimiter require_once '../app/classes/ratelimiter.php'; - $rateLimiter = new RateLimiter($db); + $rateLimiter = new RateLimiter(); + // Get user IP require_once '../app/helpers/ip_helper.php'; $user_IP = getUserIP();