Connection.php 9.7 KB

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