KerioWhmcs.php 13 KB

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