From 5ba3f6ef77410f03341aecc111058b9ee82d56b4 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 18 Feb 2024 15:19:40 +0300 Subject: sessions to pgsql --- include/sessions.php | 72 +++++++++++++++++++++++++++++++++++++++++++++ sql/pgsql/migrations/2.sql | 5 ++++ sql/pgsql/schema.sql | 6 ++++ sql/sqlite/migrations/2.sql | 1 + 4 files changed, 84 insertions(+) create mode 100644 sql/pgsql/migrations/2.sql create mode 100644 sql/sqlite/migrations/2.sql diff --git a/include/sessions.php b/include/sessions.php index 8fbab1d..556edb9 100644 --- a/include/sessions.php +++ b/include/sessions.php @@ -59,6 +59,78 @@ register_shutdown_function('session_write_close'); + if (Config::get(Config::DB_TYPE) == 'pgsql') { + + function epube_open(string $savePath, string $sessionName): bool { + return true; + } + + function epube_read(string $id): string { + global $session_expire; + + $sth = \Db::pdo()->prepare("SELECT data FROM epube_sessions WHERE id=?"); + $sth->execute([$id]); + + if ($row = $sth->fetch()) { + return base64_decode($row["data"]); + + } else { + $expire = time() + $session_expire; + + $sth = \Db::pdo()->prepare("INSERT INTO epube_sessions (id, data, expire) + VALUES (?, '', ?)"); + $sth->execute([$id, $expire]); + + return ""; + + } + + } + + function epube_write(string $id, string $data): bool { + global $session_expire; + + $data = base64_encode($data); + $expire = time() + $session_expire; + + $sth = \Db::pdo()->prepare("SELECT id FROM epube_sessions WHERE id=?"); + $sth->execute([$id]); + + if ($sth->fetch()) { + $sth = \Db::pdo()->prepare("UPDATE epube_sessions SET data=?, expire=? WHERE id=?"); + $sth->execute([$data, $expire, $id]); + } else { + $sth = \Db::pdo()->prepare("INSERT INTO epube_sessions (id, data, expire) + VALUES (?, ?, ?)"); + $sth->execute([$id, $data, $expire]); + } + + return true; + } + + function epube_close(): bool { + return true; + } + + function epube_destroy(string $id): bool { + $sth = \Db::pdo()->prepare("DELETE FROM epube_sessions WHERE id = ?"); + $sth->execute([$id]); + + return true; + } + + function epube_gc(int $lifetime): bool { + \Db::pdo()->query("DELETE FROM epube_sessions WHERE expire < " . time()); + + return true; + } + + session_set_save_handler('epube_open', + 'epube_close', 'epube_read', + 'epube_write', 'epube_destroy', + 'epube_gc'); // @phpstan-ignore-line + } + if (isset($_COOKIE[session_name()])) { if (session_status() != PHP_SESSION_ACTIVE) session_start(); diff --git a/sql/pgsql/migrations/2.sql b/sql/pgsql/migrations/2.sql new file mode 100644 index 0000000..a354573 --- /dev/null +++ b/sql/pgsql/migrations/2.sql @@ -0,0 +1,5 @@ +create table if not exists epube_sessions (id varchar(250) not null primary key, + data text, + expire integer not null); + +create index epube_sessions_expire_index on epube_sessions(expire); diff --git a/sql/pgsql/schema.sql b/sql/pgsql/schema.sql index 3616f06..2d02190 100644 --- a/sql/pgsql/schema.sql +++ b/sql/pgsql/schema.sql @@ -21,3 +21,9 @@ create table if not exists epube_favorites( id serial not null primary key, bookid bigint not null, owner varchar(200) not null references epube_users(username) on delete cascade); + +create table if not exists epube_sessions (id varchar(250) not null primary key, + data text, + expire integer not null); + +create index epube_sessions_expire_index on epube_sessions(expire); diff --git a/sql/sqlite/migrations/2.sql b/sql/sqlite/migrations/2.sql new file mode 100644 index 0000000..e0298b7 --- /dev/null +++ b/sql/sqlite/migrations/2.sql @@ -0,0 +1 @@ +select true; -- cgit v1.2.3