1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CakePHP Plugin : CakePHP Subdomain Routing |
4
|
|
|
* Copyright (c) Multidimension.al (http://multidimension.al) |
5
|
|
|
* Github : https://github.com/multidimension-al/cakephp-subdomains |
6
|
|
|
* |
7
|
|
|
* Licensed under The MIT License |
8
|
|
|
* For full copyright and license information, please see the LICENSE file |
9
|
|
|
* Redistributions of files must retain the above copyright notice. |
10
|
|
|
* |
11
|
|
|
* @copyright (c) Multidimension.al (http://multidimension.al) |
12
|
|
|
* @link https://github.com/multidimension-al/cakephp-subdomains Github |
13
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Multidimensional\Subdomains\Middleware; |
17
|
|
|
|
18
|
|
|
use Cake\Core\Configure; |
19
|
|
|
|
20
|
|
|
class SubdomainMiddleware { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request The request. |
24
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response The response. |
25
|
|
|
* @param callable $next The next middleware to call. |
26
|
|
|
* @return \Psr\Http\Message\ResponseInterface A response. |
27
|
|
|
*/ |
28
|
|
|
public function __invoke($request, $response, $next) { |
29
|
|
|
|
30
|
|
|
$subdomains = $this->getSubdomains(); |
|
|
|
|
31
|
|
|
|
32
|
|
|
$uri = $request->getUri(); |
33
|
|
|
$host = $uri->getHost(); |
34
|
|
|
|
35
|
|
|
list($prefix) = $this->getPrefixAndHost($host); |
36
|
|
|
|
37
|
|
|
if($prefix !== false){ |
38
|
|
|
|
39
|
|
|
$params = (array) $request->getAttribute('params', []); |
40
|
|
|
|
41
|
|
|
if (empty($params['prefix'])) { |
42
|
|
|
$params['prefix'] = $prefix; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$request = $request->withAttribute('params', $params); |
46
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $next($request, $response); |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getSubdomains() { |
54
|
|
|
|
55
|
|
|
$validConfiguration = Configure::check('Multidimensional/Subdomains.subdomains'); |
56
|
|
|
|
57
|
|
|
if (!$validConfiguration) { |
58
|
|
|
return []; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$subdomains = Configure::read('Multidimensional/Subdomains.subdomains'); |
62
|
|
|
|
63
|
|
|
if (!is_array($subdomains) || count($subdomains) == 0) { |
64
|
|
|
return []; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $subdomains; |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getPrefixAndHost($host) { |
72
|
|
|
|
73
|
|
|
if (empty($host)) { |
74
|
|
|
return [false, false]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (preg_match('/(.*?)\.([^\/]*\..{2,5})/i', $host, $match)) { |
78
|
|
|
|
79
|
|
|
if (in_array($match[1], $this->getSubdomains())) { |
80
|
|
|
return [$match[1], $match[2]]; |
81
|
|
|
} else { |
82
|
|
|
return [false, $match[2]]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} else { |
86
|
|
|
return [false, $host]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.