From ae362b482a882ef30a14e96bf5df3ff70a24ec28 Mon Sep 17 00:00:00 2001 From: Erik Fryklund Date: Thu, 11 Apr 2019 11:21:24 +0200 Subject: [PATCH] plugin hmail-alias integration --- plugins/hmail-aliases/LICENSE | 20 ++++++ plugins/hmail-aliases/README | 1 + plugins/hmail-aliases/VERSION | 1 + plugins/hmail-aliases/index.php | 113 ++++++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+) create mode 100644 plugins/hmail-aliases/LICENSE create mode 100644 plugins/hmail-aliases/README create mode 100644 plugins/hmail-aliases/VERSION create mode 100755 plugins/hmail-aliases/index.php diff --git a/plugins/hmail-aliases/LICENSE b/plugins/hmail-aliases/LICENSE new file mode 100644 index 0000000000..8e08efd68d --- /dev/null +++ b/plugins/hmail-aliases/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2019 Karobolas + +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 +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/plugins/hmail-aliases/README b/plugins/hmail-aliases/README new file mode 100644 index 0000000000..57efea0de8 --- /dev/null +++ b/plugins/hmail-aliases/README @@ -0,0 +1 @@ +Plugin which allows you to use hmail aliases diff --git a/plugins/hmail-aliases/VERSION b/plugins/hmail-aliases/VERSION new file mode 100644 index 0000000000..9f8e9b69a3 --- /dev/null +++ b/plugins/hmail-aliases/VERSION @@ -0,0 +1 @@ +1.0 \ No newline at end of file diff --git a/plugins/hmail-aliases/index.php b/plugins/hmail-aliases/index.php new file mode 100755 index 0000000000..59963c4f67 --- /dev/null +++ b/plugins/hmail-aliases/index.php @@ -0,0 +1,113 @@ +addHook('event.login-post-login-provide', 'loginPostLoginProvide'); + } + + public function Supported() + { + if (!class_exists('mysqli')) { + return 'The PHP exention mysqli must be installed to use this plugin'; + } + + return ''; + } + + /** + * @param \RainLoop\Model\Account $oAccount + * @throws \RainLoop\Exceptions\ClientException + */ + 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', '')); + + 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'; + } + + $hmailconn = mysqli_connect($dbservername, $dbusername, $dbpassword, $dbname, $dbport); + + // 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()."'"); + + 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 array + */ + 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)') + ); + } +}