1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Bernhard Scheirle <[email protected]> |
5
|
|
|
* @author Christoph Wurst <[email protected]> |
6
|
|
|
* |
7
|
|
|
* Mail |
8
|
|
|
* |
9
|
|
|
* This code is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
11
|
|
|
* as published by the Free Software Foundation. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
namespace OCA\Mail\Service\AutoConfig; |
23
|
|
|
|
24
|
|
|
use Horde_Mail_Transport_Smtphorde; |
25
|
|
|
use OCP\Security\ICrypto; |
26
|
|
|
use OCA\Mail\Account; |
27
|
|
|
use OCA\Mail\Db\MailAccount; |
28
|
|
|
use OCA\Mail\Service\Logger; |
29
|
|
|
|
30
|
|
|
class AutoConfig { |
31
|
|
|
|
32
|
|
|
/** @var Logger */ |
33
|
|
|
private $logger; |
34
|
|
|
|
35
|
|
|
/** @var string */ |
36
|
|
|
private $userId; |
37
|
|
|
|
38
|
|
|
/** @var ICrypto */ |
39
|
|
|
private $crypto; |
40
|
|
|
|
41
|
|
|
/** @var IspDb */ |
42
|
|
|
private $ispDb; |
43
|
|
|
|
44
|
|
|
/** @var MxRecord */ |
45
|
|
|
private $mxRecord; |
46
|
|
|
|
47
|
|
|
/** @var ImapConnectivityTester */ |
48
|
|
|
private $imapConnectivityTester; |
49
|
|
|
|
50
|
|
|
/** @var ImapServerDetector */ |
51
|
|
|
private $imapServerDetector; |
52
|
|
|
|
53
|
|
|
/** @var ImapConnector */ |
54
|
|
|
private $imapConnector; |
55
|
|
|
|
56
|
|
|
/** @var SmtpConnectivityTester */ |
57
|
|
|
private $smtpConnectivityTester; |
58
|
|
|
|
59
|
|
|
/** @var SmtpServerDetector */ |
60
|
|
|
private $smtpServerDetector; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* |
64
|
|
|
* @param Logger $logger |
65
|
|
|
* @param string $UserId |
66
|
|
|
* @param IspDb $ispDb |
67
|
|
|
* @param MxRecord $mxRecord |
68
|
|
|
* @param ImapConnectivityTester $imapTester |
69
|
|
|
* @param ImapServerDetector $imapDetector |
70
|
|
|
* @param SmtpConnectivityTester $smtpTester |
71
|
|
|
* @param SmtpServerDetector $smtpDetector |
72
|
|
|
* @param ImapConnector $imapConnector |
73
|
|
|
* @param ICrypto $crypto |
74
|
|
|
*/ |
75
|
|
|
public function __construct(Logger $logger, $UserId, |
76
|
|
|
IspDb $ispDb, MxRecord $mxRecord, |
77
|
|
|
ImapConnectivityTester $imapTester, ImapServerDetector $imapDetector, |
78
|
|
|
SmtpConnectivityTester $smtpTester, SmtpServerDetector $smtpDetector, |
79
|
|
|
ImapConnector $imapConnector, ICrypto $crypto) { |
80
|
|
|
$this->logger = $logger; |
81
|
|
|
$this->userId = $UserId; |
82
|
|
|
$this->crypto = $crypto; |
83
|
|
|
$this->ispDb = $ispDb; |
84
|
|
|
$this->mxRecord = $mxRecord; |
85
|
|
|
$this->imapConnectivityTester = $imapTester; |
86
|
|
|
$this->imapServerDetector = $imapDetector; |
87
|
|
|
$this->imapConnector = $imapConnector; |
88
|
|
|
$this->smtpConnectivityTester = $smtpTester; |
89
|
|
|
$this->smtpServerDetector = $smtpDetector; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $email |
94
|
|
|
* @param string $password |
95
|
|
|
* @param string $name |
96
|
|
|
* @return null|MailAccount |
97
|
|
|
*/ |
98
|
|
|
public function createAutoDetected($email, $password, $name) { |
99
|
|
|
|
100
|
|
|
// splitting the email address into user and host part |
101
|
|
|
// TODO: use horde libs for email address parsing |
102
|
|
|
list(, $host) = explode("@", $email); |
103
|
|
|
|
104
|
|
|
$ispdb = $this->ispDb->query($host); |
105
|
|
|
if (!empty($ispdb)) { |
106
|
|
|
$account = null; |
107
|
|
|
if (isset($ispdb['imap'])) { |
108
|
|
|
foreach ($ispdb['imap'] as $imap) { |
109
|
|
|
$host = $imap['hostname']; |
110
|
|
|
$port = $imap['port']; |
111
|
|
|
$encryptionProtocol = null; |
112
|
|
|
if ($imap['socketType'] === 'SSL') { |
113
|
|
|
$encryptionProtocol = 'ssl'; |
114
|
|
|
} |
115
|
|
|
if ($imap['socketType'] === 'STARTTLS') { |
116
|
|
|
$encryptionProtocol = 'tls'; |
117
|
|
|
} |
118
|
|
View Code Duplication |
if ($imap['username'] === '%EMAILADDRESS%') { |
|
|
|
|
119
|
|
|
$user = $email; |
120
|
|
|
} elseif ($imap['username'] === '%EMAILLOCALPART%') { |
121
|
|
|
list($user, ) = explode("@", $email); |
122
|
|
|
} else { |
123
|
|
|
$this->logger->info("Unknown username variable: " . $imap['username']); |
124
|
|
|
return null; |
125
|
|
|
} |
126
|
|
|
try { |
127
|
|
|
$account = $this->imapConnector->connect($email, $password, $name, $host, |
128
|
|
|
$port, $encryptionProtocol, $user); |
129
|
|
|
break; |
130
|
|
|
} catch (\Horde_Imap_Client_Exception $e) { |
|
|
|
|
131
|
|
|
$error = $e->getMessage(); |
132
|
|
|
$this->logger->info("Test-Account-Failed: $this->userId, $host, $port, $user, $encryptionProtocol -> $error"); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
if (!is_null($account)) { |
137
|
|
|
foreach ($ispdb['smtp'] as $smtp) { |
138
|
|
|
try { |
139
|
|
View Code Duplication |
if ($smtp['username'] === '%EMAILADDRESS%') { |
|
|
|
|
140
|
|
|
$user = $email; |
141
|
|
|
} elseif ($smtp['username'] === '%EMAILLOCALPART%') { |
142
|
|
|
list($user, ) = explode("@", $email); |
143
|
|
|
} else { |
144
|
|
|
$this->logger->info("Unknown username variable: " . $smtp['username']); |
145
|
|
|
return null; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$account->setOutboundHost($smtp['hostname']); |
149
|
|
|
$account->setOutboundPort($smtp['port']); |
150
|
|
|
$password = $this->crypto->encrypt($password); |
151
|
|
|
$account->setOutboundPassword($password); |
152
|
|
|
$account->setOutboundUser($user); |
153
|
|
|
$account->setOutboundSslMode(strtolower($smtp['socketType'])); |
154
|
|
|
|
155
|
|
|
$a = new Account($account); |
156
|
|
|
$smtp = $a->createTransport(); |
157
|
|
|
if ($smtp instanceof Horde_Mail_Transport_Smtphorde) { |
|
|
|
|
158
|
|
|
$smtp->getSMTPObject(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
break; |
162
|
|
|
} catch (\PEAR_Exception $ex) { |
|
|
|
|
163
|
|
|
$error = $ex->getMessage(); |
164
|
|
|
$this->logger->info("Test-Account-Failed(smtp): $error"); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
return $account; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$account = $this->detectImapAndSmtp($email, $password, $name); |
172
|
|
|
if (!is_null($account)) { |
173
|
|
|
return $account; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return null; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param $email |
181
|
|
|
* @param $password |
182
|
|
|
* @param $name |
183
|
|
|
* @return null|MailAccount |
184
|
|
|
*/ |
185
|
|
|
private function detectImapAndSmtp($email, $password, $name) { |
186
|
|
|
$account = $this->imapServerDetector->detect($email, $password, $name); |
187
|
|
|
if (is_null($account)) { |
188
|
|
|
return null; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$this->smtpServerDetector->detect($account, $email, $password); |
192
|
|
|
|
193
|
|
|
return $account; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
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.