zimbraAddressAvailable.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 = array();
  33. $zmAuthPIDsObj = Capsule::table('tblproducts')
  34. ->select('id')
  35. ->where('gid', 'IN', [1,3,8])
  36. ->get()
  37. ->toArray();
  38. foreach ($zmAuthPIDsObj as $zmAuthPID) {
  39. array_push($zmAuthPIDs, $zmAuthPID->id);
  40. }
  41. $hostingIDs = array();
  42. $hostingIDsObj = Capsule::table('tblhosting')
  43. ->select('id')
  44. ->where('packageid', $zmAuthPIDs)
  45. ->get()
  46. ->toArray();
  47. foreach ($hostingIDsObj as $hostingID) {
  48. array_push($hostingIDs, $hostingID->id);
  49. }
  50. $customfieldIDs = array();
  51. $customfieldIDsObj = Capsule::table('tblcustomfields')
  52. ->select('id')
  53. ->where('relid', $zmAuthPIDs)
  54. ->where('fieldname', 'LIKE', 'username%')
  55. ->orWhere('fieldname', 'LIKE', 'maildomain%')
  56. ->get()
  57. ->toArray();
  58. foreach ($customfieldIDsObj as $customfieldID) {
  59. array_push($customfieldIDs, $customfieldID->id);
  60. }
  61. $customfieldsvalues = array();
  62. $customfieldsvaluesObj = Capsule::table('tblcustomfieldsvalues')
  63. ->select('value')
  64. ->where('fieldid', $customfieldIDs)
  65. ->get()
  66. ->toArray();
  67. logModuleCall(
  68. 'zimbrasingle',
  69. __FUNCTION__,
  70. $zmAuthPIDsObj,
  71. 'debug',
  72. $hostingIDsObj
  73. );
  74. // check active Zimbra Accounts
  75. $accessData = array('zimbraServer' => '', 'adminUser' => '', 'adminPass' => '');
  76. $serverGroupIDObj = Capsule::table('tblproducts')
  77. ->select('servergroup')
  78. ->where('id', '=', $productID)
  79. ->get();
  80. $serverGroupID = $serverGroupIDObj[0]->servergroup;
  81. $serverIDObj = Capsule::table('tblservergroupsrel')
  82. ->select('serverid')
  83. ->where('groupid', '=', $serverGroupID)
  84. ->get();
  85. $serverID = $serverIDObj[0]->serverid;
  86. $server = Capsule::table('tblservers')
  87. ->select('hostname', 'username', 'password')
  88. ->where('id', '=', $serverID)
  89. ->where('active', '=', 1)
  90. ->get();
  91. $accessData['zimbraServer'] = $server[0]->hostname;
  92. $accessData['adminUser'] = $server[0]->username;
  93. $adminPassDecrypt = localAPI('DecryptPassword', array('password2' => $server[0]->password));
  94. if ($adminPassDecrypt['result'] == 'success') {
  95. $accessData['adminPass'] = $adminPassDecrypt['password'];
  96. }
  97. $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], 'admin');
  98. $login = $api->login();
  99. if(is_a($login, 'Exception')) {
  100. logModuleCall(
  101. 'zimbrasingle',
  102. __FUNCTION__,
  103. $accessData,
  104. 'Error: cannot login to ' . $accessData['zimbraServer'],
  105. $login->getMessage()
  106. );
  107. exit();
  108. } else {
  109. $apiAccountManager = new Zm_Account($api);
  110. if( $apiAccountManager->accountExists($accountName)) {
  111. echo 'no';
  112. } else {
  113. echo 'yes';
  114. }
  115. }