diff --git a/core/backend/Authentication/LegacyHandler/UserHandler.php b/core/backend/Authentication/LegacyHandler/UserHandler.php index 06c4f5090..bdfd1cc87 100644 --- a/core/backend/Authentication/LegacyHandler/UserHandler.php +++ b/core/backend/Authentication/LegacyHandler/UserHandler.php @@ -130,7 +130,7 @@ class UserHandler extends LegacyHandler */ public function getSessionLanguage(): string { - return $this->session->get('ui_language', ''); + return $this->requestStack->get('ui_language', ''); } /** @@ -142,7 +142,7 @@ class UserHandler extends LegacyHandler set_current_language($language); $this->close(); - $this->session->set('ui_language', $language); + $this->requestStack->set('ui_language', $language); } /** diff --git a/core/backend/Engine/LegacyHandler/LegacyHandler.php b/core/backend/Engine/LegacyHandler/LegacyHandler.php index 334aa8c78..f7885f705 100644 --- a/core/backend/Engine/LegacyHandler/LegacyHandler.php +++ b/core/backend/Engine/LegacyHandler/LegacyHandler.php @@ -34,7 +34,12 @@ use RuntimeException; use SugarApplication; use SugarController; use SugarThemeRegistry; +use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\SessionInterface; +use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage; use User; /** @@ -74,7 +79,7 @@ abstract class LegacyHandler /** * @var RequestStack */ - protected $session; + protected $requestStack; /** * LegacyHandler constructor. @@ -83,7 +88,7 @@ abstract class LegacyHandler * @param string $legacySessionName * @param string $defaultSessionName * @param LegacyScopeState $legacyScopeState - * @param RequestStack $session + * @param RequestStack $requestStack */ public function __construct( string $projectDir, @@ -91,14 +96,14 @@ abstract class LegacyHandler string $legacySessionName, string $defaultSessionName, LegacyScopeState $legacyScopeState, - RequestStack $session + RequestStack $requestStack ) { $this->projectDir = $projectDir; $this->legacyDir = $legacyDir; $this->legacySessionName = $legacySessionName; $this->defaultSessionName = $defaultSessionName; $this->state = $legacyScopeState; - $this->session = $session; + $this->requestStack = $requestStack; } /** @@ -294,36 +299,40 @@ abstract class LegacyHandler protected function startSymfonySession(): void { - if ($this->session->isStarted()) { - $this->session->save(); + $session = $this->getSession(); + + if ($session->isStarted()) { + $session->save(); session_write_close(); } - $this->session->setName($this->defaultSessionName); + $session->setName($this->defaultSessionName); if (isset($_COOKIE[$this->defaultSessionName])) { - $this->session->setId($_COOKIE[$this->defaultSessionName]); + $session->setId($_COOKIE[$this->defaultSessionName]); } - $this->session->start(); + $session->start(); } protected function startLegacySession(): void { - if ($this->session->isStarted()) { - $this->session->save(); + $session = $this->getSession(); + + if ($session->isStarted()) { + $session->save(); } if (session_status() === PHP_SESSION_ACTIVE) { return; } - $this->session->setName($this->legacySessionName); + $session->setName($this->legacySessionName); if (!isset($_COOKIE[$this->legacySessionName])) { $_COOKIE[$this->legacySessionName] = session_create_id(); } - $this->session->setId($_COOKIE[$this->legacySessionName]); - $this->session->start(); + $session->setId($_COOKIE[$this->legacySessionName]); + $session->start(); } /** @@ -341,4 +350,48 @@ abstract class LegacyHandler $app_strings = disable_translations($app_strings); } + + /** + * @return SessionInterface + */ + protected function getSession(): SessionInterface + { + $request = Request::createFromGlobals(); + $requestSession = null; + try { + $requestSession = $request->getSession(); + } catch (SessionNotFoundException $e) { + } + + $session = null; + + if($requestSession === null || session_id() == '' || !isset($_SESSION) || session_status() === PHP_SESSION_NONE) { + // session isn't started + session_start(); + + // Get Symfony to interface with this existing session + $session = new Session(new PhpBridgeSessionStorage()); + + // symfony will now interface with the existing PHP session + $session->start(); + $request->setSession($session); + } + + + $stack = $this->requestStack ?? null; + if ($stack === null) { + $stack = new RequestStack(); + } + + if ($requestSession === null) { + $stack->push($request); + return $session; + } + + + $session = $this->requestStack->getMainRequest()->getSession(); + + + return $session; + } } diff --git a/core/backend/Install/Command/BaseCommand.php b/core/backend/Install/Command/BaseCommand.php index da2bfa334..518685e96 100644 --- a/core/backend/Install/Command/BaseCommand.php +++ b/core/backend/Install/Command/BaseCommand.php @@ -54,7 +54,7 @@ abstract class BaseCommand extends Command /** * @var RequestStack */ - protected $session; + protected $requestStack; /** * @var string @@ -86,11 +86,11 @@ abstract class BaseCommand extends Command /** * @required - * @param RequestStack $session + * @param RequestStack $requestStack */ - public function setSession(RequestStack $session): void + public function setRequestStack(RequestStack $requestStack): void { - $this->session = $session; + $this->requestStack = $requestStack; } /** @@ -163,13 +163,13 @@ abstract class BaseCommand extends Command */ protected function startSession(): void { - if ($this->session->isStarted()) { + if ($this->requestStack->getSession()->isStarted()) { return; } - $this->session->setName($this->defaultSessionName); + $this->requestStack->getSession()->setName($this->defaultSessionName); - $this->session->start(); + $this->requestStack->getSession()->start(); } /** diff --git a/core/backend/Install/LegacyHandler/InstallHandler.php b/core/backend/Install/LegacyHandler/InstallHandler.php index a7ed80cad..02a18a2dc 100644 --- a/core/backend/Install/LegacyHandler/InstallHandler.php +++ b/core/backend/Install/LegacyHandler/InstallHandler.php @@ -78,17 +78,17 @@ class InstallHandler extends LegacyHandler * @param string $legacySessionName * @param string $defaultSessionName * @param LegacyScopeState $legacyScopeState - * @param RequestStack $session + * @param RequestStack $requestStack * @param LoggerInterface $logger */ public function __construct( - string $projectDir, - string $legacyDir, - string $legacySessionName, - string $defaultSessionName, + string $projectDir, + string $legacyDir, + string $legacySessionName, + string $defaultSessionName, LegacyScopeState $legacyScopeState, - RequestStack $session, - LoggerInterface $logger + RequestStack $requestStack, + LoggerInterface $logger ) { parent::__construct( @@ -97,7 +97,7 @@ class InstallHandler extends LegacyHandler $legacySessionName, $defaultSessionName, $legacyScopeState, - $session + $requestStack ); $this->legacyDir = $legacyDir; $this->logger = $logger;