KerioWhmcs.php 13 KB

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