|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace rdx\imap; |
|
4
|
|
|
|
|
5
|
|
|
class IMAPMessagePart extends IMAPMessageContent implements IMAPMessagePartInterface { |
|
6
|
|
|
|
|
7
|
|
|
/** @var int[] */ |
|
8
|
|
|
protected $section = []; |
|
9
|
|
|
|
|
10
|
|
|
/** @var string */ |
|
11
|
|
|
protected $subtype = ''; |
|
12
|
|
|
|
|
13
|
|
|
/** @var IMAPMessage */ |
|
14
|
|
|
protected $message; |
|
15
|
|
|
|
|
16
|
|
|
/** @var object[] */ |
|
17
|
|
|
protected $skippedParts = []; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct( IMAPMessage $message, $structure, array $section ) { |
|
20
|
|
|
$this->message = $message; |
|
21
|
|
|
$this->structure = $structure; |
|
22
|
|
|
$this->section = $section; |
|
23
|
|
|
$this->subtype = strtoupper($structure->subtype); |
|
24
|
|
|
|
|
25
|
|
|
$this->parts(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** @return IMAPMessagePart[] */ |
|
29
|
|
|
public function parts() { |
|
30
|
|
|
if ( empty($this->parts) && !empty($this->structure()->parts) ) { |
|
31
|
|
|
$parts = $this->structure()->parts; |
|
32
|
|
|
while ( count($parts) == 1 && empty($parts[0]->bytes) && !empty($parts[0]->parts) ) { |
|
33
|
|
|
$this->skippedParts[] = $parts[0]; |
|
34
|
|
|
$parts = $parts[0]->parts; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
foreach ( $parts as $n => $part ) { |
|
38
|
|
|
$this->parts[] = $this->message()->createMessagePart( |
|
39
|
|
|
$part, |
|
40
|
|
|
array_merge($this->section(), [$n + 1]) |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $this->parts; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** @return object */ |
|
49
|
|
|
public function structure() { |
|
50
|
|
|
return $this->structure; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** @return string */ |
|
54
|
|
|
public function filename() { |
|
55
|
|
|
return $this->parameter('filename') ?: $this->parameter('name'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** @return string */ |
|
59
|
|
|
public function safeFilename() { |
|
60
|
|
|
if ($filename = $this->filename()) { |
|
61
|
|
|
return trim(preg_replace('#_+#', '_', preg_replace('#[^a-z0-9\-_\.]+#i', '', $filename)), '_'); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** @return string */ |
|
66
|
|
|
public function content() { |
|
67
|
|
|
return $this->message()->mailbox()->imap()->fetchbody( |
|
68
|
|
|
$this->message()->msgNumber(), |
|
69
|
|
|
implode('.', $this->section()) |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** @return string */ |
|
74
|
|
|
public function decodedContent() { |
|
75
|
|
|
if ($raw = $this->content()) { |
|
76
|
|
|
switch ($this->parameter('encoding')) { |
|
77
|
|
|
case ENC7BIT: |
|
78
|
|
|
case ENC8BIT: |
|
79
|
|
|
case ENCQUOTEDPRINTABLE: |
|
80
|
|
|
return quoted_printable_decode($raw); |
|
81
|
|
|
|
|
82
|
|
|
case ENCBASE64: |
|
83
|
|
|
return base64_decode($raw); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $raw; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** @return bool */ |
|
91
|
|
|
public function saveAttachment($dir, $filenameOverride = null) { |
|
92
|
|
|
if ($filename = $this->safeFilename()) { |
|
93
|
|
|
$filepath = realpath($dir) . '/' . ($filenameOverride ?? $filename); |
|
94
|
|
|
if (file_put_contents($filepath, $this->decodedContent())) { |
|
95
|
|
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** @return IMAPMessage */ |
|
103
|
|
|
public function message() { |
|
104
|
|
|
return $this->message; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** @return IMAPMailbox */ |
|
108
|
|
|
public function mailbox() { |
|
109
|
|
|
return $this->message()->mailbox(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** @return int[] */ |
|
113
|
|
|
public function section() { |
|
114
|
|
|
return $this->section; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** @return string */ |
|
118
|
|
|
public function subtype() { |
|
119
|
|
|
return $this->subtype; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|