1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Protocol\Imap\Negotiation; |
5
|
|
|
|
6
|
|
|
use Genkgo\Mail\Exception\ConnectionInsecureException; |
7
|
|
|
use Genkgo\Mail\Exception\SecureConnectionUpgradeException; |
8
|
|
|
use Genkgo\Mail\Protocol\ConnectionInterface; |
9
|
|
|
use Genkgo\Mail\Protocol\Imap\Client; |
10
|
|
|
use Genkgo\Mail\Protocol\Imap\NegotiationInterface; |
11
|
|
|
use Genkgo\Mail\Protocol\Imap\Request\CapabilityCommand; |
12
|
|
|
use Genkgo\Mail\Protocol\Imap\Request\StartTlsCommand; |
13
|
|
|
use Genkgo\Mail\Protocol\Imap\Response\Command\CapabilityCommandResponse; |
14
|
|
|
use Genkgo\Mail\Protocol\Imap\Response\CompletionResult; |
15
|
|
|
|
16
|
|
|
final class TryTlsUpgradeNegotiation implements NegotiationInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ConnectionInterface |
20
|
|
|
*/ |
21
|
|
|
private $connection; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
private $crypto; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param ConnectionInterface $connection |
30
|
|
|
* @param int $crypto |
31
|
|
|
*/ |
32
|
4 |
|
public function __construct( |
33
|
|
|
ConnectionInterface $connection, |
34
|
|
|
int $crypto |
35
|
|
|
) { |
36
|
4 |
|
$this->connection = $connection; |
37
|
4 |
|
$this->crypto = $crypto; |
38
|
4 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Client $client |
42
|
|
|
* @throws ConnectionInsecureException |
43
|
|
|
*/ |
44
|
3 |
|
public function negotiate(Client $client): void |
45
|
|
|
{ |
46
|
3 |
|
if (empty($this->connection->getMetaData(['crypto']))) { |
47
|
2 |
|
$responseList = $client->emit(new CapabilityCommand($client->newTag())); |
48
|
|
|
|
49
|
2 |
|
$capabilities = CapabilityCommandResponse::fromString($responseList->first()->getBody()); |
50
|
|
|
|
51
|
|
|
$responseList |
52
|
2 |
|
->last() |
53
|
2 |
|
->assertCompletion(CompletionResult::ok()) |
54
|
2 |
|
->assertTagged(); |
55
|
|
|
|
56
|
2 |
|
if ($capabilities->isAdvertising('STARTTLS')) { |
57
|
|
|
$client |
58
|
1 |
|
->emit(new StartTlsCommand($client->newTag())) |
59
|
1 |
|
->last() |
60
|
1 |
|
->assertCompletion(CompletionResult::ok()) |
61
|
1 |
|
->assertTagged(); |
62
|
|
|
|
63
|
|
|
try { |
64
|
1 |
|
$this->connection->upgrade($this->crypto); |
65
|
|
|
} catch (SecureConnectionUpgradeException $e) { |
|
|
|
|
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
3 |
|
} |
70
|
|
|
} |
71
|
|
|
|