This repository was archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 982
Expand file tree
/
Copy pathChangePasswordVanVanDriver.php
More file actions
executable file
·216 lines (192 loc) · 5.2 KB
/
ChangePasswordVanVanDriver.php
File metadata and controls
executable file
·216 lines (192 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
/**
* change-password-docker-mailserver - Plugin that adds functionality to change the email account password of docker-mailserver container with the email frontend
*
* @author VanVan <https://github.com/VanVan>
*
*/
class ChangePasswordVanVanDriver implements \RainLoop\Providers\ChangePassword\ChangePasswordInterface
{
/**
* @var string
*/
private $sHost = '127.0.0.1';
/**
* @var int
*/
private $iPort = 21;
/**
* @var string
*/
private $sUser = 'root';
/**
* @var string
*/
private $sPassword = '';
/**
* @var string
*/
private $sLocation = 'setup.sh';
/**
* @var string
*/
private $bCheckExecution = true;
/**
* @var string
*/
private $sAllowedEmails = '';
/**
* @var \MailSo\Log\Logger
*/
private $oLogger = null;
/**
* @param string $sHost
*
* @return \ChangePasswordVanVanDriver
*/
public function SetHost($sHost)
{
$this->sHost = $sHost;
return $this;
}
/**
* @param int $iPort
*
* @return \ChangePasswordVanVanDriver
*/
public function SetPort($iPort)
{
$this->iPort = (int) $iPort;
return $this;
}
/**
* @param string $sHost
*
* @return \ChangePasswordVanVanDriver
*/
public function SetUser($sUser)
{
$this->sUser = $sUser;
return $this;
}
/**
* @param string $sPassword
*
* @return \ChangePasswordVanVanDriver
*/
public function SetPassword($sPassword)
{
$this->sPassword = $sPassword;
return $this;
}
/**
* @param string $sLocation
*
* @return \ChangePasswordVanVanDriver
*/
public function SetLocation($sLocation)
{
$this->sLocation = $sLocation;
return $this;
}
/**
* @param boolean $bCheckExecution
*
* @return \ChangePasswordVanVanDriver
*/
public function SetCheckExecution($bCheckExecution)
{
$this->bCheckExecution = $bCheckExecution;
return $this;
}
/**
* @param string $sAllowedEmails
*
* @return \ChangePasswordVanVanDriver
*/
public function SetAllowedEmails($sAllowedEmails)
{
$this->sAllowedEmails = $sAllowedEmails;
return $this;
}
/**
* @param \MailSo\Log\Logger $oLogger
*
* @return \ChangePasswordVanVanDriver
*/
public function SetLogger($oLogger)
{
if ($oLogger instanceof \MailSo\Log\Logger)
{
$this->oLogger = $oLogger;
}
return $this;
}
/**
* @param \RainLoop\Account $oAccount
*
* @return bool
*/
public function PasswordChangePossibility($oAccount)
{
return $oAccount && $oAccount->Email() &&
\RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $this->sAllowedEmails);
}
/**
* @param \RainLoop\Account $oAccount
* @param string $sPrevPassword
* @param string $sNewPassword
*
* @return bool
*/
public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword)
{
$bResult = false;
try
{
if(strpos("'", $oAccount->Email()) !== false || strpos('"', $oAccount->Email()) !== false)
return false;
if(strpos("'", $sNewPassword) !== false || strpos('"', $sNewPassword) !== false)
return false;
$connection = ssh2_connect($this->sHost, $this->iPort);
if ($connection)
if (ssh2_auth_password($connection, $this->sUser, $this->sPassword)) {
$sftp = ssh2_sftp($connection);
$streamFile = file_exists('ssh2.sftp://' . intval($sftp) . $this->sLocation);
if ($streamFile && $stream = ssh2_exec($connection, ($this->sUser=='root'?'':'sudo ').$this->sLocation . ' email update "' . $oAccount->Email() . '" "' . $sNewPassword . '"')) {
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
$stream_out_error = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
$outputString = stream_get_contents($stream_out);
$errorString = stream_get_contents($stream_out_error);
sleep(1);
if (!$this->bCheckExecution || (strlen($outputString) == 0 && (strlen($errorString) == 0 || strpos($errorString, 'error') === false)))
$bResult = true;
}
}
}
catch (\Exception $oException)
{
$bResult = false;
}
return $bResult;
}
/**
* @param string $path
* @return string
*/
public static function get_absolute_path($path) {
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
$parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
$absolutes = array();
foreach ($parts as $part) {
if ('.' == $part) continue;
if ('..' == $part) {
array_pop($absolutes);
} else {
$absolutes[] = $part;
}
}
return implode(DIRECTORY_SEPARATOR, $absolutes);
}
}