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
|
110 |
|
* @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
|
110 |
|
* @param HeaderPart[] $nameParts |
44
|
2 |
|
*/ |
45
|
2 |
|
public function __construct( |
46
|
2 |
|
LoggerInterface $logger, |
47
|
2 |
|
MbWrapper $charsetConverter, |
48
|
|
|
array $nameParts, |
49
|
108 |
|
ContainerPart $valuePart |
50
|
108 |
|
) { |
51
|
|
|
parent::__construct($logger, $charsetConverter, $nameParts, $valuePart->children); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function getNameFromParts(array $parts) : string |
55
|
|
|
{ |
56
|
|
|
$name = parent::getNameFromParts($parts); |
57
|
108 |
|
if (\preg_match('~^\s*([^\*]+)\*(\d*)(\*)?$~', $name, $matches)) { |
58
|
|
|
$name = $matches[1]; |
59
|
108 |
|
$this->index = ($matches[2] !== '') ? intval($matches[2]) : null; |
60
|
|
|
$this->encoded = (($matches[2] === '') || !empty($matches[3])); |
61
|
|
|
} |
62
|
|
|
return $name; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function decodePartValue(string $value, ?string $charset = null) : string |
66
|
|
|
{ |
67
|
|
|
if ($charset !== null) { |
68
|
1 |
|
return $this->convertEncoding(\rawurldecode($value), $charset, true); |
69
|
|
|
} |
70
|
1 |
|
return $this->convertEncoding(\rawurldecode($value)); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
protected function getValueFromParts(array $parts) : string |
74
|
|
|
{ |
75
|
1 |
|
$value = parent::getValueFromParts($parts); |
76
|
1 |
|
if ($this->encoded && \preg_match('~^([^\']*)\'?([^\']*)\'?(.*)$~', $value, $matches)) { |
77
|
|
|
$this->charset = (!empty($matches[1]) && !empty($matches[3])) ? $matches[1] : $this->charset; |
78
|
|
|
$this->language = (!empty($matches[2])) ? $matches[2] : $this->language; |
79
|
|
|
$ev = (empty($matches[3])) ? $matches[1] : $matches[3]; |
80
|
|
|
// only if it's not part of a SplitParameterPart |
81
|
|
|
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
|
|
|
return $this->decodePartValue($ev, $this->charset); |
85
|
|
|
} |
86
|
|
|
return $ev; |
87
|
|
|
} |
88
|
|
|
return $value; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns the charset if the part is an RFC-2231 part with a charset set. |
93
|
|
|
*/ |
94
|
|
|
public function getCharset() : ?string |
95
|
|
|
{ |
96
|
|
|
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
|
|
|
public function getLanguage() : ?string |
106
|
|
|
{ |
107
|
|
|
return $this->language; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function isUrlEncoded() : bool |
111
|
|
|
{ |
112
|
|
|
return $this->encoded; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getIndex() : ?int |
116
|
|
|
{ |
117
|
|
|
return $this->index; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|