1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Protocol\Smtp; |
5
|
|
|
|
6
|
|
|
use Genkgo\Mail\Exception\ConnectionBrokenException; |
7
|
|
|
use Genkgo\Mail\Exception\ConnectionTimeoutException; |
8
|
|
|
use Genkgo\Mail\Exception\ConnectionClosedException; |
9
|
|
|
use Genkgo\Mail\Exception\UnknownSmtpCommandException; |
10
|
|
|
use Genkgo\Mail\Protocol\AppendCrlfConnection; |
11
|
|
|
use Genkgo\Mail\Protocol\ConnectionInterface; |
12
|
|
|
use Genkgo\Mail\Protocol\ConnectionListenerInterface; |
13
|
|
|
use Genkgo\Mail\Protocol\Smtp\Capability\EhloCapability; |
14
|
|
|
use Genkgo\Mail\Protocol\Smtp\Capability\QuitCapability; |
15
|
|
|
use Genkgo\Mail\Protocol\Smtp\Capability\ResetCapability; |
16
|
|
|
use Genkgo\Mail\Protocol\TrimCrlfConnection; |
17
|
|
|
|
18
|
|
|
final class Server |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ConnectionListenerInterface |
22
|
|
|
*/ |
23
|
|
|
private $listener; |
24
|
|
|
/** |
25
|
|
|
* @var CapabilityInterface[] |
26
|
|
|
*/ |
27
|
|
|
private $capabilities = []; |
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $serverName; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Client constructor. |
35
|
|
|
* @param ConnectionListenerInterface $connection |
36
|
|
|
* @param array $capabilities |
37
|
|
|
* @param string $serverName |
38
|
|
|
*/ |
39
|
6 |
|
public function __construct(ConnectionListenerInterface $connection, array $capabilities, string $serverName) { |
40
|
6 |
|
$this->listener = $connection; |
41
|
6 |
|
$this->serverName = $serverName; |
42
|
|
|
|
43
|
6 |
|
$this->capabilities = array_merge( |
|
|
|
|
44
|
6 |
|
$capabilities, |
45
|
|
|
[ |
46
|
6 |
|
new EhloCapability($this->serverName, $capabilities), |
47
|
6 |
|
new QuitCapability(), |
48
|
6 |
|
new ResetCapability() |
49
|
|
|
] |
50
|
|
|
); |
51
|
6 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* |
55
|
|
|
*/ |
56
|
6 |
|
public function start(): void |
57
|
|
|
{ |
58
|
6 |
|
while ($connection = $this->listener->listen()) { |
59
|
6 |
|
$connection = new TrimCrlfConnection(new AppendCrlfConnection($connection)); |
60
|
6 |
|
$connection->send('220 Welcome to Genkgo Mail Server'); |
61
|
|
|
|
62
|
6 |
|
$this->transport( |
63
|
6 |
|
$connection, |
64
|
6 |
|
function (ConnectionInterface $connection) { |
65
|
6 |
|
$session = new Session(); |
66
|
|
|
|
67
|
6 |
|
while ($session = $session->withCommand($connection->receive())) { |
68
|
|
|
try { |
69
|
2 |
|
$session = $this->handleCommand($connection, $session); |
70
|
1 |
|
} catch (UnknownSmtpCommandException $e) { |
71
|
1 |
|
$connection->send('500 unrecognized command'); |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
if ($session->getState() === Session::STATE_DISCONNECT) { |
75
|
2 |
|
break; |
76
|
|
|
} |
77
|
|
|
} |
78
|
6 |
|
} |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param ConnectionInterface $connection |
85
|
|
|
* @param \Closure $callback |
86
|
|
|
*/ |
87
|
6 |
|
private function transport(ConnectionInterface $connection, \Closure $callback) { |
88
|
|
|
try { |
89
|
6 |
|
$callback($connection); |
90
|
4 |
|
} catch (ConnectionTimeoutException $e) { |
91
|
1 |
|
$connection->send('421 command timeout - closing connection'); |
92
|
1 |
|
$connection->disconnect(); |
93
|
3 |
|
} catch (ConnectionBrokenException $e) { |
94
|
|
|
try { |
95
|
2 |
|
$connection->send('554 transaction failed, unexpected value - closing connection'); |
96
|
1 |
|
} catch (\Exception $e) { |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
$connection->disconnect(); |
100
|
1 |
|
} catch (ConnectionClosedException $e) { |
101
|
1 |
|
$connection->disconnect(); |
102
|
|
|
} |
103
|
6 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param ConnectionInterface $connection |
107
|
|
|
* @param Session $session |
108
|
|
|
* @return Session |
109
|
|
|
* @throws UnknownSmtpCommandException |
110
|
|
|
*/ |
111
|
2 |
|
private function handleCommand(ConnectionInterface $connection, Session $session): Session |
112
|
|
|
{ |
113
|
2 |
|
$command = $session->getCommand(); |
114
|
|
|
|
115
|
2 |
|
foreach ($this->capabilities as $capability) { |
116
|
2 |
|
$advertised = $capability->advertise(); |
117
|
2 |
|
if (substr($command, 0, strlen($advertised)) === $advertised) { |
118
|
2 |
|
return $capability->manifest($connection, $session); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
throw new UnknownSmtpCommandException('Cannot handle command'); |
123
|
|
|
} |
124
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..