|
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
|
|
|
* Represents a name/value parameter part of a header. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Zaahid Bateson |
|
17
|
|
|
*/ |
|
18
|
|
|
class ParameterPart extends NameValuePart |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var string the RFC-1766 language tag if set. |
|
22
|
|
|
*/ |
|
23
|
|
|
protected ?string $language = null; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string charset of content if set. |
|
27
|
|
|
*/ |
|
28
|
|
|
protected ?string $charset = null; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var int the zero-based index of the part if part of a 'continuation' in |
|
32
|
|
|
* an RFC-2231 split parameter. |
|
33
|
|
|
*/ |
|
34
|
|
|
protected ?int $index = null; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var bool true if the part is an RFC-2231 encoded part, and the value |
|
38
|
|
|
* needs to be decoded. |
|
39
|
|
|
*/ |
|
40
|
|
|
protected bool $encoded = false; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param HeaderPart[] $nameParts |
|
44
|
|
|
*/ |
|
45
|
107 |
|
public function __construct( |
|
46
|
|
|
LoggerInterface $logger, |
|
47
|
|
|
MbWrapper $charsetConverter, |
|
48
|
|
|
array $nameParts, |
|
49
|
|
|
ContainerPart $valuePart |
|
50
|
|
|
) { |
|
51
|
107 |
|
parent::__construct($logger, $charsetConverter, $nameParts, $valuePart->children); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
107 |
|
protected function getNameFromParts(array $parts) : string |
|
55
|
|
|
{ |
|
56
|
107 |
|
$name = parent::getNameFromParts($parts); |
|
57
|
107 |
|
if (\preg_match('~^\s*([^\*]+)\*(\d*)(\*)?$~', $name, $matches)) { |
|
58
|
4 |
|
$name = $matches[1]; |
|
59
|
4 |
|
$this->index = ($matches[2] !== '') ? (int) ($matches[2]) : null; |
|
60
|
4 |
|
$this->encoded = (($matches[2] === '') || !empty($matches[3])); |
|
61
|
|
|
} |
|
62
|
107 |
|
return $name; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
4 |
|
protected function decodePartValue(string $value, ?string $charset = null) : string |
|
66
|
|
|
{ |
|
67
|
4 |
|
if ($charset !== null) { |
|
68
|
3 |
|
return $this->convertEncoding(\rawurldecode($value), $charset, true); |
|
69
|
|
|
} |
|
70
|
2 |
|
return $this->convertEncoding(\rawurldecode($value)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
107 |
|
protected function getValueFromParts(array $parts) : string |
|
74
|
|
|
{ |
|
75
|
107 |
|
$value = parent::getValueFromParts($parts); |
|
76
|
107 |
|
if ($this->encoded && \preg_match('~^([^\']*)\'?([^\']*)\'?(.*)$~', $value, $matches)) { |
|
77
|
4 |
|
$this->charset = (!empty($matches[1]) && !empty($matches[3])) ? $matches[1] : $this->charset; |
|
78
|
4 |
|
$this->language = (!empty($matches[2])) ? $matches[2] : $this->language; |
|
79
|
4 |
|
$ev = (empty($matches[3])) ? $matches[1] : $matches[3]; |
|
80
|
|
|
// only if it's not part of a SplitParameterPart |
|
81
|
4 |
|
if ($this->index === null) { |
|
82
|
|
|
// subsequent parts are decoded as a SplitParameterPart since only |
|
83
|
|
|
// the first part are supposed to have charset/language fields |
|
84
|
4 |
|
return $this->decodePartValue($ev, $this->charset); |
|
85
|
|
|
} |
|
86
|
|
|
return $ev; |
|
87
|
|
|
} |
|
88
|
104 |
|
return $value; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Returns the charset if the part is an RFC-2231 part with a charset set. |
|
93
|
|
|
*/ |
|
94
|
2 |
|
public function getCharset() : ?string |
|
95
|
|
|
{ |
|
96
|
2 |
|
return $this->charset; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Returns the RFC-1766 (or subset) language tag, if the parameter is an |
|
101
|
|
|
* RFC-2231 part with a language tag set. |
|
102
|
|
|
* |
|
103
|
|
|
* @return ?string the language if set, or null if not |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function getLanguage() : ?string |
|
106
|
|
|
{ |
|
107
|
1 |
|
return $this->language; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function isUrlEncoded() : bool |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->encoded; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
102 |
|
public function getIndex() : ?int |
|
116
|
|
|
{ |
|
117
|
102 |
|
return $this->index; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|