|
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) |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
if ($oAccount instanceof \RainLoop\Model\Account && \is_array($aImapCredentials)) |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
if ($oAccount instanceof \RainLoop\Model\Account && \is_array($aSmtpCredentials)) |
|
|
|
|
|
|
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
|
|
|
|
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.