forked from Webklex/php-imap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Part.php
324 lines (282 loc) · 7.82 KB
/
Part.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
/*
* File: Part.php
* Category: -
* Author: M.Goldenbaum
* Created: 17.09.20 20:38
* Updated: -
*
* Description:
* -
*/
namespace Webklex\PHPIMAP;
use Webklex\PHPIMAP\Exceptions\InvalidMessageDateException;
/**
* Class Part
*
* @package Webklex\PHPIMAP
*/
class Part {
/**
* Raw part
*
* @var string $raw
*/
public string $raw = "";
/**
* Part type
*
* @var int $type
*/
public int $type = IMAP::MESSAGE_TYPE_TEXT;
/**
* Part content
*
* @var string $content
*/
public string $content = "";
/**
* Part subtype
*
* @var ?string $subtype
*/
public ?string $subtype = null;
/**
* Part charset - if available
*
* @var string $charset
*/
public string $charset = "utf-8";
/**
* Part encoding method
*
* @var int $encoding
*/
public int $encoding = IMAP::MESSAGE_ENC_OTHER;
/**
* Alias to check if the part is an attachment
*
* @var boolean $ifdisposition
*/
public bool $ifdisposition = false;
/**
* Indicates if the part is an attachment
*
* @var ?string $disposition
*/
public ?string $disposition = null;
/**
* Alias to check if the part has a description
*
* @var boolean $ifdescription
*/
public bool $ifdescription = false;
/**
* Part description if available
*
* @var ?string $description
*/
public ?string $description = null;
/**
* Part filename if available
*
* @var ?string $filename
*/
public ?string $filename = null;
/**
* Part name if available
*
* @var ?string $name
*/
public ?string $name = null;
/**
* Part id if available
*
* @var ?string $id
*/
public ?string $id = null;
/**
* The part number of the current part
*
* @var integer $part_number
*/
public int $part_number = 0;
/**
* Part length in bytes
*
* @var integer $bytes
*/
public int $bytes;
/**
* Part content type
*
* @var string|null $content_type
*/
public ?string $content_type = null;
/**
* @var ?Header $header
*/
private ?Header $header;
/**
* @var Config $config
*/
protected Config $config;
/**
* Part constructor.
* @param string $raw_part
* @param Config $config
* @param Header|null $header
* @param integer $part_number
*
* @throws InvalidMessageDateException
*/
public function __construct(string $raw_part, Config $config, ?Header $header = null, int $part_number = 0) {
$this->raw = $raw_part;
$this->config = $config;
$this->header = $header;
$this->part_number = $part_number;
$this->parse();
}
/**
* Parse the raw parts
*
* @throws InvalidMessageDateException
*/
protected function parse(): void {
if ($this->header === null) {
$body = $this->findHeaders();
}else{
$body = $this->raw;
}
$this->parseDisposition();
$this->parseDescription();
$this->parseEncoding();
$this->charset = $this->header->get("charset")->first();
$this->name = $this->header->get("name");
$this->filename = $this->header->get("filename");
if($this->header->get("id")->exist()) {
$this->id = $this->header->get("id");
} else if($this->header->get("x_attachment_id")->exist()){
$this->id = $this->header->get("x_attachment_id");
} else if($this->header->get("content_id")->exist()){
$this->id = strtr($this->header->get("content_id"), [
'<' => '',
'>' => ''
]);
}
$content_types = $this->header->get("content_type")->all();
if(!empty($content_types)){
$this->subtype = $this->parseSubtype($content_types);
$content_type = $content_types[0];
$parts = explode(';', $content_type);
$this->content_type = trim($parts[0]);
}
$this->content = trim(rtrim($body));
$this->bytes = strlen($this->content);
}
/**
* Find all available headers and return the leftover body segment
*
* @return string
* @throws InvalidMessageDateException
*/
private function findHeaders(): string {
$body = $this->raw;
while (($pos = strpos($body, "\r\n")) > 0) {
$body = substr($body, $pos + 2);
}
$headers = substr($this->raw, 0, strlen($body) * -1);
$body = substr($body, 0, -2);
$this->header = new Header($headers, $this->config);
return $body;
}
/**
* Try to parse the subtype if any is present
* @param $content_type
*
* @return ?string
*/
private function parseSubtype($content_type): ?string {
if (is_array($content_type)) {
foreach ($content_type as $part){
if ((strpos($part, "/")) !== false){
return $this->parseSubtype($part);
}
}
return null;
}
if (($pos = strpos($content_type, "/")) !== false){
return substr(explode(";", $content_type)[0], $pos + 1);
}
return null;
}
/**
* Try to parse the disposition if any is present
*/
private function parseDisposition(): void {
$content_disposition = $this->header->get("content_disposition")->first();
if($content_disposition) {
$this->ifdisposition = true;
$this->disposition = (is_array($content_disposition)) ? implode(' ', $content_disposition) : explode(";", $content_disposition)[0];
}
}
/**
* Try to parse the description if any is present
*/
private function parseDescription(): void {
$content_description = $this->header->get("content_description")->first();
if($content_description) {
$this->ifdescription = true;
$this->description = $content_description;
}
}
/**
* Try to parse the encoding if any is present
*/
private function parseEncoding(): void {
$encoding = $this->header->get("content_transfer_encoding")->first();
if($encoding) {
$this->encoding = match (strtolower($encoding)) {
"quoted-printable" => IMAP::MESSAGE_ENC_QUOTED_PRINTABLE,
"base64" => IMAP::MESSAGE_ENC_BASE64,
"7bit" => IMAP::MESSAGE_ENC_7BIT,
"8bit" => IMAP::MESSAGE_ENC_8BIT,
"binary" => IMAP::MESSAGE_ENC_BINARY,
default => IMAP::MESSAGE_ENC_OTHER,
};
}
}
/**
* Check if the current part represents an attachment
*
* @return bool
*/
public function isAttachment(): bool {
$valid_disposition = in_array(strtolower($this->disposition ?? ''), $this->config->get('options.dispositions'));
if ($this->type == IMAP::MESSAGE_TYPE_TEXT && ($this->ifdisposition == 0 || empty($this->disposition) || !$valid_disposition)) {
if (($this->subtype == null || in_array((strtolower($this->subtype)), ["plain", "html"])) && $this->filename == null && $this->name == null) {
return false;
}
}
if ($this->disposition === "inline" && $this->filename == null && $this->name == null && !$this->header->has("content_id")) {
return false;
}
return true;
}
/**
* Get the part header
*
* @return Header|null
*/
public function getHeader(): ?Header {
return $this->header;
}
/**
* Get the Config instance
*
* @return Config
*/
public function getConfig(): Config {
return $this->config;
}
}