kerioDomainAvailable.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. ->where('disabled', '=', 0)
  50. ->get();
  51. $accessData['kerioServer'] = $server[0]->hostname;
  52. $accessData['adminUser'] = $server[0]->username;
  53. $adminPassDecrypt = localAPI('DecryptPassword', array('password2' => $server[0]->password));
  54. if ($adminPassDecrypt['result'] == 'success') {
  55. $accessData['adminPass'] = $adminPassDecrypt['password'];
  56. }
  57. $api = new KerioWhmcs('whmcsKerioSingle', 'Thurdata', '1.0');
  58. try {
  59. $api->login($accessData['kerioServer'], $accessData['adminUser'], $accessData['adminPass']);
  60. } catch (KerioApiException $error) {
  61. logModuleCall(
  62. 'keriosingle',
  63. __FUNCTION__,
  64. $accessData,
  65. 'Error: cannot login to ' . $accessData['kerioServer'],
  66. $error->getMessage()
  67. );
  68. exit();
  69. }
  70. $respond = $api->getDomains(['name','aliasList']);
  71. $domains= [];
  72. foreach($respond as $domain) {
  73. array_push($domains,$domain['name']);
  74. foreach($domain['aliasList'] as $alias) {
  75. array_push($domains,$alias);
  76. }
  77. }
  78. if(in_array($domainName,$domains)) {
  79. echo 'no';
  80. } else {
  81. echo 'yes';
  82. }
  83. $api->logout();