Merge remote-tracking branch 'origin/main' into main

This commit is contained in:
Minik Gaarde Lambrecht 2021-04-06 10:59:06 +02:00
commit 0ea676a033
5 changed files with 56 additions and 29 deletions

View File

@ -8,3 +8,15 @@ function isAdmin(): bool
return isset($_SESSION['admin']);
}
/**
* @param string $userName
*/
function makeAdminLogin(string $userName)
{
setcookie("userName", $userName, [
'expires' => 0,
'samesite' => 'Strict',
'path' => '/'
]);
}

View File

@ -20,6 +20,7 @@ if(isset($_POST['aLogin'])){
$_SESSION['admin'] = true;
$_SESSION['success'] = "You are now logged in";
http_response_code(200);
makeAdminLogin($userName);
}else{
session_destroy();
http_response_code(401);

View File

@ -5,7 +5,8 @@
* @return string
*/
function ZipFileHandler(string $gameFileName, string $gameFileTmp){
function ZipFileHandler(string $gameFileName, string $gameFileTmp): string
{
$fileExtGame = explode('.', $gameFileName);
$fileActualExtGame = strtolower(end($fileExtGame));
@ -25,7 +26,6 @@ function ZipFileHandler(string $gameFileName, string $gameFileTmp){
echo json_encode(["message" => "Wrong file type gameFile"]);
exit();
}
return NULL;
}
/**
@ -33,7 +33,8 @@ return NULL;
* @param string $thumbnailFileTmp
* @return string
*/
function imagesFileHandler(string $thumbnailFileName, string $thumbnailFileTmp){
function imagesFileHandler(string $thumbnailFileName, string $thumbnailFileTmp): string
{
$fileExtThumb = explode('.', $thumbnailFileName);
$fileActualExtThumb = strtolower(end($fileExtThumb));
@ -53,5 +54,4 @@ function imagesFileHandler(string $thumbnailFileName, string $thumbnailFileTmp){
echo json_encode(["message" => "Wrong file type thumbnailFile"]);
exit();
}
return NULL;
}

View File

@ -1,7 +1,7 @@
<?php
session_start();
session_unset();
session_destroy();
$cookieCon = array(
@ -11,5 +11,6 @@ $cookieCon = array(
);
setcookie("groupName", null, $cookieCon);
setcookie("groupId", null, $cookieCon);
setcookie("userName", null, $cookieCon);
echo http_response_code(200);

View File

@ -17,13 +17,15 @@ function myDB(string $servername, string $username, string $password, string $DB
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
http_response_code(400);
echo json_encode(["message" => $conn->connect_error]);
exit();
}
$sql = "CREATE DATABASE ".$DBName;
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
if ($conn->query($sql) === FALSE) {
http_response_code(400);
echo json_encode(["message" => $conn->error]);
exit();
}
$conn->close();
return array(
@ -41,24 +43,33 @@ function myDB(string $servername, string $username, string $password, string $DB
*/
function liteDB(string $DBName): array
{
New SQLite3("Database/".$DBName.".sqlite");
$test = array(
$DBName = $DBName.".sqlite";
if(file_exists($DBName)){
http_response_code(400);
echo json_encode(["message" => "database already exits"]);
exit();
}
New SQLite3($DBName);
return array(
'driver' => 'sqlite',
'database' => realpath(dirname(__FILE__)."/Database/".$DBName.".sqlite")
//'Backend/Database/'.$DBName.'.sqlite'
'database' => realpath(dirname(__FILE__)."/".$DBName)
);
var_dump($test);
return $test;
}
$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();
}
$servername = "localhost";
$username = "root";
$password = "V#_xWL6_";
$DBName = "TestDB";
if(isset($_POST["dbSetup"])){
switch($_POST["dbType"]){
case "mysql":
$dbCon = myDB($_POST["dbServername"],$_POST["dbUsername"],$_POST["dbPassword"], $_POST["dbName"]);
@ -67,28 +78,30 @@ if(isset($_POST["dbSetup"])){
$dbCon = liteDB($_POST["dbName"]);
break;
default:
die("wrong dbType");
http_response_code(400);
echo json_encode(["message" => "database type is wrong"]);
exit();
}
file_put_contents("../config/database.json", json_encode($dbCon));
file_put_contents($conFilePath, json_encode($dbCon));
require_once('../bootstrap.php');
try{
$capsule->Connection()->getPdo();
} catch (\Exception $e) {
//file_put_contents("../config/database.json", NULL);
die("Could not connect to the database. Please check your configuration. error:" . $e );
http_response_code(400);
file_put_contents($conFilePath, NULL);
echo json_encode(["message" => "Could not connect to the database. Please check your configuration. error:"]);
exit();
}
require_once ('Database/databaseMigration.php');
AdminUser::firstOrCreate([
'user_name' => $_POST["AdminUsername"], 'password' => password_hash($_POST["AdminPassword"],PASSWORD_DEFAULT)
]);
http_response_code(201);
}else{
echo "not set";
http_response_code(400);
}