kerioAddressAvailable.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. require_once(__DIR__ . '/api/Zm/Auth.php');
  20. require_once(__DIR__ . '/api/Zm/Account.php');
  21. // Mailhosting = 1, Nextcloudhosting = 3; Mailhosting + Nextcloud = 8
  22. define('zmAuthGids', [1, 3, 8]);
  23. use WHMCS\Database\Capsule;
  24. $whmcs = App::self();
  25. $accountName = $_GET['name'] . '@' . $_GET['domain'];
  26. $productID = $_GET['pid'];
  27. if(!filter_var($accountName, FILTER_VALIDATE_EMAIL)) {
  28. echo "invalid";
  29. exit;
  30. }
  31. // check reserved mail addresses
  32. $zmAuthPIDs = array();
  33. $zmAuthPIDsObj = Capsule::table('tblproducts')
  34. ->select('id')
  35. ->whereIn('gid', zmAuthGids)
  36. ->get()
  37. ->toArray();
  38. foreach ($zmAuthPIDsObj as $zmAuthPID) {
  39. array_push($zmAuthPIDs, $zmAuthPID->id);
  40. }
  41. $hostingIDs = array();
  42. $hostingIDsObj = Capsule::table('tblhosting')
  43. ->select('id')
  44. ->whereIn('packageid', $zmAuthPIDs)
  45. ->get()
  46. ->toArray();
  47. foreach ($hostingIDsObj as $hostingID) {
  48. array_push($hostingIDs, $hostingID->id);
  49. }
  50. $customfieldIDs = array();
  51. $customfieldIDsObj = Capsule::table('tblcustomfields')
  52. ->select('id')
  53. ->whereIn('relid', $zmAuthPIDs)
  54. ->where('fieldname', 'LIKE', 'username%')
  55. ->orWhere('fieldname', 'LIKE', 'maildomain%')
  56. ->get()
  57. ->toArray();
  58. foreach ($customfieldIDsObj as $customfieldID) {
  59. array_push($customfieldIDs, $customfieldID->id);
  60. }
  61. $reservedAddresses = array();
  62. $customfieldsvaluesObj = Capsule::table('tblcustomfieldsvalues')
  63. ->select('value')
  64. ->whereIn('relid', $hostingIDs)
  65. ->whereIn('fieldid', $customfieldIDs)
  66. ->get()
  67. ->toArray();
  68. $i = 0;
  69. while ($i < count($customfieldsvaluesObj)) {
  70. $mailname = $customfieldsvaluesObj[$i]->value;
  71. $i++;
  72. $maildomain = $customfieldsvaluesObj[$i]->value;
  73. $i++;
  74. $reservedAddress = $mailname . '@' . $maildomain;
  75. array_push($reservedAddresses, $reservedAddress);
  76. }
  77. if(in_array($accountName, $reservedAddresses)) {
  78. echo 'no';
  79. exit();
  80. }
  81. // check active Kerio Accounts
  82. $accessData = array('kerioServer' => '', 'adminUser' => '', 'adminPass' => '');
  83. $serverGroupIDObj = Capsule::table('tblproducts')
  84. ->select('servergroup')
  85. ->where('id', '=', $productID)
  86. ->get();
  87. $serverGroupID = $serverGroupIDObj[0]->servergroup;
  88. $serverIDObj = Capsule::table('tblservergroupsrel')
  89. ->select('serverid')
  90. ->where('groupid', '=', $serverGroupID)
  91. ->get();
  92. $serverID = $serverIDObj[0]->serverid;
  93. $server = Capsule::table('tblservers')
  94. ->select('hostname', 'username', 'password')
  95. ->where('id', '=', $serverID)
  96. ->where('active', '=', 1)
  97. ->get();
  98. $accessData['kerioServer'] = $server[0]->hostname;
  99. $accessData['adminUser'] = $server[0]->username;
  100. $adminPassDecrypt = localAPI('DecryptPassword', array('password2' => $server[0]->password));
  101. if ($adminPassDecrypt['result'] == 'success') {
  102. $accessData['adminPass'] = $adminPassDecrypt['password'];
  103. }
  104. $api = new Zm_Auth($accessData['kerioServer'], $accessData['adminUser'], $accessData['adminPass'], 'admin');
  105. $login = $api->login();
  106. if(is_a($login, 'Exception')) {
  107. logModuleCall(
  108. 'keriosingle',
  109. __FUNCTION__,
  110. $accessData,
  111. 'Error: cannot login to ' . $accessData['kerioServer'],
  112. $login->getMessage()
  113. );
  114. exit();
  115. } else {
  116. $apiAccountManager = new Zm_Account($api);
  117. if( $apiAccountManager->accountExists($accountName)) {
  118. echo 'no';
  119. } else {
  120. echo 'yes';
  121. }
  122. }