forked from Webklex/php-imap
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTNEFPartDecoder.php
More file actions
57 lines (48 loc) · 1.69 KB
/
TNEFPartDecoder.php
File metadata and controls
57 lines (48 loc) · 1.69 KB
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
<?php
namespace Webklex\PHPIMAP\Decoder;
use TNEFDecoder\TNEFAttachment;
use Webklex\PHPIMAP\{Part, Header};
class TNEFPartDecoder
{
public function __construct(private $headers = null, private $config = null) {}
public function decode($payload, $part_number): mixed
{
if ($this->getEncoding() === 'base64') {
$content = base64_decode($payload);
}
$attachment = new TNEFAttachment();
$attachment->decodeTnef($content);
$files = $attachment->getFiles();
$parts = [];
foreach ($files as $file) {
$parts[] = new Part(base64_encode($file->content), $this->config, $this->createFakeHeaders($file), $part_number++);
}
return $parts;
}
public function getEncoding(): string
{
return $this->headers->get('content_transfer_encoding')();
}
public static function isTNEFEncoded($headers): bool
{
return $headers->get("content_type")->contains("application/ms-tnef");
}
private function createFakeHeaders($file)
{
$fileName = $file->name ?? 'winmail.dat';
$fileType = $file->type ?? 'application/ms-tnef';
$fakeHeaders = [
'Content-Type' => "$fileType; name=\"$fileName \"",
'Content-Description' => $fileName,
'Content-Disposition' => 'attachment; filename=\"' . $fileName . '\"',
'content_transfer_encoding' => 'base64',
];
$headers = implode(
"",
array_map(function ($key, $value) {
return $key . ": " . $value . "\r\n";
}, array_keys($fakeHeaders), $fakeHeaders)
);
return new Header($headers, $this->config);
}
}