test.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. <?php
  2. /**
  3. * test.php
  4. *
  5. * In this file there are all the usage examples useful to learn and test all the
  6. * class methods for Zm_Account, Zm_Server, Zm_Domain and Zm_Auth
  7. *
  8. * @author Yannick Lorenz <ylorenz@1g6.biz>
  9. * @author Fabrizio La Rosa <fabrizio.larosa@unime.it>
  10. * @version 2.1
  11. * @copyright Copyright (c) 2009, Yannick Lorenz
  12. * @copyright Copyright (c) 2012, Fabrizio La Rosa
  13. * @name test.php
  14. * @filesource
  15. */
  16. /**
  17. * test.php examples
  18. */
  19. /////////////
  20. // Require //
  21. /////////////
  22. require_once("config.php");
  23. require_once("Zm/Auth.php");
  24. require_once("Zm/Account.php");
  25. require_once("Zm/Domain.php");
  26. require_once("Zm/Server.php");
  27. //////////
  28. // Args //
  29. //////////
  30. if(PHP_SAPI != "cli")
  31. $args = $_GET;
  32. else
  33. $args = parse_args($argv);
  34. if(isset($args["action"]))
  35. {
  36. $action = $args["action"];
  37. }
  38. else
  39. {
  40. echo "No action, exiting\n";
  41. exit (-1);
  42. }
  43. if(isset($args["str"]))
  44. {
  45. $account_name = $args["str"]. "@" . $domain;
  46. $domain_name = "domainsoap" . $args["str"] . ".com";
  47. $server_name = "serversoap." . $domain_name;
  48. }
  49. else
  50. {
  51. $rand = rand(111111, 999999999);
  52. $account_name = "acct_" . $rand . "@" . $domain;
  53. $domain_name = "dom" . $rand . ".com";
  54. $server_name = "srv" . $rand . "." . $domain_name;
  55. }
  56. if(isset($args["onam"]))
  57. $nam_opt = $args["onam"];
  58. if(isset($args["oval"]))
  59. $val_opt = $args["oval"];
  60. ///////////
  61. // Login //
  62. ///////////
  63. $auth = new Zm_Auth($zimbraserver, $zimbraadminemail, $zimbraadminpassword);
  64. $l = $auth->login();
  65. if(is_a($l, "Exception")) {
  66. echo "Error : cannot login to $zimbraserver :-(\n";
  67. print_exception($l);
  68. exit();
  69. }
  70. /////////////
  71. // Account //
  72. /////////////
  73. $accountManager = new Zm_Account($auth);
  74. // Get All Accounts
  75. if($action == "gaa")
  76. {
  77. // warning: this may take a long time to complete and fail
  78. // if you have a lot of accounts on your server
  79. $r = $accountManager->getAllAccounts($domain);
  80. if(is_a($r, "Exception")) {
  81. echo "Error : cannot fetch the list of accounts for domain $domain :-(\n";
  82. print_exception($r);
  83. } else {
  84. //print_var($r, "Get All Accounts");
  85. echo "OK : got a list of ".count($r)." accounts for domain $domain :-)\n";
  86. }
  87. }
  88. // Fetch Accounts via LDAP
  89. if($action == "gafl")
  90. {
  91. // you may fetch any attribute, just specify them in this array
  92. $accountAttrs = array(
  93. 'zimbraId',
  94. 'zimbraMailDeliveryAddress',
  95. 'zimbraAccountStatus',
  96. 'sn',
  97. 'givenName',
  98. 'zimbraHideInGal',
  99. 'mail',
  100. 'zimbraCalResType',
  101. 'zimbraMailQuota',
  102. 'zimbraCreateTimestamp',
  103. 'zimbraIsAdminAccount',
  104. 'zimbraIsSystemResource',
  105. );
  106. // you may fetch one or more accounts using the appropriate LDAP filters
  107. // for more info check SearchDirectoryRequest in
  108. // https://people.mozilla.org/~justdave/zmsoapdocs/soap-admin.txt
  109. // try a search by e-mail or other attributes
  110. // using "wilcard expressions" like '*', 'za*@my-domain' etc...
  111. $searchValue = $args["str"]. "*@" . $domain;
  112. $ldapFilter = sprintf("|(zimbraMailDeliveryAddress=%s)(zimbraMailAlias=%s)", $searchValue, $searchValue);
  113. $r = $accountManager->fetchAccounts($ldapFilter, $accountAttrs);
  114. if(is_a($r, "Exception")) {
  115. echo "Error : cannot fetch the list of accounts via ldap for domain $domain :-(\n";
  116. print_exception($r);
  117. } else {
  118. print_var($r, "Get All Accounts");
  119. echo "OK : got a list of ".count($r)." accounts via ldap for domain $domain :-)\n";
  120. }
  121. }
  122. // Create Account
  123. if($action == "ca")
  124. {
  125. $attrs = array("sn"=>"John", "gn"=>"Doe", "l"=>"Metropolis");
  126. $r = $accountManager->createAccount($account_name, "aPassword42", $attrs);
  127. if(is_a($r, "Exception")) {
  128. echo "Error : account $account_name not created :-(\n";
  129. print_exception($r);
  130. } else {
  131. print_var($r, "Create Account : id");
  132. echo "OK : account $account_name created :-)\n";
  133. }
  134. }
  135. // Account Exists
  136. if($action == "gax")
  137. {
  138. print_var($account_name, "Check Account Existence");
  139. $r = $accountManager->accountExists($account_name);
  140. if(!$r) {
  141. echo "NO: account $account_name doesn't exist :-(\n";
  142. exit();
  143. } else {
  144. echo "YES : account $account_name exists :-)\n";
  145. }
  146. }
  147. // Get Account Informations
  148. if($action == "gai")
  149. {
  150. $r = $accountManager->getAccountInfo($account_name);
  151. if(is_a($r, "Exception")) {
  152. echo "Error : cannot fetch the infos for account $account_name :-(\n";
  153. print_exception($r);
  154. } else {
  155. print_var($r, "Get Account Informations");
  156. echo "OK : got the infos for account $account_name :-)\n";
  157. }
  158. }
  159. // Get Account Options
  160. if($action == "gaao")
  161. {
  162. $r = $accountManager->getAccountOptions($account_name);
  163. if(is_a($r, "Exception")) {
  164. echo "Error : cannot fetch the options for account $account_name :-(\n";
  165. print_exception($r);
  166. } else {
  167. print_var($r, "Get Account Options");
  168. echo "OK : got the options for account $account_name :-)\n";
  169. }
  170. }
  171. // Get Account Option
  172. if($action == "gao")
  173. {
  174. if (!$nam_opt)
  175. $nam_opt = "zimbraMailHost";
  176. $r = $accountManager->getAccountOption($account_name, $nam_opt);
  177. if(is_a($r, "Exception")) {
  178. echo "Error : cannot fetch the option for account $account_name :-(\n";
  179. print_exception($r);
  180. } else {
  181. print_var($r, "Get Account Option");
  182. echo "OK : got the option for account $account_name :-)\n";
  183. }
  184. }
  185. // Set Account password
  186. if($action == "sap")
  187. {
  188. $r = $accountManager->setAccountPassword($account_name, "anewPassword42");
  189. if(is_a($r, "Exception")) {
  190. echo "Error : cannot change password for account $account_name :-(\n";
  191. print_exception($r);
  192. } else {
  193. print_var($r, "Set Account Password");
  194. echo "OK : password changed for account $account_name :-)\n";
  195. }
  196. }
  197. // Modify Account
  198. if($action == "ma")
  199. {
  200. if (!$nam_opt)
  201. $nam_opt = "streetAddress";
  202. if (!$val_opt)
  203. $val_opt = "234, 5th Avenue";
  204. $new_attrs = array($nam_opt=>$val_opt);
  205. print_var($new_attrs, "Modify Account");
  206. $r = $accountManager->modifyAccount($account_name, $new_attrs);
  207. if(is_a($r, "Exception")) {
  208. echo "Error : cannot modify account $account_name :-(\n";
  209. print_exception($r);
  210. } else {
  211. print_var($r, "Modify Account : Response");
  212. echo "OK : modify account $account_name :-)\n";
  213. }
  214. }
  215. // Rename Account
  216. if($action == "ra")
  217. {
  218. $new_account_name = "newname_".$account_name;
  219. $r = $accountManager->renameAccount($account_name, $new_account_name);
  220. if(is_a($r, "Exception")) {
  221. echo "Error : account $account_name not renamed to $new_account_name :-(\n";
  222. print_exception($r);
  223. } else {
  224. print_var($r, "Rename Account");
  225. echo "OK : account $account_name renamed to $new_account_name :-)\n";
  226. }
  227. }
  228. // Delete Account
  229. if($action == "da")
  230. {
  231. $r = $accountManager->deleteAccount($account_name);
  232. if(is_a($r, "Exception")) {
  233. echo "Error : account $account_name not deleted :-(\n";
  234. print_exception($r);
  235. } else {
  236. print_var($r, "Delete Account : Response");
  237. echo "OK : account $account_name deleted :-)\n";
  238. }
  239. }
  240. // Get Account Status
  241. if($action == "gat")
  242. {
  243. $r = $accountManager->getAccountStatus($account_name);
  244. if(is_a($r, "Exception")) {
  245. echo "Error : cannot fetch the status for account $account_name :-(\n";
  246. print_exception($r);
  247. } else {
  248. print_var($r, "Get Account Status");
  249. echo "OK : got the status for account $account_name :-)\n";
  250. }
  251. }
  252. // Set Account Status
  253. if($action == "sat")
  254. {
  255. if (!$val_opt)
  256. $val_opt = ($accountManager->getAccountStatus($account_name) == "active") ? "locked" : "active";
  257. print_var($val_opt, "Set Account Status");
  258. $r = $accountManager->setAccountStatus($account_name, $val_opt);
  259. if(is_a($r, "Exception")) {
  260. echo "Error : cannot modify status for account $account_name :-(\n";
  261. print_exception($r);
  262. } else {
  263. print_var($r, "Set Account Status : Response");
  264. echo "OK : modified status for account $account_name :-)\n";
  265. }
  266. }
  267. // Get Account COS name
  268. if($action == "gacn")
  269. {
  270. $r = $accountManager->getAccountCos($account_name, "NAME");
  271. if(is_a($r, "Exception")) {
  272. echo "Error : cannot fetch the COS name for account $account_name :-(\n";
  273. print_exception($r);
  274. } else {
  275. print_var($r, "Get Account COS Name");
  276. echo "OK : got the COS name for account $account_name :-)\n";
  277. }
  278. }
  279. // Get Account COS id
  280. if($action == "gaci")
  281. {
  282. $r = $accountManager->getAccountCos($account_name, "ID");
  283. if(is_a($r, "Exception")) {
  284. echo "Error : cannot fetch the COS id for account $account_name :-(\n";
  285. print_exception($r);
  286. } else {
  287. print_var($r, "Get Account COS Id");
  288. echo "OK : got the COS id for account $account_name :-)\n";
  289. }
  290. }
  291. // Set Account COS
  292. if($action == "sac")
  293. {
  294. if (!$val_opt)
  295. $val_opt = "testcos";
  296. print_var($val_opt, "Set Account COS");
  297. $r = $accountManager->setAccountCOS($account_name, $val_opt);
  298. if(is_a($r, "Exception")) {
  299. echo "Error : cannot modify COS for account $account_name :-(\n";
  300. print_exception($r);
  301. } else {
  302. print_var($r, "Set Account COS : Response");
  303. echo "OK : modified COS for account $account_name :-)\n";
  304. }
  305. }
  306. // Add account alias
  307. if($action == "aaa")
  308. {
  309. $alias = "alias_" . $account_name;
  310. $r = $accountManager->addAccountAlias($account_name, $alias);
  311. if(is_a($r, "Exception")) {
  312. echo "Error : cannot add alias for account $account_name :-(\n";
  313. print_exception($r);
  314. } else {
  315. print_var($r, "Add account alias");
  316. echo "OK : added alias to account $account_name :-)\n";
  317. }
  318. }
  319. // Get account aliases
  320. if($action == "gal")
  321. {
  322. $r = $accountManager->getAccountAliases($account_name);
  323. if(is_a($r, "Exception")) {
  324. echo "Error : cannot fetch the alias for account $account_name :-(\n";
  325. print_exception($r);
  326. } else {
  327. print_var($r, "Get Account Aliases");
  328. echo "OK : got the alias for account $account_name :-)\n";
  329. }
  330. }
  331. // Remove account alias
  332. if($action == "raa")
  333. {
  334. $alias = "alias_" . $account_name;
  335. $r = $accountManager->removeAccountAlias($account_name, $alias);
  336. if(is_a($r, "Exception")) {
  337. echo "Error : cannot remove alias for account $account_name :-(\n";
  338. print_exception($r);
  339. } else {
  340. print_var($r, "Remove account alias");
  341. echo "OK : removed alias to account $account_name :-)\n";
  342. }
  343. }
  344. ////////////
  345. // Domain //
  346. ////////////
  347. $domainManager = new Zm_Domain($auth);
  348. // Get All Domains
  349. if($action == "gad")
  350. {
  351. $r = $domainManager->getAllDomains();
  352. if(is_a($r, "Exception")) {
  353. echo "Error : cannot fetch the list of domains :-(\n";
  354. print_exception($r);
  355. } else {
  356. print_var($r, "Get All Domains");
  357. echo "OK : got the the list of domains :-)\n";
  358. }
  359. }
  360. // Create Domain
  361. if($action == "cd")
  362. {
  363. $r = $domainManager->createDomain($domain_name);
  364. if(is_a($r, "Exception")) {
  365. echo "Error : creating domain $domain_name :-(\n";
  366. print_exception($r);
  367. } else {
  368. print_var($r, "Create Domain : id");
  369. echo "OK : domain $domain_name created :-)\n";
  370. }
  371. }
  372. // Domain Exists
  373. if($action == "gdx")
  374. {
  375. print_var($domain_name, "Check Domain Existence");
  376. $r = $domainManager->domainExists($domain_name);
  377. if(!$r) {
  378. echo "NO: domain $domain_name doesn't exist :-(\n";
  379. exit();
  380. } else {
  381. echo "YES : domain $domain_name exists :-)\n";
  382. }
  383. }
  384. // Get Domain Options
  385. if($action == "gdao")
  386. {
  387. $r = $domainManager->getDomainOptions($domain_name);
  388. if(is_a($r, "Exception")) {
  389. echo "Error : cannot fetch the options for domain $domain_name :-(\n";
  390. print_exception($r);
  391. } else {
  392. print_var($r, "Get Domain Options");
  393. echo "OK : got the options for domain $domain_name :-)\n";
  394. }
  395. }
  396. // Modify Domain
  397. if($action == "md")
  398. {
  399. if (!$nam_opt)
  400. $nam_opt = "zimbraGalLdapPageSize";
  401. if (!$val_opt)
  402. $val_opt = 222;
  403. $new_attrs = array($nam_opt=>$val_opt);
  404. print_var($new_attrs, "Modify Domain : setting");
  405. $r = $domainManager->modifyDomain($domain_name, $new_attrs);
  406. if(is_a($r, "Exception")) {
  407. echo "Error : modifying domain $domain_name :-(\n";
  408. print_exception($r);
  409. } else {
  410. print_var($r, "Modify Domain : Response");
  411. echo "OK : domain $domain_name modified :-)\n";
  412. }
  413. }
  414. // Delete Domain
  415. if($action == "dd")
  416. {
  417. $r = $domainManager->deleteDomain($domain_name);
  418. if(is_a($r, "Exception")) {
  419. echo "Error : deleting domain $domain_name :-(\n";
  420. print_exception($r);
  421. } else {
  422. print_var($r, "Delete Domain : Response");
  423. echo "OK : domain $domain_name deleted :-)\n";
  424. }
  425. }
  426. ////////////
  427. // Server //
  428. ////////////
  429. $serverManager = new Zm_Server($auth);
  430. // Get All Servers
  431. if($action == "gas")
  432. {
  433. $r = $serverManager->getAllServers();
  434. if(is_a($r, "Exception")) {
  435. echo "Error : cannot fetch the list of servers for domain $domain_name :-(\n";
  436. print_exception($r);
  437. } else {
  438. print_var($r, "Get All Servers");
  439. echo "OK : got the list of servers for domain $domain_name :-)\n";
  440. }
  441. }
  442. // Create Server
  443. if($action == "cs")
  444. {
  445. $r = $serverManager->createServer($server_name);
  446. if(is_a($r, "Exception")) {
  447. echo "Error : creating server $server_name :-(\n";
  448. print_exception($r);
  449. } else {
  450. print_var($r, "Create Server : id");
  451. echo "OK : server $server_name created :-)\n";
  452. }
  453. }
  454. // Server Exists
  455. if($action == "gsx")
  456. {
  457. print_var($server_name, "Check Server Existence");
  458. $r = $serverManager->serverExists($server_name);
  459. if(!$r) {
  460. echo "NO: server $server_name doesn't exist :-(\n";
  461. exit();
  462. } else {
  463. echo "YES : server $server_name exists :-)\n";
  464. }
  465. }
  466. // Get Server Options
  467. if($action == "gsao")
  468. {
  469. $r = $serverManager->getServerOptions($server_name);
  470. if(is_a($r, "Exception")) {
  471. echo "Error : cannot fetch the options for server $server_name :-(\n";
  472. print_exception($r);
  473. } else {
  474. print_var($r, "Get Server Options");
  475. echo "OK : got the options for server $server_name :-)\n";
  476. }
  477. }
  478. // Modify Server
  479. if($action == "ms")
  480. {
  481. if (!$nam_opt)
  482. $nam_opt = "zimbraHttpSSLNumThreads";
  483. if (!$val_opt)
  484. $val_opt = 333;
  485. $new_attrs = array($nam_opt=>$val_opt);
  486. print_var($new_attrs, "Modify Server : setting");
  487. $r = $serverManager->modifyServer($server_name, $new_attrs);
  488. if(is_a($r, "Exception")) {
  489. echo "Error : modifying server $server_name :-(\n";
  490. print_exception($r);
  491. } else {
  492. print_var($r, "Modify Server : Response");
  493. echo "OK : server $server_name modified :-)\n";
  494. }
  495. }
  496. // Delete Server
  497. if($action == "ds")
  498. {
  499. $r = $serverManager->deleteServer($server_name);
  500. if(is_a($r, "Exception")) {
  501. echo "Error : deleting server $server_name :-(\n";
  502. print_exception($r);
  503. } else {
  504. print_var($r, "Delete Server : Response");
  505. echo "OK : server $server_name deleted :-)\n";
  506. }
  507. }
  508. if(!$r) echo "Invalid action!\n";
  509. ?>