diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | setup.php | 41 |
2 files changed, 42 insertions, 0 deletions
@@ -1,5 +1,6 @@ * !backend/backend.php !assignment_list* +!setup.php !.gitignore 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\""); + } +} + +?> |