66 lines
2.2 KiB
PHP
66 lines
2.2 KiB
PHP
<?php
|
|
|
|
function installpostgre()
|
|
{
|
|
// Generate a secure random password (20 chars, URL-safe)
|
|
if (function_exists('random_bytes')) {
|
|
$bytes = random_bytes(16);
|
|
} else {
|
|
$bytes = openssl_random_pseudo_bytes(16);
|
|
}
|
|
$password = substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, 20);
|
|
|
|
echo 'sudo apt update' . "\n";
|
|
echo 'sudo apt install postgresql postgresql-contrib -y' . "\n";
|
|
#$pgVersion = exec('pg_config --version | awk \'{print $NF}\' | cut -d\'.\' -f1-2');
|
|
#echo 'sudo cp /etc/postgresql/' . $pgVersion . '/main/pg_hba.conf /etc/postgresql/' . $pgVersion . '/main/pg_hba.conf.bak' . "\n";
|
|
#echo 'sudo sh -c "echo \'host all all 127.0.0.1/32 md5\' > /etc/postgresql/' . $pgVersion . '/main/pg_hba.conf"' . "\n";
|
|
echo 'sudo systemctl restart postgresql' . "\n";
|
|
echo 'sudo -u postgres psql -c "CREATE DATABASE dragoncore;"' . "\n";
|
|
echo 'sudo -u postgres psql -c "CREATE USER dragoncore2 WITH PASSWORD \'' . $password . '\';"' . "\n";
|
|
echo 'sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE dragoncore TO dragoncore;"' . "\n";
|
|
echo 'sudo -u postgres psql -c "GRANT USAGE, CREATE ON SCHEMA public TO dragoncore;"' . "\n";
|
|
echo 'sudo systemctl restart postgresql' . "\n";
|
|
|
|
// Ensure /opt/DragonCore exists
|
|
$dir = '/opt/DragonCore';
|
|
if (!is_dir($dir)) {
|
|
if (!mkdir($dir, 0755, true) && !is_dir($dir)) {
|
|
fwrite(STDERR, "Failed to create directory: $dir\n");
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Create config.php with variables (no return)
|
|
$config = <<<PHP
|
|
<?php
|
|
\$db_host = '127.0.0.1';
|
|
\$db_port = '5432';
|
|
\$db_name = 'dragoncore';
|
|
\$db_user = 'dragoncore2';
|
|
\$db_pass = '{$password}';
|
|
PHP;
|
|
|
|
$configPath = $dir . '/config.php';
|
|
if (file_put_contents($configPath, $config) === false) {
|
|
fwrite(STDERR, "Failed to write config file at {$configPath}\n");
|
|
} else {
|
|
echo "Config file created at {$configPath}\n";
|
|
}
|
|
}
|
|
|
|
if ($argc < 2) {
|
|
die("Use o MENU!\n");
|
|
}
|
|
|
|
$functionName = $argv[1];
|
|
|
|
if (function_exists($functionName)) {
|
|
array_shift($argv);
|
|
echo call_user_func_array($functionName, array_slice($argv, 1));
|
|
} else {
|
|
echo "Function $functionName does not exist.\n";
|
|
}
|
|
|
|
?>
|