| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625 |
- <?php
- /**
- * WHMCS Zimbra Provisioning Module
- *
- * Provisioning for private user accounts on the Zimbra Server
- *
- * @see https://www.zimbra.com
- * @copyright Copyright (c) Thurdata GmbH 2020
- * @license GPL
- */
- if (!defined("WHMCS")) {
- die("This file cannot be accessed directly");
- }
- use WHMCS\Database\Capsule;
- require_once("api/Zm/Auth.php");
- require_once("api/Zm/Account.php");
- require_once("api/Zm/Domain.php");
- require_once("api/Zm/Server.php");
- function zimbraSingleGetAccess()
- {
- $accessData = array('zimbraServer' => '', 'adminUser' => '', 'adminPass' => '');
- $whmcs = App::self();
- $serverGroupID = $whmcs->get_req_var('servergroup');
- $action = $whmcs->get_req_var('action');
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $action,
- "Debug" ,
- $whmcs
- );
- if(($action == 'module-settings') || ($action == 'ConfigOptions') || ($action == 'save')) {
- $productID = $whmcs->get_req_var('id');
- $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;
- } else {
- $id = $whmcs->get_req_var('id');
- $serverIDObj = Capsule::table('tblhosting')
- ->select('server')
- ->where('id', '=', $id)
- ->get();
- $serverID = $serverIDObj[0]->server;
- }
- $server = Capsule::table('tblservers')
- ->select('ipaddress', 'username', 'password')
- ->where('id', '=', $serverID)
- ->where('active', '=', 1)
- ->get();
- $accessData['zimbraServer'] = $server[0]->ipaddress;
- $accessData['adminUser'] = $server[0]->username;
- $adminPassCrypt = $server[0]->password;
- $adminPassDecrypt = localAPI('DecryptPassword', array('password2' => $adminPassCrypt));
- if ($adminPassDecrypt['result'] == 'success') {
- $accessData['adminPass'] = $adminPassDecrypt['password'];
- }
- return $accessData;
- }
- /**
- * Checks if a given email address in the given domain already exists
- *
- * @param $emailNameOnly The name before the @-sign only
- * @param $domainName The domain to search for existance of the email account
- * @return true if such an account was found or false if not
- */
- function zimbraSingleDoesEMailExist($emailNameOnly, $domainName)
- {
- $account_name = $emailNameOnly . "@" . $domainName;
- $accessData = zimbraSingleGetAccess();
- $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
- $login = $api->login();
- if(is_a($login, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : cannot login to " . $accessData['zimbraServer'],
- "$login->getMessage()"
- );
- exit();
- } else {
- $apiAccountManager = new Zm_Account($api);
- if( $apiAccountManager->accountExists($account_name)) {
- return true;
- } else {
- return false;
- }
- }
- }
- function zimbraSingleCreateCustomFields($packageconfigoption)
- {
- $whmcs = App::self();
- $productID = $whmcs->get_req_var('id');
- Capsule::table('tblcustomfields')
- ->where('relid', '=', $productID)
- ->delete();
- Capsule::table('tblcustomfields')
- ->insert(
- array(
- 'type' => 'product',
- 'relid' => $productID,
- 'fieldname' => 'givenname | Vorname',
- 'fieldtype' => 'text',
- 'required' => 'on',
- 'showorder' => 'on',
- 'sortorder' => '0'
- )
- );
- Capsule::table('tblcustomfields')
- ->insert(
- array(
- 'type' => 'product',
- 'relid' => $productID,
- 'fieldname' => 'sn | Nachname',
- 'fieldtype' => 'text',
- 'required' => 'on',
- 'showorder' => 'on',
- 'sortorder' => '1'
- )
- );
- Capsule::table('tblcustomfields')
- ->insert(
- array(
- 'type' => 'product',
- 'relid' => $productID,
- 'fieldname' => 'username | E-Mail Name',
- 'fieldtype' => 'text',
- 'required' => 'on',
- 'showorder' => 'on',
- 'sortorder' => '2'
- )
- );
- Capsule::table('tblcustomfields')
- ->insert(
- array(
- 'type' => 'product',
- 'relid' => $productID,
- 'fieldname' => 'maildomain | Mail Domaine',
- 'fieldtype' => 'dropdown',
- 'fieldoptions' => implode(',', $packageconfigoption[2]),
- 'required' => 'on',
- 'showorder' => 'on',
- 'sortorder' => '3'
- )
- );
- Capsule::table('tblcustomfields')
- ->insert(
- array(
- 'type' => 'product',
- 'relid' => $productID,
- 'fieldname' => 'password | Password',
- 'fieldtype' => 'password',
- 'required' => 'on',
- 'showorder' => 'on',
- 'sortorder' => '4'
- )
- );
- Capsule::table('tblcustomfields')
- ->insert(
- array(
- 'type' => 'product',
- 'relid' => $productID,
- 'fieldname' => 'pwrepeat | Password wiederholen',
- 'fieldtype' => 'password',
- 'required' => 'on',
- 'showorder' => 'on',
- 'sortorder' => '5'
- )
- );
- Capsule::table('tblcustomfields')
- ->insert(
- array(
- 'type' => 'product',
- 'relid' => $productID,
- 'fieldname' => 'cos | Class of Service',
- 'fieldtype' => 'dropdown',
- 'fieldoptions' => $packageconfigoption[1],
- 'adminonly' => 'on',
- 'required' => 'on',
- 'sortorder' => '6'
- )
- );
- }
- function recursiveFindAll($haystack, $needle)
- {
- $values = array();
- $iterator = new RecursiveArrayIterator($haystack);
- $recursive = new RecursiveIteratorIterator(
- $iterator,
- RecursiveIteratorIterator::SELF_FIRST
- );
- foreach ($recursive as $key => $value) {
- if ($key === $needle) {
- array_push($values, $value);
- }
- }
- return $values;
- }
- function zimbraSingleCheckPassword($pwd)
- {
- $message = '';
- if (strlen($pwd) < 8) {
- $message .= "Das das Passwort ist zu kurz. Es werden mind. 8 Zeichen benötigt" . PHP_EOL;
- }
- if (!preg_match("#[0-9]+#", $pwd)) {
- $message .= "Das Passwort muss mindestens eine Zahl enthalten" . PHP_EOL;
- }
- if (!preg_match("#[A-Z]+#", $pwd)) {
- $message .= "Das Passwort muss mindestens einen Grossbuchstaben (A-Z) enthalten" . PHP_EOL;
- }
- if (!preg_match("#[a-z]+#", $pwd)) {
- $message .= "Das Passwort muss mindestens einen Kleinbuchstaben (a-z) enthalten" . PHP_EOL;
- }
- if (!preg_match("#[^\w]+#", $pwd)) {
- $message .= "Das Passwort muss mindestens ein Sonderzeichen (.,-:=) enthalten" . PHP_EOL;
- }
- return $message;
- }
- function bytesToHuman($bytes)
- {
- $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
- for ($i = 0; $bytes > 1024; $i++) $bytes /= 1024;
- return round($bytes, 2) . ' ' . $units[$i];
- }
- function zimbraSingle_MetaData()
- {
- return array(
- 'DisplayName' => 'Zimbra Single Mailbox Provisioning',
- 'APIVersion' => '1.2',
- 'DefaultNonSSLPort' => '7071',
- 'DefaultSSLPort' => '7071',
- 'RequiresServer' => true,
- 'ServiceSingleSignOnLabel' => 'Login to Zimbra',
- 'AdminSingleSignOnLabel' => 'Login to Zimbra Admin'
- );
- }
- function zimbraSingle_TestConnection($params)
- {
- $auth = new Zm_Auth($params['serverip'], $params['serverusername'], $params['serverpassword'], "admin");
- $login = $auth->login();
- if(is_a($login, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Connection test to " . $params['serverip'] . " failed: Cannot login",
- $login->getMessage()
- );
- return array(
- 'success' => false,
- 'error' => "Connection test to " . $params['serverip'] . " failed, the error was: " . $login->getMessage(),
- );
- } else {
- return array(
- 'success' => true,
- 'error' => '',
- );
- }
- }
- function zimbraSingle_ClientArea($params)
- {
- $accessData = zimbraSingleGetAccess();
- $clientInfo = array();
- $account_name = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
- $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
- $login = $api->login();
- if(is_a($login, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error: cannot login to " . $accessData['zimbraServer'],
- $login
- );
- return false;
- }
- $apiAccountManager = new Zm_Account($api);
- $quota = $apiAccountManager->getQuota($account_name);
- if(is_a($quota, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : could not find $account_name",
- $quota
- );
- return false;
- }
- $response = $apiAccountManager->getMailbox($account_name);
- if(is_a($response, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : could not fetch mailbox info for $account_name",
- $response
- );
- return false;
- }
- $mboxSize = $response['S'];
- $usagePercent = $mboxSize * 100 / $quota;
- $clientInfo['quota'] = bytesToHuman($quota);
- $clientInfo['size'] = bytesToHuman($mboxSize);
- $clientInfo['usage'] = round($usagePercent, 2);
- $response = $apiAccountManager->getAccountInfo($account_name);
- if(is_a($response, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : could not gather informations for $account_name",
- $response
- );
- return false;
- }
- $webmailUrl = recursiveFindAll( $response, 'PUBLICMAILURL');
- $clientInfo['webmailurl'] = $webmailUrl[0]['DATA'];
- return array(
- 'templatefile' => 'clientarea',
- 'vars' => $clientInfo,
- );
- }
- function zimbraSingle_ChangePassword($params)
- {
- $accessData = zimbraSingleGetAccess();
- if ($checkPW = zimbraSingleCheckPassword($params['password'])) {
- return $checkPW;
- }
- $account_name = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
- $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
- $login = $api->login();
- if(is_a($login, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : cannot login to " . $accessData['zimbraServer'],
- $login
- );
- return false;
- }
- $apiAccountManager = new Zm_Account($api);
- $response = $apiAccountManager->setAccountPassword($account_name, $params['password']);
- if(is_a($response, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : password for $account_name could not be set",
- $response
- );
- return false;
- }
- return 'success';
- }
- function zimbraSingle_CreateAccount($params)
- {
- $accessData = zimbraSingleGetAccess();
- $attrs = array();
- $attrs["gn"] = $params['customfields']["givenname"];
- $attrs["sn"] = $params['customfields']["sn"];
- $attrs["displayName"] = $attrs["gn"] . " " . $attrs["sn"];
- $passDecrypt = localAPI('DecryptPassword', array('password2' => $params['customfields']['password']));
- if ($passDecrypt['result'] == 'success') {
- $params['customfields']['password'] = $passDecrypt['password'];
- }
- $account_name = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
- $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
- $login = $api->login();
- if(is_a($login, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : cannot login to " . $accessData['zimbraServer'],
- ""
- );
- return false;
- }
- $apiAccountManager = new Zm_Account($api);
- $cosName = $params['customfields']['cos'];
- $cosID = $apiAccountManager->getCosId($cosName);
- if(is_a($cosID, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : serviceclass $cosName not available",
- $params['customfields']['cos']
- );
- return false;
- }
- $attrs['zimbraCOSId'] = $cosID;
- $id = $apiAccountManager->createAccount($account_name, $params['customfields']['password'], $attrs);
- if(is_a($id, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : account $account_name not created",
- $id
- );
- return false;
- }
- return 'success';
- }
- function zimbraSingle_SuspendAccount($params)
- {
- $accessData = zimbraSingleGetAccess();
- $account_name = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
- $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
- $login = $api->login();
- if(is_a($login, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : cannot login to " . $accessData['zimbraServer'],
- $login
- );
- return false;
- }
- $apiAccountManager = new Zm_Account($api);
- $response = $apiAccountManager->setAccountStatus($account_name, "locked");
- if(is_a($response, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : account $account_name could not locked",
- $response
- );
- return false;
- }
- return 'success';
- }
- function zimbraSingle_UnsuspendAccount($params)
- {
- $accessData = zimbraSingleGetAccess();
- $account_name = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
- $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
- $login = $api->login();
- if(is_a($login, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : cannot login to " . $accessData['zimbraServer'],
- $login
- );
- return false;
- }
- $apiAccountManager = new Zm_Account($api);
- $response = $apiAccountManager->setAccountStatus($account_name, "active");
- if(is_a($response, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : account $account_name could not unlocked",
- $response
- );
- return false;
- }
- return 'success';
- }
- function zimbraSingle_TerminateAccount($params)
- {
- $accessData = zimbraSingleGetAccess();
- $accountName = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
- $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
- $login = $api->login();
- if(is_a($login, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : cannot login to " . $accessData['zimbraServer'],
- $login
- );
- return false;
- }
- $apiAccountManager = new Zm_Account($api);
- $response = $apiAccountManager->getAccountStatus($accountName);
- if(is_a($response, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : account $accountName could not verified",
- $response
- );
- return false;
- }
- if ($response != 'locked') {
- return "Account $accountName active, suspend account first";
- }
- $response = $apiAccountManager->deleteAccount($accountName);
- if(is_a($response, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : account $accountName could not removed",
- $response
- );
- return false;
- }
- return 'success';
- }
- function zimbraSingle_ChangePackage($params)
- {
- $accessData = zimbraSingleGetAccess();
- $account_name = $params['customfields']['username'] . '@' . $params['customfields']['maildomain'];
- $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
- $login = $api->login();
- if(is_a($login, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : cannot login to " . $accessData['zimbraServer'],
- $login
- );
- return false;
- }
- $apiAccountManager = new Zm_Account($api);
- $response = $apiAccountManager->setAccountCos($account_name, $params['customfields']['cos']);
- if(is_a($response, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : class of service for $account_name could not be set",
- $response
- );
- return false;
- }
- return 'success';
- }
- function zimbraSingle_ConfigOptions($params)
- {
- if(isset($_POST['packageconfigoption'])) {
- zimbraSingleCreateCustomFields($_POST['packageconfigoption']);
- }
- $accessData = zimbraSingleGetAccess();
- $api = new Zm_Auth($accessData['zimbraServer'], $accessData['adminUser'], $accessData['adminPass'], "admin");
- $login = $api->login();
- if(is_a($login, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error: cannot login to " . $accessData['zimbraServer'],
- $login
- );
- return false;
- }
- $apiAccountManager = new Zm_Account($api);
- $response = $apiAccountManager->getAllCos();
- if(is_a($response, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error: could not fetch classes of service",
- $response
- );
- return false;
- }
- $cosNames = recursiveFindAll($response, 'NAME');
- $configOptions = array();
- $configOptions['cos'] = array(
- "FriendlyName" => "Class of Service",
- "Type" => "dropdown",
- "Options" => implode(',', $cosNames),
- "Description" => "Select COS",
- );
- $apiDomainManager = new Zm_Domain($api);
- $response = $apiDomainManager->getAllDomains();
- if(is_a($response, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error: could fetch available maildomains",
- $response
- );
- return false;
- }
- $domainNames = recursiveFindAll($response, 'NAME');
- $configOptions['maildomains'] = array(
- "FriendlyName" => "Mail Domain",
- "Type" => "dropdown",
- "Multiple" => true,
- "Options" => implode(',', $domainNames),
- "Description" => "select maildomains",
- );
- return $configOptions;
- }
|