mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-08-29 20:19:22 +03:00
vpopmail-change-password fixes
This commit is contained in:
parent
9b8258ad91
commit
f15f25a635
3 changed files with 300 additions and 301 deletions
|
|
@ -1,229 +1,228 @@
|
|||
<?php
|
||||
class ChangePasswordVpopmailDriver implements \RainLoop\Providers\ChangePassword\ChangePasswordInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $mHost = 'localhost';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $mUser = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $mPass = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $mDatabase = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $mTable = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $mColumn = '';
|
||||
|
||||
/**
|
||||
* @var \MailSo\Log\Logger
|
||||
*/
|
||||
private $oLogger = null;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $aDomains = array();
|
||||
|
||||
/**
|
||||
* @param string $mHost
|
||||
*
|
||||
* @return \ChangePasswordVpopmailDriver
|
||||
*/
|
||||
public function SetmHost($mHost)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
||||
<?php
|
||||
|
||||
class ChangePasswordVpopmailDriver implements \RainLoop\Providers\ChangePassword\ChangePasswordInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $mHost = 'localhost';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $mUser = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $mPass = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $mDatabase = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $mTable = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $mColumn = '';
|
||||
|
||||
/**
|
||||
* @var \MailSo\Log\Logger
|
||||
*/
|
||||
private $oLogger = null;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $aDomains = array();
|
||||
|
||||
/**
|
||||
* @param string $mHost
|
||||
*
|
||||
* @return \ChangePasswordVpopmailDriver
|
||||
*/
|
||||
public function SetmHost($mHost)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
1.0
|
||||
1.1
|
||||
|
|
|
|||
|
|
@ -1,71 +1,71 @@
|
|||
<?php
|
||||
|
||||
class ChangePasswordVpopmailPlugin extends \RainLoop\Plugins\AbstractPlugin
|
||||
{
|
||||
public function Init()
|
||||
{
|
||||
$this->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')
|
||||
);
|
||||
}
|
||||
}
|
||||
<?php
|
||||
|
||||
class ChangePasswordVpopmailPlugin extends \RainLoop\Plugins\AbstractPlugin
|
||||
{
|
||||
public function Init()
|
||||
{
|
||||
$this->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')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue