KerioWhmcs.php 13 KB

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