KerioWhmcs.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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.get';
  151. /* $params = array(
  152. 'id' => array(
  153. $id,
  154. )
  155. );
  156. $result = $this->sendRequest($method, $params);
  157. return $result; */
  158. return $this->sendRequest($method);
  159. }
  160. /**
  161. * Get login name by user's Id
  162. *
  163. * @param string User Id
  164. * @param string Domain Id
  165. *
  166. * @return string Login name
  167. */
  168. public function getUserById($userId, $domainId) {
  169. $fields = array('id', 'loginName');
  170. $userList = $this->getUsers($fields, $domainId);
  171. foreach ($userList as $user) {
  172. if ($user['id'] == $userId) return $user['loginName'];
  173. }
  174. return FALSE;
  175. }
  176. /**
  177. * Get list of groups from a domain
  178. *
  179. * @param array List of fields to be obtained from engine
  180. * @param string Domain Id
  181. * @param array Additional condition for the request
  182. *
  183. * @return array List of groups
  184. */
  185. public function getGroups($fields, $domainId, $conditions = null) {
  186. $method = 'Groups.get';
  187. $params = array(
  188. 'query' => array(
  189. 'fields' => $fields,
  190. 'orderBy' => array(array(
  191. 'columnName' => 'name',
  192. 'direction' => 'Asc'
  193. ))
  194. ),
  195. 'domainId' => $domainId
  196. );
  197. if ($conditions) {
  198. $params['query']['conditions'] = $conditions;
  199. }
  200. $result = $this->sendRequest($method, $params);
  201. return $result['list'];
  202. }
  203. /**
  204. * Create new group
  205. *
  206. * @param array User defined params
  207. * @return array Result of create action
  208. */
  209. public function createGroup($params) {
  210. $method = 'Groups.create';
  211. $result = $this->sendRequest($method, $params);
  212. return $result['result'];
  213. }
  214. /**
  215. * Add members to group of given ID
  216. *
  217. * @param string Group ID
  218. * @param array List of user IDs to be added
  219. *
  220. * @return void
  221. */
  222. public function addMembersToGroup($groupId, $userList) {
  223. $method = 'Groups.addMemberList';
  224. $params = array(
  225. 'userList' => $userList,
  226. 'groupId' => $groupId
  227. );
  228. $this->sendRequest($method, $params);
  229. }
  230. /**
  231. * Get list of mailing lists from a domain
  232. *
  233. * @param array List of fields to be obtained from engine
  234. * @param string Domain Id
  235. * @param array Additional condition for the request
  236. *
  237. * @return array List of mailing lists
  238. */
  239. public function getMailingLists($fields, $domainId, $conditions = null) {
  240. $method = 'MailingLists.get';
  241. $params = array(
  242. 'query' => array(
  243. 'fields' => $fields,
  244. 'orderBy' => array(array(
  245. 'columnName' => 'name',
  246. 'direction' => 'Asc'
  247. ))
  248. ),
  249. 'domainId' => $domainId
  250. );
  251. if ($conditions) {
  252. $params['query']['conditions'] = $conditions;
  253. }
  254. $result = $this->sendRequest($method, $params);
  255. return $result['list'];
  256. }
  257. /**
  258. * Get list of mailing lists from a domain
  259. *
  260. * @param array List of fields to be obtained from engine
  261. * @param string Mailing list Id
  262. *
  263. * @return array List of mailing lists
  264. */
  265. public function getMlUserList($fields, $mlId) {
  266. $method = 'MailingLists.getMlUserList';
  267. $params = array(
  268. 'query' => array(
  269. 'fields' => $fields
  270. ),
  271. 'mlId' => $mlId
  272. );
  273. $result = $this->sendRequest($method, $params);
  274. return $result['list'];
  275. }
  276. /**
  277. * Get list of resources from a domain
  278. *
  279. * @param array List of fields to be obtained from engine
  280. * @param string Domain Id
  281. * @param array Additional condition for the request
  282. *
  283. * @return array List of mailing lists
  284. */
  285. public function getResources($fields, $domainId, $conditions = null) {
  286. $method = 'Resources.get';
  287. $params = array(
  288. 'query' => array(
  289. 'fields' => $fields,
  290. 'orderBy' => array(array(
  291. 'columnName' => 'name',
  292. 'direction' => 'Asc'
  293. ))
  294. ),
  295. 'domainId' => $domainId
  296. );
  297. if ($conditions) {
  298. $params['query']['conditions'] = $conditions;
  299. }
  300. $result = $this->sendRequest($method, $params);
  301. return $result['list'];
  302. }
  303. /**
  304. * Get list of aliases from a domain
  305. *
  306. * @param array List of fields to be obtained from engine
  307. * @param string Domain Id
  308. * @param array Additional condition for the request
  309. *
  310. * @return array List of aliases
  311. */
  312. public function getAliases($fields, $domainId, $conditions = null) {
  313. $method = 'Aliases.get';
  314. $params = array(
  315. 'query' => array(
  316. 'fields' => $fields,
  317. 'orderBy' => array(array(
  318. 'columnName' => 'name',
  319. 'direction' => 'Asc'
  320. )),
  321. 'combining' => 'Or'
  322. ),
  323. 'domainId' => $domainId
  324. );
  325. if ($conditions) {
  326. $params['query']['conditions'] = $conditions;
  327. }
  328. $result = $this->sendRequest($method, $params);
  329. return $result['list'];
  330. }
  331. /**
  332. * Get list of all services
  333. *
  334. * @param void
  335. * @return array List of services
  336. */
  337. function getServices() {
  338. $method = 'Services.get';
  339. $params = array();
  340. $result = $this->sendRequest($method, $params);
  341. return $result['services'];
  342. }
  343. /**
  344. * Get list of all services
  345. *
  346. * @param void
  347. * @return array List of services
  348. */
  349. function getServerStatistics() {
  350. $method = 'Statistics.get';
  351. $params = array();
  352. $result = $this->sendRequest($method, $params);
  353. return $result['statistics'];
  354. }
  355. /**
  356. * Get server info
  357. *
  358. * @param void
  359. * @return array List of services
  360. */
  361. function getServerInfo() {
  362. $method = 'Server.getProductInfo';
  363. $params = array();
  364. $result = $this->sendRequest($method, $params);
  365. return $result;
  366. }
  367. /**
  368. * Create alias.
  369. *
  370. * @param string Domain ID
  371. * @param string Alias
  372. * @param string Email
  373. * @param string Description, optional
  374. * @return array Result
  375. */
  376. function createAlias($domain, $alias, $email, $description = '') {
  377. $params = array(
  378. 'aliases' => array(array(
  379. 'name' => $alias,
  380. 'domainId' => $domain,
  381. 'deliverTo' => $email,
  382. 'description' => $description,
  383. 'deliverToSelect' => 'TypeEmailAddress'
  384. ))
  385. );
  386. $result = $this->sendRequest('Aliases.create', $params);
  387. return $result;
  388. }
  389. /**
  390. * Create user.
  391. *
  392. * @param string Domain ID
  393. * @param string Username
  394. * @param string Password
  395. * @return array Result
  396. */
  397. function createUser($domain, $username, $password) {
  398. $params = array(
  399. 'users' => array(array(
  400. 'loginName' => $username,
  401. 'password' => $password,
  402. 'domainId' => $domain,
  403. 'isEnabled' => TRUE
  404. ))
  405. );
  406. $result = $this->sendRequest('Users.create', $params);
  407. return $result;
  408. }
  409. /**
  410. * Modify user.
  411. *
  412. * @param string User ID
  413. * @param array Attributes
  414. * @return array Result
  415. */
  416. function modifyUser($userId, $attr) {
  417. $params = array(
  418. 'userIds' => [ $userId ],
  419. 'pattern' => $attr
  420. );
  421. $result = $this->sendRequest('Users.set', $params);
  422. return $result;
  423. }
  424. /**
  425. * Delete user.
  426. *
  427. * @param string User ID
  428. * @return array Result
  429. */
  430. function deleteUser($userId) {
  431. $params = array(
  432. 'requests' => array(
  433. array(
  434. 'userId' => $userId,
  435. 'mode' => 'DSModeDeactivate',
  436. 'method' => 'UDeleteFolder',
  437. 'targetUserId' => '',
  438. 'removeReferences' => TRUE,
  439. ),
  440. ),
  441. );
  442. $result = $this->sendRequest('Users.remove', $params);
  443. return $result;
  444. }
  445. /**
  446. * Create domain.
  447. *
  448. * @param string Domain name
  449. * @return array Result
  450. */
  451. function createDomain($domain) {
  452. $params = array(
  453. 'domains' => array(array(
  454. 'name' => $domain
  455. ))
  456. );
  457. $result = $this->sendRequest('Domains.create', $params);
  458. return $result;
  459. }
  460. /**
  461. * Modify domain.
  462. *
  463. * @param string DomainID
  464. * @param array Attributes
  465. * @return array Result
  466. */
  467. function modifyDomain($domainID, $attr) {
  468. $params = array(
  469. 'domainIds' => array($domainID),
  470. 'pattern' => $attr,
  471. );
  472. $result = $this->sendRequest('Domains.set', $params);
  473. return $result;
  474. }
  475. /**
  476. * Delete domain.
  477. *
  478. * @param string DomainID
  479. * @return array Result
  480. */
  481. function deleteDomain($domainID) {
  482. $params = array(
  483. 'domainIds' => array($domainID),
  484. );
  485. $result = $this->sendRequest('Domains.remove', $params);
  486. return $result;
  487. }
  488. /**
  489. * Get list of IP addresses from a file.
  490. *
  491. * Local function used in example spam_blacklist
  492. *
  493. * @param string Filename
  494. * @return array List of IP addresses
  495. * @throws KerioApiException
  496. */
  497. public function getBlacklistRecords($file) {
  498. $blacklist = array();
  499. if(file_exists($file) && is_readable($file)) {
  500. $data = file_get_contents($file);
  501. foreach (preg_split("/\n/", $data) as $record) {
  502. if (empty($record)) continue;
  503. array_push($blacklist, $record);
  504. }
  505. }
  506. else {
  507. throw new KerioApiException(sprintf('Cannot open file %s', $file));
  508. }
  509. return $blacklist;
  510. }
  511. /**
  512. * Get list of IP addesses from a group
  513. *
  514. * Local function used in example spam_blacklist
  515. *
  516. * @param string Group name
  517. * @return array List of IP addresses
  518. */
  519. public function getIpGroupList($name) {
  520. $params = array(
  521. "query" => array(
  522. "conditions" => array(array(
  523. "fieldName" => "name",
  524. "comparator" => "Like",
  525. "value" => $name
  526. )),
  527. "orderBy" => array(array(
  528. "columnName" => "item",
  529. "direction" => "Asc"
  530. ))
  531. )
  532. );
  533. $result = $this->sendRequest('IpAddressGroups.get', $params);
  534. return $result['list'];
  535. }
  536. /**
  537. * Add a IP address to a IP Group
  538. *
  539. * Local function used in example spam_blacklist
  540. *
  541. * @param string Group name
  542. * @param string IP address
  543. * @param string Description, optional
  544. * @return array Result
  545. */
  546. public function addHostToIpGroup($group, $ip, $description = '') {
  547. if(empty($description)) {
  548. $description = sprintf('Automatically added on %s', date(DATE_RFC822));
  549. }
  550. $params = array(
  551. "groups" => array(array(
  552. "groupId" => "",
  553. "groupName" => $group,
  554. "host" => $ip,
  555. "type" => "Host",
  556. "description" => $description,
  557. "enabled" => TRUE
  558. ))
  559. );
  560. $result = $this->sendRequest('IpAddressGroups.create', $params);
  561. return $result;
  562. }
  563. /**
  564. * Remove a IP address from a IP Group
  565. *
  566. * Local function used in example spam_blacklist
  567. *
  568. * @param string Group name
  569. * @param string IP address
  570. * @return array Result
  571. */
  572. public function removeHostFromIpGroup($group, $ip) {
  573. $list = $this->getIpGroupList(NULL);
  574. foreach ($list as $record) {
  575. if(($record['groupName'] != $group) || ($record['host'] != $ip)) continue;
  576. $hostId = $record['id'];
  577. }
  578. $params = array("groupIds" => array($hostId));
  579. $result = $this->sendRequest('IpAddressGroups.remove', $params);
  580. return $result;
  581. }
  582. /**
  583. * Random password generator
  584. *
  585. * Local function used in example createUser.
  586. *
  587. * @param integer Password lenght, default 10
  588. * @return string Random password
  589. */
  590. function genRandomPassword($length = 10) {
  591. $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
  592. $string = '';
  593. for ($p = 0; $p < $length; $p++) {
  594. $string .= $characters[mt_rand(0, (strlen($characters))-1)];
  595. }
  596. return $string;
  597. }
  598. }