zimbraAddressAvailable.php 2.1 KB

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