4 +-----------------------------------------------------------------------+
5 | program/steps/mail/func.inc |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2005-2007, RoundCube Dev. - Switzerland |
9 | Licensed under the GNU GPL |
12 | Provide webmail functionality and GUI objects |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
18 $Id: func.inc 875 2007-10-17 06:50:28Z thomasb $
22 require_once('lib/html2text.inc');
23 require_once('lib/enriched.inc');
26 $EMAIL_ADDRESS_PATTERN = '/([a-z0-9][a-z0-9\-\.\+\_]*@[a-z0-9]([a-z0-9\-][.]?)*[a-z0-9]\\.[a-z]{2,5})/i';
28 if (empty($_SESSION['mbox']))
29 $_SESSION['mbox'] = $IMAP->get_mailbox_name();
31 // set imap properties and session vars
32 if ($mbox = get_input_value('_mbox', RCUBE_INPUT_GPC))
34 $IMAP->set_mailbox($mbox);
35 $_SESSION['mbox'] = $mbox;
38 if (!empty($_GET['_page']))
40 $IMAP->set_page((int)$_GET['_page']);
41 $_SESSION['page'] = (int)$_GET['_page'];
44 // set mailbox to INBOX if not set
45 if (empty($_SESSION['mbox']))
46 $_SESSION['mbox'] = $IMAP->get_mailbox_name();
48 // set default sort col/order to session
49 if (!isset($_SESSION['sort_col']))
50 $_SESSION['sort_col'] = $CONFIG['message_sort_col'];
51 if (!isset($_SESSION['sort_order']))
52 $_SESSION['sort_order'] = $CONFIG['message_sort_order'];
54 // set message set for search result
55 if (!empty($_REQUEST['_search']) && isset($_SESSION['search'][$_REQUEST['_search']]))
56 $IMAP->set_search_set($_SESSION['search'][$_REQUEST['_search']]);
59 // define url for getting message parts
60 if (strlen($_GET['_uid']))
61 $GET_URL = rcmail_url('get', array('_mbox'=>$IMAP->get_mailbox_name(), '_uid'=>get_input_value('_uid', RCUBE_INPUT_GET)));
64 // set current mailbox in client environment
65 $OUTPUT->set_env('mailbox', $IMAP->get_mailbox_name());
66 $OUTPUT->set_env('quota', $IMAP->get_capability('quota'));
68 if ($CONFIG['trash_mbox'])
69 $OUTPUT->set_env('trash_mailbox', $CONFIG['trash_mbox']);
70 if ($CONFIG['drafts_mbox'])
71 $OUTPUT->set_env('drafts_mailbox', $CONFIG['drafts_mbox']);
72 if ($CONFIG['junk_mbox'])
73 $OUTPUT->set_env('junk_mailbox', $CONFIG['junk_mbox']);
75 if (!$OUTPUT->ajax_call)
76 rcube_add_label('checkingmail', 'deletemessage', 'movemessagetotrash');
79 if (empty($_action) || $_action == 'list')
80 $OUTPUT->set_pagetitle(rcube_charset_convert($IMAP->get_mailbox_name(), 'UTF-7'));
84 // return the message list as HTML table
85 function rcmail_message_list($attrib)
87 global $IMAP, $CONFIG, $COMM_PATH, $OUTPUT;
89 $skin_path = $CONFIG['skin_path'];
90 $image_tag = '<img src="%s%s" alt="%s" border="0" />';
92 // check to see if we have some settings for sorting
93 $sort_col = $_SESSION['sort_col'];
94 $sort_order = $_SESSION['sort_order'];
96 // add some labels to client
97 rcube_add_label('from', 'to');
99 // get message headers
100 $a_headers = $IMAP->list_headers('', '', $sort_col, $sort_order);
102 // add id to message list table if not specified
103 if (!strlen($attrib['id']))
104 $attrib['id'] = 'rcubemessagelist';
106 // allow the following attributes to be added to the <table> tag
107 $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border', 'summary'));
109 $out = '<table' . $attrib_str . ">\n";
112 // define list of cols to be displayed
113 $a_show_cols = is_array($CONFIG['list_cols']) ? $CONFIG['list_cols'] : array('subject');
114 $a_sort_cols = array('subject', 'date', 'from', 'to', 'size');
116 $mbox = $IMAP->get_mailbox_name();
118 // show 'to' instead of from in sent messages
119 if (($mbox==$CONFIG['sent_mbox'] || $mbox==$CONFIG['drafts_mbox']) && ($f = array_search('from', $a_show_cols))
120 && !array_search('to', $a_show_cols))
121 $a_show_cols[$f] = 'to';
123 // add col definition
124 $out .= '<colgroup>';
125 $out .= '<col class="icon" />';
127 foreach ($a_show_cols as $col)
128 $out .= sprintf('<col class="%s" />', $col);
130 $out .= '<col class="icon" />';
131 $out .= "</colgroup>\n";
134 $out .= "<thead><tr>\n<td class=\"icon\"> </td>\n";
137 foreach ($a_show_cols as $col)
140 $col_name = Q(rcube_label($col));
144 if ($IMAP->get_capability('sort') && in_array($col, $a_sort_cols))
146 // have buttons configured
147 if (!empty($attrib['sortdescbutton']) || !empty($attrib['sortascbutton']))
149 $sort = ' ';
152 if (!empty($attrib['sortascbutton']))
154 $sort .= $OUTPUT->button(array(
156 'prop' => $col.'_ASC',
157 'image' => $attrib['sortascbutton'],
158 'align' => 'absmiddle',
159 'title' => 'sortasc'));
163 if (!empty($attrib['sortdescbutton']))
165 $sort .= $OUTPUT->button(array(
167 'prop' => $col.'_DESC',
168 'image' => $attrib['sortdescbutton'],
169 'align' => 'absmiddle',
170 'title' => 'sortdesc'));
173 // just add a link tag to the header
177 '<a href="./#sort" onclick="return %s.command(\'sort\',\'%s\',this)" title="%s">%s</a>',
180 rcube_label('sortby'),
185 $sort_class = $col==$sort_col ? " sorted$sort_order" : '';
187 // put it all together
188 $out .= '<td class="'.$col.$sort_class.'" id="rcmHead'.$col.'">' . "$col_name$sort</td>\n";
191 $out .= '<td class="icon">'.($attrib['attachmenticon'] ? sprintf($image_tag, $skin_path, $attrib['attachmenticon'], '') : '')."</td>\n";
192 $out .= "</tr></thead>\n<tbody>\n";
194 // no messages in this mailbox
195 if (!sizeof($a_headers))
196 $OUTPUT->show_message('nomessagesfound', 'notice');
199 $a_js_message_arr = array();
201 // create row for each message
202 foreach ($a_headers as $i => $header) //while (list($i, $header) = each($a_headers))
204 $message_icon = $attach_icon = '';
205 $js_row_arr = array();
206 $zebra_class = $i%2 ? 'even' : 'odd';
208 // set messag attributes to javascript array
209 if ($header->deleted)
210 $js_row_arr['deleted'] = true;
212 $js_row_arr['unread'] = true;
213 if ($header->answered)
214 $js_row_arr['replied'] = true;
216 if ($attrib['deletedicon'] && $header->deleted)
217 $message_icon = $attrib['deletedicon'];
218 else if ($attrib['unreadicon'] && !$header->seen)
219 $message_icon = $attrib['unreadicon'];
220 else if ($attrib['repliedicon'] && $header->answered)
221 $message_icon = $attrib['repliedicon'];
222 else if ($attrib['messageicon'])
223 $message_icon = $attrib['messageicon'];
225 // set attachment icon
226 if ($attrib['attachmenticon'] && preg_match("/multipart\/[mr]/i", $header->ctype))
227 $attach_icon = $attrib['attachmenticon'];
229 $out .= sprintf('<tr id="rcmrow%d" class="message%s%s %s">'."\n",
231 $header->seen ? '' : ' unread',
232 $header->deleted ? ' deleted' : '',
235 $out .= sprintf("<td class=\"icon\">%s</td>\n", $message_icon ? sprintf($image_tag, $skin_path, $message_icon, '') : '');
238 foreach ($a_show_cols as $col)
240 if ($col=='from' || $col=='to')
241 $cont = Q(rcmail_address_string($header->$col, 3, $attrib['addicon']), 'show');
242 else if ($col=='subject')
244 $action = $mbox==$CONFIG['drafts_mbox'] ? 'compose' : 'show';
245 $uid_param = $mbox==$CONFIG['drafts_mbox'] ? '_draf_uid' : '_uid';
246 $cont = Q(rcube_imap::decode_mime_string($header->$col, $header->charset));
247 if (empty($cont)) $cont = Q(rcube_label('nosubject'));
248 $cont = sprintf('<a href="%s" onclick="return rcube_event.cancel(event)">%s</a>', Q(rcmail_url($action, array($uid_param=>$header->uid, '_mbox'=>$mbox))), $cont);
250 else if ($col=='size')
251 $cont = show_bytes($header->$col);
252 else if ($col=='date')
253 $cont = format_date($header->date);
255 $cont = Q($header->$col);
257 $out .= '<td class="'.$col.'">' . $cont . "</td>\n";
260 $out .= sprintf("<td class=\"icon\">%s</td>\n", $attach_icon ? sprintf($image_tag, $skin_path, $attach_icon, '') : '');
263 if (sizeof($js_row_arr))
264 $a_js_message_arr[$header->uid] = $js_row_arr;
267 // complete message table
268 $out .= "</tbody></table>\n";
271 $message_count = $IMAP->messagecount();
274 $OUTPUT->add_gui_object('mailcontframe', 'mailcontframe');
275 $OUTPUT->add_gui_object('messagelist', $attrib['id']);
276 $OUTPUT->set_env('messagecount', $message_count);
277 $OUTPUT->set_env('current_page', $IMAP->list_page);
278 $OUTPUT->set_env('pagecount', ceil($message_count/$IMAP->page_size));
279 $OUTPUT->set_env('sort_col', $sort_col);
280 $OUTPUT->set_env('sort_order', $sort_order);
282 if ($attrib['messageicon'])
283 $OUTPUT->set_env('messageicon', $skin_path . $attrib['messageicon']);
284 if ($attrib['deletedicon'])
285 $OUTPUT->set_env('deletedicon', $skin_path . $attrib['deletedicon']);
286 if ($attrib['unreadicon'])
287 $OUTPUT->set_env('unreadicon', $skin_path . $attrib['unreadicon']);
288 if ($attrib['repliedicon'])
289 $OUTPUT->set_env('repliedicon', $skin_path . $attrib['repliedicon']);
290 if ($attrib['attachmenticon'])
291 $OUTPUT->set_env('attachmenticon', $skin_path . $attrib['attachmenticon']);
293 $OUTPUT->set_env('messages', $a_js_message_arr);
295 $OUTPUT->include_script('list.js');
301 // return javascript commands to add rows to the message list
302 function rcmail_js_message_list($a_headers, $insert_top=FALSE)
304 global $CONFIG, $IMAP, $OUTPUT;
306 $a_show_cols = is_array($CONFIG['list_cols']) ? $CONFIG['list_cols'] : array('subject');
307 $mbox = $IMAP->get_mailbox_name();
309 // show 'to' instead of from in sent messages
310 if (($mbox == $CONFIG['sent_mbox'] || $mbox == $CONFIG['drafts_mbox'])
311 && (($f = array_search('from', $a_show_cols)) !== false) && array_search('to', $a_show_cols) === false)
312 $a_show_cols[$f] = 'to';
314 $OUTPUT->command('set_message_coltypes', $a_show_cols);
316 // loop through message headers
317 foreach ($a_headers as $n => $header)
319 $a_msg_cols = array();
320 $a_msg_flags = array();
325 // format each col; similar as in rcmail_message_list()
326 foreach ($a_show_cols as $col)
328 if ($col=='from' || $col=='to')
329 $cont = Q(rcmail_address_string($header->$col, 3), 'show');
330 else if ($col=='subject')
332 $action = $mbox==$CONFIG['drafts_mbox'] ? 'compose' : 'show';
333 $uid_param = $mbox==$CONFIG['drafts_mbox'] ? '_draf_uid' : '_uid';
334 $cont = Q(rcube_imap::decode_mime_string($header->$col, $header->charset));
335 if (!$cont) $cont = Q(rcube_label('nosubject'));
336 $cont = sprintf('<a href="%s" onclick="return rcube_event.cancel(event)">%s</a>', Q(rcmail_url($action, array($uid_param=>$header->uid, '_mbox'=>$mbox))), $cont);
338 else if ($col=='size')
339 $cont = show_bytes($header->$col);
340 else if ($col=='date')
341 $cont = format_date($header->date);
343 $cont = Q($header->$col);
345 $a_msg_cols[$col] = $cont;
348 $a_msg_flags['deleted'] = $header->deleted ? 1 : 0;
349 $a_msg_flags['unread'] = $header->seen ? 0 : 1;
350 $a_msg_flags['replied'] = $header->answered ? 1 : 0;
351 $OUTPUT->command('add_message_row',
355 preg_match("/multipart\/m/i", $header->ctype),
361 // return an HTML iframe for loading mail content
362 function rcmail_messagecontent_frame($attrib)
366 if (empty($attrib['id']))
367 $attrib['id'] = 'rcmailcontentwindow';
369 // allow the following attributes to be added to the <iframe> tag
370 $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'src', 'width', 'height', 'frameborder'));
371 $framename = $attrib['id'];
373 $out = sprintf('<iframe name="%s"%s></iframe>'."\n",
377 $OUTPUT->set_env('contentframe', $framename);
378 $OUTPUT->set_env('blankpage', $attrib['src'] ? $OUTPUT->abs_url($attrib['src']) : 'program/blank.gif');
384 function rcmail_messagecount_display($attrib)
386 global $IMAP, $OUTPUT;
389 $attrib['id'] = 'rcmcountdisplay';
391 $OUTPUT->add_gui_object('countdisplay', $attrib['id']);
393 // allow the following attributes to be added to the <span> tag
394 $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id'));
397 $out = '<span' . $attrib_str . '>';
398 $out .= rcmail_get_messagecount_text();
404 function rcmail_quota_display($attrib)
406 global $OUTPUT, $COMM_PATH;
409 $attrib['id'] = 'rcmquotadisplay';
411 $OUTPUT->add_gui_object('quotadisplay', $attrib['id']);
413 // allow the following attributes to be added to the <span> tag
414 $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id'));
416 $out = '<span' . $attrib_str . '>';
417 $out .= rcmail_quota_content($attrib['display']);
423 function rcmail_quota_content($display)
425 global $IMAP, $COMM_PATH;
427 if (!$IMAP->get_capability('QUOTA'))
428 $quota_text = rcube_label('unknown');
429 else if ($quota = $IMAP->get_quota())
431 $quota_text = sprintf("%s / %s (%.0f%%)",
432 show_bytes($quota["used"] * 1024),
433 show_bytes($quota["total"] * 1024),
436 // show quota as image (by Brett Patterson)
437 if ($display == 'image' && function_exists('imagegif'))
439 $attrib = array('width' => 100, 'height' => 14);
440 $quota_text = sprintf('<img src="./bin/quotaimg.php?u=%s&q=%d&w=%d&h=%d" width="%d" height="%d" alt="%s" title="%s / %s" />',
441 $quota['used'], $quota['total'],
442 $attrib['width'], $attrib['height'],
443 $attrib['width'], $attrib['height'],
445 show_bytes($quota["used"] * 1024),
446 show_bytes($quota["total"] * 1024));
450 $quota_text = rcube_label('unlimited');
456 function rcmail_get_messagecount_text($count=NULL, $page=NULL)
458 global $IMAP, $MESSAGE;
460 if (isset($MESSAGE['index']))
462 return rcube_label(array('name' => 'messagenrof',
463 'vars' => array('nr' => $MESSAGE['index']+1,
464 'count' => $count!==NULL ? $count : $IMAP->messagecount())));
468 $page = $IMAP->list_page;
470 $start_msg = ($page-1) * $IMAP->page_size + 1;
471 $max = $count!==NULL ? $count : $IMAP->messagecount();
474 $out = rcube_label('mailboxempty');
476 $out = rcube_label(array('name' => 'messagesfromto',
477 'vars' => array('from' => $start_msg,
478 'to' => min($max, $start_msg + $IMAP->page_size - 1),
485 function rcmail_print_body($part, $safe=FALSE, $plain=FALSE)
487 global $IMAP, $REMOTE_OBJECTS;
489 $body = is_array($part->replaces) ? strtr($part->body, $part->replaces) : $part->body;
491 // convert html to text/plain
492 if ($part->ctype_secondary=='html' && $plain)
494 $txt = new html2text($body, false, true);
495 $body = $txt->get_text();
496 $part->ctype_secondary = 'plain';
500 if ($part->ctype_secondary=='html')
502 // remove charset specification in HTML message
503 $body = preg_replace('/charset=[a-z0-9\-]+/i', '', $body);
505 if (!$safe) // remove remote images and scripts
507 $remote_patterns = array('/<img\s+(.*)src=(["\']?)([hftps]{3,5}:\/{2}[^"\'\s]+)(\2|\s|>)/Ui',
508 '/(src|background)=(["\']?)([hftps]{3,5}:\/{2}[^"\'\s]+)(\2|\s|>)/Ui',
509 '/(<base.*href=["\']?)([hftps]{3,5}:\/{2}[^"\'\s]+)([^<]*>)/i',
510 '/(<link.*href=["\']?)([hftps]{3,5}:\/{2}[^"\'\s]+)([^<]*>)/i',
511 '/url\s*\(["\']?([hftps]{3,5}:\/{2}[^"\'\s]+)["\']?\)/i',
512 '/url\s*\(["\']?([\.\/]+[^"\'\s]+)["\']?\)/i',
513 '/<script.+<\/script>/Umis');
515 $remote_replaces = array('<img \\1src=\\2./program/blocked.gif\\4',
523 // set flag if message containes remote obejcts that where blocked
524 foreach ($remote_patterns as $pattern)
526 if (preg_match($pattern, $body))
528 $REMOTE_OBJECTS = TRUE;
533 $body = preg_replace($remote_patterns, $remote_replaces, $body);
536 return Q($body, 'show', FALSE);
540 if ($part->ctype_secondary=='enriched')
542 return Q(enriched_to_html($body), 'show');
546 // make links and email-addresses clickable
547 $convert_patterns = $convert_replaces = $replace_strings = array();
549 $url_chars = 'a-z0-9_\-\+\*\$\/&%=@#:;';
550 $url_chars_within = '\?\.~,!';
552 $convert_patterns[] = "/([\w]+):\/\/([a-z0-9\-\.]+[a-z]{2,4}([$url_chars$url_chars_within]*[$url_chars])?)/ie";
553 $convert_replaces[] = "rcmail_str_replacement('<a href=\"\\1://\\2\" target=\"_blank\">\\1://\\2</a>', \$replace_strings)";
555 $convert_patterns[] = "/([^\/:]|\s)(www\.)([a-z0-9\-]{2,}[a-z]{2,4}([$url_chars$url_chars_within]*[$url_chars])?)/ie";
556 $convert_replaces[] = "rcmail_str_replacement('\\1<a href=\"http://\\2\\3\" target=\"_blank\">\\2\\3</a>', \$replace_strings)";
558 $convert_patterns[] = '/([a-z0-9][a-z0-9\-\.\+\_]*@[a-z0-9]([a-z0-9\-][.]?)*[a-z0-9]\\.[a-z]{2,5})/ie';
559 $convert_replaces[] = "rcmail_str_replacement('<a href=\"mailto:\\1\" onclick=\"return ".JS_OBJECT_NAME.".command(\'compose\',\'\\1\',this)\">\\1</a>', \$replace_strings)";
561 if ($part->ctype_parameters['format'] != 'flowed')
562 $body = wordwrap(trim($body), 80);
564 $body = preg_replace($convert_patterns, $convert_replaces, $body);
566 // split body into single lines
567 $a_lines = preg_split('/\r?\n/', $body);
570 // colorize quoted parts
571 for($n=0; $n<sizeof($a_lines); $n++)
573 $line = $a_lines[$n];
577 if (preg_match('/^(>+\s*)/', $line, $regs))
579 $q = strlen(preg_replace('/\s/', '', $regs[1]));
580 $line = substr($line, strlen($regs[1]));
582 if ($q > $quote_level)
583 $quotation = str_repeat('<blockquote>', $q - $quote_level);
584 else if ($q < $quote_level)
585 $quotation = str_repeat("</blockquote>", $quote_level - $q);
587 else if ($quote_level > 0)
588 $quotation = str_repeat("</blockquote>", $quote_level);
591 $a_lines[$n] = $quotation . Q($line, 'replace', FALSE);
594 // insert the links for urls and mailtos
595 $body = preg_replace("/##string_replacement\{([0-9]+)\}##/e", "\$replace_strings[\\1]", join("\n", $a_lines));
597 return "<div class=\"pre\">".$body."\n</div>";
603 // add a string to the replacement array and return a replacement string
604 function rcmail_str_replacement($str, &$rep)
607 $rep[$count] = stripslashes($str);
608 return "##string_replacement{".($count++)."}##";
612 function rcmail_parse_message(&$structure, $arg=array(), $recursive=FALSE)
615 static $sa_inline_objects = array();
617 // arguments are: (bool)$prefer_html, (string)$get_url
620 $a_attachments = array();
621 $a_return_parts = array();
624 $message_ctype_primary = strtolower($structure->ctype_primary);
625 $message_ctype_secondary = strtolower($structure->ctype_secondary);
627 // show message headers
628 if ($recursive && is_array($structure->headers) && isset($structure->headers['subject']))
631 $c->type = 'headers';
632 $c->headers = &$structure->headers;
633 $a_return_parts[] = $c;
636 // print body if message doesn't have multiple parts
637 if ($message_ctype_primary=='text')
639 $structure->type = 'content';
640 $a_return_parts[] = &$structure;
643 // message contains alternative parts
644 else if ($message_ctype_primary=='multipart' && $message_ctype_secondary=='alternative' && is_array($structure->parts))
646 // get html/plaintext parts
647 $plain_part = $html_part = $print_part = $related_part = NULL;
649 foreach ($structure->parts as $p => $sub_part)
651 $rel_parts = $attachmnts = null;
652 $sub_ctype_primary = strtolower($sub_part->ctype_primary);
653 $sub_ctype_secondary = strtolower($sub_part->ctype_secondary);
655 // check if sub part is
656 if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='plain')
658 else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='html')
660 else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='enriched')
662 else if ($sub_ctype_primary=='multipart' && ($sub_ctype_secondary=='related' || $sub_ctype_secondary=='mixed'))
666 // parse related part (alternative part could be in here)
667 if ($related_part!==NULL)
669 list($rel_parts, $attachmnts) = rcmail_parse_message($structure->parts[$related_part], $arg, TRUE);
670 $a_attachments = array_merge($a_attachments, $attachmnts);
673 // merge related parts if any
674 if ($rel_parts && $prefer_html && !$html_part)
675 $a_return_parts = array_merge($a_return_parts, $rel_parts);
677 // choose html/plain part to print
678 else if ($html_part!==NULL && $prefer_html)
679 $print_part = &$structure->parts[$html_part];
680 else if ($enriched_part!==NULL)
681 $print_part = &$structure->parts[$enriched_part];
682 else if ($plain_part!==NULL)
683 $print_part = &$structure->parts[$plain_part];
686 if (is_object($print_part))
688 $print_part->type = 'content';
689 $a_return_parts[] = $print_part;
691 // show plaintext warning
692 else if ($html_part!==NULL && empty($a_return_parts))
695 $c->type = 'content';
696 $c->body = rcube_label('htmlmessage');
697 $c->ctype_primary = 'text';
698 $c->ctype_secondary = 'plain';
700 $a_return_parts[] = $c;
703 // add html part as attachment
704 if ($html_part!==NULL && $structure->parts[$html_part]!==$print_part)
706 $html_part = &$structure->parts[$html_part];
707 $html_part->filename = rcube_label('htmlmessage');
708 $html_part->mimetype = 'text/html';
710 $a_attachments[] = $html_part;
714 // message contains multiple parts
715 else if (is_array($structure->parts) && !empty($structure->parts))
717 for ($i=0; $i<count($structure->parts); $i++)
719 $mail_part = &$structure->parts[$i];
720 $primary_type = strtolower($mail_part->ctype_primary);
721 $secondary_type = strtolower($mail_part->ctype_secondary);
723 // multipart/alternative
724 if ($primary_type=='multipart')
726 list($parts, $attachmnts) = rcmail_parse_message($mail_part, $arg, TRUE);
728 $a_return_parts = array_merge($a_return_parts, $parts);
729 $a_attachments = array_merge($a_attachments, $attachmnts);
732 // part text/[plain|html] OR message/delivery-status
733 else if (($primary_type=='text' && ($secondary_type=='plain' || $secondary_type=='html') && $mail_part->disposition!='attachment') ||
734 ($primary_type=='message' && $secondary_type=='delivery-status'))
736 $mail_part->type = 'content';
737 $a_return_parts[] = $mail_part;
741 else if ($primary_type=='message')
743 list($parts, $attachmnts) = rcmail_parse_message($mail_part, $arg, TRUE);
745 $a_return_parts = array_merge($a_return_parts, $parts);
746 $a_attachments = array_merge($a_attachments, $attachmnts);
749 // part is file/attachment
750 else if ($mail_part->disposition=='attachment' || $mail_part->disposition=='inline' || $mail_part->headers['content-id'] ||
751 (empty($mail_part->disposition) && $mail_part->filename))
753 // skip apple resource forks
754 if ($message_ctype_secondary=='appledouble' && $secondary_type=='applefile')
757 // part belongs to a related message
758 if ($message_ctype_secondary=='related' && $mail_part->headers['content-id'])
760 $mail_part->content_id = preg_replace(array('/^</', '/>$/'), '', $mail_part->headers['content-id']);
761 $sa_inline_objects[] = $mail_part;
763 // is regular attachment
766 if (!$mail_part->filename)
767 $mail_part->filename = 'file_'.$mail_part->mime_id;
768 $a_attachments[] = $mail_part;
773 // if this was a related part try to resolve references
774 if ($message_ctype_secondary=='related' && sizeof($sa_inline_objects))
776 $a_replaces = array();
778 foreach ($sa_inline_objects as $inline_object)
779 $a_replaces['cid:'.$inline_object->content_id] = htmlspecialchars(sprintf($get_url, $inline_object->mime_id));
781 // add replace array to each content part
782 // (will be applied later when part body is available)
783 for ($i=0; $i<count($a_return_parts); $i++)
785 if ($a_return_parts[$i]->type=='content')
786 $a_return_parts[$i]->replaces = $a_replaces;
791 // message is single part non-text
792 else if ($structure->filename)
793 $a_attachments[] = $structure;
795 return array($a_return_parts, $a_attachments);
801 // return table with message headers
802 function rcmail_message_headers($attrib, $headers=NULL)
804 global $IMAP, $OUTPUT, $MESSAGE;
807 // keep header table attrib
808 if (is_array($attrib) && !$sa_attrib)
809 $sa_attrib = $attrib;
810 else if (!is_array($attrib) && is_array($sa_attrib))
811 $attrib = $sa_attrib;
814 if (!isset($MESSAGE))
817 // get associative array of headers object
819 $headers = is_object($MESSAGE['headers']) ? get_object_vars($MESSAGE['headers']) : $MESSAGE['headers'];
823 // allow the following attributes to be added to the <table> tag
824 $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border', 'summary'));
825 $out = '<table' . $attrib_str . ">\n";
827 // show these headers
828 $standard_headers = array('subject', 'from', 'organization', 'to', 'cc', 'bcc', 'reply-to', 'date');
830 foreach ($standard_headers as $hkey)
832 if (!$headers[$hkey])
835 if ($hkey=='date' && !empty($headers[$hkey]))
836 $header_value = format_date(strtotime($headers[$hkey]));
837 else if (in_array($hkey, array('from', 'to', 'cc', 'bcc', 'reply-to')))
838 $header_value = Q(rcmail_address_string($headers[$hkey], NULL, $attrib['addicon']), 'show');
840 $header_value = Q(rcube_imap::decode_mime_string($headers[$hkey], $headers['charset']));
843 $out .= '<td class="header-title">'.Q(rcube_label($hkey)).": </td>\n";
844 $out .= '<td class="'.$hkey.'" width="90%">'.$header_value."</td>\n</tr>";
848 $out .= "\n</table>\n\n";
850 return $header_count ? $out : '';
855 function rcmail_message_body($attrib)
857 global $CONFIG, $OUTPUT, $MESSAGE, $IMAP, $GET_URL, $REMOTE_OBJECTS;
859 if (!is_array($MESSAGE['parts']) && !$MESSAGE['body'])
863 $attrib['id'] = 'rcmailMsgBody';
865 $safe_mode = (bool)$_GET['_safe'];
866 $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id'));
867 $out = '<div '. $attrib_str . ">\n";
869 $header_attrib = array();
870 foreach ($attrib as $attr => $value)
871 if (preg_match('/^headertable([a-z]+)$/i', $attr, $regs))
872 $header_attrib[$regs[1]] = $value;
875 // this is an ecrypted message
876 // -> create a plaintext body with the according message
877 if (!sizeof($MESSAGE['parts']) && $MESSAGE['headers']->ctype=='multipart/encrypted')
880 $p->type = 'content';
881 $p->ctype_primary = 'text';
882 $p->ctype_secondary = 'plain';
883 $p->body = rcube_label('encryptedmessage');
884 $MESSAGE['parts'][0] = $p;
887 if ($MESSAGE['parts'])
889 foreach ($MESSAGE['parts'] as $i => $part)
891 if ($part->type=='headers')
892 $out .= rcmail_message_headers(sizeof($header_attrib) ? $header_attrib : NULL, $part->headers);
893 else if ($part->type=='content')
895 if (empty($part->ctype_parameters) || empty($part->ctype_parameters['charset']))
896 $part->ctype_parameters['charset'] = $MESSAGE['headers']->charset;
898 // fetch part if not available
899 if (!isset($part->body))
900 $part->body = $IMAP->get_message_part($MESSAGE['UID'], $part->mime_id, $part);
902 $body = rcmail_print_body($part, $safe_mode, !$CONFIG['prefer_html']);
903 $out .= '<div class="message-part">';
905 if ($part->ctype_secondary != 'plain')
906 $out .= rcmail_sanitize_html($body, $attrib['id']);
915 $out .= $MESSAGE['body'];
918 $ctype_primary = strtolower($MESSAGE['structure']->ctype_primary);
919 $ctype_secondary = strtolower($MESSAGE['structure']->ctype_secondary);
921 // list images after mail body
922 if (get_boolean($attrib['showimages']) && $ctype_primary=='multipart' &&
923 !empty($MESSAGE['attachments']) && !strstr($message_body, '<html') && strlen($GET_URL))
925 foreach ($MESSAGE['attachments'] as $attach_prop)
927 if (strpos($attach_prop->mimetype, 'image/')===0)
928 $out .= sprintf("\n<hr />\n<p align=\"center\"><img src=\"%s&_part=%s\" alt=\"%s\" title=\"%s\" /></p>\n",
929 htmlspecialchars($GET_URL), $attach_prop->mime_id,
930 $attach_prop->filename,
931 $attach_prop->filename);
935 // tell client that there are blocked remote objects
936 if ($REMOTE_OBJECTS && !$safe_mode)
937 $OUTPUT->set_env('blockedobjects', true);
945 // modify a HTML message that it can be displayed inside a HTML page
946 function rcmail_sanitize_html($body, $container_id)
948 // remove any null-byte characters before parsing
949 $body = preg_replace('/\x00/', '', $body);
953 $body_lc = strtolower($body);
955 // check for <base href>
956 if (preg_match(($base_reg = '/(<base.*href=["\']?)([hftps]{3,5}:\/{2}[^"\'\s]+)([^<]*>)/i'), $body, $base_regs))
957 $base_url = $base_regs[2];
960 while (($pos = strpos($body_lc, '<style', $last_style_pos)) && ($pos2 = strpos($body_lc, '</style>', $pos)))
962 $pos = strpos($body_lc, '>', $pos)+1;
964 // replace all css definitions with #container [def]
965 $styles = rcmail_mod_css_styles(substr($body, $pos, $pos2-$pos), $container_id, $base_url);
967 $body = substr($body, 0, $pos) . $styles . substr($body, $pos2);
968 $body_lc = strtolower($body);
969 $last_style_pos = $pos2;
973 // remove SCRIPT tags
974 foreach (array('script', 'applet', 'object', 'embed', 'iframe') as $tag)
976 while (($pos = strpos($body_lc, '<'.$tag)) && ($pos2 = strpos($body_lc, '</'.$tag.'>', $pos)))
978 $pos2 += strlen('</'.$tag.'>');
979 $body = substr($body, 0, $pos) . substr($body, $pos2, strlen($body)-$pos2);
980 $body_lc = strtolower($body);
984 // replace event handlers on any object
985 while ($body != $prev_body)
988 $body = preg_replace('/(<[^!][^>]*\s)(on[^=>]+)=([^>]+>)/im', '$1__removed=$3', $body);
989 $body = preg_replace('/(<[^!][^>]*\shref=["\']?)(javascript:)([^>]*?>)/im', '$1null:$3', $body);
992 // resolve <base href>
995 $body = preg_replace('/(src|background|href)=(["\']?)([\.\/]+[^"\'\s]+)(\2|\s|>)/Uie', "'\\1=\"'.make_absolute_url('\\3', '$base_url').'\"'", $body);
996 $body = preg_replace('/(url\s*\()(["\']?)([\.\/]+[^"\'\)\s]+)(\2)\)/Uie', "'\\1\''.make_absolute_url('\\3', '$base_url').'\')'", $body);
997 $body = preg_replace($base_reg, '', $body);
1000 // modify HTML links to open a new window if clicked
1001 $body = preg_replace('/<(a|link)\s+([^>]+)>/Uie', "rcmail_alter_html_link('\\1','\\2', '$container_id');", $body);
1003 // add comments arround html and other tags
1004 $out = preg_replace(array(
1006 '/(<\/?html[^>]*>)/i',
1007 '/(<\/?head[^>]*>)/i',
1008 '/(<title[^>]*>.*<\/title>)/Ui',
1009 '/(<\/?meta[^>]*>)/i'),
1013 $out = preg_replace(
1019 '<div class="rcmBody"\\1>',
1024 // quote <? of php and xml files that are specified as text/html
1025 $out = preg_replace(array('/<\?/', '/\?>/'), array('<?', '?>'), $out);
1031 // parse link attributes and set correct target
1032 function rcmail_alter_html_link($tag, $attrs, $container_id)
1034 $in = preg_replace('/=([^("|\'|\s)]+)(\s|$)/', '="\1"', $in);
1035 $attrib = parse_attrib_string($attrs);
1037 if ($tag == 'link' && preg_match('/^https?:\/\//i', $attrib['href']))
1038 $attrib['href'] = "./bin/modcss.php?u=" . urlencode($attrib['href']) . "&c=" . urlencode($container_id);
1040 else if (stristr((string)$attrib['href'], 'mailto:'))
1041 $attrib['onclick'] = sprintf(
1042 "return %s.command('compose','%s',this)",
1044 JQ(substr($attrib['href'], 7)));
1046 else if (!empty($attrib['href']) && $attrib['href']{0}!='#')
1047 $attrib['target'] = '_blank';
1049 return "<$tag" . create_attrib_string($attrib, array('href','name','target','onclick','id','class','style','title','rel','type','media')) . ' />';
1053 function rcmail_has_html_part($message_parts)
1055 if (!is_array($message_parts))
1058 // check all message parts
1059 foreach ($message_parts as $pid => $part)
1061 $mimetype = strtolower($part->ctype_primary.'/'.$part->ctype_secondary);
1062 if ($mimetype=='text/html')
1071 // return first HTML part of a message
1072 function rcmail_first_html_part($message_struct)
1076 if (!is_array($message_struct['parts']))
1081 // check all message parts
1082 foreach ($message_struct['parts'] as $pid => $part)
1084 $mimetype = strtolower($part->ctype_primary.'/'.$part->ctype_secondary);
1085 if ($mimetype=='text/html')
1087 $html_part = $IMAP->get_message_part($message_struct['UID'], $pid, $part);
1093 // remove special chars encoding
1094 //$trans = array_flip(get_html_translation_table(HTML_ENTITIES));
1095 //$html_part = strtr($html_part, $trans);
1104 // return first text part of a message
1105 function rcmail_first_text_part($message_struct)
1109 if (empty($message_struct['parts']))
1110 return $message_struct['UID'] ? $IMAP->get_body($message_struct['UID']) : false;
1112 // check all message parts
1113 foreach ($message_struct['parts'] as $pid => $part)
1115 $mimetype = strtolower($part->ctype_primary.'/'.$part->ctype_secondary);
1117 if ($mimetype=='text/plain')
1118 return $IMAP->get_message_part($message_struct['UID'], $pid, $part);
1120 else if ($mimetype=='text/html')
1122 $html_part = $IMAP->get_message_part($message_struct['UID'], $pid, $part);
1124 // remove special chars encoding
1125 $trans = array_flip(get_html_translation_table(HTML_ENTITIES));
1126 $html_part = strtr($html_part, $trans);
1128 // create instance of html2text class
1129 $txt = new html2text($html_part);
1130 return $txt->get_text();
1138 // decode address string and re-format it as HTML links
1139 function rcmail_address_string($input, $max=NULL, $addicon=NULL)
1141 global $IMAP, $PRINT_MODE, $CONFIG, $OUTPUT, $EMAIL_ADDRESS_PATTERN;
1143 $a_parts = $IMAP->decode_address_list($input);
1145 if (!sizeof($a_parts))
1148 $c = count($a_parts);
1152 foreach ($a_parts as $part)
1156 $out .= sprintf('%s <%s>', Q($part['name']), $part['mailto']);
1157 else if (preg_match($EMAIL_ADDRESS_PATTERN, $part['mailto']))
1159 $out .= sprintf('<a href="mailto:%s" onclick="return %s.command(\'compose\',\'%s\',this)" class="rcmContactAddress" title="%s">%s</a>',
1162 JQ($part['mailto']),
1167 $out .= sprintf(' <a href="#add" onclick="return %s.command(\'add-contact\',\'%s\',this)" title="%s"><img src="%s%s" alt="add" border="0" /></a>',
1169 urlencode($part['string']),
1170 rcube_label('addtoaddressbook'),
1171 $CONFIG['skin_path'],
1177 $out .= Q($part['name']);
1178 if ($part['mailto'])
1179 $out .= (strlen($out) ? ' ' : '') . sprintf('<%s>', Q($part['mailto']));
1183 $out .= ','.($max ? ' ' : ' ');
1185 if ($max && $j==$max && $c>$j)
1196 function rcmail_message_part_controls()
1198 global $CONFIG, $IMAP, $MESSAGE;
1200 if (!is_array($MESSAGE) || !is_array($MESSAGE['parts']) || !($_GET['_uid'] && $_GET['_part']) || !$MESSAGE['parts'][$_GET['_part']])
1203 $part = &$MESSAGE['parts'][$_GET['_part']];
1205 $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'cellspacing', 'cellpadding', 'border', 'summary'));
1206 $out = '<table '. $attrib_str . ">\n";
1210 $out .= sprintf('<tr><td class="title">%s</td><td>%s</td><td>[<a href="./?%s">%s</a>]</tr>'."\n",
1211 Q(rcube_label('filename')),
1213 str_replace('_frame=', '_download=', $_SERVER['QUERY_STRING']),
1214 Q(rcube_label('download')));
1218 $out .= sprintf('<tr><td class="title">%s</td><td>%s</td></tr>'."\n",
1219 Q(rcube_label('filesize')),
1220 show_bytes($part->size));
1222 $out .= "\n</table>";
1229 function rcmail_message_part_frame($attrib)
1233 $part = $MESSAGE['parts'][$_GET['_part']];
1234 $ctype_primary = strtolower($part->ctype_primary);
1236 $attrib['src'] = Q('./?'.str_replace('_frame=', ($ctype_primary=='text' ? '_show=' : '_preload='), $_SERVER['QUERY_STRING']));
1238 $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'src', 'width', 'height'));
1239 $out = '<iframe '. $attrib_str . "></iframe>";
1245 // clear message composing settings
1246 function rcmail_compose_cleanup()
1248 if (!isset($_SESSION['compose']))
1251 // remove attachment files from temp dir
1252 if (is_array($_SESSION['compose']['attachments']))
1253 foreach ($_SESSION['compose']['attachments'] as $attachment)
1254 @unlink($attachment['path']);
1256 unset($_SESSION['compose']);
1260 // register UI objects
1261 $OUTPUT->add_handlers(array(
1262 'mailboxlist' => 'rcmail_mailbox_list',
1263 'messages' => 'rcmail_message_list',
1264 'messagecountdisplay' => 'rcmail_messagecount_display',
1265 'quotadisplay' => 'rcmail_quota_display',
1266 'messageheaders' => 'rcmail_message_headers',
1267 'messagebody' => 'rcmail_message_body',
1268 'messagecontentframe' => 'rcmail_messagecontent_frame',
1269 'messagepartframe' => 'rcmail_message_part_frame',
1270 'messagepartcontrols' => 'rcmail_message_part_controls',
1271 'searchform' => 'rcmail_search_form'