|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class IspConfigChangePasswordDriver implements \RainLoop\Providers\ChangePassword\ChangePasswordInterface |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
/** |
|
6
|
|
|
* @var string |
|
7
|
|
|
*/ |
|
8
|
|
|
private $sDsn = ''; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
private $sUser = ''; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private $sPassword = ''; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $sAllowedEmails = ''; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var \MailSo\Log\Logger |
|
27
|
|
|
*/ |
|
28
|
|
|
private $oLogger = null; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string $sDsn |
|
32
|
|
|
* @param string $sUser |
|
33
|
|
|
* @param string $sPassword |
|
34
|
|
|
* |
|
35
|
|
|
* @return \IspConfigChangePasswordDriver |
|
36
|
|
|
*/ |
|
37
|
|
|
public function SetConfig($sDsn, $sUser, $sPassword) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->sDsn = $sDsn; |
|
40
|
|
|
$this->sUser = $sUser; |
|
41
|
|
|
$this->sPassword = $sPassword; |
|
42
|
|
|
|
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $sAllowedEmails |
|
48
|
|
|
* |
|
49
|
|
|
* @return \IspConfigChangePasswordDriver |
|
50
|
|
|
*/ |
|
51
|
|
|
public function SetAllowedEmails($sAllowedEmails) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->sAllowedEmails = $sAllowedEmails; |
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param \MailSo\Log\Logger $oLogger |
|
59
|
|
|
* |
|
60
|
|
|
* @return \IspConfigChangePasswordDriver |
|
61
|
|
|
*/ |
|
62
|
|
|
public function SetLogger($oLogger) |
|
63
|
|
|
{ |
|
64
|
|
|
if ($oLogger instanceof \MailSo\Log\Logger) |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
$this->oLogger = $oLogger; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param \RainLoop\Model\Account $oAccount |
|
74
|
|
|
* |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
|
|
public function PasswordChangePossibility($oAccount) |
|
78
|
|
|
{ |
|
79
|
|
|
return $oAccount && $oAccount->Email() && |
|
80
|
|
|
\RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $this->sAllowedEmails); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param \RainLoop\Model\Account $oAccount |
|
85
|
|
|
* @param string $sPrevPassword |
|
86
|
|
|
* @param string $sNewPassword |
|
87
|
|
|
* |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
|
|
public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword) |
|
91
|
|
|
{ |
|
92
|
|
|
if ($this->oLogger) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->oLogger->Write('ISP: Try to change password for '.$oAccount->Email()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$bResult = false; |
|
98
|
|
|
if (!empty($this->sDsn) && 0 < \strlen($this->sUser) && 0 < \strlen($this->sPassword) && $oAccount) |
|
99
|
|
|
{ |
|
100
|
|
|
try |
|
101
|
|
|
{ |
|
102
|
|
|
$oPdo = new \PDO($this->sDsn, $this->sUser, $this->sPassword); |
|
103
|
|
|
$oPdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
|
104
|
|
|
|
|
105
|
|
|
$oStmt = $oPdo->prepare('SELECT password, mailuser_id FROM mail_user WHERE login = ? LIMIT 1'); |
|
106
|
|
|
if ($oStmt->execute(array($oAccount->IncLogin()))) |
|
107
|
|
|
{ |
|
108
|
|
|
$aFetchResult = $oStmt->fetchAll(\PDO::FETCH_ASSOC); |
|
109
|
|
|
if (\is_array($aFetchResult) && isset($aFetchResult[0]['password'], $aFetchResult[0]['mailuser_id'])) |
|
110
|
|
|
{ |
|
111
|
|
|
$sDbPassword = \stripslashes($aFetchResult[0]['password']); |
|
112
|
|
|
$sDbSalt = '$1$'.\substr($sDbPassword, 3, 8).'$'; |
|
113
|
|
|
|
|
114
|
|
|
if (\crypt(\stripslashes($sPrevPassword), $sDbSalt) === $sDbPassword) |
|
115
|
|
|
{ |
|
116
|
|
|
$oStmt = $oPdo->prepare('UPDATE mail_user SET password = ? WHERE mailuser_id = ?'); |
|
117
|
|
|
$bResult = (bool) $oStmt->execute( |
|
118
|
|
|
array($this->cryptPassword($sNewPassword), $aFetchResult[0]['mailuser_id'])); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
catch (\Exception $oException) |
|
124
|
|
|
{ |
|
125
|
|
|
if ($this->oLogger) |
|
126
|
|
|
{ |
|
127
|
|
|
$this->oLogger->WriteException($oException); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $bResult; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param string $sPassword |
|
137
|
|
|
* @return string |
|
138
|
|
|
*/ |
|
139
|
|
View Code Duplication |
private function cryptPassword($sPassword) |
|
|
|
|
|
|
140
|
|
|
{ |
|
141
|
|
|
$sSalt = ''; |
|
142
|
|
|
$sBase64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; |
|
143
|
|
|
|
|
144
|
|
|
for ($iIndex = 0; $iIndex < 8; $iIndex++) |
|
145
|
|
|
{ |
|
146
|
|
|
$sSalt .= $sBase64[\rand(0, 63)]; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return \crypt($sPassword, '$1$'.$sSalt.'$'); |
|
150
|
|
|
} |
|
151
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.