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\Message; |
8
|
|
|
|
9
|
|
|
use ZBateson\MailMimeParser\Message\Writer\MimePartWriter; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Description of WritableMimePart |
13
|
|
|
* |
14
|
|
|
* @author Zaahid Bateson |
15
|
|
|
*/ |
16
|
|
|
class WritableMimePart |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var \ZBateson\MailMimeParser\Message\Writer\MimePartWriter the part |
20
|
|
|
* writer for this MimePart |
21
|
|
|
*/ |
22
|
|
|
protected $partWriter = null; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Registers the passed part as a child of the current part. |
26
|
|
|
* |
27
|
|
|
* If the $position parameter is non-null, adds the part at the passed |
28
|
|
|
* position index. |
29
|
|
|
* |
30
|
|
|
* @param \ZBateson\MailMimeParser\Message\Part\MimePart $part |
31
|
|
|
* @param int $position |
32
|
|
|
*/ |
33
|
|
|
public function addPart(MimePart $part, $position = null) |
34
|
|
|
{ |
35
|
|
|
if ($part !== $this) { |
36
|
|
|
$part->setParent($this); |
37
|
|
|
array_splice($this->parts, ($position === null) ? count($this->parts) : $position, 0, [ $part ]); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Removes the child part from this part and returns its position or |
43
|
|
|
* null if it wasn't found. |
44
|
|
|
* |
45
|
|
|
* Note that if the part is not a direct child of this part, the returned |
46
|
|
|
* position is its index within its parent (calls removePart on its direct |
47
|
|
|
* parent). |
48
|
|
|
* |
49
|
|
|
* @param \ZBateson\MailMimeParser\Message\Part\MimePart $part |
50
|
|
|
* @return int or null if not found |
51
|
|
|
*/ |
52
|
|
|
public function removePart(MimePart $part) |
53
|
|
|
{ |
54
|
|
|
$parent = $part->getParent(); |
55
|
|
|
if ($this !== $parent && $parent !== null) { |
56
|
|
|
return $parent->removePart($part); |
57
|
|
|
} else { |
58
|
|
|
$position = array_search($part, $this->parts, true); |
59
|
|
|
if ($position !== false) { |
60
|
|
|
array_splice($this->parts, $position, 1); |
61
|
|
|
return $position; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Removes all parts that are matched by the passed PartFilter. |
69
|
|
|
* |
70
|
|
|
* @param \ZBateson\MailMimeParser\Message\PartFilter $filter |
71
|
|
|
*/ |
72
|
|
|
public function removeAllParts(PartFilter $filter = null) |
73
|
|
|
{ |
74
|
|
|
foreach ($this->getAllParts($filter) as $part) { |
|
|
|
|
75
|
|
|
$this->removePart($part); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Attaches the resource handle for the part's content. The attached handle |
82
|
|
|
* is closed when the MimePart object is destroyed. |
83
|
|
|
* |
84
|
|
|
* @param resource $contentHandle |
85
|
|
|
*/ |
86
|
|
|
public function attachContentResourceHandle($contentHandle) |
87
|
|
|
{ |
88
|
|
|
if ($this->handle !== null && $this->handle !== $contentHandle) { |
89
|
|
|
fclose($this->handle); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
$this->handle = $contentHandle; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Attaches the resource handle representing the original stream that |
96
|
|
|
* created this part (including any sub-parts). The attached handle is |
97
|
|
|
* closed when the MimePart object is destroyed. |
98
|
|
|
* |
99
|
|
|
* This stream is not modified or changed as the part is changed and is only |
100
|
|
|
* set during parsing in MessageParser. |
101
|
|
|
* |
102
|
|
|
* @param resource $handle |
103
|
|
|
*/ |
104
|
|
|
public function attachOriginalStreamHandle($handle) |
105
|
|
|
{ |
106
|
|
|
if ($this->originalStreamHandle !== null && $this->originalStreamHandle !== $handle) { |
|
|
|
|
107
|
|
|
fclose($this->originalStreamHandle); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
$this->originalStreamHandle = $handle; |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Detaches the content resource handle from this part but does not close |
115
|
|
|
* it. |
116
|
|
|
*/ |
117
|
|
|
protected function detachContentResourceHandle() |
118
|
|
|
{ |
119
|
|
|
$this->handle = null; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Sets the content of the part to the passed string (effectively creates |
124
|
|
|
* a php://temp stream with the passed content and calls |
125
|
|
|
* attachContentResourceHandle with the opened stream). |
126
|
|
|
* |
127
|
|
|
* @param string $string |
128
|
|
|
*/ |
129
|
|
|
public function setContent($string) |
130
|
|
|
{ |
131
|
|
|
$handle = fopen('php://temp', 'r+'); |
132
|
|
|
fwrite($handle, $string); |
133
|
|
|
rewind($handle); |
134
|
|
|
$this->attachContentResourceHandle($handle); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Adds a header with the given $name and $value. |
140
|
|
|
* |
141
|
|
|
* Creates a new \ZBateson\MailMimeParser\Header\AbstractHeader object and |
142
|
|
|
* registers it as a header. |
143
|
|
|
* |
144
|
|
|
* @param string $name |
145
|
|
|
* @param string $value |
146
|
|
|
*/ |
147
|
|
|
public function setRawHeader($name, $value) |
148
|
|
|
{ |
149
|
|
|
$this->headers[strtolower($name)] = $this->headerFactory->newInstance($name, $value); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Removes the header with the given name |
154
|
|
|
* |
155
|
|
|
* @param string $name |
156
|
|
|
*/ |
157
|
|
|
public function removeHeader($name) |
158
|
|
|
{ |
159
|
|
|
unset($this->headers[strtolower($name)]); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Sets the parent part. |
165
|
|
|
* |
166
|
|
|
* @param \ZBateson\MailMimeParser\Message\Part\MessagePart $part |
167
|
|
|
*/ |
168
|
|
|
public function setParent(MessagePart $part) |
169
|
|
|
{ |
170
|
|
|
$this->parent = $part; |
|
|
|
|
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: