From 7f45312de6b5f7c9852d223ae094b1c6d44d3272 Mon Sep 17 00:00:00 2001 From: Michel Wohlert Date: Fri, 1 Dec 2017 22:05:35 +0100 Subject: [PATCH 1/7] Added option to select PostgreSQL --- .../ChangePasswordPostfixAdminDriver.php | 29 ++++++++++++++++++- .../postfixadmin-change-password/index.php | 27 ++++++++++------- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php b/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php index 7c72af236b..095a31e729 100755 --- a/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php +++ b/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php @@ -2,6 +2,11 @@ class ChangePasswordPostfixAdminDriver implements \RainLoop\Providers\ChangePassword\ChangePasswordInterface { + /** + * @var string + */ + private $sEngine = 'MySQL'; + /** * @var string */ @@ -57,6 +62,17 @@ class ChangePasswordPostfixAdminDriver implements \RainLoop\Providers\ChangePass */ private $oLogger = null; + /** + * @param string $sHost + * + * @return \ChangePasswordPostfixAdminDriver + */ + public function SetEngine($sEngine) + { + $this->sEngine = $sEngine; + return $this; + } + /** * @param string $sHost * @@ -215,7 +231,18 @@ public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNe { try { - $sDsn = 'mysql:host='.$this->sHost.';port='.$this->iPort.';dbname='.$this->sDatabase; + switch($sEngine){ + case 'MySQL': + $sDsn = 'mysql:host='.$this->sHost.';port='.$this->iPort.';dbname='.$this->sDatabase; + break; + case 'PostgreSQL': + $sDsn = 'pgsql:host='.$this->sHost.';port='.$this->iPort.';dbname='.$this->sDatabase; + break; + default: + $sDsn = 'mysql:host='.$this->sHost.';port='.$this->iPort.';dbname='.$this->sDatabase; + break; + } + $oPdo = new \PDO($sDsn, $this->sUser, $this->sPassword); $oPdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); diff --git a/plugins/postfixadmin-change-password/index.php b/plugins/postfixadmin-change-password/index.php index 3e1223d4b7..ef2f5eee18 100755 --- a/plugins/postfixadmin-change-password/index.php +++ b/plugins/postfixadmin-change-password/index.php @@ -14,13 +14,13 @@ public function Supported() { if (!extension_loaded('pdo') || !class_exists('PDO')) { - return 'The PHP extension PDO (mysql) must be installed to use this plugin'; + return 'The PHP extension PDO must be installed to use this plugin'; } $aDrivers = \PDO::getAvailableDrivers(); - if (!is_array($aDrivers) || !in_array('mysql', $aDrivers)) + if (!is_array($aDrivers) || (!in_array('mysql', $aDrivers) && !in_array('pgsql', $aDrivers))) { - return 'The PHP extension PDO (mysql) must be installed to use this plugin'; + return 'The PHP extension PDO (mysql or pgsql) must be installed to use this plugin'; } return ''; @@ -41,6 +41,7 @@ public function MainFabrica($sName, &$oProvider) $oProvider = new ChangePasswordPostfixAdminDriver(); $oProvider + ->SetEngine($this->Config()->Get('plugin', 'engine','')) ->SetHost($this->Config()->Get('plugin', 'host', '')) ->SetPort((int) $this->Config()->Get('plugin', 'port', 3306)) ->SetDatabase($this->Config()->Get('plugin', 'database', '')) @@ -64,22 +65,26 @@ public function MainFabrica($sName, &$oProvider) public function configMapping() { return array( - \RainLoop\Plugins\Property::NewInstance('host')->SetLabel('MySQL Host') + \RainLoop\Plugins\Property::NewInstance('engine')->SetLabel('Engine') + ->SetType(\RainLoop\Enumerations\PluginPropertyType::SELECTION) + ->SetDefaultValue(array('MySQL', 'PostgreSQL')) + ->SetDescription('Database Engine'), + \RainLoop\Plugins\Property::NewInstance('host')->SetLabel('Host') ->SetDefaultValue('127.0.0.1'), - \RainLoop\Plugins\Property::NewInstance('port')->SetLabel('MySQL Port') + \RainLoop\Plugins\Property::NewInstance('port')->SetLabel('Port') ->SetType(\RainLoop\Enumerations\PluginPropertyType::INT) ->SetDefaultValue(3306), - \RainLoop\Plugins\Property::NewInstance('database')->SetLabel('MySQL Database') + \RainLoop\Plugins\Property::NewInstance('database')->SetLabel('Database') ->SetDefaultValue('postfixadmin'), - \RainLoop\Plugins\Property::NewInstance('table')->SetLabel('MySQL table') + \RainLoop\Plugins\Property::NewInstance('table')->SetLabel('table') ->SetDefaultValue('mailbox'), - \RainLoop\Plugins\Property::NewInstance('usercol')->SetLabel('MySQL username column') + \RainLoop\Plugins\Property::NewInstance('usercol')->SetLabel('username column') ->SetDefaultValue('username'), - \RainLoop\Plugins\Property::NewInstance('passcol')->SetLabel('MySQL password column') + \RainLoop\Plugins\Property::NewInstance('passcol')->SetLabel('password column') ->SetDefaultValue('password'), - \RainLoop\Plugins\Property::NewInstance('user')->SetLabel('MySQL User') + \RainLoop\Plugins\Property::NewInstance('user')->SetLabel('User') ->SetDefaultValue('postfixadmin'), - \RainLoop\Plugins\Property::NewInstance('password')->SetLabel('MySQL Password') + \RainLoop\Plugins\Property::NewInstance('password')->SetLabel('Password') ->SetType(\RainLoop\Enumerations\PluginPropertyType::PASSWORD) ->SetDefaultValue(''), \RainLoop\Plugins\Property::NewInstance('encrypt')->SetLabel('Encrypt') From df07e1b96c05846cfcb03932c5513b2e2fd10a6f Mon Sep 17 00:00:00 2001 From: Michel Wohlert Date: Fri, 1 Dec 2017 22:07:52 +0100 Subject: [PATCH 2/7] fix indentation --- .../ChangePasswordPostfixAdminDriver.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php b/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php index 095a31e729..4fc8bbd3cd 100755 --- a/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php +++ b/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php @@ -232,15 +232,15 @@ public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNe try { switch($sEngine){ - case 'MySQL': - $sDsn = 'mysql:host='.$this->sHost.';port='.$this->iPort.';dbname='.$this->sDatabase; - break; - case 'PostgreSQL': + case 'MySQL': + $sDsn = 'mysql:host='.$this->sHost.';port='.$this->iPort.';dbname='.$this->sDatabase; + break; + case 'PostgreSQL': $sDsn = 'pgsql:host='.$this->sHost.';port='.$this->iPort.';dbname='.$this->sDatabase; break; - default: - $sDsn = 'mysql:host='.$this->sHost.';port='.$this->iPort.';dbname='.$this->sDatabase; - break; + default: + $sDsn = 'mysql:host='.$this->sHost.';port='.$this->iPort.';dbname='.$this->sDatabase; + break; } From 0d96cd66929313ad25af654aabddd30989e5de2f Mon Sep 17 00:00:00 2001 From: Michel Wohlert Date: Fri, 1 Dec 2017 22:37:20 +0100 Subject: [PATCH 3/7] fix scope variable --- .../ChangePasswordPostfixAdminDriver.php | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php b/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php index 4fc8bbd3cd..8ebcfe07b3 100755 --- a/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php +++ b/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php @@ -231,6 +231,7 @@ public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNe { try { + $sDsn = ''; switch($sEngine){ case 'MySQL': $sDsn = 'mysql:host='.$this->sHost.';port='.$this->iPort.';dbname='.$this->sDatabase; From 09cdc285ab180aedae74146df2d1f600a2937298 Mon Sep 17 00:00:00 2001 From: Michel Wohlert Date: Fri, 1 Dec 2017 23:16:07 +0100 Subject: [PATCH 4/7] Fix bad variable --- .../ChangePasswordPostfixAdminDriver.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php b/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php index 8ebcfe07b3..772db8ca84 100755 --- a/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php +++ b/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php @@ -63,7 +63,7 @@ class ChangePasswordPostfixAdminDriver implements \RainLoop\Providers\ChangePass private $oLogger = null; /** - * @param string $sHost + * @param string $sEngine * * @return \ChangePasswordPostfixAdminDriver */ @@ -232,7 +232,7 @@ public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNe try { $sDsn = ''; - switch($sEngine){ + switch($this->sEngine){ case 'MySQL': $sDsn = 'mysql:host='.$this->sHost.';port='.$this->iPort.';dbname='.$this->sDatabase; break; From 9336b5bcb7cf2266cd0c95840454938bf71cdeb4 Mon Sep 17 00:00:00 2001 From: Michel Wohlert Date: Fri, 1 Dec 2017 23:28:32 +0100 Subject: [PATCH 5/7] Added Exception when trying to use mysql-crypt in PostgreSQL --- .../ChangePasswordPostfixAdminDriver.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php b/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php index 772db8ca84..761b9092e8 100755 --- a/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php +++ b/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php @@ -317,14 +317,18 @@ private function cryptPassword($sPassword, $oPdo) break; case 'mysql_encrypt': - $oStmt = $oPdo->prepare('SELECT ENCRYPT(?) AS encpass'); - if ($oStmt->execute(array($sPassword))) - { - $aFetchResult = $oStmt->fetchAll(\PDO::FETCH_ASSOC); - if (\is_array($aFetchResult) && isset($aFetchResult[0]['encpass'])) + if($this->sEngine == 'MySQL'){ + $oStmt = $oPdo->prepare('SELECT ENCRYPT(?) AS encpass'); + if ($oStmt->execute(array($sPassword))) { - $sResult = $aFetchResult[0]['encpass']; + $aFetchResult = $oStmt->fetchAll(\PDO::FETCH_ASSOC); + if (\is_array($aFetchResult) && isset($aFetchResult[0]['encpass'])) + { + $sResult = $aFetchResult[0]['encpass']; + } } + }else{ + throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::CouldNotSaveNewPassword); } break; } From 0eeb31b7b7edd4a32a6f09e4ace2cc46c5bb17d9 Mon Sep 17 00:00:00 2001 From: Michel Wohlert Date: Fri, 1 Dec 2017 23:29:15 +0100 Subject: [PATCH 6/7] Bump Version to 1.3 --- plugins/postfixadmin-change-password/VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/postfixadmin-change-password/VERSION b/plugins/postfixadmin-change-password/VERSION index ea710abb95..7e32cd5698 100644 --- a/plugins/postfixadmin-change-password/VERSION +++ b/plugins/postfixadmin-change-password/VERSION @@ -1 +1 @@ -1.2 \ No newline at end of file +1.3 From 7186833a268fc0193bc0ca36e4aa9c4d0e2244f0 Mon Sep 17 00:00:00 2001 From: Michel Wohlert Date: Fri, 1 Dec 2017 23:33:46 +0100 Subject: [PATCH 7/7] Fix indentation --- .../ChangePasswordPostfixAdminDriver.php | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php b/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php index 761b9092e8..fd2284b8b1 100755 --- a/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php +++ b/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php @@ -233,15 +233,15 @@ public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNe { $sDsn = ''; switch($this->sEngine){ - case 'MySQL': - $sDsn = 'mysql:host='.$this->sHost.';port='.$this->iPort.';dbname='.$this->sDatabase; - break; - case 'PostgreSQL': - $sDsn = 'pgsql:host='.$this->sHost.';port='.$this->iPort.';dbname='.$this->sDatabase; + case 'MySQL': + $sDsn = 'mysql:host='.$this->sHost.';port='.$this->iPort.';dbname='.$this->sDatabase; break; - default: - $sDsn = 'mysql:host='.$this->sHost.';port='.$this->iPort.';dbname='.$this->sDatabase; - break; + case 'PostgreSQL': + $sDsn = 'pgsql:host='.$this->sHost.';port='.$this->iPort.';dbname='.$this->sDatabase; + break; + default: + $sDsn = 'mysql:host='.$this->sHost.';port='.$this->iPort.';dbname='.$this->sDatabase; + break; } @@ -318,19 +318,19 @@ private function cryptPassword($sPassword, $oPdo) case 'mysql_encrypt': if($this->sEngine == 'MySQL'){ - $oStmt = $oPdo->prepare('SELECT ENCRYPT(?) AS encpass'); - if ($oStmt->execute(array($sPassword))) + $oStmt = $oPdo->prepare('SELECT ENCRYPT(?) AS encpass'); + if ($oStmt->execute(array($sPassword))) + { + $aFetchResult = $oStmt->fetchAll(\PDO::FETCH_ASSOC); + if (\is_array($aFetchResult) && isset($aFetchResult[0]['encpass'])) { - $aFetchResult = $oStmt->fetchAll(\PDO::FETCH_ASSOC); - if (\is_array($aFetchResult) && isset($aFetchResult[0]['encpass'])) - { - $sResult = $aFetchResult[0]['encpass']; - } + $sResult = $aFetchResult[0]['encpass']; } - }else{ - throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::CouldNotSaveNewPassword); } - break; + }else{ + throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::CouldNotSaveNewPassword); + } + break; } return $sResult;