4 +-----------------------------------------------------------------------+
5 | program/steps/mail/func.inc |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2005-2008, 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 1494 2008-06-09 12:22:54Z alec $
22 require_once('lib/enriched.inc');
23 require_once('include/rcube_smtp.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))
33 $IMAP->set_mailbox(($_SESSION['mbox'] = $mbox));
35 if (!empty($_GET['_page']))
36 $IMAP->set_page(($_SESSION['page'] = intval($_GET['_page'])));
38 // set mailbox to INBOX if not set
39 if (empty($_SESSION['mbox']))
40 $_SESSION['mbox'] = $IMAP->get_mailbox_name();
42 // set default sort col/order to session
43 if (!isset($_SESSION['sort_col']))
44 $_SESSION['sort_col'] = $CONFIG['message_sort_col'];
45 if (!isset($_SESSION['sort_order']))
46 $_SESSION['sort_order'] = $CONFIG['message_sort_order'];
48 // set message set for search result
49 if (!empty($_REQUEST['_search']) && isset($_SESSION['search'][$_REQUEST['_search']]))
51 $IMAP->set_search_set($_SESSION['search'][$_REQUEST['_search']]);
52 $OUTPUT->set_env('search_request', $_REQUEST['_search']);
53 $OUTPUT->set_env('search_text', $_SESSION['last_text_search']);
57 // set current mailbox in client environment
58 $OUTPUT->set_env('mailbox', $IMAP->get_mailbox_name());
59 $OUTPUT->set_env('quota', $IMAP->get_capability('quota'));
61 if ($CONFIG['trash_mbox'])
62 $OUTPUT->set_env('trash_mailbox', $CONFIG['trash_mbox']);
63 if ($CONFIG['drafts_mbox'])
64 $OUTPUT->set_env('drafts_mailbox', $CONFIG['drafts_mbox']);
65 if ($CONFIG['junk_mbox'])
66 $OUTPUT->set_env('junk_mailbox', $CONFIG['junk_mbox']);
68 if (!$OUTPUT->ajax_call)
69 rcube_add_label('checkingmail', 'deletemessage', 'movemessagetotrash', 'movingmessage');
72 if (empty($RCMAIL->action) || $RCMAIL->action == 'list')
73 $OUTPUT->set_pagetitle(rcmail_localize_foldername($IMAP->get_mailbox_name()));
78 * return the message list as HTML table
80 function rcmail_message_list($attrib)
82 global $IMAP, $CONFIG, $COMM_PATH, $OUTPUT;
84 $skin_path = $CONFIG['skin_path'];
85 $image_tag = '<img src="%s%s" alt="%s" border="0" />';
87 // check to see if we have some settings for sorting
88 $sort_col = $_SESSION['sort_col'];
89 $sort_order = $_SESSION['sort_order'];
91 // add some labels to client
92 rcube_add_label('from', 'to');
94 // get message headers
95 $a_headers = $IMAP->list_headers('', '', $sort_col, $sort_order);
97 // add id to message list table if not specified
98 if (!strlen($attrib['id']))
99 $attrib['id'] = 'rcubemessagelist';
101 // allow the following attributes to be added to the <table> tag
102 $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border', 'summary'));
104 $out = '<table' . $attrib_str . ">\n";
107 // define list of cols to be displayed
108 $a_show_cols = is_array($CONFIG['list_cols']) ? $CONFIG['list_cols'] : array('subject');
109 $a_sort_cols = array('subject', 'date', 'from', 'to', 'size');
111 $mbox = $IMAP->get_mailbox_name();
113 // show 'to' instead of from in sent messages
114 if (($mbox==$CONFIG['sent_mbox'] || $mbox==$CONFIG['drafts_mbox']) && ($f = array_search('from', $a_show_cols))
115 && !array_search('to', $a_show_cols))
116 $a_show_cols[$f] = 'to';
118 // add col definition
119 $out .= '<colgroup>';
120 $out .= '<col class="icon" />';
122 foreach ($a_show_cols as $col)
123 $out .= sprintf('<col class="%s" />', $col);
125 $out .= '<col class="icon" />';
126 $out .= "</colgroup>\n";
129 $out .= "<thead><tr>\n<td class=\"icon\"> </td>\n";
132 foreach ($a_show_cols as $col)
135 $col_name = Q(rcube_label($col));
139 if (in_array($col, $a_sort_cols))
141 // have buttons configured
142 if (!empty($attrib['sortdescbutton']) || !empty($attrib['sortascbutton']))
144 $sort = ' ';
147 if (!empty($attrib['sortascbutton']))
149 $sort .= $OUTPUT->button(array(
151 'prop' => $col.'_ASC',
152 'image' => $attrib['sortascbutton'],
153 'align' => 'absmiddle',
154 'title' => 'sortasc'));
158 if (!empty($attrib['sortdescbutton']))
160 $sort .= $OUTPUT->button(array(
162 'prop' => $col.'_DESC',
163 'image' => $attrib['sortdescbutton'],
164 'align' => 'absmiddle',
165 'title' => 'sortdesc'));
168 // just add a link tag to the header
172 '<a href="./#sort" onclick="return %s.command(\'sort\',\'%s\',this)" title="%s">%s</a>',
175 rcube_label('sortby'),
180 $sort_class = $col==$sort_col ? " sorted$sort_order" : '';
182 // put it all together
183 $out .= '<td class="'.$col.$sort_class.'" id="rcmHead'.$col.'">' . "$col_name$sort</td>\n";
186 $out .= '<td class="icon">'.($attrib['attachmenticon'] ? sprintf($image_tag, $skin_path, $attrib['attachmenticon'], '') : '')."</td>\n";
187 $out .= "</tr></thead>\n<tbody>\n";
189 // no messages in this mailbox
190 if (!sizeof($a_headers))
191 $OUTPUT->show_message('nomessagesfound', 'notice');
194 $a_js_message_arr = array();
196 // create row for each message
197 foreach ($a_headers as $i => $header) //while (list($i, $header) = each($a_headers))
199 $message_icon = $attach_icon = '';
200 $js_row_arr = array();
201 $zebra_class = $i%2 ? 'even' : 'odd';
203 // set messag attributes to javascript array
204 if ($header->deleted)
205 $js_row_arr['deleted'] = true;
207 $js_row_arr['unread'] = true;
208 if ($header->answered)
209 $js_row_arr['replied'] = true;
211 if ($attrib['deletedicon'] && $header->deleted)
212 $message_icon = $attrib['deletedicon'];
213 else if ($attrib['unreadicon'] && !$header->seen)
214 $message_icon = $attrib['unreadicon'];
215 else if ($attrib['repliedicon'] && $header->answered)
216 $message_icon = $attrib['repliedicon'];
217 else if ($attrib['messageicon'])
218 $message_icon = $attrib['messageicon'];
220 // set attachment icon
221 if ($attrib['attachmenticon'] && preg_match("/multipart\/[mr]/i", $header->ctype))
222 $attach_icon = $attrib['attachmenticon'];
224 $out .= sprintf('<tr id="rcmrow%d" class="message%s%s %s">'."\n",
226 $header->seen ? '' : ' unread',
227 $header->deleted ? ' deleted' : '',
230 $out .= sprintf("<td class=\"icon\">%s</td>\n", $message_icon ? sprintf($image_tag, $skin_path, $message_icon, '') : '');
232 if (!empty($header->charset))
233 $IMAP->set_charset($header->charset);
236 foreach ($a_show_cols as $col)
238 if ($col=='from' || $col=='to')
239 $cont = Q(rcmail_address_string($header->$col, 3, $attrib['addicon']), 'show');
240 else if ($col=='subject')
242 $action = $mbox==$CONFIG['drafts_mbox'] ? 'compose' : 'show';
243 $uid_param = $mbox==$CONFIG['drafts_mbox'] ? '_draft_uid' : '_uid';
244 $cont = Q($IMAP->decode_header($header->$col));
245 if (empty($cont)) $cont = Q(rcube_label('nosubject'));
246 $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);
248 else if ($col=='size')
249 $cont = show_bytes($header->$col);
250 else if ($col=='date')
251 $cont = format_date($header->date);
253 $cont = Q($header->$col);
255 $out .= '<td class="'.$col.'">' . $cont . "</td>\n";
258 $out .= sprintf("<td class=\"icon\">%s</td>\n", $attach_icon ? sprintf($image_tag, $skin_path, $attach_icon, '') : '');
261 if (sizeof($js_row_arr))
262 $a_js_message_arr[$header->uid] = $js_row_arr;
265 // complete message table
266 $out .= "</tbody></table>\n";
269 $message_count = $IMAP->messagecount();
272 $OUTPUT->add_gui_object('mailcontframe', 'mailcontframe');
273 $OUTPUT->add_gui_object('messagelist', $attrib['id']);
274 $OUTPUT->set_env('messagecount', $message_count);
275 $OUTPUT->set_env('current_page', $IMAP->list_page);
276 $OUTPUT->set_env('pagecount', ceil($message_count/$IMAP->page_size));
277 $OUTPUT->set_env('sort_col', $sort_col);
278 $OUTPUT->set_env('sort_order', $sort_order);
280 if ($attrib['messageicon'])
281 $OUTPUT->set_env('messageicon', $skin_path . $attrib['messageicon']);
282 if ($attrib['deletedicon'])
283 $OUTPUT->set_env('deletedicon', $skin_path . $attrib['deletedicon']);
284 if ($attrib['unreadicon'])
285 $OUTPUT->set_env('unreadicon', $skin_path . $attrib['unreadicon']);
286 if ($attrib['repliedicon'])
287 $OUTPUT->set_env('repliedicon', $skin_path . $attrib['repliedicon']);
288 if ($attrib['attachmenticon'])
289 $OUTPUT->set_env('attachmenticon', $skin_path . $attrib['attachmenticon']);
291 $OUTPUT->set_env('messages', $a_js_message_arr);
292 $OUTPUT->set_env('coltypes', $a_show_cols);
294 $OUTPUT->include_script('list.js');
301 * return javascript commands to add rows to the message list
303 function rcmail_js_message_list($a_headers, $insert_top=FALSE)
305 global $CONFIG, $IMAP, $OUTPUT;
307 $a_show_cols = is_array($CONFIG['list_cols']) ? $CONFIG['list_cols'] : array('subject');
308 $mbox = $IMAP->get_mailbox_name();
310 // show 'to' instead of from in sent messages
311 if (($mbox == $CONFIG['sent_mbox'] || $mbox == $CONFIG['drafts_mbox'])
312 && (($f = array_search('from', $a_show_cols)) !== false) && array_search('to', $a_show_cols) === false)
313 $a_show_cols[$f] = 'to';
315 $OUTPUT->command('set_message_coltypes', $a_show_cols);
317 // loop through message headers
318 foreach ($a_headers as $n => $header)
320 $a_msg_cols = array();
321 $a_msg_flags = array();
326 if (!empty($header->charset))
327 $IMAP->set_charset($header->charset);
329 // format each col; similar as in rcmail_message_list()
330 foreach ($a_show_cols as $col)
332 if ($col=='from' || $col=='to')
333 $cont = Q(rcmail_address_string($header->$col, 3), 'show');
334 else if ($col=='subject')
336 $action = $mbox==$CONFIG['drafts_mbox'] ? 'compose' : 'show';
337 $uid_param = $mbox==$CONFIG['drafts_mbox'] ? '_draft_uid' : '_uid';
338 $cont = Q($IMAP->decode_header($header->$col));
339 if (!$cont) $cont = Q(rcube_label('nosubject'));
340 $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);
342 else if ($col=='size')
343 $cont = show_bytes($header->$col);
344 else if ($col=='date')
345 $cont = format_date($header->date);
347 $cont = Q($header->$col);
349 $a_msg_cols[$col] = $cont;
352 $a_msg_flags['deleted'] = $header->deleted ? 1 : 0;
353 $a_msg_flags['unread'] = $header->seen ? 0 : 1;
354 $a_msg_flags['replied'] = $header->answered ? 1 : 0;
355 $OUTPUT->command('add_message_row',
359 preg_match("/multipart\/m/i", $header->ctype),
366 * return an HTML iframe for loading mail content
368 function rcmail_messagecontent_frame($attrib)
372 if (empty($attrib['id']))
373 $attrib['id'] = 'rcmailcontentwindow';
375 // allow the following attributes to be added to the <iframe> tag
376 $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'src', 'width', 'height', 'frameborder'));
377 $framename = $attrib['id'];
379 $out = sprintf('<iframe name="%s"%s></iframe>'."\n",
383 $OUTPUT->set_env('contentframe', $framename);
384 $OUTPUT->set_env('blankpage', $attrib['src'] ? $OUTPUT->abs_url($attrib['src']) : 'program/blank.gif');
393 function rcmail_messagecount_display($attrib)
395 global $IMAP, $OUTPUT;
398 $attrib['id'] = 'rcmcountdisplay';
400 $OUTPUT->add_gui_object('countdisplay', $attrib['id']);
402 // allow the following attributes to be added to the <span> tag
403 $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id'));
406 $out = '<span' . $attrib_str . '>';
407 $out .= rcmail_get_messagecount_text();
416 function rcmail_quota_display($attrib)
418 global $OUTPUT, $COMM_PATH;
421 $attrib['id'] = 'rcmquotadisplay';
423 if(isset($attrib['display']))
424 $_SESSION['quota_display'] = $attrib['display'];
426 $OUTPUT->add_gui_object('quotadisplay', $attrib['id']);
428 // allow the following attributes to be added to the <span> tag
429 $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'display'));
431 $out = '<span' . $attrib_str . '>';
432 $out .= rcmail_quota_content();
441 function rcmail_quota_content($quota=NULL)
443 global $IMAP, $COMM_PATH;
445 $display = isset($_SESSION['quota_display']) ? $_SESSION['quota_display'] : '';
447 if (is_array($quota) && !empty($quota['used']) && !empty($quota['total']))
449 if (!isset($quota['percent']))
450 $quota['percent'] = $quota['used'] / $quota['total'];
452 elseif (!$IMAP->get_capability('QUOTA'))
453 return rcube_label('unknown');
455 $quota = $IMAP->get_quota();
459 $quota_text = sprintf('%s / %s (%.0f%%)',
460 show_bytes($quota['used'] * 1024),
461 show_bytes($quota['total'] * 1024),
464 // show quota as image (by Brett Patterson)
465 if ($display == 'image' && function_exists('imagegif'))
467 $attrib = array('width' => 100, 'height' => 14);
468 $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" />',
469 $quota['used'], $quota['total'],
470 $attrib['width'], $attrib['height'],
471 $attrib['width'], $attrib['height'],
473 show_bytes($quota["used"] * 1024),
474 show_bytes($quota["total"] * 1024));
478 $quota_text = rcube_label('unlimited');
487 function rcmail_get_messagecount_text($count=NULL, $page=NULL)
489 global $IMAP, $MESSAGE;
491 if (isset($MESSAGE->index))
493 return rcube_label(array('name' => 'messagenrof',
494 'vars' => array('nr' => $MESSAGE->index+1,
495 'count' => $count!==NULL ? $count : $IMAP->messagecount())));
499 $page = $IMAP->list_page;
501 $start_msg = ($page-1) * $IMAP->page_size + 1;
502 $max = $count!==NULL ? $count : $IMAP->messagecount();
505 $out = rcube_label('mailboxempty');
507 $out = rcube_label(array('name' => 'messagesfromto',
508 'vars' => array('from' => $start_msg,
509 'to' => min($max, $start_msg + $IMAP->page_size - 1),
517 * Convert the given message part to proper HTML
518 * which can be displayed the message view
520 * @param object rcube_message_part Message part
521 * @param bool True if external objects (ie. images ) are allowed
522 * @param bool True if part should be converted to plaintext
523 * @return string Formatted HTML string
525 function rcmail_print_body($part, $safe=false, $plain=false)
527 global $REMOTE_OBJECTS;
529 // convert html to text/plain
530 if ($part->ctype_secondary == 'html' && $plain) {
531 $txt = new html2text($part->body, false, true);
532 $body = $txt->get_text();
533 $part->ctype_secondary = 'plain';
536 else if ($part->ctype_secondary == 'html') {
537 // charset was converted to UTF-8 in rcube_imap::get_message_part() -> change charset specification in HTML accordingly
539 if(preg_match('/(\s+content=[\'"]\w+\/\w+;\s+charset)=([a-z0-9-]+)/i', $html))
540 $html = preg_replace('/(\s+content=[\'"]\w+\/\w+;\s+charset)=([a-z0-9-]+)/i', '\\1='.RCMAIL_CHARSET, $html);
542 $html = substr_replace($html, '<meta http-equiv="Content-Type" content="text/html; charset='.RCMAIL_CHARSET.'" />', intval(stripos($html, '</head>')), 0);
544 // clean HTML with washhtml by Frederic Motte
545 $body = washtml::wash($html, array(
546 'show_washed' => false,
547 'allow_remote' => $safe,
548 'blocked_src' => "./program/blocked.gif",
549 'charset' => RCMAIL_CHARSET,
550 'cid_map' => $part->replaces,
553 $REMOTE_OBJECTS = !$full_inline;
558 else if ($part->ctype_secondary=='enriched') {
559 $part->ctype_secondary = 'html';
560 return Q(enriched_to_html($body), 'show');
566 /**** assert plaintext ****/
568 // make links and email-addresses clickable
569 $convert_patterns = $convert_replaces = $replace_strings = array();
571 $url_chars = 'a-z0-9_\-\+\*\$\/&%=@#:;';
572 $url_chars_within = '\?\.~,!';
574 $convert_patterns[] = "/([\w]+):\/\/([a-z0-9\-\.]+[a-z]{2,4}([$url_chars$url_chars_within]*[$url_chars])?)/ie";
575 $convert_replaces[] = "rcmail_str_replacement('<a href=\"\\1://\\2\" target=\"_blank\">\\1://\\2</a>', \$replace_strings)";
577 $convert_patterns[] = "/([^\/:]|\s)(www\.)([a-z0-9\-]{2,}[a-z]{2,4}([$url_chars$url_chars_within]*[$url_chars])?)/ie";
578 $convert_replaces[] = "rcmail_str_replacement('\\1<a href=\"http://\\2\\3\" target=\"_blank\">\\2\\3</a>', \$replace_strings)";
580 $convert_patterns[] = '/([a-z0-9][a-z0-9\-\.\+\_]*@[a-z0-9]([a-z0-9\-][.]?)*[a-z0-9]\\.[a-z]{2,5})/ie';
581 $convert_replaces[] = "rcmail_str_replacement('<a href=\"mailto:\\1\" onclick=\"return ".JS_OBJECT_NAME.".command(\'compose\',\'\\1\',this)\">\\1</a>', \$replace_strings)";
583 // if ($part->ctype_parameters['format'] != 'flowed')
584 // $body = wordwrap(trim($body), 80);
586 // search for patterns like links and e-mail addresses
587 $body = preg_replace($convert_patterns, $convert_replaces, $body);
589 // split body into single lines
590 $a_lines = preg_split('/\r?\n/', $body);
593 // colorize quoted parts
594 for ($n=0; $n < sizeof($a_lines); $n++) {
595 $line = $a_lines[$n];
599 if (preg_match('/^(>+\s*)+/', $line, $regs)) {
600 $q = strlen(preg_replace('/\s/', '', $regs[0]));
601 $line = substr($line, strlen($regs[0]));
603 if ($q > $quote_level)
604 $quotation = str_repeat('<blockquote>', $q - $quote_level);
605 else if ($q < $quote_level)
606 $quotation = str_repeat("</blockquote>", $quote_level - $q);
608 else if ($quote_level > 0)
609 $quotation = str_repeat("</blockquote>", $quote_level);
612 $a_lines[$n] = $quotation . Q($line, 'replace', false); // htmlquote plaintext
615 // insert the links for urls and mailtos
616 $body = preg_replace("/##string_replacement\{([0-9]+)\}##/e", "\$replace_strings[\\1]", join("\n", $a_lines));
618 return "<div class=\"pre\">".$body."\n</div>";
624 * add a string to the replacement array and return a replacement string
626 function rcmail_str_replacement($str, &$rep)
629 $rep[$count] = stripslashes($str);
630 return "##string_replacement{".($count++)."}##";
636 * return table with message headers
638 function rcmail_message_headers($attrib, $headers=NULL)
640 global $IMAP, $OUTPUT, $MESSAGE;
643 // keep header table attrib
644 if (is_array($attrib) && !$sa_attrib)
645 $sa_attrib = $attrib;
646 else if (!is_array($attrib) && is_array($sa_attrib))
647 $attrib = $sa_attrib;
650 if (!isset($MESSAGE))
653 // get associative array of headers object
655 $headers = is_object($MESSAGE->headers) ? get_object_vars($MESSAGE->headers) : $MESSAGE->headers;
657 // add empty subject if none exsists
658 if (empty($headers['subject']))
659 $headers['subject'] = rcube_label('nosubject');
663 // allow the following attributes to be added to the <table> tag
664 $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border', 'summary'));
665 $out = '<table' . $attrib_str . ">\n";
667 // show these headers
668 $standard_headers = array('subject', 'from', 'organization', 'to', 'cc', 'bcc', 'reply-to', 'date');
670 foreach ($standard_headers as $hkey)
672 if (!$headers[$hkey])
675 if ($hkey=='date' && !empty($headers[$hkey]))
676 $header_value = format_date($headers[$hkey]);
677 else if (in_array($hkey, array('from', 'to', 'cc', 'bcc', 'reply-to')))
678 $header_value = Q(rcmail_address_string($headers[$hkey], NULL, $attrib['addicon']), 'show');
680 $header_value = Q($IMAP->decode_header($headers[$hkey]));
683 $out .= '<td class="header-title">'.Q(rcube_label($hkey)).": </td>\n";
684 $out .= '<td class="'.$hkey.'" width="90%">'.$header_value."</td>\n</tr>";
688 $out .= "\n</table>\n\n";
690 return $header_count ? $out : '';
695 * Handler for the 'messagebody' GUI object
697 * @param array Named parameters
698 * @return string HTML content showing the message body
700 function rcmail_message_body($attrib)
702 global $CONFIG, $OUTPUT, $MESSAGE, $IMAP, $REMOTE_OBJECTS;
704 if (!is_array($MESSAGE->parts) && empty($MESSAGE->body))
708 $attrib['id'] = 'rcmailMsgBody';
710 $safe_mode = $MESSAGE->is_safe || intval($_GET['_safe']);
713 $header_attrib = array();
714 foreach ($attrib as $attr => $value)
715 if (preg_match('/^headertable([a-z]+)$/i', $attr, $regs))
716 $header_attrib[$regs[1]] = $value;
718 if (!empty($MESSAGE->parts))
720 foreach ($MESSAGE->parts as $i => $part)
722 if ($part->type == 'headers')
723 $out .= rcmail_message_headers(sizeof($header_attrib) ? $header_attrib : NULL, $part->headers);
724 else if ($part->type == 'content')
726 if (empty($part->ctype_parameters) || empty($part->ctype_parameters['charset']))
727 $part->ctype_parameters['charset'] = $MESSAGE->headers->charset;
729 // fetch part if not available
730 if (!isset($part->body))
731 $part->body = $MESSAGE->get_part_content($part->mime_id);
733 $body = rcmail_print_body($part, $safe_mode, !$CONFIG['prefer_html']);
735 if ($part->ctype_secondary == 'html')
736 $out .= html::div('message-htmlpart', rcmail_html4inline($body, $attrib['id']));
738 $out .= html::div('message-part', $body);
743 $out .= html::div('message-part', html::div('pre', Q($MESSAGE->body)));
746 $ctype_primary = strtolower($MESSAGE->structure->ctype_primary);
747 $ctype_secondary = strtolower($MESSAGE->structure->ctype_secondary);
749 // list images after mail body
750 if (get_boolean($attrib['showimages']) && $ctype_primary == 'multipart' &&
751 !empty($MESSAGE->attachments) && !strstr($message_body, '<html')) {
752 foreach ($MESSAGE->attachments as $attach_prop) {
753 if (strpos($attach_prop->mimetype, 'image/') === 0) {
754 $out .= html::tag('hr') . html::p(array('align' => "center"),
756 'src' => $MESSAGE->get_part_url($attach_prop->mime_id),
757 'title' => $attach_prop->filename,
758 'alt' => $attach_prop->filename,
764 // tell client that there are blocked remote objects
765 if ($REMOTE_OBJECTS && !$safe_mode)
766 $OUTPUT->set_env('blockedobjects', true);
768 return html::div($attrib, $out);
774 * modify a HTML message that it can be displayed inside a HTML page
776 function rcmail_html4inline($body, $container_id)
780 $body_lc = strtolower($body);
782 // check for <base href>
783 if (preg_match(($base_reg = '/(<base.*href=["\']?)([hftps]{3,5}:\/{2}[^"\'\s]+)([^<]*>)/i'), $body, $base_regs))
784 $base_url = $base_regs[2];
787 while (($pos = strpos($body_lc, '<style', $last_style_pos)) && ($pos2 = strpos($body_lc, '</style>', $pos)))
789 $pos = strpos($body_lc, '>', $pos)+1;
791 // replace all css definitions with #container [def]
792 $styles = rcmail_mod_css_styles(substr($body, $pos, $pos2-$pos), $container_id, $base_url);
794 $body = substr($body, 0, $pos) . $styles . substr($body, $pos2);
795 $body_lc = strtolower($body);
796 $last_style_pos = $pos2;
799 // resolve <base href>
802 $body = preg_replace('/(src|background|href)=(["\']?)([\.\/]+[^"\'\s]+)(\2|\s|>)/Uie', "'\\1=\"'.make_absolute_url('\\3', '$base_url').'\"'", $body);
803 $body = preg_replace('/(url\s*\()(["\']?)([\.\/]+[^"\'\)\s]+)(\2)\)/Uie', "'\\1\''.make_absolute_url('\\3', '$base_url').'\')'", $body);
804 $body = preg_replace($base_reg, '', $body);
807 // modify HTML links to open a new window if clicked
808 $body = preg_replace('/<(a|link)\s+([^>]+)>/Uie', "rcmail_alter_html_link('\\1','\\2', '$container_id');", $body);
810 // add comments arround html and other tags
811 $out = preg_replace(array(
812 '/(<!DOCTYPE[^>]*>)/i',
814 '/(<\/?html[^>]*>)/i',
815 '/(<\/?head[^>]*>)/i',
816 '/(<title[^>]*>.*<\/title>)/Ui',
817 '/(<\/?meta[^>]*>)/i'),
822 array('/<body([^>]*)>/i', '/<\/body>/i'),
823 array('<div class="rcmBody"\\1>', '</div>'),
826 // quote <? of php and xml files that are specified as text/html
827 $out = preg_replace(array('/<\?/', '/\?>/'), array('<?', '?>'), $out);
834 * parse link attributes and set correct target
836 function rcmail_alter_html_link($tag, $attrs, $container_id)
838 $attrib = parse_attrib_string($attrs);
840 if ($tag == 'link' && preg_match('/^https?:\/\//i', $attrib['href']))
841 $attrib['href'] = "./bin/modcss.php?u=" . urlencode($attrib['href']) . "&c=" . urlencode($container_id);
843 else if (stristr((string)$attrib['href'], 'mailto:'))
844 $attrib['onclick'] = sprintf(
845 "return %s.command('compose','%s',this)",
847 JQ(substr($attrib['href'], 7)));
849 else if (!empty($attrib['href']) && $attrib['href']{0}!='#')
850 $attrib['target'] = '_blank';
852 return "<$tag" . create_attrib_string($attrib, array('href','name','target','onclick','id','class','style','title','rel','type','media')) . ' />';
857 * decode address string and re-format it as HTML links
859 function rcmail_address_string($input, $max=NULL, $addicon=NULL)
861 global $IMAP, $PRINT_MODE, $CONFIG, $OUTPUT, $EMAIL_ADDRESS_PATTERN;
863 $a_parts = $IMAP->decode_address_list($input);
865 if (!sizeof($a_parts))
868 $c = count($a_parts);
872 foreach ($a_parts as $part)
876 $out .= sprintf('%s <%s>', Q($part['name']), $part['mailto']);
877 else if (preg_match($EMAIL_ADDRESS_PATTERN, $part['mailto']))
879 $out .= sprintf('<a href="mailto:%s" onclick="return %s.command(\'compose\',\'%s\',this)" class="rcmContactAddress" title="%s">%s</a>',
887 $out .= sprintf(' <a href="#add" onclick="return %s.command(\'add-contact\',\'%s\',this)" title="%s"><img src="%s%s" alt="add" border="0" /></a>',
889 urlencode($part['string']),
890 rcube_label('addtoaddressbook'),
891 $CONFIG['skin_path'],
897 $out .= Q($part['name']);
899 $out .= (strlen($out) ? ' ' : '') . sprintf('<%s>', Q($part['mailto']));
903 $out .= ','.($max ? ' ' : ' ');
905 if ($max && $j==$max && $c>$j)
916 function rcmail_message_part_controls()
920 $part = asciiwords(get_input_value('_part', RCUBE_INPUT_GPC));
921 if (!is_object($MESSAGE) || !is_array($MESSAGE->parts) || !($_GET['_uid'] && $_GET['_part']) || !$MESSAGE->mime_parts[$part])
924 $part = $MESSAGE->mime_parts[$part];
925 $table = new html_table(array('cols' => 3));
927 if (!empty($part->filename)) {
928 $table->add('title', Q(rcube_label('filename')));
929 $table->add(null, Q($part->filename));
930 $table->add(null, '[' . html::a('?'.str_replace('_frame=', '_download=', $_SERVER['QUERY_STRING']), Q(rcube_label('download'))) . ']');
933 if (!empty($part->size)) {
934 $table->add('title', Q(rcube_label('filesize')));
935 $table->add(null, Q(show_bytes($part->size)));
938 return $table->show($attrib);
943 function rcmail_message_part_frame($attrib)
947 $part = $MESSAGE->mime_parts[asciiwords(get_input_value('_part', RCUBE_INPUT_GPC))];
948 $ctype_primary = strtolower($part->ctype_primary);
950 $attrib['src'] = Q('./?'.str_replace('_frame=', ($ctype_primary=='text' ? '_show=' : '_preload='), $_SERVER['QUERY_STRING']));
952 $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'src', 'width', 'height'));
953 $out = '<iframe '. $attrib_str . "></iframe>";
960 * clear message composing settings
962 function rcmail_compose_cleanup()
964 if (!isset($_SESSION['compose']))
967 // remove attachment files from temp dir
968 if (is_array($_SESSION['compose']['attachments']))
969 foreach ($_SESSION['compose']['attachments'] as $attachment)
970 @unlink($attachment['path']);
972 unset($_SESSION['compose']);
977 * Send the given message compose object using the configured method
979 function rcmail_deliver_message(&$message, $from, $mailto)
983 $msg_body = $message->get();
984 $headers = $message->headers();
986 // send thru SMTP server using custom SMTP library
987 if ($CONFIG['smtp_server'])
989 // generate list of recipients
990 $a_recipients = array($mailto);
992 if (strlen($headers['Cc']))
993 $a_recipients[] = $headers['Cc'];
994 if (strlen($headers['Bcc']))
995 $a_recipients[] = $headers['Bcc'];
997 // clean Bcc from header for recipients
998 $send_headers = $headers;
999 unset($send_headers['Bcc']);
1000 // here too, it because txtHeaders() below use $message->_headers not only $send_headers
1001 unset($message->_headers['Bcc']);
1004 $smtp_response = array();
1005 $sent = smtp_mail($from, $a_recipients, ($foo = $message->txtHeaders($send_headers, true)), $msg_body, $smtp_response);
1009 raise_error(array('code' => 800, 'type' => 'smtp', 'line' => __LINE__, 'file' => __FILE__,
1010 'message' => "SMTP error: ".join("\n", $smtp_response)), TRUE, FALSE);
1013 // send mail using PHP's mail() function
1016 // unset some headers because they will be added by the mail() function
1017 $headers_enc = $message->headers($headers);
1018 $headers_php = $message->_headers;
1019 unset($headers_php['To'], $headers_php['Subject']);
1021 // reset stored headers and overwrite
1022 $message->_headers = array();
1023 $header_str = $message->txtHeaders($headers_php);
1025 if (ini_get('safe_mode'))
1026 $sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str);
1028 $sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str, "-f$from");
1031 if ($sent) // remove MDN headers after sending
1032 unset($headers['Return-Receipt-To'], $headers['Disposition-Notification-To']);
1034 $message->_headers = array();
1035 $message->headers($headers);
1041 function rcmail_send_mdn($uid)
1043 global $CONFIG, $USER, $IMAP;
1045 $message = new rcube_message($uid);
1047 if ($message->headers->mdn_to && !$message->headers->mdn_sent)
1049 $identity = $USER->get_identity();
1050 $sender = format_email_recipient($identity['email'], $identity['name']);
1051 $recipient = array_shift($IMAP->decode_address_list($message->headers->mdn_to));
1052 $mailto = $recipient['mailto'];
1054 $compose = new rcube_mail_mime(rcmail_header_delm());
1055 $compose->setParam(array(
1056 'text_encoding' => 'quoted-printable',
1057 'html_encoding' => 'quoted-printable',
1058 'head_encoding' => 'quoted-printable',
1059 'head_charset' => RCMAIL_CHARSET,
1060 'html_charset' => RCMAIL_CHARSET,
1061 'text_charset' => RCMAIL_CHARSET,
1064 // compose headers array
1066 'Date' => date('r'),
1068 'To' => $message->headers->mdn_to,
1069 'Subject' => rcube_label('receiptread') . ': ' . $message->subject,
1070 'Message-ID' => sprintf('<%s@%s>', md5(uniqid('rcmail'.rand(),true)), rcmail_mail_domain($_SESSION['imap_host'])),
1071 'X-Sender' => $identity['email'],
1072 'Content-Type' => 'multipart/report; report-type=disposition-notification',
1075 if (!empty($CONFIG['useragent']))
1076 $headers['User-Agent'] = $CONFIG['useragent'];
1078 $body = rcube_label("yourmessage") . "\r\n\r\n" .
1079 "\t" . rcube_label("to") . ': ' . rcube_imap::decode_mime_string($message->headers->to, $message->headers->charset) . "\r\n" .
1080 "\t" . rcube_label("subject") . ': ' . $message->subject . "\r\n" .
1081 "\t" . rcube_label("sent") . ': ' . format_date($message->headers->date, $CONFIG['date_long']) . "\r\n" .
1082 "\r\n" . rcube_label("receiptnote") . "\r\n";
1084 $ua = !empty($CONFIG['useragent']) ? $CONFIG['useragent'] : "RoundCube Webmail (Version ".RCMAIL_VERSION.")";
1085 $report = "Reporting-UA: $ua\r\n";
1087 if ($message->headers->to)
1088 $report .= "Original-Recipient: {$message->headers->to}\r\n";
1090 $report .= "Final-Recipient: rfc822; {$identity['email']}\r\n" .
1091 "Original-Message-ID: {$message->headers->messageID}\r\n" .
1092 "Disposition: manual-action/MDN-sent-manually; displayed\r\n";
1094 $compose->headers($headers);
1095 $compose->setTXTBody(wordwrap($body, 75, "\r\n"));
1096 $compose->addAttachment($report, 'message/disposition-notification', 'MDNPart2.txt', false, '7bit', 'inline');
1098 $sent = rcmail_deliver_message($compose, $identity['email'], $mailto);
1102 $IMAP->set_flag($message->uid, 'MDNSENT');
1111 // register UI objects
1112 $OUTPUT->add_handlers(array(
1113 'mailboxlist' => 'rcmail_mailbox_list',
1114 'messages' => 'rcmail_message_list',
1115 'messagecountdisplay' => 'rcmail_messagecount_display',
1116 'quotadisplay' => 'rcmail_quota_display',
1117 'messageheaders' => 'rcmail_message_headers',
1118 'messagebody' => 'rcmail_message_body',
1119 'messagecontentframe' => 'rcmail_messagecontent_frame',
1120 'messagepartframe' => 'rcmail_message_part_frame',
1121 'messagepartcontrols' => 'rcmail_message_part_controls',
1122 'searchform' => array($OUTPUT, 'search_form'),