KerioWhmcs.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Api;
  3. use ThurData\Servers\KerioEmail\Api\KerioConnectApi;
  4. /**
  5. *
  6. * Administration API for Kerio Connect - Sample Class.
  7. *
  8. * @copyright Copyright &copy; 2012-2012 Kerio Technologies s.r.o.
  9. * @version 1.3.0.62
  10. */
  11. class KerioWhmcs extends KerioConnectApi {
  12. /**
  13. * Class constructor.
  14. * Register application
  15. *
  16. * @param string Application name
  17. * @param string Vendor of the application
  18. * @param string Application versiopn
  19. *
  20. * @return void
  21. */
  22. public function __construct($name, $vendor, $version) {
  23. $this->api = parent::__construct($name, $vendor, $version);
  24. return $this->api;
  25. }
  26. /**
  27. * Login and get list of available constants user by Kerio Connect
  28. *
  29. * @param string Hostname of remote server
  30. * @param string Administrator username
  31. * @param string Administrator password
  32. *
  33. * @return void
  34. */
  35. public function login($hostname, $username, $password) {
  36. $login = parent::login($hostname, $username, $password);
  37. $this->getServerConstants();
  38. return $login;
  39. }
  40. /**
  41. * Obtain server constants
  42. *
  43. * @param void
  44. * @return void
  45. */
  46. protected function getServerConstants() {
  47. $this->constants = parent::getConstants();
  48. }
  49. /**
  50. * Return list of available constants
  51. *
  52. * @param void
  53. * @return void
  54. */
  55. public function getConstants() {
  56. return $this->constants;
  57. }
  58. /**
  59. * Get list of available domains
  60. *
  61. * @param array List of fields to be obtained from engine
  62. * @return array List of available domains
  63. */
  64. public function getDomains($fields) {
  65. $method = 'Domains.get';
  66. $params = array(
  67. 'query' => array(
  68. 'fields' => $fields
  69. )
  70. );
  71. $result = $this->sendRequest($method, $params);
  72. return $result['list'];
  73. }
  74. /**
  75. * Get Id of a domain by name
  76. *
  77. * @param string Domain name
  78. * @return string Domain Id
  79. */
  80. public function getDomainId($domain) {
  81. $method = 'Domains.get';
  82. $params = array(
  83. 'query' => array(
  84. 'fields' => array(
  85. 'id',
  86. 'name'
  87. )
  88. )
  89. );
  90. $result = $this->sendRequest($method, $params);
  91. foreach($result['list'] as $key) {
  92. if ($key['name'] == $domain) {
  93. return $key['id'];
  94. }
  95. }
  96. return FALSE;
  97. }
  98. /**
  99. * Get list of users from a domain
  100. *
  101. * @param array List of fields to be obtained from engine
  102. * @param string Domain Id
  103. * @param array Additional condition for the request
  104. *
  105. * @return array List of users
  106. */
  107. public function getUsers($fields, $domainId, $conditions = null) {
  108. $method = 'Users.get';
  109. $params = array(
  110. 'query' => array(
  111. 'fields' => $fields,
  112. 'orderBy' => array(array(
  113. 'columnName' => 'loginName',
  114. 'direction' => 'Asc'
  115. ))
  116. ),
  117. 'domainId' => $domainId
  118. );
  119. if ($conditions) {
  120. $params['query']['conditions'] = $conditions;
  121. }
  122. $result = $this->sendRequest($method, $params);
  123. return $result['list'];
  124. }
  125. /**
  126. * Get address of a user
  127. *
  128. * @param string User Id
  129. *
  130. * @return array address of user
  131. */
  132. public function getAddress($id) {
  133. $method = 'Users.getPersonalContact';
  134. $params = array(
  135. 'userIds' => array(
  136. $id,
  137. )
  138. );
  139. $result = $this->sendRequest($method, $params);
  140. return $result;
  141. }
  142. /**
  143. * Get contact of a user
  144. *
  145. * @param string Contact Id
  146. *
  147. * @return array contact of user
  148. */
  149. public function getContact($id) {
  150. $method = 'CompanyContacts.getAvailable';
  151. $params = array(
  152. 'domainId' => $id,
  153. );
  154. $result = $this->sendRequest($method, $params);
  155. return $result;
  156. }
  157. /**
  158. * Get login name by user's Id
  159. *
  160. * @param string User Id
  161. * @param string Domain Id
  162. *
  163. * @return string Login name
  164. */
  165. public function getUserById($userId, $domainId) {
  166. $fields = array('id', 'loginName');
  167. $userList = $this->getUsers($fields, $domainId);
  168. foreach ($userList as $user) {
  169. if ($user['id'] == $userId) return $user['loginName'];
  170. }
  171. return FALSE;
  172. }
  173. /**
  174. * Get list of groups from a domain
  175. *
  176. * @param array List of fields to be obtained from engine
  177. * @param string Domain Id
  178. * @param array Additional condition for the request
  179. *
  180. * @return array List of groups
  181. */
  182. public function getGroups($fields, $domainId, $conditions = null) {
  183. $method = 'Groups.get';
  184. $params = array(
  185. 'query' => array(
  186. 'fields' => $fields,
  187. 'orderBy' => array(array(
  188. 'columnName' => 'name',
  189. 'direction' => 'Asc'
  190. ))
  191. ),
  192. 'domainId' => $domainId
  193. );
  194. if ($conditions) {
  195. $params['query']['conditions'] = $conditions;
  196. }
  197. $result = $this->sendRequest($method, $params);
  198. return $result['list'];
  199. }
  200. /**
  201. * Create new group
  202. *
  203. * @param array User defined params
  204. * @return array Result of create action
  205. */
  206. public function createGroup($params) {
  207. $method = 'Groups.create';
  208. $result = $this->sendRequest($method, $params);
  209. return $result['result'];
  210. }
  211. /**
  212. * Add members to group of given ID
  213. *
  214. * @param string Group ID
  215. * @param array List of user IDs to be added
  216. *
  217. * @return void
  218. */
  219. public function addMembersToGroup($groupId, $userList) {
  220. $method = 'Groups.addMemberList';
  221. $params = array(
  222. 'userList' => $userList,
  223. 'groupId' => $groupId
  224. );
  225. $this->sendRequest($method, $params);
  226. }
  227. /**
  228. * Get list of mailing lists from a domain
  229. *
  230. * @param array List of fields to be obtained from engine
  231. * @param string Domain Id
  232. * @param array Additional condition for the request
  233. *
  234. * @return array List of mailing lists
  235. */
  236. public function getMailingLists($fields, $domainId, $conditions = null) {
  237. $method = 'MailingLists.get';
  238. $params = array(
  239. 'query' => array(
  240. 'fields' => $fields,
  241. 'orderBy' => array(array(
  242. 'columnName' => 'name',
  243. 'direction' => 'Asc'
  244. ))
  245. ),
  246. 'domainId' => $domainId
  247. );
  248. if ($conditions) {
  249. $params['query']['conditions'] = $conditions;
  250. }
  251. $result = $this->sendRequest($method, $params);
  252. return $result['list'];
  253. }
  254. /**
  255. * Get list of mailing lists from a domain
  256. *
  257. * @param array List of fields to be obtained from engine
  258. * @param string Mailing list Id
  259. *
  260. * @return array List of mailing lists
  261. */
  262. public function getMlUserList($fields, $mlId) {
  263. $method = 'MailingLists.getMlUserList';
  264. $params = array(
  265. 'query' => array(
  266. 'fields' => $fields
  267. ),
  268. 'mlId' => $mlId
  269. );
  270. $result = $this->sendRequest($method, $params);
  271. return $result['list'];
  272. }
  273. /**
  274. * Get list of resources from a domain
  275. *
  276. * @param array List of fields to be obtained from engine
  277. * @param string Domain Id
  278. * @param array Additional condition for the request
  279. *
  280. * @return array List of mailing lists
  281. */
  282. public function getResources($fields, $domainId, $conditions = null) {
  283. $method = 'Resources.get';
  284. $params = array(
  285. 'query' => array(
  286. 'fields' => $fields,
  287. 'orderBy' => array(array(
  288. 'columnName' => 'name',
  289. 'direction' => 'Asc'
  290. ))
  291. ),
  292. 'domainId' => $domainId
  293. );
  294. if ($conditions) {
  295. $params['query']['conditions'] = $conditions;
  296. }
  297. $result = $this->sendRequest($method, $params);
  298. return $result['list'];
  299. }
  300. /**
  301. * Get list of aliases from a domain
  302. *
  303. * @param array List of fields to be obtained from engine
  304. * @param string Domain Id
  305. * @param array Additional condition for the request
  306. *
  307. * @return array List of aliases
  308. */
  309. public function getAliases($fields, $domainId, $conditions = null) {
  310. $method = 'Aliases.get';
  311. $params = array(
  312. 'query' => array(
  313. 'fields' => $fields,
  314. 'orderBy' => array(array(
  315. 'columnName' => 'name',
  316. 'direction' => 'Asc'
  317. )),
  318. 'combining' => 'Or'
  319. ),
  320. 'domainId' => $domainId
  321. );
  322. if ($conditions) {
  323. $params['query']['conditions'] = $conditions;
  324. }
  325. $result = $this->sendRequest($method, $params);
  326. return $result['list'];
  327. }
  328. /**
  329. * Get list of all services
  330. *
  331. * @param void
  332. * @return array List of services
  333. */
  334. function getServices() {
  335. $method = 'Services.get';
  336. $params = array();
  337. $result = $this->sendRequest($method, $params);
  338. return $result['services'];
  339. }
  340. /**
  341. * Get list of all services
  342. *
  343. * @param void
  344. * @return array List of services
  345. */
  346. function getServerStatistics() {
  347. $method = 'Statistics.get';
  348. $params = array();
  349. $result = $this->sendRequest($method, $params);
  350. return $result['statistics'];
  351. }
  352. /**
  353. * Get server info
  354. *
  355. * @param void
  356. * @return array List of services
  357. */
  358. function getServerInfo() {
  359. $method = 'Server.getProductInfo';
  360. $params = array();
  361. $result = $this->sendRequest($method, $params);
  362. return $result;
  363. }
  364. /**
  365. * Create alias.
  366. *
  367. * @param string Domain ID
  368. * @param string Alias
  369. * @param string Email
  370. * @param string Description, optional
  371. * @return array Result
  372. */
  373. function createAlias($domain, $alias, $email, $description = '') {
  374. $params = array(
  375. 'aliases' => array(array(
  376. 'name' => $alias,
  377. 'domainId' => $domain,
  378. 'deliverTo' => $email,
  379. 'description' => $description,
  380. 'deliverToSelect' => 'TypeEmailAddress'
  381. ))
  382. );
  383. $result = $this->sendRequest('Aliases.create', $params);
  384. return $result;
  385. }
  386. /**
  387. * Create user.
  388. *
  389. * @param string Domain ID
  390. * @param string Username
  391. * @param string Password
  392. * @return array Result
  393. */
  394. function createUser($domain, $username, $password) {
  395. $params = array(
  396. 'users' => array(array(
  397. 'loginName' => $username,
  398. 'password' => $password,
  399. 'domainId' => $domain,
  400. 'isEnabled' => TRUE
  401. ))
  402. );
  403. $result = $this->sendRequest('Users.create', $params);
  404. return $result;
  405. }
  406. /**
  407. * Modify user.
  408. *
  409. * @param string User ID
  410. * @param array Attributes
  411. * @return array Result
  412. */
  413. function modifyUser($userId, $attr) {
  414. $params = array(
  415. 'userIds' => [ $userId ],
  416. 'pattern' => $attr
  417. );
  418. $result = $this->sendRequest('Users.set', $params);
  419. return $result;
  420. }
  421. /**
  422. * Delete user.
  423. *
  424. * @param string User ID
  425. * @return array Result
  426. */
  427. function deleteUser($userId) {
  428. $params = array(
  429. 'requests' => array(
  430. array(
  431. 'userId' => $userId,
  432. 'mode' => 'DSModeDeactivate',
  433. 'method' => 'UDeleteFolder',
  434. 'targetUserId' => '',
  435. 'removeReferences' => TRUE,
  436. ),
  437. ),
  438. );
  439. $result = $this->sendRequest('Users.remove', $params);
  440. return $result;
  441. }
  442. /**
  443. * Create domain.
  444. *
  445. * @param string Domain name
  446. * @return array Result
  447. */
  448. function createDomain($domain) {
  449. $params = array(
  450. 'domains' => array(array(
  451. 'name' => $domain
  452. ))
  453. );
  454. $result = $this->sendRequest('Domains.create', $params);
  455. return $result;
  456. }
  457. /**
  458. * Modify domain.
  459. *
  460. * @param string DomainID
  461. * @param array Attributes
  462. * @return array Result
  463. */
  464. function modifyDomain($domainID, $attr) {
  465. $params = array(
  466. 'domainIds' => array($domainID),
  467. 'pattern' => $attr,
  468. );
  469. $result = $this->sendRequest('Domains.set', $params);
  470. return $result;
  471. }
  472. /**
  473. * Delete domain.
  474. *
  475. * @param string DomainID
  476. * @return array Result
  477. */
  478. function deleteDomain($domainID) {
  479. $params = array(
  480. 'domainIds' => array($domainID),
  481. );
  482. $result = $this->sendRequest('Domains.remove', $params);
  483. return $result;
  484. }
  485. /**
  486. * Get list of IP addresses from a file.
  487. *
  488. * Local function used in example spam_blacklist
  489. *
  490. * @param string Filename
  491. * @return array List of IP addresses
  492. * @throws KerioApiException
  493. */
  494. public function getBlacklistRecords($file) {
  495. $blacklist = array();
  496. if(file_exists($file) && is_readable($file)) {
  497. $data = file_get_contents($file);
  498. foreach (preg_split("/\n/", $data) as $record) {
  499. if (empty($record)) continue;
  500. array_push($blacklist, $record);
  501. }
  502. }
  503. else {
  504. throw new KerioApiException(sprintf('Cannot open file %s', $file));
  505. }
  506. return $blacklist;
  507. }
  508. /**
  509. * Get list of IP addesses from a group
  510. *
  511. * Local function used in example spam_blacklist
  512. *
  513. * @param string Group name
  514. * @return array List of IP addresses
  515. */
  516. public function getIpGroupList($name) {
  517. $params = array(
  518. "query" => array(
  519. "conditions" => array(array(
  520. "fieldName" => "name",
  521. "comparator" => "Like",
  522. "value" => $name
  523. )),
  524. "orderBy" => array(array(
  525. "columnName" => "item",
  526. "direction" => "Asc"
  527. ))
  528. )
  529. );
  530. $result = $this->sendRequest('IpAddressGroups.get', $params);
  531. return $result['list'];
  532. }
  533. /**
  534. * Add a IP address to a IP Group
  535. *
  536. * Local function used in example spam_blacklist
  537. *
  538. * @param string Group name
  539. * @param string IP address
  540. * @param string Description, optional
  541. * @return array Result
  542. */
  543. public function addHostToIpGroup($group, $ip, $description = '') {
  544. if(empty($description)) {
  545. $description = sprintf('Automatically added on %s', date(DATE_RFC822));
  546. }
  547. $params = array(
  548. "groups" => array(array(
  549. "groupId" => "",
  550. "groupName" => $group,
  551. "host" => $ip,
  552. "type" => "Host",
  553. "description" => $description,
  554. "enabled" => TRUE
  555. ))
  556. );
  557. $result = $this->sendRequest('IpAddressGroups.create', $params);
  558. return $result;
  559. }
  560. /**
  561. * Remove a IP address from a IP Group
  562. *
  563. * Local function used in example spam_blacklist
  564. *
  565. * @param string Group name
  566. * @param string IP address
  567. * @return array Result
  568. */
  569. public function removeHostFromIpGroup($group, $ip) {
  570. $list = $this->getIpGroupList(NULL);
  571. foreach ($list as $record) {
  572. if(($record['groupName'] != $group) || ($record['host'] != $ip)) continue;
  573. $hostId = $record['id'];
  574. }
  575. $params = array("groupIds" => array($hostId));
  576. $result = $this->sendRequest('IpAddressGroups.remove', $params);
  577. return $result;
  578. }
  579. /**
  580. * Random password generator
  581. *
  582. * Local function used in example createUser.
  583. *
  584. * @param integer Password lenght, default 10
  585. * @return string Random password
  586. */
  587. function genRandomPassword($length = 10) {
  588. $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
  589. $string = '';
  590. for ($p = 0; $p < $length; $p++) {
  591. $string .= $characters[mt_rand(0, (strlen($characters))-1)];
  592. }
  593. return $string;
  594. }
  595. }