snappymail/plugins/postfixadmin-change-password/ChangePasswordPostfixAdminDriver.php
2014-11-15 18:45:54 +04:00

246 lines
4.4 KiB
PHP

<?php
class ChangePasswordPostfixAdminDriver implements \RainLoop\Providers\ChangePassword\ChangePasswordInterface
{
/**
* @var string
*/
private $sHost = '127.0.0.1';
/**
* @var int
*/
private $iPort = 3306;
/**
* @var string
*/
private $sDatabase = 'postfixadmin';
/**
* @var string
*/
private $sUser = 'postfixadmin';
/**
* @var string
*/
private $sPassword = '';
/**
* @var string
*/
private $sEncrypt = '';
/**
* @var string
*/
private $sAllowedEmails = '';
/**
* @var \MailSo\Log\Logger
*/
private $oLogger = null;
/**
* @param string $sHost
*
* @return \ChangePasswordPostfixAdminDriver
*/
public function SetHost($sHost)
{
$this->sHost = $sHost;
return $this;
}
/**
* @param int $iPort
*
* @return \ChangePasswordPostfixAdminDriver
*/
public function SetPort($iPort)
{
$this->iPort = (int) $iPort;
return $this;
}
/**
* @param string $sDatabase
*
* @return \ChangePasswordPostfixAdminDriver
*/
public function SetDatabase($sDatabase)
{
$this->sDatabase = $sDatabase;
return $this;
}
/**
* @param string $sUser
*
* @return \ChangePasswordPostfixAdminDriver
*/
public function SetUser($sUser)
{
$this->sUser = $sUser;
return $this;
}
/**
* @param string $sPassword
*
* @return \ChangePasswordPostfixAdminDriver
*/
public function SetPassword($sPassword)
{
$this->sPassword = $sPassword;
return $this;
}
/**
* @param string $sEncrypt
*
* @return \ChangePasswordPostfixAdminDriver
*/
public function SetEncrypt($sEncrypt)
{
$this->sEncrypt = $sEncrypt;
return $this;
}
/**
* @param string $sAllowedEmails
*
* @return \ChangePasswordPostfixAdminDriver
*/
public function SetAllowedEmails($sAllowedEmails)
{
$this->sAllowedEmails = $sAllowedEmails;
return $this;
}
/**
* @param \MailSo\Log\Logger $oLogger
*
* @return \ChangePasswordPostfixAdminDriver
*/
public function SetLogger($oLogger)
{
if ($oLogger instanceof \MailSo\Log\Logger)
{
$this->oLogger = $oLogger;
}
return $this;
}
/**
* @param \RainLoop\Model\Account $oAccount
*
* @return bool
*/
public function PasswordChangePossibility($oAccount)
{
return $oAccount && $oAccount->Email() &&
\RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $this->sAllowedEmails);
}
/**
* @param \RainLoop\Model\Account $oAccount
* @param string $sPrevPassword
* @param string $sNewPassword
*
* @return bool
*/
public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword)
{
if ($this->oLogger)
{
$this->oLogger->Write('Postfix: Try to change password for '.$oAccount->Email());
}
$bResult = false;
if (0 < \strlen($sNewPassword))
{
try
{
$sDsn = 'mysql:host='.$this->sHost.';port='.$this->iPort.';dbname='.$this->sDatabase;
$oPdo = new \PDO($sDsn, $this->sUser, $this->sPassword);
$oPdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$sUpdatePassword = $this->cryptPassword($sNewPassword, $oPdo);
if (0 < \strlen($sUpdatePassword))
{
$oStmt = $oPdo->prepare('UPDATE mailbox SET password = ? WHERE username = ?');
$bResult = (bool) $oStmt->execute(array($sUpdatePassword, $oAccount->Email()));
}
else
{
if ($this->oLogger)
{
$this->oLogger->Write('Postfix: Encyted password is ematy',
\MailSo\Log\Enumerations\Type::ERROR);
}
}
$oPdo = null;
}
catch (\Exception $oException)
{
if ($this->oLogger)
{
$this->oLogger->WriteException($oException);
}
}
}
return $bResult;
}
/**
* @param string $sPassword
* @param \PDO $oPdo
*
* @return string
*/
private function cryptPassword($sPassword, $oPdo)
{
$sResult = '';
switch ($this->sEncrypt)
{
default:
case 'cleartext':
$sResult = $sPassword;
break;
case 'md5crypt':
include_once __DIR__.'/md5crypt.php';
$sResult = md5crypt($sPassword);
break;
case 'md5':
$sResult = md5($sPassword);
break;
case 'system':
$sResult = crypt($sPassword);
break;
case 'mysql_encrypt':
$oStmt = $oPdo->prepare('SELECT ENCRYPT(?) AS encpass');
if ($oStmt->execute(array($sPassword)))
{
$aFetchResult = $oStmt->fetchAll(\PDO::FETCH_ASSOC);
if (\is_array($aFetchResult) && isset($aFetchResult[0]['encpass']))
{
$sResult = $aFetchResult[0]['encpass'];
}
}
break;
}
return $sResult;
}
}