|
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
|
|
|
namespace ZBateson\MailMimeParser\Header\Part; |
|
8
|
|
|
|
|
9
|
|
|
use ZBateson\MailMimeParser\MailMimeParser; |
|
10
|
|
|
use ZBateson\MailMimeParser\Util\CharsetConverter; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Abstract base class representing a single part of a parsed header. |
|
14
|
|
|
* |
|
15
|
|
|
* @author Zaahid Bateson |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class HeaderPart |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string the value of the part |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $value; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var CharsetConverter $charsetConverter the charset converter used for |
|
26
|
|
|
* converting strings in HeaderPart::convertEncoding |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $charsetConverter; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Sets up dependencies. |
|
32
|
|
|
* |
|
33
|
|
|
* @param CharsetConverter $charsetConverter |
|
34
|
|
|
*/ |
|
35
|
22 |
|
public function __construct(CharsetConverter $charsetConverter) |
|
36
|
|
|
{ |
|
37
|
22 |
|
$this->charsetConverter = $charsetConverter; |
|
38
|
22 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Returns the part's value. |
|
42
|
|
|
* |
|
43
|
|
|
* @return string the value of the part |
|
44
|
|
|
*/ |
|
45
|
13 |
|
public function getValue() |
|
46
|
|
|
{ |
|
47
|
13 |
|
return $this->value; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Returns the value of the part (which is a string). |
|
52
|
|
|
* |
|
53
|
|
|
* @return string the value |
|
54
|
|
|
*/ |
|
55
|
2 |
|
public function __toString() |
|
56
|
|
|
{ |
|
57
|
2 |
|
return $this->value; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Returns true if spaces before this part should be ignored. True is only |
|
62
|
|
|
* returned for MimeLiterals if the part begins with a mime-encoded string, |
|
63
|
|
|
* Tokens if the Token's value is a single space, and for CommentParts. |
|
64
|
|
|
* |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function ignoreSpacesBefore() |
|
68
|
|
|
{ |
|
69
|
1 |
|
return false; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Returns true if spaces after this part should be ignored. True is only |
|
74
|
|
|
* returned for MimeLiterals if the part ends with a mime-encoded string |
|
75
|
|
|
* Tokens if the Token's value is a single space, and for CommentParts. |
|
76
|
|
|
* |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
1 |
|
public function ignoreSpacesAfter() |
|
80
|
|
|
{ |
|
81
|
1 |
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Ensures the encoding of the passed string is set to UTF-8. |
|
86
|
|
|
* |
|
87
|
|
|
* The method does nothing if the passed $from charset is UTF-8 already, or |
|
88
|
|
|
* if $force is set to false and mb_check_encoding for $str returns true |
|
89
|
|
|
* for 'UTF-8'. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $str |
|
92
|
|
|
* @param string $from |
|
93
|
|
|
* @param boolean $force |
|
94
|
|
|
* @return string utf-8 string |
|
95
|
|
|
*/ |
|
96
|
18 |
|
protected function convertEncoding($str, $from = 'ISO-8859-1', $force = false) |
|
97
|
|
|
{ |
|
98
|
18 |
|
if ($from !== 'UTF-8') { |
|
99
|
|
|
// mime header part decoding will force it. This is necessary for |
|
100
|
|
|
// UTF-7 because mb_check_encoding will return true |
|
101
|
18 |
|
if ($force || !mb_check_encoding($str, 'UTF-8')) { |
|
102
|
9 |
|
return $this->charsetConverter->convert($str, $from, 'UTF-8'); |
|
103
|
|
|
} |
|
104
|
18 |
|
} |
|
105
|
18 |
|
return $str; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|