Completed
Push — master ( 62e6fb...2faf41 )
by Rain
02:54 queued 01:22
created

AutoDomainGrabPlugin::FilterImapCredentials()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 20

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
cc 7
nc 4
nop 2
dl 20
loc 20
rs 8.6666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This plug-in automatically detects the IMAP and SMTP settings by extracting them from the email address itself.
5
 * For example, user inputs: "[email protected]"
6
 * This plugin sets the IMAP and SMTP host to "example.com" upon login, and then connects to it.
7
 *
8
 * Based on:
9
 * https://github.com/RainLoop/rainloop-webmail/blob/master/plugins/override-smtp-credentials/index.php
10
 * 
11
 */
12
13
class AutoDomainGrabPlugin extends \RainLoop\Plugins\AbstractPlugin
14
{
15
	
16
	private $imap_prefix = "mail.";
17
	private $smtp_prefix = "mail.";
18
	
19
	public function Init()
20
	{
21
		$this->addHook('filter.smtp-credentials', 'FilterSmtpCredentials');
22
		$this->addHook('filter.imap-credentials', 'FilterImapCredentials');
23
	}
24
25
	/**
26
	 * This function detects the IMAP Host, and if it is set to "auto", replaces it with the MX or email domain.
27
	 *
28
	 * @param \RainLoop\Model\Account $oAccount
29
	 * @param array $aImapCredentials
30
	 */
31 View Code Duplication
	public function FilterImapCredentials($oAccount, &$aImapCredentials)
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...
32
	{
33
		if ($oAccount instanceof \RainLoop\Model\Account && \is_array($aImapCredentials))
0 ignored issues
show
Bug introduced by
The class RainLoop\Model\Account 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...
34
		{
35
			// Check for mail.$DOMAIN as entered value in RL settings
36
			if (!empty($aImapCredentials['Host']) && 'auto' === $aImapCredentials['Host'])
37
			{
38
				$domain = substr(strrchr($oAccount->Email(), "@"), 1);
39
				$mxhosts = array();
40
				if(getmxrr($domain, $mxhosts) && sizeof($mxhosts) > 0)
41
				{
42
					$aImapCredentials['Host'] = $mxhosts[0];
43
				}
44
				else 
45
				{
46
					$aImapCredentials['Host'] = $this->imap_prefix.$domain;
47
				}
48
			}
49
		}
50
	}
51
52
	/**
53
	 * This function detects the SMTP Host, and if it is set to "auto", replaces it with the MX or email domain.
54
	 *
55
	 * @param \RainLoop\Model\Account $oAccount
56
	 * @param array $aSmtpCredentials
57
	 */
58 View Code Duplication
	public function FilterSmtpCredentials($oAccount, &$aSmtpCredentials)
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...
59
	{
60
		if ($oAccount instanceof \RainLoop\Model\Account && \is_array($aSmtpCredentials))
0 ignored issues
show
Bug introduced by
The class RainLoop\Model\Account 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...
61
		{
62
			// Check for mail.$DOMAIN as entered value in RL settings
63
			if (!empty($aSmtpCredentials['Host']) && 'auto' === $aSmtpCredentials['Host'])
64
			{
65
				$domain = substr(strrchr($oAccount->Email(), "@"), 1);
66
				$mxhosts = array();
67
				if(getmxrr($domain, $mxhosts) && sizeof($mxhosts) > 0)
68
				{
69
					$aSmtpCredentials['Host'] = $mxhosts[0];
70
				} 
71
				else 
72
				{
73
					$aSmtpCredentials['Host'] = $this->smtp_prefix.$domain;
74
				}
75
			}
76
		}
77
	}
78
}
79