KerioWhmcs.php 14 KB

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