{$context}: empty SQL. Skipping."); return; } try { $db->exec($sql); } catch (PDOException $e) { fail($logs, "{$context}: " . htmlspecialchars($e->getMessage(), ENT_QUOTES)); } } // ----------------- // Load App Settings // ----------------- if (!file_exists(INITIALIZATION_FILE_NAME)) { fail($logs, "Initialization file not found: " . htmlspecialchars(INITIALIZATION_FILE_NAME, ENT_QUOTES) . ""); } $applicationSettings = @simplexml_load_file(INITIALIZATION_FILE_NAME); if (!$applicationSettings) { fail($logs, "Unable to load Application Settings from " . htmlspecialchars(INITIALIZATION_FILE_NAME, ENT_QUOTES) . "."); } // ------------- // Connect to DB // ------------- $host = (string) ($applicationSettings->DatabaseServer ?? ''); $database = (string) ($applicationSettings->DatabaseName ?? ''); $user = (string) ($applicationSettings->DatabaseUser ?? ''); $pass = (string) ($applicationSettings->DatabasePassword ?? ''); if ($host === '' || $database === '' || $user === '') { fail($logs, "Missing DB settings in settings.xml (DatabaseServer/DatabaseName/DatabaseUser)."); } try { $dsn = "mysql:host={$host};dbname={$database};charset=utf8mb4"; $db = new PDO($dsn, $user, $pass, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ]); } catch (Throwable $e) { fail($logs, "Unable to connect to the database: " . htmlspecialchars($e->getMessage(), ENT_QUOTES)); } // --------------------- // Find view_*.php files // --------------------- $dirFiles = scandir(__DIR__) ?: []; $viewFiles = array_values(array_filter($dirFiles, function ($f) { if (pathinfo($f, PATHINFO_EXTENSION) !== 'php') return false; if (strpos($f, 'view_') !== 0) return false; return is_file(__DIR__ . '/' . $f); })); if (!$viewFiles) { logLine($logs, "⚠️ No view definition files found (expected view_*.php). Nothing to do."); renderPage($logs, true); exit(0); } // ------------ // Create views // ------------ $created = 0; $failed = 0; foreach ($viewFiles as $file) { $viewName = pathinfo($file, PATHINFO_FILENAME); logLine($logs, "Processing " . htmlspecialchars($viewName, ENT_QUOTES) . "..."); $SQL = null; try { $result = include __DIR__ . '/' . $file; if ($result === false) { $failed++; logLine($logs, "❌ Failed to include " . htmlspecialchars($file, ENT_QUOTES) . ". Skipping."); continue; } } catch (Throwable $e) { $failed++; logLine($logs, "❌ Exception including " . htmlspecialchars($file, ENT_QUOTES) . ": " . htmlspecialchars($e->getMessage(), ENT_QUOTES)); continue; } // Drop then create try { $db->exec("DROP VIEW IF EXISTS `{$viewName}`"); } catch (PDOException $e) { // Non-fatal; keep going logLine($logs, "⚠️ Could not drop {$viewName}: " . htmlspecialchars($e->getMessage(), ENT_QUOTES)); } try { runSQL($db, $SQL, $viewName, $logs); $created++; logLine($logs, "✅ {$viewName} was created."); } catch (Throwable $e) { $failed++; logLine($logs, "❌ Error creating {$viewName}: " . htmlspecialchars($e->getMessage(), ENT_QUOTES)); } } logLine($logs, "
{$created} views created. {$failed} failed."); renderPage($logs, $failed === 0);