|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Mail-in-a-box Password Change Plugin |
|
4
|
|
|
* |
|
5
|
|
|
* Based on VirtualminChangePasswordDriver |
|
6
|
|
|
* |
|
7
|
|
|
* Author: Marius Gripsgard |
|
8
|
|
|
*/ |
|
9
|
|
|
class MailInABoxChangePasswordDriver implements \RainLoop\Providers\ChangePassword\ChangePasswordInterface |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
private $sAllowedEmails = ''; |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private $sHost = ''; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private $sAdminUser = ''; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $sAdminPassword = ''; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var \MailSo\Log\Logger |
|
29
|
|
|
*/ |
|
30
|
|
|
private $oLogger = null; |
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $sHost |
|
33
|
|
|
* @param string $sAdminUser |
|
34
|
|
|
* @param string $sAdminPassword |
|
35
|
|
|
* |
|
36
|
|
|
* @return \MailInABoxChangePasswordDriver |
|
37
|
|
|
*/ |
|
38
|
|
|
public function SetConfig($sHost, $sAdminUser, $sAdminPassword) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->sHost = $sHost; |
|
41
|
|
|
$this->sAdminUser = $sAdminUser; |
|
42
|
|
|
$this->sAdminPassword = $sAdminPassword; |
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $sAllowedEmails |
|
47
|
|
|
* |
|
48
|
|
|
* @return \MailInABoxChangePasswordDriver |
|
49
|
|
|
*/ |
|
50
|
|
|
public function SetAllowedEmails($sAllowedEmails) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->sAllowedEmails = $sAllowedEmails; |
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
/** |
|
56
|
|
|
* @param \MailSo\Log\Logger $oLogger |
|
57
|
|
|
* |
|
58
|
|
|
* @return \MailInABoxChangePasswordDriver |
|
59
|
|
|
*/ |
|
60
|
|
|
public function SetLogger($oLogger) |
|
61
|
|
|
{ |
|
62
|
|
|
if ($oLogger instanceof \MailSo\Log\Logger) |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
$this->oLogger = $oLogger; |
|
65
|
|
|
} |
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
/** |
|
69
|
|
|
* @param string $sDesc |
|
70
|
|
|
* @param int $iType = \MailSo\Log\Enumerations\Type::INFO |
|
71
|
|
|
* |
|
72
|
|
|
* @return \MailInABoxChangePasswordDriver |
|
73
|
|
|
*/ |
|
74
|
|
|
public function WriteLog($sDesc, $iType = \MailSo\Log\Enumerations\Type::INFO) |
|
75
|
|
|
{ |
|
76
|
|
|
if ($this->oLogger) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->oLogger->Write($sDesc, $iType); |
|
79
|
|
|
} |
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
/** |
|
83
|
|
|
* @param \RainLoop\Model\Account $oAccount |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
|
|
public function PasswordChangePossibility($oAccount) |
|
88
|
|
|
{ |
|
89
|
|
|
return $oAccount && $oAccount->Email() && |
|
90
|
|
|
\RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $this->sAllowedEmails); |
|
91
|
|
|
} |
|
92
|
|
|
/** |
|
93
|
|
|
* @param \RainLoop\Model\Account $oAccount |
|
94
|
|
|
* @param string $sPrevPassword |
|
95
|
|
|
* @param string $sNewPassword |
|
96
|
|
|
* |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
|
|
public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->WriteLog('Mail-in-a-box: Try to change password for '.$oAccount->Email()); |
|
102
|
|
|
$bResult = false; |
|
103
|
|
|
if (!empty($this->sHost) && !empty($this->sAdminUser) && !empty($this->sAdminPassword) && $oAccount) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->WriteLog('Mail-in-a-box:[Check] Required Fields Present'); |
|
106
|
|
|
$sEmail = \trim(\strtolower($oAccount->Email())); |
|
107
|
|
|
$sHost = \rtrim(\trim($this->sHost), '/'); |
|
108
|
|
|
$sUrl = $sHost.'/admin/mail/users/password'; |
|
109
|
|
|
|
|
110
|
|
|
$sAdminUser = $this->sAdminUser; |
|
111
|
|
|
$sAdminPassword = $this->sAdminPassword; |
|
112
|
|
|
$iCode = 0; |
|
|
|
|
|
|
113
|
|
|
$aPost = array( |
|
114
|
|
|
'email' => $sEmail, |
|
115
|
|
|
'password' => $sNewPassword, |
|
116
|
|
|
); |
|
117
|
|
|
$aOptions = array( |
|
118
|
|
|
CURLOPT_URL => $sUrl, |
|
119
|
|
|
CURLOPT_HEADER => false, |
|
120
|
|
|
CURLOPT_FAILONERROR => true, |
|
121
|
|
|
CURLOPT_SSL_VERIFYPEER => false, |
|
122
|
|
|
CURLOPT_RETURNTRANSFER => true, |
|
123
|
|
|
CURLOPT_POST => true, |
|
124
|
|
|
CURLOPT_POSTFIELDS => \http_build_query($aPost, '', '&'), |
|
125
|
|
|
CURLOPT_TIMEOUT => 20, |
|
126
|
|
|
CURLOPT_SSL_VERIFYHOST => false, |
|
127
|
|
|
CURLOPT_USERPWD => $sAdminUser.':'.$sAdminPassword, |
|
128
|
|
|
CURLOPT_HTTPAUTH => CURLAUTH_BASIC |
|
129
|
|
|
); |
|
130
|
|
|
$oCurl = \curl_init(); |
|
131
|
|
|
\curl_setopt_array($oCurl, $aOptions); |
|
132
|
|
|
$this->WriteLog('Mail-in-a-box: Send post request: '.$sUrl); |
|
133
|
|
|
$mResult = \curl_exec($oCurl); |
|
134
|
|
|
$iCode = (int) \curl_getinfo($oCurl, CURLINFO_HTTP_CODE); |
|
135
|
|
|
$sContentType = (string) \curl_getinfo($oCurl, CURLINFO_CONTENT_TYPE); |
|
136
|
|
|
$this->WriteLog('Mail-in-a-box: Post request result: (Status: '.$iCode.', ContentType: '.$sContentType.')'); |
|
137
|
|
View Code Duplication |
if (false === $mResult || 200 !== $iCode) |
|
|
|
|
|
|
138
|
|
|
{ |
|
139
|
|
|
$this->WriteLog('Mail-in-a-box: Error: '.\curl_error($oCurl), \MailSo\Log\Enumerations\Type::WARNING); |
|
140
|
|
|
} |
|
141
|
|
|
if (\is_resource($oCurl)) |
|
142
|
|
|
{ |
|
143
|
|
|
\curl_close($oCurl); |
|
144
|
|
|
} |
|
145
|
|
|
if (false !== $mResult && 200 === $iCode) |
|
146
|
|
|
{ |
|
147
|
|
|
$this->WriteLog('Mail-in-a-box: Password Change Status: Success'); |
|
148
|
|
|
$bResult = true; |
|
149
|
|
|
} |
|
150
|
|
|
else |
|
151
|
|
|
{ |
|
152
|
|
|
$this->WriteLog('Mail-in-a-box[Error]: Empty Response: Code: '.$iCode); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
return $bResult; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
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.