208 lines
6.5 KiB
PHP
208 lines
6.5 KiB
PHP
<?php
|
|
/**
|
|
* MN3 WMS — First-Time Setup
|
|
* Usage: php setup.php
|
|
*
|
|
* 1. Collects config values interactively
|
|
* 2. Writes app/config.php
|
|
* 3. Applies install.sql (creates both databases and all tables)
|
|
* 4. Runs migrate.php (registers baseline migration)
|
|
*/
|
|
|
|
define('ROOT', __DIR__);
|
|
|
|
if (php_sapi_name() !== 'cli') {
|
|
exit("Run this script from the command line only.\n");
|
|
}
|
|
|
|
// ── Helpers ────────────────────────────────────────────────────────────────
|
|
|
|
function ask(string $question, string $default = ''): string
|
|
{
|
|
$hint = $default !== '' ? " [{$default}]" : '';
|
|
echo $question . $hint . ': ';
|
|
$val = trim(fgets(STDIN));
|
|
return $val !== '' ? $val : $default;
|
|
}
|
|
|
|
function ask_secret(string $question): string
|
|
{
|
|
echo $question . ': ';
|
|
shell_exec('stty -echo');
|
|
$val = trim(fgets(STDIN));
|
|
shell_exec('stty echo');
|
|
echo "\n";
|
|
return $val;
|
|
}
|
|
|
|
function success(string $msg): void { echo "\033[32m✔ {$msg}\033[0m\n"; }
|
|
function fail(string $msg): void { echo "\033[31m✘ {$msg}\033[0m\n"; }
|
|
function info(string $msg): void { echo "\033[33m→ {$msg}\033[0m\n"; }
|
|
function line(): void { echo str_repeat('─', 50) . "\n"; }
|
|
|
|
// ── Header ─────────────────────────────────────────────────────────────────
|
|
|
|
echo "\n";
|
|
line();
|
|
echo " MN3 WMS — Setup\n";
|
|
line();
|
|
echo "\n";
|
|
|
|
// ── Check config.php ───────────────────────────────────────────────────────
|
|
|
|
$config_path = ROOT . '/app/config.php';
|
|
|
|
if (file_exists($config_path)) {
|
|
echo "app/config.php already exists.\n";
|
|
$overwrite = ask('Overwrite it?', 'no');
|
|
if (strtolower($overwrite[0]) !== 'y') {
|
|
info('Skipping config — using existing app/config.php');
|
|
echo "\n";
|
|
goto run_sql;
|
|
}
|
|
}
|
|
|
|
// ── Collect values ─────────────────────────────────────────────────────────
|
|
|
|
echo "\n";
|
|
info('App settings');
|
|
echo "\n";
|
|
$base_url = ask('App folder URL (e.g. /wms-app/)', '/wms-app/');
|
|
if (!str_starts_with($base_url, '/')) $base_url = '/' . $base_url;
|
|
if (!str_ends_with($base_url, '/')) $base_url .= '/';
|
|
|
|
echo "\n";
|
|
info('Database settings');
|
|
echo "\n";
|
|
$db_server = ask('DB host', 'localhost');
|
|
$db_user = ask('DB user', 'root');
|
|
$db_pass = ask_secret('DB password');
|
|
$db_name = ask('DB name (system)', 'wms');
|
|
$db_name2 = ask('DB name (operational)', 'wms2');
|
|
|
|
echo "\n";
|
|
info('SMTP settings (Gmail)');
|
|
echo "\n";
|
|
$smtp_user = ask('Gmail address');
|
|
$smtp_pass = ask_secret('Gmail app password');
|
|
|
|
// ── Test DB connection ─────────────────────────────────────────────────────
|
|
|
|
echo "\n";
|
|
info('Testing database connection...');
|
|
|
|
try {
|
|
$pdo = new PDO("mysql:host={$db_server};charset=utf8", $db_user, $db_pass);
|
|
success('Database connection OK');
|
|
} catch (PDOException $e) {
|
|
fail('Cannot connect to database: ' . $e->getMessage());
|
|
exit(1);
|
|
}
|
|
|
|
// ── Write config.php ───────────────────────────────────────────────────────
|
|
|
|
$pinkey = 'wms';
|
|
$method = 'AES-256-CBC';
|
|
$iv = '1234567890123456';
|
|
$smtp_pass_encrypted = openssl_encrypt($smtp_pass, $method, $pinkey, 0, $iv);
|
|
|
|
$config = <<<PHP
|
|
<?php
|
|
|
|
//<><><><><><><><> MAIN CONFIG <><><><><><><><>//
|
|
if(true){
|
|
\$isTest = "master";
|
|
\$base_url = "{$base_url}";
|
|
\$server_url = \$base_url."app/";
|
|
\$include_url = \$_SERVER['DOCUMENT_ROOT'].\$server_url;
|
|
|
|
\$db_server = "{$db_server}";
|
|
\$db_user = "{$db_user}";
|
|
\$db_pass = "{$db_pass}";
|
|
\$db_type = "mysql";
|
|
\$db_database = "{$db_name}";
|
|
\$db_database2 = "{$db_name2}";
|
|
|
|
\$db_server2 = (!empty(\$db_server2))?\$db_server2:\$db_server;
|
|
\$db_user2 = (!empty(\$db_user2))?\$db_user2:\$db_user;
|
|
\$db_pass2 = (!empty(\$db_pass2))?\$db_pass2:\$db_pass;
|
|
\$db_type2 = (!empty(\$db_type2))?\$db_type2:\$db_type;
|
|
|
|
\$time_zone = "Asia/Bangkok";
|
|
}
|
|
|
|
|
|
|
|
// unique key — used for SMTP password encryption
|
|
\$pinkey = "wms";
|
|
|
|
\$SMTP = [];
|
|
\$SMTP['server'] = "smtp.gmail.com";
|
|
\$SMTP['username'] = "{$smtp_user}";
|
|
\$SMTP['port'] = "587";
|
|
\$SMTP['password'] = "{$smtp_pass}";
|
|
\$method = "AES-256-CBC";
|
|
\$iv = "1234567890123456";
|
|
\$SMTP['password'] = openssl_encrypt(\$SMTP['password'], \$method, \$pinkey, 0, \$iv);
|
|
PHP;
|
|
|
|
file_put_contents($config_path, $config);
|
|
success('app/config.php written');
|
|
|
|
// ── Apply install.sql ──────────────────────────────────────────────────────
|
|
|
|
run_sql:
|
|
echo "\n";
|
|
info('Applying install.sql...');
|
|
|
|
// Load config if not already loaded
|
|
if (!isset($db_server)) {
|
|
require $config_path;
|
|
}
|
|
|
|
$install_sql = ROOT . '/install.sql';
|
|
if (!file_exists($install_sql)) {
|
|
fail('install.sql not found — skipping schema creation');
|
|
goto run_migrate;
|
|
}
|
|
|
|
$cmd = "mysql -h{$db_server} -u{$db_user} -p{$db_pass} < " . escapeshellarg($install_sql) . " 2>&1";
|
|
$output = [];
|
|
$code = 0;
|
|
exec($cmd, $output, $code);
|
|
|
|
if ($code !== 0) {
|
|
fail('install.sql failed: ' . implode("\n", $output));
|
|
exit(1);
|
|
}
|
|
success('install.sql applied — both databases created');
|
|
|
|
// ── Run migrate.php ────────────────────────────────────────────────────────
|
|
|
|
run_migrate:
|
|
echo "\n";
|
|
info('Running migrate.php...');
|
|
|
|
$cmd = 'php ' . escapeshellarg(ROOT . '/migrate.php') . ' 2>&1';
|
|
$output = [];
|
|
$code = 0;
|
|
exec($cmd, $output, $code);
|
|
|
|
foreach ($output as $line) {
|
|
echo " {$line}\n";
|
|
}
|
|
|
|
if ($code !== 0) {
|
|
fail('migrate.php failed');
|
|
exit(1);
|
|
}
|
|
|
|
// ── Done ───────────────────────────────────────────────────────────────────
|
|
|
|
echo "\n";
|
|
line();
|
|
success('Setup complete');
|
|
line();
|
|
echo "\n";
|
|
echo " Visit: http://your-server" . ($base_url ?? '/wms-app/') . "\n\n";
|