| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- /**
- * Helper script to check the availibility of a Kerio mailbox useable with ajax requests
- *
- * @see https://www.kerio.com
- * @copyright Copyright (c) Thurdata GmbH 2020
- * @license GPL
- *
- */
- $pos = strpos($_SERVER['HTTP_REFERER'],getenv('HTTP_HOST'));
- if($pos===false) {
- die('Restricted access');
- }
- /**
- * Requires the whmcs init
- * Requires this PHP api to make soap calls and parse responses
- */
- require_once(__DIR__ . '/../../../init.php');
- require_once(__DIR__ . '/api/Zm/Auth.php');
- require_once(__DIR__ . '/api/Zm/Account.php');
- // Mailhosting = 1, Nextcloudhosting = 3; Mailhosting + Nextcloud = 8
- define('zmAuthGids', [1, 3, 8]);
- use WHMCS\Database\Capsule;
- $whmcs = App::self();
- $accountName = $_GET['name'] . '@' . $_GET['domain'];
- $productID = $_GET['pid'];
- if(!filter_var($accountName, FILTER_VALIDATE_EMAIL)) {
- echo "invalid";
- exit;
- }
- // check reserved mail addresses
- $zmAuthPIDs = array();
- $zmAuthPIDsObj = Capsule::table('tblproducts')
- ->select('id')
- ->whereIn('gid', zmAuthGids)
- ->get()
- ->toArray();
- foreach ($zmAuthPIDsObj as $zmAuthPID) {
- array_push($zmAuthPIDs, $zmAuthPID->id);
- }
- $hostingIDs = array();
- $hostingIDsObj = Capsule::table('tblhosting')
- ->select('id')
- ->whereIn('packageid', $zmAuthPIDs)
- ->get()
- ->toArray();
- foreach ($hostingIDsObj as $hostingID) {
- array_push($hostingIDs, $hostingID->id);
- }
- $customfieldIDs = array();
- $customfieldIDsObj = Capsule::table('tblcustomfields')
- ->select('id')
- ->whereIn('relid', $zmAuthPIDs)
- ->where('fieldname', 'LIKE', 'username%')
- ->orWhere('fieldname', 'LIKE', 'maildomain%')
- ->get()
- ->toArray();
- foreach ($customfieldIDsObj as $customfieldID) {
- array_push($customfieldIDs, $customfieldID->id);
- }
- $reservedAddresses = array();
- $customfieldsvaluesObj = Capsule::table('tblcustomfieldsvalues')
- ->select('value')
- ->whereIn('relid', $hostingIDs)
- ->whereIn('fieldid', $customfieldIDs)
- ->get()
- ->toArray();
- $i = 0;
- while ($i < count($customfieldsvaluesObj)) {
- $mailname = $customfieldsvaluesObj[$i]->value;
- $i++;
- $maildomain = $customfieldsvaluesObj[$i]->value;
- $i++;
- $reservedAddress = $mailname . '@' . $maildomain;
- array_push($reservedAddresses, $reservedAddress);
- }
- if(in_array($accountName, $reservedAddresses)) {
- echo 'no';
- exit();
- }
- // check active Kerio Accounts
- $accessData = array('kerioServer' => '', 'adminUser' => '', 'adminPass' => '');
- $serverGroupIDObj = Capsule::table('tblproducts')
- ->select('servergroup')
- ->where('id', '=', $productID)
- ->get();
- $serverGroupID = $serverGroupIDObj[0]->servergroup;
- $serverIDObj = Capsule::table('tblservergroupsrel')
- ->select('serverid')
- ->where('groupid', '=', $serverGroupID)
- ->get();
- $serverID = $serverIDObj[0]->serverid;
- $server = Capsule::table('tblservers')
- ->select('hostname', 'username', 'password')
- ->where('id', '=', $serverID)
- ->where('active', '=', 1)
- ->get();
- $accessData['kerioServer'] = $server[0]->hostname;
- $accessData['adminUser'] = $server[0]->username;
- $adminPassDecrypt = localAPI('DecryptPassword', array('password2' => $server[0]->password));
- if ($adminPassDecrypt['result'] == 'success') {
- $accessData['adminPass'] = $adminPassDecrypt['password'];
- }
- $api = new Zm_Auth($accessData['kerioServer'], $accessData['adminUser'], $accessData['adminPass'], 'admin');
- $login = $api->login();
- if(is_a($login, 'Exception')) {
- logModuleCall(
- 'keriosingle',
- __FUNCTION__,
- $accessData,
- 'Error: cannot login to ' . $accessData['kerioServer'],
- $login->getMessage()
- );
- exit();
- } else {
- $apiAccountManager = new Zm_Account($api);
- if( $apiAccountManager->accountExists($accountName)) {
- echo 'no';
- } else {
- echo 'yes';
- }
- }
|