Server.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. * Zm_Server
  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_Server class documentation
  14. */
  15. // utils.php contains a small collection of useful functions
  16. require_once ("utils.php");
  17. /**
  18. * Zm_Server is a class which allows to manage Kerio servers via SOAP
  19. *
  20. * You may create, modify, rename, delete and get the attributes of a Kerio server using this class
  21. *
  22. * For the usage examples of all class methods check the source code of test.php
  23. */
  24. class Zm_Server
  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. * getAllServers
  41. * @return array informations
  42. */
  43. function getAllServers()
  44. {
  45. $result = null;
  46. try
  47. {
  48. $result = $this->auth->execSoapCall(
  49. "GetAllServersRequest"
  50. );
  51. }
  52. catch (SoapFault $exception)
  53. {
  54. $result = $exception;
  55. }
  56. return $result;
  57. }
  58. /**
  59. * getServerId
  60. * @param string $name a server name
  61. * @return string a server id
  62. */
  63. function getServerId($name)
  64. {
  65. $result = null;
  66. $params = array(
  67. new SoapVar('<ns1:server by="name">' . $name . '</ns1:server>', XSD_ANYXML),
  68. );
  69. try
  70. {
  71. $result = $this->auth->execSoapCall(
  72. "GetServerRequest",
  73. $params
  74. );
  75. $result = $result['SOAP:ENVELOPE']['SOAP:BODY']['GETSERVERRESPONSE']['SERVER']['ID'];
  76. }
  77. catch (SoapFault $exception)
  78. {
  79. $result = $exception;
  80. }
  81. return $result;
  82. }
  83. /**
  84. * serverExists
  85. * @param string $idOrNameServer server id or server name
  86. * @param string $type value of the server (auto, name, id)
  87. * @return bool exists
  88. */
  89. function serverExists($idOrNameServer, $type="auto")
  90. {
  91. if($type == "auto")
  92. $realType = getServerType($idOrNameServer);
  93. else
  94. $realType = $type;
  95. $result = null;
  96. $params = array(
  97. new SoapVar('<ns1:server by="' . $realType . '">' . $idOrNameServer . '</ns1:server>', XSD_ANYXML),
  98. );
  99. try
  100. {
  101. $result = $this->auth->execSoapCall(
  102. "GetServerRequest",
  103. $params
  104. );
  105. }
  106. catch (SoapFault $exception)
  107. {
  108. $result = $exception;
  109. }
  110. return (!is_a($result, "Exception"));
  111. }
  112. /**
  113. * getServerOptions
  114. * @param string $idOrNameServer server id or server name
  115. * @param string $type value of the server (auto, name, id)
  116. * @return array
  117. */
  118. function getServerOptions($idOrNameServer, $type="auto")
  119. {
  120. if($type == "auto")
  121. $realType = getServerType($idOrNameServer);
  122. else
  123. $realType = $type;
  124. $result = null;
  125. $params = array(
  126. new SoapVar('<ns1:server by="' . $realType . '">' . $idOrNameServer . '</ns1:server>', XSD_ANYXML),
  127. );
  128. try
  129. {
  130. $result = $this->auth->execSoapCall(
  131. "GetServerRequest",
  132. $params
  133. );
  134. $attrs = array();
  135. foreach ($result['SOAP:ENVELOPE']['SOAP:BODY']['GETSERVERRESPONSE']['SERVER']['A'] as $a) {
  136. $attrs[$a['N']] = $a['DATA'];
  137. }
  138. $result = $attrs;
  139. }
  140. catch (SoapFault $exception)
  141. {
  142. $result = $exception;
  143. }
  144. return $result;
  145. }
  146. /**
  147. * createServer
  148. * @param string $name a server name
  149. * @param array $attrs an optional array containing the server attributes to be set
  150. * @return array an array with the new server's info
  151. */
  152. function createServer($name, $attrs = array ())
  153. {
  154. $result = null;
  155. $params = array(
  156. new SoapParam($name, "name"),
  157. );
  158. foreach ($attrs as $key=>$value)
  159. $params[] = new SoapVar('<ns1:a n="' . $key . '">' . $value . '</ns1:a>', XSD_ANYXML);
  160. try
  161. {
  162. $result = $this->auth->execSoapCall(
  163. "CreateServerRequest",
  164. $params
  165. );
  166. $result = $result['SOAP:ENVELOPE']['SOAP:BODY']['CREATESERVERRESPONSE']['SERVER'];
  167. usleep(250000); // introduce a small delay, otherwise some troubles may arise if we modify the new server right after its creation
  168. }
  169. catch (SoapFault $exception)
  170. {
  171. $result = $exception;
  172. }
  173. return $result;
  174. }
  175. /**
  176. * modifyServer
  177. * @param string $idOrNameServer server id or server name
  178. * @param array $attrs an array containing the server attributes to be set
  179. * @param string $type value of the server (auto, name, id)
  180. * @return array informations
  181. */
  182. function modifyServer($idOrNameServer, $attrs = array(), $type="auto")
  183. {
  184. if($type == "auto")
  185. $realType = getServerType($idOrNameServer);
  186. else
  187. $realType = $type;
  188. if($realType == "name")
  189. $serverId = $this->getServerId($idOrNameServer);
  190. else
  191. $serverId = $idOrNameServer;
  192. $result = null;
  193. $params = array(
  194. new SoapParam($serverId, "id"),
  195. );
  196. foreach ($attrs as $key=>$value)
  197. $params[] = new SoapVar('<ns1:a n="' . $key . '">' . $value . '</ns1:a>', XSD_ANYXML);
  198. try
  199. {
  200. $result = $this->auth->execSoapCall(
  201. "ModifyServerRequest",
  202. $params
  203. );
  204. }
  205. catch (SoapFault $exception)
  206. {
  207. $result = $exception;
  208. }
  209. return $result;
  210. }
  211. /**
  212. * deleteServer
  213. * @param string $idOrNameServer server id or server name
  214. * @param string $type value of the server (auto, name, id)
  215. * @return array informations
  216. */
  217. function deleteServer($idOrNameServer, $type="auto")
  218. {
  219. if($type == "auto")
  220. $realType = getServerType($idOrNameServer);
  221. else
  222. $realType = $type;
  223. if($realType == "name")
  224. $serverId = $this->getServerId($idOrNameServer);
  225. else
  226. $serverId = $idOrNameServer;
  227. $result = null;
  228. $params = array(
  229. new SoapParam($serverId, "id"),
  230. );
  231. try
  232. {
  233. $result = $this->auth->execSoapCall(
  234. "DeleteServerRequest",
  235. $params
  236. );
  237. }
  238. catch (SoapFault $exception)
  239. {
  240. $result = $exception;
  241. }
  242. return $result;
  243. }
  244. }
  245. ?>