diff options
author | lshprung <lshprung@yahoo.com> | 2022-04-11 20:32:46 -0700 |
---|---|---|
committer | lshprung <lshprung@yahoo.com> | 2022-04-11 20:32:46 -0700 |
commit | b54e381e0419022d5a686c5729744b93b9d5692c (patch) | |
tree | a38d82e40163811dfcc292d02603a5b4fd07d5bc /setup.php | |
parent | 0d1d59dd72d00b8ba57bcc81c35f3efe55bae310 (diff) |
Created setup script
Diffstat (limited to 'setup.php')
-rw-r--r-- | setup.php | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/setup.php b/setup.php new file mode 100644 index 0000000..0e7a9cc --- /dev/null +++ b/setup.php @@ -0,0 +1,41 @@ +<?php + +$config_path = "backend/config.php"; +$check = "y"; + +print "This script will help to setup MySQL database access\n"; + +if(file_exists($config_path)){ + $check = readline("Warning: \"$config_path\" already exists. This script will overwrite the contents of this file. Proceed? [y/N] "); +} + +if($check == "y"){ + print "\n"; + + if(!$handle = fopen($config_path, "w")){ + die("Error: Cannot open file \"$config_path\"\n"); + } + + $input["hostname"] = readline("Enter hostname [default: localhost]: "); + if(!$input["hostname"]) $input["hostname"] = "localhost"; + $input["username"] = readline("Enter username: "); + $input["password"] = readline("Enter password: "); + $input["database"] = readline("Enter database: "); + + fwrite_wrapper($handle, "<?php\n\n"); + fwrite_wrapper($handle, "\$db = mysqli_connect(\"" . $input["hostname"] . "\", \"" . $input["username"] . "\", \"" . $input["password"] . "\", \"" . $input["database"] . "\");\n"); + fwrite_wrapper($handle, "if(!\$db) die(\"Failed to connect to database: \" . mysqli_connect_error());\n\n"); + fwrite_wrapper($handle, "?>"); + + echo "\nSuccessfully wrote \"$config_path\"\n"; +} + +function fwrite_wrapper($file, $string){ + GLOBAL $config_path; + + if(fwrite($file, $string) === FALSE){ + die("Cannot write to file \"$config_path\""); + } +} + +?> |