This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
ispconfig-blocks.php
802 lines (649 loc) · 28.2 KB
/
ispconfig-blocks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
<?php
class BlockException extends Exception
{
}
add_action('init', array( 'IspconfigBlock', 'init' ), 20);
function array_map_assoc(callable $f, array $a)
{
return array_column(array_map($f, array_keys($a), $a), 1, 0);
}
class IspconfigBlock
{
private $postData = [];
public static function init()
{
new self();
}
public function __construct()
{
add_action('enqueue_block_editor_assets', [$this, 'LoadAssets']);
// dynamic gutenberg block rendering
register_block_type('ole1986/ispconfig-block', [
'render_callback' => [$this, 'Output']
]);
}
public function LoadAssets()
{
wp_enqueue_script('ole1986-ispconfig-blocks', WPISPCONFIG3_PLUGIN_URL . 'js/ispconfig-blocks.js', [ 'wp-blocks', 'wp-i18n' ], WPISPCONFIG3_VERSION);
wp_enqueue_style('ole1986-ispconfig-blocks', WPISPCONFIG3_PLUGIN_URL . 'style/ispconfig-blocks.css', null, WPISPCONFIG3_VERSION);
// script translations requires the json files (in ped format)
// to be available in the languages directory
wp_set_script_translations('ole1986-ispconfig-blocks', 'wp-ispconfig3', WPISPCONFIG3_PLUGIN_DIR . 'languages');
}
private function alert($message)
{
return '<div class="ispconfig-msg ispconfig-msg-error">' . $message . '</div>';
}
private function info($message)
{
return '<div class="ispconfig-msg">' . $message . '</div>';
}
private function success($message)
{
return '<div class="ispconfig-msg ispconfig-msg-success">' . $message . '</div>';
}
protected function createClient($props, &$content)
{
$postData = $this->postData;
if (WPISPConfig3::$OPTIONS['user_create_wordpress']) {
// when user creation is enabled for wordpress, add it accordingly
$user_id = wp_insert_user([
'user_login' => $postData['client_username'],
'user_pass' => $postData['client_password'],
'user_email' => $postData['client_email']
]);
if (is_wp_error($user_id)) {
$content .= $this->alert('Failed to create wp user: ' . $user_id->get_error_message());
return false;
}
}
$opt = [
'contact_name' => $postData['client_contact_name'],
'email' => $postData['client_email'],
'username' => $postData['client_username'],
'password' => $postData['client_password']
];
if (!empty($postData['client_template_master'])) {
// check if template exists when defined
$templates = Ispconfig::$Self->GetClientTemplates();
$found = array_filter($templates, function ($template) use ($postData) {
return $template['template_id'] == intval($postData['client_template_master']);
});
if (empty($found)) {
$content .= $this->alert('No client template found for id ' . $templateField['value']);
return false;
}
$opt['template_master'] = $postData['client_template_master'];
}
Ispconfig::$Self->AddClient($opt);
$content .= $this->success('Client ' . $postData['client_contact_name'] . ' successfully added');
return true;
}
protected function lockClient($props, &$content)
{
$postData = $this->postData;
$user = Ispconfig::$Self->GetClientByUser($postData['client_username']);
if (empty($user)) {
$content .= $this->alert('Client ID not found');
return false;
}
$opt = ['username' => strtolower($user['username']), 'locked' => 'y', 'canceled' => 'y'];
Ispconfig::$Self->UpdClient($opt);
$content .= $this->success("Client account " . $user['username'] . ' locked');
return true;
}
protected function unlockClient($props, &$content)
{
$postData = $this->postData;
$user = Ispconfig::$Self->GetClientByUser($postData['client_username']);
if (empty($user)) {
$content .= $this->alert('Client ID not found');
return false;
}
$opt = ['username' => strtolower($user['username']), 'locked' => 'y', 'canceled' => 'y'];
Ispconfig::$Self->UpdClient($opt);
$content .= $this->success("Client account " . $user['username'] . ' unlocked');
return true;
}
protected function createWebsite($props, &$content)
{
$postData = $this->postData;
if (!Ispconfig::$Self->GetClientID()) {
$user = Ispconfig::$Self->GetClientByUser($postData['client_username']);
if (empty($user)) {
throw new BlockException('Client user could not be found');
}
}
$domain = Ispconfig::$Self->AddWebsite(['domain' => $postData['website_domain']]);
$content .= $this->success("Website " . $postData['website_domain'] . " successfully created (".$domain.")");
return true;
}
protected function createDatabase($props, &$content, $website_id = 0)
{
$postData = $this->postData;
$user = Ispconfig::$Self->GetClientByUser($postData['client_username']);
if (empty($user)) {
throw new BlockException('Client user could not be found');
}
$prefix = preg_replace("/\.-/", "_", $user['username']);
$dbuserid = Ispconfig::$Self->AddDatabaseUser([
'database_user' => $prefix . '_' . $postData['database_user'],
'database_password' => $postData['database_password']
]);
$database = Ispconfig::$Self->AddDatabase([
'website_id' => $website_id,
'database_name' => $prefix . '_' . $postData['database_name'],
'database_user_id' => $dbuserid
]);
$content .= $this->success("Database " . $postData['database_name'] . " successfully created");
return true;
}
protected function createMail($props, &$content)
{
$postData = $this->postData;
$part = preg_split('/@/', $postData['mail_address']);
if (count($part) < 2) {
$this->alert('Invalid mail address');
return;
}
try {
$domain = Ispconfig::validateDomain($part[1]);
} catch (BlockException $e) {
$this->alert($e->getMessage());
return;
}
$result = Ispconfig::$Self->GetMailDomainByDomain($domain);
$client = Ispconfig::$Self->GetClientByGroupID($result['sys_groupid']);
print_r($client);
}
protected function updateClient($props, &$content)
{
$postData = $this->postData;
$user = Ispconfig::$Self->GetClientByUser($postData['client_username']);
if (empty($user)) {
$content .= $this->alert('Client ID not found');
return false;
}
Ispconfig::$Self->UpdClient(['username' => $postData['client_username'], 'company_name' => $postData['client_company_name'], 'contact_name' => $postData['client_contact_name']]);
$content .= $this->success("Client information updated");
return true;
}
protected function updateClientDetails($props, &$content)
{
$postData = $this->postData;
$user = Ispconfig::$Self->GetClientByUser($postData['client_username']);
if (empty($user)) {
$content .= $this->alert('Client ID not found');
return false;
}
$data = [];
foreach ($postData as $key => $value) {
if ($key == 'client_username') {
continue;
}
if (preg_match('/^client_/', $key)) {
$newKey = substr($key, 7);
$data[$newKey] = $value;
}
}
$data['username'] = $user['username'];
Ispconfig::$Self->UpdClient($data);
$content .= $this->success("Client information updated");
return true;
}
private function computeField(&$field)
{
switch ($field['computed']) {
default:
break;
case 'session':
if (!session_id()) {
session_start();
}
// assuming that a cumputed 'session' field
// is always the 'client_username' from action_create_client
$field['value'] = $_SESSION['ispconfig']['action_create_client']['postData'][$field['id']];
$field['readonly'] = true;
break;
case 'generate':
$field['value'] = substr(sha1(uniqid(rand(), true)), 0, 10);
$field['readonly'] = true;
break;
case 'wp-locale':
$lang = get_locale();
if (!empty($lang)) {
$field['value'] = substr($lang, 0, 2);
}
break;
case 'wp-user':
$user = wp_get_current_user();
$field['value'] = $user->user_login;
$field['readonly'] = true;
break;
case 'wp-name':
$user = wp_get_current_user();
$field['value'] = $user->user_nicename;
break;
case 'wp-email':
$user = wp_get_current_user();
$field['value'] = $user->user_email;
break;
case 'get-data':
if (!empty($_GET[$field['value']])) {
$field['value'] = $_GET[$field['value']];
} else {
$field['value'] = '';
}
$field['computed'] = '';
break;
}
}
/**
* Used to optionally fill input fields with existing data from ISPConfig3
*/
private function onLoad($props, &$content)
{
if ('POST' === $_SERVER[ 'REQUEST_METHOD' ]) {
return true;
}
$fields = array_filter($props['fields'], function ($field) {
return isset($field['id']) && $field['id'] == 'client_username';
});
$loginField = array_pop($fields);
$loginFieldIsEmptyAndNonEditable = empty($loginField['value']) && ($loginField['readonly'] || $loginField['hidden']);
if ($loginFieldIsEmptyAndNonEditable) {
$content .= $this->alert('No client login provided');
return false;
}
if (isset($loginField) && $loginField['computed'] == 'session' && empty($_SESSION['ispconfig'])) {
$content .= $this->alert('The session has been expired for the client username');
return false;
}
if (isset($loginField) && $loginField['computed'] == 'wp-user' && !is_user_logged_in()) {
$content .= $this->alert(__('You need to sign in before continue', 'wp-ispconfig3'));
return false;
}
switch ($props['action']) {
default:
break;
case 'action_create_client':
$fields = array_filter($props['fields'], function ($field) {
return $field['id'] == 'client_template_master';
});
$templateField = array_pop($fields);
if ($templateField != null) {
if (empty($templateField['value'])) {
$content .= $this->alert('No client template provided. Either delete the field or set a proper ISPConfig3 template id');
return true;
}
Ispconfig::$Self->withSoap();
$templates = Ispconfig::$Self->GetClientTemplates();
$found = array_filter($templates, function ($template) use ($templateField) {
return $template['template_id'] == intval($templateField['value']);
});
if (empty($found)) {
$content .= $this->alert('No client template found for id ' . $templateField['value']);
return true;
}
}
break;
case 'action_create_mail':
$content .= $this->alert('Creating email accounts is not yet implemented');
return true;
break;
case 'action_update_client_bank':
case 'action_update_client':
// fetch client info from ISPConfig REST API
// to prefill the post data
Ispconfig::$Self->withSoap();
$user = Ispconfig::$Self->GetClientByUser($loginField['value'], true);
$this->postData = array_map_assoc(function ($k, $v) {
return ['client_'. $k, $v];
}, (array)$user);
break;
case 'action_locknow_client':
return false;
break;
}
return true;
}
/**
* Recover all hidden/read-only fields with its original value
* @param $props properties defined in ispconfig-blocks.js through gutenberg blocks
*/
private function recoverFieldValues($props)
{
if (empty($props['fields'])) {
return;
}
foreach ($props['fields'] as $field) {
// read-only fields cannot be changed
// so recover them from its original value
if ($field['readonly'] || $field['hidden']) {
$this->postData[$field['id']] = $field['value'];
}
}
}
private function validatePostData($action, $props, &$content)
{
$postData = $this->postData;
switch ($action) {
case 'action_create_client':
if (empty($postData['client_username'])) {
$content .= $this->alert('No client username defined');
return false;
}
$user = Ispconfig::$Self->GetClientByUser($postData['client_username']);
if (empty($user)) {
// check if the user already exist in wordpress
if (WPISPConfig3::$OPTIONS['user_create_wordpress'] && username_exists($postData['client_username'])) {
$content .= $this->alert('The given username already exists in the other system');
return false;
}
// when the user does not exist, continue validating required
// field to create a new client
if (empty($postData['client_contact_name'])) {
$content .= $this->alert('No client contact name defined');
return false;
}
if (empty($postData['client_email'])) {
$content .= $this->alert('No client email defined');
return false;
}
if (!is_email($postData['client_email'])) {
$content .= $this->alert('N valid client email address defined');
return false;
}
if (empty($postData['client_password'])) {
$content .= $this->alert('No client password defined');
return false;
}
if (strlen($postData['client_password']) < 5) {
$content .= $this->alert('Client password is to short');
return false;
}
}
break;
case 'action_create_website':
if (empty($postData['website_domain'])) {
$content .= $this->alert('No website given');
return false;
}
break;
case 'action_create_database':
if (empty($postData['database_name'])) {
$content .= $this->alert('No database name given');
return false;
}
if (empty($postData['database_password'])) {
$content .= $this->alert('No database password given');
return false;
}
break;
case 'action_update_client':
if (empty($postData['client_contact_name'])) {
$content .= $this->alert('No client contact name defined');
return false;
}
if (empty($postData['client_language'])) {
$content .= $this->alert('No language given');
return false;
}
if (!preg_match("/^[A-Za-z]{2}$/", $postData['client_language'])) {
$content .= $this->alert('No valid ISO 639-1 language code. (E.g. en)');
return false;
}
break;
case 'action_update_client_bank':
if (empty($postData['client_bank_account_owner'])) {
$content .= $this->alert('Missing bank account owner');
return false;
}
if (empty($postData['client_bank_name'])) {
$content .= $this->alert('Missing bank name');
return false;
}
if (!is_email($postData['client_bank_account_iban'])) {
$content .= $this->alert('Missing bank account number');
return false;
}
break;
case 'action_check_domain':
try {
$postData['domain_name'] = $this->postData['domain_name'] = Ispconfig::$Self->validateDomain($postData['domain_name']);
} catch (BlockException $e) {
$content .= $this->alert($e->getMessage());
return false;
}
break;
}
return true;
}
/**
* Process the SOAP action including those which are privously added into session
* @return bool true when all actions executed, false when user action required or something faled
*/
private function processAction($props, &$content, $skipSession = false)
{
$ok = false;
$postData = $this->postData;
if (!$skipSession && isset($_SESSION['ispconfig'])) {
$pending = array_filter($_SESSION['ispconfig'], function ($v, $k) {
return !isset($v['_processed']);
}, ARRAY_FILTER_USE_BOTH);
uksort($pending, function ($a, $b) {
// always make sure client creation is take first
if ($a == 'action_create_client') {
return -1;
}
return strcasecmp($a, $b);
});
foreach ($pending as $k => $v) {
$this->postData = $v['postData'];
// mark current action as processed before
$_SESSION['ispconfig'][$k]['_processed'] = true;
// recursive function call
if (!$this->processAction($v['props'], $content, true)) {
// but abort when an action returned false
throw new BlockException("Process aborted");
}
}
$this->postData = $postData;
}
switch ($this->postData['action']) {
case 'action_create_client':
$ok = $this->createClient($props, $content);
break;
case 'action_create_website':
$isAvailable = Ispconfig::$Self->IsDomainAvailable($this->postData['website_domain']);
if ($isAvailable) {
$ok = $this->createWebsite($props, $content);
} else {
$content .= $this->alert(__('The domain is already registered', 'wp-ispconfig3'));
}
break;
case 'action_create_database':
$ok = $this->createDatabase($props, $content);
break;
case 'action_create_mail':
$ok = $this->createMail($props, $content);
break;
case 'action_update_client':
$ok = $this->updateClient($props, $content);
break;
case 'action_update_client_bank':
$ok = $this->updateClientDetails($props, $content);
break;
case 'action_lock_client':
$ok = $this->lockClient($props, $content);
break;
case 'action_unlock_client':
$ok = $this->unlockClient($props, $content);
break;
case 'action_check_domain':
$isAvailable = Ispconfig::$Self->IsDomainAvailable($this->postData['domain_name']);
$ok = true;
if ($isAvailable) {
$content .= $this->success(__('The domain is available', 'wp-ispconfig3'));
if (!empty($props['submission']) && $props['submission']['action'] == 'continue' && !empty($props['submission']['url'])) {
$content .= $this->info('You will be redirected shortly...');
$content .= '<script>jQuery(function() {
document.location.href = "'. sprintf('%s?domain=%s', $props['submission']['url'], $this->postData['domain_name']) .'"
})</script>';
}
} else {
$content .= $this->alert(__('The domain is already registered', 'wp-ispconfig3'));
}
break;
default:
throw new BlockException("No action defined for " . $this->postData['action']);
}
return $ok;
}
private function onPost($props, &$content)
{
if ('POST' !== $_SERVER[ 'REQUEST_METHOD' ]) {
return false;
}
if (empty($_POST)) {
return false;
}
// sanitize text input of all $_POST data
foreach ($_POST as $key => $value) {
$this->postData[$key] = sanitize_text_field($value);
}
// recover the values of read-only fields
$this->recoverFieldValues($props);
// connect to ISPConfig REST API
Ispconfig::$Self->withSoap();
// validate the users input dependent on the given action
$valid = $this->validatePostData($this->postData['action'], $props, $content);
if (!$valid) {
// stop here, but keep session intact
return false;
}
if (isset($props['submission']) && $props['submission']['action'] == 'continue' && $this->postData['action'] != 'action_check_domain') {
if (!session_id()) {
session_start();
}
$_SESSION['ispconfig'][$this->postData['action']] = [
'props' => $props,
'postData' => $this->postData
];
wp_redirect($props['submission']['url']);
exit;
}
$stack[$this->postData['action']] = [
'props' => $props,
'postData' => $this->postData
];
if (isset($_SESSION['ispconfig'])) {
$stack += $_SESSION['ispconfig'];
}
return $this->processAction($props, $content);
}
public function Output($props, $content)
{
$buttonClass = 'btn btn-default';
$buttonPrimaryClass = 'btn btn-primary';
// set the default action if none is set
if (empty($props['action'])) {
$props['action'] = 'action_create_client';
}
// compute fields
if (is_array($props['fields'])) {
foreach ($props['fields'] as &$field) {
$this->computeField($field);
}
}
// update field from other plugin using "add_filter" hooks
$props['fields'] = apply_filters('ispconfig_block_' . $props['action'], $props['fields']);
try {
$onLoad = $this->onLoad($props, $content);
// abort the fields output when onLoad returns false
if (!$onLoad) {
// this may or may not contain previously added content
// E.g. error messages
return $content;
}
if ($this->onPost($props, $content)) {
// submit email to address when post succeeded
// and setting matches the current post action
if (in_array($this->postData['action'], (array)WPISPConfig3::$OPTIONS['confirm_actions'])) {
$email = $this->postData['client_email'];
if (empty($email) && !empty($_SESSION['ispconfig']['action_create_client'])) {
// try from session
$email = $_SESSION['ispconfig']['action_create_client']['postData']['client_email'];
}
$options = $this->postData;
if (!empty($_SESSION['ispconfig'])) {
$sessionPostData = array_column($_SESSION['ispconfig'], 'postData');
$sessionPostData = array_merge(...$sessionPostData);
$options += $sessionPostData;
}
if (!empty($email)) {
$sent = Ispconfig::$Self->SendConfirmation($email, $options);
if ($sent) {
$content .= $this->info('Confirmation email sent to' . $email);
}
} else {
$content .= $this->info('No email has been submitted due to missing email address');
}
}
// clear session when post call was succesfully
if (isset($_SESSION['ispconfig'])) {
unset($_SESSION['ispconfig']);
}
// only display alerts or notifications (if available) and skip the fields
return $content;
}
} catch (BlockException $e) {
unset($_SESSION['ispconfig']);
$content .= $this->alert($e->getMessage());
return $content;
} catch (SoapFault $e) {
unset($_SESSION['ispconfig']);
$content .= $this->alert($e->getMessage());
return $content;
}
$content.= "<form method='post'>";
ob_start();
foreach ($props['fields'] as &$field) {
$input_attr = [];
if ($field['hidden']) {
continue;
}
if ($field['readonly']) {
$input_attr = ['readonly' => 'true'];
}
if (!empty($this->postData[$field['id']])) {
$field['value'] = $this->postData[$field['id']];
}
$type = "text";
if ($field['password']) {
$type = "password";
}
WPISPConfig3::getField($field['id'], __($field['id'], 'wp-ispconfig3'), $type, ['container' => 'div', 'value' => $field['value'], 'input_attr' => $input_attr]);
}
$content.= ob_get_clean();
if (!empty($props['action'])) {
$button_title = __($props['action'], 'wp-ispconfig3');
if (!empty($props['submission']['button_title'])) {
$button_title = $props['submission']['button_title'];
}
$content .= '<div style="margin-top: 1em">';
$content .= '<input type="hidden" name="action" value="'.$props['action'] .'" />';
if (!empty($props['submission']['button_back'])) {
$back_button_title = "Back";
if (!empty($props['submission']['button_back_title'])) {
$back_button_title = $props['submission']['button_back_title'];
}
$content .= '<input type="button" class="'. $buttonClass .'" onclick="javascript:history.back();" value="'. $back_button_title .'" /> ';
}
$content .= '<input type="submit" class="'. $buttonPrimaryClass .'" name="submit" value="'. $button_title .'" />';
$content .= '</div>';
}
$content.= "</form>";
return $content;
}
}