diff --git a/plugins/change-passwords-customsql/README.md b/plugins/change-passwords-customsql/README.md new file mode 100644 index 000000000..3f3d265bb --- /dev/null +++ b/plugins/change-passwords-customsql/README.md @@ -0,0 +1,10 @@ +Rainloop change password custom mysql plugin +============================================ + +This plugin adds change password capability to Rainloop webmail by write your own SQL statement + +##### Installation is simple: + +1. Drop the change-password-customsql in the plugins directory (eg. _RainLoopDir_/data/data_xxxxx/_default/plugins/*) +2. In rainloop admin panel go to Plugins, and activate change-password-customsql. +3. Enter mysql details on the plugin config screen. diff --git a/plugins/change-passwords-customsql/change-passwords-customsql/ChangePasswordCustomSqlDriver.php b/plugins/change-passwords-customsql/change-passwords-customsql/ChangePasswordCustomSqlDriver.php new file mode 100644 index 000000000..4a60dc7fe --- /dev/null +++ b/plugins/change-passwords-customsql/change-passwords-customsql/ChangePasswordCustomSqlDriver.php @@ -0,0 +1,211 @@ +mHost = $mHost; + return $this; + } + + /** + * @param string $mUser + * + * @return \ChangePasswordCustomSqlDriver + */ + public function SetmUser($mUser) + { + $this->mUser = $mUser; + return $this; + } + + /** + * @param string $mPass + * + * @return \ChangePasswordCustomSqlDriver + */ + public function SetmPass($mPass) + { + $this->mPass = $mPass; + return $this; + } + + /** + * @param string $mDatabase + * + * @return \ChangePasswordCustomSqlDriver + */ + public function SetmDatabase($mDatabase) + { + $this->mDatabase = $mDatabase; + return $this; + } + + /** + * @param string $mTable + * + * @return \ChangePasswordCustomSqlDriver + */ + public function SetmTable($mTable) + { + $this->mTable = $mTable; + return $this; + } + + /** + * @param string $mSql + * + * @return \ChangePasswordCustomSqlDriver + */ + public function SetmSql($mSql) + { + $this->mSql = $mSql; + return $this; + } + + /** + * @param \MailSo\Log\Logger $oLogger + * + * @return \ChangePasswordCustomSqlDriver + */ + public function SetLogger($oLogger) + { + if ($oLogger instanceof \MailSo\Log\Logger) + { + $this->oLogger = $oLogger; + } + + return $this; + } + + /** + * @param \RainLoop\Account $oAccount + * + * @return bool + */ + public function PasswordChangePossibility($oAccount) + { + return $oAccount && $oAccount->Email() && + \RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $this->sAllowedEmails); + } + + /** + * @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); + + //prepare SQL varaibles + $sEmail = $oAccount->Email(); + $sEmailUser = \MailSo\Base\Utils::GetAccountNameFromEmail($sEmail); + $sEmailDomain = \MailSo\Base\Utils::GetDomainFromEmail($sEmail); + + //simple check + + $old = array(':email', ':oldpass', ':newpass', ':domain', ':username', ':table' ); + $new = array($sEmail, $sPrevPassword, $sNewPassword, $sEmailDomain, $sEmailUser, $this->mTable); + + $this->mSql = str_replace($old, $new, $this->mSql); + + $update = $conn->prepare($this->mSql); + $mSqlReturn = $update->execute(array()); +/* $mSqlReturn = $update->execute(array( + ':email' => $sEmail, + ':oldpass' => $sPrevPassword, + ':newpass' => $sNewPassword, + ':domain' => $sEmailDomain, + ':username' => $sEmailUser, + ':table' => $this->mTable + )); +*/ + if ($mSqlReturn == true) + { + $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/change-passwords-customsql/change-passwords-customsql/LICENSE b/plugins/change-passwords-customsql/change-passwords-customsql/LICENSE new file mode 100644 index 000000000..b5bc06b1b --- /dev/null +++ b/plugins/change-passwords-customsql/change-passwords-customsql/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2017 Martin Vilimovsky (vilimovsky@mvcs.cz) + +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/change-passwords-customsql/change-passwords-customsql/README b/plugins/change-passwords-customsql/change-passwords-customsql/README new file mode 100644 index 000000000..b35d488fb --- /dev/null +++ b/plugins/change-passwords-customsql/change-passwords-customsql/README @@ -0,0 +1 @@ +You can use your own SQL (MySQL) statement (with wildcards) to change password \ No newline at end of file diff --git a/plugins/change-passwords-customsql/change-passwords-customsql/VERSION b/plugins/change-passwords-customsql/change-passwords-customsql/VERSION new file mode 100644 index 000000000..9f8e9b69a --- /dev/null +++ b/plugins/change-passwords-customsql/change-passwords-customsql/VERSION @@ -0,0 +1 @@ +1.0 \ No newline at end of file diff --git a/plugins/change-passwords-customsql/change-passwords-customsql/index.php b/plugins/change-passwords-customsql/change-passwords-customsql/index.php new file mode 100644 index 000000000..8a04177f5 --- /dev/null +++ b/plugins/change-passwords-customsql/change-passwords-customsql/index.php @@ -0,0 +1,57 @@ +addHook('main.fabrica', 'MainFabrica'); + } + + /** + * @param string $sName + * @param mixed $oProvider + */ + public function MainFabrica($sName, &$oProvider) + { + switch ($sName) + { + case 'change-password': + + include_once __DIR__.'/ChangePasswordCustomSqlDriver.php'; + + $oProvider = new ChangePasswordCustomSqlDriver(); + + $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', '')) + ->SetmSql($this->Config()->Get('plugin', 'mSql', '')) + ; + + break; + } + } + + /** + * @return array + */ + public function configMapping() + { + return array( + \RainLoop\Plugins\Property::NewInstance('mHost')->SetLabel('MySQL Host') + ->SetDefaultValue('127.0.0.1'), + \RainLoop\Plugins\Property::NewInstance('mUser')->SetLabel('MySQL User'), + \RainLoop\Plugins\Property::NewInstance('mPass')->SetLabel('MySQL Password') + ->SetType(\RainLoop\Enumerations\PluginPropertyType::PASSWORD), + \RainLoop\Plugins\Property::NewInstance('mDatabase')->SetLabel('MySQL Database'), + \RainLoop\Plugins\Property::NewInstance('mTable')->SetLabel('MySQL Table'), + \RainLoop\Plugins\Property::NewInstance('mSql')->SetLabel('SQL statement') + ->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING_TEXT) + ->SetDescription('SQL statement (allowed wildcards :table, :email, :oldpass, :newpass, :domain, :username).When using MD5 OR SHA1 at tables, write it directly here as SQL functions. Fro non-SQL encryptions use another plugin or wait for new version.') + ->SetDefaultValue('UPDATE :table SET password = md5(:newpass) WHERE domain = :domain AND username = :username and oldpass = md5(:oldpass)') + ); + } +}