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 ( $this->parts === null ) { |
31
|
|
|
$this->parts = []; |
32
|
|
|
|
33
|
|
|
if ( !empty($this->structure()->parts) ) { |
34
|
|
|
$parts = $this->structure()->parts; |
35
|
|
|
while ( count($parts) == 1 && empty($parts[0]->bytes) && !empty($parts[0]->parts) ) { |
36
|
|
|
$this->skippedParts[] = $parts[0]; |
37
|
|
|
$parts = $parts[0]->parts; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
foreach ( $parts as $n => $part ) { |
41
|
|
|
$this->parts[] = $this->message()->createMessagePart( |
42
|
|
|
$part, |
43
|
|
|
array_merge($this->section(), [$n + 1]) |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this->parts; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** @return object */ |
53
|
|
|
public function structure() { |
54
|
|
|
return $this->structure; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** @return string */ |
58
|
|
|
public function filename() { |
59
|
|
|
return $this->parameter('filename') ?: $this->parameter('name'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** @return string */ |
63
|
|
|
public function safeFilename() { |
64
|
|
|
if ( $filename = $this->filename() ) { |
65
|
|
|
return trim(preg_replace('#_+#', '_', preg_replace('#[^a-z0-9\-_\.]+#i', '', $filename)), '_'); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** @return string */ |
70
|
|
|
public function content() { |
71
|
|
|
return $this->message()->mailbox()->imap()->fetchbody( |
72
|
|
|
$this->message()->msgNumber(), |
73
|
|
|
implode('.', $this->section()) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** @return string */ |
78
|
|
|
public function decodedContent() { |
79
|
|
|
if ( $raw = $this->content() ) { |
80
|
|
|
switch ( $this->parameter('encoding') ) { |
81
|
|
|
case ENC7BIT: |
82
|
|
|
case ENC8BIT: |
83
|
|
|
case ENCQUOTEDPRINTABLE: |
84
|
|
|
return quoted_printable_decode($raw); |
85
|
|
|
|
86
|
|
|
case ENCBASE64: |
87
|
|
|
return base64_decode($raw); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $raw; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** @return string */ |
95
|
|
|
public function headerString() { |
96
|
|
|
$x = explode("\r\n\r\n", $this->content()); |
97
|
|
|
if ( count($x) > 1 ) { |
98
|
|
|
return trim($x[0]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return ''; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** @return bool */ |
105
|
|
|
public function saveAttachment( $dir, $filenameOverride = null ) { |
106
|
|
|
if ( $filename = $this->safeFilename() ) { |
107
|
|
|
$filepath = realpath($dir) . '/' . ($filenameOverride ?? $filename); |
108
|
|
|
if ( file_put_contents($filepath, $this->decodedContent()) ) { |
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** @return IMAPMessage */ |
117
|
|
|
public function message() { |
118
|
|
|
return $this->message; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** @return IMAPMailbox */ |
122
|
|
|
public function mailbox() { |
123
|
|
|
return $this->message()->mailbox(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** @return int[] */ |
127
|
|
|
public function section() { |
128
|
|
|
return $this->section; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** @return string */ |
132
|
|
|
public function subtype() { |
133
|
|
|
return strtoupper($this->subtype); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
|