17 use Symfony\Component\HttpFoundation\Cookie;
82 session_set_save_handler([$this,
'open'], [$this,
'close'], [$this,
'read'], [$this,
'write'], [$this,
'destroy'], [$this,
'gc']);
83 session_save_path($sessionSavePath);
84 session_name($this->cookieName);
85 ini_set(
'session.cookie_httponly',
true);
86 if ($this->hasSameSiteCookieSupport()) {
87 ini_set(
'session.cookie_samesite', Cookie::SAMESITE_STRICT);
89 ini_set(
'session.cookie_path', (
string)GeneralUtility::getIndpEnv(
'TYPO3_SITE_PATH'));
91 ini_set(
'session.gc_probability', (
string)100);
92 ini_set(
'session.gc_divisor', (
string)100);
93 ini_set(
'session.gc_maxlifetime', (
string)$this->expireTimeInMinutes * 2 * 60);
95 $sessionCreationError =
'Error: session.auto-start is enabled.<br />';
96 $sessionCreationError .=
'The PHP option session.auto-start is enabled. Disable this option in php.ini or .htaccess:<br />';
97 $sessionCreationError .=
'<pre>php_value session.auto_start Off</pre>';
98 throw new \TYPO3\CMS\Install\Exception($sessionCreationError, 1294587485);
100 if (session_status() === PHP_SESSION_ACTIVE) {
101 $sessionCreationError =
'Session already started by session_start().<br />';
102 $sessionCreationError .=
'Make sure no installed extension is starting a session in its ext_localconf.php or ext_tables.php.';
103 throw new \TYPO3\CMS\Install\Exception($sessionCreationError, 1294587486);
106 if (!$this->hasSameSiteCookieSupport()) {
107 $this->resendCookieHeader([$this->cookieName]);
119 if (empty(
$GLOBALS[
'TYPO3_CONF_VARS'][
'SYS'][
'encryptionKey'])) {
120 throw new \TYPO3\CMS\Install\Exception(
121 'No encryption key set to secure session',
125 $sessionSavePath = sprintf(
126 $this->basePath . $this->sessionPath,
127 GeneralUtility::hmac(
'session:' .
$GLOBALS[
'TYPO3_CONF_VARS'][
'SYS'][
'encryptionKey'])
130 return $sessionSavePath;
142 if (!is_dir($sessionSavePath)) {
144 GeneralUtility::mkdir_deep($sessionSavePath);
145 }
catch (\RuntimeException $exception) {
146 throw new \TYPO3\CMS\Install\Exception(
153 <IfModule !mod_authz_core.c>
160 <IfModule mod_authz_core.c>
164 GeneralUtility::writeFile($sessionSavePath .
'/.htaccess', $htaccessContent);
165 $indexContent =
'<!DOCTYPE html>';
166 $indexContent .=
'<html><head><title></title><meta http-equiv=Refresh Content="0; Url=../../"/>';
167 $indexContent .=
'</head></html>';
168 GeneralUtility::writeFile($sessionSavePath .
'/index.html', $indexContent);
179 $_SESSION[
'active'] =
true;
198 $_SESSION[
'active'] =
false;
208 session_regenerate_id();
209 if (!$this->hasSameSiteCookieSupport()) {
210 $this->resendCookieHeader([$this->cookieName]);
222 return $_SESSION[
'active'] ===
true;
245 if (empty(
$GLOBALS[
'TYPO3_CONF_VARS'][
'SYS'][
'encryptionKey'])) {
246 throw new \TYPO3\CMS\Install\Exception(
247 'No encryption key set to secure session',
254 return md5(
$GLOBALS[
'TYPO3_CONF_VARS'][
'SYS'][
'encryptionKey'] .
'|' . $sessionId);
265 $_SESSION[
'authorized'] =
true;
266 $_SESSION[
'lastSessionId'] = time();
267 $_SESSION[
'tstamp'] = time();
268 $_SESSION[
'expires'] = time() + $this->expireTimeInMinutes * 60;
279 $_SESSION[
'authorized'] =
true;
280 $_SESSION[
'lastSessionId'] = time();
281 $_SESSION[
'tstamp'] = time();
282 $_SESSION[
'expires'] = time() + $this->expireTimeInMinutes * 60;
283 $_SESSION[
'isBackendSession'] =
true;
295 if (!$_SESSION[
'authorized']) {
298 if ($_SESSION[
'expires'] < time()) {
312 if (!$_SESSION[
'authorized'] || !$_SESSION[
'isBackendSession']) {
315 if ($_SESSION[
'expires'] < time()) {
331 if (!$_SESSION[
'authorized']) {
335 if ($_SESSION[
'expires'] < time()) {
349 $_SESSION[
'tstamp'] = time();
350 $_SESSION[
'expires'] = time() + $this->expireTimeInMinutes * 60;
351 if (time() > $_SESSION[
'lastSessionId'] + $this->regenerateSessionIdTime * 60) {
353 $_SESSION[
'lastSessionId'] = time();
365 if (!is_array($_SESSION[
'messages'])) {
366 $_SESSION[
'messages'] = [];
368 $_SESSION[
'messages'][] = $message;
379 if (is_array($_SESSION[
'messages'])) {
380 $messages = $_SESSION[
'messages'];
382 $_SESSION[
'messages'] = [];
411 public function open($savePath, $sessionName)
432 public function read($id)
436 if (file_exists($sessionFile)) {
437 if ($fd = fopen($sessionFile,
'rb')) {
438 $lockres = flock($fd, LOCK_SH);
440 $length = filesize($sessionFile);
442 $content = fread($fd, $length);
452 $this->
write($id, $content);
464 public function write($id, $sessionData)
468 $changePermissions = !@is_file($sessionFile);
469 if ($fd = fopen($sessionFile,
'cb')) {
470 if (flock($fd, LOCK_EX)) {
472 $res = fwrite($fd, $sessionData);
473 if ($res !==
false) {
481 if ($changePermissions) {
482 GeneralUtility::fixPermissions($sessionFile);
487 'Session file not writable. Please check permission on ' .
504 return @unlink($sessionFile);
513 public function gc($maxLifeTime)
516 $files = glob($sessionSavePath .
'/hash_*');
517 if (!is_array($files)) {
520 foreach ($files as $filename) {
521 if (filemtime($filename) + $this->expireTimeInMinutes * 60 < time()) {
541 session_write_close();
562 return filter_var(ini_get($configOption), FILTER_VALIDATE_BOOLEAN, [FILTER_REQUIRE_SCALAR, FILTER_NULL_ON_FAILURE]);