2021-03-26 07:35:51 +00:00
|
|
|
<?php
|
|
|
|
//use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
|
|
use \Backend\Models\AdminUser;
|
|
|
|
use JetBrains\PhpStorm\ArrayShape;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $servername
|
|
|
|
* @param string $username
|
|
|
|
* @param string $password
|
|
|
|
* @param string $DBName
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
function myDB(string $servername, string $username, string $password, string $DBName): array
|
|
|
|
{
|
|
|
|
$conn = new mysqli($servername, $username, $password);
|
|
|
|
|
|
|
|
if ($conn->connect_error) {
|
2021-03-26 11:06:46 +00:00
|
|
|
http_response_code(400);
|
|
|
|
echo json_encode(["message" => $conn->connect_error]);
|
|
|
|
exit();
|
2021-03-26 07:35:51 +00:00
|
|
|
}
|
|
|
|
$sql = "CREATE DATABASE ".$DBName;
|
2021-03-26 11:06:46 +00:00
|
|
|
if ($conn->query($sql) === FALSE) {
|
|
|
|
http_response_code(400);
|
|
|
|
echo json_encode(["message" => $conn->error]);
|
|
|
|
exit();
|
2021-03-26 07:35:51 +00:00
|
|
|
}
|
|
|
|
$conn->close();
|
|
|
|
return array(
|
|
|
|
"driver" => "mysql",
|
|
|
|
"host" => $servername,
|
|
|
|
"database" => $DBName,
|
|
|
|
"username" => $username,
|
|
|
|
"password" => $password
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $DBName
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
function liteDB(string $DBName): array
|
|
|
|
{
|
2021-03-26 11:06:46 +00:00
|
|
|
$DBName = $DBName.".sqlite";
|
|
|
|
if(file_exists($DBName)){
|
|
|
|
http_response_code(400);
|
|
|
|
echo json_encode(["message" => "database already exits"]);
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
New SQLite3($DBName);
|
|
|
|
return array(
|
2021-03-26 07:35:51 +00:00
|
|
|
'driver' => 'sqlite',
|
2021-03-26 11:06:46 +00:00
|
|
|
'database' => realpath(dirname(__FILE__)."/".$DBName)
|
2021-03-26 07:35:51 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-26 11:06:46 +00:00
|
|
|
$conFilePath = '../config/database.json';
|
|
|
|
if(!file_exists($conFilePath)){
|
|
|
|
$confile = fopen($conFilePath, "w");
|
|
|
|
fclose($confile);
|
|
|
|
}
|
|
|
|
if (0 !== filesize($conFilePath)){
|
|
|
|
http_response_code(400);
|
|
|
|
echo json_encode(["message" => "already have database connection"]);
|
|
|
|
exit();
|
|
|
|
}
|
2021-03-26 07:35:51 +00:00
|
|
|
|
|
|
|
|
2021-03-26 11:06:46 +00:00
|
|
|
if(isset($_POST["dbSetup"])){
|
2021-03-26 07:35:51 +00:00
|
|
|
|
|
|
|
switch($_POST["dbType"]){
|
|
|
|
case "mysql":
|
|
|
|
$dbCon = myDB($_POST["dbServername"],$_POST["dbUsername"],$_POST["dbPassword"], $_POST["dbName"]);
|
|
|
|
break;
|
|
|
|
case "sqlite":
|
|
|
|
$dbCon = liteDB($_POST["dbName"]);
|
|
|
|
break;
|
|
|
|
default:
|
2021-03-26 11:06:46 +00:00
|
|
|
http_response_code(400);
|
|
|
|
echo json_encode(["message" => "database type is wrong"]);
|
|
|
|
exit();
|
2021-03-26 07:35:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-26 11:06:46 +00:00
|
|
|
file_put_contents($conFilePath, json_encode($dbCon));
|
2021-03-26 07:35:51 +00:00
|
|
|
|
|
|
|
try{
|
2021-04-06 10:24:45 +00:00
|
|
|
require_once('../bootstrap.php');
|
2021-03-26 07:35:51 +00:00
|
|
|
} catch (\Exception $e) {
|
2021-03-26 11:06:46 +00:00
|
|
|
file_put_contents($conFilePath, NULL);
|
2021-04-06 10:24:45 +00:00
|
|
|
http_response_code(400);
|
|
|
|
echo json_encode(["message" => "Could not connect to the database. Please check your configuration. error:" . $e]);
|
2021-03-26 11:06:46 +00:00
|
|
|
exit();
|
2021-03-26 07:35:51 +00:00
|
|
|
}
|
|
|
|
require_once ('Database/databaseMigration.php');
|
|
|
|
AdminUser::firstOrCreate([
|
|
|
|
'user_name' => $_POST["AdminUsername"], 'password' => password_hash($_POST["AdminPassword"],PASSWORD_DEFAULT)
|
|
|
|
]);
|
2021-03-26 11:06:46 +00:00
|
|
|
http_response_code(201);
|
2021-03-26 07:35:51 +00:00
|
|
|
|
|
|
|
}else{
|
2021-03-26 11:06:46 +00:00
|
|
|
http_response_code(400);
|
2021-03-26 07:35:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|