From 1dccfdba3ba667f48a62b7f0c23a1349da21fc26 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Fri, 29 Apr 2022 11:30:11 +0200 Subject: [PATCH] Revamp for #351 --- plugins/change-password-poppassd/LICENSE | 20 ++++ .../driver.php} | 101 +++++++----------- plugins/change-password-poppassd/index.php | 19 ++++ plugins/change-password/index.php | 20 +++- 4 files changed, 95 insertions(+), 65 deletions(-) create mode 100644 plugins/change-password-poppassd/LICENSE rename plugins/{change-password/drivers/poppassd.php => change-password-poppassd/driver.php} (70%) create mode 100644 plugins/change-password-poppassd/index.php diff --git a/plugins/change-password-poppassd/LICENSE b/plugins/change-password-poppassd/LICENSE new file mode 100644 index 000000000..4aed64b3a --- /dev/null +++ b/plugins/change-password-poppassd/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2015 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/change-password/drivers/poppassd.php b/plugins/change-password-poppassd/driver.php similarity index 70% rename from plugins/change-password/drivers/poppassd.php rename to plugins/change-password-poppassd/driver.php index d94951637..66218537e 100644 --- a/plugins/change-password/drivers/poppassd.php +++ b/plugins/change-password-poppassd/driver.php @@ -1,14 +1,13 @@ oLogger) { - $oPoppassdClient->SetLogger($this->oLogger); - } - $oPoppassdClient->Connect( + $this->Connect( $this->oConfig->Get('plugin', 'poppassd_host', ''), (int) $this->oConfig->Get('plugin', 'poppassd_port', 106) ); - $oPoppassdClient->Login($oAccount->Login(), $sPrevPassword) - ->NewPass($sNewPassword) - ->Disconnect() + + if ($this->bIsLoggined) { + $this->writeLogException( + new \RuntimeException('Already authenticated for this session'), + \MailSo\Log\Enumerations\Type::ERROR, true); + } + + $sLogin = \trim($sLogin); + + try + { + $this->sendRequestWithCheck('user', \trim($sLogin), true); + $this->sendRequestWithCheck('pass', $sPassword, true); + } + catch (\Throwable $oException) + { + $this->writeLogException($oException, \MailSo\Log\Enumerations\Type::NOTICE, true); + } + + $this->bIsLoggined = true; + + if ($this->bIsLoggined) { + $this->sendRequestWithCheck('newpass', $sNewPassword); + } else { + $this->writeLogException( + new \RuntimeException('Required login'), + \MailSo\Log\Enumerations\Type::ERROR, true); + } + + + $this->Disconnect() ; return true; @@ -65,10 +88,7 @@ class ChangePasswordDriverISPConfig return false; } -} -class PoppassdClient extends \MailSo\Net\NetClient -{ private $bIsLoggined = false, $iRequestTime = 0; @@ -83,32 +103,6 @@ class PoppassdClient extends \MailSo\Net\NetClient $this->validateResponse(); } - public function Login(string $sLogin, string $sPassword) : self - { - if ($this->bIsLoggined) { - $this->writeLogException( - new \RuntimeException('Already authenticated for this session'), - \MailSo\Log\Enumerations\Type::ERROR, true); - } - - $sLogin = \trim($sLogin); - $sPassword = $sPassword; - - try - { - $this->sendRequestWithCheck('user', $sLogin, true); - $this->sendRequestWithCheck('pass', $sPassword, true); - } - catch (\Throwable $oException) - { - $this->writeLogException($oException, \MailSo\Log\Enumerations\Type::NOTICE, true); - } - - $this->bIsLoggined = true; - - return $this; - } - public function Logout() : void { if ($this->bIsLoggined) { @@ -117,19 +111,6 @@ class PoppassdClient extends \MailSo\Net\NetClient $this->bIsLoggined = false; } - public function NewPass(string $sNewPassword) : self - { - if ($this->bIsLoggined) { - $this->sendRequestWithCheck('newpass', $sNewPassword); - } else { - $this->writeLogException( - new \RuntimeException('Required login'), - \MailSo\Log\Enumerations\Type::ERROR, true); - } - - return $this; - } - private function secureRequestParams($sCommand, $sAddToCommand) : ?string { if (\strlen($sAddToCommand)) { @@ -144,9 +125,10 @@ class PoppassdClient extends \MailSo\Net\NetClient return null; } - private function sendRequest(string $sCommand, string $sAddToCommand = '') : self + private function sendRequestWithCheck(string $sCommand, string $sAddToCommand = '', bool $bAuthRequestValidate = false) : void { - if (!\strlen(\trim($sCommand))) { + $sCommand = \trim($sCommand); + if (!\strlen($sCommand)) { $this->writeLogException( new \MailSo\Base\Exceptions\InvalidArgumentException(), \MailSo\Log\Enumerations\Type::ERROR, true); @@ -154,7 +136,6 @@ class PoppassdClient extends \MailSo\Net\NetClient $this->IsConnected(true); - $sCommand = \trim($sCommand); $sRealCommand = $sCommand . (\strlen($sAddToCommand) ? ' '.$sAddToCommand : ''); $sFakeCommand = ''; @@ -166,15 +147,7 @@ class PoppassdClient extends \MailSo\Net\NetClient $this->iRequestTime = \microtime(true); $this->sendRaw($sRealCommand, true, $sFakeCommand); - return $this; - } - - private function sendRequestWithCheck(string $sCommand, string $sAddToCommand = '', bool $bAuthRequestValidate = false) : self - { - $this->sendRequest($sCommand, $sAddToCommand); $this->validateResponse($bAuthRequestValidate); - - return $this; } private function validateResponse(bool $bAuthRequestValidate = false) : self diff --git a/plugins/change-password-poppassd/index.php b/plugins/change-password-poppassd/index.php new file mode 100644 index 000000000..4e6874baf --- /dev/null +++ b/plugins/change-password-poppassd/index.php @@ -0,0 +1,19 @@ +Config()->Get('plugin', "driver_{$name}_enabled", false))) { + require_once $file; + if ($class::isSupported()) { + yield $name => $class; + } + } + } + catch (\Throwable $oException) + { + \trigger_error("ERROR {$class}: " . $oException->getMessage()); + } + } } public function Supported() : string