1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace rdx\imap; |
4
|
|
|
|
5
|
|
|
class IMAPMessage implements IMAPMessagePartInterface { |
6
|
|
|
|
7
|
|
|
public $mailbox; // typeof IMAPMailbox |
8
|
|
|
|
9
|
|
|
public $msgNumber = 1; // starts at 1, not 0 |
10
|
|
|
public $header = ''; |
11
|
|
|
public $unseen = true; |
12
|
|
|
|
13
|
|
|
public $headers; // typeof stdClass |
14
|
|
|
public $structure; // typeof stdClass |
15
|
|
|
|
16
|
|
|
public $subject = ''; |
17
|
|
|
public $parts = array(); |
18
|
|
|
public $parameters = array(); |
19
|
|
|
public $plainBody; |
20
|
|
|
public $HTMLBody; |
21
|
|
|
public $attachments = array(); // typeof Array<IMAPMessageAttachment> |
22
|
|
|
|
23
|
|
|
public function __construct( IMAPMailbox $mailbox, $msgNumber, $header, $unseen ) { |
24
|
|
|
$this->mailbox = $mailbox; |
25
|
|
|
$this->msgNumber = $msgNumber; |
26
|
|
|
$this->header = $header; |
27
|
|
|
$this->unseen = $unseen; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function flags( $flags, $clear ) { |
31
|
|
|
$cb = $clear ? 'imap_clearflag_full' : 'imap_setflag_full'; |
32
|
|
|
|
33
|
|
|
$feedback = array(); |
34
|
|
|
foreach ( (array)$flags AS $flag ) { |
35
|
|
|
$flag = '\\' . ucfirst($flag); |
36
|
|
|
$feedback[] = $cb($this->mailbox->imap(), (string)$this->msgNumber, $flag); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return is_array($flags) ? $feedback : $feedback[0]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function flag( $flags ) { |
43
|
|
|
return $this->flags($flags, false); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function unflag( $flags ) { |
47
|
|
|
return $this->flags($flags, true); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function utc() { |
51
|
|
|
$headers = $this->headers(); |
52
|
|
|
return strtotime($headers->Date); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function subject() { |
56
|
|
|
if ( !$this->subject ) { |
57
|
|
|
$headers = $this->headers(); |
58
|
|
|
|
59
|
|
|
$subject = imap_utf8($headers->Subject); |
60
|
|
|
|
61
|
|
|
$this->subject = trim($subject); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->subject; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function headers() { |
68
|
|
|
if ( !$this->headers ) { |
69
|
|
|
$this->headers = imap_headerinfo($this->mailbox->imap(), $this->msgNumber); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->headers; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function parts() { |
76
|
|
|
if ( !$this->parts ) { |
|
|
|
|
77
|
|
|
$structure = $this->structure(); |
78
|
|
|
|
79
|
|
|
// Possibilities: |
80
|
|
|
// - PLAIN |
81
|
|
|
// - ALTERNATIVE |
82
|
|
|
// - MIXED |
83
|
|
|
// - DELIVERY-STATUS |
84
|
|
|
// - RFC822 |
85
|
|
|
// - REPORT |
86
|
|
|
// - HTML |
87
|
|
|
// - CALENDAR |
88
|
|
|
|
89
|
|
|
if ( empty($structure->parts) ) { |
90
|
|
|
$this->parts[] = new IMAPMessagePart( |
91
|
|
|
$this, |
92
|
|
|
$structure, |
93
|
|
|
[1] |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
else { |
97
|
|
|
foreach ($structure->parts as $n => $part) { |
98
|
|
|
$this->parts[] = new IMAPMessagePart( |
99
|
|
|
$this, |
100
|
|
|
$part, |
101
|
|
|
[$n+1] |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->parts; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
View Code Duplication |
public function allParts( $withContainers = false ) { |
|
|
|
|
111
|
|
|
$parts = []; |
112
|
|
|
$iterate = function($message) use (&$iterate, &$parts, $withContainers) { |
113
|
|
|
foreach ( $message->parts() as $part ) { |
114
|
|
|
if ( $part->parts() ) { |
115
|
|
|
if ( $withContainers ) { |
116
|
|
|
$parts[] = $part; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$iterate($part); |
120
|
|
|
} |
121
|
|
|
else { |
122
|
|
|
$parts[] = $part; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
}; |
126
|
|
|
|
127
|
|
|
$iterate($this); |
128
|
|
|
return $parts; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function part( $index ) { |
132
|
|
|
$parts = $this->parts(); |
133
|
|
|
return @$parts[$index]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function structure() { |
137
|
|
|
if ( !$this->structure ) { |
138
|
|
|
$this->structure = imap_fetchstructure($this->mailbox->imap(), $this->msgNumber); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this->structure; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function content() { |
145
|
|
|
if ( count($this->parts()) == 1 ) { |
146
|
|
|
$parts = $this->part(0)->content(); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return ''; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
View Code Duplication |
public function parameters() { |
|
|
|
|
153
|
|
|
if ( !$this->parameters ) { |
|
|
|
|
154
|
|
|
$structure = $this->structure(); |
155
|
|
|
|
156
|
|
|
$this->parameters['bytes'] = @$structure->bytes; |
157
|
|
|
|
158
|
|
|
foreach ((array) @$structure->parameters as $param) { |
159
|
|
|
$this->parameters[ strtolower($param->attribute) ] = $param->value; |
160
|
|
|
} |
161
|
|
|
foreach ((array) @$structure->dparameters as $param) { |
162
|
|
|
$this->parameters[ strtolower($param->attribute) ] = $param->value; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $this->parameters; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function simpleStructure() { |
170
|
|
|
$parts = []; |
171
|
|
|
foreach ( $this->allParts(true) as $part ) { |
172
|
|
|
$name = ''; |
173
|
|
|
|
174
|
|
|
$name .= implode('.', $part->section) . '. '; |
175
|
|
|
if ( $part->parts() ) { |
176
|
|
|
$name .= '*'; |
177
|
|
|
} |
178
|
|
|
$name .= $part->subtype; |
179
|
|
|
if ( $bytes = $part->parameter('bytes') ) { |
180
|
|
|
$name .= ' (' . $bytes . ')'; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$parts[] = $name; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $parts; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.