testuser.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * testuser.php
  4. *
  5. * In this file there are all the usage examples useful to learn and test all the
  6. * class methods for Zm_User
  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. * testuser.php examples
  18. */
  19. /////////////
  20. // Require //
  21. /////////////
  22. require_once("config.php");
  23. require_once("Zm/Auth.php");
  24. require_once("Zm/User.php");
  25. //////////
  26. // Args //
  27. //////////
  28. if(PHP_SAPI != "cli")
  29. $args = $_GET;
  30. else
  31. $args = parse_args($argv);
  32. if(isset($args["action"]))
  33. {
  34. $action = $args["action"];
  35. }
  36. else
  37. {
  38. echo "No action, exiting\n";
  39. exit (-1);
  40. }
  41. if(isset($args["str"]))
  42. {
  43. $user_name = $args["str"]. "@" . $domain;
  44. }
  45. if(isset($args["onam"]))
  46. $nam_opt = $args["onam"];
  47. if(isset($args["oval"]))
  48. $val_opt = $args["oval"];
  49. ///////////
  50. // Login //
  51. ///////////
  52. $userpassword = "Password42";
  53. $auth = new Zm_Auth($zimbraserver, $user_name, $userpassword, "user");
  54. $l = $auth->login();
  55. if(is_a($l, "Exception")) {
  56. echo "Error : cannot login to $zimbraserver :-(\n";
  57. print_exception($l);
  58. exit();
  59. }
  60. //////////
  61. // User //
  62. //////////
  63. $userManager = new Zm_User($auth);
  64. // User Exists
  65. if($action == "gux")
  66. {
  67. print_var($user_name, "Check User Existence");
  68. $r = $userManager->userExists($user_name);
  69. if(!$r) {
  70. echo "NO: user $user_name doesn't exist :-(\n";
  71. exit();
  72. } else {
  73. echo "YES : user $user_name exists :-)\n";
  74. }
  75. }
  76. // Get User Informations
  77. if($action == "gui")
  78. {
  79. $r = $userManager->getUserInfo($user_name);
  80. if(is_a($r, "Exception")) {
  81. echo "Error : cannot fetch the infos for user $user_name :-(\n";
  82. print_exception($r);
  83. } else {
  84. print_var($r, "Get User Informations");
  85. echo "OK : got the infos for user $user_name :-)\n";
  86. }
  87. }
  88. // Get User Prefs
  89. if($action == "gup")
  90. {
  91. $r = $userManager->getUserPrefs($user_name);
  92. if(is_a($r, "Exception")) {
  93. echo "Error : cannot fetch prefs for user $user_name :-(\n";
  94. print_exception($r);
  95. } else {
  96. print_var($r, "Get User Prefs");
  97. echo "OK : got prefs for user $user_name :-)\n";
  98. }
  99. }
  100. // Get User Attrs
  101. if($action == "gua")
  102. {
  103. if (!$nam_opt)
  104. $nam_opt = "zimbraMailHost";
  105. $r = $userManager->getUserAttrs($user_name);
  106. if(is_a($r, "Exception")) {
  107. echo "Error : cannot fetch attrs for user $user_name :-(\n";
  108. print_exception($r);
  109. } else {
  110. print_var($r, "Get User Attrs");
  111. echo "OK : got attrs for user $user_name :-)\n";
  112. }
  113. }
  114. // Change User password
  115. if($action == "cup")
  116. {
  117. $r = $userManager->changeUserPassword($user_name, $userpassword, "newPassword42");
  118. if(is_a($r, "Exception")) {
  119. echo "Error : cannot change password for user $user_name :-(\n";
  120. print_exception($r);
  121. } else {
  122. print_var($r, "Change User Password");
  123. echo "OK : password changed for user $user_name :-)\n";
  124. }
  125. }
  126. // Modify Prefs
  127. if($action == "mup")
  128. {
  129. if (!$nam_opt)
  130. $nam_opt = "zimbraPrefMailItemsPerPage";
  131. if (!$val_opt)
  132. $val_opt = "42";
  133. $new_prefs = array($nam_opt=>$val_opt);
  134. print_var($new_prefs, "Modify Prefs");
  135. $r = $userManager->modifyUserPrefs($user_name, $new_prefs);
  136. if(is_a($r, "Exception")) {
  137. echo "Error : cannot modify prefs for $user_name :-(\n";
  138. print_exception($r);
  139. } else {
  140. print_var($r, "Modify Prefs : Response");
  141. echo "OK : modify user prefs for $user_name :-)\n";
  142. }
  143. }
  144. if(!$r) echo "Invalid action!\n";
  145. ?>