MailcowChangePasswordDriver::ChangePassword()   C
last analyzed

Complexity

Conditions 15
Paths 110

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
nc 110
nop 3
dl 0
loc 54
rs 5.8333
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
class MailcowChangePasswordDriver implements \RainLoop\Providers\ChangePassword\ChangePasswordInterface
4
{
5
	/**
6
	 * @var string
7
	 */
8
	private $sDsn = '';
9
10
	/**
11
	 * @var string
12
	 */
13
	private $sUser = '';
14
15
	/**
16
	 * @var string
17
	 */
18
	private $sPassword = '';
19
20
	/**
21
	 * @var string
22
	 */
23
	private $sAllowedEmails = '';
24
25
	/**
26
	 * @var \MailSo\Log\Logger
27
	 */
28
	private $oLogger = null;
29
30
	/**
31
	 * @param string $sDsn
32
	 * @param string $sUser
33
	 * @param string $sPassword
34
	 *
35
	 * @return \IspConfigChangePasswordDriver
36
	 */
37
	public function SetConfig($sDsn, $sUser, $sPassword)
38
	{
39
		$this->sDsn = $sDsn;
40
		$this->sUser = $sUser;
41
		$this->sPassword = $sPassword;
42
43
		return $this;
44
	}
45
46
	/**
47
	 * @param string $sAllowedEmails
48
	 *
49
	 * @return \IspConfigChangePasswordDriver
50
	 */
51
	public function SetAllowedEmails($sAllowedEmails)
52
	{
53
		$this->sAllowedEmails = $sAllowedEmails;
54
		return $this;
55
	}
56
57
	/**
58
	 * @param \MailSo\Log\Logger $oLogger
59
	 *
60
	 * @return \IspConfigChangePasswordDriver
61
	 */
62
	public function SetLogger($oLogger)
63
	{
64
		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...
65
		{
66
			$this->oLogger = $oLogger;
67
		}
68
69
		return $this;
70
	}
71
72
	/**
73
	 * @param \RainLoop\Account $oAccount
74
	 *
75
	 * @return bool
76
	 */
77
	public function PasswordChangePossibility($oAccount)
78
	{
79
		return $oAccount && $oAccount->Email() &&
80
			\RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $this->sAllowedEmails);
81
	}
82
83
	/**
84
	 * @param \RainLoop\Account $oAccount
85
	 * @param string $sPrevPassword
86
	 * @param string $sNewPassword
87
	 *
88
	 * @return bool
89
	 */
90
	public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword)
91
	{
92
		if ($this->oLogger)
93
		{
94
			$this->oLogger->Write('Mailcow: Try to change password for '.$oAccount->Email());
95
		}
96
97
		$bResult = false;
98
		if (!empty($this->sDsn) && 0 < \strlen($this->sUser) && 0 < \strlen($this->sPassword) && $oAccount)
99
		{
100
			try
101
			{
102
				$oPdo = new \PDO($this->sDsn, $this->sUser, $this->sPassword);
103
				$oPdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
104
105
				$oStmt = $oPdo->prepare('SELECT password, username FROM mailbox WHERE username = ? LIMIT 1');
106
				if ($oStmt->execute(array($oAccount->IncLogin())))
107
				{
108
					$aFetchResult = $oStmt->fetchAll(\PDO::FETCH_ASSOC);
109
					if (\is_array($aFetchResult) && isset($aFetchResult[0]['password'], $aFetchResult[0]['username']))
110
					{
111
						$sDbPassword = $aFetchResult[0]['password'];
112
						if (\substr($sDbPassword, 0, 14) === '{SHA512-CRYPT}') {
113
							$sDbSalt = \substr($sDbPassword, 17, 16);
114
						} else {
115
							$sDbSalt = \substr($sDbPassword, 3, 16);
116
						}
117
118
						if ('{SHA512-CRYPT}'.\crypt($sPrevPassword, '$6$'.$sDbSalt) === $sDbPassword)
119
						{
120
							$oStmt = $oPdo->prepare('UPDATE mailbox SET password = ? WHERE username = ?');
121
							if ($oStmt->execute(array($this->cryptPassword($sNewPassword), $aFetchResult[0]['username']))) {
122
								$oStmt = $oPdo ->prepare('UPDATE users SET digesta1=MD5(CONCAT(?, ":SabreDAV:", ?)) WHERE username=?');
123
								if ($oStmt->execute(array($aFetchResult[0]['username'],$sNewPassword,$aFetchResult[0]['username']))) {
124
									//the MailCow & SabreDav have been updated, now update the doveadm password
125
									exec("/usr/bin/doveadm pw -s SHA512-CRYPT -p $sNewPassword", $hash, $return);
126
									$bResult = true;
127
								}
128
							}
129
						}
130
					}
131
				}
132
			}
133
			catch (\Exception $oException)
134
			{
135
				if ($this->oLogger)
136
				{
137
					$this->oLogger->WriteException($oException);
138
				}
139
			}
140
		}
141
142
		return $bResult;
143
	}
144
145
	/**
146
	 * @param string $sPassword
147
	 * @return string
148
	 */
149 View Code Duplication
	private function cryptPassword($sPassword)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
150
	{
151
		$sSalt = '';
152
		$sBase64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
153
154
		for ($iIndex = 0; $iIndex < 16; $iIndex++)
155
		{
156
			$sSalt .= $sBase64[\rand(0, 63)];
157
		}
158
159
		$crypted = \crypt($sPassword, '$6$'.$sSalt);
160
		return '{SHA512-CRYPT}'.$crypted;
161
	}
162
}
163