Completed
Push — master ( 59c197...ef601c )
by Rain
20:42 queued 12:08
created

MailInABoxChangePasswordDriver::WriteLog()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * Mail-in-a-box Password Change Plugin
4
 *
5
 * Based on VirtualminChangePasswordDriver
6
 *
7
 * Author: Marius Gripsgard
8
 */
9
class MailInABoxChangePasswordDriver implements \RainLoop\Providers\ChangePassword\ChangePasswordInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11
	/**
12
	 * @var string
13
	 */
14
	private $sAllowedEmails = '';
15
	/**
16
	 * @var string
17
	 */
18
	private $sHost = '';
19
	/**
20
	 * @var string
21
	 */
22
	private $sAdminUser = '';
23
	/**
24
	 * @var string
25
	 */
26
	private $sAdminPassword = '';
27
	/**
28
	 * @var \MailSo\Log\Logger
29
	 */
30
	private $oLogger = null;
31
	/**
32
	 * @param string $sHost
33
	 * @param string $sAdminUser
34
	 * @param string $sAdminPassword
35
	 *
36
	 * @return \MailInABoxChangePasswordDriver
37
	 */
38
	public function SetConfig($sHost, $sAdminUser, $sAdminPassword)
39
	{
40
		$this->sHost = $sHost;
41
		$this->sAdminUser = $sAdminUser;
42
		$this->sAdminPassword = $sAdminPassword;
43
		return $this;
44
	}
45
	/**
46
	 * @param string $sAllowedEmails
47
	 *
48
	 * @return \MailInABoxChangePasswordDriver
49
	 */
50
	public function SetAllowedEmails($sAllowedEmails)
51
	{
52
		$this->sAllowedEmails = $sAllowedEmails;
53
		return $this;
54
	}
55
	/**
56
	 * @param \MailSo\Log\Logger $oLogger
57
	 *
58
	 * @return \MailInABoxChangePasswordDriver
59
	 */
60
	public function SetLogger($oLogger)
61
	{
62
		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...
63
		{
64
			$this->oLogger = $oLogger;
65
		}
66
		return $this;
67
	}
68
	/**
69
	 * @param string $sDesc
70
	 * @param int $iType = \MailSo\Log\Enumerations\Type::INFO
71
	 *
72
	 * @return \MailInABoxChangePasswordDriver
73
	 */
74
	public function WriteLog($sDesc, $iType = \MailSo\Log\Enumerations\Type::INFO)
75
	{
76
		if ($this->oLogger)
77
		{
78
			$this->oLogger->Write($sDesc, $iType);
79
		}
80
		return $this;
81
	}
82
	/**
83
	 * @param \RainLoop\Model\Account $oAccount
84
	 *
85
	 * @return bool
86
	 */
87
	public function PasswordChangePossibility($oAccount)
88
	{
89
		return $oAccount && $oAccount->Email() &&
90
			\RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $this->sAllowedEmails);
91
	}
92
	/**
93
	 * @param \RainLoop\Model\Account $oAccount
94
	 * @param string $sPrevPassword
95
	 * @param string $sNewPassword
96
	 *
97
	 * @return bool
98
	 */
99
	public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword)
100
	{
101
		$this->WriteLog('Mail-in-a-box: Try to change password for '.$oAccount->Email());
102
		$bResult = false;
103
		if (!empty($this->sHost) && !empty($this->sAdminUser) && !empty($this->sAdminPassword) && $oAccount)
104
		{
105
			$this->WriteLog('Mail-in-a-box:[Check] Required Fields Present');
106
			$sEmail = \trim(\strtolower($oAccount->Email()));
107
			$sHost = \rtrim(\trim($this->sHost), '/');
108
			$sUrl = $sHost.'/admin/mail/users/password';
109
110
			$sAdminUser = $this->sAdminUser;
111
			$sAdminPassword = $this->sAdminPassword;
112
			$iCode = 0;
0 ignored issues
show
Unused Code introduced by
$iCode 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...
113
			$aPost = array(
114
				'email'		=> $sEmail,
115
				'password'		=> $sNewPassword,
116
			);
117
			$aOptions = array(
118
				CURLOPT_URL => $sUrl,
119
				CURLOPT_HEADER => false,
120
				CURLOPT_FAILONERROR => true,
121
				CURLOPT_SSL_VERIFYPEER => false,
122
				CURLOPT_RETURNTRANSFER => true,
123
				CURLOPT_POST => true,
124
				CURLOPT_POSTFIELDS => \http_build_query($aPost, '', '&'),
125
				CURLOPT_TIMEOUT => 20,
126
				CURLOPT_SSL_VERIFYHOST => false,
127
				CURLOPT_USERPWD => $sAdminUser.':'.$sAdminPassword,
128
				CURLOPT_HTTPAUTH => CURLAUTH_BASIC
129
			);
130
			$oCurl = \curl_init();
131
			\curl_setopt_array($oCurl, $aOptions);
132
			$this->WriteLog('Mail-in-a-box: Send post request: '.$sUrl);
133
			$mResult = \curl_exec($oCurl);
134
			$iCode = (int) \curl_getinfo($oCurl, CURLINFO_HTTP_CODE);
135
			$sContentType = (string) \curl_getinfo($oCurl, CURLINFO_CONTENT_TYPE);
136
			$this->WriteLog('Mail-in-a-box: Post request result: (Status: '.$iCode.', ContentType: '.$sContentType.')');
137 View Code Duplication
			if (false === $mResult || 200 !== $iCode)
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...
138
			{
139
				$this->WriteLog('Mail-in-a-box: Error: '.\curl_error($oCurl), \MailSo\Log\Enumerations\Type::WARNING);
140
			}
141
			if (\is_resource($oCurl))
142
			{
143
				\curl_close($oCurl);
144
			}
145
			if (false !== $mResult && 200 === $iCode)
146
			{
147
				$this->WriteLog('Mail-in-a-box: Password Change Status: Success');
148
				$bResult = true;
149
			}
150
			else
151
			{
152
				$this->WriteLog('Mail-in-a-box[Error]: Empty Response: Code: '.$iCode);
153
			}
154
		}
155
		return $bResult;
156
	}
157
}
158