big changes hehe

This commit is contained in:
Bruno Rybársky 2024-01-18 11:49:38 +01:00
parent b785babb3f
commit e4bb8f10a3
9 changed files with 155 additions and 94 deletions

@ -0,0 +1,22 @@
<?php
require_once "lib/account.php";
function endpoint($endpoint_data)
{
switch ($endpoint_data["action"]){
case "login":
return doLogin($endpoint_data["email"], $endpoint_data["password"]);
break;
case "logout":
return doLogout();
break;
case "register":
return doRegister($endpoint_data["firstname"], $endpoint_data["lastname"], $endpoint_data["nickname"], $endpoint_data["email"], $endpoint_data["password"], $endpoint_data["minecraftnick"], $endpoint_data["activation_token"]);
break;
}
}

@ -1,9 +1,11 @@
<?php <?php
/** @noinspection PhpIncludeInspection */ /** @noinspection PhpIncludeInspection */
require_once "secrets/config.php"; require_once "secrets/config.php";
require_once "lib/navpages.php";
require_once "lib/routing.php";
require_once "lib/config.php"; require_once "lib/config.php";
require_once "lib/navigation.php";
require_once "lib/router.php";
require_once "lib/page.php";
require_once "lib/endpoint.php";
$routerConfig = array(); $routerConfig = array();
$routerRequest = array(); $routerRequest = array();
@ -13,8 +15,16 @@ if(initRouter()) {
/** @noinspection PhpArrayIsAlwaysEmptyInspection */ /** @noinspection PhpArrayIsAlwaysEmptyInspection */
session_set_cookie_params(0, '/', "." . $routerRequest["domain"] . "." . $routerRequest["tld"], true, true); session_set_cookie_params(0, '/', "." . $routerRequest["domain"] . "." . $routerRequest["tld"], true, true);
session_start(); session_start();
/** @noinspection PhpArrayIsAlwaysEmptyInspection */ if($routerRequest["type"] == "api") {
echo getPage($routerRequest["page_name"]); echo getEndpoint($routerRequest["page_name"], $_REQUEST);
}elseif ($routerRequest["type"] == "page") {
/** @noinspection PhpArrayIsAlwaysEmptyInspection */
echo getPage($routerRequest["page_name"]);
}
else{
echo "Unknown request";
}
} }
else{ else{
exit(); exit();

@ -4,29 +4,25 @@ function isLoggedIn(){
return $_SESSION["ID"] > 0 && !empty($_SESSION["email"]); return $_SESSION["ID"] > 0 && !empty($_SESSION["email"]);
} }
function doLogin(){ function doLogin($email, $password){
global $mysqli; global $mysqli;
if(!empty($_POST["email"]) && !empty($_POST["password"])){ if(!empty($email) && !empty($password)){
$email = $_POST["email"];
$pass = $_POST["password"];
/* prepare statement */
$stmt = $mysqli->prepare("SELECT ID, FirstName, LastName, Nickname, PasswordHash, MinecraftNick, isAdmin FROM Users WHERE EMAIL = ? AND isActive = 1"); $stmt = $mysqli->prepare("SELECT ID, FirstName, LastName, Nickname, PasswordHash, MinecraftNick, isAdmin FROM Users WHERE EMAIL = ? AND isActive = 1");
$stmt->bind_param("s", $email); $stmt->bind_param("s", $email);
$stmt->execute(); $stmt->execute();
$idcko = 0; $idcko = 0;
$fname = ""; $fname = "";
$lname = ""; $lname = "";
$nickname = ""; $nickname = "";
$pwdhash = ""; $pwdhash = "";
$mcnick = ""; $mcnick = "";
/* bind variables to prepared statement */
$stmt->bind_result($idcko, $fname, $lname, $nickname, $pwdhash, $mcnick, false); $stmt->bind_result($idcko, $fname, $lname, $nickname, $pwdhash, $mcnick, false);
/* fetch values */
$found = false; $found = false;
if($stmt->num_rows() > 0){ if($stmt->num_rows() > 0){
$stmt->fetch(); $stmt->fetch();
if (password_verify($pass, $pwdhash)){ if (password_verify($password, $pwdhash)){
$_SESSION["ID"] = $idcko; $_SESSION["ID"] = $idcko;
$_SESSION["first_name"] = $fname; $_SESSION["first_name"] = $fname;
$_SESSION["last_name"] = $lname; $_SESSION["last_name"] = $lname;
@ -38,51 +34,32 @@ function doLogin(){
} }
} }
$stmt->close(); $stmt->close();
if($found){ return $found ? ["status" => "success"] : ["status" => "fail"];
$status = ["status" => "success"];
}
else{
$status = ["status" => "fail"];
}
echo json_encode($status);
} }
} }
function doLogout(){ function doLogout(){
if(isLoggedIn()){ if(isLoggedIn()){
session_destroy(); session_destroy();
$status = ["status" => "success"]; return ["status" => "success"];
} else {
return ["status" => "fail"];
} }
else{
$status = ["status" => "fail"];
}
echo json_encode($status);
} }
function doRegister(){ function doRegister($firstname, $lastname, $nickname, $email, $password, $minecraftnick, $activationtoken){
global $mysqli;
$status = ["status" => "fail"]; $status = ["status" => "fail"];
if (!empty($_POST["activationtoken"])){ if (!empty($activationtoken)){
global $mysqli; $passwordHash = password_hash($password, PASSWORD_DEFAULT);
$stmt = $mysqli->prepare("UPDATE Users SET FirstName = ?, LastName = ?, Nickname = ?, Email = ?, PasswordHash = ?, MinecraftNick = ?, isAdmin = 0, isActivated = 1 WHERE isActivated = 0 AND ActivationToken = ?");
$firstName = $_POST["firstname"]; $stmt->bind_param("sssssss", $firstname, $lastname, $nickname, $email, $passwordHash, $minecraftnick, $activationtoken);
$lastName = $_POST["lastname"]; $stmt->execute();
$nickname = $_POST["nickname"]; if ($stmt->affected_rows > 0) {
$email = $_POST["email"]; $status["status"] = "success";
$password = $_POST["password"];
$minecraftNick = $_POST["minecraftnick"];
$activationToken = $_POST["activationtoken"];
if (!empty($firstName) && !empty($lastName) && !empty($nickname) && !empty($email) && !empty($password)) {
$passwordHash = password_hash($password, PASSWORD_DEFAULT);
$stmt = $mysqli->prepare("UPDATE Users SET FirstName = ?, LastName = ?, Nickname = ?, Email = ?, PasswordHash = ?, MinecraftNick = ?, isAdmin = 0, isActivated = 1 WHERE isActivated = 0 AND ActivationToken = ?");
$stmt->bind_param("ssssss", $firstName, $lastName, $nickname, $email, $passwordHash, $minecraftNick, $activationToken);
$stmt->execute();
if ($stmt->affected_rows > 0) {
$status["status"] = "success";
}
$stmt->close();
} }
$stmt->close();
} }
echo json_encode($status); return $status;
} }

@ -8,6 +8,8 @@
$routerConfig["template_dir"] = "templates/"; $routerConfig["template_dir"] = "templates/";
$routerConfig["endpoint_dir"] = "endpoints/";
$routerConfig["page_dir"] = "pages/"; $routerConfig["page_dir"] = "pages/";
$routerConfig["protocol"] = "https://"; $routerConfig["protocol"] = "https://";

41
lib/endpoint.php Normal file

@ -0,0 +1,41 @@
<?php
function runEndpoint($endpoint_file)
{
global $routerRequest;
$endpoint_data = $_POST
require_once $endpoint_file;
return endpoint($endpoint_data);
}
function getEndpoint($endpoint_name)
{
$output = array();
$output["status"] = "fail";
global $routerConfig;
global $routerRequest;
if(!$endpoint_name){
$endpoint_name = $routerRequest["page_name"];
}
$endpoint_file = $routerConfig["endpoint_dir"] . $routerRequest["subdomain"] . "/" . $endpoint_name . ".php";
$endpoint_file_global = $routerConfig["endpoint_dir"] . "global/" . $endpoint_name . ".php";
if (file_exists($endpoint_file_global)){
$output = runEndpoint($endpoint_file_global);
}
elseif (file_exists($endpoint_file)){
$output = runEndpoint($endpoint_file);
}
else{
$output["error"] = "Not found";
http_response_code(404);
}
return json_encode($output);
}

@ -1,49 +1,4 @@
<?php <?php
function initRouter(){
global $routerRequest;
global $routerConfig;
$routerRequest["requestAddress"] = array_slice(explode('.', $_SERVER['HTTP_HOST']), -3, 3); //get the last 3 elements
$needsRedirect = false;
if(count($routerRequest["requestAddress"]) < 3){
// Root domain accessed directly
$needsRedirect = true;
$routerRequest["subdomain"] = $routerConfig["default_site"];
$routerRequest["domain"] = basename($routerRequest["requestAddress"][0]);
$routerRequest["tld"] = basename($routerRequest["requestAddress"][1]);
} else {
$routerRequest["subdomain"] = basename($routerRequest["requestAddress"][0]);
$routerRequest["domain"] = basename($routerRequest["requestAddress"][1]);
$routerRequest["tld"] = basename($routerRequest["requestAddress"][2]);
$routerRequest["page_name"] = basename($_SERVER["QUERY_STRING"]);
if (empty($routerRequest["page_name"])) {
// Page name is empty
$needsRedirect = true;
$routerRequest["page_name"] = $routerConfig["default_page"];
}
}
if ($needsRedirect) {
$redirectAddress = $routerConfig["protocol"] .
$routerRequest["subdomain"] . "." .
$routerRequest["domain"] . "." .
$routerRequest["tld"] . "/" .
$routerRequest["page_name"];
// Redirect with default page name
header("Location: $redirectAddress");
}
return !$needsRedirect;
}
function renderDynamicPage($page_file) function renderDynamicPage($page_file)
{ {
require_once $page_file; require_once $page_file;
@ -90,5 +45,4 @@ function getPage($page_name = null){
$out = str_replace("__TEMPLATE__NAV__", $nav, $out); $out = str_replace("__TEMPLATE__NAV__", $nav, $out);
$out = str_replace("__TEMPLATE__PAGE__", $page, $out); $out = str_replace("__TEMPLATE__PAGE__", $page, $out);
return str_replace("__TEMPLATE_PAGE_NAME__", $page_name, $out); return str_replace("__TEMPLATE_PAGE_NAME__", $page_name, $out);
} }

55
lib/router.php Normal file

@ -0,0 +1,55 @@
<?php
function initRouter(){
global $routerRequest;
global $routerConfig;
$routerRequest["requestAddress"] = array_slice(explode('.', $_SERVER['HTTP_HOST']), -3, 3); //get the last 3 elements
$needsRedirect = false;
if(count($routerRequest["requestAddress"]) < 3){
// Root domain accessed directly
$needsRedirect = true;
$routerRequest["subdomain"] = $routerConfig["default_site"];
$routerRequest["domain"] = basename($routerRequest["requestAddress"][0]);
$routerRequest["tld"] = basename($routerRequest["requestAddress"][1]);
} else {
$routerRequest["subdomain"] = basename($routerRequest["requestAddress"][0]);
$routerRequest["domain"] = basename($routerRequest["requestAddress"][1]);
$routerRequest["tld"] = basename($routerRequest["requestAddress"][2]);
$routerRequest["page_name"] = basename($_SERVER["QUERY_STRING"]);
if (empty($routerRequest["page_name"])) {
// Page name is empty
$needsRedirect = true;
$routerRequest["page_name"] = $routerConfig["default_page"];
}
}
if ($needsRedirect) {
$redirectAddress = $routerConfig["protocol"] .
$routerRequest["subdomain"] . "." .
$routerRequest["domain"] . "." .
$routerRequest["tld"] . "/" .
$routerRequest["page_name"];
// Redirect with default page name
header("Location: $redirectAddress");
}
else{
if($_SERVER["REQUEST_METHOD"] == "POST"){
$routerRequest["type"] = "api";
}
if(empty($routerRequest["type"])){
$routerRequest["type"] = "page";
}
}
return !$needsRedirect;
}

@ -1,6 +1,6 @@
<?php <?php
require_once "lib/routing.php"; require_once "lib/router.php";
function render() function render()
{ {