IspConfigChangePasswordDriver::SetAllowedEmails()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
class IspConfigChangePasswordDriver 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\Model\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\Model\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('ISP: 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, mailuser_id FROM mail_user WHERE login = ? 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]['mailuser_id']))
110
					{
111
						$sDbPassword = \stripslashes($aFetchResult[0]['password']);
112
						$sDbSalt = '$1$'.\substr($sDbPassword, 3, 8).'$';
113
114
						if (\crypt(\stripslashes($sPrevPassword), $sDbSalt) === $sDbPassword)
115
						{
116
							$oStmt = $oPdo->prepare('UPDATE mail_user SET password = ? WHERE mailuser_id = ?');
117
							$bResult = (bool) $oStmt->execute(
118
								array($this->cryptPassword($sNewPassword), $aFetchResult[0]['mailuser_id']));
119
						}
120
					}
121
				}
122
			}
123
			catch (\Exception $oException)
124
			{
125
				if ($this->oLogger)
126
				{
127
					$this->oLogger->WriteException($oException);
128
				}
129
			}
130
		}
131
132
		return $bResult;
133
	}
134
135
	/**
136
	 * @param string $sPassword
137
	 * @return string
138
	 */
139 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...
140
	{
141
		$sSalt = '';
142
		$sBase64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
143
144
		for ($iIndex = 0; $iIndex < 8; $iIndex++)
145
		{
146
			$sSalt .= $sBase64[\rand(0, 63)];
147
		}
148
149
		return \crypt($sPassword, '$1$'.$sSalt.'$');
150
	}
151
}