commit | author | age
|
8fa58e
|
1 |
<?php |
T |
2 |
|
|
3 |
/* |
|
4 |
+-----------------------------------------------------------------------+ |
|
5 |
| program/include/rcube_message.php | |
|
6 |
| | |
|
7 |
| This file is part of the RoundCube Webmail client | |
|
8 |
| Copyright (C) 2008, RoundCube Dev. - Switzerland | |
|
9 |
| Licensed under the GNU GPL | |
|
10 |
| | |
|
11 |
| PURPOSE: | |
|
12 |
| Logical representation of a mail message with all its data | |
|
13 |
| and related functions | |
|
14 |
+-----------------------------------------------------------------------+ |
|
15 |
| Author: Thomas Bruederli <roundcube@gmail.com> | |
|
16 |
+-----------------------------------------------------------------------+ |
|
17 |
|
|
18 |
$Id: rcube_imap.php 1344 2008-04-30 08:21:42Z thomasb $ |
|
19 |
|
|
20 |
*/ |
|
21 |
|
|
22 |
|
|
23 |
/** |
45f56c
|
24 |
* Logical representation of a mail message with all its data |
T |
25 |
* and related functions |
8fa58e
|
26 |
* |
T |
27 |
* @package Mail |
|
28 |
* @author Thomas Bruederli <roundcube@gmail.com> |
|
29 |
*/ |
|
30 |
class rcube_message |
|
31 |
{ |
|
32 |
private $app; |
|
33 |
private $imap; |
|
34 |
private $opt = array(); |
|
35 |
private $inline_parts = array(); |
|
36 |
private $parse_alternative = false; |
|
37 |
|
|
38 |
public $uid = null; |
|
39 |
public $headers; |
|
40 |
public $structure; |
|
41 |
public $parts = array(); |
|
42 |
public $mime_parts = array(); |
|
43 |
public $attachments = array(); |
|
44 |
public $subject = ''; |
|
45 |
public $is_safe = false; |
|
46 |
|
|
47 |
|
|
48 |
function __construct($uid) |
|
49 |
{ |
|
50 |
$this->app = rcmail::get_instance(); |
|
51 |
$this->imap = $this->app->imap; |
|
52 |
|
|
53 |
$this->uid = $uid; |
|
54 |
$this->headers = $this->imap->get_headers($uid); |
|
55 |
$this->subject = rcube_imap::decode_mime_string($this->headers->subject, $this->headers->charset); |
|
56 |
|
|
57 |
$this->is_safe = (intval($_GET['_safe']) || $_SESSION['safe_messages'][$uid]) ? true : false; |
|
58 |
$_SESSION['safe_messages'][$uid] = $this->is_safe; |
|
59 |
|
|
60 |
$this->opt = array( |
|
61 |
'safe' => $this->is_safe, |
|
62 |
'prefer_html' => $this->app->config->get('prefer_html'), |
|
63 |
'get_url' => rcmail_url('get', array('_mbox' => $this->imap->get_mailbox_name(), '_uid' => $uid)) |
|
64 |
); |
|
65 |
|
|
66 |
if ($this->structure = $this->imap->get_structure($uid)) { |
|
67 |
$this->get_mime_numbers($this->structure); |
45f56c
|
68 |
$this->parse_structure($this->structure); |
8fa58e
|
69 |
} |
T |
70 |
else { |
|
71 |
$this->body = $this->imap->get_body($uid); |
|
72 |
} |
|
73 |
} |
|
74 |
|
|
75 |
|
|
76 |
/** |
|
77 |
* Return a (decoded) message header |
|
78 |
* |
|
79 |
* @param string Header name |
|
80 |
* @param bool Don't mime-decode the value |
|
81 |
* @return string Header value |
|
82 |
*/ |
|
83 |
public function get_header($name, $raw = false) |
|
84 |
{ |
5d6ec2
|
85 |
$value = $this->headers->$name; |
8fa58e
|
86 |
return $raw ? $value : $this->imap->decode_header($value); |
T |
87 |
} |
|
88 |
|
712b30
|
89 |
/** |
A |
90 |
* Set is_safe var and session data |
|
91 |
* |
|
92 |
* @param bool enable/disable |
|
93 |
*/ |
|
94 |
public function set_safe($safe = true) |
|
95 |
{ |
|
96 |
$this->is_safe = $safe; |
|
97 |
$_SESSION['safe_messages'][$this->uid] = $this->is_safe; |
|
98 |
} |
8fa58e
|
99 |
|
T |
100 |
/** |
|
101 |
* Compose a valid URL for getting a message part |
|
102 |
* |
|
103 |
* @param string Part MIME-ID |
|
104 |
* @return string URL or false if part does not exist |
|
105 |
*/ |
|
106 |
public function get_part_url($mime_id) |
|
107 |
{ |
|
108 |
if ($this->mime_parts[$mime_id]) |
|
109 |
return $this->opt['get_url'] . "&_part=" . $mime_id; |
|
110 |
else |
|
111 |
return false; |
|
112 |
} |
|
113 |
|
|
114 |
|
|
115 |
/** |
|
116 |
* Get content of a specific part of this message |
|
117 |
* |
|
118 |
* @param string Part MIME-ID |
|
119 |
* @return string Part content |
|
120 |
*/ |
|
121 |
public function get_part_content($mime_id) |
|
122 |
{ |
|
123 |
if ($part = $this->mime_parts[$mime_id]) |
|
124 |
return $this->imap->get_message_part($this->uid, $mime_id, $part); |
|
125 |
else |
|
126 |
return null; |
|
127 |
} |
|
128 |
|
|
129 |
|
|
130 |
/** |
|
131 |
* Determine if the message contains a HTML part |
|
132 |
* |
|
133 |
* @return bool True if a HTML is available, False if not |
|
134 |
*/ |
|
135 |
function has_html_part() |
|
136 |
{ |
|
137 |
// check all message parts |
|
138 |
foreach ($this->parts as $pid => $part) { |
|
139 |
$mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary); |
|
140 |
if ($mimetype == 'text/html') |
|
141 |
return true; |
|
142 |
} |
|
143 |
|
|
144 |
return false; |
|
145 |
} |
|
146 |
|
|
147 |
/** |
|
148 |
* Return the first HTML part of this message |
|
149 |
* |
|
150 |
* @return string HTML message part content |
|
151 |
*/ |
|
152 |
function first_html_part() |
|
153 |
{ |
|
154 |
$html_part = null; |
|
155 |
|
|
156 |
// check all message parts |
|
157 |
foreach ($this->mime_parts as $mime_id => $part) { |
|
158 |
$mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary); |
|
159 |
if ($mimetype == 'text/html') { |
|
160 |
$html_part = $this->imap->get_message_part($this->uid, $mime_id, $part); |
|
161 |
} |
|
162 |
} |
|
163 |
|
|
164 |
return $html_part; |
|
165 |
} |
|
166 |
|
|
167 |
|
|
168 |
/** |
|
169 |
* Return the first text part of this message |
|
170 |
* |
|
171 |
* @return string Plain text message/part content |
|
172 |
*/ |
|
173 |
function first_text_part() |
|
174 |
{ |
|
175 |
// no message structure, return complete body |
|
176 |
if (empty($this->parts)) |
|
177 |
return $this->body; |
|
178 |
|
|
179 |
$out = null; |
|
180 |
|
|
181 |
// check all message parts |
|
182 |
foreach ($this->mime_parts as $mime_id => $part) { |
|
183 |
$mimetype = strtolower($part->ctype_primary . '/' . $part->ctype_secondary); |
|
184 |
|
|
185 |
if ($mimetype == 'text/plain') { |
|
186 |
$out = $this->imap->get_message_part($this->uid, $mime_id, $part); |
|
187 |
break; |
|
188 |
} |
|
189 |
else if ($mimetype == 'text/html') { |
|
190 |
$html_part = $this->imap->get_message_part($this->uid, $mime_id, $part); |
|
191 |
|
|
192 |
// remove special chars encoding |
|
193 |
$trans = array_flip(get_html_translation_table(HTML_ENTITIES)); |
|
194 |
$html_part = strtr($html_part, $trans); |
|
195 |
|
|
196 |
// create instance of html2text class |
|
197 |
$txt = new html2text($html_part); |
|
198 |
$out = $txt->get_text(); |
|
199 |
break; |
|
200 |
} |
|
201 |
} |
|
202 |
|
|
203 |
return $out; |
|
204 |
} |
|
205 |
|
|
206 |
|
|
207 |
/** |
|
208 |
* Raad the message structure returend by the IMAP server |
|
209 |
* and build flat lists of content parts and attachments |
|
210 |
* |
|
211 |
* @param object rcube_message_part Message structure node |
|
212 |
* @param bool True when called recursively |
|
213 |
*/ |
|
214 |
private function parse_structure($structure, $recursive = false) |
|
215 |
{ |
|
216 |
$message_ctype_primary = strtolower($structure->ctype_primary); |
|
217 |
$message_ctype_secondary = strtolower($structure->ctype_secondary); |
|
218 |
|
|
219 |
// show message headers |
|
220 |
if ($recursive && is_array($structure->headers) && isset($structure->headers['subject'])) { |
|
221 |
$c = new stdClass; |
|
222 |
$c->type = 'headers'; |
|
223 |
$c->headers = &$structure->headers; |
|
224 |
$this->parts[] = $c; |
|
225 |
} |
|
226 |
|
|
227 |
// print body if message doesn't have multiple parts |
|
228 |
if ($message_ctype_primary == 'text' && !$recursive) { |
|
229 |
$structure->type = 'content'; |
|
230 |
$this->parts[] = &$structure; |
|
231 |
} |
|
232 |
// message contains alternative parts |
|
233 |
else if ($message_ctype_primary == 'multipart' && ($message_ctype_secondary == 'alternative') && is_array($structure->parts)) { |
|
234 |
// get html/plaintext parts |
|
235 |
$plain_part = $html_part = $print_part = $related_part = null; |
|
236 |
|
|
237 |
foreach ($structure->parts as $p => $sub_part) { |
|
238 |
$rel_parts = $attachmnts = null; |
|
239 |
$sub_ctype_primary = strtolower($sub_part->ctype_primary); |
|
240 |
$sub_ctype_secondary = strtolower($sub_part->ctype_secondary); |
|
241 |
|
|
242 |
// check if sub part is |
|
243 |
if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='plain') |
|
244 |
$plain_part = $p; |
|
245 |
else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='html') |
|
246 |
$html_part = $p; |
|
247 |
else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='enriched') |
|
248 |
$enriched_part = $p; |
|
249 |
else if ($sub_ctype_primary=='multipart' && ($sub_ctype_secondary=='related' || $sub_ctype_secondary=='mixed')) |
|
250 |
$related_part = $p; |
|
251 |
} |
|
252 |
|
|
253 |
// parse related part (alternative part could be in here) |
|
254 |
if ($related_part !== null && !$this->parse_alternative) { |
|
255 |
$this->parse_alternative = true; |
|
256 |
$this->parse_structure($structure->parts[$related_part], true); |
|
257 |
$this->parse_alternative = false; |
|
258 |
|
|
259 |
// if plain part was found, we should unset it if html is preferred |
|
260 |
if ($this->opt['prefer_html'] && count($this->parts)) |
|
261 |
$plain_part = null; |
|
262 |
} |
|
263 |
|
|
264 |
// choose html/plain part to print |
|
265 |
if ($html_part !== null && $this->opt['prefer_html']) { |
|
266 |
$print_part = &$structure->parts[$html_part]; |
|
267 |
} |
|
268 |
else if ($enriched_part !== null) { |
|
269 |
$print_part = &$structure->parts[$enriched_part]; |
|
270 |
} |
|
271 |
else if ($plain_part !== null) { |
|
272 |
$print_part = &$structure->parts[$plain_part]; |
|
273 |
} |
|
274 |
|
|
275 |
// add the right message body |
|
276 |
if (is_object($print_part)) { |
|
277 |
$print_part->type = 'content'; |
|
278 |
$this->parts[] = $print_part; |
|
279 |
} |
|
280 |
// show plaintext warning |
|
281 |
else if ($html_part !== nullL && empty($this->parts)) { |
|
282 |
$c = new stdClass; |
|
283 |
$c->type = 'content'; |
|
284 |
$c->body = rcube_label('htmlmessage'); |
|
285 |
$c->ctype_primary = 'text'; |
|
286 |
$c->ctype_secondary = 'plain'; |
|
287 |
|
|
288 |
$this->parts[] = $c; |
|
289 |
} |
|
290 |
|
|
291 |
// add html part as attachment |
|
292 |
if ($html_part !== null && $structure->parts[$html_part] !== $print_part) { |
|
293 |
$html_part = &$structure->parts[$html_part]; |
|
294 |
$html_part->filename = rcube_label('htmlmessage'); |
|
295 |
$html_part->mimetype = 'text/html'; |
|
296 |
|
|
297 |
$this->attachments[] = $html_part; |
|
298 |
} |
|
299 |
} |
|
300 |
// this is an ecrypted message -> create a plaintext body with the according message |
|
301 |
else if ($message_ctype_primary == 'multipart' && $message_ctype_secondary == 'encrypted') { |
|
302 |
$p = new stdClass; |
|
303 |
$p->type = 'content'; |
|
304 |
$p->ctype_primary = 'text'; |
|
305 |
$p->ctype_secondary = 'plain'; |
|
306 |
$p->body = rcube_label('encryptedmessage'); |
|
307 |
|
|
308 |
$this->parts[] = $p; |
|
309 |
} |
|
310 |
// message contains multiple parts |
|
311 |
else if (is_array($structure->parts) && !empty($structure->parts)) { |
|
312 |
// iterate over parts |
|
313 |
for ($i=0; $i < count($structure->parts); $i++) { |
|
314 |
$mail_part = &$structure->parts[$i]; |
|
315 |
$primary_type = strtolower($mail_part->ctype_primary); |
|
316 |
$secondary_type = strtolower($mail_part->ctype_secondary); |
|
317 |
|
|
318 |
// multipart/alternative |
|
319 |
if ($primary_type=='multipart') { |
|
320 |
$this->parse_structure($mail_part, true); |
|
321 |
} |
|
322 |
// part text/[plain|html] OR message/delivery-status |
|
323 |
else if (($primary_type == 'text' && ($secondary_type == 'plain' || $secondary_type == 'html') && $mail_part->disposition != 'attachment') || |
|
324 |
($primary_type == 'message' && ($secondary_type == 'delivery-status' || $secondary_type == 'disposition-notification'))) { |
|
325 |
|
|
326 |
// add text part if we're not in alternative mode or if it matches the prefs |
|
327 |
if (!$this->parse_alternative || |
|
328 |
($secondary_type == 'html' && $this->opt['prefer_html']) || |
|
329 |
($secondary_type == 'plain' && !$this->opt['prefer_html'])) { |
|
330 |
$mail_part->type = 'content'; |
|
331 |
$this->parts[] = $mail_part; |
|
332 |
} |
|
333 |
|
|
334 |
// list as attachment as well |
|
335 |
if (!empty($mail_part->filename)) |
|
336 |
$this->attachments[] = $mail_part; |
|
337 |
} |
|
338 |
// part message/* |
|
339 |
else if ($primary_type=='message') { |
|
340 |
$this->parse_structure($mail_part, true); |
|
341 |
} |
|
342 |
// ignore "virtual" protocol parts |
|
343 |
else if ($primary_type == 'protocol') |
|
344 |
continue; |
|
345 |
|
|
346 |
// part is file/attachment |
|
347 |
else if ($mail_part->disposition == 'attachment' || $mail_part->disposition == 'inline' || |
|
348 |
$mail_part->headers['content-id'] || (empty($mail_part->disposition) && $mail_part->filename)) { |
|
349 |
// skip apple resource forks |
|
350 |
if ($message_ctype_secondary == 'appledouble' && $secondary_type == 'applefile') |
|
351 |
continue; |
|
352 |
|
|
353 |
// part belongs to a related message |
|
354 |
if ($message_ctype_secondary == 'related' && $mail_part->headers['content-id']) { |
|
355 |
$mail_part->content_id = preg_replace(array('/^</', '/>$/'), '', $mail_part->headers['content-id']); |
|
356 |
$this->inline_parts[] = $mail_part; |
|
357 |
} |
|
358 |
// is regular attachment |
|
359 |
else { |
|
360 |
if (!$mail_part->filename) |
|
361 |
$mail_part->filename = 'Part '.$mail_part->mime_id; |
|
362 |
$this->attachments[] = $mail_part; |
|
363 |
} |
|
364 |
} |
|
365 |
} |
|
366 |
|
|
367 |
// if this was a related part try to resolve references |
45f56c
|
368 |
if ($message_ctype_secondary == 'related' && sizeof($this->inline_parts)) { |
8fa58e
|
369 |
$a_replaces = array(); |
T |
370 |
|
|
371 |
foreach ($this->inline_parts as $inline_object) { |
45f56c
|
372 |
$a_replaces['cid:'.$inline_object->content_id] = $this->get_part_url($inline_object->mime_id); |
8fa58e
|
373 |
} |
T |
374 |
|
|
375 |
// add replace array to each content part |
|
376 |
// (will be applied later when part body is available) |
45f56c
|
377 |
foreach ($this->parts as $i => $part) { |
T |
378 |
if ($part->type == 'content') |
|
379 |
$this->parts[$i]->replaces = $a_replaces; |
8fa58e
|
380 |
} |
T |
381 |
} |
|
382 |
} |
|
383 |
|
|
384 |
// message is single part non-text |
|
385 |
else if ($structure->filename) { |
|
386 |
$this->attachments[] = $structure; |
|
387 |
} |
|
388 |
} |
|
389 |
|
|
390 |
|
|
391 |
/** |
|
392 |
* Fill aflat array with references to all parts, indexed by part numbers |
|
393 |
* |
|
394 |
* @param object rcube_message_part Message body structure |
|
395 |
*/ |
|
396 |
private function get_mime_numbers(&$part) |
|
397 |
{ |
|
398 |
if (strlen($part->mime_id)) |
|
399 |
$this->mime_parts[$part->mime_id] = &$part; |
|
400 |
|
|
401 |
if (is_array($part->parts)) |
|
402 |
for ($i=0; $i<count($part->parts); $i++) |
|
403 |
$this->get_mime_numbers($part->parts[$i]); |
|
404 |
} |
|
405 |
|
|
406 |
|
|
407 |
} |
|
408 |
|