1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Protocol\Smtp\Capability; |
5
|
|
|
|
6
|
|
|
use Genkgo\Mail\Protocol\ConnectionInterface; |
7
|
|
|
use Genkgo\Mail\Protocol\Smtp\CapabilityInterface; |
8
|
|
|
use Genkgo\Mail\Protocol\Smtp\Session; |
9
|
|
|
|
10
|
|
|
final class EhloCapability implements CapabilityInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var CapabilityInterface[] |
14
|
|
|
*/ |
15
|
|
|
private $capabilities; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $serverName; |
21
|
|
|
|
22
|
|
|
private const PROTOCOL_COMMANDS = [ |
23
|
|
|
'MAIL FROM' => true, |
24
|
|
|
'EHLO' => true, |
25
|
|
|
'RCPT TO' => true, |
26
|
|
|
'RSET' => true, |
27
|
|
|
'QUIT' => true, |
28
|
|
|
'HELO' => true, |
29
|
|
|
'DATA' => true, |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $serverName |
34
|
|
|
* @param array|CapabilityInterface[] $capabilities |
35
|
|
|
*/ |
36
|
8 |
|
public function __construct(string $serverName, array $capabilities) |
37
|
|
|
{ |
38
|
8 |
|
$this->serverName = $serverName; |
39
|
8 |
|
$this->capabilities = $capabilities; |
40
|
8 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param ConnectionInterface $connection |
44
|
|
|
* @param Session $session |
45
|
|
|
* @return Session |
46
|
|
|
*/ |
47
|
2 |
|
public function manifest(ConnectionInterface $connection, Session $session): Session |
48
|
|
|
{ |
49
|
2 |
|
$command = $session->getCommand(); |
50
|
|
|
|
51
|
2 |
|
$advertisements = \array_filter( |
52
|
2 |
|
\array_map( |
53
|
|
|
function (CapabilityInterface $capability) { |
54
|
1 |
|
return $capability->advertise(); |
55
|
2 |
|
}, |
56
|
2 |
|
$this->capabilities |
57
|
|
|
), |
58
|
|
|
function (string $advertisement) { |
59
|
1 |
|
return !isset(self::PROTOCOL_COMMANDS[$advertisement]); |
60
|
2 |
|
} |
61
|
|
|
); |
62
|
|
|
|
63
|
2 |
|
$messages = \array_merge( |
64
|
2 |
|
[$this->serverName . ' Hello ' . \substr($command, 5)], |
65
|
2 |
|
$advertisements |
66
|
|
|
); |
67
|
|
|
|
68
|
2 |
|
foreach ($messages as $message) { |
69
|
2 |
|
if (\next($messages) === false) { |
70
|
2 |
|
$connection->send('250 ' . $message); |
71
|
|
|
} else { |
72
|
1 |
|
$connection->send('250-' . $message); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
return $session->withState(Session::STATE_EHLO); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
3 |
|
public function advertise(): string |
83
|
|
|
{ |
84
|
3 |
|
return 'EHLO'; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|