|
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 Psr\Log\LogLevel; |
|
12
|
|
|
use ZBateson\MailMimeParser\ErrorBag; |
|
13
|
|
|
use ZBateson\MailMimeParser\Header\IHeaderPart; |
|
14
|
|
|
use ZBateson\MbWrapper\MbWrapper; |
|
15
|
|
|
use ZBateson\MbWrapper\UnsupportedCharsetException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Abstract base class representing a single part of a parsed header. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Zaahid Bateson |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class HeaderPart extends ErrorBag implements IHeaderPart |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string the representative value of the part after any conversion or |
|
26
|
|
|
* processing has been done on it (e.g. removing new lines, converting, |
|
27
|
|
|
* whatever else). |
|
28
|
|
|
*/ |
|
29
|
|
|
protected string $value; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var MbWrapper $charsetConverter the charset converter used for |
|
33
|
|
|
* converting strings in HeaderPart::convertEncoding |
|
34
|
|
|
*/ |
|
35
|
|
|
protected MbWrapper $charsetConverter; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var bool set to true to ignore spaces before this part |
|
39
|
|
|
*/ |
|
40
|
|
|
protected bool $canIgnoreSpacesBefore = false; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var bool set to true to ignore spaces after this part |
|
44
|
|
|
*/ |
|
45
|
|
|
protected bool $canIgnoreSpacesAfter = false; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* True if the part is a space token |
|
49
|
|
|
*/ |
|
50
|
|
|
protected bool $isSpace = false; |
|
51
|
|
|
|
|
52
|
163 |
|
public function __construct(LoggerInterface $logger, MbWrapper $charsetConverter, string $value) |
|
53
|
|
|
{ |
|
54
|
163 |
|
parent::__construct($logger); |
|
55
|
163 |
|
$this->charsetConverter = $charsetConverter; |
|
56
|
163 |
|
$this->value = $value; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns the part's representative value after any necessary processing |
|
61
|
|
|
* has been performed. For the raw value, call getRawValue(). |
|
62
|
|
|
*/ |
|
63
|
143 |
|
public function getValue() : string |
|
64
|
|
|
{ |
|
65
|
143 |
|
return $this->value; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Returns the value of the part (which is a string). |
|
70
|
|
|
* |
|
71
|
|
|
* @return string the value |
|
72
|
|
|
*/ |
|
73
|
3 |
|
public function __toString() : string |
|
74
|
|
|
{ |
|
75
|
3 |
|
return $this->value; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Ensures the encoding of the passed string is set to UTF-8. |
|
80
|
|
|
* |
|
81
|
|
|
* The method does nothing if the passed $from charset is UTF-8 already, or |
|
82
|
|
|
* if $force is set to false and mb_check_encoding for $str returns true |
|
83
|
|
|
* for 'UTF-8'. |
|
84
|
|
|
* |
|
85
|
|
|
* @return string utf-8 string |
|
86
|
|
|
*/ |
|
87
|
158 |
|
protected function convertEncoding(string $str, string $from = 'ISO-8859-1', bool $force = false) : string |
|
88
|
|
|
{ |
|
89
|
158 |
|
if ($from !== 'UTF-8') { |
|
90
|
|
|
// mime header part decoding will force it. This is necessary for |
|
91
|
|
|
// UTF-7 because mb_check_encoding will return true |
|
92
|
158 |
|
if ($force || !($this->charsetConverter->checkEncoding($str, 'UTF-8'))) { |
|
93
|
|
|
try { |
|
94
|
65 |
|
return $this->charsetConverter->convert($str, $from, 'UTF-8'); |
|
95
|
2 |
|
} catch (UnsupportedCharsetException $ce) { |
|
96
|
2 |
|
$this->addError('Unable to convert charset', LogLevel::ERROR, $ce); |
|
97
|
2 |
|
return $this->charsetConverter->convert($str, 'ISO-8859-1', 'UTF-8'); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
158 |
|
return $str; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
4 |
|
public function getComments() : array |
|
105
|
|
|
{ |
|
106
|
4 |
|
return []; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Default implementation returns an empty array. |
|
111
|
|
|
*/ |
|
112
|
5 |
|
protected function getErrorBagChildren() : array |
|
113
|
|
|
{ |
|
114
|
5 |
|
return []; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|