-
Notifications
You must be signed in to change notification settings - Fork 12
/
zmt_ui.module
627 lines (572 loc) · 21 KB
/
zmt_ui.module
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
<?php
/**
* Implements hook_menu().
*/
function zmt_ui_menu() {
$items = array();
$items['admin/config/system/zmt'] = array(
'title' => t('Zimbra multi-tenancy settings'),
'description' => 'Zimbra multi-tenancy settings.',
'page callback' => 'zmt_setting_form_page',
'access arguments' => array('administer zmt'),
'file' => 'zmt.admin.inc',
);
$items['zmt/server/pull'] = array(
'title' => t('Pull data'),
'page callback' => 'zmt_server_pull_data_page',
'access callback' => 'zmt_server_add_access',
'type' => MENU_LOCAL_ACTION,
'file' => 'zmt.admin.inc',
);
$items['zmt/pulling/progress/%'] = array(
'title' => t('Pulling progress bar'),
'page callback' => 'zmt_pulling_progress',
'page arguments' => array(3),
'access callback' => 'zmt_server_add_access',
'type' => MENU_CALLBACK,
'file' => 'zmt.admin.inc',
);
$items['zmt/account/quota'] = array(
'title' => 'Mailbox Quota',
'page callback' => 'zmt_account_quota_usage',
'access callback' => 'zmt_account_list_access',
'type' => MENU_LOCAL_ACTION,
'file' => 'zmt.admin.inc',
);
return $items;
}
/**
* Implements hook_zmt_domain_presave().
*/
function zmt_ui_zmt_domain_presave($domain) {
module_load_include('inc', 'zmt_core', 'includes/zmt.zimbra');
$attrs = array(
'zimbraDomainStatus' => $domain->status,
);
$api = zimbra_admin_api_by_server($domain->server_id);
if (empty($domain->zimbra_id)) {
$zimbra_domain = zimbra_get_domain($api, $domain->name);
if (!$zimbra_domain) {
$zimbra_domain = zimbra_create_domain($api, $domain->name, $attrs);
}
else {
zimbra_modify_domain($api, $zimbra_domain->id, $attrs);
}
$domain->zimbra_id = isset($zimbra_domain->id) ? $zimbra_domain->id : '';
}
else {
zimbra_modify_domain($api, $domain->zimbra_id, $attrs);
}
}
/**
* Implements hook_zmt_domain_delete().
*/
function zmt_ui_zmt_domain_delete($domain) {
if ((int) $domain->delete_domain > 0 && !empty($domain->zimbra_id)) {
module_load_include('inc', 'zmt_core', 'includes/zmt.zimbra');
$api = zimbra_admin_api_by_server($domain->server_id);
zimbra_delete_domain($api, $domain->zimbra_id);
}
}
/**
* Implements hook_zmt_account_delete().
*/
function zmt_ui_zmt_account_delete($account) {
if ((int) $account->delete_account > 0 && !empty($account->zimbra_id)) {
module_load_include('inc', 'zmt_core', 'includes/zmt.zimbra');
$api = zimbra_admin_api_by_server($account->server_id);
zimbra_delete_account($api, $account->zimbra_id);
}
}
/**
* Implements hook_zmt_alias_presave().
*/
function zmt_ui_zmt_alias_presave($alias) {
module_load_include('inc', 'zmt_core', 'includes/zmt.zimbra');
if (!empty($alias->id)) {
$domain_name = $alias->domain;
$server_id = $alias->server_id;
$account_zimbra_id = $alias->account_zimbra_id;
}
else {
$account = zmt_account_load($alias->account_id);
$domain_name = !empty($account->domain) ? $account->domain : '';
$server_id = !empty($account->server_id) ? (int) $account->server_id : 0;
$account_zimbra_id = !empty($account->zimbra_id) ? $account->zimbra_id : '';
}
if (!empty($account_zimbra_id)) {
$api = zimbra_admin_api_by_server($server_id);
$alias_address = $alias->name . '@' . $domain_name;
if (empty($alias->id)) {
zimbra_add_account_alias($api, $account_zimbra_id, $alias_address);
}
else {
$old_alias = zmt_alias_load($alias->id);
$old_alias_address = $old_alias->name . '@' . $domain_name;
if ($account_zimbra_id != $alias->zimbra_target_id || $old_alias_address != $alias_address) {
zimbra_remove_account_alias($api, $old_alias_address, $alias->zimbra_target_id);
zimbra_add_account_alias($api, $account_zimbra_id, $alias_address);
}
}
$alias->zimbra_target_id = $account_zimbra_id;
}
}
/**
* Implements hook_zmt_alias_delete().
*/
function zmt_ui_zmt_alias_delete($alias) {
if ((int) $alias->delete_alias > 0 && !empty($alias->zimbra_target_id)) {
module_load_include('inc', 'zmt_core', 'includes/zmt.zimbra');
$api = zimbra_admin_api_by_server($alias->server_id);
zimbra_remove_account_alias($api, $alias->name . '@' . $alias->domain, $alias->zimbra_target_id);
}
}
/**
* Implements hook_zmt_group_delete().
*/
function zmt_ui_zmt_group_delete($group) {
if ((int) $group->delete_dl > 0 && !empty($group->zimbra_id)) {
module_load_include('inc', 'zmt_core', 'includes/zmt.zimbra');
$api = zimbra_admin_api_by_server($group->server_id);
zimbra_delete_dl($api, $group->zimbra_id);
}
}
/**
* Implements hook_zmt_group_member_presave().
*/
function zmt_ui_zmt_group_member_presave($member) {
if (!empty($member->id)) {
$group_name = $member->group_name;
$domain_name = $member->domain;
$server_id = $member->server_id;
}
else {
$group = zmt_group_load($member->group_id);
$group_name = $group->name;
$domain_name = !empty($group->domain) ? $group->domain : '';
$server_id = !empty($group->server_id) ? (int) $group->server_id : 0;
}
module_load_include('inc', 'zmt_core', 'includes/zmt.zimbra');
$api = zimbra_admin_api_by_server($server_id);
$dl = zimbra_get_dl($api, $group_name . '@' . $domain_name);
$members = zimbra_get_dl_members($api, $group_name . '@' . $domain_name);
if (!in_array($member->name, $members) && $dl) {
zimbra_add_dl_members($api, $dl->id, array($member->name));
}
}
/**
* Implements hook_zmt_group_member_delete().
*/
function zmt_ui_zmt_group_member_delete($member) {
module_load_include('inc', 'zmt_core', 'includes/zmt.zimbra');
$api = zimbra_admin_api_by_server($member->server_id);
$dl = zimbra_get_dl($api, $member->group_name . '@' . $member->domain);
$members = zimbra_get_dl_members($api, $member->group_name . '@' . $member->domain);
if (in_array($member->name, $members)) {
zimbra_remove_dl_members($api, $dl->id, array($member->name));
}
}
/**
* Implements hook_form_FORM_ID_alter() for the domain delete form.
*/
function zmt_ui_form_zmt_domain_delete_form_alter(&$form, &$form_state) {
$form['#validate'][] = 'zmt_ui_domain_delete_form_validate';
}
/**
* Validate callback for zmt_domain_delete_form().
*/
function zmt_ui_domain_delete_form_validate($form, &$form_state) {
if (!form_set_error() && !zmt_is_ajax_request()) {
$domain = $form_state['domain'];
if ((int) $domain->delete_domain > 0 && !empty($domain->zimbra_id)) {
module_load_include('inc', 'zmt_core', 'includes/zmt.zimbra');
$api = zimbra_admin_api_by_server($domain->server_id);
$zimbra_domain = zimbra_get_domain($api, $domain->name);
if ($zimbra_domain) {
$total_account = (int) zimbra_count_account($api);
$total_alias = (int) zimbra_count_alias($api);
$total_dl = (int) zimbra_count_dl($api);
$total_mailboxes = $total_account + $total_alias + $total_dl;
if ($total_mailboxes > 0) {
form_set_error('actions', t('Cannot delete this domain, because it is not empty in zimbra server. Please contact your administrator.'));
}
}
}
}
}
/**
* Implements hook_form_FORM_ID_alter() for the account import form.
*/
function zmt_ui_form_zmt_account_import_form_alter(&$form, &$form_state) {
$form['#validate'][] = 'zmt_ui_zmt_account_import_form_validate';
}
/**
* Account import form validate callback.
*/
function zmt_ui_zmt_account_import_form_validate($form, &$form_state) {
$op = isset($form_state['values']['op']);
$synchronized_accounts = array();
if (!form_set_error() && ($op || !zmt_is_ajax_request())) {
$accounts = !empty($form_state['accounts']) ? $form_state['accounts'] : FALSE;
if (!empty($accounts) && is_array($accounts)) {
$progress = 0;
$total_account = count($accounts);
$message_name = 'zmt_importing_message_' . $form_state['time'];
$progress_name = 'zmt_importing_progress_' . $form_state['time'];
variable_set($message_name, t('Importing accounts to zimbra server...'));
variable_set($progress_name, $progress);
module_load_include('inc', 'zmt_core', 'includes/zmt.zimbra');
$domain_id = (int) $form_state['values']['domain_id'];
$cos_id = (int) $form_state['values']['cos_id'];
$domain = zmt_domain_load($domain_id);
$cos = zmt_cos_load($cos_id);
$domain_name = !empty($domain->name) ? $domain->name : '';
$server_id = !empty($domain->server_id) ? (int) $domain->server_id : 0;
$cos_zimbra_id = !empty($cos->zimbra_id) ? $cos->zimbra_id : '';
$api = zimbra_admin_api_by_server($server_id);
foreach ($accounts as $account) {
$synchronized = FALSE;
$attrs = array(
'displayName' => $account->full_name,
'sn' => $account->full_name,
'zimbraAccountStatus' => $account->status,
'description' => $account->description,
'title' => $account->title,
'telephoneNumber' => $account->telephone,
'company' => $account->company,
'mobile' => $account->mobile,
);
if (!empty($cos_zimbra_id)) {
$attrs['zimbraCOSId'] = $cos_zimbra_id;
}
$mail_address = $account->name . '@' . $domain_name;
$zimbra_account = zimbra_get_account($api, $mail_address);
if (!$zimbra_account) {
$zimbra_account = zimbra_create_account($api, $mail_address, $account->password, $attrs);
if ($zimbra_account instanceof Exception) {
drupal_set_message(t('Account @name is invalid. @message', array(
'@name' => $account->name,
'@message' => $zimbra_account->getMessage(),
)), 'error');
}
else {
$synchronized = TRUE;
}
}
else {
zimbra_modify_account($api, $zimbra_account->id, $attrs);
$result = zimbra_check_password_strength($api, $zimbra_account->id, $account->password);
if ($result instanceof Exception) {
drupal_set_message(t('Account @name is invalid. @message', array(
'@name' => $account->name,
'@message' => $result->getMessage(),
)), 'error');
}
else {
zimbra_set_password($api, $zimbra_account->id, $account->password);
$synchronized = TRUE;
}
}
if ($synchronized) {
$account->zimbra_id = isset($zimbra_account->id) ? $zimbra_account->id : '';
$synchronized_accounts[] = $account;
}
$progress++;
variable_set($progress_name, floor($progress * 100 / $total_account));
}
}
}
$form_state['accounts'] = $synchronized_accounts;
sleep(1);
}
/**
* Implements hook_form_FORM_ID_alter() for the group edit form.
*/
function zmt_ui_form_zmt_group_edit_form_alter(&$form, &$form_state) {
$form['#validate'][] = 'zmt_ui_group_edit_form_validate';
}
/**
* Group edit form validate callback.
*/
function zmt_ui_group_edit_form_validate($form, &$form_state) {
if (!form_set_error() && !zmt_is_ajax_request()) {
module_load_include('inc', 'zmt_core', 'includes/zmt.zimbra');
$group = $form_state['#group'];
$values = &$form_state['values'];
$domain = zmt_domain_load((int) $values['domain_id']);
$domain_name = !empty($domain->name) ? $domain->name : '';
$server_id = !empty($domain->server_id) ? (int) $domain->server_id : 0;
$api = zimbra_admin_api_by_server($server_id);
$mail_address = $values['group_name'] . '@' . $domain_name;
if (empty($group->zimbra_id)) {
$dl = zimbra_get_dl($api, $mail_address);
if (!$dl) {
$dl = zimbra_create_dl($api, $mail_address, array(
'displayName' => $values['full_name'],
'description' => $values['full_name'],
));
}
if ($dl instanceof Exception) {
form_set_error('group_name', t($dl->getMessage()));
}
else {
$group->zimbra_id = isset($dl->id) ? $dl->id : '';
}
}
else {
$result = zimbra_modify_dl($api, $group->zimbra_id, array(
'displayName' => $values['full_name'],
'description' => $values['full_name'],
));
if ($result instanceof Exception) {
form_set_error('group_name', t($result->getMessage()));
}
}
if (!form_set_error()) {
$dl = zimbra_get_dl($api, $mail_address, TRUE);
if ($dl) {
$dl_members = zimbra_get_dl_members($api, $mail_address);
$value_members = is_array($values['members']) ? array_values($values['members']) : array();
$members = array();
foreach ($value_members as $member) {
$member = drupal_json_decode($member);
if (!empty($member['name'])) {
$members[] = $member['name'];
}
}
if (!empty($dl_members)) {
$remove_members = array();
foreach ($dl_members as $dl_member) {
if (!in_array($dl_member, $members)) {
$remove_members[] = $dl_member;
}
}
if (!empty($remove_members)) {
zimbra_remove_dl_members($api, $dl->id, $remove_members);
}
$add_members = array();
foreach ($members as $member) {
if (!in_array($member, $dl_members)) {
$add_members[] = $member;
}
}
if (!empty($add_members)) {
zimbra_add_dl_members($api, $dl->id, $add_members);
}
}
else {
if (!empty($members)) {
zimbra_add_dl_members($api, $dl->id, $members);
}
}
}
}
}
}
/**
* Implements hook_form_FORM_ID_alter() for the group delete form.
*/
function zmt_ui_form_zmt_group_delete_form_alter(&$form, &$form_state) {
$form['#validate'][] = 'zmt_ui_group_delete_form_validate';
}
/**
* Group delete form validate callback.
*/
function zmt_ui_group_delete_form_validate($form, &$form_state) {
if (!form_set_error() && !zmt_is_ajax_request()) {
module_load_include('inc', 'zmt_core', 'includes/zmt.zimbra');
$group = $form_state['#group'];
$members = zmt_group_member_load_multiple_by_group(array($group->id));
if (!empty($members)) {
$member_names = array();
foreach ($members as $member) {
$member_names[] = $member->name;
}
$server_id = !empty($group->server_id) ? (int) $group->server_id : 0;
$domain_name = !empty($group->domain) ? $group->domain : '';
if (empty($server_id) && empty($domain_name)) {
$domain = zmt_domain_load($group->domain_id);
$server_id = !empty($domain->server_id) ? (int) $domain->server_id : 0;
$domain_name = !empty($domain->name) ? $domain->name : '';
}
$api = zimbra_admin_api_by_server($server_id);
$dl = zimbra_get_dl($api, $group->name . '@' . $domain_name);
if ($dl) {
zimbra_remove_dl_members($api, $dl->id, $member_names);
}
}
}
}
/**
* Implements hook_form_FORM_ID_alter() for the account edit form.
*/
function zmt_ui_form_zmt_account_edit_form_alter(&$form, &$form_state) {
$form['#validate'][] = 'zmt_ui_account_edit_form_validate';
}
/**
* Account edit form validate callback.
*/
function zmt_ui_account_edit_form_validate($form, &$form_state) {
if (!form_set_error() && !zmt_is_ajax_request()) {
module_load_include('inc', 'zmt_core', 'includes/zmt.zimbra');
$values = &$form_state['values'];
$account = $form_state['account'];
$domain = zmt_domain_load((int) $values['domain_id']);
$cos = zmt_cos_load((int) $values['cos_id']);
$domain_name = !empty($domain->name) ? $domain->name : '';
$server_id = !empty($domain->server_id) ? (int) $domain->server_id : 0;
$cos_zimbra_id = !empty($cos->zimbra_id) ? $cos->zimbra_id : '';
$api = zimbra_admin_api_by_server($server_id);
$attrs = array(
'displayName' => check_plain($values['full_name']),
'sn' => check_plain($values['full_name']),
'zimbraAccountStatus' => check_plain($values['status']),
'description' => check_plain($values['description']),
'title' => check_plain($values['title']),
'telephoneNumber' => check_plain($values['telephone']),
'company' => check_plain($values['company']),
'mobile' => check_plain($values['mobile']),
'zimbraMailForwardingAddress' => array(),
);
if (!empty($form_state['hidden_forwarding'])) {
$attrs['zimbraMailForwardingAddress'] = $form_state['hidden_forwarding'];
}
if (!empty($cos_zimbra_id)) {
$attrs['zimbraCOSId'] = $cos_zimbra_id;
}
$name = filter_var($values['name'], FILTER_SANITIZE_EMAIL);
$mail_address = $name . '@' . $domain_name;
$password = check_plain($values['password']);
$zimbra_account = zimbra_get_account($api, $mail_address);
if (empty($account->zimbra_id)) {
if (!$zimbra_account) {
$zimbra_account = zimbra_create_account($api, $mail_address, $password, $attrs);
if ($zimbra_account instanceof Exception) {
$message = $zimbra_account->getMessage();
if (strpos($message, 'invalid password') !== FALSE) {
form_set_error('password', t($message));
}
else {
form_set_error('name', t($message));
}
}
}
else {
if (!empty($password)) {
$result = zimbra_check_password_strength($api, $zimbra_account->id, $password);
if ($result instanceof Exception) {
form_set_error('password', t($result->getMessage()));
}
else {
zimbra_set_password($api, $account->zimbra_id, $password);
}
}
if (!form_set_error()) {
$result = zimbra_modify_account($api, $zimbra_account->id, $attrs);
if ($result instanceof Exception) {
form_set_error('name', t($result->getMessage()));
}
}
}
$account->zimbra_id = isset($zimbra_account->id) ? $zimbra_account->id : '';
}
else {
$result = zimbra_modify_account($api, $account->zimbra_id, $attrs);
if ($result instanceof Exception) {
form_set_error('name', t($result->getMessage()));
}
else {
$account_id = !empty($account->id) ? $account->id : 0;
$old_account = zmt_account_load($account_id);
if ($old_account) {
$old_address = $old_account->name . '@' . $domain_name;
if ($old_address !== $mail_address) {
$result = zimbra_rename_account($api, $account->zimbra_id, $mail_address);
if ($result instanceof Exception) {
form_set_error('name', t($result->getMessage()));
}
}
}
}
if (!form_set_error() && !empty($password)) {
$result = zimbra_check_password_strength($api, $account->zimbra_id, $password);
if ($result instanceof Exception) {
form_set_error('password', t($result->getMessage()));
}
else {
zimbra_set_password($api, $account->zimbra_id, $password);
}
}
}
}
}
/**
* Implements hook_form_FORM_ID_alter() for the server edit form.
*/
function zmt_ui_form_zmt_server_edit_form_alter(&$form, &$form_state) {
$form['name']['#weight'] = -1;
$form['test_connection'] = array(
'#type' => 'submit',
'#value' => t('Test connection'),
'#weight' => 0,
'#ajax' => array(
'callback' => 'ajax_zmt_server_test_connection',
),
);
$form['#validate'][] = 'zmt_ui_test_server_connection_validate';
}
/**
* Zimbra server ajax test connection callback.
*/
function ajax_zmt_server_test_connection($form, $form_state) {
module_load_include('inc', 'zmt_core', 'includes/zmt.zimbra');
$values = $form_state['values'];
$server = $form_state['server'];
$location = $values['service_location'];
$user = $values['admin_user'];
$password = $values['admin_password'];
if (empty($password)) {
$password = zmt_password_decrypt($server->admin_password);
}
$auth_token = zimbra_test_connection($location, $user, $password);
if ($auth_token) {
if (!empty($server->id)) {
$server->auth_token = $auth_token;
zmt_server_save($server);
}
$message = t('Connection to zimbra server success!');
} else {
$message = t('Connection to zimbra server failed!');
}
$commands = array();
$commands[] = ajax_command_alert($message);
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Form validation handler for zmt_server_edit_form().
*
* @see zmt_server_edit_form()
*/
function zmt_ui_test_server_connection_validate($form, &$form_state) {
if (!zmt_is_ajax_request()) {
module_load_include('inc', 'zmt_core', 'includes/zmt.zimbra');
$values = $form_state['values'];
$server = $form_state['server'];
$location = $values['service_location'];
$user = $values['admin_user'];
$password = $values['admin_password'];
if (empty($password)) {
$password = zmt_password_decrypt($server->admin_password);
}
$auth_token = zimbra_test_connection($location, $user, $password);
if ($auth_token) {
if (!empty($server->id)) {
$server->auth_token = $auth_token;
zmt_server_save($server);
}
}
else {
form_set_error('', t('Connection to zimbra server failed!'));
}
}
}