zimbraAddressAvailable.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Helper script to check the availibility of a Zimbra mailbox useable with ajax requests
  4. *
  5. * @see https://www.zimbra.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 = Capsule::table('tblproducts')
  33. ->select('id')
  34. ->where('gid', zmAuthGids)
  35. ->get();
  36. logModuleCall(
  37. 'zimbrasingle',
  38. __FUNCTION__,
  39. $zmAuthPIDs,
  40. 'debug',
  41. $accountName
  42. );
  43. // check active Zimbra Accounts
  44. $accessData = array('zimbraServer' => '', 'adminUser' => '', 'adminPass' => '');
  45. $serverGroupIDObj = Capsule::table('tblproducts')
  46. ->select('servergroup')
  47. ->where('id', '=', $productID)
  48. ->get();
  49. $serverGroupID = $serverGroupIDObj[0]->servergroup;
  50. $serverIDObj = Capsule::table('tblservergroupsrel')
  51. ->select('serverid')
  52. ->where('groupid', '=', $serverGroupID)
  53. ->get();
  54. $serverID = $serverIDObj[0]->serverid;
  55. $server = Capsule::table('tblservers')
  56. ->select('hostname', 'username', 'password')
  57. ->where('id', '=', $serverID)
  58. ->where('active', '=', 1)
  59. ->get();
  60. $accessData['zimbraServer'] = $server[0]->hostname;
  61. $accessData['adminUser'] = $server[0]->username;
  62. $adminPassDecrypt = localAPI('DecryptPassword', array('password2' => $server[0]->password));
  63. if ($adminPassDecrypt['result'] == 'success') {
  64. $accessData['adminPass'] = $adminPassDecrypt['password'];
  65. }
  66. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], 'admin');
  67. $login = $api->login();
  68. if(is_a($login, 'Exception')) {
  69. logModuleCall(
  70. 'zimbrasingle',
  71. __FUNCTION__,
  72. $accessData,
  73. 'Error: cannot login to ' . $accessData['zimbraServer'],
  74. $login->getMessage()
  75. );
  76. exit();
  77. } else {
  78. $apiAccountManager = new Zm_Account($api);
  79. if( $apiAccountManager->accountExists($accountName)) {
  80. echo 'no';
  81. } else {
  82. echo 'yes';
  83. }
  84. }