db_host = trim($config['db_host'] ?? ''); $this->db_name = trim($config['db_name'] ?? ''); $this->db_user = trim($config['db_user'] ?? ''); $this->db_pass = trim($config['db_pass'] ?? ''); $this->db_port = trim($config['db_port'] ?? '5432'); $this->admin_password = trim($config['admin_password'] ?? ''); } public function validate() { $this->errors = []; if (empty($this->db_host)) { $this->errors[] = 'Database host is required.'; } if (empty($this->db_name)) { $this->errors[] = 'Database name is required.'; } if (empty($this->db_user)) { $this->errors[] = 'Database username is required.'; } if (empty($this->db_pass)) { $this->errors[] = 'Database password is required.'; } if (empty($this->admin_password)) { $this->errors[] = 'Admin password is required.'; } if (!is_numeric($this->db_port) || $this->db_port < 1 || $this->db_port > 65535) { $this->errors[] = 'Database port must be a valid port number (1-65535).'; } if (strlen($this->admin_password) < 6) { $this->errors[] = 'Admin password must be at least 6 characters long.'; } return empty($this->errors); } public function testDatabaseConnection() { try { $this->pdo = new PDO( "pgsql:host={$this->db_host};port={$this->db_port};dbname={$this->db_name}", $this->db_user, $this->db_pass, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_STRINGIFY_FETCHES => false, PDO::ATTR_EMULATE_PREPARES => false ] ); return true; } catch (PDOException $e) { $this->errors[] = 'Database connection failed: ' . $e->getMessage(); return false; } } public function updateConfigFile() { $const_content = "db_host}\"; const DB_NAME=\"{$this->db_name}\"; const DB_USER=\"{$this->db_user}\"; const DB_PASS=\"{$this->db_pass}\"; const DB_PORT = {$this->db_port}; const DB_SCMA='public'; const SESS_USR_KEY = 'qgis_user'; const WWW_DIR = '".__DIR__."'; const DATA_DIR = '".dirname(__DIR__)."/data'; "; // Ensure incl directory exists if (!is_dir('incl')) { if (!mkdir('incl', 0755, true)) { $this->errors[] = 'Failed to create incl directory. Check permissions.'; return false; } } if (file_put_contents('incl/const.php', $const_content) === false) { $this->errors[] = 'Failed to write const.php file. Check file permissions.'; return false; } $this->success_messages[] = 'Configuration file updated successfully.'; return true; } public function runDatabaseSetup() { try { // Read SQL files $setup_sql = file_get_contents('installer/setup.sql'); $init_sql = file_get_contents('installer/init.sql'); if ($setup_sql === false) { $this->errors[] = 'Failed to read installer/setup.sql file.'; return false; } if ($init_sql === false) { $this->errors[] = 'Failed to read installer/init.sql file.'; return false; } // Replace placeholder password with actual hash $admin_password_hash = password_hash($this->admin_password, PASSWORD_DEFAULT); $init_sql = str_replace('ADMIN_APP_PASS', $admin_password_hash, $init_sql); // Execute setup SQL (creates tables) $this->pdo->exec($setup_sql); $this->success_messages[] = 'Database tables created successfully.'; // Execute init SQL (inserts initial data) $this->pdo->exec($init_sql); $this->success_messages[] = 'Initial data inserted successfully.'; return true; } catch (PDOException $e) { $this->errors[] = 'Database setup failed: ' . $e->getMessage(); return false; } } public function createDataDirectory() { $data_dir = __DIR__ . '/../data'; if (!is_dir($data_dir)) { if (!mkdir($data_dir, 0755, true)) { $this->errors[] = 'Failed to create data directory. Check permissions.'; return false; } if (!mkdir($data_dir.'/uploads', 0755, true)) { $this->errors[] = 'Failed to create data/uploads directory. Check permissions.'; return false; } } if (!is_dir('assets')) { if (!mkdir('assets', 0755, true)) { $this->errors[] = 'Failed to create assets directory. Check permissions.'; return false; } } if (!is_dir('assets/brand')) { if (!mkdir('assets/brand', 0755, true)) { $this->errors[] = 'Failed to create assets/brand directory. Check permissions.'; return false; } } $this->success_messages[] = 'Required directories created successfully.'; return true; } public function install() { if (!$this->validate()) { return false; } if (!$this->testDatabaseConnection()) { return false; } if (!$this->updateConfigFile()) { return false; } if (!$this->createDataDirectory()) { return false; } if (!$this->runDatabaseSetup()) { return false; } $this->success_messages[] = 'Installation completed successfully!'; return true; } public function getErrors() { return $this->errors; } public function getSuccessMessages() { return $this->success_messages; } public function hasErrors() { return !empty($this->errors); } public function getFirstError() { return !empty($this->errors) ? $this->errors[0] : ''; } public function getAllMessages() { return array_merge($this->success_messages, $this->errors); } public function cleanup(){ # remove install files $files = scandir('installer'); foreach($files as $f){ unlink('installer/'.$f); } rmdir('installer'); unlink('install.php'); } } // Check if already installed if (file_exists('incl/const.php')) { $const_content = file_get_contents('incl/const.php'); if (strpos($const_content, '${APP_DB}') === false) { // Already installed, redirect to main page header('Location: index.php'); exit; } } $error = ''; $success = ''; // Check if form was submitted if ($_SERVER['REQUEST_METHOD'] === 'POST') { $installer = new GeoLiteInstaller($_POST); if ($installer->install()) { $installer->cleanup(); $success = implode(' ', $installer->getSuccessMessages()) . ' You can now access the application.'; } else { $error = $installer->getFirstError(); } } ?> GeoLite Installation
Check Installation Status

Requirements