Skip to content
Snippets Groups Projects
Commit 1b7714d2 authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

adjust session handling so that it corresponds to the previous behaviour, fixes #5038

Closes #5038

Merge request studip/studip!3791
parent 5eecc9a1
No related branches found
No related tags found
No related merge requests found
...@@ -22,6 +22,6 @@ class SessionGcJob extends CronJob ...@@ -22,6 +22,6 @@ class SessionGcJob extends CronJob
public function execute($last_result, $parameters = []) public function execute($last_result, $parameters = [])
{ {
return sess()->doGarbageCollect(); sess()->doGarbageCollect();
} }
} }
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
*/ */
namespace Studip\Session; namespace Studip\Session;
class Manager class Manager
{ {
public const STATE_UNKNOWN = false; public const STATE_UNKNOWN = false;
...@@ -53,21 +52,23 @@ class Manager ...@@ -53,21 +52,23 @@ class Manager
{ {
if (!$this->isStarted()) { if (!$this->isStarted()) {
ini_set('session.use_strict_mode', 1); ini_set('session.use_strict_mode', 1);
$current = session_get_cookie_params();
$lifetime = (int) ($this->options['lifetime'] ?: $current['lifetime']); session_set_cookie_params([
$path = $this->options['path'] ?: $current['path']; 'lifetime' => 0,
$domain = $this->options['domain'] ?: $current['domain']; 'path' => $this->getCookieParam('path'),
$samesite = $this->options['samesite'] ?: $current['samesite']; 'domain' => $this->getCookieParam('domain'),
$secure = (bool) $this->options['secure']; 'secure' => (bool) $this->getCookieParam('secure', false),
$httponly = (bool) $this->options['httponly']; 'samesite' => $this->getCookieParam('samesite'),
'httponly' => (bool) $this->getCookieParam('httponly', false),
]);
session_set_cookie_params(compact('lifetime', 'path', 'domain', 'secure', 'samesite', 'httponly'));
session_name($this->options['name']); session_name($this->options['name']);
session_cache_limiter('nocache'); session_cache_limiter('nocache');
session_set_save_handler($this->handler, true); session_set_save_handler($this->handler, true);
session_start(); session_start([
'gc_maxlifetime' => (int) $this->getCookieParam('lifetime'),
]);
} }
} }
...@@ -103,6 +104,25 @@ class Manager ...@@ -103,6 +104,25 @@ class Manager
return $this->options['name']; return $this->options['name'];
} }
/**
* Returns the value for the given cookie parameter. The value is taken
* from the configured options array (or from the current session
* configuration in php).
*
* If no value is found, null is retuned.
*/
public function getCookieParam(string $key, bool $from_config = true): mixed
{
$value = $this->options[$key] ?? null;
if ($from_config) {
$current = session_get_cookie_params();
$value = $value ?: $current[$key] ?? null;
}
return $value;
}
public function destroy(): void public function destroy(): void
{ {
if (!$this->isStarted()) { if (!$this->isStarted()) {
...@@ -174,12 +194,8 @@ class Manager ...@@ -174,12 +194,8 @@ class Manager
/** /**
* returns a SessionDecoder object containing the session variables * returns a SessionDecoder object containing the session variables
* for the given session id * for the given session id
*
* @static
* @param string $sid a session id
* @return \SessionDecoder
*/ */
public function getSessionVars($sid): \SessionDecoder public function getSessionVars(string $sid): \SessionDecoder
{ {
$data = $this->handler->read($sid); $data = $this->handler->read($sid);
return new \SessionDecoder($data); return new \SessionDecoder($data);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment