7
7
*/
8
8
namespace Magento \Store \App \Response ;
9
9
10
+ use Laminas \Uri \Uri ;
11
+ use Magento \Framework \App \ActionInterface ;
12
+ use Magento \Framework \App \Area ;
13
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
10
14
use 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 ;
11
26
12
27
/**
13
28
* Class Redirect computes redirect urls responses.
14
29
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
30
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
15
31
*/
16
- class Redirect implements \ Magento \ Framework \ App \ Response \ RedirectInterface
32
+ class Redirect implements RedirectInterface
17
33
{
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
+
18
37
/**
19
- * @var \Magento\Framework\App\ RequestInterface
38
+ * @var RequestInterface
20
39
*/
21
40
protected $ _request ;
22
41
23
42
/**
24
- * @var \Magento\Store\Model\ StoreManagerInterface
43
+ * @var StoreManagerInterface
25
44
*/
26
45
protected $ _storeManager ;
27
46
28
47
/**
29
- * @var \Magento\Framework\Encryption\ UrlCoder
48
+ * @var UrlCoder
30
49
*/
31
50
protected $ _urlCoder ;
32
51
33
52
/**
34
- * @var \Magento\Framework\Session\ SessionManagerInterface
53
+ * @var SessionManagerInterface
35
54
*/
36
55
protected $ _session ;
37
56
38
57
/**
39
- * @var \Magento\Framework\Session\ SidResolverInterface
58
+ * @var SidResolverInterface
40
59
*/
41
60
protected $ _sidResolver ;
42
61
@@ -46,36 +65,51 @@ class Redirect implements \Magento\Framework\App\Response\RedirectInterface
46
65
protected $ _canUseSessionIdInParam ;
47
66
48
67
/**
49
- * @var \Magento\Framework\ UrlInterface
68
+ * @var UrlInterface
50
69
*/
51
70
protected $ _urlBuilder ;
52
71
53
72
/**
54
- * @var \Laminas\ Uri\Uri|null
73
+ * @var Uri
55
74
*/
56
75
private $ uri ;
57
76
77
+ /**
78
+ * @var State
79
+ */
80
+ private $ appState ;
81
+
82
+ /**
83
+ * @var ScopeConfigInterface
84
+ */
85
+ private $ scopeConfig ;
86
+
58
87
/**
59
88
* Constructor
60
89
*
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
68
97
* @param bool $canUseSessionIdInParam
98
+ * @param State|null $appState
99
+ * @param ScopeConfigInterface|null $scopeConfig
100
+ * @SuppressWarnings(PHPMD.ExcessiveParameterList)
69
101
*/
70
102
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
79
113
) {
80
114
$ this ->_canUseSessionIdInParam = $ canUseSessionIdInParam ;
81
115
$ this ->_request = $ request ;
@@ -84,20 +118,22 @@ public function __construct(
84
118
$ this ->_session = $ session ;
85
119
$ this ->_sidResolver = $ sidResolver ;
86
120
$ 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);
88
124
}
89
125
90
126
/**
91
127
* Get the referrer url.
92
128
*
93
129
* @return string
94
- * @throws \Magento\Framework\Exception\ NoSuchEntityException
130
+ * @throws NoSuchEntityException
95
131
*/
96
132
protected function _getUrl ()
97
133
{
98
134
$ 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 );
101
137
102
138
if ($ encodedUrl ) {
103
139
$ refererUrl = $ this ->_urlCoder ->decode ($ encodedUrl );
@@ -113,6 +149,7 @@ protected function _getUrl()
113
149
} else {
114
150
$ refererUrl = $ this ->normalizeRefererUrl ($ refererUrl );
115
151
}
152
+
116
153
return $ refererUrl ;
117
154
}
118
155
@@ -130,9 +167,9 @@ public function getRefererUrl()
130
167
* Set referer url for redirect in response
131
168
*
132
169
* @param string $defaultUrl
133
- * @return \Magento\Framework\App\ ActionInterface
170
+ * @return ActionInterface
134
171
*
135
- * @throws \Magento\Framework\Exception\ NoSuchEntityException
172
+ * @throws NoSuchEntityException
136
173
*/
137
174
public function getRedirectUrl ($ defaultUrl = null )
138
175
{
@@ -149,7 +186,7 @@ public function getRedirectUrl($defaultUrl = null)
149
186
* @param string $defaultUrl
150
187
* @return string
151
188
*
152
- * @throws \Magento\Framework\Exception\ NoSuchEntityException
189
+ * @throws NoSuchEntityException
153
190
*/
154
191
public function error ($ defaultUrl )
155
192
{
@@ -160,6 +197,7 @@ public function error($defaultUrl)
160
197
if (!$ this ->_isUrlInternal ($ errorUrl )) {
161
198
$ errorUrl = $ this ->_storeManager ->getStore ()->getBaseUrl ();
162
199
}
200
+
163
201
return $ errorUrl ;
164
202
}
165
203
@@ -169,17 +207,17 @@ public function error($defaultUrl)
169
207
* @param string $defaultUrl
170
208
* @return string
171
209
*
172
- * @throws \Magento\Framework\Exception\ NoSuchEntityException
210
+ * @throws NoSuchEntityException
173
211
*/
174
212
public function success ($ defaultUrl )
175
213
{
176
214
$ successUrl = $ this ->_request ->getParam (self ::PARAM_NAME_SUCCESS_URL );
177
- if (empty ($ successUrl )) {
178
- $ successUrl = $ defaultUrl ;
179
- }
215
+ $ successUrl = $ successUrl ?: $ defaultUrl ;
216
+
180
217
if (!$ this ->_isUrlInternal ($ successUrl )) {
181
218
$ successUrl = $ this ->_storeManager ->getStore ()->getBaseUrl ();
182
219
}
220
+
183
221
return $ successUrl ;
184
222
}
185
223
@@ -194,12 +232,12 @@ public function updatePathParams(array $arguments)
194
232
/**
195
233
* Set redirect into response
196
234
*
197
- * @param \Magento\Framework\App\ ResponseInterface $response
235
+ * @param ResponseInterface $response
198
236
* @param string $path
199
237
* @param array $arguments
200
238
* @return void
201
239
*/
202
- public function redirect (\ Magento \ Framework \ App \ ResponseInterface $ response , $ path , $ arguments = [])
240
+ public function redirect (ResponseInterface $ response , $ path , $ arguments = [])
203
241
{
204
242
$ arguments = $ this ->updatePathParams ($ arguments );
205
243
$ response ->setRedirect ($ this ->_urlBuilder ->getUrl ($ path , $ arguments ));
@@ -213,15 +251,69 @@ public function redirect(\Magento\Framework\App\ResponseInterface $response, $pa
213
251
*/
214
252
protected function _isUrlInternal ($ url )
215
253
{
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 ;
221
295
}
296
+
222
297
return false ;
223
298
}
224
299
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
+
225
317
/**
226
318
* Normalize path to avoid wrong store change
227
319
*
@@ -264,10 +356,10 @@ protected function normalizeRefererQueryParts($refererQuery)
264
356
$ store = $ this ->_storeManager ->getStore ();
265
357
266
358
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 ())
269
361
) {
270
- $ refererQuery [\ Magento \ Store \ Model \ StoreManagerInterface::PARAM_NAME ] = $ store ->getCode ();
362
+ $ refererQuery [StoreManagerInterface::PARAM_NAME ] = $ store ->getCode ();
271
363
}
272
364
273
365
return $ refererQuery ;
0 commit comments