Domain.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * Zm_Domain
  4. *
  5. * @author Yannick Lorenz <ylorenz@1g6.biz>
  6. * @author Fabrizio La Rosa <fabrizio.larosa@unime.it>
  7. * @version 2.2
  8. * @copyright Copyright (c) 2009, Yannick Lorenz
  9. * @copyright Copyright (c) 2012, Fabrizio La Rosa
  10. * @example ../test.php
  11. */
  12. /**
  13. * Zm_Domain class documentation
  14. */
  15. // utils.php contains a small collection of useful functions
  16. require_once ("utils.php");
  17. /**
  18. * Zm_Domain is a class which allows to manage Zimbra domains via SOAP
  19. *
  20. * You may create, modify, rename, delete and get the attributes of a Zimbra domain using this class
  21. *
  22. * For the usage examples of all class methods check the source code of test.php
  23. */
  24. class Zm_Domain
  25. {
  26. /**
  27. * $auth
  28. * @var Zm_Auth $auth soap authentication
  29. */
  30. private $auth;
  31. /**
  32. * Constructor
  33. * @param Zm_Auth $auth soap authentication
  34. */
  35. function __construct($auth)
  36. {
  37. $this->auth = $auth;
  38. }
  39. /**
  40. * getAllDomains
  41. * @return array informations
  42. */
  43. function getAllDomains()
  44. {
  45. $result = null;
  46. try
  47. {
  48. $result = $this->auth->execSoapCall(
  49. "GetAllDomainsRequest"
  50. );
  51. }
  52. catch (SoapFault $exception)
  53. {
  54. $result = $exception;
  55. }
  56. return $result;
  57. }
  58. /**
  59. * getDomainId
  60. * @param string $name a domain name
  61. * @return string a domain id
  62. */
  63. function getDomainId($name)
  64. {
  65. $result = null;
  66. $params = array(
  67. new SoapVar('<ns1:domain by="name">' . $name . '</ns1:domain>', XSD_ANYXML),
  68. );
  69. try
  70. {
  71. $result = $this->auth->execSoapCall(
  72. "GetDomainInfoRequest",
  73. $params
  74. );
  75. $result = $result['SOAP:ENVELOPE']['SOAP:BODY']['GETDOMAININFORESPONSE']['DOMAIN']['ID'];
  76. }
  77. catch (SoapFault $exception)
  78. {
  79. $result = $exception;
  80. }
  81. return $result;
  82. }
  83. /**
  84. * domainExists
  85. * @param string $idOrNameDomain domain id or domain name
  86. * @param string $type value of the domain (auto, name, id)
  87. * @return bool exists
  88. */
  89. function domainExists($idOrNameDomain, $type="auto")
  90. {
  91. if($type == "auto")
  92. $realType = getDomainType($idOrNameDomain);
  93. else
  94. $realType = $type;
  95. if($realType == "name")
  96. $domainId = $this->getDomainId($idOrNameDomain);
  97. else
  98. $domainId = $idOrNameDomain;
  99. $result = $this->getDomainId($domainId);
  100. return (!stristr($result, "dummy"));
  101. }
  102. /**
  103. * getDomainOptions
  104. * @param string $idOrNameDomain domain id or domain name
  105. * @param string $type value of the domain (auto, name, id)
  106. * @return array
  107. */
  108. function getDomainOptions($idOrNameDomain, $type="auto")
  109. {
  110. if($type == "auto")
  111. $realType = getDomainType($idOrNameDomain);
  112. else
  113. $realType = $type;
  114. $result = null;
  115. $params = array(
  116. new SoapVar('<ns1:domain by="' . $realType . '">' . $idOrNameDomain . '</ns1:domain>', XSD_ANYXML),
  117. );
  118. try
  119. {
  120. $result = $this->auth->execSoapCall(
  121. "GetDomainRequest",
  122. $params
  123. );
  124. $attrs = array();
  125. foreach ($result['SOAP:ENVELOPE']['SOAP:BODY']['GETDOMAINRESPONSE']['DOMAIN']['A'] as $a) {
  126. $attrs[$a['N']] = $a['DATA'];
  127. }
  128. $result = $attrs;
  129. }
  130. catch (SoapFault $exception)
  131. {
  132. $result = $exception;
  133. }
  134. return $result;
  135. }
  136. /**
  137. * createDomain
  138. * @param string $name a domain name
  139. * @param array $attrs an optional array containing the domain attributes to be set
  140. * @return array an array with the new domain's info
  141. */
  142. function createDomain($name, $attrs = array())
  143. {
  144. $result = null;
  145. $params = array(
  146. new SoapParam($name, "name"),
  147. );
  148. foreach ($attrs as $key=>$value)
  149. $params[] = new SoapVar('<ns1:a n="' . $key . '">' . $value . '</ns1:a>', XSD_ANYXML);
  150. try
  151. {
  152. $result = $this->auth->execSoapCall(
  153. "CreateDomainRequest",
  154. $params
  155. );
  156. $result = $result['SOAP:ENVELOPE']['SOAP:BODY']['CREATEDOMAINRESPONSE']['DOMAIN'];
  157. usleep(250000); // introduce a small delay, otherwise some troubles may arise if we modify the new domain right after its creation
  158. }
  159. catch (SoapFault $exception)
  160. {
  161. $result = $exception;
  162. }
  163. return $result;
  164. }
  165. /**
  166. * modifyDomain
  167. * @param string $idOrNameDomain domain id or domain name
  168. * @param array $attrs an array containing the domain attributes to be set
  169. * @param string $type value of the domain (auto, name, id)
  170. * @return array
  171. */
  172. function modifyDomain($idOrNameDomain, $attrs = array(), $type="auto")
  173. {
  174. if($type == "auto")
  175. $realType = getDomainType($idOrNameDomain);
  176. else
  177. $realType = $type;
  178. if($realType == "name")
  179. $domainId = $this->getDomainId($idOrNameDomain);
  180. else
  181. $domainId = $idOrNameDomain;
  182. $result = null;
  183. $params = array(
  184. new SoapParam($domainId, "id"),
  185. );
  186. foreach ($attrs as $key=>$value)
  187. $params[] = new SoapVar('<ns1:a n="' . $key . '">' . $value . '</ns1:a>', XSD_ANYXML);
  188. try
  189. {
  190. $result = $this->auth->execSoapCall(
  191. "ModifyDomainRequest",
  192. $params
  193. );
  194. }
  195. catch (SoapFault $exception)
  196. {
  197. $result = $exception;
  198. }
  199. return $result;
  200. }
  201. /**
  202. * deleteDomain
  203. * @param string $idOrNameDomain domain id or domain name
  204. * @param string $type value of the domain (auto, name, id)
  205. * @return array informations
  206. */
  207. function deleteDomain($idOrNameDomain, $type="auto")
  208. {
  209. if($type == "auto")
  210. $realType = getDomainType($idOrNameDomain);
  211. else
  212. $realType = $type;
  213. if($realType == "name")
  214. $domainId = $this->getDomainId($idOrNameDomain);
  215. else
  216. $domainId = $idOrNameDomain;
  217. $result = null;
  218. $params = array(
  219. new SoapParam($domainId, "id"),
  220. );
  221. try
  222. {
  223. $result = $this->auth->execSoapCall(
  224. "DeleteDomainRequest",
  225. $params
  226. );
  227. }
  228. catch (SoapFault $exception)
  229. {
  230. $result = $exception;
  231. }
  232. return $result;
  233. }
  234. }
  235. ?>