about summary refs log tree commit diff stats
path: root/include
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2024-02-18 15:19:40 +0300
committerAndrew Dolgov <fox@fakecake.org>2024-02-18 15:19:40 +0300
commit5ba3f6ef77410f03341aecc111058b9ee82d56b4 (patch)
tree23cc19dde7b4d12a2a88bff32183393c910f29be /include
parenta7e5b30e034d7b656a7f1a58b40fff47b38b0dd9 (diff)
downloadthe-epube-5ba3f6ef77410f03341aecc111058b9ee82d56b4.tar.gz
the-epube-5ba3f6ef77410f03341aecc111058b9ee82d56b4.tar.xz
sessions to pgsql
Diffstat (limited to 'include')
-rw-r--r--include/sessions.php72
1 files changed, 72 insertions, 0 deletions
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();