KerioWhmcs.php 16 KB

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