Connection.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. namespace ModulesGarden\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap;
  3. use ModulesGarden\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Interfaces\ConnectionInterface;
  4. use ModulesGarden\Servers\KerioEmail\App\Libs\Kerio\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:kerioAdmin';
  23. const URI_ACCOUNT = 'urn:kerioAccount';
  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\KerioEmail\App\Libs\Kerio\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. /**
  185. *
  186. * Save the soapHeader with token
  187. */
  188. $this->authToken = $result['authToken'];
  189. $this->setSoapHeader($this->authToken);
  190. /**
  191. *
  192. * set connected as true
  193. */
  194. $this->setConnected(true);
  195. }
  196. catch (SoapFault $exception)
  197. {
  198. $result = $exception;
  199. /**
  200. * set connected as false
  201. */
  202. $this->setConnected(false);
  203. $this->setConnectionError($exception->getMessage());
  204. }
  205. return $result;
  206. }
  207. /**
  208. * @return mixed
  209. */
  210. function getSoapHeader()
  211. {
  212. return $this->header;
  213. }
  214. /**
  215. * @param null $authToken
  216. */
  217. function setSoapHeader($authToken = null)
  218. {
  219. if (!$authToken)
  220. {
  221. $this->header = new SoapHeader('urn:kerio', 'context');
  222. }
  223. else
  224. {
  225. $this->header = [
  226. new SoapHeader(
  227. 'urn:kerio',
  228. 'context',
  229. new SoapVar('<ns2:context><ns2:authToken>' . $authToken . '</ns2:authToken></ns2:context>', XSD_ANYXML)
  230. )
  231. ];
  232. }
  233. }
  234. /**
  235. * @param $request
  236. * @param array $params
  237. * @param array $options
  238. * @return Response
  239. */
  240. public function request($request, $params = [], $options = [])
  241. {
  242. /**
  243. * headers
  244. */
  245. $soapHeader = $this->getSoapHeader();
  246. /**
  247. * response model
  248. */
  249. $response = $this->getResponseModel();
  250. /**
  251. * main request
  252. */
  253. try
  254. {
  255. /**
  256. *
  257. * request to api
  258. */
  259. $this->soapClient->__soapCall(
  260. $request,
  261. $params,
  262. $options,
  263. $soapHeader
  264. );
  265. }
  266. catch (\SoapFault $ex)
  267. {
  268. $response->setLastError($ex->getMessage());
  269. }
  270. catch (\Exception $ex)
  271. {
  272. $response->setLastError($ex->getMessage());
  273. }
  274. /**
  275. * Kerio api response
  276. */
  277. $soapRes = $this->soapClient->__getLastResponse();
  278. /**
  279. *
  280. * set response model params
  281. */
  282. $response
  283. ->setRequest($request)
  284. ->setParams($params)
  285. ->setOptions($options)
  286. ->setHeaders($soapHeader)
  287. ->setXmlResponse($soapRes)
  288. ->response();
  289. return $response;
  290. }
  291. /**
  292. * @param $response
  293. */
  294. public function setResponseModel($response)
  295. {
  296. $this->responseModel = $response;
  297. }
  298. /**
  299. * @return Response
  300. */
  301. public function getResponseModel()
  302. {
  303. if (!$this->responseModel)
  304. {
  305. $this->responseModel = new Response();
  306. }
  307. return $this->responseModel;
  308. }
  309. /**
  310. * @return $this
  311. */
  312. public function cleanResponse()
  313. {
  314. $this->responseModel = null;
  315. return $this;
  316. }
  317. /**
  318. * @return bool
  319. */
  320. public function isConnected()
  321. {
  322. return $this->connected;
  323. }
  324. /**
  325. * @param $connected
  326. * @return $this
  327. */
  328. public function setConnected($connected)
  329. {
  330. $this->connected = $connected;
  331. return $this;
  332. }
  333. /**
  334. * @return null
  335. */
  336. public function getConnectionError()
  337. {
  338. return $this->connectionError;
  339. }
  340. /**
  341. * @param $connectionError
  342. * @return $this
  343. */
  344. public function setConnectionError($connectionError)
  345. {
  346. $this->connectionError = $connectionError;
  347. return $this;
  348. }
  349. /**
  350. * @return string
  351. */
  352. public function getAuthToken()
  353. {
  354. return $this->authToken;
  355. }
  356. /**
  357. * @return string
  358. */
  359. public function getServerUrl()
  360. {
  361. return $this->serverUrl;
  362. }
  363. }