KerioWhmcs.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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. 'orderBy' => array(array(
  246. 'columnName' => 'name',
  247. 'direction' => 'Asc'
  248. ))
  249. ),
  250. 'domainId' => $domainId
  251. );
  252. if ($conditions) {
  253. $params['query']['conditions'] = $conditions;
  254. }
  255. $result = $this->sendRequest($method, $params);
  256. return $result['list'];
  257. }
  258. /**
  259. * Create new mailinglist
  260. *
  261. * @param array Mailinglist
  262. * @return array Result of create action
  263. */
  264. public function createMailinglist($list) {
  265. $method = 'MailingLists.create';
  266. $params['mailingLists'] = array($list);
  267. $result = $this->sendRequest($method, $params);
  268. return $result['result'];
  269. }
  270. /**
  271. * Modify mailinglist
  272. *
  273. * @param string MailinglistID
  274. * @param array fields to set
  275. * @return array Result of delete action
  276. */
  277. public function modifyMailinglist($fields,$mlId) {
  278. $method = 'MailingLists.set';
  279. $params['mlIds'] = array($mlId);
  280. $params['pattern'] = $fields;
  281. $result = $this->sendRequest($method, $params);
  282. return $result;
  283. }
  284. /**
  285. * Delete mailinglist
  286. *
  287. * @param string MailinglistID
  288. * @return array Result of delete action
  289. */
  290. public function deleteMailinglist($mlId) {
  291. $method = 'MailingLists.remove';
  292. $params['mlIds'] = array($mlId);
  293. $result = $this->sendRequest($method, $params);
  294. return $result;
  295. }
  296. /**
  297. * Get list of mailing lists from a domain
  298. *
  299. * @param array List of fields to be obtained from engine
  300. * @param string Mailing list Id
  301. *
  302. * @return array List of mailing lists
  303. */
  304. public function getMlUserList($fields, $mlId) {
  305. $method = 'MailingLists.getMlUserList';
  306. $params = array(
  307. 'query' => array(
  308. 'fields' => $fields
  309. ),
  310. 'mlId' => $mlId
  311. );
  312. $result = $this->sendRequest($method, $params);
  313. return $result['list'];
  314. }
  315. /**
  316. * Add users to mailing list
  317. *
  318. * @param array List of fields of users to ba added
  319. * @param string Mailing list Id
  320. *
  321. * @return array List of mailing lists
  322. */
  323. public function addMlUserList($members, $mlId) {
  324. $method = 'MailingLists.addMlUserList';
  325. $params['members'] = $members;
  326. $params['mlId'] = $mlId;
  327. $result = $this->sendRequest($method, $params);
  328. return $result;
  329. }
  330. /**
  331. * Remove users from mailing list
  332. *
  333. * @param array List of fields of users to ba added
  334. * @param string Mailing list Id
  335. *
  336. * @return array List of mailing lists
  337. */
  338. public function delMlUserList($members, $mlId) {
  339. $method = 'MailingLists.removeMlUserList';
  340. $params['members'] = $members;
  341. $params['mlId'] = $mlId;
  342. $result = $this->sendRequest($method, $params);
  343. return $result;
  344. }
  345. /**
  346. * Get list of resources from a domain
  347. *
  348. * @param array List of fields to be obtained from engine
  349. * @param string Domain Id
  350. * @param array Additional condition for the request
  351. *
  352. * @return array List of mailing lists
  353. */
  354. public function getResources($fields, $domainId, $conditions = null) {
  355. $method = 'Resources.get';
  356. $params = array(
  357. 'query' => array(
  358. 'fields' => $fields,
  359. 'orderBy' => array(array(
  360. 'columnName' => 'name',
  361. 'direction' => 'Asc'
  362. ))
  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. * Create resource.
  374. *
  375. * @param array Resource attributes
  376. * @param string Domain ID
  377. * @return array Result
  378. */
  379. function createResouce($attr, $domain) {
  380. $params = array(
  381. 'resources' => array(array(
  382. 'name' => $attr['name'],
  383. 'description' => $attr['description'],
  384. 'type' => $attr['type'],
  385. 'isEnabled' => $attr['status'] === 'active' ? TRUE : FALSE,
  386. 'resourceUsers' => array(array('id' => $domain, 'type' => 'AuthDomainPrincipal')),
  387. 'manager' => array('id' => $attr['manager'], 'type' => 'UserPrincipal'),
  388. 'domainId' => $domain
  389. ))
  390. );
  391. $result = $this->sendRequest('Resources.create', $params);
  392. return $result;
  393. }
  394. /**
  395. * Update resource.
  396. *
  397. * @param array Resource attributes
  398. * @param string Resource ID
  399. * @return array Result
  400. */
  401. function updateResouce($attr, $resource) {
  402. $params = array(
  403. 'resourceIds' => array($resource),
  404. 'pattern' => $attr
  405. );
  406. $result = $this->sendRequest('Resources.set', $params);
  407. return $result;
  408. }
  409. /**
  410. * Remove resource.
  411. *
  412. * @param string Resource ID
  413. * @return array Result
  414. */
  415. function delResouce($resource) {
  416. $params = array(
  417. 'resourceIds' => array($resource)
  418. );
  419. $result = $this->sendRequest('Resources.remove', $params);
  420. return $result;
  421. }
  422. /**
  423. * Get list of aliases from a domain
  424. *
  425. * @param array List of fields to be obtained from engine
  426. * @param string Domain Id
  427. * @param array Additional condition for the request
  428. *
  429. * @return array List of aliases
  430. */
  431. public function getAliases($fields, $domainId, $conditions = null) {
  432. $method = 'Aliases.get';
  433. $params = array(
  434. 'query' => array(
  435. 'fields' => $fields,
  436. 'orderBy' => array(array(
  437. 'columnName' => 'name',
  438. 'direction' => 'Asc'
  439. )),
  440. 'combining' => 'Or'
  441. ),
  442. 'domainId' => $domainId
  443. );
  444. if ($conditions) {
  445. $params['query']['conditions'] = $conditions;
  446. }
  447. $result = $this->sendRequest($method, $params);
  448. return $result['list'];
  449. }
  450. /**
  451. * Get list of all services
  452. *
  453. * @param void
  454. * @return array List of services
  455. */
  456. function getServices() {
  457. $method = 'Services.get';
  458. $params = array();
  459. $result = $this->sendRequest($method, $params);
  460. return $result['services'];
  461. }
  462. /**
  463. * Get list of all services
  464. *
  465. * @param void
  466. * @return array List of services
  467. */
  468. function getServerStatistics() {
  469. $method = 'Statistics.get';
  470. $params = array();
  471. $result = $this->sendRequest($method, $params);
  472. return $result['statistics'];
  473. }
  474. /**
  475. * Get server info
  476. *
  477. * @param void
  478. * @return array List of services
  479. */
  480. function getServerInfo() {
  481. $method = 'Server.getProductInfo';
  482. $params = array();
  483. $result = $this->sendRequest($method, $params);
  484. return $result;
  485. }
  486. /**
  487. * Create alias.
  488. *
  489. * @param string Domain ID
  490. * @param string Alias
  491. * @param string Email
  492. * @param string Description, optional
  493. * @return array Result
  494. */
  495. function createAlias($domain, $alias, $email, $description = '') {
  496. $params = array(
  497. 'aliases' => array(array(
  498. 'name' => $alias,
  499. 'domainId' => $domain,
  500. 'deliverTo' => $email,
  501. 'description' => $description,
  502. 'deliverToSelect' => 'TypeEmailAddress'
  503. ))
  504. );
  505. $result = $this->sendRequest('Aliases.create', $params);
  506. return $result;
  507. }
  508. /**
  509. * Delete Alias.
  510. *
  511. * @param string aliasID
  512. * @return array Result
  513. */
  514. function deleteAlias($aliasID) {
  515. $params = array(
  516. 'aliasIds' => array($aliasID),
  517. );
  518. $result = $this->sendRequest('Aliases.remove', $params);
  519. return $result;
  520. }
  521. /**
  522. * Create user.
  523. *
  524. * @param string Domain ID
  525. * @param string Username
  526. * @param string Password
  527. * @return array Result
  528. */
  529. function createUser($domain, $username, $password, $active = TRUE) {
  530. $params = array(
  531. 'users' => array(array(
  532. 'loginName' => $username,
  533. 'password' => $password,
  534. 'domainId' => $domain,
  535. 'isEnabled' => $active
  536. ))
  537. );
  538. $result = $this->sendRequest('Users.create', $params);
  539. return $result;
  540. }
  541. /**
  542. * Modify user.
  543. *
  544. * @param string User ID
  545. * @param array Attributes
  546. * @return array Result
  547. */
  548. function modifyUser($userId, $attr) {
  549. $params = array(
  550. 'userIds' => [ $userId ],
  551. 'pattern' => $attr
  552. );
  553. $result = $this->sendRequest('Users.set', $params);
  554. return $result;
  555. }
  556. /**
  557. * Count user.
  558. *
  559. * @param string Domain ID
  560. * @return int Result
  561. */
  562. function countUser($domainId) {
  563. $params = array(
  564. 'domainId' => $domainId
  565. );
  566. $result = $this->sendRequest('Domains.getUserCountInfo', $params);
  567. return intval($result['countInfo']['currentUsers']);
  568. }
  569. /**
  570. * Delete user.
  571. *
  572. * @param string User ID
  573. * @return array Result
  574. */
  575. function deleteUser($userId) {
  576. $params = array(
  577. 'requests' => array(
  578. array(
  579. 'userId' => $userId,
  580. 'mode' => 'DSModeDeactivate',
  581. 'method' => 'UDeleteFolder',
  582. 'targetUserId' => '',
  583. 'removeReferences' => TRUE,
  584. ),
  585. ),
  586. );
  587. $result = $this->sendRequest('Users.remove', $params);
  588. return $result;
  589. }
  590. /**
  591. * Create domain.
  592. *
  593. * @param string Domain name
  594. * @return array Result
  595. */
  596. function createDomain($domain) {
  597. $params = array(
  598. 'domains' => array(array(
  599. 'name' => $domain
  600. ))
  601. );
  602. $result = $this->sendRequest('Domains.create', $params);
  603. return $result;
  604. }
  605. /**
  606. * Modify domain.
  607. *
  608. * @param string DomainID
  609. * @param array Attributes
  610. * @return array Result
  611. */
  612. function modifyDomain($domainID, $attr) {
  613. $params = array(
  614. 'domainIds' => array($domainID),
  615. 'pattern' => $attr,
  616. );
  617. $result = $this->sendRequest('Domains.set', $params);
  618. return $result;
  619. }
  620. /**
  621. * Delete domain.
  622. *
  623. * @param string DomainID
  624. * @return array Result
  625. */
  626. function deleteDomain($domainID) {
  627. $params = array(
  628. 'domainIds' => array($domainID),
  629. );
  630. $result = $this->sendRequest('Domains.remove', $params);
  631. return $result;
  632. }
  633. /**
  634. * Get list of IP addresses from a file.
  635. *
  636. * Local function used in example spam_blacklist
  637. *
  638. * @param string Filename
  639. * @return array List of IP addresses
  640. * @throws KerioApiException
  641. */
  642. public function getBlacklistRecords($file) {
  643. $blacklist = array();
  644. if(file_exists($file) && is_readable($file)) {
  645. $data = file_get_contents($file);
  646. foreach (preg_split("/\n/", $data) as $record) {
  647. if (empty($record)) continue;
  648. array_push($blacklist, $record);
  649. }
  650. }
  651. else {
  652. throw new KerioApiException(sprintf('Cannot open file %s', $file));
  653. }
  654. return $blacklist;
  655. }
  656. /**
  657. * Get list of IP addesses from a group
  658. *
  659. * Local function used in example spam_blacklist
  660. *
  661. * @param string Group name
  662. * @return array List of IP addresses
  663. */
  664. public function getIpGroupList($name) {
  665. $params = array(
  666. "query" => array(
  667. "conditions" => array(array(
  668. "fieldName" => "name",
  669. "comparator" => "Like",
  670. "value" => $name
  671. )),
  672. "orderBy" => array(array(
  673. "columnName" => "item",
  674. "direction" => "Asc"
  675. ))
  676. )
  677. );
  678. $result = $this->sendRequest('IpAddressGroups.get', $params);
  679. return $result['list'];
  680. }
  681. /**
  682. * Add a IP address to a IP Group
  683. *
  684. * Local function used in example spam_blacklist
  685. *
  686. * @param string Group name
  687. * @param string IP address
  688. * @param string Description, optional
  689. * @return array Result
  690. */
  691. public function addHostToIpGroup($group, $ip, $description = '') {
  692. if(empty($description)) {
  693. $description = sprintf('Automatically added on %s', date(DATE_RFC822));
  694. }
  695. $params = array(
  696. "groups" => array(array(
  697. "groupId" => "",
  698. "groupName" => $group,
  699. "host" => $ip,
  700. "type" => "Host",
  701. "description" => $description,
  702. "enabled" => TRUE
  703. ))
  704. );
  705. $result = $this->sendRequest('IpAddressGroups.create', $params);
  706. return $result;
  707. }
  708. /**
  709. * Remove a IP address from a IP Group
  710. *
  711. * Local function used in example spam_blacklist
  712. *
  713. * @param string Group name
  714. * @param string IP address
  715. * @return array Result
  716. */
  717. public function removeHostFromIpGroup($group, $ip) {
  718. $list = $this->getIpGroupList(NULL);
  719. foreach ($list as $record) {
  720. if(($record['groupName'] != $group) || ($record['host'] != $ip)) continue;
  721. $hostId = $record['id'];
  722. }
  723. $params = array("groupIds" => array($hostId));
  724. $result = $this->sendRequest('IpAddressGroups.remove', $params);
  725. return $result;
  726. }
  727. /**
  728. * Random password generator
  729. *
  730. * Local function used in example createUser.
  731. *
  732. * @param integer Password lenght, default 10
  733. * @return string Random password
  734. */
  735. function genRandomPassword($length = 10) {
  736. $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
  737. $string = '';
  738. for ($p = 0; $p < $length; $p++) {
  739. $string .= $characters[mt_rand(0, (strlen($characters))-1)];
  740. }
  741. return $string;
  742. }
  743. }