Completed
Push — master ( ef601c...11989a )
by Rain
26:04 queued 17:32
created

ChangePasswordPostfixAdminDriver   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 336
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 336
rs 8.3999
c 0
b 0
f 0
wmc 38
lcom 2
cbo 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A SetEngine() 0 5 1
A SetHost() 0 5 1
A SetPort() 0 5 1
A SetDatabase() 0 5 1
A SetTable() 0 5 1
A SetUserColumn() 0 5 1
A SetPasswordColumn() 0 5 1
A SetUser() 0 5 1
A SetPassword() 0 5 1
A SetEncrypt() 0 5 1
A SetAllowedEmails() 0 5 1
A SetLogger() 0 9 2
A PasswordChangePossibility() 0 5 3
C ChangePassword() 0 60 9
C cryptPassword() 0 52 13
1
<?php
2
3
class ChangePasswordPostfixAdminDriver implements \RainLoop\Providers\ChangePassword\ChangePasswordInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
  /**
6
	* @var string
7
	*/
8
	private $sEngine = 'MySQL';
9
10
	/**
11
	 * @var string
12
	 */
13
	private $sHost = '127.0.0.1';
14
15
	/**
16
	 * @var int
17
	 */
18
	private $iPort = 3306;
19
20
	/**
21
	 * @var string
22
	 */
23
	private $sDatabase = 'postfixadmin';
24
25
	/**
26
	* @var string
27
	*/
28
	private $sTable = 'mailbox';
29
30
	/**
31
	* @var string
32
	*/
33
	private $sUsercol = 'username';
34
35
	/**
36
	* @var string
37
	*/
38
	private $sPasscol = 'password';
39
40
	/**
41
	 * @var string
42
	 */
43
	private $sUser = 'postfixadmin';
44
45
	/**
46
	 * @var string
47
	 */
48
	private $sPassword = '';
49
50
	/**
51
	 * @var string
52
	 */
53
	private $sEncrypt = '';
54
55
	/**
56
	 * @var string
57
	 */
58
	private $sAllowedEmails = '';
59
60
	/**
61
	 * @var \MailSo\Log\Logger
62
	 */
63
	private $oLogger = null;
64
65
	/**
66
	 * @param string $sEngine
67
	 *
68
	 * @return \ChangePasswordPostfixAdminDriver
69
	 */
70
	 public function SetEngine($sEngine)
71
	 {
72
		 $this->sEngine = $sEngine;
73
		 return $this;
74
	 }
75
76
	/**
77
	 * @param string $sHost
78
	 *
79
	 * @return \ChangePasswordPostfixAdminDriver
80
	 */
81
	public function SetHost($sHost)
82
	{
83
		$this->sHost = $sHost;
84
		return $this;
85
	}
86
87
	/**
88
	 * @param int $iPort
89
	 *
90
	 * @return \ChangePasswordPostfixAdminDriver
91
	 */
92
	public function SetPort($iPort)
93
	{
94
		$this->iPort = (int) $iPort;
95
		return $this;
96
	}
97
98
	/**
99
	 * @param string $sDatabase
100
	 *
101
	 * @return \ChangePasswordPostfixAdminDriver
102
	 */
103
	public function SetDatabase($sDatabase)
104
	{
105
		$this->sDatabase = $sDatabase;
106
		return $this;
107
	}
108
109
	/**
110
	* @param string $sTable
111
	*
112
	* @return \ChangePasswordPostfixAdminDriver
113
	*/
114
	public function SetTable($sTable)
115
	{
116
		$this->sTable = $sTable;
117
		return $this;
118
	}
119
120
	/**
121
	* @param string $sUsercol
122
	*
123
	* @return \ChangePasswordPostfixAdminDriver
124
	*/
125
	public function SetUserColumn($sUsercol)
126
	{
127
		$this->sUsercol = $sUsercol;
128
		return $this;
129
	}
130
131
	/**
132
	* @param string $sPasscol
133
	*
134
	* @return \ChangePasswordPostfixAdminDriver
135
	*/
136
	public function SetPasswordColumn($sPasscol)
137
	{
138
		$this->sPasscol = $sPasscol;
139
		return $this;
140
	}
141
142
	/**
143
	 * @param string $sUser
144
	 *
145
	 * @return \ChangePasswordPostfixAdminDriver
146
	 */
147
	public function SetUser($sUser)
148
	{
149
		$this->sUser = $sUser;
150
		return $this;
151
	}
152
153
	/**
154
	 * @param string $sPassword
155
	 *
156
	 * @return \ChangePasswordPostfixAdminDriver
157
	 */
158
	public function SetPassword($sPassword)
159
	{
160
		$this->sPassword = $sPassword;
161
		return $this;
162
	}
163
164
	/**
165
	 * @param string $sEncrypt
166
	 *
167
	 * @return \ChangePasswordPostfixAdminDriver
168
	 */
169
	public function SetEncrypt($sEncrypt)
170
	{
171
		$this->sEncrypt = $sEncrypt;
172
		return $this;
173
	}
174
175
	/**
176
	 * @param string $sAllowedEmails
177
	 *
178
	 * @return \ChangePasswordPostfixAdminDriver
179
	 */
180
	public function SetAllowedEmails($sAllowedEmails)
181
	{
182
		$this->sAllowedEmails = $sAllowedEmails;
183
		return $this;
184
	}
185
186
	/**
187
	 * @param \MailSo\Log\Logger $oLogger
188
	 *
189
	 * @return \ChangePasswordPostfixAdminDriver
190
	 */
191
	public function SetLogger($oLogger)
192
	{
193
		if ($oLogger instanceof \MailSo\Log\Logger)
0 ignored issues
show
Bug introduced by
The class MailSo\Log\Logger does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
194
		{
195
			$this->oLogger = $oLogger;
196
		}
197
198
		return $this;
199
	}
200
201
	/**
202
	 * @param \RainLoop\Model\Account $oAccount
203
	 *
204
	 * @return bool
205
	 */
206
	public function PasswordChangePossibility($oAccount)
207
	{
208
		return $oAccount && $oAccount->Email() &&
209
			\RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $this->sAllowedEmails);
210
	}
211
212
	/**
213
	 * @param \RainLoop\Model\Account $oAccount
214
	 * @param string $sPrevPassword
215
	 * @param string $sNewPassword
216
	 *
217
	 * @return bool
218
	 */
219
	public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword)
220
	{
221
		if ($this->oLogger)
222
		{
223
			$this->oLogger->Write('Postfix: Try to change password for '.$oAccount->Email());
224
		}
225
226
		unset($sPrevPassword);
227
228
		$bResult = false;
229
230
		if (0 < \strlen($sNewPassword))
231
		{
232
			try
233
			{
234
				$sDsn = '';
0 ignored issues
show
Unused Code introduced by
$sDsn is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
235
				switch($this->sEngine){
236
					case 'MySQL':
237
				  		$sDsn = 'mysql:host='.$this->sHost.';port='.$this->iPort.';dbname='.$this->sDatabase;
238
						break;
239
				  	case 'PostgreSQL':
240
				 		$sDsn = 'pgsql:host='.$this->sHost.';port='.$this->iPort.';dbname='.$this->sDatabase;
241
						break;
242
				  	default:
243
				    		$sDsn = 'mysql:host='.$this->sHost.';port='.$this->iPort.';dbname='.$this->sDatabase;
244
					  	break;
245
				}
246
247
248
				$oPdo = new \PDO($sDsn, $this->sUser, $this->sPassword);
249
				$oPdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
250
251
				$sUpdatePassword = $this->cryptPassword($sNewPassword, $oPdo);
252
				if (0 < \strlen($sUpdatePassword))
253
				{
254
					$oStmt = $oPdo->prepare("UPDATE {$this->sTable} SET {$this->sPasscol} = ? WHERE {$this->sUsercol} = ?");
255
					$bResult = (bool) $oStmt->execute(array($sUpdatePassword, $oAccount->Email()));
256
				}
257
				else
258
				{
259
					if ($this->oLogger)
260
					{
261
						$this->oLogger->Write('Postfix: Encrypted password is empty',
262
							\MailSo\Log\Enumerations\Type::ERROR);
263
					}
264
				}
265
266
				$oPdo = null;
0 ignored issues
show
Unused Code introduced by
$oPdo is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
267
			}
268
			catch (\Exception $oException)
269
			{
270
				if ($this->oLogger)
271
				{
272
					$this->oLogger->WriteException($oException);
273
				}
274
			}
275
		}
276
277
		return $bResult;
278
	}
279
280
	/**
281
	 * @param string $sPassword
282
	 * @param \PDO $oPdo
283
	 *
284
	 * @return string
285
	 */
286
	private function cryptPassword($sPassword, $oPdo)
287
	{
288
		$sResult = '';
289
		$sSalt = substr(str_shuffle('./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'), 0, 16);
290
		switch (strtolower($this->sEncrypt))
291
		{
292
			default:
293
			case 'plain':
294
			case 'cleartext':
295
				$sResult = '{PLAIN}' . $sPassword;
296
				break;
297
298
			case 'md5crypt':
299
				include_once __DIR__.'/md5crypt.php';
300
				$sResult = '{MD5-CRYPT}' . md5crypt($sPassword);
301
				break;
302
303
			case 'md5':
304
				$sResult = '{PLAIN-MD5}' . md5($sPassword);
305
				break;
306
307
			case 'system':
308
				$sResult = '{CRYPT}' . crypt($sPassword);
309
				break;
310
311
			case 'sha256-crypt':
312
				$sResult = '{SHA256-CRYPT}' . crypt($sPassword,'$5$'.$sSalt);
313
				break;
314
315
			case 'sha512-crypt':
316
				$sResult = '{SHA512-CRYPT}' . crypt($sPassword,'$6$'.$sSalt);
317
				break;
318
319
			case 'mysql_encrypt':
320
			  if($this->sEngine == 'MySQL'){
321
			  	$oStmt = $oPdo->prepare('SELECT ENCRYPT(?) AS encpass');
322
				if ($oStmt->execute(array($sPassword)))
323
				{
324
					$aFetchResult = $oStmt->fetchAll(\PDO::FETCH_ASSOC);
325
					if (\is_array($aFetchResult) && isset($aFetchResult[0]['encpass']))
326
					{
327
						$sResult = $aFetchResult[0]['encpass'];
328
					}
329
				}
330
			}else{
331
				throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::CouldNotSaveNewPassword);
332
			}
333
			break;
334
		}
335
336
		return $sResult;
337
	}
338
}
339