KerioWhmcs.php 13 KB

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