1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Christoph Wurst <[email protected]> |
5
|
|
|
* |
6
|
|
|
* Mail |
7
|
|
|
* |
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
10
|
|
|
* as published by the Free Software Foundation. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace OCA\Mail\Service\AutoConfig; |
23
|
|
|
|
24
|
|
|
use OCP\Security\ICrypto; |
25
|
|
|
use OCA\Mail\Account; |
26
|
|
|
use OCA\Mail\Db\MailAccount; |
27
|
|
|
use OCA\Mail\Service\Logger; |
28
|
|
|
|
29
|
|
|
class SmtpConnectivityTester extends ConnectivityTester { |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ICrypto |
33
|
|
|
*/ |
34
|
|
|
private $crypto; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $userId; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param ICrypto $crypto |
43
|
|
|
* @param Logger $logger |
44
|
|
|
* @param string $UserId |
45
|
|
|
*/ |
46
|
|
|
public function __construct(ICrypto $crypto, Logger $logger, $UserId) { |
47
|
|
|
parent::__construct($logger); |
48
|
|
|
$this->crypto = $crypto; |
49
|
|
|
$this->userId = $UserId; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param MailAccount $account |
54
|
|
|
* @param $host |
55
|
|
|
* @param $users |
56
|
|
|
* @param $password |
57
|
|
|
* @param bool $withHostPrefix |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
public function test(MailAccount $account, $host, $users, $password, |
61
|
|
|
$withHostPrefix = false) { |
62
|
|
|
if (!is_array($users)) { |
63
|
|
|
$users = [$users]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// port 25 should be the last one to test |
67
|
|
|
$ports = [587, 465, 25]; |
68
|
|
|
$protocols = ['ssl', 'tls', null]; |
69
|
|
|
$hostPrefixes = ['']; |
70
|
|
|
if ($withHostPrefix) { |
71
|
|
|
$hostPrefixes = ['', 'imap.']; |
72
|
|
|
} |
73
|
|
|
foreach ($hostPrefixes as $hostPrefix) { |
74
|
|
|
$url = $hostPrefix . $host; |
75
|
|
|
if (gethostbyname($url) === $url) { |
76
|
|
|
continue; |
77
|
|
|
} |
78
|
|
|
foreach ($ports as $port) { |
79
|
|
|
if (!$this->canConnect($url, $port)) { |
80
|
|
|
continue; |
81
|
|
|
} |
82
|
|
|
foreach ($protocols as $protocol) { |
83
|
|
|
foreach ($users as $user) { |
84
|
|
|
try { |
85
|
|
|
$account->setOutboundHost($url); |
86
|
|
|
$account->setOutboundPort($port); |
87
|
|
|
$account->setOutboundUser($user); |
88
|
|
|
$password = $this->crypto->encrypt($password); |
89
|
|
|
$account->setOutboundPassword($password); |
90
|
|
|
$account->setOutboundSslMode($protocol); |
91
|
|
|
|
92
|
|
|
$a = new Account($account); |
93
|
|
|
$smtp = $a->createTransport(); |
94
|
|
|
$smtp->getSMTPObject(); |
95
|
|
|
|
96
|
|
|
$this->logger->info("Test-Account-Successful: $this->userId, $url, $port, $user, $protocol"); |
97
|
|
|
|
98
|
|
|
return true; |
99
|
|
|
} catch (\Exception $e) { |
100
|
|
|
$error = $e->getMessage(); |
101
|
|
|
$this->logger->info("Test-Account-Failed: $this->userId, $url, $port, $user, $protocol -> $error"); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|