Skip to content

Commit f8650f3

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

5 files changed

Lines changed: 32 additions & 4 deletions

File tree

lib/base.php

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

458458
$session->set('LAST_ACTIVITY', time());
459+
$session->close();
459460
}
460461

461462
/**

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: 8 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
/**
@@ -131,9 +133,11 @@ public function exists(string $key): bool {
131133
* @param string $key
132134
*/
133135
public function remove(string $key) {
136+
$this->reopen();
134137
$this->isModified = true;
135138
unset($this->sessionValues[$key]);
136139
$this->session->remove(self::encryptedSessionName);
140+
$this->close();
137141
}
138142

139143
/**
@@ -149,6 +153,10 @@ public function clear() {
149153
$this->session->clear();
150154
}
151155

156+
public function reopen() {
157+
$this->session->reopen();
158+
}
159+
152160
/**
153161
* Wrapper around session_regenerate_id
154162
*

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 only 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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ public function remove(string $key);
8383
*/
8484
public function clear();
8585

86+
/**
87+
* Reopen a session for writing again
88+
*
89+
* @return mixed
90+
* @since 25.0.0
91+
*/
92+
public function reopen();
93+
8694
/**
8795
* Close the session and release the lock
8896
* @since 7.0.0

0 commit comments

Comments
 (0)