Connection.php 9.5 KB

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