diff --git a/plugins/hmail-aliases/LICENSE b/plugins/hmail-aliases/LICENSE index 8e08efd68d..69d4b4db02 100644 --- a/plugins/hmail-aliases/LICENSE +++ b/plugins/hmail-aliases/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2019 Karobolas +Copyright (c) 2019 Karobolas, 2023 Oxymoron290 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/plugins/hmail-aliases/index.php b/plugins/hmail-aliases/index.php index 59963c4f67..aa26843b59 100755 --- a/plugins/hmail-aliases/index.php +++ b/plugins/hmail-aliases/index.php @@ -9,11 +9,12 @@ public function Init() public function Supported() { - if (!class_exists('mysqli')) { - return 'The PHP exention mysqli must be installed to use this plugin'; - } + if (!class_exists('COM')) + { + return 'The PHP extension COM must be installed to use this plugin'; + } - return ''; + return ''; } /** @@ -22,70 +23,84 @@ public function Supported() */ public function loginPostLoginProvide(\RainLoop\Model\Account &$oAccount) { - $dbservername = \trim($this->Config()->Get('plugin', 'hamilserver', '')); - $dbusername = \trim($this->Config()->Get('plugin', 'hmaildbusername', '')); - $dbpassword = \trim($this->Config()->Get('plugin', 'hmaildbpassword', '')); - $dbname = \trim($this->Config()->Get('plugin', 'hmaildbtable', '')); - $dbport = \trim($this->Config()->Get('plugin', 'hmaildbport', '')); - $datadir = \trim($this->Config()->Get('plugin', 'rainloopDatalocation', '')); + // Get Logger + $oLogger = $this->Manager()->Actions()->Logger(); + // Load values from configuration + $sLogin = (string) $this->Config()->Get('plugin', 'login', ''); + $sPassword = (string) $this->Config()->Get('plugin', 'password', ''); + // prime result value + $bResult = false; - if ($datadir != ""){ - $userpath = $datadir.'data/_data_/_default_/storage/cfg/'.substr($oAccount->Email(), 0, 2).'/'.$oAccount->Email().'/identities'; - } else { - $userpath = APP_INDEX_ROOT_PATH.'_data_/_default_/storage/cfg/'.substr($oAccount->Email(), 0, 2).'/'.$oAccount->Email().'/identities'; - } + try + { + // Create a connection with the hMailServer + $oHmailApp = new COM("hMailServer.Application"); + $oHmailApp->Connect(); - $hmailconn = mysqli_connect($dbservername, $dbusername, $dbpassword, $dbname, $dbport); + //$oLogger->Write("Connected"); + if ($oHmailApp->Authenticate($sLogin, $sPassword)) + { + //$oLogger->Write("Authenticated"); + $sEmail = $oAccount->Email(); + //$oLogger->Write("Using ". $sEmail); + $sDomain = \MailSo\Base\Utils::GetDomainFromEmail($sEmail); + //$oLogger->Write("Searching for domain ".$sDomain); - // Check connection - if (!$hmailconn) { - echo "Hmail-aliases: connection to db failed"; - return; - } - //Get aliases - $result = $hmailconn->query("SELECT * FROM " . $dbname . ".hm_aliases WHERE aliasvalue='".$oAccount->Email()."'"); + $oHmailDomain = $oHmailApp->Domains->ItemByName($sDomain); + if ($oHmailDomain) + { + $oHmailAccount = $oHmailDomain->Accounts->ItemByAddress($sEmail); + if ($oHmailAccount) + { + // Get account details for alias + $firstName = $oHmailAccount->PersonFirstName; + $lastName = $oHmailAccount->PersonLastName; + if ($firstName == "" && $lastName == "") { + $name = ""; + } else { + $name = $firstName." ".$lastName; + } + + // ========vvv========= Update Rainloop Identity ========vvv========= + $identities = $this->Manager()->Actions()->GetIdentities($oAccount); + if(empty($identities)){ + // I am assuming [0] is the "default" identity... + $identity = \RainLoop\Model\Identity::NewInstanceFromAccount($oAccount); + array_push($identities, $identity); + } + + $identity = $identities[0]; + $identity->FromJSON(array('Email' => $sEmail, 'Name' => $name)); + // TODO Account::DoIdentityUpdate is possible if I knew how to use actoinParams + $result = $this->Manager()->Actions()->SetIdentities($oAccount, $identities); + $oLogger->Write('HMAILSERVER Identity Update Successful'); + // ========^^^========= Update Rainloop Idnetity ========^^^========= + $bResult = true; + } + else + { + $oLogger->Write('HMAILSERVER: Unknown account ('.$sEmail.')', \MailSo\Log\Enumerations\Type::ERROR); + } + } + else + { + $oLogger->Write('HMAILSERVER: Unknown domain ('.$sDomain.')', \MailSo\Log\Enumerations\Type::ERROR); + } + } + else + { + $oLogger->Write('HMAILSERVER: Auth error', \MailSo\Log\Enumerations\Type::ERROR); + } + } + catch (\Exception $oException) + { + if ($oLogger) + { + $oLogger->WriteException($oException); + } + } - if ($result->num_rows > 0) { - $newidentitiesobj = array(); - - //Get user account - $result2 = $hmailconn->query("SELECT * FROM " . $dbname . ".hm_accounts WHERE accountaddress='".$oAccount->Email()."'"); - $result2 = $result2->fetch_assoc(); - $firstname = $result2['accountpersonfirstname']; - $lastname = $result2['accountpersonlastname']; - - if ($firstname == "" && $lastname == "") { - $name = ""; - } else { - $name = $firstname." ".$lastname; - } - - //Get existing settings. If not a alias created by hmail. Transfer settings to the new array. - $identities = file_get_contents($userpath, true); - if ($identities != "") { - $identities = json_decode($identities, true); - error_log(print_r($identities, true)); - foreach ($identities as $row) { - if (strpos($row['Id'], 'HMAIL') === false) { - array_push($newidentitiesobj, $row); - } - } - } - - // output data of each row - while ($row = $result->fetch_assoc()) { - $obj = array(); - $obj['Id'] = "HMAIL".base64_encode($row["aliasname"]); - $obj['Email'] = $row["aliasname"]; - $obj['Name'] = $name; - $obj['ReplyTo'] = ""; - $obj['Bcc'] = ""; - $obj['Signature'] = ""; - $obj['SignatureInsertBefore'] = false; - array_push($newidentitiesobj, $obj); - } - file_put_contents($userpath, json_encode($newidentitiesobj)); - } + return $bResult; } /** @@ -94,20 +109,11 @@ public function loginPostLoginProvide(\RainLoop\Model\Account &$oAccount) public function configMapping() { return array( - \RainLoop\Plugins\Property::NewInstance('hamilserver')->SetLabel('db-host') - ->SetDefaultValue('localhost'), - \RainLoop\Plugins\Property::NewInstance('hmaildbusername')->SetLabel('db-username') - ->SetDefaultValue(''), - \RainLoop\Plugins\Property::NewInstance('hmaildbpassword')->SetLabel('db-password') - ->SetType(\RainLoop\Enumerations\PluginPropertyType::PASSWORD), - \RainLoop\Plugins\Property::NewInstance('hmaildbtable')->SetLabel('db-table') - ->SetDefaultValue('mailserver'), - \RainLoop\Plugins\Property::NewInstance('hmaildbport')->SetLabel('db-port') - ->SetDefaultValue('3306') - ->SetDescription('Connect to mysql hmailserver. The user must have rights to read "hm_aliases" table.'), - \RainLoop\Plugins\Property::NewInstance('rainloopDatalocation')->SetLabel('Data folder location') - ->SetDefaultValue('') - ->SetDescription('Incase of custom data directory location. Eg. nextcloud/owncloud version (Leave blank for default)') + \RainLoop\Plugins\Property::NewInstance('login')->SetLabel('HmailServer Admin Login') + ->SetDefaultValue('Administrator'), + \RainLoop\Plugins\Property::NewInstance('password')->SetLabel('HmailServer Admin Password') + ->SetType(\RainLoop\Enumerations\PluginPropertyType::PASSWORD) + ->SetDefaultValue('') ); } }