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\Shell; |
17
|
|
|
|
18
|
|
|
use Cake\Console\Shell; |
19
|
|
|
use Cake\Core\Configure; |
20
|
|
|
use Multidimensional\Subdomains\Middleware\SubdomainMiddleware; |
21
|
|
|
|
22
|
|
|
class SubdomainsInstallShell extends Shell |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
public function main() |
29
|
|
|
{ |
30
|
|
|
$this->clear(); |
31
|
|
|
|
32
|
|
|
$this->helper('Multidimensional/Subdomains.Header')->output(); |
33
|
|
|
|
34
|
|
|
$subdomains = $this->_getSubdomains(); |
35
|
|
|
$continue = $this->_runProgram($subdomains); |
36
|
|
|
|
37
|
|
|
if ($continue) { |
38
|
|
|
do { |
39
|
|
|
$this->_inputSubdomain($subdomains); |
40
|
|
|
$this->_displayCurrentUniqueSubdomains($subdomains); |
41
|
|
|
$this->_deleteSubdomain($subdomains); |
42
|
|
|
$this->_writeConfig($subdomains); |
43
|
|
|
$this->_finalCheck($subdomains); |
44
|
|
|
} while (!$this->_countSubdomains($subdomains) && $this->_inputYesNo('Start over?')); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->_displayFinal($subdomains); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $subdomains |
|
|
|
|
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
private function _runProgram($subdomains) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
if ($this->_countSubdomains($subdomains)) { |
57
|
|
|
return $this->_inputYesNo('Update configuration?'); |
58
|
|
|
} else { |
59
|
|
|
return $this->_inputYesNo('Install subdomains plugin?'); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param array $subdomains |
|
|
|
|
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
private function _displayCurrentUniqueSubdomains(&$subdomains) |
68
|
|
|
{ |
69
|
|
|
if ($this->_countSubdomains($subdomains)) { |
70
|
|
|
$subdomains = $this->_uniqueSubdomains($subdomains); |
71
|
|
|
$subdomains = $this->_modifyArray($subdomains); |
72
|
|
|
$this->_displayCurrentSubdomains($subdomains); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param array $subdomains |
|
|
|
|
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
private function _inputSubdomain(&$subdomains) |
81
|
|
|
{ |
82
|
|
|
$valid = true; |
83
|
|
|
$this->out(); |
84
|
|
|
|
85
|
|
|
while (!$valid || $this->_inputYesNo('Add a subdomain?')) { |
86
|
|
|
$this->out(); |
87
|
|
|
$subdomain = strtolower($this->in('Subdomain:')); |
88
|
|
|
$valid = $this->_validateSubdomain($subdomain); |
89
|
|
|
$this->out(); |
90
|
|
|
|
91
|
|
|
if ($valid) { |
92
|
|
|
$subdomains[] = $subdomain; |
93
|
|
|
} else { |
94
|
|
|
$this->err('Invalid subdomain.'); |
95
|
|
|
} |
96
|
|
|
}; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param array $subdomains |
|
|
|
|
101
|
|
|
* @return array $subdomains |
102
|
|
|
*/ |
103
|
|
|
private function _uniqueSubdomains($subdomains) |
104
|
|
|
{ |
105
|
|
|
if ($this->_countSubdomains($subdomains)) { |
106
|
|
|
return array_values(array_unique($subdomains)); |
107
|
|
|
} else { |
108
|
|
|
return $subdomains; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param array $subdomains |
|
|
|
|
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
|
|
private function _writeConfig($subdomains) |
117
|
|
|
{ |
118
|
|
|
Configure::write('Multidimensional/Subdomains.Subdomains', array_values($subdomains)); |
119
|
|
|
Configure::dump('subdomains', 'default', ['Multidimensional/Subdomains']); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param array $subdomains |
|
|
|
|
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
private function _displayFinal($subdomains) |
127
|
|
|
{ |
128
|
|
|
$this->out(); |
129
|
|
|
if ($this->_countSubdomains($subdomains)) { |
130
|
|
|
$this->out('Configuration saved!', 2); |
131
|
|
|
} else { |
132
|
|
|
$this->err('Plugin not currently active.', 2); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param array $subdomains |
|
|
|
|
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
private function _finalCheck($subdomains) |
141
|
|
|
{ |
142
|
|
|
if (!$this->_countSubdomains($subdomains)) { |
143
|
|
|
$this->out(); |
144
|
|
|
$this->err('No subdomains configured.', 2); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return SubdomainMiddleware |
|
|
|
|
150
|
|
|
*/ |
151
|
|
|
private function _getSubdomains() |
152
|
|
|
{ |
153
|
|
|
$subdomainMiddleware = new SubdomainMiddleware(); |
154
|
|
|
$subdomains = $subdomainMiddleware->getSubdomains(); |
155
|
|
|
$defaultSubdomains = $subdomainMiddleware->defaultSubdomains; |
156
|
|
|
|
157
|
|
|
return array_diff($subdomains, $defaultSubdomains); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param array $subdomains |
|
|
|
|
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
private function _modifyArray(array $subdomains) |
165
|
|
|
{ |
166
|
|
|
if ($this->_countSubdomains($subdomains)) { |
167
|
|
|
return array_combine( |
168
|
|
|
range(1, count($subdomains)), |
169
|
|
|
array_values($subdomains) |
170
|
|
|
); |
171
|
|
|
} else { |
172
|
|
|
return $subdomains; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param array $subdomains |
|
|
|
|
178
|
|
|
* @return void |
179
|
|
|
*/ |
180
|
|
|
private function _displayCurrentSubdomains(array $subdomains) |
181
|
|
|
{ |
182
|
|
|
if ($this->_countSubdomains($subdomains)) { |
183
|
|
|
$this->out(); |
184
|
|
|
$this->out('Current subdomains:', 2); |
185
|
|
|
|
186
|
|
|
foreach ($subdomains as $key => $value) { |
187
|
|
|
$this->out(' ' . ($key) . '. ' . $value); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$this->out(); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param string $string |
|
|
|
|
196
|
|
|
* @return bool |
197
|
|
|
*/ |
198
|
|
|
private function _inputYesNo($string) |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
return strtolower($this->in($string, ['y', 'n'])) === 'y'; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param array $subdomains |
|
|
|
|
205
|
|
|
* @return void |
206
|
|
|
*/ |
207
|
|
|
private function _deleteSubdomain(&$subdomains) |
208
|
|
|
{ |
209
|
|
|
while ($this->_countSubdomains($subdomains) && $this->_inputYesNo('Delete a subdomain?')) { |
210
|
|
|
$this->out(); |
211
|
|
|
$key = (int)$this->in('Enter number to delete:', array_keys($subdomains)); |
212
|
|
|
|
213
|
|
|
if (isset($subdomains[$key])) { |
214
|
|
|
$this->out(); |
215
|
|
|
$this->out('Deleted: ' . $subdomains[$key], 2); |
216
|
|
|
unset($subdomains[$key]); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param string|null $subdomain |
|
|
|
|
223
|
|
|
* @return string|null |
|
|
|
|
224
|
|
|
*/ |
225
|
|
|
private function _validateSubdomain($subdomain) |
226
|
|
|
{ |
227
|
|
|
if (is_null($subdomain) || empty($subdomain)) { |
228
|
|
|
return false; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return preg_match('/^[A-Za-z0-9]{1}(?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9]{1})?$/', $subdomain); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param array $subdomains |
|
|
|
|
236
|
|
|
* @return int |
237
|
|
|
*/ |
238
|
|
|
private function _countSubdomains($subdomains) |
239
|
|
|
{ |
240
|
|
|
if (!isset($subdomains) || !is_array($subdomains)) { |
241
|
|
|
return 0; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return count($subdomains); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|