zimbraAddressAvailable.php 2.6 KB

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