/** * Establishes the connection to the database server * * This methods establishes the default connection to the database server by using configuration parameters that * come from the basis configuration object and then, register the {@link iMSCP_Database} instance in the * {@link iMSCP_Registry} for further usage. * * A PDO instance is also registered in the registry for further usage. * * @throws iMSCP_Exception_Database|iMSCP_Exception * @return void */ protected function initializeDatabase() { try { $db_pass_key = $db_pass_iv = ''; eval(@file_get_contents($this->config->CONF_DIR . '/imscp-db-keys')); if (!empty($db_pass_key) && !empty($db_pass_iv)) { iMSCP_Registry::set('MCRYPT_KEY', $db_pass_key); iMSCP_Registry::set('MCRYPT_IV', $db_pass_iv); $connection = iMSCP_Database::connect($this->config->DATABASE_USER, decryptBlowfishCbcPassword($this->config->DATABASE_PASSWORD), $this->config->DATABASE_TYPE, $this->config->DATABASE_HOST, $this->config->DATABASE_NAME); if (!$connection->execute('SET NAMES `utf8`')) { throw new iMSCP_Exception(sprintf('Unable to set charset for database communication. SQL returned: %s', $connection->errorMsg())); } } else { throw new iMSCP_Exception('Database key and/or initialization vector was not generated.'); } } catch (PDOException $e) { throw new iMSCP_Exception_Database('Unable to establish the connection to the database. SQL returned: ' . $e->getMessage()); } // Register Database instance in registry for further usage. iMSCP_Registry::set('db', $connection); }
/** * Check database connection. * * @param string $sql_database * @param string $sql_user * @param string $sql_pass * @return bool */ function check_db_connection($sql_database, $sql_user, $sql_pass) { /** @var $cfg iMSCP_Config_Handler_File */ $cfg = iMSCP_Registry::get('config'); try { iMSCP_Database::connect($sql_user, $sql_pass, $cfg->DATABASE_TYPE, $cfg->DATABASE_HOST, $sql_database, 'privateConnection'); } catch (PDOException $e) { return false; } return true; }