137 lines
4.3 KiB
PHP
137 lines
4.3 KiB
PHP
<?php
|
|
//$fs = new furatalogSessions();
|
|
|
|
//$fs->setSessionData("nsfw", true);
|
|
//$fs->getSessionData("nsfw");
|
|
|
|
class furatalogSessions {
|
|
public $cookieKey = "";
|
|
public $dataObj;
|
|
private mysqli $db;
|
|
private $cookie_duration = 30 * 24 * 60 * 60;
|
|
|
|
public function __construct() {
|
|
$this->dataObj = (object) array();
|
|
$this->connectDb();
|
|
$this->getCookieKey();
|
|
$this->getData();
|
|
}
|
|
|
|
public function setSessionData($varName, $content) {
|
|
$this->dataObj->{$varName} = $content;
|
|
$this->saveToDb();
|
|
$this->saveCookie();
|
|
}
|
|
|
|
public function getSessionData($varName) {
|
|
try {
|
|
return $this->dataObj->{$varName};
|
|
} catch (Exception $e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public function unsetSessionData($varName) {
|
|
unset($this->dataObj->{$varName});
|
|
$this->saveToDb();
|
|
$this->saveCookie();
|
|
|
|
}
|
|
|
|
public function issetSessionData($varName) {
|
|
return isset($this->dataObj->{$varName});
|
|
}
|
|
|
|
public function destroy() {
|
|
$this->deleteData($this->cookieKey);
|
|
$this->deleteCookie();
|
|
}
|
|
|
|
private function saveToDb() {
|
|
$jsonDataToSave = json_encode($this->dataObj, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
|
|
|
|
$tmpresult = $this->db->query("SELECT cookieKey FROM `sessions` WHERE cookieKey=\"" . $this->cookieKey . "\"");
|
|
if (!($tmpresult->num_rows > 0)) {
|
|
$this->db->query("INSERT INTO `sessions`(`cookieKey`, `lastused`, `data`) VALUES ('" . $this->cookieKey . "', now(),'" . $jsonDataToSave . "')");
|
|
} else {
|
|
$this->db->query("UPDATE `sessions` SET lastused=now(), data='" . $jsonDataToSave . "' WHERE cookieKey=\"" . $this->cookieKey . "\";");
|
|
}
|
|
}
|
|
|
|
private function saveCookie() {
|
|
setcookie("_cookieKey", $this->cookieKey, [
|
|
'expires' => time() + $this->cookie_duration,
|
|
'path' => '/',
|
|
'domain' => 'furatalog.xyz',
|
|
'secure' => true,
|
|
'httponly' => false,
|
|
'samesite' => 'Lax'
|
|
]);
|
|
}
|
|
|
|
private function connectDb() {
|
|
$this->db = new mysqli("localhost", "furatalog_admin_usr", "NR6tLk7c56bPT5[]", "furatalog");
|
|
}
|
|
|
|
private function getCookieKey() {
|
|
$this->cookieKey = isset($_COOKIE["_cookieKey"]) ? $_COOKIE["_cookieKey"] : "";
|
|
|
|
if ($this->cookieKey == "") {
|
|
$this->cookieKey = $this->generateKey();
|
|
}
|
|
}
|
|
|
|
private function getData() {
|
|
$tmpresult = $this->db->query("SELECT id, cookieKey, UNIX_TIMESTAMP(lastused) as lastused, data FROM sessions WHERE cookieKey=\"" . $this->cookieKey . "\"");
|
|
if ($tmpresult->num_rows > 0) {
|
|
$fetchedData = $tmpresult->fetch_assoc();
|
|
|
|
if ($fetchedData["lastused"] < strtotime('-30 days')) {
|
|
$this->deleteData($fetchedData["cookieKey"]);
|
|
$this->deleteCookie();
|
|
$this->getCookieKey();
|
|
} else {
|
|
$this->dataObj = json_decode($fetchedData["data"]);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function deleteCookie() {
|
|
setcookie("_cookieKey", "", [
|
|
'expires' => time()-3600,
|
|
'path' => '/',
|
|
'domain' => 'furatalog.xyz',
|
|
'secure' => true,
|
|
'httponly' => false,
|
|
'samesite' => 'Lax'
|
|
]);
|
|
}
|
|
|
|
private function deleteData($cookieKey) {
|
|
if ($cookieKey != "" && $cookieKey != null) {
|
|
$tmpresult = $this->db->query("DELETE FROM sessions WHERE cookieKey=\"" . $cookieKey . "\"");
|
|
}
|
|
}
|
|
|
|
private function generateKey() {
|
|
$isNotInDB = false;
|
|
|
|
do {
|
|
$length = 20;
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
$charactersLength = strlen($characters);
|
|
$randomString = '';
|
|
|
|
for ($i = 0; $i < $length; $i++) {
|
|
$randomString .= $characters[random_int(0, $charactersLength - 1)];
|
|
}
|
|
|
|
$tmpresult = $this->db->query("SELECT cookieKey FROM `sessions` WHERE cookieKey=\"" . $randomString . "\"");
|
|
if (!($tmpresult->num_rows > 0)) {
|
|
$isNotInDB = true;
|
|
}
|
|
} while (!$isNotInDB);
|
|
|
|
return $randomString;
|
|
}
|
|
} |