kerioDomainAvailable.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Helper script to check the availibility of a Kerio mailbox useable with ajax requests
  4. *
  5. * @see https://www.kerio.com
  6. * @copyright Copyright (c) Thurdata GmbH 2020
  7. * @license GPL
  8. *
  9. */
  10. $pos = strpos($_SERVER['HTTP_REFERER'],getenv('HTTP_HOST'));
  11. if($pos===false) {
  12. die('Restricted access');
  13. }
  14. /**
  15. * Requires the whmcs init
  16. * Requires this PHP api to make soap calls and parse responses
  17. */
  18. require_once(__DIR__ . '/../../../init.php');
  19. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  20. use WHMCS\Database\Capsule;
  21. /**
  22. * Requires this PHP api to make soap calls and parse responses
  23. * This is an extend version of:
  24. * @see https://github.com/alloylab/kerio-admin-api-soap-php
  25. */
  26. require_once(__DIR__ . '/api/KerioWhmcs.php');
  27. $whmcs = App::self();
  28. $domainName = $_GET['domain'];
  29. $productID = $_GET['pid'];
  30. if(!filter_var($domainName, FILTER_VALIDATE_DOMAIN)) {
  31. echo "invalid";
  32. exit;
  33. }
  34. // check active Kerio Domains
  35. $accessData = array('kerioServer' => '', 'adminUser' => '', 'adminPass' => '');
  36. $serverGroupIDObj = Capsule::table('tblproducts')
  37. ->select('servergroup')
  38. ->where('id', '=', $productID)
  39. ->get();
  40. $serverGroupID = $serverGroupIDObj[0]->servergroup;
  41. $serverIDObj = Capsule::table('tblservergroupsrel')
  42. ->select('serverid')
  43. ->where('groupid', '=', $serverGroupID)
  44. ->get();
  45. $serverID = $serverIDObj[0]->serverid;
  46. $server = Capsule::table('tblservers')
  47. ->select('hostname', 'username', 'password')
  48. ->where('id', '=', $serverID)
  49. ->get();
  50. $accessData['kerioServer'] = $server[0]->hostname;
  51. $accessData['adminUser'] = $server[0]->username;
  52. $adminPassDecrypt = localAPI('DecryptPassword', array('password2' => $server[0]->password));
  53. if ($adminPassDecrypt['result'] == 'success') {
  54. $accessData['adminPass'] = $adminPassDecrypt['password'];
  55. }
  56. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  57. try {
  58. $api->login($accessData['kerioServer'], $accessData['adminUser'], $accessData['adminPass']);
  59. } catch (KerioApiException $error) {
  60. logModuleCall(
  61. 'kerioEmail',
  62. __FUNCTION__,
  63. $accessData,
  64. 'Error: cannot login to ' . $accessData['kerioServer'],
  65. $error->getMessage()
  66. );
  67. exit();
  68. }
  69. $respond = $api->getDomains(['name','aliasList']);
  70. $domains= [];
  71. foreach($respond as $domain) {
  72. array_push($domains,$domain['name']);
  73. foreach($domain['aliasList'] as $alias) {
  74. array_push($domains,$alias);
  75. }
  76. }
  77. if(in_array($domainName,$domains)) {
  78. echo 'no';
  79. } else {
  80. echo 'yes';
  81. }
  82. $api->logout();