Connection.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. 'soap_version' => SOAP_1_2,
  104. 'style' => SOAP_RPC,
  105. 'use' => SOAP_LITERAL,
  106. 'stream_context' => stream_context_create(
  107. [
  108. 'ssl' => [
  109. 'verify_peer' => false,
  110. 'verify_peer_name' => false,
  111. ]])
  112. ]
  113. );
  114. /**
  115. * set credential params
  116. */
  117. $this->params = $params;
  118. }
  119. /**
  120. * @param $server
  121. * @param int $port
  122. * @param $username
  123. * @param $password
  124. * @param $user
  125. * @return array
  126. */
  127. protected function getLoginDetails($server, $port, $username, $password, $user, $authToken = null, $preauth = null)
  128. {
  129. $this->serverUrl = self::HTTPS_PROTOCOL . $server . ":" . $port;
  130. /**
  131. *
  132. * admin details
  133. */
  134. if ($user == "admin")
  135. {
  136. $location = self::HTTPS_PROTOCOL . $server . ":" . $port . self::ADMIN_PATH;
  137. $uri = self::URI_ADMIN;
  138. $params = [
  139. new SoapParam($username, "name"),
  140. new SoapParam($password, "password"),
  141. ];
  142. }
  143. /**
  144. *
  145. * user details
  146. */
  147. if ($user == "user")
  148. {
  149. $location = self::HTTPS_PROTOCOL . $server . ":" . ($port ? $port : self::CLIENT_PORT) . self::ACCOUNT_PATH;
  150. $uri = self::URI_ACCOUNT;
  151. $params = [
  152. new SoapVar('<ns1:account by="name">' . $username . '</ns1:account>', XSD_ANYXML),
  153. ];
  154. if($authToken)
  155. {
  156. $params[] = new SoapParam($authToken, "authToken");
  157. }else{
  158. $params[] = new SoapParam($password, "password");
  159. }
  160. }
  161. /**
  162. *
  163. * return data
  164. */
  165. return [$location, $uri, $params];
  166. }
  167. /**
  168. *
  169. *
  170. * @return \Exception|mixed|SoapFault|null
  171. */
  172. public function login()
  173. {
  174. try
  175. {
  176. /**
  177. * set headers
  178. */
  179. $this->setSoapHeader();
  180. /**
  181. *
  182. * authentication request
  183. */
  184. $result = $this->soapClient->__soapCall("AuthRequest", $this->params, null, $this->getSoapHeader());
  185. /**
  186. *
  187. * Save the soapHeader with token
  188. */
  189. $this->authToken = $result['authToken'];
  190. $this->setSoapHeader($this->authToken);
  191. /**
  192. *
  193. * set connected as true
  194. */
  195. $this->setConnected(true);
  196. }
  197. catch (SoapFault $exception)
  198. {
  199. $result = $exception;
  200. /**
  201. * set connected as false
  202. */
  203. $this->setConnected(false);
  204. $this->setConnectionError($exception->getMessage());
  205. }
  206. return $result;
  207. }
  208. /**
  209. * @return mixed
  210. */
  211. function getSoapHeader()
  212. {
  213. return $this->header;
  214. }
  215. /**
  216. * @param null $authToken
  217. */
  218. function setSoapHeader($authToken = null)
  219. {
  220. if (!$authToken)
  221. {
  222. $this->header = new SoapHeader('urn:zimbra', 'context');
  223. }
  224. else
  225. {
  226. $this->header = [
  227. new SoapHeader(
  228. 'urn:zimbra',
  229. 'context',
  230. new SoapVar('<ns2:context><ns2:authToken>' . $authToken . '</ns2:authToken></ns2:context>', XSD_ANYXML)
  231. )
  232. ];
  233. }
  234. }
  235. /**
  236. * @param $request
  237. * @param array $params
  238. * @param array $options
  239. * @return Response
  240. */
  241. public function request($request, $params = [], $options = [])
  242. {
  243. /**
  244. * headers
  245. */
  246. $soapHeader = $this->getSoapHeader();
  247. /**
  248. * response model
  249. */
  250. $response = $this->getResponseModel();
  251. /**
  252. * main request
  253. */
  254. try
  255. {
  256. /**
  257. *
  258. * request to api
  259. */
  260. $this->soapClient->__soapCall(
  261. $request,
  262. $params,
  263. $options,
  264. $soapHeader
  265. );
  266. }
  267. catch (\SoapFault $ex)
  268. {
  269. $response->setLastError($ex->getMessage());
  270. }
  271. catch (\Exception $ex)
  272. {
  273. $response->setLastError($ex->getMessage());
  274. }
  275. /**
  276. * Zimbra api response
  277. */
  278. $soapRes = $this->soapClient->__getLastResponse();
  279. /**
  280. *
  281. * set response model params
  282. */
  283. $response
  284. ->setRequest($request)
  285. ->setParams($params)
  286. ->setOptions($options)
  287. ->setHeaders($soapHeader)
  288. ->setXmlResponse($soapRes)
  289. ->response();
  290. logModuleCall(
  291. 'zimbraEmail',
  292. __FUNCTION__,
  293. $response,
  294. "Debug",
  295. $soapRes
  296. );
  297. return $response;
  298. }
  299. /**
  300. * @param $response
  301. */
  302. public function setResponseModel($response)
  303. {
  304. $this->responseModel = $response;
  305. }
  306. /**
  307. * @return Response
  308. */
  309. public function getResponseModel()
  310. {
  311. if (!$this->responseModel)
  312. {
  313. $this->responseModel = new Response();
  314. }
  315. return $this->responseModel;
  316. }
  317. /**
  318. * @return $this
  319. */
  320. public function cleanResponse()
  321. {
  322. $this->responseModel = null;
  323. return $this;
  324. }
  325. /**
  326. * @return bool
  327. */
  328. public function isConnected()
  329. {
  330. return $this->connected;
  331. }
  332. /**
  333. * @param $connected
  334. * @return $this
  335. */
  336. public function setConnected($connected)
  337. {
  338. $this->connected = $connected;
  339. return $this;
  340. }
  341. /**
  342. * @return null
  343. */
  344. public function getConnectionError()
  345. {
  346. return $this->connectionError;
  347. }
  348. /**
  349. * @param $connectionError
  350. * @return $this
  351. */
  352. public function setConnectionError($connectionError)
  353. {
  354. $this->connectionError = $connectionError;
  355. return $this;
  356. }
  357. /**
  358. * @return string
  359. */
  360. public function getAuthToken()
  361. {
  362. return $this->authToken;
  363. }
  364. /**
  365. * @return string
  366. */
  367. public function getServerUrl()
  368. {
  369. return $this->serverUrl;
  370. }
  371. }