KerioWhmcs.php 12 KB

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