diff --git a/app/Controller/AppController.php b/app/Controller/AppController.php index 72f0dfc0d..ccda2d1a6 100755 --- a/app/Controller/AppController.php +++ b/app/Controller/AppController.php @@ -124,17 +124,8 @@ class AppController extends Controller if (!empty($this->params['named']['sql'])) { $this->sql_dump = 1; } - // check for a supported datasource configuration - $dataSourceConfig = ConnectionManager::getDataSource('default')->config; - if (!isset($dataSourceConfig['encoding'])) { - $db = ConnectionManager::getDataSource('default'); - $db->setConfig(array('encoding' => 'utf8')); - ConnectionManager::create('default', $db->config); - } - $dataSource = $dataSourceConfig['datasource']; - if ($dataSource != 'Database/Mysql' && $dataSource != 'Database/Postgres') { - throw new Exception('datasource not supported: ' . $dataSource); - } + + $this->_setupDatabaseConnection(); $this->set('ajax', $this->request->is('ajax')); $this->set('queryVersion', $this->__queryVersion); @@ -506,6 +497,24 @@ class AppController extends Controller $this->render('/Servers/json/simple'); } + /* + * Setup & validate the database connection configuration + * @throws Exception if the configured database is not supported. + */ + protected function _setupDatabaseConnection() { + // check for a supported datasource configuration + $dataSourceConfig = ConnectionManager::getDataSource('default')->config; + if (!isset($dataSourceConfig['encoding'])) { + $db = ConnectionManager::getDataSource('default'); + $db->setConfig(array('encoding' => 'utf8')); + ConnectionManager::create('default', $db->config); + } + $dataSource = $dataSourceConfig['datasource']; + if ($dataSource != 'Database/Mysql' && $dataSource != 'Database/Postgres') { + throw new Exception('datasource not supported: ' . $dataSource); + } + } + /* * Sanitize the configured `MISP.baseurl` and expose it to the view as `baseurl`. */