ChangePasswordLdapDriver::ChangePassword()   F
last analyzed

Complexity

Conditions 23
Paths 1182

Size

Total Lines 118

Duplication

Lines 34
Ratio 28.81 %

Importance

Changes 0
Metric Value
cc 23
nc 1182
nop 3
dl 34
loc 118
rs 0
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 ChangePasswordLdapDriver implements \RainLoop\Providers\ChangePassword\ChangePasswordInterface
4
{
5
	/**
6
	 * @var string
7
	 */
8
	private $sHostName = '127.0.0.1';
9
10
	/**
11
	 * @var int
12
	 */
13
	private $iHostPort = 389;
14
15
	/**
16
	 * @var string
17
	 */
18
	private $sUserDnFormat = '';
19
20
	/**
21
	 * @var string
22
	 */
23
	private $sPasswordField = 'userPassword';
24
25
	/**
26
	 * @var string
27
	 */
28
	private $sPasswordEncType = 'SHA';
29
30
	/**
31
	 * @var \MailSo\Log\Logger
32
	 */
33
	private $oLogger = null;
34
35
	/**
36
	 * @var string
37
	 */
38
	private $sAllowedEmails = '';
39
40
	/**
41
	 * @param string $sHostName
42
	 * @param int $iHostPort
43
	 * @param string $sUserDnFormat
44
	 * @param string $sPasswordField
45
	 * @param string $sPasswordEncType
46
	 *
47
	 * @return \ChangePasswordLdapDriver
48
	 */
49
	public function SetConfig($sHostName, $iHostPort, $sUserDnFormat, $sPasswordField, $sPasswordEncType)
50
	{
51
		$this->sHostName = $sHostName;
52
		$this->iHostPort = $iHostPort;
53
		$this->sUserDnFormat = $sUserDnFormat;
54
		$this->sPasswordField = $sPasswordField;
55
		$this->sPasswordEncType = $sPasswordEncType;
56
57
		return $this;
58
	}
59
60
	/**
61
	 * @param string $sAllowedEmails
62
	 *
63
	 * @return \ChangePasswordLdapDriver
64
	 */
65
	public function SetAllowedEmails($sAllowedEmails)
66
	{
67
		$this->sAllowedEmails = $sAllowedEmails;
68
69
		return $this;
70
	}
71
72
	/**
73
	 * @param \MailSo\Log\Logger $oLogger
74
	 *
75
	 * @return \ChangePasswordLdapDriver
76
	 */
77
	public function SetLogger($oLogger)
78
	{
79
		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...
80
		{
81
			$this->oLogger = $oLogger;
82
		}
83
84
		return $this;
85
	}
86
87
	/**
88
	 * @param \RainLoop\Account $oAccount
89
	 *
90
	 * @return bool
91
	 */
92
	public function PasswordChangePossibility($oAccount)
93
	{
94
		return $oAccount && $oAccount->Email() &&
95
			\RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $this->sAllowedEmails);
96
	}
97
98
	/**
99
	 * @param \RainLoop\Model\Account $oAccount
100
	 * @param string $sPrevPassword
101
	 * @param string $sNewPassword
102
	 *
103
	 * @return bool
104
	 */
105
	public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword)
106
	{
107
		$bResult = false;
108
109
		try
110
		{
111
			$sDomain = \MailSo\Base\Utils::GetDomainFromEmail($oAccount->Email());
112
			$sUserDn = \strtr($this->sUserDnFormat, array(
113
				'{domain}' => $sDomain,
114
				'{domain:dc}' => 'dc='.\strtr($sDomain, array('.' => ',dc=')),
115
				'{email}' => $oAccount->Email(),
116
				'{email:user}' => \MailSo\Base\Utils::GetAccountNameFromEmail($oAccount->Email()),
117
				'{email:domain}' => $sDomain,
118
				'{login}' => $oAccount->Login(),
119
				'{imap:login}' => $oAccount->Login(),
120
				'{imap:host}' => $oAccount->DomainIncHost(),
121
				'{imap:port}' => $oAccount->DomainIncPort(),
122
				'{gecos}' => function_exists('posix_getpwnam') ? posix_getpwnam($oAccount->Login()) : ''
123
			));
124
125
			$oCon = @\ldap_connect($this->sHostName, $this->iHostPort);
126
			if ($oCon)
127
			{
128
				if (!@\ldap_set_option($oCon, LDAP_OPT_PROTOCOL_VERSION, 3))
129
				{
130
					$this->oLogger->Write(
131
						'Failed to set LDAP Protocol version to 3, TLS not supported.',
132
						\MailSo\Log\Enumerations\Type::WARNING,
133
						'LDAP'
134
					);
135
				}
136
				else if (@!ldap_start_tls($oCon))
137
				{
138
					$this->oLogger->Write("ldap_start_tls failed: ".$oCon, \MailSo\Log\Enumerations\Type::WARNING, 'LDAP');
139
				}
140
141 View Code Duplication
				if (!@\ldap_bind($oCon, $sUserDn, $sPrevPassword))
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...
142
				{
143
					if ($this->oLogger)
144
					{
145
						$sError = $oCon ? @\ldap_error($oCon) : '';
146
						$iErrno = $oCon ? @\ldap_errno($oCon) : 0;
147
148
						$this->oLogger->Write('ldap_bind error: '.$sError.' ('.$iErrno.')',
149
							\MailSo\Log\Enumerations\Type::WARNING, 'LDAP');
150
					}
151
152
					return false;
153
				}
154
			}
155
			else
156
			{
157
				return false;
158
			}
159
160
			$sSshaSalt = '';
161
			$sShaPrefix = '{SHA}';
162
			$sEncodedNewPassword = $sNewPassword;
163
			switch (\strtolower($this->sPasswordEncType))
164
			{
165
				case 'ssha':
166
					$sSshaSalt = $this->getSalt(4);
167
					$sShaPrefix = '{SSHA}';
168
				case 'sha':
169
					switch (true)
170
					{
171
						default:
172 View Code Duplication
						case \function_exists('sha1'):
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...
173
							$sEncodedNewPassword = $sShaPrefix.\base64_encode(\sha1($sNewPassword.$sSshaSalt, true).$sSshaSalt);
174
							break;
175 View Code Duplication
						case \function_exists('hash'):
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...
176
							$sEncodedNewPassword = $sShaPrefix.\base64_encode(\hash('sha1', $sNewPassword, true).$sSshaSalt);
177
							break;
178
						case \function_exists('mhash') && defined('MHASH_SHA1'):
179
							$sEncodedNewPassword = $sShaPrefix.\base64_encode(\mhash(MHASH_SHA1, $sNewPassword).$sSshaSalt);
180
							break;
181
					}
182
					break;
183
				case 'md5':
184
					$sEncodedNewPassword = '{MD5}'.\base64_encode(\pack('H*', \md5($sNewPassword)));
185
					break;
186
				case 'crypt':
187
					$sEncodedNewPassword = '{CRYPT}'.\crypt($sNewPassword, $this->getSalt(2));
188
					break;
189
			}
190
191
			$aEntry = array();
192
			$aEntry[$this->sPasswordField] = (string) $sEncodedNewPassword;
193
194 View Code Duplication
			if (!!@\ldap_modify($oCon, $sUserDn, $aEntry))
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...
195
			{
196
				$bResult = true;
197
			}
198
			else
199
			{
200
				if ($this->oLogger)
201
				{
202
					$sError = $oCon ? @\ldap_error($oCon) : '';
203
					$iErrno = $oCon ? @\ldap_errno($oCon) : 0;
204
205
					$this->oLogger->Write('ldap_modify error: '.$sError.' ('.$iErrno.')',
206
						\MailSo\Log\Enumerations\Type::WARNING, 'LDAP');
207
				}
208
			}
209
		}
210
		catch (\Exception $oException)
211
		{
212
			if ($this->oLogger)
213
			{
214
				$this->oLogger->WriteException($oException,
215
					\MailSo\Log\Enumerations\Type::WARNING, 'LDAP');
216
			}
217
218
			$bResult = false;
219
		}
220
221
		return $bResult;
222
	}
223
224
	/**
225
	 * @param int $iLength
226
	 *
227
	 * @return string
228
	 */
229
	private function getSalt($iLength)
230
	{
231
		$sChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
232
		$iCharsLength = \strlen($sChars);
233
234
		$sResult = '';
235
		while (\strlen($sResult) < $iLength)
236
		{
237
			$sResult .= \substr($sChars, \rand() % $iCharsLength, 1);
238
		}
239
240
		return $sResult;
241
	}
242
}
243