|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace rdx\imap; |
|
4
|
|
|
|
|
5
|
|
|
use rdx\imap\IMAPMailbox; |
|
6
|
|
|
use rdx\imap\IMAPMessageContent; |
|
7
|
|
|
use rdx\imap\IMAPMessagePart; |
|
8
|
|
|
|
|
9
|
|
|
class IMAPMessage extends IMAPMessageContent implements IMAPMessagePartInterface { |
|
10
|
|
|
|
|
11
|
|
|
protected $mailbox; // rdx\imap\IMAPMailbox |
|
12
|
|
|
|
|
13
|
|
|
protected $msgNumber = 1; // starts at 1, not 0 |
|
14
|
|
|
protected $unseen = true; |
|
15
|
|
|
|
|
16
|
|
|
protected $headers = []; |
|
17
|
|
|
protected $subject = ''; |
|
18
|
|
|
|
|
19
|
|
|
// protected $plainBody; |
|
20
|
|
|
// protected $HTMLBody; |
|
21
|
|
|
// protected $attachments = []; // Array<IMAPMessageAttachment> |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
public function __construct( IMAPMailbox $mailbox, $msgNumber, $unseen = null ) { |
|
24
|
|
|
$this->mailbox = $mailbox; |
|
25
|
|
|
$this->msgNumber = $msgNumber; |
|
26
|
|
|
$this->unseen = $unseen; |
|
27
|
|
|
|
|
28
|
|
|
if ( $unseen === null ) { |
|
29
|
|
|
$this->unseen = (bool) trim($this->header('unseen')); |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
protected function flags( $flags, $clear ) { |
|
34
|
|
|
$cb = [$this->imap(), $clear ? 'clearflag' : 'setflag']; |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
$feedback = []; |
|
37
|
|
|
foreach ( (array)$flags AS $flag ) { |
|
38
|
|
|
$flag = '\\' . ucfirst($flag); |
|
39
|
|
|
$feedback[] = call_user_func($cb, $this->msgNumber, $flag); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return is_array($flags) ? $feedback : $feedback[0]; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function flag( $flags ) { |
|
46
|
|
|
return $this->flags($flags, false); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function unflag( $flags ) { |
|
50
|
|
|
return $this->flags($flags, true); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function utc() { |
|
54
|
|
|
return strtotime($this->header('date')); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function subject() { |
|
58
|
|
|
if ( empty($this->subject) ) { |
|
59
|
|
|
$subject = $this->mailbox()->imap()->utf8($this->header('subject')); |
|
60
|
|
|
$this->subject = trim($subject); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $this->subject; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function headers() { |
|
67
|
|
|
if ( empty($this->headers) ) { |
|
68
|
|
|
$headers = $this->mailbox()->imap()->headerinfo($this->msgNumber); |
|
69
|
|
|
foreach ( $headers as $name => $value ) { |
|
70
|
|
|
$this->headers[ strtolower($name) ] = $value; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this->headers; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function header( $name ) { |
|
78
|
|
|
$headers = $this->headers(); |
|
79
|
|
|
return @$headers[ strtolower($name) ]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function createMessagePart( $structure, $section ) { |
|
83
|
|
|
return new IMAPMessagePart($this, $structure, $section); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function parts() { |
|
87
|
|
|
if ( empty($this->parts) ) { |
|
88
|
|
|
$structure = $this->structure(); |
|
89
|
|
|
|
|
90
|
|
|
// Possibilities: |
|
91
|
|
|
// - PLAIN |
|
92
|
|
|
// - ALTERNATIVE |
|
93
|
|
|
// - MIXED |
|
94
|
|
|
// - DELIVERY-STATUS |
|
95
|
|
|
// - RFC822 |
|
96
|
|
|
// - REPORT |
|
97
|
|
|
// - HTML |
|
98
|
|
|
// - CALENDAR |
|
99
|
|
|
// - JPEG |
|
100
|
|
|
|
|
101
|
|
|
if ( empty($structure->parts) ) { |
|
102
|
|
|
$this->parts[] = $this->createMessagePart( |
|
103
|
|
|
$structure, |
|
104
|
|
|
[1] |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
else { |
|
108
|
|
|
foreach ($structure->parts as $n => $part) { |
|
109
|
|
|
$this->parts[] = $this->createMessagePart( |
|
110
|
|
|
$part, |
|
111
|
|
|
[$n+1] |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $this->parts; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function structure() { |
|
121
|
|
|
if ( empty($this->structure) ) { |
|
122
|
|
|
$this->structure = $this->mailbox()->imap()->fetchstructure($this->msgNumber); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $this->structure; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function content() { |
|
129
|
|
|
if ( count($this->parts()) == 1 ) { |
|
130
|
|
|
return $this->part(0)->content(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return ''; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function simpleStructure() { |
|
137
|
|
|
$parts = []; |
|
138
|
|
|
foreach ( $this->allParts(true) as $part ) { |
|
139
|
|
|
$name = ''; |
|
140
|
|
|
|
|
141
|
|
|
$name .= implode('.', $part->section()) . '. '; |
|
142
|
|
|
if ( $part->parts() ) { |
|
143
|
|
|
$name .= '*'; |
|
144
|
|
|
} |
|
145
|
|
|
$name .= $part->subtype(); |
|
146
|
|
|
if ( $bytes = $part->parameter('bytes') ) { |
|
147
|
|
|
$name .= ' (' . $bytes . ')'; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$parts[] = $name; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $parts; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function msgNumber() { |
|
157
|
|
|
return $this->msgNumber; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function mailbox() { |
|
161
|
|
|
return $this->mailbox; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.