ChangePasswordVpopmailDriver   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 226
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 226
rs 10
c 0
b 0
f 0
wmc 27
lcom 2
cbo 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A SetmHost() 0 5 1
A SetmUser() 0 5 1
A SetmPass() 0 5 1
A SetmDatabase() 0 5 1
A SetmTable() 0 5 1
A SetmColumn() 0 5 1
A SetLogger() 0 9 2
A SetAllowedDomains() 0 9 3
A PasswordChangePossibility() 0 5 3
C ChangePassword() 0 69 13
1
<?php
2
3
class ChangePasswordVpopmailDriver implements \RainLoop\Providers\ChangePassword\ChangePasswordInterface
4
{
5
	/**
6
	 * @var string
7
	 */
8
	private $mHost = 'localhost';
9
10
	/**
11
	 * @var string
12
	 */
13
	private $mUser = '';
14
15
	/**
16
	 * @var string
17
	 */
18
	private $mPass = '';
19
20
	/**
21
	 * @var string
22
	 */
23
	private $mDatabase = '';
24
25
	/**
26
	 * @var string
27
	 */
28
	private $mTable = '';
29
30
	/**
31
	 * @var string
32
	 */
33
	private $mColumn = '';
34
35
	/**
36
	 * @var \MailSo\Log\Logger
37
	 */
38
	private $oLogger = null;
39
40
	/**
41
	 * @var array
42
	 */
43
	private $aDomains = array();
44
45
	/**
46
	 * @param string $mHost
47
	 *
48
	 * @return \ChangePasswordVpopmailDriver
49
	 */
50
	public function SetmHost($mHost)
51
	{
52
		$this->mHost = $mHost;
53
		return $this;
54
	}
55
56
	/**
57
	 * @param string $mUser
58
	 *
59
	 * @return \ChangePasswordVpopmailDriver
60
	 */
61
	public function SetmUser($mUser)
62
	{
63
		$this->mUser = $mUser;
64
		return $this;
65
	}
66
67
	/**
68
	 * @param string $mPass
69
	 *
70
	 * @return \ChangePasswordVpopmailDriver
71
	 */
72
	public function SetmPass($mPass)
73
	{
74
		$this->mPass = $mPass;
75
		return $this;
76
	}
77
78
	/**
79
	 * @param string $mDatabase
80
	 *
81
	 * @return \ChangePasswordVpopmailDriver
82
	 */
83
	public function SetmDatabase($mDatabase)
84
	{
85
		$this->mDatabase = $mDatabase;
86
		return $this;
87
	}
88
89
	/**
90
	 * @param string $mTable
91
	 *
92
	 * @return \ChangePasswordVpopmailDriver
93
	 */
94
	public function SetmTable($mTable)
95
	{
96
		$this->mTable = $mTable;
97
		return $this;
98
	}
99
100
	/**
101
	 * @param string $mColumn
102
	 *
103
	 * @return \ChangePasswordVpopmailDriver
104
	 */
105
	public function SetmColumn($mColumn)
106
	{
107
		$this->mColumn = $mColumn;
108
		return $this;
109
	}
110
111
	/**
112
	 * @param \MailSo\Log\Logger $oLogger
113
	 *
114
	 * @return \ChangePasswordVpopmailDriver
115
	 */
116
	public function SetLogger($oLogger)
117
	{
118
		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...
119
		{
120
			$this->oLogger = $oLogger;
121
		}
122
123
		return $this;
124
	}
125
126
	/**
127
	 * @param array $aDomains
128
	 *
129
	 * @return bool
130
	 */
131
	public function SetAllowedDomains($aDomains)
132
	{
133
		if (\is_array($aDomains) && 0 < \count($aDomains))
134
		{
135
			$this->aDomains = $aDomains;
136
		}
137
138
		return $this;
139
	}
140
141
	/**
142
	 * @param \RainLoop\Account $oAccount
143
	 *
144
	 * @return bool
145
	 */
146
	public function PasswordChangePossibility($oAccount)
147
	{
148
		return 0 === \count($this->aDomains) || ($oAccount && \in_array(\strtolower(
149
			\MailSo\Base\Utils::GetDomainFromEmail($oAccount->Email)), $this->aDomains));
150
	}
151
152
	/**
153
	 * @param \RainLoop\Account $oAccount
154
	 * @param string $sPrevPassword
155
	 * @param string $sNewPassword
156
	 *
157
	 * @return bool
158
	 */
159
	public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword)
160
	{
161
		if ($this->oLogger)
162
		{
163
			$this->oLogger->Write('Try to change password for '.$oAccount->Email());
164
		}
165
166
		if (empty($this->mHost) || empty($this->mDatabase) || empty($this->mColumn) || empty($this->mTable))
167
		{
168
			return false;
169
		}
170
171
		$bResult = false;
0 ignored issues
show
Unused Code introduced by
$bResult 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...
172
173
		$sDsn = 'mysql:host='.$this->mHost.';dbname='.$this->mDatabase.';charset=utf8';
174
		$aOptions = array(
175
			PDO::ATTR_EMULATE_PREPARES  => false,
176
			PDO::ATTR_PERSISTENT        => true,
177
			PDO::ATTR_ERRMODE           => PDO::ERRMODE_EXCEPTION
178
		);
179
180
		$sLoginPart = \MailSo\Base\Utils::GetAccountNameFromEmail($oAccount->Email());
181
		$sDomainPart = \MailSo\Base\Utils::GetDomainFromEmail($oAccount->Email());
182
183
		try
184
		{
185
			$oConn = new PDO($sDsn, $this->mUser, $this->mPass, $aOptions);
186
187
			$oSelect = $oConn->prepare('SELECT '.$this->mColumn.' FROM '.$this->mTable.' WHERE pw_name=? AND pw_domain=? LIMIT 1');
188
			$oSelect->execute(array($sLoginPart, $sDomainPart));
189
190
			$aColCrypt = $oSelect->fetchAll(PDO::FETCH_ASSOC);
191
192
			$sCryptPass = isset($aColCrypt[0][$this->mColumn]) ? $aColCrypt[0][$this->mColumn] : '';
193
			if (0 < \strlen($sCryptPass) && \crypt($sPrevPassword, $sCryptPass) === $sCryptPass)
194
			{
195
				$oUpdate = $oConn->prepare('UPDATE '.$this->mTable.' SET '.$this->mColumn.'=ENCRYPT(?,concat("$1$",right(md5(rand()), 8 ),"$")), pw_clear_passwd=\'\' WHERE pw_name=? AND pw_domain=?');
196
				$oUpdate->execute(array(
197
					$sNewPassword,
198
					$sLoginPart,
199
					$sDomainPart
200
				));
201
202
				$bResult = true;
203
				if ($this->oLogger)
204
				{
205
					$this->oLogger->Write('Success! Password changed.');
206
				}
207
			}
208
			else
209
			{
210
				$bResult = false;
211
				if ($this->oLogger)
212
				{
213
					$this->oLogger->Write('Something went wrong. Either current password is incorrect, or new password does not match criteria.');
214
				}
215
			}
216
		}
217
		catch (\Exception $oException)
218
		{
219
			$bResult = false;
220
			if ($this->oLogger)
221
			{
222
				$this->oLogger->WriteException($oException);
223
			}
224
		}
225
226
		return $bResult;
227
	}
228
}
229