From f15f25a6356d1b27f501ea26d184982f8463a8b9 Mon Sep 17 00:00:00 2001 From: RainLoop Team Date: Wed, 20 May 2015 16:09:50 +0400 Subject: [PATCH] vpopmail-change-password fixes --- .../ChangePasswordVpopmailDriver.php | 457 +++++++++--------- plugins/vpopmail-change-password/VERSION | 2 +- plugins/vpopmail-change-password/index.php | 142 +++--- 3 files changed, 300 insertions(+), 301 deletions(-) diff --git a/plugins/vpopmail-change-password/ChangePasswordVpopmailDriver.php b/plugins/vpopmail-change-password/ChangePasswordVpopmailDriver.php index aa7529bd3..7013d226e 100755 --- a/plugins/vpopmail-change-password/ChangePasswordVpopmailDriver.php +++ b/plugins/vpopmail-change-password/ChangePasswordVpopmailDriver.php @@ -1,229 +1,228 @@ -mHost = $mHost; - return $this; - } - - /** - * @param string $mUser - * - * @return \ChangePasswordVpopmailDriver - */ - public function SetmUser($mUser) - { - $this->mUser = $mUser; - return $this; - } - - /** - * @param string $mPass - * - * @return \ChangePasswordVpopmailDriver - */ - public function SetmPass($mPass) - { - $this->mPass = $mPass; - return $this; - } - - /** - * @param string $mDatabase - * - * @return \ChangePasswordVpopmailDriver - */ - public function SetmDatabase($mDatabase) - { - $this->mDatabase = $mDatabase; - return $this; - } - - /** - * @param string $mTable - * - * @return \ChangePasswordVpopmailDriver - */ - public function SetmTable($mTable) - { - $this->mTable = $mTable; - return $this; - } - - /** - * @param string $mColumn - * - * @return \ChangePasswordVpopmailDriver - */ - public function SetmColumn($mColumn) - { - $this->mColumn = $mColumn; - return $this; - } - - /** - * @param \MailSo\Log\Logger $oLogger - * - * @return \ChangePasswordVpopmailDriver - */ - public function SetLogger($oLogger) - { - if ($oLogger instanceof \MailSo\Log\Logger) - { - $this->oLogger = $oLogger; - } - - return $this; - } - - /** - * @param array $aDomains - * - * @return bool - */ - public function SetAllowedDomains($aDomains) - { - if (\is_array($aDomains) && 0 < \count($aDomains)) - { - $this->aDomains = $aDomains; - } - - return $this; - } - - /** - * @param \RainLoop\Account $oAccount - * - * @return bool - */ - public function PasswordChangePossibility($oAccount) - { - return $oAccount && $oAccount->Domain() && - \in_array(\strtolower($oAccount->Domain()->Name()), $this->aDomains); - } - - /** - * @param \RainLoop\Account $oAccount - * @param string $sPrevPassword - * @param string $sNewPassword - * - * @return bool - */ - public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword) - { - if ($this->oLogger) - { - $this->oLogger->Write('Try to change password for '.$oAccount->Email()); - } - - $bResult = false; - - $dsn = 'mysql:host='.$this->mHost.';dbname='.$this->mDatabase.';charset=utf8'; - $options = array( - PDO::ATTR_EMULATE_PREPARES => false, - PDO::ATTR_PERSISTENT => true, - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION - ); - - try - { - $conn = new PDO($dsn,$this->mUser,$this->mPass,$options); - - $select = $conn->prepare("SELECT $this->mColumn FROM $this->mTable WHERE pw_name=? AND pw_domain=? LIMIT 1"); - - $email_parts = explode("@", $oAccount->Email()); - - $select->execute(array( - $email_parts[0], - $email_parts[1] - )); - - $colCrypt = $select->fetchAll(PDO::FETCH_ASSOC); - $sCryptPass = $colCrypt[0][$this->mColumn]; - - if (0 < strlen($sCryptPass) && crypt($sPrevPassword, $sCryptPass) === $sCryptPass /*&& 7 < mb_strlen($sNewPassword) && 20 > mb_strlen($sNewPassword) && !preg_match('/[^A-Za-z0-9]+/', $sNewPassword)*/) - { - - $update = $conn->prepare('UPDATE '.$this->mTable.' SET '.$this->mColumn.'=ENCRYPT(?,concat("$1$",right(md5(rand()), 8 ),"$")), pw_clear_passwd=\'\' WHERE pw_name=? AND pw_domain=?'); - $update->execute(array( - $sNewPassword, - $email_parts[0], - $email_parts[1] - )); - - - - $bResult = true; - if ($this->oLogger) - { - $this->oLogger->Write('Success! Password changed.'); - } - } - else - { - $bResult = false; - if ($this->oLogger) - { - $this->oLogger->Write('Something went wrong. Either current password is incorrect, or new password does not match criteria.'); - } - } - - } - catch (\Exception $oException) - { - $bResult = false; - if ($this->oLogger) - { - $this->oLogger->WriteException($oException); - } - } - - return $bResult; - } -} +mHost = $mHost; + return $this; + } + + /** + * @param string $mUser + * + * @return \ChangePasswordVpopmailDriver + */ + public function SetmUser($mUser) + { + $this->mUser = $mUser; + return $this; + } + + /** + * @param string $mPass + * + * @return \ChangePasswordVpopmailDriver + */ + public function SetmPass($mPass) + { + $this->mPass = $mPass; + return $this; + } + + /** + * @param string $mDatabase + * + * @return \ChangePasswordVpopmailDriver + */ + public function SetmDatabase($mDatabase) + { + $this->mDatabase = $mDatabase; + return $this; + } + + /** + * @param string $mTable + * + * @return \ChangePasswordVpopmailDriver + */ + public function SetmTable($mTable) + { + $this->mTable = $mTable; + return $this; + } + + /** + * @param string $mColumn + * + * @return \ChangePasswordVpopmailDriver + */ + public function SetmColumn($mColumn) + { + $this->mColumn = $mColumn; + return $this; + } + + /** + * @param \MailSo\Log\Logger $oLogger + * + * @return \ChangePasswordVpopmailDriver + */ + public function SetLogger($oLogger) + { + if ($oLogger instanceof \MailSo\Log\Logger) + { + $this->oLogger = $oLogger; + } + + return $this; + } + + /** + * @param array $aDomains + * + * @return bool + */ + public function SetAllowedDomains($aDomains) + { + if (\is_array($aDomains) && 0 < \count($aDomains)) + { + $this->aDomains = $aDomains; + } + + return $this; + } + + /** + * @param \RainLoop\Account $oAccount + * + * @return bool + */ + public function PasswordChangePossibility($oAccount) + { + return 0 === \count($this->aDomains) || ($oAccount && \in_array(\strtolower( + \MailSo\Base\Utils::GetDomainFromEmail($oAccount->Email)), $this->aDomains)); + } + + /** + * @param \RainLoop\Account $oAccount + * @param string $sPrevPassword + * @param string $sNewPassword + * + * @return bool + */ + public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword) + { + if ($this->oLogger) + { + $this->oLogger->Write('Try to change password for '.$oAccount->Email()); + } + + if (empty($this->mHost) || empty($this->mDatabase) || empty($this->mColumn) || empty($this->mTable)) + { + return false; + } + + $bResult = false; + + $sDsn = 'mysql:host='.$this->mHost.';dbname='.$this->mDatabase.';charset=utf8'; + $aOptions = array( + PDO::ATTR_EMULATE_PREPARES => false, + PDO::ATTR_PERSISTENT => true, + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION + ); + + $sLoginPart = \MailSo\Base\Utils::GetAccountNameFromEmail($oAccount->Email()); + $sDomainPart = \MailSo\Base\Utils::GetDomainFromEmail($oAccount->Email()); + + try + { + $oConn = new PDO($sDsn, $this->mUser, $this->mPass, $aOptions); + + $oSelect = $oConn->prepare('SELECT '.$this->mColumn.' FROM '.$this->mTable.' WHERE pw_name=? AND pw_domain=? LIMIT 1'); + $oSelect->execute(array($sLoginPart, $sDomainPart)); + + $aColCrypt = $oSelect->fetchAll(PDO::FETCH_ASSOC); + + $sCryptPass = isset($aColCrypt[0][$this->mColumn]) ? $aColCrypt[0][$this->mColumn] : ''; + if (0 < \strlen($sCryptPass) && \crypt($sPrevPassword, $sCryptPass) === $sCryptPass) + { + $oUpdate = $oConn->prepare('UPDATE '.$this->mTable.' SET '.$this->mColumn.'=ENCRYPT(?,concat("$1$",right(md5(rand()), 8 ),"$")), pw_clear_passwd=\'\' WHERE pw_name=? AND pw_domain=?'); + $oUpdate->execute(array( + $sNewPassword, + $sLoginPart, + $sDomainPart + )); + + $bResult = true; + if ($this->oLogger) + { + $this->oLogger->Write('Success! Password changed.'); + } + } + else + { + $bResult = false; + if ($this->oLogger) + { + $this->oLogger->Write('Something went wrong. Either current password is incorrect, or new password does not match criteria.'); + } + } + } + catch (\Exception $oException) + { + $bResult = false; + if ($this->oLogger) + { + $this->oLogger->WriteException($oException); + } + } + + return $bResult; + } +} diff --git a/plugins/vpopmail-change-password/VERSION b/plugins/vpopmail-change-password/VERSION index d3827e75a..9459d4ba2 100755 --- a/plugins/vpopmail-change-password/VERSION +++ b/plugins/vpopmail-change-password/VERSION @@ -1 +1 @@ -1.0 +1.1 diff --git a/plugins/vpopmail-change-password/index.php b/plugins/vpopmail-change-password/index.php index 8f85a9fea..8eb5fd5fe 100755 --- a/plugins/vpopmail-change-password/index.php +++ b/plugins/vpopmail-change-password/index.php @@ -1,71 +1,71 @@ -addHook('main.fabrica', 'MainFabrica'); - } - - /** - * @param string $sName - * @param mixed $oProvider - */ - public function MainFabrica($sName, &$oProvider) - { - switch ($sName) - { - case 'change-password': - - include_once __DIR__.'/ChangePasswordVpopmailDriver.php'; - - $oProvider = new ChangePasswordVpopmailDriver(); - - $sDomains = \strtolower(\trim(\preg_replace('/[\s;,]+/', ' ', - $this->Config()->Get('plugin', 'domains', '')))); - - if (0 < \strlen($sDomains)) - { - $aDomains = \explode(' ', $sDomains); - $oProvider->SetAllowedDomains($aDomains); - } - - $oProvider - ->SetLogger($this->Manager()->Actions()->Logger()) - ->SetmHost($this->Config()->Get('plugin', 'mHost', '')) - ->SetmUser($this->Config()->Get('plugin', 'mUser', '')) - ->SetmPass($this->Config()->Get('plugin', 'mPass', '')) - ->SetmDatabase($this->Config()->Get('plugin', 'mDatabase', '')) - ->SetmTable($this->Config()->Get('plugin', 'mTable', '')) - ->SetmColumn($this->Config()->Get('plugin', 'mColumn', '')) - ; - - break; - } - } - - /** - * @return array - */ - public function configMapping() - { - return array( - \RainLoop\Plugins\Property::NewInstance('domains')->SetLabel('Allowed Domains') - ->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING_TEXT) - ->SetDescription('Allowed domains, space as delimiter') - ->SetDefaultValue('* gmail.com yahoo.com dusal.net'), - \RainLoop\Plugins\Property::NewInstance('mHost')->SetLabel('MySQL Host') - ->SetDefaultValue('localhost'), - \RainLoop\Plugins\Property::NewInstance('mUser')->SetLabel('MySQL User') - ->SetDefaultValue('vpopmail'), - \RainLoop\Plugins\Property::NewInstance('mPass')->SetLabel('MySQL Password') - ->SetType(\RainLoop\Enumerations\PluginPropertyType::PASSWORD), - \RainLoop\Plugins\Property::NewInstance('mDatabase')->SetLabel('MySQL Database') - ->SetDefaultValue('vpopmail'), - \RainLoop\Plugins\Property::NewInstance('mTable')->SetLabel('MySQL Table') - ->SetDefaultValue('vpopmail'), - \RainLoop\Plugins\Property::NewInstance('mColumn')->SetLabel('MySQL Column') - ->SetDefaultValue('pw_passwd') - ); - } -} +addHook('main.fabrica', 'MainFabrica'); + } + + /** + * @param string $sName + * @param mixed $oProvider + */ + public function MainFabrica($sName, &$oProvider) + { + switch ($sName) + { + case 'change-password': + + include_once __DIR__.'/ChangePasswordVpopmailDriver.php'; + + $oProvider = new ChangePasswordVpopmailDriver(); + + $sDomains = \strtolower(\trim(\preg_replace('/[\s;,]+/', ' ', + $this->Config()->Get('plugin', 'domains', '')))); + + if (0 < \strlen($sDomains)) + { + $aDomains = \explode(' ', $sDomains); + $oProvider->SetAllowedDomains($aDomains); + } + + $oProvider + ->SetLogger($this->Manager()->Actions()->Logger()) + ->SetmHost($this->Config()->Get('plugin', 'mHost', '')) + ->SetmUser($this->Config()->Get('plugin', 'mUser', '')) + ->SetmPass($this->Config()->Get('plugin', 'mPass', '')) + ->SetmDatabase($this->Config()->Get('plugin', 'mDatabase', '')) + ->SetmTable($this->Config()->Get('plugin', 'mTable', '')) + ->SetmColumn($this->Config()->Get('plugin', 'mColumn', '')) + ; + + break; + } + } + + /** + * @return array + */ + public function configMapping() + { + return array( + \RainLoop\Plugins\Property::NewInstance('domains')->SetLabel('Allowed Domains') + ->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING_TEXT) + ->SetDescription('Allowed domains, space as delimiter') + ->SetDefaultValue('gmail.com yahoo.com'), + \RainLoop\Plugins\Property::NewInstance('mHost')->SetLabel('MySQL Host') + ->SetDefaultValue('localhost'), + \RainLoop\Plugins\Property::NewInstance('mUser')->SetLabel('MySQL User') + ->SetDefaultValue('vpopmail'), + \RainLoop\Plugins\Property::NewInstance('mPass')->SetLabel('MySQL Password') + ->SetType(\RainLoop\Enumerations\PluginPropertyType::PASSWORD), + \RainLoop\Plugins\Property::NewInstance('mDatabase')->SetLabel('MySQL Database') + ->SetDefaultValue('vpopmail'), + \RainLoop\Plugins\Property::NewInstance('mTable')->SetLabel('MySQL Table') + ->SetDefaultValue('vpopmail'), + \RainLoop\Plugins\Property::NewInstance('mColumn')->SetLabel('MySQL Column') + ->SetDefaultValue('pw_passwd') + ); + } +}