1212use function implode ;
1313use function ini_get ;
1414use function ini_set ;
15+ use function is_string ;
1516use function preg_replace ;
1617use function session_abort ;
1718use function session_cache_limiter ;
@@ -38,20 +39,33 @@ class Session
3839 */
3940 private static function generateToken (): void
4041 {
42+ /**
43+ * Token which is used for authenticating access queries.
44+ * (we use "space PMA_token space" to prevent overwriting)
45+ */
4146 $ _SESSION [' PMA_token ' ] = Util::generateRandom (16 , true );
4247 $ _SESSION [' HMAC_secret ' ] = Util::generateRandom (16 );
4348
4449 /**
4550 * Check if token is properly generated (the generation can fail, for example
4651 * due to missing /dev/random for openssl).
4752 */
48- if (! empty ( $ _SESSION [ ' PMA_token ' ]) ) {
53+ if (self :: getToken () !== '' ) {
4954 return ;
5055 }
5156
5257 throw new SessionHandlerException ('Failed to generate random CSRF token! ' );
5358 }
5459
60+ public static function getToken (): string
61+ {
62+ if (isset ($ _SESSION [' PMA_token ' ]) && is_string ($ _SESSION [' PMA_token ' ])) {
63+ return $ _SESSION [' PMA_token ' ];
64+ }
65+
66+ return '' ;
67+ }
68+
5569 /**
5670 * tries to secure session from hijacking and fixation
5771 * should be called before login and after successful login
@@ -192,11 +206,7 @@ public static function setUp(Config $config, ErrorHandler $errorHandler): void
192206 self ::sessionFailed ($ errors );
193207 }
194208
195- /**
196- * Token which is used for authenticating access queries.
197- * (we use "space PMA_token space" to prevent overwriting)
198- */
199- if (! empty ($ _SESSION [' PMA_token ' ])) {
209+ if (self ::getToken () !== '' ) {
200210 return ;
201211 }
202212
@@ -219,7 +229,7 @@ public static function setUp(Config $config, ErrorHandler $errorHandler): void
219229 // A third cookie will be sent by session_regenerate_id() which will override these two
220230 session_start ();
221231
222- if (! empty ( $ _SESSION [ ' PMA_token ' ]) ) {
232+ if (self :: getToken () !== '' ) {
223233 return ;
224234 }
225235
0 commit comments