SidResolver.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * SID resolver
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Session;
  9. use Magento\Framework\App\State;
  10. /**
  11. * Class SidResolver
  12. */
  13. class SidResolver implements SidResolverInterface
  14. {
  15. /**
  16. * Config path for flag whether use SID on frontend
  17. */
  18. const XML_PATH_USE_FRONTEND_SID = 'web/session/use_frontend_sid';
  19. /**
  20. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  21. */
  22. protected $scopeConfig;
  23. /**
  24. * @var \Magento\Framework\UrlInterface
  25. */
  26. protected $urlBuilder;
  27. /**
  28. * @var \Magento\Framework\App\RequestInterface
  29. */
  30. protected $request;
  31. /**
  32. * @var array
  33. */
  34. protected $sidNameMap;
  35. /**
  36. * Use session var instead of SID for session in URL
  37. *
  38. * @var bool
  39. */
  40. protected $_useSessionVar = false;
  41. /**
  42. * Use session in URL flag
  43. *
  44. * @var bool|null
  45. * @see \Magento\Framework\UrlInterface
  46. */
  47. protected $_useSessionInUrl;
  48. /**
  49. * @var string
  50. */
  51. protected $_scopeType;
  52. /**
  53. * @var State
  54. */
  55. private $appState;
  56. /**
  57. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  58. * @param \Magento\Framework\UrlInterface $urlBuilder
  59. * @param \Magento\Framework\App\RequestInterface $request
  60. * @param string $scopeType
  61. * @param array $sidNameMap
  62. * @param State|null $appState
  63. */
  64. public function __construct(
  65. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  66. \Magento\Framework\UrlInterface $urlBuilder,
  67. \Magento\Framework\App\RequestInterface $request,
  68. $scopeType,
  69. array $sidNameMap = [],
  70. State $appState = null
  71. ) {
  72. $this->scopeConfig = $scopeConfig;
  73. $this->urlBuilder = $urlBuilder;
  74. $this->request = $request;
  75. $this->sidNameMap = $sidNameMap;
  76. $this->_scopeType = $scopeType;
  77. $this->appState = $appState ?: \Magento\Framework\App\ObjectManager::getInstance()->get(State::class);
  78. }
  79. /**
  80. * Get Sid
  81. *
  82. * @param SessionManagerInterface $session
  83. *
  84. * @return string|null
  85. * @throws \Magento\Framework\Exception\LocalizedException
  86. */
  87. public function getSid(SessionManagerInterface $session)
  88. {
  89. if ($this->appState->getAreaCode() !== \Magento\Framework\App\Area::AREA_FRONTEND) {
  90. return null;
  91. }
  92. $sidKey = null;
  93. $useSidOnFrontend = $this->getUseSessionInUrl();
  94. if ($useSidOnFrontend && $this->request->getQuery(
  95. $this->getSessionIdQueryParam($session),
  96. false
  97. ) && $this->urlBuilder->isOwnOriginUrl()
  98. ) {
  99. $sidKey = $this->request->getQuery($this->getSessionIdQueryParam($session));
  100. }
  101. return $sidKey;
  102. }
  103. /**
  104. * Get session id query param
  105. *
  106. * @param SessionManagerInterface $session
  107. * @return string
  108. */
  109. public function getSessionIdQueryParam(SessionManagerInterface $session)
  110. {
  111. $sessionName = $session->getName();
  112. if ($sessionName && isset($this->sidNameMap[$sessionName])) {
  113. return $this->sidNameMap[$sessionName];
  114. }
  115. return self::SESSION_ID_QUERY_PARAM;
  116. }
  117. /**
  118. * Set use session var instead of SID for URL
  119. *
  120. * @param bool $var
  121. * @return $this
  122. */
  123. public function setUseSessionVar($var)
  124. {
  125. $this->_useSessionVar = (bool)$var;
  126. return $this;
  127. }
  128. /**
  129. * Retrieve use flag session var instead of SID for URL
  130. *
  131. * @return bool
  132. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  133. */
  134. public function getUseSessionVar()
  135. {
  136. return $this->_useSessionVar;
  137. }
  138. /**
  139. * Set Use session in URL flag
  140. *
  141. * @param bool $flag
  142. * @return $this
  143. */
  144. public function setUseSessionInUrl($flag = true)
  145. {
  146. $this->_useSessionInUrl = (bool)$flag;
  147. return $this;
  148. }
  149. /**
  150. * Retrieve use session in URL flag.
  151. *
  152. * @return bool
  153. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  154. */
  155. public function getUseSessionInUrl()
  156. {
  157. if ($this->_useSessionInUrl === null) {
  158. //Using config value by default, can be overridden by using the
  159. //setter.
  160. $this->_useSessionInUrl = $this->scopeConfig->isSetFlag(
  161. self::XML_PATH_USE_FRONTEND_SID,
  162. $this->_scopeType
  163. );
  164. }
  165. return $this->_useSessionInUrl;
  166. }
  167. }