ChangePasswordPostfixAdminDriver   A
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 340
Duplicated Lines 5.59 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 19
loc 340
rs 9.28
c 0
b 0
f 0
wmc 39
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
B ChangePassword() 19 60 9
C cryptPassword() 0 56 14

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
class ChangePasswordPostfixAdminDriver implements \RainLoop\Providers\ChangePassword\ChangePasswordInterface
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 View Code Duplication
				  	case 'PostgreSQL':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
240
				 		$sDsn = 'pgsql:host='.$this->sHost.';port='.$this->iPort.';dbname='.$this->sDatabase;
241
						break;
242 View Code Duplication
				  	default:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
				if (0 < \strlen($sUpdatePassword))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
		if (function_exists('random_bytes')) {
290
			$sSalt = substr(base64_encode(random_bytes(32)), 0, 16);
291
		} else {
292
			$sSalt = substr(str_shuffle('./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'), 0, 16);	
293
		}
294
		switch (strtolower($this->sEncrypt))
295
		{
296
			default:
297
			case 'plain':
298
			case 'cleartext':
299
				$sResult = '{PLAIN}' . $sPassword;
300
				break;
301
302
			case 'md5crypt':
303
				include_once __DIR__.'/md5crypt.php';
304
				$sResult = '{MD5-CRYPT}' . md5crypt($sPassword);
305
				break;
306
307
			case 'md5':
308
				$sResult = '{PLAIN-MD5}' . md5($sPassword);
309
				break;
310
311
			case 'system':
312
				$sResult = '{CRYPT}' . crypt($sPassword);
313
				break;
314
315
			case 'sha256-crypt':
316
				$sResult = '{SHA256-CRYPT}' . crypt($sPassword,'$5$'.$sSalt);
317
				break;
318
319
			case 'sha512-crypt':
320
				$sResult = '{SHA512-CRYPT}' . crypt($sPassword,'$6$'.$sSalt);
321
				break;
322
323
			case 'mysql_encrypt':
324
			  if($this->sEngine == 'MySQL'){
325
			  	$oStmt = $oPdo->prepare('SELECT ENCRYPT(?) AS encpass');
326
				if ($oStmt->execute(array($sPassword)))
327
				{
328
					$aFetchResult = $oStmt->fetchAll(\PDO::FETCH_ASSOC);
329
					if (\is_array($aFetchResult) && isset($aFetchResult[0]['encpass']))
330
					{
331
						$sResult = $aFetchResult[0]['encpass'];
332
					}
333
				}
334
			}else{
335
				throw new \RainLoop\Exceptions\ClientException(\RainLoop\Notifications::CouldNotSaveNewPassword);
336
			}
337
			break;
338
		}
339
340
		return $sResult;
341
	}
342
}
343