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
|
|
|
public $defaultSubdomains = ['www', 'false']; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request The request. |
27
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response The response. |
28
|
|
|
* @param callable $next The next middleware to call. |
29
|
|
|
* @return \Psr\Http\Message\ResponseInterface A response. |
30
|
|
|
*/ |
31
|
|
|
public function __invoke($request, $response, $next) |
32
|
|
|
{ |
33
|
|
|
$uri = $request->getUri(); |
34
|
|
|
$host = $uri->getHost(); |
35
|
|
|
|
36
|
|
|
list($prefix) = $this->getPrefixAndHost($host); |
37
|
|
|
|
38
|
|
|
if ($prefix !== false) { |
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
|
|
|
return $next($request, $response); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
4 |
|
public function getSubdomains() |
55
|
|
|
{ |
56
|
4 |
|
$validConfiguration = Configure::check('Multidimensional/Subdomains.Subdomains'); |
57
|
|
|
|
58
|
4 |
|
if (!$validConfiguration) { |
59
|
|
|
return $this->defaultSubdomains; |
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
$subdomains = Configure::read('Multidimensional/Subdomains.Subdomains'); |
63
|
|
|
|
64
|
4 |
|
if (!is_array($subdomains) || count($subdomains) == 0) { |
65
|
|
|
return $this->defaultSubdomains; |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
return array_merge($this->defaultSubdomains, $subdomains); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $host |
|
|
|
|
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
2 |
|
public function getPrefixAndHost($host) |
76
|
|
|
{ |
77
|
2 |
|
if (empty($host)) { |
78
|
|
|
return [false, false]; |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
if (preg_match('/(.*?)\.([^\/]*\..{2,5})/i', $host, $match)) { |
82
|
2 |
|
if (in_array($match[1], $this->getSubdomains())) { |
83
|
2 |
|
return [$match[1], $match[2]]; |
84
|
|
|
} else { |
85
|
2 |
|
return [false, $match[2]]; |
86
|
|
|
} |
87
|
|
|
} else { |
88
|
2 |
|
return [false, $host]; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|