|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the ZBateson\MailMimeParser project. |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace ZBateson\MailMimeParser\Header\Part; |
|
9
|
|
|
|
|
10
|
|
|
use Psr\Log\LoggerInterface; |
|
11
|
|
|
use ZBateson\MbWrapper\MbWrapper; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Holds extra information about a parsed Received header part, for FROM and BY |
|
15
|
|
|
* parts, namely: ehlo name, hostname, and address. |
|
16
|
|
|
* |
|
17
|
|
|
* The parsed parts would be mapped as follows: |
|
18
|
|
|
* |
|
19
|
|
|
* FROM ehlo name (hostname [address]), for example: FROM computer (domain.com |
|
20
|
|
|
* [1.2.3.4]) would contain "computer" for getEhloName(), domain.com for |
|
21
|
|
|
* getHostname and 1.2.3.4 for getAddress(). |
|
22
|
|
|
* |
|
23
|
|
|
* This doesn't change if the ehlo name is an address, it is still returned in |
|
24
|
|
|
* getEhloName(), and not in getAddress(). Additionally square brackets are not |
|
25
|
|
|
* stripped from getEhloName() if its an address. For example: "FROM [1.2.3.4]" |
|
26
|
|
|
* would return "[1.2.3.4]" in a call to getEhloName(). |
|
27
|
|
|
* |
|
28
|
|
|
* For further information on how the header's parsed, check the documentation |
|
29
|
|
|
* for {@see \ZBateson\MailMimeParser\Header\Consumer\Received\DomainConsumer}. |
|
30
|
|
|
* |
|
31
|
|
|
* @author Zaahid Bateson |
|
32
|
|
|
*/ |
|
33
|
|
|
class ReceivedDomainPart extends ReceivedPart |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var string The name used to identify the server in the EHLO line. |
|
37
|
|
|
*/ |
|
38
|
|
|
protected ?string $ehloName = null; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string The hostname. |
|
42
|
|
|
*/ |
|
43
|
|
|
protected ?string $hostname = null; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string The address. |
|
47
|
|
|
*/ |
|
48
|
|
|
protected ?string $address = null; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param HeaderPart[] $children |
|
52
|
|
|
*/ |
|
53
|
1 |
|
public function __construct( |
|
54
|
|
|
LoggerInterface $logger, |
|
55
|
|
|
MbWrapper $charsetConverter, |
|
56
|
|
|
string $name, |
|
57
|
|
|
array $children |
|
58
|
|
|
) { |
|
59
|
1 |
|
parent::__construct($logger, $charsetConverter, $name, $children); |
|
60
|
|
|
|
|
61
|
1 |
|
$this->ehloName = ($this->value !== '') ? $this->value : null; |
|
62
|
1 |
|
$cps = $this->getComments(); |
|
63
|
1 |
|
$commentPart = (!empty($cps)) ? $cps[0] : null; |
|
64
|
|
|
|
|
65
|
1 |
|
$pattern = '~^(\[(IPv[64])?(?P<addr1>[a-f\d\.\:]+)\])?\s*(helo=)?(?P<name>[a-z0-9\-]+[a-z0-9\-\.]+)?\s*(\[(IPv[64])?(?P<addr2>[a-f\d\.\:]+)\])?$~i'; |
|
66
|
1 |
|
if ($commentPart !== null && \preg_match($pattern, $commentPart->getComment(), $matches)) { |
|
67
|
|
|
$this->value .= ' (' . $commentPart->getComment() . ')'; |
|
68
|
|
|
$this->hostname = (!empty($matches['name'])) ? $matches['name'] : null; |
|
69
|
|
|
$this->address = (!empty($matches['addr1'])) ? $matches['addr1'] : ((!empty($matches['addr2'])) ? $matches['addr2'] : null); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Returns the name used to identify the server in the first part of the |
|
75
|
|
|
* extended-domain line. |
|
76
|
|
|
* |
|
77
|
|
|
* Note that this is not necessarily the name used in the EHLO line to an |
|
78
|
|
|
* SMTP server, since implementations differ so much, not much can be |
|
79
|
|
|
* guaranteed except the position it was parsed in. |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public function getEhloName() : ?string |
|
82
|
|
|
{ |
|
83
|
1 |
|
return $this->ehloName; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns the hostname of the server, or whatever string in the hostname |
|
88
|
|
|
* position when parsing (but never an address). |
|
89
|
|
|
*/ |
|
90
|
1 |
|
public function getHostname() : ?string |
|
91
|
|
|
{ |
|
92
|
1 |
|
return $this->hostname; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Returns the address of the server, or whatever string that looks like an |
|
97
|
|
|
* address in the address position when parsing (but never a hostname). |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function getAddress() : ?string |
|
100
|
|
|
{ |
|
101
|
1 |
|
return $this->address; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|