zimbraAddressAvailable.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. use WHMCS\Database\Capsule;
  22. $whmcs = App::self();
  23. $accountName = $_GET['name'] . '@' . $_GET['domain'];
  24. $productID = $_GET['pid'];
  25. $accessData = array('zimbraServer' => '', 'adminUser' => '', 'adminPass' => '');
  26. $serverGroupIDObj = Capsule::table('tblproducts')
  27. ->select('servergroup')
  28. ->where('id', '=', $productID)
  29. ->get();
  30. $serverGroupID = $serverGroupIDObj[0]->servergroup;
  31. $serverIDObj = Capsule::table('tblservergroupsrel')
  32. ->select('serverid')
  33. ->where('groupid', '=', $serverGroupID)
  34. ->get();
  35. $serverID = $serverIDObj[0]->serverid;
  36. $server = Capsule::table('tblservers')
  37. ->select('hostname', 'username', 'password')
  38. ->where('id', '=', $serverID)
  39. ->where('active', '=', 1)
  40. ->get();
  41. $accessData['zimbraServer'] = $server[0]->hostname;
  42. $accessData['adminUser'] = $server[0]->username;
  43. $adminPassDecrypt = localAPI('DecryptPassword', array('password2' => $server[0]->password));
  44. if ($adminPassDecrypt['result'] == 'success') {
  45. $accessData['adminPass'] = $adminPassDecrypt['password'];
  46. }
  47. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], 'admin');
  48. $login = $api->login();
  49. if(is_a($login, 'Exception')) {
  50. logModuleCall(
  51. 'zimbrasingle',
  52. __FUNCTION__,
  53. $accessData,
  54. 'Error: cannot login to ' . $accessData['zimbraServer'],
  55. $login->getMessage()
  56. );
  57. exit();
  58. } else {
  59. $apiAccountManager = new Zm_Account($api);
  60. if( $apiAccountManager->accountExists($accountName)) {
  61. echo 'no';
  62. } else {
  63. echo 'yes';
  64. }
  65. }