Skip to content

Commit d42911c

Browse files
committed
RFC: Reopen sessions if we need to write to them
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent 8a3f8b0 commit d42911c

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

lib/base.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ public static function initSession() {
453453
}
454454

455455
$session->set('LAST_ACTIVITY', time());
456+
$session->close();
456457
}
457458

458459
/**

lib/private/AppFramework/Middleware/SessionMiddleware.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public function __construct(ControllerMethodReflector $reflector,
5151
*/
5252
public function beforeController($controller, $methodName) {
5353
$useSession = $this->reflector->hasAnnotation('UseSession');
54-
if (!$useSession) {
55-
$this->session->close();
54+
if ($useSession) {
55+
$this->session->reopen();
5656
}
5757
}
5858

lib/private/Session/CryptoSessionData.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,10 @@ protected function initializeSession() {
9797
* @param mixed $value
9898
*/
9999
public function set(string $key, $value) {
100+
$this->reopen();
100101
$this->sessionValues[$key] = $value;
101102
$this->isModified = true;
103+
$this->close();
102104
}
103105

104106
/**
@@ -149,6 +151,10 @@ public function clear() {
149151
$this->session->clear();
150152
}
151153

154+
public function reopen() {
155+
$this->session->reopen();
156+
}
157+
152158
/**
153159
* Wrapper around session_regenerate_id
154160
*

lib/private/Session/Internal.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ public function __construct(string $name) {
6868
* @param integer $value
6969
*/
7070
public function set(string $key, $value) {
71-
$this->validateSession();
71+
$reopened = $this->reopen();
7272
$_SESSION[$key] = $value;
73+
if ($reopened) {
74+
$this->close();
75+
}
7376
}
7477

7578
/**
@@ -101,6 +104,7 @@ public function remove(string $key) {
101104
}
102105

103106
public function clear() {
107+
$this->reopen();
104108
$this->invoke('session_unset');
105109
$this->regenerateId();
106110
$this->startSession(true);
@@ -120,6 +124,7 @@ public function close() {
120124
* @return void
121125
*/
122126
public function regenerateId(bool $deleteOldSession = true, bool $updateToken = false) {
127+
$this->reopen();
123128
$oldId = null;
124129

125130
if ($updateToken) {
@@ -172,7 +177,13 @@ public function getId(): string {
172177
* @throws \Exception
173178
*/
174179
public function reopen() {
175-
throw new \Exception('The session cannot be reopened - reopen() is ony to be used in unit testing.');
180+
if ($this->sessionClosed) {
181+
$this->startSession();
182+
$this->sessionClosed = false;
183+
return true;
184+
}
185+
186+
return false;
176187
}
177188

178189
/**

lib/public/ISession.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ public function remove(string $key);
8282
* @since 6.0.0
8383
*/
8484
public function clear();
85+
86+
public function reopen();
8587

8688
/**
8789
* Close the session and release the lock

0 commit comments

Comments
 (0)