| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574 |
- <?php
- 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 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 zimbraSingleGetAccess()
- {
- global $packageid;
- $accessData = array('zimbraServer' => '', 'adminUser' => '', 'adminPass' => '');
- $whmcs = App::self();
- $productID = $whmcs->get_req_var('id');
- $serverGroupID = $whmcs->get_req_var('servergroup');
- if($productID) {
- $serverIDObj = Capsule::table('tblhosting')
- ->select('server')
- ->where('id', '=', $productID)
- ->get();
- $serverID = $serverIDObj[0]->server;
- } elseif($serverGroupID) {
- $serverIDObj = Capsule::table('tblservergroupsrel')
- ->select('serverid')
- ->where('groupid', '=', $serverGroupID)
- ->get();
- $serverID = $serverIDObj[0]->serverid;
- } elseif($packageid) {
- $serverIDObj = Capsule::table('tblhosting')
- ->select('server')
- ->where('packageid', '=', $packageid)
- ->get();
- $serverID = $serverIDObj[0]->server;
- } else {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error: could not get accessdata 3",
- ""
- );
- return false;
- }
- $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 zimbraSingleCreateAccount($userData)
- {
- $accessData = zimbraSingleGetAccess();
- $attrs = array();
- $attrs["gn"] = $userData["givenname"];
- $attrs["sn"] = $userData["sn"];
- $attrs["displayName"] = $attrs["gn"] . " " . $attrs["sn"];
- $passDecrypt = localAPI('DecryptPassword', array('password2' => $userData['password']));
- if ($passDecrypt['result'] == 'success') {
- $userData['password'] = $passDecrypt['password'];
- }
- $account_name = $userData['username'] . '@' . $userData['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 = $userData['cos'];
- $cosID = $apiAccountManager->getCosId($cosName);
- if(is_a($cosID, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : serviceclass $cosName not available",
- $userData
- );
- return false;
- }
- $attrs['zimbraCOSId'] = $cosID;
- $id = $apiAccountManager->createAccount($account_name, $userData['password'], $attrs);
- if(is_a($id, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : account $account_name not created",
- $id
- );
- return false;
- }
- return $id;
- }
- function zimbraSingleSuspendAccount($userData)
- {
- $accessData = zimbraSingleGetAccess();
- $account_name = $userData['username'] . '@' . $userData['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;
- } else {
- $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",
- ""
- );
- return false;
- } else {
- return $response;
- }
- }
- }
- function zimbraSingleUnsuspendAccount($userData)
- {
- $accessData = zimbraSingleGetAccess();
- $account_name = $userData['username'] . '@' . $userData['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;
- } else {
- $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",
- ""
- );
- return false;
- } else {
- return $response;
- }
- }
- }
- function zimbraSingleDeleteAccount($userData)
- {
- $accessData = zimbraSingleGetAccess();
- $accountName = $userData['username'] . '@' . $userData['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);
- $response = $apiAccountManager->getAccountStatus($accountName);
- if(is_a($response, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : account $accountName could not verified",
- ""
- );
- 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",
- ""
- );
- return false;
- }
- return 'success';
- }
- function zimbraSingleChangePassword($userData) {
- $accessData = zimbraSingleGetAccess();
- $passDecrypt = localAPI('DecryptPassword', array('password2' => $userData['password']));
- if ($passDecrypt['result'] == 'success') {
- $userData['password'] = $passDecrypt['password'];
- }
- if ($checkPW = zimbraSingleCheckPassword($userData['password'])) {
- return $checkPW;
- }
- $account_name = $userData['username'] . '@' . $userData['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);
- $response = $apiAccountManager->setAccountPassword($account_name, $userData['password']);
- if(is_a($response, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : password for $account_name could not be set",
- ""
- );
- return false;
- }
- return 'success';
- }
- function zimbraSingleChangePackage($userData) {
- $accessData = zimbraSingleGetAccess();
- $account_name = $userData['username'] . '@' . $userData['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);
- $response = $apiAccountManager->setAccountCos($account_name, $userData['cos']);
- if(is_a($response, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : class of service for $account_name could not be set",
- ""
- );
- return false;
- }
- return $response;
- }
- function zimbraSingleClientArea($userData)
- {
- $accessData = zimbraSingleGetAccess();
- $clientInfo = array();
- $account_name = $userData['username'] . '@' . $userData['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);
- $response = $apiAccountManager->getAccount($account_name);
- if(is_a($response, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : could not find $account_name",
- ""
- );
- return false;
- }
- // $clientInfo['quota'] = bytesToHuman($bytes);
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "debug: getQuota",
- $response
- );
-
- $response = $apiAccountManager->getAccountInfo($account_name);
- if(is_a($response, "Exception")) {
- logModuleCall(
- 'zimbrasingle',
- __FUNCTION__,
- $params,
- "Error : could not gather informations for $account_name",
- ""
- );
- return false;
- }
- $webMailURL = recursiveFindAll( $response, 'PUBLICMAILURL');
- return $webMailURL;
- }
- function zimbraSingleConfigOptions($params) {
- $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'],
- ""
- );
- 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",
- ""
- );
- 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",
- ""
- );
- return false;
- }
- $domainNames = recursiveFindAll($response, 'NAME');
- $configOptions['maildomains'] = array(
- "FriendlyName" => "Mail Domain",
- "Type" => "dropdown",
- "Multiple" => true,
- "Options" => implode(',', $domainNames),
- "Description" => "select maildomains",
- );
- return $configOptions;
- }
- 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' => 'cos | Class of Service',
- 'fieldtype' => 'dropdown',
- 'fieldoptions' => $packageconfigoption[1],
- 'adminonly' => 'on',
- 'required' => 'on',
- 'sortorder' => '5'
- )
- );
- }
- 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) < 9) {
- $message .= "Das das Passwort ist zu kurz. Es werden mind. 9 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];
- }
|