-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.php
594 lines (509 loc) · 13.8 KB
/
test.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
<?php
/**
* test.php
*
* In this file there are all the usage examples useful to learn and test all the
* class methods for Zm_Account, Zm_Server, Zm_Domain and Zm_Auth
*
* @author Yannick Lorenz <[email protected]>
* @author Fabrizio La Rosa <[email protected]>
* @version 2.1
* @copyright Copyright (c) 2009, Yannick Lorenz
* @copyright Copyright (c) 2012, Fabrizio La Rosa
* @name test.php
* @filesource
*/
/**
* test.php examples
*/
/////////////
// Require //
/////////////
require_once("config.php");
require_once("Zm/Auth.php");
require_once("Zm/Account.php");
require_once("Zm/Domain.php");
require_once("Zm/Server.php");
//////////
// Args //
//////////
if(PHP_SAPI != "cli")
$args = $_GET;
else
$args = parse_args($argv);
if(isset($args["action"]))
{
$action = $args["action"];
}
else
{
echo "No action, exiting\n";
exit (-1);
}
if(isset($args["str"]))
{
$account_name = $args["str"]. "@" . $domain;
$domain_name = "domainsoap" . $args["str"] . ".com";
$server_name = "serversoap." . $domain_name;
}
else
{
$rand = rand(111111, 999999999);
$account_name = "acct_" . $rand . "@" . $domain;
$domain_name = "dom" . $rand . ".com";
$server_name = "srv" . $rand . "." . $domain_name;
}
if(isset($args["onam"]))
$nam_opt = $args["onam"];
if(isset($args["oval"]))
$val_opt = $args["oval"];
///////////
// Login //
///////////
$auth = new Zm_Auth($zimbraserver, $zimbraadminemail, $zimbraadminpassword);
$l = $auth->login();
if(is_a($l, "Exception")) {
echo "Error : cannot login to $zimbraserver :-(\n";
print_exception($l);
exit();
}
/////////////
// Account //
/////////////
$accountManager = new Zm_Account($auth);
// Get All Accounts
if($action == "gaa")
{
// warning: this may take a long time to complete and fail
// if you have a lot of accounts on your server
$r = $accountManager->getAllAccounts($domain);
if(is_a($r, "Exception")) {
echo "Error : cannot fetch the list of accounts for domain $domain :-(\n";
print_exception($r);
} else {
//print_var($r, "Get All Accounts");
echo "OK : got a list of ".count($r)." accounts for domain $domain :-)\n";
}
}
// Fetch Accounts via LDAP
if($action == "gafl")
{
// you may fetch any attribute, just specify them in this array
$accountAttrs = array(
'zimbraId',
'zimbraMailDeliveryAddress',
'zimbraAccountStatus',
'sn',
'givenName',
'zimbraHideInGal',
'mail',
'zimbraCalResType',
'zimbraMailQuota',
'zimbraCreateTimestamp',
'zimbraIsAdminAccount',
'zimbraIsSystemResource',
);
// you may fetch one or more accounts using the appropriate LDAP filters
// for more info check SearchDirectoryRequest in
// https://people.mozilla.org/~justdave/zmsoapdocs/soap-admin.txt
// try a search by e-mail or other attributes
// using "wilcard expressions" like '*', 'za*@my-domain' etc...
$searchValue = $args["str"]. "*@" . $domain;
$ldapFilter = sprintf("|(zimbraMailDeliveryAddress=%s)(zimbraMailAlias=%s)", $searchValue, $searchValue);
$r = $accountManager->fetchAccounts($ldapFilter, $accountAttrs);
if(is_a($r, "Exception")) {
echo "Error : cannot fetch the list of accounts via ldap for domain $domain :-(\n";
print_exception($r);
} else {
print_var($r, "Get All Accounts");
echo "OK : got a list of ".count($r)." accounts via ldap for domain $domain :-)\n";
}
}
// Create Account
if($action == "ca")
{
$attrs = array("sn"=>"John", "gn"=>"Doe", "l"=>"Metropolis");
$r = $accountManager->createAccount($account_name, "aPassword42", $attrs);
if(is_a($r, "Exception")) {
echo "Error : account $account_name not created :-(\n";
print_exception($r);
} else {
print_var($r, "Create Account : id");
echo "OK : account $account_name created :-)\n";
}
}
// Account Exists
if($action == "gax")
{
print_var($account_name, "Check Account Existence");
$r = $accountManager->accountExists($account_name);
if(!$r) {
echo "NO: account $account_name doesn't exist :-(\n";
exit();
} else {
echo "YES : account $account_name exists :-)\n";
}
}
// Get Account Informations
if($action == "gai")
{
$r = $accountManager->getAccountInfo($account_name);
if(is_a($r, "Exception")) {
echo "Error : cannot fetch the infos for account $account_name :-(\n";
print_exception($r);
} else {
print_var($r, "Get Account Informations");
echo "OK : got the infos for account $account_name :-)\n";
}
}
// Get Account Options
if($action == "gaao")
{
$r = $accountManager->getAccountOptions($account_name);
if(is_a($r, "Exception")) {
echo "Error : cannot fetch the options for account $account_name :-(\n";
print_exception($r);
} else {
print_var($r, "Get Account Options");
echo "OK : got the options for account $account_name :-)\n";
}
}
// Get Account Option
if($action == "gao")
{
if (!$nam_opt)
$nam_opt = "zimbraMailHost";
$r = $accountManager->getAccountOption($account_name, $nam_opt);
if(is_a($r, "Exception")) {
echo "Error : cannot fetch the option for account $account_name :-(\n";
print_exception($r);
} else {
print_var($r, "Get Account Option");
echo "OK : got the option for account $account_name :-)\n";
}
}
// Set Account password
if($action == "sap")
{
$r = $accountManager->setAccountPassword($account_name, "anewPassword42");
if(is_a($r, "Exception")) {
echo "Error : cannot change password for account $account_name :-(\n";
print_exception($r);
} else {
print_var($r, "Set Account Password");
echo "OK : password changed for account $account_name :-)\n";
}
}
// Modify Account
if($action == "ma")
{
if (!$nam_opt)
$nam_opt = "streetAddress";
if (!$val_opt)
$val_opt = "234, 5th Avenue";
$new_attrs = array($nam_opt=>$val_opt);
print_var($new_attrs, "Modify Account");
$r = $accountManager->modifyAccount($account_name, $new_attrs);
if(is_a($r, "Exception")) {
echo "Error : cannot modify account $account_name :-(\n";
print_exception($r);
} else {
print_var($r, "Modify Account : Response");
echo "OK : modify account $account_name :-)\n";
}
}
// Rename Account
if($action == "ra")
{
$new_account_name = "newname_".$account_name;
$r = $accountManager->renameAccount($account_name, $new_account_name);
if(is_a($r, "Exception")) {
echo "Error : account $account_name not renamed to $new_account_name :-(\n";
print_exception($r);
} else {
print_var($r, "Rename Account");
echo "OK : account $account_name renamed to $new_account_name :-)\n";
}
}
// Delete Account
if($action == "da")
{
$r = $accountManager->deleteAccount($account_name);
if(is_a($r, "Exception")) {
echo "Error : account $account_name not deleted :-(\n";
print_exception($r);
} else {
print_var($r, "Delete Account : Response");
echo "OK : account $account_name deleted :-)\n";
}
}
// Get Account Status
if($action == "gat")
{
$r = $accountManager->getAccountStatus($account_name);
if(is_a($r, "Exception")) {
echo "Error : cannot fetch the status for account $account_name :-(\n";
print_exception($r);
} else {
print_var($r, "Get Account Status");
echo "OK : got the status for account $account_name :-)\n";
}
}
// Set Account Status
if($action == "sat")
{
if (!$val_opt)
$val_opt = ($accountManager->getAccountStatus($account_name) == "active") ? "locked" : "active";
print_var($val_opt, "Set Account Status");
$r = $accountManager->setAccountStatus($account_name, $val_opt);
if(is_a($r, "Exception")) {
echo "Error : cannot modify status for account $account_name :-(\n";
print_exception($r);
} else {
print_var($r, "Set Account Status : Response");
echo "OK : modified status for account $account_name :-)\n";
}
}
// Get Account COS name
if($action == "gacn")
{
$r = $accountManager->getAccountCos($account_name, "NAME");
if(is_a($r, "Exception")) {
echo "Error : cannot fetch the COS name for account $account_name :-(\n";
print_exception($r);
} else {
print_var($r, "Get Account COS Name");
echo "OK : got the COS name for account $account_name :-)\n";
}
}
// Get Account COS id
if($action == "gaci")
{
$r = $accountManager->getAccountCos($account_name, "ID");
if(is_a($r, "Exception")) {
echo "Error : cannot fetch the COS id for account $account_name :-(\n";
print_exception($r);
} else {
print_var($r, "Get Account COS Id");
echo "OK : got the COS id for account $account_name :-)\n";
}
}
// Set Account COS
if($action == "sac")
{
if (!$val_opt)
$val_opt = "testcos";
print_var($val_opt, "Set Account COS");
$r = $accountManager->setAccountCOS($account_name, $val_opt);
if(is_a($r, "Exception")) {
echo "Error : cannot modify COS for account $account_name :-(\n";
print_exception($r);
} else {
print_var($r, "Set Account COS : Response");
echo "OK : modified COS for account $account_name :-)\n";
}
}
// Add account alias
if($action == "aaa")
{
$alias = "alias_" . $account_name;
$r = $accountManager->addAccountAlias($account_name, $alias);
if(is_a($r, "Exception")) {
echo "Error : cannot add alias for account $account_name :-(\n";
print_exception($r);
} else {
print_var($r, "Add account alias");
echo "OK : added alias to account $account_name :-)\n";
}
}
// Get account aliases
if($action == "gal")
{
$r = $accountManager->getAccountAliases($account_name);
if(is_a($r, "Exception")) {
echo "Error : cannot fetch the alias for account $account_name :-(\n";
print_exception($r);
} else {
print_var($r, "Get Account Aliases");
echo "OK : got the alias for account $account_name :-)\n";
}
}
// Remove account alias
if($action == "raa")
{
$alias = "alias_" . $account_name;
$r = $accountManager->removeAccountAlias($account_name, $alias);
if(is_a($r, "Exception")) {
echo "Error : cannot remove alias for account $account_name :-(\n";
print_exception($r);
} else {
print_var($r, "Remove account alias");
echo "OK : removed alias to account $account_name :-)\n";
}
}
////////////
// Domain //
////////////
$domainManager = new Zm_Domain($auth);
// Get All Domains
if($action == "gad")
{
$r = $domainManager->getAllDomains();
if(is_a($r, "Exception")) {
echo "Error : cannot fetch the list of domains :-(\n";
print_exception($r);
} else {
print_var($r, "Get All Domains");
echo "OK : got the the list of domains :-)\n";
}
}
// Create Domain
if($action == "cd")
{
$r = $domainManager->createDomain($domain_name);
if(is_a($r, "Exception")) {
echo "Error : creating domain $domain_name :-(\n";
print_exception($r);
} else {
print_var($r, "Create Domain : id");
echo "OK : domain $domain_name created :-)\n";
}
}
// Domain Exists
if($action == "gdx")
{
print_var($domain_name, "Check Domain Existence");
$r = $domainManager->domainExists($domain_name);
if(!$r) {
echo "NO: domain $domain_name doesn't exist :-(\n";
exit();
} else {
echo "YES : domain $domain_name exists :-)\n";
}
}
// Get Domain Options
if($action == "gdao")
{
$r = $domainManager->getDomainOptions($domain_name);
if(is_a($r, "Exception")) {
echo "Error : cannot fetch the options for domain $domain_name :-(\n";
print_exception($r);
} else {
print_var($r, "Get Domain Options");
echo "OK : got the options for domain $domain_name :-)\n";
}
}
// Modify Domain
if($action == "md")
{
if (!$nam_opt)
$nam_opt = "zimbraGalLdapPageSize";
if (!$val_opt)
$val_opt = 222;
$new_attrs = array($nam_opt=>$val_opt);
print_var($new_attrs, "Modify Domain : setting");
$r = $domainManager->modifyDomain($domain_name, $new_attrs);
if(is_a($r, "Exception")) {
echo "Error : modifying domain $domain_name :-(\n";
print_exception($r);
} else {
print_var($r, "Modify Domain : Response");
echo "OK : domain $domain_name modified :-)\n";
}
}
// Delete Domain
if($action == "dd")
{
$r = $domainManager->deleteDomain($domain_name);
if(is_a($r, "Exception")) {
echo "Error : deleting domain $domain_name :-(\n";
print_exception($r);
} else {
print_var($r, "Delete Domain : Response");
echo "OK : domain $domain_name deleted :-)\n";
}
}
////////////
// Server //
////////////
$serverManager = new Zm_Server($auth);
// Get All Servers
if($action == "gas")
{
$r = $serverManager->getAllServers();
if(is_a($r, "Exception")) {
echo "Error : cannot fetch the list of servers for domain $domain_name :-(\n";
print_exception($r);
} else {
print_var($r, "Get All Servers");
echo "OK : got the list of servers for domain $domain_name :-)\n";
}
}
// Create Server
if($action == "cs")
{
$r = $serverManager->createServer($server_name);
if(is_a($r, "Exception")) {
echo "Error : creating server $server_name :-(\n";
print_exception($r);
} else {
print_var($r, "Create Server : id");
echo "OK : server $server_name created :-)\n";
}
}
// Server Exists
if($action == "gsx")
{
print_var($server_name, "Check Server Existence");
$r = $serverManager->serverExists($server_name);
if(!$r) {
echo "NO: server $server_name doesn't exist :-(\n";
exit();
} else {
echo "YES : server $server_name exists :-)\n";
}
}
// Get Server Options
if($action == "gsao")
{
$r = $serverManager->getServerOptions($server_name);
if(is_a($r, "Exception")) {
echo "Error : cannot fetch the options for server $server_name :-(\n";
print_exception($r);
} else {
print_var($r, "Get Server Options");
echo "OK : got the options for server $server_name :-)\n";
}
}
// Modify Server
if($action == "ms")
{
if (!$nam_opt)
$nam_opt = "zimbraHttpSSLNumThreads";
if (!$val_opt)
$val_opt = 333;
$new_attrs = array($nam_opt=>$val_opt);
print_var($new_attrs, "Modify Server : setting");
$r = $serverManager->modifyServer($server_name, $new_attrs);
if(is_a($r, "Exception")) {
echo "Error : modifying server $server_name :-(\n";
print_exception($r);
} else {
print_var($r, "Modify Server : Response");
echo "OK : server $server_name modified :-)\n";
}
}
// Delete Server
if($action == "ds")
{
$r = $serverManager->deleteServer($server_name);
if(is_a($r, "Exception")) {
echo "Error : deleting server $server_name :-(\n";
print_exception($r);
} else {
print_var($r, "Delete Server : Response");
echo "OK : server $server_name deleted :-)\n";
}
}
if(!$r) echo "Invalid action!\n";
?>