/** * Setup method -- make sure static adapter is available for testing. * * @return void */ public static function prepareAdapter() { try { DbGlobalAdapter::getStaticAdapter(); } catch (\Zend\Db\TableGateway\Exception\RuntimeException $e) { DbGlobalAdapter::setStaticAdapter(DbAdapterFactory::getAdapter()); } }
/** * Display repair instructions for database problems. * * @return mixed */ public function fixdatabaseAction() { $view = $this->createViewModel(); $view->dbname = $this->params()->fromPost('dbname', 'vufind'); $view->dbuser = $this->params()->fromPost('dbuser', 'vufind'); $view->dbhost = $this->params()->fromPost('dbhost', 'localhost'); $view->dbrootuser = $this->params()->fromPost('dbrootuser', 'root'); if (!preg_match('/^\\w*$/', $view->dbname)) { $this->flashMessenger()->setNamespace('error')->addMessage('Database name must be alphanumeric.'); } else { if (!preg_match('/^\\w*$/', $view->dbuser)) { $this->flashMessenger()->setNamespace('error')->addMessage('Database user must be alphanumeric.'); } else { if (strlen($this->params()->fromPost('submit', '')) > 0) { $newpass = $this->params()->fromPost('dbpass'); $newpassConf = $this->params()->fromPost('dbpassconfirm'); if (empty($newpass) || empty($newpassConf)) { $this->flashMessenger()->setNamespace('error')->addMessage('Password fields must not be blank.'); } else { if ($newpass != $newpassConf) { $this->flashMessenger()->setNamespace('error')->addMessage('Password fields must match.'); } else { // Connect to database: $connection = 'mysql://' . $view->dbrootuser . ':' . $this->params()->fromPost('dbrootpass') . '@' . $view->dbhost; $db = AdapterFactory::getAdapterFromConnectionString($connection . '/mysql'); try { $query = 'CREATE DATABASE ' . $view->dbname; $db->query($query, $db::QUERY_MODE_EXECUTE); $grant = "GRANT SELECT,INSERT,UPDATE,DELETE ON " . $view->dbname . ".* TO '{$view->dbuser}'@'{$view->dbhost}' " . "IDENTIFIED BY " . $db->getPlatform()->quoteValue($newpass) . " WITH GRANT OPTION"; $db->query($grant, $db::QUERY_MODE_EXECUTE); $db->query('FLUSH PRIVILEGES', $db::QUERY_MODE_EXECUTE); $db = AdapterFactory::getAdapterFromConnectionString($connection . '/' . $view->dbname); $sql = file_get_contents(APPLICATION_PATH . '/module/VuFind/sql/mysql.sql'); $statements = explode(';', $sql); foreach ($statements as $current) { // Skip empty sections: if (strlen(trim($current)) == 0) { continue; } $db->query($current, $db::QUERY_MODE_EXECUTE); } // If we made it this far, we can update the config file and // forward back to the home action! $string = "mysql://{$view->dbuser}:{$newpass}@" . $view->dbhost . '/' . $view->dbname; $config = ConfigReader::getLocalConfigPath('config.ini', null, true); $writer = new ConfigWriter($config); $writer->set('Database', 'database', $string); if (!$writer->save()) { return $this->forwardTo('Install', 'fixbasicconfig'); } return $this->redirect()->toRoute('install-home'); } catch (\Exception $e) { $this->flashMessenger()->setNamespace('error')->addMessage($e->getMessage()); } } } } } } return $view; }
/** * Set up the default database adapter. Do this early since the session handler * may depend on database access. * * @return void */ protected function initDatabase() { DbGlobalAdapter::setStaticAdapter(DbAdapterFactory::getAdapter()); }
/** * Prompt the user for database credentials. * * @return mixed */ public function getdbcredentialsAction() { $print = $this->params()->fromPost('printsql', 'nope'); if ($print == 'Skip') { $this->logsql = true; return $this->forwardTo('Upgrade', 'FixDatabase'); } else { $dbrootuser = $this->params()->fromPost('dbrootuser', 'root'); // Process form submission: if (strlen($this->params()->fromPost('submit', '')) > 0) { $pass = $this->params()->fromPost('dbrootpass'); // Test the connection: try { // Query a table known to exist $db = AdapterFactory::getAdapter($dbrootuser, $pass); $db->query("SELECT * FROM user;"); $this->session->dbRootUser = $dbrootuser; $this->session->dbRootPass = $pass; return $this->forwardTo('Upgrade', 'FixDatabase'); } catch (\Exception $e) { $this->flashMessenger()->setNamespace('error')->addMessage('Could not connect; please try again.'); } } } return $this->createViewModel(array('dbrootuser' => $dbrootuser)); }