77 */
88namespace Magento \Store \App \Response ;
99
10+ use Laminas \Uri \Uri ;
11+ use Magento \Framework \App \ActionInterface ;
12+ use Magento \Framework \App \Area ;
13+ use Magento \Framework \App \Config \ScopeConfigInterface ;
1014use Magento \Framework \App \ObjectManager ;
15+ use Magento \Framework \App \RequestInterface ;
16+ use Magento \Framework \App \Response \RedirectInterface ;
17+ use Magento \Framework \App \ResponseInterface ;
18+ use Magento \Framework \App \State ;
19+ use Magento \Framework \Encryption \UrlCoder ;
20+ use Magento \Framework \Exception \NoSuchEntityException ;
21+ use Magento \Framework \Session \SessionManagerInterface ;
22+ use Magento \Framework \Session \SidResolverInterface ;
23+ use Magento \Framework \UrlInterface ;
24+ use Magento \Store \Model \ScopeInterface ;
25+ use Magento \Store \Model \StoreManagerInterface ;
1126
1227/**
1328 * Class Redirect computes redirect urls responses.
1429 * @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
30+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1531 */
16- class Redirect implements \ Magento \ Framework \ App \ Response \ RedirectInterface
32+ class Redirect implements RedirectInterface
1733{
34+ private const XML_PATH_USE_CUSTOM_ADMIN_URL = 'admin/url/use_custom ' ;
35+ private const XML_PATH_CUSTOM_ADMIN_URL = 'admin/url/custom ' ;
36+
1837 /**
19- * @var \Magento\Framework\App\ RequestInterface
38+ * @var RequestInterface
2039 */
2140 protected $ _request ;
2241
2342 /**
24- * @var \Magento\Store\Model\ StoreManagerInterface
43+ * @var StoreManagerInterface
2544 */
2645 protected $ _storeManager ;
2746
2847 /**
29- * @var \Magento\Framework\Encryption\ UrlCoder
48+ * @var UrlCoder
3049 */
3150 protected $ _urlCoder ;
3251
3352 /**
34- * @var \Magento\Framework\Session\ SessionManagerInterface
53+ * @var SessionManagerInterface
3554 */
3655 protected $ _session ;
3756
3857 /**
39- * @var \Magento\Framework\Session\ SidResolverInterface
58+ * @var SidResolverInterface
4059 */
4160 protected $ _sidResolver ;
4261
@@ -46,36 +65,51 @@ class Redirect implements \Magento\Framework\App\Response\RedirectInterface
4665 protected $ _canUseSessionIdInParam ;
4766
4867 /**
49- * @var \Magento\Framework\ UrlInterface
68+ * @var UrlInterface
5069 */
5170 protected $ _urlBuilder ;
5271
5372 /**
54- * @var \Laminas\ Uri\Uri|null
73+ * @var Uri
5574 */
5675 private $ uri ;
5776
77+ /**
78+ * @var State
79+ */
80+ private $ appState ;
81+
82+ /**
83+ * @var ScopeConfigInterface
84+ */
85+ private $ scopeConfig ;
86+
5887 /**
5988 * Constructor
6089 *
61- * @param \Magento\Framework\App\ RequestInterface $request
62- * @param \Magento\Store\Model\ StoreManagerInterface $storeManager
63- * @param \Magento\Framework\Encryption\ UrlCoder $urlCoder
64- * @param \Magento\Framework\Session\ SessionManagerInterface $session
65- * @param \Magento\Framework\Session\ SidResolverInterface $sidResolver
66- * @param \Magento\Framework\ UrlInterface $urlBuilder
67- * @param \Laminas\Uri\ Uri|null $uri
90+ * @param RequestInterface $request
91+ * @param StoreManagerInterface $storeManager
92+ * @param UrlCoder $urlCoder
93+ * @param SessionManagerInterface $session
94+ * @param SidResolverInterface $sidResolver
95+ * @param UrlInterface $urlBuilder
96+ * @param Uri|null $uri
6897 * @param bool $canUseSessionIdInParam
98+ * @param State|null $appState
99+ * @param ScopeConfigInterface|null $scopeConfig
100+ * @SuppressWarnings(PHPMD.ExcessiveParameterList)
69101 */
70102 public function __construct (
71- \Magento \Framework \App \RequestInterface $ request ,
72- \Magento \Store \Model \StoreManagerInterface $ storeManager ,
73- \Magento \Framework \Encryption \UrlCoder $ urlCoder ,
74- \Magento \Framework \Session \SessionManagerInterface $ session ,
75- \Magento \Framework \Session \SidResolverInterface $ sidResolver ,
76- \Magento \Framework \UrlInterface $ urlBuilder ,
77- \Laminas \Uri \Uri $ uri = null ,
78- $ canUseSessionIdInParam = true
103+ RequestInterface $ request ,
104+ StoreManagerInterface $ storeManager ,
105+ UrlCoder $ urlCoder ,
106+ SessionManagerInterface $ session ,
107+ SidResolverInterface $ sidResolver ,
108+ UrlInterface $ urlBuilder ,
109+ Uri $ uri = null ,
110+ $ canUseSessionIdInParam = true ,
111+ ?State $ appState = null ,
112+ ?ScopeConfigInterface $ scopeConfig = null
79113 ) {
80114 $ this ->_canUseSessionIdInParam = $ canUseSessionIdInParam ;
81115 $ this ->_request = $ request ;
@@ -84,20 +118,22 @@ public function __construct(
84118 $ this ->_session = $ session ;
85119 $ this ->_sidResolver = $ sidResolver ;
86120 $ this ->_urlBuilder = $ urlBuilder ;
87- $ this ->uri = $ uri ?: ObjectManager::getInstance ()->get (\Laminas \Uri \Uri::class);
121+ $ this ->uri = $ uri ?: ObjectManager::getInstance ()->get (Uri::class);
122+ $ this ->appState = $ appState ?: ObjectManager::getInstance ()->get (State::class);
123+ $ this ->scopeConfig = $ scopeConfig ?: ObjectManager::getInstance ()->get (ScopeConfigInterface::class);
88124 }
89125
90126 /**
91127 * Get the referrer url.
92128 *
93129 * @return string
94- * @throws \Magento\Framework\Exception\ NoSuchEntityException
130+ * @throws NoSuchEntityException
95131 */
96132 protected function _getUrl ()
97133 {
98134 $ refererUrl = $ this ->_request ->getServer ('HTTP_REFERER ' );
99- $ encodedUrl = $ this ->_request ->getParam (\ Magento \ Framework \ App \ ActionInterface::PARAM_NAME_URL_ENCODED )
100- ?: $ this ->_request ->getParam (\ Magento \ Framework \ App \ ActionInterface::PARAM_NAME_BASE64_URL );
135+ $ encodedUrl = $ this ->_request ->getParam (ActionInterface::PARAM_NAME_URL_ENCODED )
136+ ?: $ this ->_request ->getParam (ActionInterface::PARAM_NAME_BASE64_URL );
101137
102138 if ($ encodedUrl ) {
103139 $ refererUrl = $ this ->_urlCoder ->decode ($ encodedUrl );
@@ -113,6 +149,7 @@ protected function _getUrl()
113149 } else {
114150 $ refererUrl = $ this ->normalizeRefererUrl ($ refererUrl );
115151 }
152+
116153 return $ refererUrl ;
117154 }
118155
@@ -130,9 +167,9 @@ public function getRefererUrl()
130167 * Set referer url for redirect in response
131168 *
132169 * @param string $defaultUrl
133- * @return \Magento\Framework\App\ ActionInterface
170+ * @return ActionInterface
134171 *
135- * @throws \Magento\Framework\Exception\ NoSuchEntityException
172+ * @throws NoSuchEntityException
136173 */
137174 public function getRedirectUrl ($ defaultUrl = null )
138175 {
@@ -149,7 +186,7 @@ public function getRedirectUrl($defaultUrl = null)
149186 * @param string $defaultUrl
150187 * @return string
151188 *
152- * @throws \Magento\Framework\Exception\ NoSuchEntityException
189+ * @throws NoSuchEntityException
153190 */
154191 public function error ($ defaultUrl )
155192 {
@@ -160,6 +197,7 @@ public function error($defaultUrl)
160197 if (!$ this ->_isUrlInternal ($ errorUrl )) {
161198 $ errorUrl = $ this ->_storeManager ->getStore ()->getBaseUrl ();
162199 }
200+
163201 return $ errorUrl ;
164202 }
165203
@@ -169,17 +207,17 @@ public function error($defaultUrl)
169207 * @param string $defaultUrl
170208 * @return string
171209 *
172- * @throws \Magento\Framework\Exception\ NoSuchEntityException
210+ * @throws NoSuchEntityException
173211 */
174212 public function success ($ defaultUrl )
175213 {
176214 $ successUrl = $ this ->_request ->getParam (self ::PARAM_NAME_SUCCESS_URL );
177- if (empty ($ successUrl )) {
178- $ successUrl = $ defaultUrl ;
179- }
215+ $ successUrl = $ successUrl ?: $ defaultUrl ;
216+
180217 if (!$ this ->_isUrlInternal ($ successUrl )) {
181218 $ successUrl = $ this ->_storeManager ->getStore ()->getBaseUrl ();
182219 }
220+
183221 return $ successUrl ;
184222 }
185223
@@ -194,12 +232,12 @@ public function updatePathParams(array $arguments)
194232 /**
195233 * Set redirect into response
196234 *
197- * @param \Magento\Framework\App\ ResponseInterface $response
235+ * @param ResponseInterface $response
198236 * @param string $path
199237 * @param array $arguments
200238 * @return void
201239 */
202- public function redirect (\ Magento \ Framework \ App \ ResponseInterface $ response , $ path , $ arguments = [])
240+ public function redirect (ResponseInterface $ response , $ path , $ arguments = [])
203241 {
204242 $ arguments = $ this ->updatePathParams ($ arguments );
205243 $ response ->setRedirect ($ this ->_urlBuilder ->getUrl ($ path , $ arguments ));
@@ -213,15 +251,69 @@ public function redirect(\Magento\Framework\App\ResponseInterface $response, $pa
213251 */
214252 protected function _isUrlInternal ($ url )
215253 {
216- if (strpos ($ url , 'http ' ) !== false ) {
217- $ directLinkType = \Magento \Framework \UrlInterface::URL_TYPE_DIRECT_LINK ;
218- $ unsecureBaseUrl = $ this ->_storeManager ->getStore ()->getBaseUrl ($ directLinkType , false );
219- $ secureBaseUrl = $ this ->_storeManager ->getStore ()->getBaseUrl ($ directLinkType , true );
220- return (strpos ($ url , (string ) $ unsecureBaseUrl ) === 0 ) || (strpos ($ url , (string ) $ secureBaseUrl ) === 0 );
254+ return strpos ($ url , 'http ' ) !== false
255+ ? $ this ->isInternalUrl ($ url ) || $ this ->isCustomAdminUrl ($ url )
256+ : false ;
257+ }
258+
259+ /**
260+ * Is `Use Custom Admin URL` config enabled
261+ *
262+ * @return bool
263+ */
264+ private function isUseCustomAdminUrlEnabled (): bool
265+ {
266+ return $ this ->scopeConfig ->isSetFlag (
267+ self ::XML_PATH_USE_CUSTOM_ADMIN_URL ,
268+ ScopeInterface::SCOPE_STORE
269+ );
270+ }
271+
272+ /**
273+ * Returns custom admin url
274+ *
275+ * @return string
276+ */
277+ private function getCustomAdminUrl (): string
278+ {
279+ return $ this ->scopeConfig ->getValue (
280+ self ::XML_PATH_CUSTOM_ADMIN_URL ,
281+ ScopeInterface::SCOPE_STORE
282+ );
283+ }
284+
285+ /**
286+ * Is internal custom admin url
287+ *
288+ * @param string $url
289+ * @return bool
290+ */
291+ private function isCustomAdminUrl (string $ url ): bool
292+ {
293+ if ($ this ->appState ->getAreaCode () === Area::AREA_ADMINHTML && $ this ->isUseCustomAdminUrlEnabled ()) {
294+ return strpos ($ url , $ this ->getCustomAdminUrl ()) === 0 ;
221295 }
296+
222297 return false ;
223298 }
224299
300+ /**
301+ * Is url internal
302+ *
303+ * @param string $url
304+ * @return bool
305+ */
306+ private function isInternalUrl (string $ url ): bool
307+ {
308+ $ directLinkType = UrlInterface::URL_TYPE_DIRECT_LINK ;
309+ $ unsecureBaseUrl = $ this ->_storeManager ->getStore ()
310+ ->getBaseUrl ($ directLinkType , false );
311+ $ secureBaseUrl = $ this ->_storeManager ->getStore ()
312+ ->getBaseUrl ($ directLinkType , true );
313+
314+ return strpos ($ url , (string ) $ unsecureBaseUrl ) === 0 || strpos ($ url , (string ) $ secureBaseUrl ) === 0 ;
315+ }
316+
225317 /**
226318 * Normalize path to avoid wrong store change
227319 *
@@ -264,10 +356,10 @@ protected function normalizeRefererQueryParts($refererQuery)
264356 $ store = $ this ->_storeManager ->getStore ();
265357
266358 if ($ store
267- && !empty ($ refererQuery [\ Magento \ Store \ Model \ StoreManagerInterface::PARAM_NAME ])
268- && ($ refererQuery [\ Magento \ Store \ Model \ StoreManagerInterface::PARAM_NAME ] !== $ store ->getCode ())
359+ && !empty ($ refererQuery [StoreManagerInterface::PARAM_NAME ])
360+ && ($ refererQuery [StoreManagerInterface::PARAM_NAME ] !== $ store ->getCode ())
269361 ) {
270- $ refererQuery [\ Magento \ Store \ Model \ StoreManagerInterface::PARAM_NAME ] = $ store ->getCode ();
362+ $ refererQuery [StoreManagerInterface::PARAM_NAME ] = $ store ->getCode ();
271363 }
272364
273365 return $ refererQuery ;
0 commit comments