KerioWhmcs.php 15 KB

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