diff --git a/plugins/vesta-change-password/LICENSE b/plugins/vesta-change-password/LICENSE new file mode 100644 index 000000000..271342337 --- /dev/null +++ b/plugins/vesta-change-password/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2013 RainLoop Team + +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/vesta-change-password/README b/plugins/vesta-change-password/README new file mode 100644 index 000000000..4f2f06281 --- /dev/null +++ b/plugins/vesta-change-password/README @@ -0,0 +1 @@ +Plugin that adds functionality to change the email account password (Vesta Control Panel). \ No newline at end of file diff --git a/plugins/vesta-change-password/VERSION b/plugins/vesta-change-password/VERSION new file mode 100644 index 000000000..9f8e9b69a --- /dev/null +++ b/plugins/vesta-change-password/VERSION @@ -0,0 +1 @@ +1.0 \ No newline at end of file diff --git a/plugins/vesta-change-password/VestaChangePasswordDriver.php b/plugins/vesta-change-password/VestaChangePasswordDriver.php new file mode 100644 index 000000000..cbb8be5c1 --- /dev/null +++ b/plugins/vesta-change-password/VestaChangePasswordDriver.php @@ -0,0 +1,146 @@ +sHost = $sHost; + $this->iPort = $iPort; + + return $this; + } + + /** + * @param string $sAllowedEmails + * + * @return \VestaChangePasswordDriver + */ + public function SetAllowedEmails($sAllowedEmails) + { + $this->sAllowedEmails = $sAllowedEmails; + return $this; + } + + /** + * @param \MailSo\Log\Logger $oLogger + * + * @return \VestaChangePasswordDriver + */ + 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('Vesta: Try to change password for '.$oAccount->Email()); + } + + $bResult = false; + if (!empty($this->sHost) && 0 < $this->iPort && $oAccount) + { + $sEmail = \trim(\strtolower($oAccount->Email())); + + $sHost = \trim($this->sHost); + $sHost = \str_replace('{user:host-imap}', $oAccount->Domain()->IncHost(), $sHost); + $sHost = \str_replace('{user:host-smtp}', $oAccount->Domain()->OutHost(), $sHost); + $sHost = \str_replace('{user:domain}', \MailSo\Base\Utils::GetDomainFromEmail($sEmail), $sHost); + $sHost = \rtrim($this->sHost, '/\\'); + $sHost = 'https://'.$sHost; + + $sUrl = $sHost.':'.$this->iPort.'/reset/mail/'; + + $iCode = 0; + $oHttp = \MailSo\Base\Http::SingletonInstance(); + + if ($this->oLogger) + { + $this->oLogger->Write('Vesta[Api Request]:'.$sUrl); + } + + $mResult = $oHttp->SendPostRequest($sUrl, + array( + 'email' => $sEmail, + 'password' => $sPrevPassword, + 'new' => $sNewPassword, + ), 'MailSo Http User Agent (v1)', $iCode, $this->oLogger); + + if (false !== $mResult && 200 === $iCode) + { + $aRes = null; + @\parse_str($mResult, $aRes); + if (is_array($aRes) && (!isset($aRes['error']) || (int) $aRes['error'] !== 1)) + { + $bResult = true; + } + else + { + if ($this->oLogger) + { + $this->oLogger->Write('Vesta[Error]: Response: '.$mResult); + } + } + } + else + { + if ($this->oLogger) + { + $this->oLogger->Write('Vesta[Error]: Empty Response: Code:'.$iCode); + } + } + } + + return $bResult; + } +} diff --git a/plugins/vesta-change-password/index.php b/plugins/vesta-change-password/index.php new file mode 100644 index 000000000..f79c8711a --- /dev/null +++ b/plugins/vesta-change-password/index.php @@ -0,0 +1,55 @@ +addHook('main.fabrica', 'MainFabrica'); + } + + /** + * @param string $sName + * @param mixed $oProvider + */ + public function MainFabrica($sName, &$oProvider) + { + switch ($sName) + { + case 'change-password': + + $sHost = \trim($this->Config()->Get('plugin', 'vesta_host', '')); + $iPort = (int) $this->Config()->Get('plugin', 'vesta_port', 8083); + + if (!empty($sHost) && 0 < $iPort) + { + include_once __DIR__.'/VestaChangePasswordDriver.php'; + + $oProvider = new VestaChangePasswordDriver(); + $oProvider->SetLogger($this->Manager()->Actions()->Logger()); + $oProvider->SetConfig($sHost, $iPort); + $oProvider->SetAllowedEmails(\strtolower(\trim($this->Config()->Get('plugin', 'allowed_emails', '')))); + } + + break; + } + } + + /** + * @return array + */ + public function configMapping() + { + return array( + \RainLoop\Plugins\Property::NewInstance('vesta_host')->SetLabel('Vesta Host') + ->SetDefaultValue('') + ->SetDescription('Allowed patterns: {user:host-imap}, {user:host-smtp}, {user:domain}'), + \RainLoop\Plugins\Property::NewInstance('Vesta_port')->SetLabel('Vesta Port') + ->SetType(\RainLoop\Enumerations\PluginPropertyType::INT) + ->SetDefaultValue(8083), + \RainLoop\Plugins\Property::NewInstance('allowed_emails')->SetLabel('Allowed emails') + ->SetType(\RainLoop\Enumerations\PluginPropertyType::STRING_TEXT) + ->SetDescription('Allowed emails, space as delimiter, wildcard supported. Example: user1@domain1.net user2@domain1.net *@domain2.net') + ->SetDefaultValue('*') + ); + } +}