Skip to content

Commit fcb7863

Browse files
committed
Fix redirection of local url
1 parent 60d0acb commit fcb7863

File tree

13 files changed

+83
-78
lines changed

13 files changed

+83
-78
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 5 additions & 3 deletions
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

Lines changed: 4 additions & 10 deletions
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

Lines changed: 0 additions & 3 deletions
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

Lines changed: 2 additions & 4 deletions
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

Lines changed: 12 additions & 23 deletions
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

Lines changed: 5 additions & 5 deletions
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/redirection.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Redirection
2+
===========
3+
4+
Set up automated mobile/desktop/tablet redirection
5+
6+
In this example, we will make sure to activate the automatic redirection to a mobile site http://m.site.com when the user uses a mobile device and desktop http://site.com when the user uses a computer or desktop browser.
7+
8+
If the user reaches the mobile site http://m.site.com, on his desktop browser he should be redirected to the full version at http://site.com.
9+
10+
If the user reaches the desktop site http://site.com, with his mobile he should be redirected to the full version at http://m.site.com.
11+
12+
```yaml
13+
# config/packages/mobile_detect.yaml
14+
mobile_detect:
15+
redirect:
16+
full:
17+
action: redirect # redirect, no_redirect, redirect_without_path
18+
host: http://localhost:8001 # with scheme (http|https), default null, url validate
19+
is_enabled: true # default false
20+
status_code: 301 # default 302
21+
mobile:
22+
action: redirect # redirect, no_redirect, redirect_without_path
23+
host: http://localhost:8002 # with scheme (http|https), default null, url validate
24+
is_enabled: true # default false
25+
status_code: 301 # default 302
26+
```

src/Resources/views/Collector/device.html.twig

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@
2020
{% set viewLink = '' %}
2121
{% if viewData.isCurrent %}
2222
{% set viewLink = '<span class="sf-toolbar-status sf-toolbar-status-green">current</span>' %}
23-
{% elseif viewData.enabled == false %}
24-
{% set viewLink = 'External' %}
2523
{% else %}
2624
{% set viewLink = '<span><a href="' ~ viewData.link ~ '">Use this view</a></span>' %}
2725
{% endif %}
2826
<div class="sf-toolbar-info-piece">
29-
<b>{{ viewData.label }}</b>
30-
{{ viewLink | raw }}
27+
<b>{{ viewData.label }}{{ viewData.enabled == false ? ' (External)' : ''}}</b>
28+
{{ viewLink|raw }}
3129
</div>
3230
{% endfor %}
3331
{% endset %}

0 commit comments

Comments
 (0)