1 | <?php |
||||||
2 | /* (c) Asafov Sergey <[email protected]> |
||||||
3 | * This recipe for work with ISPManager Lite panel by API. |
||||||
4 | */ |
||||||
5 | namespace Deployer; |
||||||
6 | use Deployer\Exception\Exception; |
||||||
7 | use Deployer\Utility\Httpie; |
||||||
8 | |||||||
9 | set('ispmanager_owner', 'www-root'); |
||||||
10 | set('ispmanager_doc_root', '/var/www/' . get('ispmanager_owner') . '/data/'); |
||||||
11 | |||||||
12 | // ISPManager default configuration |
||||||
13 | set('ispmanager', [ |
||||||
14 | 'api' => [ |
||||||
15 | 'dsn' => 'https://root:password@localhost:1500/ispmgr', |
||||||
16 | 'secure' => true, |
||||||
17 | ], |
||||||
18 | 'createDomain' => NULL, |
||||||
19 | 'updateDomain' => NULL, |
||||||
20 | 'deleteDomain' => NULL, |
||||||
21 | 'createDatabase' => NULL, |
||||||
22 | 'deleteDatabase' => NULL, |
||||||
23 | 'phpSelect' => NULL, |
||||||
24 | 'createAlias' => NULL, |
||||||
25 | 'deleteAlias' => NULL, |
||||||
26 | ]); |
||||||
27 | |||||||
28 | // Vhost default configuration |
||||||
29 | set('vhost', [ |
||||||
30 | 'name' => '{{domain}}', |
||||||
31 | 'php_enable' => 'on', |
||||||
32 | 'aliases' => 'www.{{domain}}', |
||||||
33 | 'home' => 'www/{{domain}}', |
||||||
34 | 'owner' => get('ispmanager_owner'), |
||||||
35 | 'email' => 'webmaster@{{domain}}', |
||||||
36 | 'charset' => 'off', |
||||||
37 | 'dirindex' => 'index.php index.html', |
||||||
38 | 'ssi' => 'on', |
||||||
39 | 'php' => 'on', |
||||||
40 | 'php_mode' => 'php_mode_mod', |
||||||
41 | 'basedir' => 'on', |
||||||
42 | 'php_apache_version' => 'native', |
||||||
43 | 'cgi' => 'off', |
||||||
44 | 'log_access' => 'on', |
||||||
45 | 'log_error' => 'on', |
||||||
46 | ]); |
||||||
47 | |||||||
48 | // Storage |
||||||
49 | set('ispmanager_session', ''); |
||||||
50 | set('ispmanager_databases', [ |
||||||
51 | 'servers' => [], |
||||||
52 | 'hosts' => [], |
||||||
53 | 'dblist' => [], |
||||||
54 | ]); |
||||||
55 | |||||||
56 | set('ispmanager_domains', []); |
||||||
57 | set('ispmanager_phplist', []); |
||||||
58 | set('ispmanager_aliaslist', []); |
||||||
59 | |||||||
60 | desc('Initialisation'); |
||||||
61 | task('ispmanager:init', function () { |
||||||
62 | $config = get ('ispmanager'); |
||||||
63 | |||||||
64 | if (!is_null ($config['createDatabase']) || !is_null ($config['deleteDatabase'])) { |
||||||
65 | invoke('ispmanager:db-server-list'); |
||||||
66 | invoke('ispmanager:db-list'); |
||||||
67 | } |
||||||
68 | |||||||
69 | if (!is_null ($config['createDomain']) || !is_null ($config['deleteDomain'])) { |
||||||
70 | invoke('ispmanager:domain-list'); |
||||||
71 | } |
||||||
72 | |||||||
73 | if (!is_null ($config['phpSelect'])) { |
||||||
74 | invoke('ispmanager:domain-list'); |
||||||
75 | invoke('ispmanager:get-php-list'); |
||||||
76 | } |
||||||
77 | |||||||
78 | if (!is_null ($config['createAlias']) || !is_null ($config['deleteAlias'])) { |
||||||
79 | invoke('ispmanager:domain-list'); |
||||||
80 | } |
||||||
81 | }); |
||||||
82 | |||||||
83 | desc('Take database servers list'); |
||||||
84 | task('ispmanager:db-server-list', function () { |
||||||
85 | $response = ispmanagerRequest('get', [ |
||||||
86 | 'func' => 'db.server', |
||||||
87 | ]); |
||||||
88 | |||||||
89 | $hostList = []; |
||||||
90 | $serverList = []; |
||||||
91 | |||||||
92 | if (isset ($response['doc']['elem']) && count ($response['doc']['elem']) > 0) { |
||||||
93 | foreach ($response['doc']['elem'] as $dbServer) { |
||||||
94 | $serverList[$dbServer['name']['$']] = [ |
||||||
95 | 'host' => $dbServer['host']['$'], |
||||||
96 | 'name' => $dbServer['name']['$'], |
||||||
97 | 'version' => $dbServer['savedver']['$'], |
||||||
98 | ]; |
||||||
99 | |||||||
100 | if (!strpos ($dbServer['host']['$'], ':')) { |
||||||
101 | $dbHost = $dbServer['host']['$'] . ':3306'; |
||||||
102 | } |
||||||
103 | else { |
||||||
104 | $dbHost = $dbServer['host']['$']; |
||||||
105 | } |
||||||
106 | |||||||
107 | $hostList[$dbHost] =[ |
||||||
108 | 'host' => $dbHost, |
||||||
109 | 'name' => $dbServer['name']['$'], |
||||||
110 | 'version' => $dbServer['savedver']['$'], |
||||||
111 | ]; |
||||||
112 | } |
||||||
113 | } |
||||||
114 | |||||||
115 | add('ispmanager_databases', [ |
||||||
116 | 'servers' => $serverList, |
||||||
117 | 'hosts' => $hostList, |
||||||
118 | ]); |
||||||
119 | }); |
||||||
120 | |||||||
121 | desc('Take databases list'); |
||||||
122 | task('ispmanager:db-list', function () { |
||||||
123 | $response = ispmanagerRequest('get', [ |
||||||
124 | 'func' => 'db', |
||||||
125 | ]); |
||||||
126 | |||||||
127 | $dbList = []; |
||||||
128 | if (isset ($response['doc']['elem']) && count ($response['doc']['elem']) > 0) { |
||||||
129 | foreach ($response['doc']['elem'] as $db) { |
||||||
130 | $dbList[$db['pair']['$']] = [ |
||||||
131 | 'name' => $db['name']['$'], |
||||||
132 | 'server' => $db['server']['$'], |
||||||
133 | 'location' => $db['pair']['$'], |
||||||
134 | ]; |
||||||
135 | } |
||||||
136 | } |
||||||
137 | |||||||
138 | add('ispmanager_databases', [ |
||||||
139 | 'dblist' => $dbList |
||||||
140 | ]); |
||||||
141 | }); |
||||||
142 | |||||||
143 | desc('Take domain list'); |
||||||
144 | task('ispmanager:domain-list', function () { |
||||||
145 | $response = ispmanagerRequest('get', [ |
||||||
146 | 'func' => 'webdomain', |
||||||
147 | ]); |
||||||
148 | |||||||
149 | $domainList = []; |
||||||
150 | if (isset ($response['doc']['elem']) && count ($response['doc']['elem']) > 0) { |
||||||
151 | foreach ($response['doc']['elem'] as $domain) { |
||||||
152 | $domainList[] = $domain['name']['$']; |
||||||
153 | } |
||||||
154 | } |
||||||
155 | |||||||
156 | add('ispmanager_domains', $domainList); |
||||||
157 | }); |
||||||
158 | |||||||
159 | desc('Create new database'); |
||||||
160 | task('ispmanager:db-create', function () { |
||||||
161 | $config = get ('ispmanager'); |
||||||
162 | |||||||
163 | if (is_null ($config['createDatabase'])) { |
||||||
164 | warning ('Action for database create is not active'); |
||||||
165 | return; |
||||||
166 | } |
||||||
167 | |||||||
168 | $dsnData = parse_url($config['createDatabase']['dsn']); |
||||||
169 | |||||||
170 | $dbInfo = get ('ispmanager_databases'); |
||||||
171 | |||||||
172 | $hostInfo = NULL; |
||||||
173 | foreach ($dbInfo['hosts'] as $hostData) { |
||||||
174 | if ($hostData['host'] == $dsnData['host'] . ':' . $dsnData['port']) { |
||||||
175 | $hostInfo = $hostData; |
||||||
176 | break; |
||||||
177 | } |
||||||
178 | } |
||||||
179 | |||||||
180 | if (is_null ($hostInfo)) { |
||||||
181 | throw new Exception('Incorrect DB host'); |
||||||
182 | } |
||||||
183 | |||||||
184 | $dbName = substr ($dsnData['path'], 1); |
||||||
185 | |||||||
186 | $dbLocation = $dbName . '->' . $hostInfo['name']; |
||||||
187 | |||||||
188 | if (isset ($dbInfo['dblist'][$dbLocation])) { |
||||||
189 | if (!isset ($config['createDatabase']['skipIfExist']) || !$config['createDatabase']['skipIfExist']) { |
||||||
190 | throw new Exception('Database already exists!'); |
||||||
191 | } |
||||||
192 | else { |
||||||
193 | warning ('Database already exists - skipping'); |
||||||
194 | return true; |
||||||
195 | } |
||||||
196 | } |
||||||
197 | |||||||
198 | $dbCreateRequest = [ |
||||||
199 | 'func' => 'db.edit', |
||||||
200 | 'name' => $dbName, |
||||||
201 | 'owner' => get('ispmanager_owner'), |
||||||
202 | 'server' => $hostInfo['name'], |
||||||
203 | 'charset' => $config['createDatabase']['charset'], |
||||||
204 | 'sok' => 'ok', |
||||||
205 | ]; |
||||||
206 | |||||||
207 | if ($dsnData['user'] == '*') { |
||||||
208 | $dbCreateRequest['user'] = '*'; |
||||||
209 | $dbCreateRequest['username'] = $dbName; |
||||||
210 | |||||||
211 | if ($dsnData['pass'] == '*') { |
||||||
212 | $dbCreateRequest['password'] = generatePassword (8); |
||||||
213 | } |
||||||
214 | else { |
||||||
215 | $dbCreateRequest['password'] = $dsnData['pass']; |
||||||
216 | } |
||||||
217 | } |
||||||
218 | else { |
||||||
219 | $dbCreateRequest['user'] = $dsnData['user']; |
||||||
220 | } |
||||||
221 | |||||||
222 | |||||||
223 | $response = ispmanagerRequest('post', $dbCreateRequest); |
||||||
224 | |||||||
225 | if (isset ($response['doc']['error']['msg']['$'])) { |
||||||
226 | throw new Exception($response['doc']['error']['msg']['$']); |
||||||
227 | } |
||||||
228 | else { |
||||||
229 | info ('Database successfully created'); |
||||||
230 | } |
||||||
231 | }); |
||||||
232 | |||||||
233 | desc('Delete database'); |
||||||
234 | task('ispmanager:db-delete', function () { |
||||||
235 | $config = get ('ispmanager'); |
||||||
236 | |||||||
237 | if (is_null ($config['deleteDatabase'])) { |
||||||
238 | warning ('Action for database delete is not active'); |
||||||
239 | return; |
||||||
240 | } |
||||||
241 | |||||||
242 | $dbInfo = get ('ispmanager_databases'); |
||||||
243 | $dsnData = parse_url($config['deleteDatabase']['dsn']); |
||||||
244 | |||||||
245 | $hostInfo = NULL; |
||||||
246 | foreach ($dbInfo['hosts'] as $hostData) { |
||||||
247 | if ($hostData['host'] == $dsnData['host'] . ':' . $dsnData['port']) { |
||||||
248 | $hostInfo = $hostData; |
||||||
249 | break; |
||||||
250 | } |
||||||
251 | } |
||||||
252 | |||||||
253 | if (is_null ($hostInfo)) { |
||||||
254 | throw new Exception('Incorrect DB host'); |
||||||
255 | } |
||||||
256 | |||||||
257 | $dbName = substr ($dsnData['path'], 1); |
||||||
258 | |||||||
259 | $dbLocation = $dbName . '->' . $hostInfo['name']; |
||||||
260 | |||||||
261 | if (!isset ($dbInfo['dblist'][$dbLocation])) { |
||||||
262 | if (!isset ($config['deleteDatabase']['skipIfNotExist']) || !$config['deleteDatabase']['skipIfNotExist']) { |
||||||
263 | throw new Exception('Database not exist!'); |
||||||
264 | } |
||||||
265 | else { |
||||||
266 | warning ('Database not exist - skipping'); |
||||||
267 | return true; |
||||||
268 | } |
||||||
269 | } |
||||||
270 | |||||||
271 | $dbDeleteRequest = [ |
||||||
272 | 'func' => 'db.delete', |
||||||
273 | 'elid' => $dbLocation, |
||||||
274 | ]; |
||||||
275 | |||||||
276 | $response = ispmanagerRequest('post', $dbDeleteRequest); |
||||||
277 | |||||||
278 | if (isset ($response['doc']['error']['msg']['$'])) { |
||||||
279 | throw new Exception($response['doc']['error']['msg']['$']); |
||||||
280 | } |
||||||
281 | else { |
||||||
282 | info ('Database successfully deleted'); |
||||||
283 | } |
||||||
284 | }); |
||||||
285 | |||||||
286 | desc('Create new domain'); |
||||||
287 | task('ispmanager:domain-create', function () { |
||||||
288 | $config = get ('ispmanager'); |
||||||
289 | |||||||
290 | if (is_null ($config['createDomain'])) { |
||||||
291 | warning ('Action for domain create is not active'); |
||||||
292 | return; |
||||||
293 | } |
||||||
294 | |||||||
295 | if (!isset ($config['createDomain']['name']) || $config['createDomain']['name'] == '') { |
||||||
296 | throw new Exception('Invalid domain name!'); |
||||||
297 | } |
||||||
298 | |||||||
299 | // Check domain exists |
||||||
300 | $existDomains = get ('ispmanager_domains'); |
||||||
301 | |||||||
302 | if (in_array ($config['createDomain']['name'], $existDomains)) { |
||||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
303 | if (!isset ($config['createDomain']['skipIfExist']) || !$config['createDomain']['skipIfExist']) { |
||||||
304 | throw new Exception('Domain already exists!'); |
||||||
305 | } |
||||||
306 | else { |
||||||
307 | warning1 ('Domain already exists - skipping'); |
||||||
0 ignored issues
–
show
The function
warning1 was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
308 | return true; |
||||||
309 | } |
||||||
310 | } |
||||||
311 | |||||||
312 | // Build vhost create request |
||||||
313 | $vhostTemplate = get('vhost'); |
||||||
314 | |||||||
315 | $domainCreateRequest = [ |
||||||
316 | 'func' => 'webdomain.edit', |
||||||
317 | 'sok' => 'ok', |
||||||
318 | ]; |
||||||
319 | |||||||
320 | foreach ($vhostTemplate as $key => $value) { |
||||||
321 | $domainCreateRequest[$key] = str_replace('{{domain}}', $config['createDomain']['name'], $vhostTemplate[$key]); |
||||||
322 | } |
||||||
323 | |||||||
324 | $response = ispmanagerRequest('post', $domainCreateRequest); |
||||||
325 | |||||||
326 | if (isset ($response['doc']['error']['msg']['$'])) { |
||||||
327 | throw new Exception($response['doc']['error']['msg']['$']); |
||||||
328 | } |
||||||
329 | else { |
||||||
330 | info ('Domain successfully created'); |
||||||
331 | } |
||||||
332 | }); |
||||||
333 | |||||||
334 | desc('Get allowed PHP modes and versions'); |
||||||
335 | task('ispmanager:get-php-list', function () { |
||||||
336 | // Get www-root settings for fpm version |
||||||
337 | $response = ispmanagerRequest('get', [ |
||||||
338 | 'func' => 'user.edit', |
||||||
339 | 'elid' => get('ispmanager_owner'), |
||||||
340 | 'elname' => get('ispmanager_owner'), |
||||||
341 | ]); |
||||||
342 | |||||||
343 | $userFPMVersion = isset ($response['doc']['limit_php_fpm_version']['$']) ? $response['doc']['limit_php_fpm_version']['$'] : NULL; |
||||||
344 | |||||||
345 | $response = ispmanagerRequest('get', [ |
||||||
346 | 'func' => 'phpversions' |
||||||
347 | ]); |
||||||
348 | |||||||
349 | $versions = []; |
||||||
350 | foreach ($response['doc']['elem'] as $phpVersion) { |
||||||
351 | $versions[$phpVersion['key']['$']] = [ |
||||||
352 | 'name' => $phpVersion['name']['$'], |
||||||
353 | 'php_mode_mod' => false, |
||||||
354 | 'php_mode_cgi' => false, |
||||||
355 | 'php_mode_fcgi_apache' => false, |
||||||
356 | 'php_mode_fcgi_nginxfpm' => false, |
||||||
357 | ]; |
||||||
358 | |||||||
359 | if (isset ($phpVersion['default_apache']) && $phpVersion['default_apache']['$'] == 'on') { |
||||||
360 | $versions[$phpVersion['key']['$']]['php_mode_mod'] = true; |
||||||
361 | } |
||||||
362 | |||||||
363 | if (isset ($phpVersion['cgi']) && $phpVersion['cgi']['$'] == 'on') { |
||||||
364 | $versions[$phpVersion['key']['$']]['php_mode_cgi'] = true; |
||||||
365 | } |
||||||
366 | |||||||
367 | if (isset ($phpVersion['apache']) && $phpVersion['apache']['$'] == 'on') { |
||||||
368 | $versions[$phpVersion['key']['$']]['php_mode_fcgi_apache'] = true; |
||||||
369 | } |
||||||
370 | |||||||
371 | if (isset ($phpVersion['fpm']) && $phpVersion['fpm']['$'] == 'on' && $phpVersion['key']['$'] == $userFPMVersion) { |
||||||
372 | $versions[$phpVersion['key']['$']]['php_mode_fcgi_nginxfpm'] = true; |
||||||
373 | } |
||||||
374 | |||||||
375 | } |
||||||
376 | |||||||
377 | add('ispmanager_phplist', $versions); |
||||||
378 | }); |
||||||
379 | |||||||
380 | desc('Print allowed PHP modes and versions'); |
||||||
381 | task('ispmanager:print-php-list', function () { |
||||||
382 | invoke('ispmanager:get-php-list'); |
||||||
383 | |||||||
384 | $versions = get('ispmanager_phplist'); |
||||||
385 | writeln ("PHP versions: "); |
||||||
386 | writeln(str_repeat('*', 32)); |
||||||
387 | foreach ($versions as $versionKey => $versionData) { |
||||||
388 | writeln('PHP ' . $versionData['name'] . ' (ID: ' . $versionKey . ')'); |
||||||
389 | writeln(str_repeat('*', 32)); |
||||||
390 | if (!$versionData['php_mode_mod']) { |
||||||
391 | writeln('Apache module support (php_mode_mod) - <fg=red;options=bold>NO</>'); |
||||||
392 | } |
||||||
393 | else { |
||||||
394 | writeln('Apache module support (php_mode_mod) - <fg=green;options=bold>YES</>'); |
||||||
395 | } |
||||||
396 | |||||||
397 | if (!$versionData['php_mode_cgi']) { |
||||||
398 | writeln('CGI support (php_mode_cgi) - <fg=red;options=bold>NO</>'); |
||||||
399 | } |
||||||
400 | else { |
||||||
401 | writeln('CGI support (php_mode_cgi) - <fg=green;options=bold>YES</>'); |
||||||
402 | } |
||||||
403 | |||||||
404 | if (!$versionData['php_mode_fcgi_apache']) { |
||||||
405 | writeln('Apache fast-cgi support (php_mode_fcgi_apache) - <fg=red;options=bold>NO</>'); |
||||||
406 | } |
||||||
407 | else { |
||||||
408 | writeln('Apache fast-cgi support (php_mode_fcgi_apache) - <fg=green;options=bold>YES</>'); |
||||||
409 | } |
||||||
410 | |||||||
411 | if (!$versionData['php_mode_fcgi_nginxfpm']) { |
||||||
412 | writeln('nginx fast-cgi support (php_mode_fcgi_nginxfpm) - <fg=red;options=bold>NO</>'); |
||||||
413 | } |
||||||
414 | else { |
||||||
415 | writeln('nginx fast-cgi support (php_mode_fcgi_nginxfpm) - <fg=green;options=bold>YES</>'); |
||||||
416 | } |
||||||
417 | |||||||
418 | writeln(str_repeat('*', 32)); |
||||||
419 | } |
||||||
420 | }); |
||||||
421 | |||||||
422 | desc('Switch PHP version for domain'); |
||||||
423 | task('ispmanager:domain-php-select', function () { |
||||||
424 | $config = get ('ispmanager'); |
||||||
425 | |||||||
426 | if (is_null ($config['phpSelect'])) { |
||||||
427 | warning ('Action for domain update is not active'); |
||||||
428 | return; |
||||||
429 | } |
||||||
430 | |||||||
431 | if (!isset ($config['phpSelect']['name']) || $config['phpSelect']['name'] == '') { |
||||||
432 | throw new Exception('Invalid domain name!'); |
||||||
433 | } |
||||||
434 | |||||||
435 | $existDomains = get ('ispmanager_domains'); |
||||||
436 | |||||||
437 | if (!in_array ($config['phpSelect']['name'], $existDomains)) { |
||||||
0 ignored issues
–
show
It seems like
$existDomains can also be of type string ; however, parameter $haystack of in_array() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
438 | throw new Exception('Domain not exist!'); |
||||||
439 | } |
||||||
440 | |||||||
441 | if (!isset ($config['phpSelect']['mode']) || !isset ($config['phpSelect']['version'])) { |
||||||
442 | throw new Exception('Incorrect settings for select php version'); |
||||||
443 | return; |
||||||
0 ignored issues
–
show
return is not reachable.
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed. Unreachable code is most often the result of function fx() {
try {
doSomething();
return true;
}
catch (\Exception $e) {
return false;
}
return false;
}
In the above example, the last
Loading history...
|
|||||||
444 | } |
||||||
445 | |||||||
446 | $phpVersions = get ('ispmanager_phplist'); |
||||||
447 | |||||||
448 | $newVersion = $config['phpSelect']['version']; |
||||||
449 | $newMode = $config['phpSelect']['mode']; |
||||||
450 | |||||||
451 | if (!isset ($phpVersions[$newVersion])) { |
||||||
452 | throw new Exception('Incorrect php version'); |
||||||
453 | return; |
||||||
454 | } |
||||||
455 | |||||||
456 | $versionData = $phpVersions[$newVersion]; |
||||||
457 | |||||||
458 | if (!isset ($versionData[$newMode]) || !$versionData[$newMode]) { |
||||||
459 | throw new Exception('Incorrect php mode'); |
||||||
460 | } |
||||||
461 | |||||||
462 | $domainUpdateRequest = [ |
||||||
463 | 'func' => 'webdomain.edit', |
||||||
464 | 'elid' => $config['phpSelect']['name'], |
||||||
465 | 'name' => $config['phpSelect']['name'], |
||||||
466 | 'php_mode' => $newMode, |
||||||
467 | 'sok' => 'ok', |
||||||
468 | ]; |
||||||
469 | |||||||
470 | if ($newMode == 'php_mode_mod') { |
||||||
471 | $domainUpdateRequest['php_apache_version'] = $newVersion; |
||||||
472 | } |
||||||
473 | elseif ($newMode == 'php_mode_cgi') { |
||||||
474 | $domainUpdateRequest['php_cgi_version'] = $newVersion; |
||||||
475 | } |
||||||
476 | elseif ($newMode == 'php_mode_fcgi_apache') { |
||||||
477 | $domainUpdateRequest['php_cgi_version'] = $newVersion; |
||||||
478 | $domainUpdateRequest['php_apache_version'] = $newVersion; |
||||||
479 | } |
||||||
480 | elseif ($newMode == 'php_mode_fcgi_nginxfpm') { |
||||||
481 | $domainUpdateRequest['php_cgi_version'] = $newVersion; |
||||||
482 | $domainUpdateRequest['php_fpm_version'] = $newVersion; |
||||||
483 | } |
||||||
484 | else { |
||||||
485 | throw new Exception('Unknown PHP mode!'); |
||||||
486 | } |
||||||
487 | |||||||
488 | $response = ispmanagerRequest('post', $domainUpdateRequest); |
||||||
489 | |||||||
490 | if (isset ($response['doc']['error']['msg']['$'])) { |
||||||
491 | throw new Exception($response['doc']['error']['msg']['$']); |
||||||
492 | } |
||||||
493 | else { |
||||||
494 | info ('PHP successfully selected'); |
||||||
495 | } |
||||||
496 | }); |
||||||
497 | |||||||
498 | desc('Create new domain alias'); |
||||||
499 | task('ispmanager:domain-alias-create', function () { |
||||||
500 | $config = get ('ispmanager'); |
||||||
501 | |||||||
502 | if (is_null ($config['createAlias'])) { |
||||||
503 | warning ('Action for alias create is not active'); |
||||||
504 | return; |
||||||
505 | } |
||||||
506 | |||||||
507 | if (!isset ($config['createAlias']['name']) || $config['createAlias']['name'] == '') { |
||||||
508 | throw new Exception('Invalid domain name!'); |
||||||
509 | } |
||||||
510 | |||||||
511 | $existDomains = get ('ispmanager_domains'); |
||||||
512 | |||||||
513 | if (!in_array ($config['createAlias']['name'], $existDomains)) { |
||||||
0 ignored issues
–
show
It seems like
$existDomains can also be of type string ; however, parameter $haystack of in_array() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
514 | throw new Exception('Domain not exist!'); |
||||||
515 | } |
||||||
516 | |||||||
517 | if (!isset ($config['createAlias']['alias']) || $config['createAlias']['alias'] == '') { |
||||||
518 | throw new Exception('Invalid alias name!'); |
||||||
519 | } |
||||||
520 | |||||||
521 | // Get current domain data |
||||||
522 | $response = ispmanagerRequest('get', [ |
||||||
523 | 'func' => 'webdomain.edit', |
||||||
524 | 'elid' => $config['createAlias']['name'], |
||||||
525 | 'elname' => $config['createAlias']['name'], |
||||||
526 | ]); |
||||||
527 | |||||||
528 | $existAliases = []; |
||||||
529 | if (isset ($response['doc']['aliases']['$'])) { |
||||||
530 | $existAliases = explode (' ', $response['doc']['aliases']['$']); |
||||||
531 | } |
||||||
532 | |||||||
533 | $newAliasList = []; |
||||||
534 | $createAliasList = explode (' ', $config['createAlias']['alias']); |
||||||
535 | foreach ($createAliasList as $createAlias) { |
||||||
536 | if (in_array ($createAlias, $existAliases)) { |
||||||
537 | if (!isset ($config['createAlias']['skipIfExist']) || !$config['createAlias']['skipIfExist']) { |
||||||
538 | throw new Exception('Alias already exists!'); |
||||||
539 | } |
||||||
540 | else { |
||||||
541 | warning ('Alias ' . $createAlias . ' already exists - skipping'); |
||||||
542 | continue; |
||||||
543 | } |
||||||
544 | } |
||||||
545 | |||||||
546 | $newAliasList[] = $createAlias; |
||||||
547 | } |
||||||
548 | |||||||
549 | $saveAliases = array_merge($existAliases, $newAliasList); |
||||||
550 | |||||||
551 | $domainUpdateRequest = [ |
||||||
552 | 'func' => 'webdomain.edit', |
||||||
553 | 'elid' => $config['createAlias']['name'], |
||||||
554 | 'name' => $config['createAlias']['name'], |
||||||
555 | 'aliases' => implode (' ', $saveAliases), |
||||||
556 | 'sok' => 'ok', |
||||||
557 | ]; |
||||||
558 | |||||||
559 | $response = ispmanagerRequest('post', $domainUpdateRequest); |
||||||
560 | |||||||
561 | if (isset ($response['doc']['error']['msg']['$'])) { |
||||||
562 | throw new Exception($response['doc']['error']['msg']['$']); |
||||||
563 | } |
||||||
564 | else { |
||||||
565 | info ('Alias successfully created'); |
||||||
566 | } |
||||||
567 | }); |
||||||
568 | |||||||
569 | desc('Delete domain alias'); |
||||||
570 | task('ispmanager:domain-alias-delete', function () { |
||||||
571 | $config = get ('ispmanager'); |
||||||
572 | |||||||
573 | if (is_null ($config['deleteAlias'])) { |
||||||
574 | warning ('Action for alias create is not active'); |
||||||
575 | return; |
||||||
576 | } |
||||||
577 | |||||||
578 | if (!isset ($config['deleteAlias']['name']) || $config['deleteAlias']['name'] == '') { |
||||||
579 | throw new Exception('Invalid domain name!'); |
||||||
580 | } |
||||||
581 | |||||||
582 | $existDomains = get ('ispmanager_domains'); |
||||||
583 | |||||||
584 | if (!in_array ($config['deleteAlias']['name'], $existDomains)) { |
||||||
0 ignored issues
–
show
It seems like
$existDomains can also be of type string ; however, parameter $haystack of in_array() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
585 | throw new Exception('Domain not exist!'); |
||||||
586 | } |
||||||
587 | |||||||
588 | if (!isset ($config['deleteAlias']['alias']) || $config['deleteAlias']['alias'] == '') { |
||||||
589 | throw new Exception('Invalid alias name!'); |
||||||
590 | } |
||||||
591 | |||||||
592 | // Get current domain data |
||||||
593 | $response = ispmanagerRequest('get', [ |
||||||
594 | 'func' => 'webdomain.edit', |
||||||
595 | 'elid' => $config['createAlias']['name'], |
||||||
596 | 'elname' => $config['createAlias']['name'], |
||||||
597 | ]); |
||||||
598 | |||||||
599 | $existAliases = []; |
||||||
600 | if (isset ($response['doc']['aliases']['$'])) { |
||||||
601 | $existAliases = explode (' ', $response['doc']['aliases']['$']); |
||||||
602 | } |
||||||
603 | |||||||
604 | $deleteAliasList = explode (' ', $config['deleteAlias']['alias']); |
||||||
605 | foreach ($deleteAliasList as $deleteAlias) { |
||||||
606 | if (!in_array($deleteAlias, $existAliases)) { |
||||||
607 | if (!isset ($config['deleteAlias']['skipIfNotExist']) || !$config['deleteAlias']['skipIfNotExist']) { |
||||||
608 | throw new Exception('Alias not exist!'); |
||||||
609 | } |
||||||
610 | else { |
||||||
611 | warning ('Alias ' . $deleteAlias . ' not exist - skipping'); |
||||||
612 | continue; |
||||||
613 | } |
||||||
614 | } |
||||||
615 | |||||||
616 | if (($index = array_search ($deleteAlias, $existAliases))!== FALSE){ |
||||||
617 | unset ($existAliases[$index]); |
||||||
618 | } |
||||||
619 | } |
||||||
620 | |||||||
621 | $domainUpdateRequest = [ |
||||||
622 | 'func' => 'webdomain.edit', |
||||||
623 | 'elid' => $config['deleteAlias']['name'], |
||||||
624 | 'name' => $config['deleteAlias']['name'], |
||||||
625 | 'aliases' => implode (' ', $existAliases), |
||||||
626 | 'sok' => 'ok', |
||||||
627 | ]; |
||||||
628 | |||||||
629 | $response = ispmanagerRequest('post', $domainUpdateRequest); |
||||||
630 | |||||||
631 | if (isset ($response['doc']['error']['msg']['$'])) { |
||||||
632 | throw new Exception($response['doc']['error']['msg']['$']); |
||||||
633 | } |
||||||
634 | else { |
||||||
635 | info ('Alias successfully deleted'); |
||||||
636 | } |
||||||
637 | }); |
||||||
638 | |||||||
639 | desc('Delete domain'); |
||||||
640 | task('ispmanager:domain-delete', function () { |
||||||
641 | $config = get ('ispmanager'); |
||||||
642 | |||||||
643 | if (is_null ($config['deleteDomain'])) { |
||||||
644 | warning ('Action for domain delete is not active'); |
||||||
645 | return; |
||||||
646 | } |
||||||
647 | |||||||
648 | if (!isset ($config['deleteDomain']['name']) || $config['deleteDomain']['name'] == '') { |
||||||
649 | throw new Exception('Invalid domain name!'); |
||||||
650 | } |
||||||
651 | |||||||
652 | // Check domain exists |
||||||
653 | $existDomains = get ('ispmanager_domains'); |
||||||
654 | |||||||
655 | if (!in_array ($config['deleteDomain']['name'], $existDomains)) { |
||||||
0 ignored issues
–
show
It seems like
$existDomains can also be of type string ; however, parameter $haystack of in_array() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
656 | if (!isset ($config['deleteDomain']['skipIfNotExist']) || !$config['deleteDomain']['skipIfNotExist']) { |
||||||
657 | throw new Exception('Domain not exist!'); |
||||||
658 | } |
||||||
659 | else { |
||||||
660 | warning ('Domain not exist - skipping'); |
||||||
661 | return true; |
||||||
662 | } |
||||||
663 | } |
||||||
664 | |||||||
665 | // Build request |
||||||
666 | $domainDeleteRequest = [ |
||||||
667 | 'func' => 'webdomain.delete.confirm', |
||||||
668 | 'elid' => $config['deleteDomain']['name'], |
||||||
669 | 'sok' => 'ok', |
||||||
670 | ]; |
||||||
671 | |||||||
672 | if (!isset ($config['deleteDomain']['removeDir']) || !$config['deleteDomain']['removeDir']) { |
||||||
673 | $domainDeleteRequest['remove_directory'] = 'off'; |
||||||
674 | } |
||||||
675 | else { |
||||||
676 | $domainDeleteRequest['remove_directory'] = 'on'; |
||||||
677 | } |
||||||
678 | |||||||
679 | $response = ispmanagerRequest('post', $domainDeleteRequest); |
||||||
680 | |||||||
681 | if (isset ($response['doc']['error']['msg']['$'])) { |
||||||
682 | throw new Exception($response['doc']['error']['msg']['$']); |
||||||
683 | } |
||||||
684 | else { |
||||||
685 | info ('Domain successfully deleted'); |
||||||
686 | } |
||||||
687 | }); |
||||||
688 | |||||||
689 | desc('Auto task processing'); |
||||||
690 | task('ispmanager:process', function () { |
||||||
691 | $config = get ('ispmanager'); |
||||||
692 | |||||||
693 | if (!is_null ($config['createDatabase'])) { |
||||||
694 | invoke('ispmanager:db-create'); |
||||||
695 | } |
||||||
696 | |||||||
697 | if (!is_null ($config['deleteDatabase'])) { |
||||||
698 | invoke('ispmanager:db-delete'); |
||||||
699 | } |
||||||
700 | |||||||
701 | if (!is_null ($config['createDomain'])) { |
||||||
702 | invoke('ispmanager:domain-create'); |
||||||
703 | } |
||||||
704 | |||||||
705 | if (!is_null ($config['deleteDomain'])) { |
||||||
706 | invoke('ispmanager:domain-delete'); |
||||||
707 | } |
||||||
708 | |||||||
709 | if (!is_null ($config['phpSelect'])) { |
||||||
710 | invoke('ispmanager:domain-php-select'); |
||||||
711 | } |
||||||
712 | |||||||
713 | if (!is_null ($config['createAlias'])) { |
||||||
714 | invoke('ispmanager:domain-alias-create'); |
||||||
715 | } |
||||||
716 | |||||||
717 | if (!is_null ($config['deleteAlias'])) { |
||||||
718 | invoke('ispmanager:domain-alias-delete'); |
||||||
719 | } |
||||||
720 | }); |
||||||
721 | |||||||
722 | function ispmanagerRequest ($method, $requestData) { |
||||||
723 | $config = get ('ispmanager'); |
||||||
724 | $dsnData = parse_url($config['api']['dsn']); |
||||||
725 | |||||||
726 | $requestUrl = $dsnData['scheme'] . '://' . $dsnData['host'] . ':' . $dsnData['port'] . $dsnData['path']; |
||||||
727 | |||||||
728 | if ($config['api']['secure'] && get('ispmanager_session') == '') { |
||||||
729 | ispmanagerAuthRequest ($requestUrl, $dsnData['user'], $dsnData['pass']); |
||||||
730 | } |
||||||
731 | |||||||
732 | if ($method == 'post') { |
||||||
733 | return Httpie::post($requestUrl) |
||||||
734 | ->form (prepareRequest($requestData)) |
||||||
735 | ->setopt(CURLOPT_SSL_VERIFYPEER, false) |
||||||
736 | ->setopt(CURLOPT_SSL_VERIFYHOST, false) |
||||||
737 | ->getJson(); |
||||||
738 | } |
||||||
739 | elseif ($method == 'get') { |
||||||
740 | return Httpie::get($requestUrl) |
||||||
741 | ->query(prepareRequest($requestData)) |
||||||
742 | ->setopt(CURLOPT_SSL_VERIFYPEER, false) |
||||||
743 | ->setopt(CURLOPT_SSL_VERIFYHOST, false) |
||||||
744 | ->getJson(); |
||||||
745 | } |
||||||
746 | else { |
||||||
747 | throw new Exception('Unknown request method'); |
||||||
748 | } |
||||||
749 | } |
||||||
750 | |||||||
751 | function ispmanagerAuthRequest ($url, $login, $pass) { |
||||||
752 | $authRequestData = [ |
||||||
753 | 'func' => 'auth', |
||||||
754 | 'username' => $login, |
||||||
755 | 'password' => $pass, |
||||||
756 | ]; |
||||||
757 | |||||||
758 | $responseData = Httpie::post($url) |
||||||
759 | ->setopt(CURLOPT_SSL_VERIFYPEER, false) |
||||||
760 | ->setopt(CURLOPT_SSL_VERIFYHOST, false) |
||||||
761 | ->form (prepareRequest($authRequestData)) |
||||||
762 | ->getJson(); |
||||||
763 | |||||||
764 | if (isset ($responseData['doc']['auth']['$id'])) { |
||||||
765 | set('ispmanager_session', $responseData['doc']['auth']['$id']); |
||||||
766 | } |
||||||
767 | else { |
||||||
768 | throw new Exception('Error while create auth session'); |
||||||
769 | } |
||||||
770 | } |
||||||
771 | |||||||
772 | function prepareRequest ($requestData) { |
||||||
773 | $config = get ('ispmanager'); |
||||||
774 | $dsnData = parse_url($config['api']['dsn']); |
||||||
775 | |||||||
776 | if (!isset ($requestData['out'])) { |
||||||
777 | $requestData['out'] = 'json'; |
||||||
778 | } |
||||||
779 | |||||||
780 | if (!$config['api']['secure']) { |
||||||
781 | $requestData['authinfo'] = $dsnData['user'] . ':' . $dsnData['pass']; |
||||||
782 | } |
||||||
783 | else { |
||||||
784 | if (get('ispmanager_session') != '') { |
||||||
785 | $requestData['auth'] = get('ispmanager_session'); |
||||||
786 | } |
||||||
787 | } |
||||||
788 | |||||||
789 | return $requestData; |
||||||
790 | } |
||||||
791 | |||||||
792 | function generatePassword ($lenght) { |
||||||
793 | return substr (md5(uniqid()), 0, $lenght); |
||||||
794 | } |
||||||
795 | |||||||
796 | // Callbacks before actions under domains |
||||||
797 | before('ispmanager:domain-alias-create', 'ispmanager:init'); |
||||||
798 | before('ispmanager:domain-alias-delete', 'ispmanager:init'); |
||||||
799 | before('ispmanager:domain-create', 'ispmanager:init'); |
||||||
800 | before('ispmanager:domain-delete', 'ispmanager:init'); |
||||||
801 | before('ispmanager:domain-php-select', 'ispmanager:init'); |
||||||
802 | |||||||
803 | // Callbacks before actions under databases |
||||||
804 | before('ispmanager:db-create', 'ispmanager:init'); |
||||||
805 | before('ispmanager:db-delete', 'ispmanager:init'); |