From bcb6bf0bf40b4a6b080f24209b25f238e4749335 Mon Sep 17 00:00:00 2001 From: RundelhausCode Date: Fri, 26 Mar 2021 11:00:45 +0100 Subject: [PATCH 1/4] after role back --- Backend/Controllers/Admin/Admin.php | 9 ++++ Backend/Controllers/Admin/AdminLogin.php | 1 + Backend/Controllers/Group/Logout.php | 3 +- composer.lock | 52 ++++++++++++------------ 4 files changed, 38 insertions(+), 27 deletions(-) diff --git a/Backend/Controllers/Admin/Admin.php b/Backend/Controllers/Admin/Admin.php index c394c4a..097b9a2 100644 --- a/Backend/Controllers/Admin/Admin.php +++ b/Backend/Controllers/Admin/Admin.php @@ -8,3 +8,12 @@ function isAdmin(): bool return isset($_SESSION['admin']); } +function makeAdminLogin(string $userName) +{ + setcookie("userName", $userName, [ + 'expires' => 0, + 'samesite' => 'Strict', + 'path' => '/' + ]); +} + diff --git a/Backend/Controllers/Admin/AdminLogin.php b/Backend/Controllers/Admin/AdminLogin.php index 813b3dd..f7ec881 100644 --- a/Backend/Controllers/Admin/AdminLogin.php +++ b/Backend/Controllers/Admin/AdminLogin.php @@ -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); diff --git a/Backend/Controllers/Group/Logout.php b/Backend/Controllers/Group/Logout.php index f8e4552..fa7cbb1 100644 --- a/Backend/Controllers/Group/Logout.php +++ b/Backend/Controllers/Group/Logout.php @@ -1,7 +1,7 @@ Date: Fri, 26 Mar 2021 12:06:46 +0100 Subject: [PATCH 2/4] setup.php error handeling --- Backend/setup.php | 61 ++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/Backend/setup.php b/Backend/setup.php index 1c83ba7..289514a 100644 --- a/Backend/setup.php +++ b/Backend/setup.php @@ -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:" . $e]); + 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); } From bcc69a88ad4fa9308dadb76872c32d2fe4d4999e Mon Sep 17 00:00:00 2001 From: RundelhausCode Date: Fri, 26 Mar 2021 14:03:02 +0100 Subject: [PATCH 3/4] documentaiont update --- Backend/Controllers/Admin/Admin.php | 3 +++ Backend/Controllers/FileHandler/FileHandler.php | 8 ++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Backend/Controllers/Admin/Admin.php b/Backend/Controllers/Admin/Admin.php index 097b9a2..51232d5 100644 --- a/Backend/Controllers/Admin/Admin.php +++ b/Backend/Controllers/Admin/Admin.php @@ -8,6 +8,9 @@ function isAdmin(): bool return isset($_SESSION['admin']); } +/** + * @param string $userName + */ function makeAdminLogin(string $userName) { setcookie("userName", $userName, [ diff --git a/Backend/Controllers/FileHandler/FileHandler.php b/Backend/Controllers/FileHandler/FileHandler.php index 5cbb8eb..e31cb14 100644 --- a/Backend/Controllers/FileHandler/FileHandler.php +++ b/Backend/Controllers/FileHandler/FileHandler.php @@ -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; } \ No newline at end of file From 4ac00af6221e147b476f6b107be36c1b1ea29bba Mon Sep 17 00:00:00 2001 From: RundelhausCode Date: Tue, 6 Apr 2021 10:58:22 +0200 Subject: [PATCH 4/4] error code update --- Backend/setup.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Backend/setup.php b/Backend/setup.php index 289514a..46ff1f5 100644 --- a/Backend/setup.php +++ b/Backend/setup.php @@ -91,7 +91,7 @@ if(isset($_POST["dbSetup"])){ } catch (\Exception $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:" . $e]); + echo json_encode(["message" => "Could not connect to the database. Please check your configuration. error:"]); exit(); } require_once ('Database/databaseMigration.php');