Connection.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. namespace ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap;
  3. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Interfaces\ConnectionInterface;
  4. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Helpers\XmlParser;
  5. use \SoapFault;
  6. use \SoapHeader;
  7. use \SoapParam;
  8. use \SoapVar;
  9. /**
  10. *
  11. * Created by PhpStorm.
  12. * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
  13. * Date: 28.08.19
  14. * Time: 13:47
  15. * Class Connection
  16. */
  17. class Connection implements ConnectionInterface
  18. {
  19. /**
  20. * uri
  21. */
  22. const URI_ADMIN = 'urn:zimbraAdmin';
  23. const URI_ACCOUNT = 'urn:zimbraAccount';
  24. /**
  25. * protocol
  26. */
  27. const HTTPS_PROTOCOL = 'https://';
  28. /**
  29. * path
  30. */
  31. const ADMIN_PATH = '/service/admin/soap/';
  32. const ACCOUNT_PATH = '/service/soap/';
  33. /**
  34. * ports
  35. */
  36. const CLIENT_PORT = '8443';
  37. /**
  38. * @var MySoapClient
  39. */
  40. protected $soapClient;
  41. /**
  42. * @var mixed
  43. */
  44. protected $params;
  45. /**
  46. * @var
  47. */
  48. protected $header;
  49. /**
  50. * @var
  51. */
  52. protected $responseModel;
  53. /**
  54. * @var bool
  55. */
  56. protected $connected = false;
  57. /**
  58. * @var null
  59. */
  60. protected $connectionError = null;
  61. /**
  62. * @var string
  63. */
  64. protected $authToken = '';
  65. /**
  66. * @var string
  67. */
  68. protected $serverUrl = '';
  69. protected $server;
  70. protected $port;
  71. /**
  72. * Connection constructor.
  73. * @param $server
  74. * @param int $port
  75. * @param $username
  76. * @param $password
  77. * @param string $user
  78. * @throws SoapFault
  79. */
  80. public function __construct($server, $port = 7071, $username, $password = null, $user = "admin", $authToken = null , $preauth = null)
  81. {
  82. /**
  83. * set params
  84. */
  85. $this->server = $server;
  86. $this->port = $port;
  87. /**
  88. *
  89. * get login credential by type
  90. */
  91. list($location, $uri, $params) = $this->getLoginDetails($server, $port, $username, $password, $user,$authToken, $preauth);
  92. /**
  93. *
  94. * create new soap client with data
  95. */
  96. $this->soapClient = new \ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\MySoapClient(null,
  97. [
  98. 'location' => $location,
  99. 'uri' => $uri,
  100. 'default_socket_timeout' => 10,
  101. 'trace' => 1,
  102. 'exceptions' => 1,
  103. 'keep_alive' => false,
  104. 'soap_version' => SOAP_1_2,
  105. 'style' => SOAP_RPC,
  106. 'use' => SOAP_LITERAL,
  107. 'stream_context' => stream_context_create(
  108. [
  109. 'ssl' => [
  110. 'verify_peer' => false,
  111. 'verify_peer_name' => false,
  112. ]])
  113. ]
  114. );
  115. /**
  116. * set credential params
  117. */
  118. $this->params = $params;
  119. }
  120. /**
  121. * @param $server
  122. * @param int $port
  123. * @param $username
  124. * @param $password
  125. * @param $user
  126. * @return array
  127. */
  128. protected function getLoginDetails($server, $port, $username, $password, $user, $authToken = null, $preauth = null)
  129. {
  130. $this->serverUrl = self::HTTPS_PROTOCOL . $server . ":" . $port;
  131. /**
  132. *
  133. * admin details
  134. */
  135. if ($user == "admin")
  136. {
  137. $location = self::HTTPS_PROTOCOL . $server . ":" . $port . self::ADMIN_PATH;
  138. $uri = self::URI_ADMIN;
  139. $params = [
  140. new SoapParam($username, "name"),
  141. new SoapParam($password, "password"),
  142. ];
  143. }
  144. /**
  145. *
  146. * user details
  147. */
  148. if ($user == "user")
  149. {
  150. $location = self::HTTPS_PROTOCOL . $server . ":" . ($port ? $port : self::CLIENT_PORT) . self::ACCOUNT_PATH;
  151. $uri = self::URI_ACCOUNT;
  152. $params = [
  153. new SoapVar('<ns1:account by="name">' . $username . '</ns1:account>', XSD_ANYXML),
  154. ];
  155. if($authToken)
  156. {
  157. $params[] = new SoapParam($authToken, "authToken");
  158. }else{
  159. $params[] = new SoapParam($password, "password");
  160. }
  161. }
  162. /**
  163. *
  164. * return data
  165. */
  166. return [$location, $uri, $params];
  167. }
  168. /**
  169. *
  170. *
  171. * @return \Exception|mixed|SoapFault|null
  172. */
  173. public function login()
  174. {
  175. try
  176. {
  177. /**
  178. * set headers
  179. */
  180. $this->setSoapHeader();
  181. /**
  182. *
  183. * authentication request
  184. */
  185. $result = $this->soapClient->__soapCall("AuthRequest", $this->params, null, $this->getSoapHeader());
  186. /**
  187. *
  188. * Save the soapHeader with token
  189. */
  190. $this->authToken = $result['authToken'];
  191. $this->setSoapHeader($this->authToken);
  192. /**
  193. *
  194. * set connected as true
  195. */
  196. $this->setConnected(true);
  197. }
  198. catch (SoapFault $exception)
  199. {
  200. $result = $exception;
  201. /**
  202. * set connected as false
  203. */
  204. $this->setConnected(false);
  205. $this->setConnectionError($exception->getMessage());
  206. }
  207. return $result;
  208. }
  209. /**
  210. * @return mixed
  211. */
  212. function getSoapHeader()
  213. {
  214. return $this->header;
  215. }
  216. /**
  217. * @param null $authToken
  218. */
  219. function setSoapHeader($authToken = null)
  220. {
  221. if (!$authToken)
  222. {
  223. $this->header = new SoapHeader('urn:zimbra', 'context');
  224. }
  225. else
  226. {
  227. $this->header = [
  228. new SoapHeader(
  229. 'urn:zimbra',
  230. 'context',
  231. new SoapVar('<ns2:context><ns2:authToken>' . $authToken . '</ns2:authToken></ns2:context>', XSD_ANYXML)
  232. )
  233. ];
  234. }
  235. }
  236. /**
  237. * @param $request
  238. * @param array $params
  239. * @param array $options
  240. * @return Response
  241. */
  242. public function request($request, $params = [], $options = [])
  243. {
  244. /**
  245. * headers
  246. */
  247. $soapHeader = $this->getSoapHeader();
  248. /**
  249. * response model
  250. */
  251. $response = $this->getResponseModel();
  252. /**
  253. * main request
  254. */
  255. try
  256. {
  257. /**
  258. *
  259. * request to api
  260. */
  261. $this->soapClient->__soapCall(
  262. $request,
  263. $params,
  264. $options,
  265. $soapHeader
  266. );
  267. }
  268. catch (\SoapFault $ex)
  269. {
  270. $response->setLastError($ex->getMessage());
  271. }
  272. catch (\Exception $ex)
  273. {
  274. $response->setLastError($ex->getMessage());
  275. }
  276. /**
  277. * Zimbra api response
  278. */
  279. $soapRes = $this->soapClient->__getLastResponse();
  280. /**
  281. *
  282. * set response model params
  283. */
  284. $response
  285. ->setRequest($request)
  286. ->setParams($params)
  287. ->setOptions($options)
  288. ->setHeaders($soapHeader)
  289. ->setXmlResponse($soapRes)
  290. ->response();
  291. return $response;
  292. }
  293. /**
  294. * @param $response
  295. */
  296. public function setResponseModel($response)
  297. {
  298. $this->responseModel = $response;
  299. }
  300. /**
  301. * @return Response
  302. */
  303. public function getResponseModel()
  304. {
  305. if (!$this->responseModel)
  306. {
  307. $this->responseModel = new Response();
  308. }
  309. return $this->responseModel;
  310. }
  311. /**
  312. * @return $this
  313. */
  314. public function cleanResponse()
  315. {
  316. $this->responseModel = null;
  317. return $this;
  318. }
  319. /**
  320. * @return bool
  321. */
  322. public function isConnected()
  323. {
  324. return $this->connected;
  325. }
  326. /**
  327. * @param $connected
  328. * @return $this
  329. */
  330. public function setConnected($connected)
  331. {
  332. $this->connected = $connected;
  333. return $this;
  334. }
  335. /**
  336. * @return null
  337. */
  338. public function getConnectionError()
  339. {
  340. return $this->connectionError;
  341. }
  342. /**
  343. * @param $connectionError
  344. * @return $this
  345. */
  346. public function setConnectionError($connectionError)
  347. {
  348. $this->connectionError = $connectionError;
  349. return $this;
  350. }
  351. /**
  352. * @return string
  353. */
  354. public function getAuthToken()
  355. {
  356. return $this->authToken;
  357. }
  358. /**
  359. * @return string
  360. */
  361. public function getServerUrl()
  362. {
  363. return $this->serverUrl;
  364. }
  365. }