2023-08-12 04:09:17 +05:30
|
|
|
<?php
|
2024-01-13 05:54:42 +05:30
|
|
|
// Things related to authentication
|
|
|
|
|
|
|
|
|
2023-08-12 04:09:17 +05:30
|
|
|
|
2023-11-01 00:27:17 +05:30
|
|
|
// Includes
|
2024-01-20 22:35:45 +05:30
|
|
|
if (isset($IS_FRONTEND) && $IS_FRONTEND)
|
2023-12-20 08:38:13 +05:30
|
|
|
require_once("api/_db.php");
|
|
|
|
else
|
|
|
|
require_once("_db.php");
|
2023-08-12 04:09:17 +05:30
|
|
|
|
2023-08-16 09:04:01 +05:30
|
|
|
|
|
|
|
|
2023-08-30 07:11:13 +05:30
|
|
|
// End currently active session
|
2024-01-13 05:54:42 +05:30
|
|
|
function AUTH_EndSession () {
|
2023-08-30 07:11:13 +05:30
|
|
|
session_unset();
|
|
|
|
session_destroy();
|
|
|
|
if (isset($_COOKIE["PHPSESSID"])) {
|
|
|
|
unset($_COOKIE["PHPSESSID"]);
|
|
|
|
setcookie("PHPSESSID", "", time() - 3600, "/");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-09-01 01:56:16 +05:30
|
|
|
// A few tips:
|
|
|
|
// session_start() - start OR RESUME session
|
|
|
|
// If $_SESSION["userid"] is set - it counted as active login session
|
|
|
|
// If its not set - it counted as no login session
|
|
|
|
session_start();
|
2023-08-12 04:09:17 +05:30
|
|
|
|
|
|
|
$LOGGED_IN = false;
|
2023-09-08 01:35:23 +05:30
|
|
|
$THIS_USER = null; // ID of logged in user
|
2023-08-12 04:09:17 +05:30
|
|
|
|
2023-09-01 01:56:16 +05:30
|
|
|
if (session_status() === PHP_SESSION_ACTIVE && isset($_SESSION["userid"])) { // If there are active session
|
2023-08-12 04:09:17 +05:30
|
|
|
// Check if user still exist
|
2024-01-15 07:28:29 +05:30
|
|
|
$s = $db->prepare("SELECT id FROM users WHERE id = ?");
|
2023-08-12 04:09:17 +05:30
|
|
|
$s->bind_param("s", $_SESSION["userid"]);
|
|
|
|
$s->execute();
|
2023-08-16 09:04:01 +05:30
|
|
|
if (!(bool)$s->get_result()->fetch_assoc()) { // If not, then destroy session
|
2024-01-13 05:54:42 +05:30
|
|
|
AUTH_EndSession();
|
2023-08-12 04:09:17 +05:30
|
|
|
die("user id used in session does not exist");
|
|
|
|
}
|
|
|
|
$LOGGED_IN = true;
|
2023-09-08 01:35:23 +05:30
|
|
|
$THIS_USER = $_SESSION["userid"];
|
2023-09-01 01:56:16 +05:30
|
|
|
} elseif (session_status() === PHP_SESSION_DISABLED) { // If sessions are disabled
|
|
|
|
die("ERROR: please enable sessions in php config");
|
|
|
|
}
|
|
|
|
|
2024-01-15 07:28:29 +05:30
|
|
|
// HACK
|
2023-09-01 01:56:16 +05:30
|
|
|
if ($Config["debug"] && isset($_REQUEST["debug"])) { // If there are not any session and debug mode is on
|
|
|
|
// ATTENTION: FOR DEBUG PURPOSES ONLY!
|
|
|
|
if ($_REQUEST["debug"] == "drop") {
|
2024-01-13 05:54:42 +05:30
|
|
|
AUTH_EndSession();
|
2023-09-01 01:56:16 +05:30
|
|
|
die("session discarded");
|
|
|
|
}
|
|
|
|
$_SESSION["userid"] = intval($_REQUEST["debug"]);
|
|
|
|
print_r(["created_session" => $_SESSION]);
|
|
|
|
die();
|
2023-08-12 04:09:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
?>
|