|
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\Routing; |
|
17
|
|
|
|
|
18
|
|
|
use Cake\Network\Request; |
|
19
|
|
|
use Cake\Routing\Router; |
|
20
|
|
|
use Cake\Core\Configure; |
|
21
|
|
|
|
|
22
|
|
|
trait SubdomainRouteTrait { |
|
23
|
|
|
|
|
24
|
|
|
private function _getSubdomains() { |
|
25
|
|
|
|
|
26
|
|
|
$validConfiguration = Configure::check('Multidimensional/Subdomains.subdomains'); |
|
27
|
|
|
|
|
28
|
|
|
if (!$validConfiguration) { |
|
29
|
|
|
return []; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$subdomains = Configure::read('Multidimensional/Subdomains.subdomains'); |
|
33
|
|
|
|
|
34
|
|
|
if (!is_array($subdomains) || count($subdomains) == 0) { |
|
35
|
|
|
return []; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return $subdomains; |
|
39
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
private function _getPrefixAndHost(array $context = []) { |
|
43
|
|
|
|
|
44
|
|
|
if (empty($context['_host'])) { |
|
45
|
|
|
$request = Router::getRequest(true) ?: Request::createFromGlobals(); |
|
46
|
|
|
$host = $request->host(); |
|
47
|
|
|
} else { |
|
48
|
|
|
$host = $context['_host']; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (preg_match('/(.*?)\.([^\/]*\..{2,5})/i', $host, $parts)) { |
|
52
|
|
|
|
|
53
|
|
|
if (in_array($parts[1], $this->_getSubdomains())) { |
|
54
|
|
|
return [$parts[1], $parts[2]]; |
|
55
|
|
|
} else { |
|
56
|
|
|
return [false, $parts[2]]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
} else { |
|
60
|
|
|
return [false, $host]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private function _checkPrefix($prefix) { |
|
68
|
|
|
|
|
69
|
|
|
$routePrefix = isset($this->defaults['prefix']) ? $this->defaults['prefix'] : false; |
|
70
|
|
|
|
|
71
|
|
|
return $prefix === $routePrefix; |
|
72
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function parse($url, $method = '') { |
|
76
|
|
|
|
|
77
|
|
|
list($prefix) = $this->_getPrefixAndHost(); |
|
78
|
|
|
|
|
79
|
|
|
if (!$this->_checkPrefix($prefix)) { |
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return parent::parse($url, $method); |
|
84
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function match(array $url, array $context = []) { |
|
88
|
|
|
|
|
89
|
|
|
if (!isset($url['prefix'])) { |
|
90
|
|
|
$url['prefix'] = false; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if (!$this->_checkPrefix($url['prefix'])) { |
|
94
|
|
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
list($prefix, $host) = $this->_getPrefixAndHost($context); |
|
98
|
|
|
|
|
99
|
|
|
if ($prefix !== $url['prefix']) { |
|
100
|
|
|
$url['_host'] = $url['prefix'] === false ? $host : $url['prefix'] . '.' . $host; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return parent::match($url, $context); |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|