2021-07-29 13:52:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
2022-01-14 19:45:30 +00:00
|
|
|
/*
|
2022-02-06 20:37:02 +00:00
|
|
|
This file is a part of myTinyTodo.
|
|
|
|
|
(C) Copyright 2021-2022 Max Pozdeev <maxpozdeev@gmail.com>
|
|
|
|
|
Licensed under the GNU GPL version 2 or any later. See file COPYRIGHT for details.
|
2022-01-14 19:45:30 +00:00
|
|
|
*/
|
|
|
|
|
|
2021-07-29 13:52:44 +00:00
|
|
|
class MTTSessionHandler implements SessionHandlerInterface
|
|
|
|
|
{
|
2022-02-06 20:37:02 +00:00
|
|
|
/**
|
|
|
|
|
* @var Database_Abstract
|
|
|
|
|
*/
|
|
|
|
|
private $db;
|
2021-07-29 13:52:44 +00:00
|
|
|
|
2022-02-06 20:37:02 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function open($path, $name): bool
|
|
|
|
|
{
|
|
|
|
|
$this->db = DBConnection::instance();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-07-29 13:52:44 +00:00
|
|
|
|
2022-02-06 20:37:02 +00:00
|
|
|
/** @return bool */
|
|
|
|
|
public function close(): bool
|
2021-07-29 13:52:44 +00:00
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-06 20:37:02 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $id
|
|
|
|
|
* @return string
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
#[\ReturnTypeWillChange]
|
|
|
|
|
public function read($id)
|
|
|
|
|
{
|
|
|
|
|
// read session data if not expired
|
|
|
|
|
$time = time();
|
|
|
|
|
$expire = $time;
|
2022-09-08 20:16:31 +00:00
|
|
|
$r = $this->db->sq("SELECT data,last_access,expires FROM {$this->db->prefix}sessions WHERE id = ?", [$id]);
|
2022-02-06 20:37:02 +00:00
|
|
|
if ( is_null($r) ) return '';
|
2022-09-08 20:16:31 +00:00
|
|
|
if ( (int)$r[2] < $time) {
|
|
|
|
|
// maybe regenerate id?
|
|
|
|
|
$r[0] = '';
|
|
|
|
|
}
|
2021-07-29 13:52:44 +00:00
|
|
|
|
2022-02-06 20:37:02 +00:00
|
|
|
// update last access time and set expires in 14 days
|
|
|
|
|
// refresh once in a second
|
|
|
|
|
if ( $r[1] < $time ) {
|
|
|
|
|
$expire = $time + 14 * 86400;
|
|
|
|
|
$this->db->ex("UPDATE {$this->db->prefix}sessions SET last_access=?,expires=? WHERE id = ?",
|
|
|
|
|
array($time, $expire, $id) );
|
|
|
|
|
}
|
|
|
|
|
return $r[0];
|
|
|
|
|
}
|
2021-07-29 13:52:44 +00:00
|
|
|
|
2022-02-06 20:37:02 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $id
|
|
|
|
|
* @param string $data
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function write($id, $data): bool
|
|
|
|
|
{
|
2022-08-27 18:07:31 +00:00
|
|
|
$exists = $this->db->sq("SELECT COUNT(*) FROM {$this->db->prefix}sessions WHERE id = ?", [$id]);
|
2022-02-06 20:37:02 +00:00
|
|
|
if (!$exists) {
|
|
|
|
|
// Create new session with 14 days lifetime
|
|
|
|
|
$expire = time() + 14 * 86400;
|
|
|
|
|
$this->db->ex("INSERT INTO {$this->db->prefix}sessions (id,data,expires) VALUES (?,?,?)",
|
|
|
|
|
array($id, $data, $expire) );
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// Update existing session
|
|
|
|
|
$this->db->ex("UPDATE {$this->db->prefix}sessions SET data = ? WHERE id = ?",
|
|
|
|
|
array($data, $id) );
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-07-29 13:52:44 +00:00
|
|
|
|
2022-02-06 20:37:02 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $id
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function destroy($id): bool
|
|
|
|
|
{
|
2022-08-27 18:07:31 +00:00
|
|
|
$this->db->ex("DELETE FROM {$this->db->prefix}sessions WHERE id = ?", [$id]);
|
2022-02-06 20:37:02 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2021-07-29 13:52:44 +00:00
|
|
|
|
2022-02-06 20:37:02 +00:00
|
|
|
/**
|
|
|
|
|
* @param int $max_lifetime
|
|
|
|
|
* @return int|false
|
|
|
|
|
*/
|
|
|
|
|
#[\ReturnTypeWillChange]
|
|
|
|
|
public function gc($max_lifetime)
|
|
|
|
|
{
|
|
|
|
|
// We ignore php runtime 'session.gc_maxlifetime'
|
|
|
|
|
$expire = time();
|
2022-08-27 18:07:31 +00:00
|
|
|
$this->db->ex("DELETE FROM {$this->db->prefix}sessions WHERE expires < $expire");
|
|
|
|
|
return $this->db->affected();
|
2022-02-06 20:37:02 +00:00
|
|
|
}
|
2021-07-29 13:52:44 +00:00
|
|
|
}
|
|
|
|
|
|