This commit is contained in:
2023-08-12 01:39:17 +03:00
parent e92f792640
commit 79a031401a
13 changed files with 127 additions and 8 deletions

23
api/_auth.php Normal file
View File

@@ -0,0 +1,23 @@
<?php
require_once("_db.php"); //("api/_db.php");
session_start();
$LOGGED_IN = false;
if (isset($_SESSION["userid"])) {
// Check if user still exist
$s = $db->prepare("SELECT * FROM users WHERE id = ?");
$s->bind_param("s", $_SESSION["userid"]);
$s->execute();
if (!(bool)$s->get_result()->fetch_assoc()) {
session_unset();
session_destroy();
echo "user id does not exist";
die("user id used in session does not exist");
}
$LOGGED_IN = true;
}
?>

25
api/_config.php Normal file
View File

@@ -0,0 +1,25 @@
<?php
$Config = array();
$Config_FileName = "config.json";
$Config_PossiblePaths = array(
"./" . $Config_FileName,
"../" . $Config_FileName,
"../../" . $Config_FileName,
"../../../" . $Config_FileName,
"./api/" . $Config_FileName,
);
foreach ($Config_PossiblePaths as $path) {
if (file_exists($path)) {
$content = file_get_contents($path);
$Config = json_decode($content, true);
break;
}
}
if (!$Config) {
die("invalid configuration file");
}
?>

View File

@@ -1,3 +1,7 @@
<?php
require_once("_config.php"); //("api/_config.php");
$db = new mysqli($Config["db"]["addr"], $Config["db"]["user"], $Config["db"]["pass"], $Config["db"]["name"]);
?>

6
api/_errors.php Normal file
View File

@@ -0,0 +1,6 @@
<?php
// Internal errors
$Err_Int_JSONEncodeError = "int.jsonencode";
?>

22
api/_json.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
require_once("_errors.php");
function ReturnJSONData ($arr) {
$data = json_encode($arr);
if (!$data) {
$data = json_encode(array("error" => $Err_Int_JSONEncodeError));
}
header("Content-Type: application/json; charset=utf-8");
echo $data;
exit;
}
function ReturnJSONError ($err, $desc) {
ReturnJSONData(array(
"error" => $err,
"description" => $desc
));
}
?>

5
api/user/create.php Normal file
View File

@@ -0,0 +1,5 @@
<?php
// TODO
?>

14
api/user/index.php Normal file
View File

@@ -0,0 +1,14 @@
<?php
require_once("../_auth.php");
require_once("../_json.php");
// TODO
if ($LOGGED_IN) {
echo 1;
} else {
echo 2;
}
?>