diff --git a/gatherling/Auth/Session.php b/gatherling/Auth/Session.php new file mode 100644 index 00000000..37c5ad41 --- /dev/null +++ b/gatherling/Auth/Session.php @@ -0,0 +1,74 @@ += NOW()'; + $args = ['token' => $token]; + $details = DB::value($sql, $args); + return $details ? json_decode($details, true) : []; + } + + private static function save(): void + { + if (isset($_COOKIE['remember_me'])) { + $token = $_COOKIE['remember_me']; + } else { + $token = bin2hex(random_bytes(32)); + } + // Force to object so that we get '{}' instead of '[]' when empty + $details = json_encode((object) $_SESSION); + $expiry = time() + self::$LIFETIME; + $sql = ' + INSERT INTO + sessions (token, details, expiry) + VALUES + (:token, :details, FROM_UNIXTIME(:expiry)) + ON DUPLICATE KEY UPDATE + details = :details, + expiry = FROM_UNIXTIME(:expiry)'; + $args = [ + 'token' => $token, + 'details' => $details, + 'expiry' => $expiry, + ]; + DB::execute($sql, $args); + setcookie('remember_me', $token, $expiry, '/'); + $sql = 'DELETE FROM sessions WHERE expiry < NOW()'; + DB::execute($sql); + } +} diff --git a/gatherling/Data/sql/migrations/52.sql b/gatherling/Data/sql/migrations/54.sql similarity index 100% rename from gatherling/Data/sql/migrations/52.sql rename to gatherling/Data/sql/migrations/54.sql diff --git a/gatherling/Data/sql/migrations/55.sql b/gatherling/Data/sql/migrations/55.sql new file mode 100644 index 00000000..6bb80622 --- /dev/null +++ b/gatherling/Data/sql/migrations/55.sql @@ -0,0 +1,6 @@ +CREATE TABLE IF NOT EXISTS sessions ( + id INT AUTO_INCREMENT PRIMARY KEY, + token VARCHAR(64) UNIQUE NOT NULL, + details TEXT NOT NULL, + expiry TIMESTAMP NOT NULL +); diff --git a/gatherling/admin/db-upgrade.php b/gatherling/admin/db-upgrade.php index d8488210..6d5e4558 100644 --- a/gatherling/admin/db-upgrade.php +++ b/gatherling/admin/db-upgrade.php @@ -11,7 +11,6 @@ function main(): void { Setup::setupDatabase(); - Setup::setupTestDatabase(); echo 'done'; } diff --git a/gatherling/config.php.docker b/gatherling/config.php.docker index a8b521aa..8042e806 100644 --- a/gatherling/config.php.docker +++ b/gatherling/config.php.docker @@ -24,9 +24,6 @@ $CONFIG['style'] = "ChandraNeue"; # A description for the ical calendar which is accessible at calendar.php $CONFIG['calendar_description'] = "a description for the events calendar"; -# How long to store session cookies in seconds -$CONFIG['cookie_lifetime'] = 5184000; - # API Key for Brevo email sending (password reset) $CONFIG['brevo_api_key'] = 'xkeysib-foobar-baz'; diff --git a/gatherling/config.php.example b/gatherling/config.php.example index 4e19ae47..6252f497 100644 --- a/gatherling/config.php.example +++ b/gatherling/config.php.example @@ -29,9 +29,6 @@ $CONFIG['style'] = "ChandraNeue"; # A description for the ical calendar which is accessible at calendar.php $CONFIG['calendar_description'] = "a description for the events calendar"; -# How long to store session cookies in seconds -$CONFIG['cookie_lifetime'] = 5184000; - # API Key for Brevo email sending (password reset) $CONFIG['brevo_api_key'] = 'xkeysib-foobar-baz'; diff --git a/gatherling/lib.php b/gatherling/lib.php index 545aa2fb..6911fde7 100644 --- a/gatherling/lib.php +++ b/gatherling/lib.php @@ -1,21 +1,17 @@