Skip to content

Commit 7db6d75

Browse files
authored
Merge pull request #11 from tattali/redirection
Fix redirection of local url
2 parents 0ff284d + 49e7b8e commit 7db6d75

File tree

16 files changed

+177
-197
lines changed

16 files changed

+177
-197
lines changed

.github/workflows/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ jobs:
8181
run: vendor/bin/phpstan analyse
8282

8383
- name: Unit and Feature tests via PHPUnit
84-
run: vendor/bin/phpunit
84+
run: php vendor/bin/phpunit
8585

8686
- name: Upload coverage file
8787
uses: actions/upload-artifact@v3

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@ Documentation
2323
-------------
2424

2525
### Installation
26-
For Symfony legacy versions the documentation is [here](src/Resources/doc/legacy-versions.md)
2726
```sh
2827
composer require tattali/mobile-detect-bundle
2928
```
30-
29+
*Install with Symfony legacy versions: [here](src/Resources/doc/legacy-versions.md)*
3130
### Usage
3231

3332
#### Checking device
@@ -43,6 +42,7 @@ public function someaction(MobileDetectorInterface $mobileDetector)
4342
}
4443
```
4544

45+
With Twig
4646
```twig
4747
{% if is_mobile() %}
4848
{% if is_tablet() %}
@@ -63,6 +63,7 @@ Or using the Symfony toolbar
6363
#### Going further
6464

6565
- [Symfony legacy versions](src/Resources/doc/legacy-versions.md)
66+
- [Redirection](src/Resources/doc/redirection.md)
6667
- [Full reference](src/Resources/doc/reference.md)
6768

6869
Contribute and feedback

composer.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
"description": "Symfony5/6 bundle for detect mobile devices, managing mobile view types, redirect to mobile version.",
44
"keywords": [
55
"mobile detect",
6-
"mobile redirect",
6+
"device detect",
7+
"device detector",
78
"mobile view managing",
8-
"mobile",
99
"mobiledetect",
1010
"mobiledetectbundle",
11-
"symfony mobile"
11+
"symfony mobile detect",
12+
"symfony mobiledetect",
13+
"symfony"
1214
],
1315
"homepage": "https://github.com/tattali/MobileDetectBundle",
1416
"type": "symfony-bundle",

src/DataCollector/DeviceDataCollector.php

+4-10
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,9 @@ public function reset(): void
114114

115115
protected function canUseView(string $view, ?string $host): bool
116116
{
117-
if (!\is_array($this->redirectConfig)) {
118-
return true;
119-
}
120-
121-
if (!isset($this->redirectConfig[$view])) {
122-
return true;
123-
}
124-
125-
if (!isset($this->redirectConfig[$view]['is_enabled'])
117+
if (!\is_array($this->redirectConfig)
118+
|| !isset($this->redirectConfig[$view])
119+
|| !isset($this->redirectConfig[$view]['is_enabled'])
126120
|| false === $this->redirectConfig[$view]['is_enabled']
127121
) {
128122
return true;
@@ -156,7 +150,7 @@ private function generateSwitchLink(
156150
$requestSwitchView->server->set(
157151
'QUERY_STRING',
158152
Request::normalizeQueryString(
159-
http_build_query($requestSwitchView->query->all(), '', '&')
153+
http_build_query($requestSwitchView->query->all())
160154
)
161155
);
162156

src/DependencyInjection/Configuration.php

-3
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ public function getConfigTreeBuilder(): TreeBuilder
7474
->booleanNode('save_referer_path')->defaultTrue()->end()
7575
->end()
7676
->end()
77-
->arrayNode('service')
78-
->addDefaultsIfNotSet()
79-
->end()
8077
->scalarNode('cookie_key')->defaultValue(DeviceView::COOKIE_KEY_DEFAULT)->cannotBeEmpty()->end()
8178
->scalarNode('cookie_path')->defaultValue(DeviceView::COOKIE_PATH_DEFAULT)->cannotBeEmpty()->end()
8279
->scalarNode('cookie_domain')->defaultValue(DeviceView::COOKIE_DOMAIN_DEFAULT)->cannotBeEmpty()->end()

src/DependencyInjection/MobileDetectExtension.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,8 @@ public function load(array $configs, ContainerBuilder $container)
5858
$container->setParameter('mobile_detect.switch_param', $config['switch_param']);
5959
}
6060

61-
protected function validHost(string $url): bool
61+
protected function validHost(?string $url): bool
6262
{
63-
$pattern = '/^(?:(http|https):\\/\\/)([A-Z0-9][A-Z0-9_-]*(?:\\.[A-Z0-9][A-Z0-9_-]*)+):?(\\d+)?\\/?/i';
64-
65-
return (bool) preg_match($pattern, $url);
63+
return (bool) filter_var($url, \FILTER_VALIDATE_URL);
6664
}
6765
}

src/EventListener/RequestResponseListener.php

+12-23
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ protected function getRedirectResponseBySwitchParam(Request $request): RedirectR
178178
$redirectUrl .= '?'.Request::normalizeQueryString(http_build_query($queryParams, '', '&'));
179179
}
180180
} else {
181-
$redirectUrl = $this->getCurrentHost($request);
181+
$redirectUrl = $request->getSchemeAndHttpHost();
182182
}
183183
}
184184

@@ -188,24 +188,18 @@ protected function getRedirectResponseBySwitchParam(Request $request): RedirectR
188188
/**
189189
* Do we have to redirect?
190190
*
191-
* @param string $view For which view should be check?
191+
* @param string $viewType The view we want to redirect to
192192
*/
193-
protected function mustRedirect(Request $request, string $view): bool
193+
protected function mustRedirect(Request $request, string $viewType): bool
194194
{
195-
if (!isset($this->redirectConf[$view])
196-
|| !$this->redirectConf[$view]['is_enabled']
197-
|| (self::NO_REDIRECT === $this->getRoutingOption($request->get('_route'), $view))
195+
if (!isset($this->redirectConf[$viewType])
196+
|| !$this->redirectConf[$viewType]['is_enabled']
197+
|| (self::NO_REDIRECT === $this->getRoutingOption($request->get('_route'), $viewType))
198198
) {
199199
return false;
200200
}
201201

202-
$isHost = ($this->getCurrentHost($request) === $this->redirectConf[$view]['host']);
203-
204-
if (!$isHost) {
205-
return true;
206-
}
207-
208-
return false;
202+
return $request->getSchemeAndHttpHost() !== $this->redirectConf[$viewType]['host'];
209203
}
210204

211205
protected function getRoutingOption(string $routeName, string $optionName): ?string
@@ -228,28 +222,23 @@ protected function getRoutingOption(string $routeName, string $optionName): ?str
228222
return null;
229223
}
230224

231-
protected function getCurrentHost(Request $request): string
232-
{
233-
return $request->getScheme().'://'.$request->getHost();
234-
}
235-
236-
protected function getRedirectUrl(Request $request, string $platform): ?string
225+
protected function getRedirectUrl(Request $request, string $view): ?string
237226
{
238-
if (($routingOption = $this->getRoutingOption($request->get('_route'), $platform))) {
227+
if (($routingOption = $this->getRoutingOption($request->get('_route'), $view))) {
239228
if (self::REDIRECT === $routingOption) {
240229
// Make sure to hint at the device override, otherwise infinite loop
241230
// redirection may occur if different device views are hosted on
242231
// different domains (since the cookie can't be shared across domains)
243232
$queryParams = $request->query->all();
244-
$queryParams[$this->deviceView->getSwitchParam()] = $platform;
233+
$queryParams[$this->deviceView->getSwitchParam()] = $view;
245234

246-
return rtrim($this->redirectConf[$platform]['host'], '/').$request->getPathInfo().'?'.Request::normalizeQueryString(http_build_query($queryParams, '', '&'));
235+
return rtrim($this->redirectConf[$view]['host'], '/').$request->getPathInfo().'?'.Request::normalizeQueryString(http_build_query($queryParams));
247236
}
248237
if (self::REDIRECT_WITHOUT_PATH === $routingOption) {
249238
// Make sure to hint at the device override, otherwise infinite loop
250239
// redirections may occur if different device views are hosted on
251240
// different domains (since the cookie can't be shared across domains)
252-
return $this->redirectConf[$platform]['host'].'?'.$this->deviceView->getSwitchParam().'='.$platform;
241+
return $this->redirectConf[$view]['host'].'?'.$this->deviceView->getSwitchParam().'='.$view;
253242
}
254243

255244
return null;

src/Resources/config/services.yaml

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
parameters:
2-
mobile_detect.cookie_expire_datetime_modifier:
3-
mobile_detect.cookie_key:
4-
mobile_detect.redirect:
5-
mobile_detect.switch_device_view.save_referer_path:
6-
mobile_detect.switch_param:
2+
mobile_detect.cookie_expire_datetime_modifier: ~
3+
mobile_detect.cookie_key: ~
4+
mobile_detect.redirect: ~
5+
mobile_detect.switch_device_view.save_referer_path: ~
6+
mobile_detect.switch_param: ~
77

88
services:
99
MobileDetectBundle\EventListener\RequestResponseListener:

src/Resources/doc/index.md

-109
This file was deleted.

0 commit comments

Comments
 (0)