zimbraAddressAvailable.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. ->fetch();
  36. $hostingIDs = Capsule::table('tblhosting')
  37. ->select('id')
  38. ->where('packageid', $zmAuthPIDs)
  39. ->get()
  40. ->toArray();
  41. logModuleCall(
  42. 'zimbrasingle',
  43. __FUNCTION__,
  44. $zmAuthPIDs,
  45. 'debug',
  46. $hostingIDs
  47. );
  48. // check active Zimbra Accounts
  49. $accessData = array('zimbraServer' => '', 'adminUser' => '', 'adminPass' => '');
  50. $serverGroupIDObj = Capsule::table('tblproducts')
  51. ->select('servergroup')
  52. ->where('id', '=', $productID)
  53. ->get();
  54. $serverGroupID = $serverGroupIDObj[0]->servergroup;
  55. $serverIDObj = Capsule::table('tblservergroupsrel')
  56. ->select('serverid')
  57. ->where('groupid', '=', $serverGroupID)
  58. ->get();
  59. $serverID = $serverIDObj[0]->serverid;
  60. $server = Capsule::table('tblservers')
  61. ->select('hostname', 'username', 'password')
  62. ->where('id', '=', $serverID)
  63. ->where('active', '=', 1)
  64. ->get();
  65. $accessData['zimbraServer'] = $server[0]->hostname;
  66. $accessData['adminUser'] = $server[0]->username;
  67. $adminPassDecrypt = localAPI('DecryptPassword', array('password2' => $server[0]->password));
  68. if ($adminPassDecrypt['result'] == 'success') {
  69. $accessData['adminPass'] = $adminPassDecrypt['password'];
  70. }
  71. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], 'admin');
  72. $login = $api->login();
  73. if(is_a($login, 'Exception')) {
  74. logModuleCall(
  75. 'zimbrasingle',
  76. __FUNCTION__,
  77. $accessData,
  78. 'Error: cannot login to ' . $accessData['zimbraServer'],
  79. $login->getMessage()
  80. );
  81. exit();
  82. } else {
  83. $apiAccountManager = new Zm_Account($api);
  84. if( $apiAccountManager->accountExists($accountName)) {
  85. echo 'no';
  86. } else {
  87. echo 'yes';
  88. }
  89. }