diff --git a/src/Controller/Component/InstallComponent.php b/src/Controller/Component/InstallComponent.php index d746780..685bb59 100644 --- a/src/Controller/Component/InstallComponent.php +++ b/src/Controller/Component/InstallComponent.php @@ -16,19 +16,14 @@ class InstallComponent extends Component public function installationCheck() { - // check for installation of database configuration // first check for database connection using default connection // connection to the database try { $connection = ConnectionManager::get('default'); - $connected = $connection->connect(); + $connection->connect(); } catch (\Exception $connectionError) { - $connected = false; - } - - if ($connected === false) { if (Configure::read('Database.installed') == true) { // if database connection not established // and Database.installed is set to TRUE diff --git a/src/Controller/InstallController.php b/src/Controller/InstallController.php index 3e695de..6ffab95 100644 --- a/src/Controller/InstallController.php +++ b/src/Controller/InstallController.php @@ -77,59 +77,60 @@ public function connection() { } } + // add some extra bits and pieces that may be helpful + $salt = ''; + $salt_chars = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; + for ($i = 0; $i < 40; ++ $i) { + $salt .= $salt_chars[mt_rand(0, strlen($salt_chars) - 1)]; + } + Configure::write('Installer.Connection.salt', $salt); + Configure::write('Installer.Connection.baseurl', "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}"); + try { /** * Try to connect the database with the new configuration */ ConnectionManager::config('my_default', Configure::read('Installer.Connection')); $db = ConnectionManager::get('my_default'); + $db->connect(); - try { - $connected = $db->connect(); - } catch (Exception $connectionError) { - $connected = false; - $this->Flash->error(__('Cannot connect to database: {0}', $connectionError->getMessage())); - } + /** + * We will create the true database configuration file with our configuration + */ + $success = true; + $written = []; + foreach (Configure::read('Installer.Files') as $key => $config) { + if ($config['use'] && !file_exists(CONFIG . $config['filename'])) { + $input = new File($config['default']); + $content = $input->read(); + foreach (Configure::read('Installer.Connection') as $k => $v) { + $content = str_replace('{default_' . $k . '}', $v, $content); + } - if ($connected) { - /** - * We will create the true database configuration file with our configuration - */ - $success = true; - $written = []; - foreach (Configure::read('Installer.Files') as $key => $config) { - if ($config['use'] && !file_exists(CONFIG . $config['filename'])) { - $input = new File($config['default']); - $content = $input->read(); - foreach (Configure::read('Installer.Connection') as $k => $v) { - $content = str_replace('{default_' . $k . '}', $v, $content); - } - - $output = new File(CONFIG . $config['filename']); - if ($output->write($content)) { - $written[] = $output; - } else { - $this->Flash->error(__('{0} file cannot be modified', $config['filename'])); - $success = false; - break; - } + $output = new File(CONFIG . $config['filename']); + if ($output->write($content)) { + $written[] = $output; + } else { + $this->Flash->error(__('{0} file cannot be modified', $config['filename'])); + $success = false; + break; } } + } - if ($success) { - $this->Flash->success(__('Connected to the database')); + if ($success) { + $this->Flash->success(__('Connected to the database')); - // import database if import_database is checked - if ($import_database) { - $this->redirect(['action' => 'data']); - } else { - $this->redirect(['action' => 'finish']); - } + // import database if import_database is checked + if ($import_database) { + $this->redirect(['action' => 'data']); } else { - // Remove any config files that were written successfully, so that we try them again next time. - foreach ($written as $file) { - $file->delete(); - } + $this->redirect(['action' => 'finish']); + } + } else { + // Remove any config files that were written successfully, so that we try them again next time. + foreach ($written as $file) { + $file->delete(); } } } catch (MissingConnectionException $e) { @@ -155,33 +156,16 @@ public function data() { // connection to the database try { - $database_connect = $db->connect(); - } catch (Exception $connectionError) { - $database_connect = false; - $this->Flash->error(__('Can not connect to database: {0}', $connectionError->getMessage())); - } - - $this->set(compact('database_connect')); - - if ($this->request->is('post')) { - - $db = ConnectionManager::get('default'); + $db->connect(); + $this->set(['database_connect' => true]); - // connection to the database - try { - $connected = $db->connect(); - } catch (Exception $connectionError) { - $connected = false; - $this->Flash->error(__('Can not connect to database: {0}', $connectionError->getMessage())); - } - - // connection to the database - if ($connected) { - if ($this->_importSchema($db) && $this->_handleMigrations()) { - $this->Flash->success(__('Database imported')); - $this->redirect(array('action' => 'finish')); - } + if ($this->request->is('post') && $this->_importSchema($db) && $this->_handleMigrations()) { + $this->Flash->success(__('Database imported')); + $this->redirect(array('action' => 'finish')); } + } catch (Exception $connectionError) { + $this->set(['database_connect' => false]); + $this->Flash->error(__('Can not connect to database: {0}', $connectionError->getMessage())); } $this->set($d);