From 52cb2ec048af76066dd56e04285d5617d05977ab Mon Sep 17 00:00:00 2001 From: DariusIII Date: Tue, 25 Apr 2017 22:36:38 +0200 Subject: [PATCH] Add first version of new installer, update .env.example with admin data placeholders --- .env.example | 4 + _install/install_nntmux.php | 199 ++++++++++++++++++++++++++++++++++++ 2 files changed, 203 insertions(+) create mode 100644 _install/install_nntmux.php diff --git a/.env.example b/.env.example index 4e10ed492..96ab94ff4 100644 --- a/.env.example +++ b/.env.example @@ -27,3 +27,7 @@ NN_SSL_CAPATH = NN_SSL_VERIFY_PEER = NN_SSL_VERIFY_HOST = NN_SSL_ALLOW_SELF_SIGNED = 1 + +ADMIN_USER = +ADMIN_PASS = +ADMIN_EMAIL = diff --git a/_install/install_nntmux.php b/_install/install_nntmux.php new file mode 100644 index 000000000..605ef8ffc --- /dev/null +++ b/_install/install_nntmux.php @@ -0,0 +1,199 @@ + true, + 'createDb' => true, + 'dbhost' => getenv('DB_HOST'), + 'dbname' => getenv('DB_Name'), + 'dbpass' => getenv('DB_PASSWORD'), + 'dbport' => getenv('PORT'), + 'dbsock' => getenv('DB_SOCKET'), + 'dbtype' => getenv('DB_SYSTEM'), + 'dbuser' => getenv('DB_USER'), + ] + ); + $dbConnCheck = true; + } catch (\PDOException $e) { + ColorCLI::doEcho(ColorCLI::error('Unable to connect to MySQL server.')); + $error = true; + $dbConnCheck = false; + } catch (\RuntimeException $e) { + switch ($e->getCode()) { + case 1: + case 2: + case 3: + $error = true; + ColorCLI::doEcho(ColorCLI::alternate($e->getMessage())); + break; + default: + var_dump($e); + throw new \RuntimeException($e->getMessage(), $e->getCode(), $e); + } + } + + // Check if the MySQL version is correct. + $goodVersion = false; + if (!$error) { + try { + $goodVersion = $pdo->isDbVersionAtLeast(NN_MINIMUM_MYSQL_VERSION); + } catch (\PDOException $e) { + $goodVersion = false; + $error = true; + ColorCLI::doEcho(ColorCLI::error('Could not get version from MySQL server.')); + } + + if ($goodVersion === false) { + $error = true; + ColorCLI::doEcho(ColorCLI::error( + 'You are using an unsupported version of ' . + getenv('DB_SYSTEM') . + ' the minimum allowed version is ' . + NN_MINIMUM_MYSQL_VERSION)); + } + } +} + +// Start inserting data into the DB. +if (!$error) { + + $DbSetup = new DbUpdate( + [ + 'backup' => false, + 'db' => $pdo, + ] + ); + + try { + $DbSetup->processSQLFile(); // Setup default schema + $DbSetup->processSQLFile( // Process any custom stuff. + [ + 'filepath' => NN_RES . 'db' . DS . 'schema' . DS . 'mysql-data.sql' + ] + ); + $DbSetup->loadTables(); // Load default data files + } catch (\PDOException $err) { + $error = true; + ColorCLI::doEcho(ColorCLI::error('Error inserting: (' . $err->getMessage() . ')')); + } + + if (!$error) { + // Check one of the standard tables was created and has data. + $dbInstallWorked = false; + $reschk = $pdo->query('SELECT COUNT(id) AS num FROM tmux'); + if ($reschk === false) { + $dbCreateCheck = false; + $error = true; + ColorCLI::doEcho(ColorCLI::warningOver('Could not select data from your database, check that tables and data are properly created/inserted.')); + } else { + foreach ($reschk as $row) { + if ($row['num'] > 0) { + $dbInstallWorked = true; + break; + } + } + } + + $ver = new Versions(); + $patch = $ver->getSQLPatchFromFile(); + + if ($dbInstallWorked) { + if ($patch > 0) { + $updateSettings = $pdo->exec( + "UPDATE settings SET value = '$patch' WHERE section = '' AND subsection = '' AND name = 'sqlpatch'" + ); + } else { + $updateSettings = false; + } + + // If it all worked, move to the next page. + if ($updateSettings) { + ColorCLI::doEcho(ColorCLI::info('Database updated successfully')); + } else { + $error = true; + ColorCLI::doEcho(ColorCLI::error('Could not update sqlpatch to ' . $patch . ' for your database.')); + } + } else { + $dbCreateCheck = false; + $error = true; + ColorCLI::doEcho(ColorCLI::warning('Could not select data from your database.')); + } + } +} + +//Insert admin user into database +if (getenv('ADMIN_USER') === '' || getenv('ADMIN_PASS') === '' || getenv('ADMIN_EMAIL') === '') { + $error = true; +} else { + switch (getenv('DB_SYSTEM')) { + case 'mysql': + $adapter = 'MySql'; + break; + case 'pgsql': + $adapter = 'PostgreSql'; + break; + default: + break; + } + + if ($adapter !== null) { + if (empty(getenv('DB_SOCKET'))) { + $host = empty(getenv('DB_PORT')) ? getenv('DB_HOST') : getenv('DB_HOST') . ':' . getenv('DB_PORT'); + } else { + $host = getenv('DB_SOCKET'); + } + + lithium\data\Connections::add('default', + [ + 'type' => 'database', + 'adapter' => $adapter, + 'host' => $host, + 'login' => getenv('DB_USER'), + 'password' => getenv('DB_PASSWORD'), + 'database' => getenv('DB_NAME'), + 'encoding' => 'UTF-8', + 'persistent' => false, + ] + ); + } + + $user = new Users(); + if (!$user->isValidUsername(getenv('ADMIN_USER'))) { + $error = true; + } else { + $usrCheck = $user->getByUsername(getenv('ADMIN_USER')); + if ($usrCheck) { + $error = true; + } + } + if (!$user->isValidEmail(getenv('ADMIN_EMAIL'))) { + $error = true; + } + + if (!$error) { + $adminCheck = $user->add(getenv('ADMIN_USER'), getenv('ADMIN_PASS'), getenv('ADMIN_EMAIL'), 2, '', ''); + if (!is_numeric($adminCheck)) { + $error = true; + } else { + $user->login($adminCheck, '', 1); + } + } +} \ No newline at end of file