zimbraAddressAvailable.php 2.5 KB

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