32 lines
940 B
PHP
32 lines
940 B
PHP
|
<?php
|
||
|
|
||
|
class Session
|
||
|
{
|
||
|
public static function createOrUpdate(array $data): void
|
||
|
{
|
||
|
App::$db->do("INSERT OR REPLACE INTO sessions (player_id, token, expires) VALUES (?, ?, DATETIME(CURRENT_TIMESTAMP, '+30 days'));", $data);
|
||
|
}
|
||
|
|
||
|
public static function get(int $id): array|false
|
||
|
{
|
||
|
$session = App::$db->do("SELECT * FROM sessions WHERE player_id = ? LIMIT 1;", [$id]);
|
||
|
return $session->fetch() ?: false;
|
||
|
}
|
||
|
|
||
|
public static function delete(int $id): void
|
||
|
{
|
||
|
App::$db->do("DELETE FROM sessions WHERE player_id = ?;", [$id]);
|
||
|
}
|
||
|
|
||
|
public static function validate(int $id, string $token): bool
|
||
|
{
|
||
|
$session = App::$db->do("SELECT * FROM sessions WHERE player_id = ? AND token = ? LIMIT 1;", [$id, $token]);
|
||
|
if ($session === false) return false;
|
||
|
$session = $session->fetch();
|
||
|
|
||
|
// if the current time is after the expires column, the token is invalid
|
||
|
if (strtotime($session['expires']) < time()) return false;
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
}
|