From 836093c0a91eebda1d95bb9f2d07ab21ac1a60aa Mon Sep 17 00:00:00 2001 From: Thanakorn S Date: Tue, 12 May 2026 14:34:55 +0700 Subject: [PATCH] automate setup process --- setup.php | 207 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 setup.php diff --git a/setup.php b/setup.php new file mode 100644 index 0000000..6e0f52d --- /dev/null +++ b/setup.php @@ -0,0 +1,207 @@ +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 = <<<><><><><><><> 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";