andre 5 лет назад
Родитель
Сommit
efd85d755d
2 измененных файлов с 546 добавлено и 580 удалено
  1. 6 549
      zimbraSingle.inc
  2. 540 31
      zimbraSingle.php

+ 6 - 549
zimbraSingle.inc

@@ -1,587 +1,44 @@
 <?php
 <?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()
-{
-    $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 zimbraSingleCreateAccount($userData)
 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)
 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)
 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)
 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($params) {
 function zimbraSingleChangePassword($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'],
-            ""
-        );
-        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",
-            ""
-        );
-        return false;
-    }
-    return 'success';
+
 }
 }
 
 
 function zimbraSingleChangePackage($userData) {
 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)
 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);
-    $quota = $apiAccountManager->getQuota($account_name);
-    if(is_a($quota, "Exception")) {
-        logModuleCall(
-            'zimbrasingle',
-            __FUNCTION__,
-            $params,
-            "Error : could not find $account_name",
-            ""
-        );
-        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",
-            ""
-        );
-        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",
-            ""
-        );
-        return false;
-    }
-    $webmailUrl = recursiveFindAll( $response, 'PUBLICMAILURL');
-    $clientInfo['webmailurl'] = $webmailUrl[0]['DATA'];
-    return $clientInfo;
-}
-
-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' => '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 zimbraSingleConfigOptions($params) {
 
 
-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];
-}

+ 540 - 31
zimbraSingle.php

@@ -14,7 +14,242 @@ if (!defined("WHMCS")) {
     die("This file cannot be accessed directly");
     die("This file cannot be accessed directly");
 }
 }
 use WHMCS\Database\Capsule;
 use WHMCS\Database\Capsule;
-require_once dirname(__FILE__) . '/zimbraSingle.inc';
+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)
 function zimbraSingle_TestConnection($params)
 {
 {
@@ -42,65 +277,290 @@ function zimbraSingle_TestConnection($params)
 
 
 function zimbraSingle_ClientArea($params)
 function zimbraSingle_ClientArea($params)
 {
 {
-    $response = zimbraSingleClientArea($params['customfields']);
+    $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(
     return array(
         'templatefile' => 'clientarea',
         'templatefile' => 'clientarea',
-        'vars' => $response,
+        'vars' => $clientInfo,
     );
     );
 }
 }
 
 
 function zimbraSingle_ChangePassword($params)
 function zimbraSingle_ChangePassword($params)
 {
 {
-    $response = zimbraSingleChangePassword($params);
-    if($response == 'success') {
-        return 'success';
+    $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 $response;
+    return 'success';
 }
 }
 
 
 function zimbraSingle_CreateAccount($params)
 function zimbraSingle_CreateAccount($params)
 {
 {
-    $response = zimbraSingleCreateAccount($params['customfields']);
-    if($response) {
-        return 'success';
+    $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 'Error creating account';
+    return 'success';
 }
 }
 
 
 function zimbraSingle_SuspendAccount($params)
 function zimbraSingle_SuspendAccount($params)
 {
 {
-    $response = zimbraSingleSuspendAccount($params['customfields']);
-    if($response) {
-        return 'success';
+    $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;
     }
     }
-    return 'Error suspending account';
+    $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)
 function zimbraSingle_UnsuspendAccount($params)
 {
 {
-    $response = zimbraSingleUnsuspendAccount($params['customfields']);
-    if($response) {
-        return 'success';
+    $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 'Error unsuspending account';
+    return 'success';
 }
 }
 
 
 function zimbraSingle_TerminateAccount($params)
 function zimbraSingle_TerminateAccount($params)
 {
 {
-    $response = zimbraSingleDeleteAccount($params['customfields']);
-    if($response == 'success') {
-        return 'success';
+    $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 $response;
+    return 'success';
 }
 }
 
 
 function zimbraSingle_ChangePackage($params)
 function zimbraSingle_ChangePackage($params)
 {
 {
-    $response = zimbraSingleChangePackage($params['customfields']);
-    if($response) {
-        return 'success';
+    $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;
     }
     }
-    return 'Error changing package';
+    $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)
 function zimbraSingle_ConfigOptions($params)
@@ -108,11 +568,60 @@ function zimbraSingle_ConfigOptions($params)
     if(isset($_POST['packageconfigoption'])) {
     if(isset($_POST['packageconfigoption'])) {
         zimbraSingleCreateCustomFields($_POST['packageconfigoption']);
         zimbraSingleCreateCustomFields($_POST['packageconfigoption']);
     }
     }
-    $response = zimbraSingleConfigOptions($params);
-    if($response) {
-        return $response;
+    $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;
     }
     }
-    return 'Error setting config options';
+    $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;
 }
 }
 
 
 function zimbraSingle_genUsername($name)
 function zimbraSingle_genUsername($name)