4 +-----------------------------------------------------------------------+
5 | program/steps/mail/func.inc |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2005, 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 429 2006-12-22 22:26:24Z 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();
32 // set imap properties and session vars
33 if (strlen($_GET['_mbox']))
35 $IMAP->set_mailbox($_GET['_mbox']);
36 $_SESSION['mbox'] = $_GET['_mbox'];
39 if (strlen($_GET['_page']))
41 $IMAP->set_page($_GET['_page']);
42 $_SESSION['page'] = $_GET['_page'];
45 // set mailbox to INBOX if not set
46 if (empty($_SESSION['mbox']))
47 $_SESSION['mbox'] = $IMAP->get_mailbox_name();
49 // set default sort col/order to session
50 if (!isset($_SESSION['sort_col']))
51 $_SESSION['sort_col'] = $CONFIG['message_sort_col'];
52 if (!isset($_SESSION['sort_order']))
53 $_SESSION['sort_order'] = $CONFIG['message_sort_order'];
56 // define url for getting message parts
57 if (strlen($_GET['_uid']))
58 $GET_URL = sprintf('%s&_action=get&_mbox=%s&_uid=%d', $COMM_PATH, $IMAP->get_mailbox_name(), $_GET['_uid']);
61 // set current mailbox in client environment
62 $OUTPUT->add_script(sprintf("%s.set_env('mailbox', '%s');", $JS_OBJECT_NAME, $IMAP->get_mailbox_name()));
64 if ($CONFIG['trash_mbox'])
65 $OUTPUT->add_script(sprintf("%s.set_env('trash_mailbox', '%s');", $JS_OBJECT_NAME, $CONFIG['trash_mbox']));
67 if ($CONFIG['drafts_mbox'])
68 $OUTPUT->add_script(sprintf("%s.set_env('drafts_mailbox', '%s');", $JS_OBJECT_NAME, $CONFIG['drafts_mbox']));
70 if ($CONFIG['junk_mbox'])
71 $OUTPUT->add_script(sprintf("%s.set_env('junk_mailbox', '%s');", $JS_OBJECT_NAME, $CONFIG['junk_mbox']));
73 // return the mailboxlist in HTML
74 function rcmail_mailbox_list($attrib)
76 global $IMAP, $CONFIG, $OUTPUT, $JS_OBJECT_NAME, $COMM_PATH;
77 static $s_added_script = FALSE;
80 // add some labels to client
81 rcube_add_label('purgefolderconfirm');
83 // $mboxlist_start = rcube_timer();
85 $type = $attrib['type'] ? $attrib['type'] : 'ul';
86 $add_attrib = $type=='select' ? array('style', 'class', 'id', 'name', 'onchange') :
87 array('style', 'class', 'id');
89 if ($type=='ul' && !$attrib['id'])
90 $attrib['id'] = 'rcmboxlist';
92 // allow the following attributes to be added to the <ul> tag
93 $attrib_str = create_attrib_string($attrib, $add_attrib);
95 $out = '<' . $type . $attrib_str . ">\n";
97 // add no-selection option
98 if ($type=='select' && $attrib['noselection'])
99 $out .= sprintf('<option value="0">%s</option>'."\n",
100 rcube_label($attrib['noselection']));
103 $mbox_name = $IMAP->get_mailbox_name();
105 // for these mailboxes we have localized labels
106 $special_mailboxes = array('inbox', 'sent', 'drafts', 'trash', 'junk');
109 // build the folders tree
110 if (empty($a_mailboxes))
113 $a_folders = $IMAP->list_mailboxes();
114 $delimiter = $IMAP->get_hierarchy_delimiter();
115 $a_mailboxes = array();
117 // rcube_print_time($mboxlist_start, 'list_mailboxes()');
119 foreach ($a_folders as $folder)
120 rcmail_build_folder_tree($a_mailboxes, $folder, $delimiter);
123 // var_dump($a_mailboxes);
126 $out .= rcmail_render_folder_tree_select($a_mailboxes, $special_mailboxes, $mbox_name, $attrib['maxlength']);
128 $out .= rcmail_render_folder_tree_html($a_mailboxes, $special_mailboxes, $mbox_name, $attrib['maxlength']);
130 // rcube_print_time($mboxlist_start, 'render_folder_tree()');
134 $OUTPUT->add_script(sprintf("%s.gui_object('mailboxlist', '%s');", $JS_OBJECT_NAME, $attrib['id']));
136 return $out . "</$type>";
142 // create a hierarchical array of the mailbox list
143 function rcmail_build_folder_tree(&$arrFolders, $folder, $delm='/', $path='')
145 $pos = strpos($folder, $delm);
148 $subFolders = substr($folder, $pos+1);
149 $currentFolder = substr($folder, 0, $pos);
154 $currentFolder = $folder;
157 $path .= $currentFolder;
159 if (!isset($arrFolders[$currentFolder]))
161 $arrFolders[$currentFolder] = array('id' => $path,
162 'name' => rcube_charset_convert($currentFolder, 'UTF-7'),
163 'folders' => array());
166 if (!empty($subFolders))
167 rcmail_build_folder_tree($arrFolders[$currentFolder]['folders'], $subFolders, $delm, $path.$delm);
171 // return html for a structured list <ul> for the mailbox tree
172 function rcmail_render_folder_tree_html(&$arrFolders, &$special, &$mbox_name, $maxlength, $nestLevel=0)
174 global $JS_OBJECT_NAME, $COMM_PATH, $IMAP, $CONFIG, $OUTPUT;
178 foreach ($arrFolders as $key => $folder)
180 $zebra_class = ($nestLevel*$idx)%2 ? 'even' : 'odd';
183 $folder_lc = strtolower($folder['id']);
184 if (in_array($folder_lc, $special))
185 $foldername = rcube_label($folder_lc);
188 $foldername = $folder['name'];
190 // shorten the folder name to a given length
191 if ($maxlength && $maxlength>1)
193 $fname = abbrevate_string($foldername, $maxlength);
194 if ($fname != $foldername)
195 $title = ' title="'.rep_specialchars_output($foldername, 'html', 'all').'"';
196 $foldername = $fname;
200 // add unread message count display
201 if ($unread_count = $IMAP->messagecount($folder['id'], 'RECENT', ($folder['id']==$mbox_name)))
202 $foldername .= sprintf(' (%d)', $unread_count);
204 // make folder name safe for ids and class names
205 $folder_css = $class_name = preg_replace('/[^a-z0-9\-_]/', '', $folder_lc);
207 // set special class for Sent, Drafts, Trash and Junk
208 if ($folder['id']==$CONFIG['sent_mbox'])
209 $class_name = 'sent';
210 else if ($folder['id']==$CONFIG['drafts_mbox'])
211 $class_name = 'drafts';
212 else if ($folder['id']==$CONFIG['trash_mbox'])
213 $class_name = 'trash';
214 else if ($folder['id']==$CONFIG['junk_mbox'])
215 $class_name = 'junk';
217 $out .= sprintf('<li id="rcmbx%s" class="mailbox %s %s%s%s"><a href="%s&_mbox=%s"'.
218 ' onclick="return %s.command(\'list\',\'%s\')"'.
219 ' onmouseover="return %s.focus_mailbox(\'%s\')"' .
220 ' onmouseout="return %s.unfocus_mailbox(\'%s\')"' .
221 ' onmouseup="return %s.mbox_mouse_up(\'%s\')"%s>%s</a>',
225 $unread_count ? ' unread' : '',
226 addslashes($folder['id'])==addslashes($mbox_name) ? ' selected' : '',
228 urlencode($folder['id']),
230 addslashes($folder['id']),
232 addslashes($folder['id']),
234 addslashes($folder['id']),
236 addslashes($folder['id']),
238 rep_specialchars_output($foldername, 'html', 'all'));
240 if (!empty($folder['folders']))
241 $out .= "\n<ul>\n" . rcmail_render_folder_tree_html($folder['folders'], $special, $mbox_name, $maxlength, $nestLevel+1) . "</ul>\n";
251 // return html for a flat list <select> for the mailbox tree
252 function rcmail_render_folder_tree_select(&$arrFolders, &$special, &$mbox_name, $maxlength, $nestLevel=0)
254 global $IMAP, $OUTPUT;
258 foreach ($arrFolders as $key=>$folder)
260 $folder_lc = strtolower($folder['id']);
261 if (in_array($folder_lc, $special))
262 $foldername = rcube_label($folder_lc);
265 $foldername = $folder['name'];
267 // shorten the folder name to a given length
268 if ($maxlength && $maxlength>1)
269 $foldername = abbrevate_string($foldername, $maxlength);
272 $out .= sprintf('<option value="%s">%s%s</option>'."\n",
274 str_repeat(' ', $nestLevel*4),
275 rep_specialchars_output($foldername, 'html', 'all'));
277 if (!empty($folder['folders']))
278 $out .= rcmail_render_folder_tree_select($folder['folders'], $special, $mbox_name, $maxlength, $nestLevel+1);
287 // return the message list as HTML table
288 function rcmail_message_list($attrib)
290 global $IMAP, $CONFIG, $COMM_PATH, $OUTPUT, $JS_OBJECT_NAME;
292 $skin_path = $CONFIG['skin_path'];
293 $image_tag = '<img src="%s%s" alt="%s" border="0" />';
295 // check to see if we have some settings for sorting
296 $sort_col = $_SESSION['sort_col'];
297 $sort_order = $_SESSION['sort_order'];
299 // add some labels to client
300 rcube_add_label('from', 'to');
302 // get message headers
303 $a_headers = $IMAP->list_headers('', '', $sort_col, $sort_order);
305 // add id to message list table if not specified
306 if (!strlen($attrib['id']))
307 $attrib['id'] = 'rcubemessagelist';
309 // allow the following attributes to be added to the <table> tag
310 $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border', 'summary'));
312 $out = '<table' . $attrib_str . ">\n";
315 // define list of cols to be displayed
316 $a_show_cols = is_array($CONFIG['list_cols']) ? $CONFIG['list_cols'] : array('subject');
317 $a_sort_cols = array('subject', 'date', 'from', 'to', 'size');
319 // show 'to' instead of from in sent messages
320 if (($IMAP->get_mailbox_name()==$CONFIG['sent_mbox'] || $IMAP->get_mailbox_name()==$CONFIG['drafts_mbox']) && ($f = array_search('from', $a_show_cols))
321 && !array_search('to', $a_show_cols))
322 $a_show_cols[$f] = 'to';
324 // add col definition
325 $out .= '<colgroup>';
326 $out .= '<col class="icon" />';
328 foreach ($a_show_cols as $col)
329 $out .= sprintf('<col class="%s" />', $col);
331 $out .= '<col class="icon" />';
332 $out .= "</colgroup>\n";
335 $out .= "<thead><tr>\n<td class=\"icon\"> </td>\n";
338 foreach ($a_show_cols as $col)
341 $col_name = rep_specialchars_output(rcube_label($col));
345 if ($IMAP->get_capability('sort') && in_array($col, $a_sort_cols))
347 // have buttons configured
348 if (!empty($attrib['sortdescbutton']) || !empty($attrib['sortascbutton']))
350 $sort = ' ';
353 if (!empty($attrib['sortascbutton']))
355 $sort .= rcube_button(array('command' => 'sort',
356 'prop' => $col.'_ASC',
357 'image' => $attrib['sortascbutton'],
358 'align' => 'absmiddle',
359 'title' => 'sortasc'));
363 if (!empty($attrib['sortdescbutton']))
365 $sort .= rcube_button(array('command' => 'sort',
366 'prop' => $col.'_DESC',
367 'image' => $attrib['sortdescbutton'],
368 'align' => 'absmiddle',
369 'title' => 'sortdesc'));
372 // just add a link tag to the header
375 $col_name = sprintf('<a href="./#sort" onclick="return %s.command(\'sort\',\'%s\',this)" title="%s">%s</a>',
378 rcube_label('sortby'),
383 $sort_class = $col==$sort_col ? " sorted$sort_order" : '';
385 // put it all together
386 $out .= '<td class="'.$col.$sort_class.'" id="rcmHead'.$col.'">' . "$col_name$sort</td>\n";
389 $out .= '<td class="icon">'.($attrib['attachmenticon'] ? sprintf($image_tag, $skin_path, $attrib['attachmenticon'], '') : '')."</td>\n";
390 $out .= "</tr></thead>\n<tbody>\n";
392 // no messages in this mailbox
393 if (!sizeof($a_headers))
395 $out .= rep_specialchars_output(
396 sprintf('<tr><td colspan="%d">%s</td></tr>',
397 sizeof($a_show_cols)+2,
398 rcube_label('nomessagesfound')));
402 $a_js_message_arr = array();
404 // create row for each message
405 foreach ($a_headers as $i => $header) //while (list($i, $header) = each($a_headers))
407 $message_icon = $attach_icon = '';
408 $js_row_arr = array();
409 $zebra_class = $i%2 ? 'even' : 'odd';
411 // set messag attributes to javascript array
412 if ($header->deleted)
413 $js_row_arr['deleted'] = true;
415 $js_row_arr['unread'] = true;
416 if ($header->answered)
417 $js_row_arr['replied'] = true;
419 if ($attrib['deletedicon'] && $header->deleted)
420 $message_icon = $attrib['deletedicon'];
421 else if ($attrib['unreadicon'] && !$header->seen)
422 $message_icon = $attrib['unreadicon'];
423 else if ($attrib['repliedicon'] && $header->answered)
424 $message_icon = $attrib['repliedicon'];
425 else if ($attrib['messageicon'])
426 $message_icon = $attrib['messageicon'];
428 // set attachment icon
429 if ($attrib['attachmenticon'] && preg_match("/multipart\/m/i", $header->ctype))
430 $attach_icon = $attrib['attachmenticon'];
432 $out .= sprintf('<tr id="rcmrow%d" class="message%s%s %s">'."\n",
434 $header->seen ? '' : ' unread',
435 $header->deleted ? ' deleted' : '',
438 $out .= sprintf("<td class=\"icon\">%s</td>\n", $message_icon ? sprintf($image_tag, $skin_path, $message_icon, '') : '');
441 foreach ($a_show_cols as $col)
443 if ($col=='from' || $col=='to')
444 $cont = rep_specialchars_output(rcmail_address_string($header->$col, 3, $attrib['addicon']));
445 else if ($col=='subject')
447 $cont = rep_specialchars_output($IMAP->decode_header($header->$col), 'html', 'all');
448 // firefox/mozilla temporary workaround to pad subject with content so that whitespace in rows responds to drag+drop
449 $cont .= '<img src="./program/blank.gif" height="5" width="1000" alt="" />';
451 else if ($col=='size')
452 $cont = show_bytes($header->$col);
453 else if ($col=='date')
454 $cont = format_date($header->date); //date('m.d.Y G:i:s', strtotime($header->date));
456 $cont = rep_specialchars_output($header->$col, 'html', 'all');
458 $out .= '<td class="'.$col.'">' . $cont . "</td>\n";
461 $out .= sprintf("<td class=\"icon\">%s</td>\n", $attach_icon ? sprintf($image_tag, $skin_path, $attach_icon, '') : '');
464 if (sizeof($js_row_arr))
465 $a_js_message_arr[$header->uid] = $js_row_arr;
468 // complete message table
469 $out .= "</tbody></table>\n";
472 $message_count = $IMAP->messagecount();
475 $javascript .= sprintf("%s.gui_object('mailcontframe', '%s');\n", $JS_OBJECT_NAME, 'mailcontframe');
476 $javascript .= sprintf("%s.gui_object('messagelist', '%s');\n", $JS_OBJECT_NAME, $attrib['id']);
477 $javascript .= sprintf("%s.set_env('messagecount', %d);\n", $JS_OBJECT_NAME, $message_count);
478 $javascript .= sprintf("%s.set_env('current_page', %d);\n", $JS_OBJECT_NAME, $IMAP->list_page);
479 $javascript .= sprintf("%s.set_env('pagecount', %d);\n", $JS_OBJECT_NAME, ceil($message_count/$IMAP->page_size));
480 $javascript .= sprintf("%s.set_env('sort_col', '%s');\n", $JS_OBJECT_NAME, $sort_col);
481 $javascript .= sprintf("%s.set_env('sort_order', '%s');\n", $JS_OBJECT_NAME, $sort_order);
483 if ($attrib['messageicon'])
484 $javascript .= sprintf("%s.set_env('messageicon', '%s%s');\n", $JS_OBJECT_NAME, $skin_path, $attrib['messageicon']);
485 if ($attrib['deletedicon'])
486 $javascript .= sprintf("%s.set_env('deletedicon', '%s%s');\n", $JS_OBJECT_NAME, $skin_path, $attrib['deletedicon']);
487 if ($attrib['unreadicon'])
488 $javascript .= sprintf("%s.set_env('unreadicon', '%s%s');\n", $JS_OBJECT_NAME, $skin_path, $attrib['unreadicon']);
489 if ($attrib['repliedicon'])
490 $javascript .= sprintf("%s.set_env('repliedicon', '%s%s');\n", $JS_OBJECT_NAME, $skin_path, $attrib['repliedicon']);
491 if ($attrib['attachmenticon'])
492 $javascript .= sprintf("%s.set_env('attachmenticon', '%s%s');\n", $JS_OBJECT_NAME, $skin_path, $attrib['attachmenticon']);
494 $javascript .= sprintf("%s.set_env('messages', %s);", $JS_OBJECT_NAME, array2js($a_js_message_arr));
496 $OUTPUT->add_script($javascript);
504 // return javascript commands to add rows to the message list
505 function rcmail_js_message_list($a_headers, $insert_top=FALSE)
507 global $CONFIG, $IMAP;
510 $a_show_cols = is_array($CONFIG['list_cols']) ? $CONFIG['list_cols'] : array('subject');
512 // show 'to' instead of from in sent messages
513 if (strtolower($IMAP->get_mailbox_name())=='sent' && ($f = array_search('from', $a_show_cols))
514 && !array_search('to', $a_show_cols))
515 $a_show_cols[$f] = 'to';
517 $commands .= sprintf("this.set_message_coltypes(%s);\n", array2js($a_show_cols));
519 // loop through message headers
520 for ($n=0; $a_headers[$n]; $n++)
522 $header = $a_headers[$n];
523 $a_msg_cols = array();
524 $a_msg_flags = array();
526 // format each col; similar as in rcmail_message_list()
527 foreach ($a_show_cols as $col)
529 if ($col=='from' || $col=='to')
530 $cont = rep_specialchars_output(rcmail_address_string($header->$col, 3));
531 else if ($col=='subject')
532 $cont = rep_specialchars_output($IMAP->decode_header($header->$col), 'html', 'all');
533 else if ($col=='size')
534 $cont = show_bytes($header->$col);
535 else if ($col=='date')
536 $cont = format_date($header->date); //date('m.d.Y G:i:s', strtotime($header->date));
538 $cont = rep_specialchars_output($header->$col, 'html', 'all');
540 $a_msg_cols[$col] = $cont;
543 $a_msg_flags['deleted'] = $header->deleted ? 1 : 0;
544 $a_msg_flags['unread'] = $header->seen ? 0 : 1;
545 $a_msg_flags['replied'] = $header->answered ? 1 : 0;
546 $commands .= sprintf("this.add_message_row(%s, %s, %s, %b, %b);\n",
548 array2js($a_msg_cols),
549 array2js($a_msg_flags),
550 preg_match("/multipart\/m/i", $header->ctype),
558 // return code for search function
559 function rcmail_search_form($attrib)
561 global $OUTPUT, $JS_OBJECT_NAME;
563 // add some labels to client
564 rcube_add_label('searching');
566 $attrib['name'] = '_q';
568 if (empty($attrib['id']))
569 $attrib['id'] = 'rcmqsearchbox';
571 $input_q = new textfield($attrib);
572 $out = $input_q->show();
574 $OUTPUT->add_script(sprintf("%s.gui_object('qsearchbox', '%s');",
578 // add form tag around text field
579 if (empty($attrib['form']))
580 $out = sprintf('<form name="rcmqsearchform" action="./" '.
581 'onsubmit="%s.command(\'search\');return false" style="display:inline;">%s</form>',
589 function rcmail_messagecount_display($attrib)
591 global $IMAP, $OUTPUT, $JS_OBJECT_NAME;
594 $attrib['id'] = 'rcmcountdisplay';
596 $OUTPUT->add_script(sprintf("%s.gui_object('countdisplay', '%s');",
600 // allow the following attributes to be added to the <span> tag
601 $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id'));
604 $out = '<span' . $attrib_str . '>';
605 $out .= rcmail_get_messagecount_text();
611 function rcmail_quota_display($attrib)
613 global $IMAP, $OUTPUT, $JS_OBJECT_NAME;
616 $attrib['id'] = 'rcmquotadisplay';
618 $OUTPUT->add_script(sprintf("%s.gui_object('quotadisplay', '%s');", $JS_OBJECT_NAME, $attrib['id']));
620 // allow the following attributes to be added to the <span> tag
621 $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id'));
623 if (!$IMAP->get_capability('QUOTA'))
624 $quota_text = rcube_label('unknown');
625 else if (!($quota_text = $IMAP->get_quota()))
626 $quota_text = rcube_label('unlimited');
628 $out = '<span' . $attrib_str . '>';
635 function rcmail_get_messagecount_text($count=NULL, $page=NULL)
637 global $IMAP, $MESSAGE;
639 if (isset($MESSAGE['index']))
641 return rcube_label(array('name' => 'messagenrof',
642 'vars' => array('nr' => $MESSAGE['index']+1,
643 'count' => $count!==NULL ? $count : $IMAP->messagecount())));
647 $page = $IMAP->list_page;
649 $start_msg = ($page-1) * $IMAP->page_size + 1;
650 $max = $count!==NULL ? $count : $IMAP->messagecount();
653 $out = rcube_label('mailboxempty');
655 $out = rcube_label(array('name' => 'messagesfromto',
656 'vars' => array('from' => $start_msg,
657 'to' => min($max, $start_msg + $IMAP->page_size - 1),
660 return rep_specialchars_output($out);
664 function rcmail_print_body($part, $safe=FALSE, $plain=FALSE) // $body, $ctype_primary='text', $ctype_secondary='plain', $encoding='7bit', $safe=FALSE, $plain=FALSE)
666 global $IMAP, $REMOTE_OBJECTS, $JS_OBJECT_NAME;
668 // extract part properties: body, ctype_primary, ctype_secondary, encoding, parameters
671 $block = $plain ? '%s' : '%s'; //'<div style="display:block;">%s</div>';
672 $body = $IMAP->mime_decode($body, $encoding);
673 $body = $IMAP->charset_decode($body, $parameters);
676 if ($ctype_secondary=='html')
678 if (!$safe) // remove remote images and scripts
680 $remote_patterns = array('/(src|background)=(["\']?)([hftps]{3,5}:\/{2}[^"\'\s]+)(\2|\s|>)/Ui',
681 // '/(src|background)=(["\']?)([\.\/]+[^"\'\s]+)(\2|\s|>)/Ui',
682 '/(<base.*href=["\']?)([hftps]{3,5}:\/{2}[^"\'\s]+)([^<]*>)/i',
683 '/(<link.*href=["\']?)([hftps]{3,5}:\/{2}[^"\'\s]+)([^<]*>)/i',
684 '/url\s*\(["\']?([hftps]{3,5}:\/{2}[^"\'\s]+)["\']?\)/i',
685 '/url\s*\(["\']?([\.\/]+[^"\'\s]+)["\']?\)/i',
686 '/<script.+<\/script>/Umis');
688 $remote_replaces = array('', // '\\1=\\2#\\4',
696 // set flag if message containes remote obejcts that where blocked
697 foreach ($remote_patterns as $pattern)
699 if (preg_match($pattern, $body))
701 $REMOTE_OBJECTS = TRUE;
706 $body = preg_replace($remote_patterns, $remote_replaces, $body);
709 return sprintf($block, rep_specialchars_output($body, 'html', '', FALSE));
713 if ($ctype_secondary=='enriched')
715 $body = enriched_to_html($body);
716 return sprintf($block, rep_specialchars_output($body, 'html'));
720 // make links and email-addresses clickable
721 $convert_patterns = $convert_replaces = $replace_strings = array();
723 $url_chars = 'a-z0-9_\-\+\*\$\/&%=@#:';
724 $url_chars_within = '\?\.~,!';
726 $convert_patterns[] = "/([\w]+):\/\/([a-z0-9\-\.]+[a-z]{2,4}([$url_chars$url_chars_within]*[$url_chars])?)/ie";
727 $convert_replaces[] = "rcmail_str_replacement('<a href=\"\\1://\\2\" target=\"_blank\">\\1://\\2</a>', \$replace_strings)";
729 $convert_patterns[] = "/([^\/:]|\s)(www\.)([a-z0-9\-]{2,}[a-z]{2,4}([$url_chars$url_chars_within]*[$url_chars])?)/ie";
730 $convert_replaces[] = "rcmail_str_replacement('\\1<a href=\"http://\\2\\3\" target=\"_blank\">\\2\\3</a>', \$replace_strings)";
732 $convert_patterns[] = '/([a-z0-9][a-z0-9\-\.\+\_]*@[a-z0-9]([a-z0-9\-][.]?)*[a-z0-9]\\.[a-z]{2,5})/ie';
733 $convert_replaces[] = "rcmail_str_replacement('<a href=\"mailto:\\1\" onclick=\"return $JS_OBJECT_NAME.command(\'compose\',\'\\1\',this)\">\\1</a>', \$replace_strings)";
735 $body = wordwrap(trim($body), 80);
736 $body = preg_replace($convert_patterns, $convert_replaces, $body);
738 // split body into single lines
739 $a_lines = preg_split('/\r?\n/', $body);
741 // colorize quoted parts
742 for($n=0; $n<sizeof($a_lines); $n++)
744 $line = $a_lines[$n];
748 else if ($line{1}=='>')
750 else if ($line{0}=='>')
755 $line = rep_specialchars_output($line, 'html', 'replace', FALSE);
758 $a_lines[$n] = sprintf('<font color="%s">%s</font>', $color, $line);
760 $a_lines[$n] = $line;
763 // insert the links for urls and mailtos
764 $body = preg_replace("/##string_replacement\{([0-9]+)\}##/e", "\$replace_strings[\\1]", join("\n", $a_lines));
766 return sprintf($block, "<pre>\n".$body."\n</pre>");
772 // add a string to the replacement array and return a replacement string
773 function rcmail_str_replacement($str, &$rep)
776 $rep[$count] = stripslashes($str);
777 return "##string_replacement{".($count++)."}##";
781 function rcmail_parse_message($structure, $arg=array(), $recursive=FALSE)
784 static $sa_inline_objects = array();
786 // arguments are: (bool)$prefer_html, (string)$get_url
789 $a_attachments = array();
790 $a_return_parts = array();
793 $message_ctype_primary = strtolower($structure->ctype_primary);
794 $message_ctype_secondary = strtolower($structure->ctype_secondary);
796 // show message headers
797 if ($recursive && is_array($structure->headers) && isset($structure->headers['subject']))
798 $a_return_parts[] = array('type' => 'headers',
799 'headers' => $structure->headers);
801 // print body if message doesn't have multiple parts
802 if ($message_ctype_primary=='text')
804 $a_return_parts[] = array('type' => 'content',
805 'body' => $structure->body,
806 'ctype_primary' => $message_ctype_primary,
807 'ctype_secondary' => $message_ctype_secondary,
808 'parameters' => $structure->ctype_parameters,
809 'encoding' => $structure->headers['content-transfer-encoding']);
812 // message contains alternative parts
813 else if ($message_ctype_primary=='multipart' && $message_ctype_secondary=='alternative' && is_array($structure->parts))
815 // get html/plaintext parts
816 $plain_part = $html_part = $print_part = $related_part = NULL;
818 foreach ($structure->parts as $p => $sub_part)
820 $sub_ctype_primary = strtolower($sub_part->ctype_primary);
821 $sub_ctype_secondary = strtolower($sub_part->ctype_secondary);
823 // check if sub part is
824 if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='plain')
826 else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='html')
828 else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='enriched')
830 else if ($sub_ctype_primary=='multipart' && $sub_ctype_secondary=='related')
834 // parse related part (alternative part could be in here)
835 if ($related_part!==NULL && $prefer_html)
837 list($parts, $attachmnts) = rcmail_parse_message($structure->parts[$related_part], $arg, TRUE);
838 $a_return_parts = array_merge($a_return_parts, $parts);
839 $a_attachments = array_merge($a_attachments, $attachmnts);
842 // print html/plain part
843 else if ($html_part!==NULL && $prefer_html)
844 $print_part = $structure->parts[$html_part];
845 else if ($enriched_part!==NULL)
846 $print_part = $structure->parts[$enriched_part];
847 else if ($plain_part!==NULL)
848 $print_part = $structure->parts[$plain_part];
851 if (is_object($print_part))
852 $a_return_parts[] = array('type' => 'content',
853 'body' => $print_part->body,
854 'ctype_primary' => strtolower($print_part->ctype_primary),
855 'ctype_secondary' => strtolower($print_part->ctype_secondary),
856 'parameters' => $print_part->ctype_parameters,
857 'encoding' => $print_part->headers['content-transfer-encoding']);
858 // show plaintext warning
859 else if ($html_part!==NULL)
860 $a_return_parts[] = array('type' => 'content',
861 'body' => rcube_label('htmlmessage'),
862 'ctype_primary' => 'text',
863 'ctype_secondary' => 'plain');
865 // add html part as attachment
866 if ($html_part!==NULL && $structure->parts[$html_part]!==$print_part)
868 $html_part = $structure->parts[$html_part];
869 $a_attachments[] = array('filename' => rcube_label('htmlmessage'),
870 'encoding' => $html_part->headers['content-transfer-encoding'],
871 'mimetype' => 'text/html',
872 'part_id' => $html_part->mime_id,
873 'size' => strlen($IMAP->mime_decode($html_part->body, $html_part->headers['content-transfer-encoding'])));
877 // message contains multiple parts
878 else if ($message_ctype_primary=='multipart' && is_array($structure->parts))
880 foreach ($structure->parts as $mail_part)
882 $primary_type = strtolower($mail_part->ctype_primary);
883 $secondary_type = strtolower($mail_part->ctype_secondary);
885 // multipart/alternative
886 if ($primary_type=='multipart') // && ($secondary_type=='alternative' || $secondary_type=='mixed' || $secondary_type=='related'))
888 list($parts, $attachmnts) = rcmail_parse_message($mail_part, $arg, TRUE);
890 $a_return_parts = array_merge($a_return_parts, $parts);
891 $a_attachments = array_merge($a_attachments, $attachmnts);
894 // part text/[plain|html] OR message/delivery-status
895 else if (($primary_type=='text' && ($secondary_type=='plain' || $secondary_type=='html') && $mail_part->disposition!='attachment') ||
896 ($primary_type=='message' && $secondary_type=='delivery-status'))
898 $a_return_parts[] = array('type' => 'content',
899 'body' => $mail_part->body,
900 'ctype_primary' => $primary_type,
901 'ctype_secondary' => $secondary_type,
902 'parameters' => $mail_part->ctype_parameters,
903 'encoding' => $mail_part->headers['content-transfer-encoding']);
907 else if ($primary_type=='message')
909 /* don't parse headers here; they're parsed within the recursive call to rcmail_parse_message()
910 if ($mail_part->parts[0]->headers)
911 $a_return_parts[] = array('type' => 'headers',
912 'headers' => $mail_part->parts[0]->headers);
915 list($parts, $attachmnts) = rcmail_parse_message($mail_part->parts[0], $arg, TRUE);
917 $a_return_parts = array_merge($a_return_parts, $parts);
918 $a_attachments = array_merge($a_attachments, $attachmnts);
921 // part is file/attachment
922 else if ($mail_part->disposition=='attachment' || $mail_part->disposition=='inline' || $mail_part->headers['content-id'] ||
923 (empty($mail_part->disposition) && ($mail_part->d_parameters['filename'] || $mail_part->ctype_parameters['name'])))
925 if ($message_ctype_secondary=='related' && $mail_part->headers['content-id'])
926 $sa_inline_objects[] = array('filename' => rcube_imap::decode_mime_string($mail_part->d_parameters['filename']),
927 'mimetype' => strtolower("$primary_type/$secondary_type"),
928 'part_id' => $mail_part->mime_id,
929 'content_id' => preg_replace(array('/^</', '/>$/'), '', $mail_part->headers['content-id']));
931 else if ($mail_part->d_parameters['filename'])
932 $a_attachments[] = array('filename' => rcube_imap::decode_mime_string($mail_part->d_parameters['filename']),
933 'encoding' => strtolower($mail_part->headers['content-transfer-encoding']),
934 'mimetype' => strtolower("$primary_type/$secondary_type"),
935 'part_id' => $mail_part->mime_id,
936 'size' => strlen($IMAP->mime_decode($mail_part->body, $mail_part->headers['content-transfer-encoding'])) /*,
937 'content' => $mail_part->body */);
939 else if ($mail_part->ctype_parameters['name'])
940 $a_attachments[] = array('filename' => rcube_imap::decode_mime_string($mail_part->ctype_parameters['name']),
941 'encoding' => strtolower($mail_part->headers['content-transfer-encoding']),
942 'mimetype' => strtolower("$primary_type/$secondary_type"),
943 'part_id' => $mail_part->mime_id,
944 'size' => strlen($IMAP->mime_decode($mail_part->body, $mail_part->headers['content-transfer-encoding'])) /*,
945 'content' => $mail_part->body */);
947 else if ($mail_part->headers['content-description'])
948 $a_attachments[] = array('filename' => rcube_imap::decode_mime_string($mail_part->headers['content-description']),
949 'encoding' => strtolower($mail_part->headers['content-transfer-encoding']),
950 'mimetype' => strtolower("$primary_type/$secondary_type"),
951 'part_id' => $mail_part->mime_id,
952 'size' => strlen($IMAP->mime_decode($mail_part->body, $mail_part->headers['content-transfer-encoding'])) /*,
953 'content' => $mail_part->body */);
958 // if this was a related part try to resolve references
959 if ($message_ctype_secondary=='related' && sizeof($sa_inline_objects))
961 $a_replace_patters = array();
962 $a_replace_strings = array();
964 foreach ($sa_inline_objects as $inline_object)
966 $a_replace_patters[] = 'cid:'.$inline_object['content_id'];
967 $a_replace_strings[] = sprintf($get_url, $inline_object['part_id']);
970 foreach ($a_return_parts as $i => $return_part)
972 if ($return_part['type']!='content')
975 // decode body and replace cid:...
976 $a_return_parts[$i]['body'] = str_replace($a_replace_patters, $a_replace_strings, $IMAP->mime_decode($return_part['body'], $return_part['encoding']));
977 $a_return_parts[$i]['encoding'] = '7bit';
983 // join all parts together
984 //$out .= join($part_delimiter, $a_return_parts);
986 return array($a_return_parts, $a_attachments);
992 // return table with message headers
993 function rcmail_message_headers($attrib, $headers=NULL)
995 global $IMAP, $OUTPUT, $MESSAGE;
998 // keep header table attrib
999 if (is_array($attrib) && !$sa_attrib)
1000 $sa_attrib = $attrib;
1001 else if (!is_array($attrib) && is_array($sa_attrib))
1002 $attrib = $sa_attrib;
1005 if (!isset($MESSAGE))
1008 // get associative array of headers object
1010 $headers = is_object($MESSAGE['headers']) ? get_object_vars($MESSAGE['headers']) : $MESSAGE['headers'];
1014 // allow the following attributes to be added to the <table> tag
1015 $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border', 'summary'));
1016 $out = '<table' . $attrib_str . ">\n";
1018 // show these headers
1019 $standard_headers = array('subject', 'from', 'organization', 'to', 'cc', 'bcc', 'reply-to', 'date');
1021 foreach ($standard_headers as $hkey)
1023 if (!$headers[$hkey])
1026 if ($hkey=='date' && !empty($headers[$hkey]))
1027 $header_value = format_date(strtotime($headers[$hkey]));
1028 else if (in_array($hkey, array('from', 'to', 'cc', 'bcc', 'reply-to')))
1029 $header_value = rep_specialchars_output(rcmail_address_string($headers[$hkey], NULL, $attrib['addicon']));
1031 $header_value = rep_specialchars_output($IMAP->decode_header($headers[$hkey]), '', 'all');
1034 $out .= '<td class="header-title">'.rep_specialchars_output(rcube_label($hkey)).": </td>\n";
1035 $out .= '<td class="'.$hkey.'" width="90%">'.$header_value."</td>\n</tr>";
1039 $out .= "\n</table>\n\n";
1041 return $header_count ? $out : '';
1046 function rcmail_message_body($attrib)
1048 global $CONFIG, $OUTPUT, $MESSAGE, $GET_URL, $REMOTE_OBJECTS, $JS_OBJECT_NAME;
1050 if (!is_array($MESSAGE['parts']) && !$MESSAGE['body'])
1054 $attrib['id'] = 'rcmailMsgBody';
1056 $safe_mode = (bool)$_GET['_safe'];
1057 $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id'));
1058 $out = '<div '. $attrib_str . ">\n";
1060 $header_attrib = array();
1061 foreach ($attrib as $attr => $value)
1062 if (preg_match('/^headertable([a-z]+)$/i', $attr, $regs))
1063 $header_attrib[$regs[1]] = $value;
1066 // this is an ecrypted message
1067 // -> create a plaintext body with the according message
1068 if (!sizeof($MESSAGE['parts']) && $MESSAGE['headers']->ctype=='multipart/encrypted')
1070 $MESSAGE['parts'][0] = array('type' => 'content',
1071 'ctype_primary' => 'text',
1072 'ctype_secondary' => 'plain',
1073 'body' => rcube_label('encryptedmessage'));
1076 if ($MESSAGE['parts'])
1078 foreach ($MESSAGE['parts'] as $i => $part)
1080 if ($part['type']=='headers')
1081 $out .= rcmail_message_headers(sizeof($header_attrib) ? $header_attrib : NULL, $part['headers']);
1082 else if ($part['type']=='content')
1084 if (empty($part['parameters']) || empty($part['parameters']['charset']))
1085 $part['parameters']['charset'] = $MESSAGE['headers']->charset;
1087 // $body = rcmail_print_body($part['body'], $part['ctype_primary'], $part['ctype_secondary'], $part['encoding'], $safe_mode);
1088 $body = rcmail_print_body($part, $safe_mode);
1089 $out .= '<div class="message-part">';
1091 if ($part['ctype_secondary']!='plain')
1092 $out .= rcmail_mod_html_body($body, $attrib['id']);
1101 $out .= $MESSAGE['body'];
1104 $ctype_primary = strtolower($MESSAGE['structure']->ctype_primary);
1105 $ctype_secondary = strtolower($MESSAGE['structure']->ctype_secondary);
1107 // list images after mail body
1108 if (get_boolean($attrib['showimages']) && $ctype_primary=='multipart' && $ctype_secondary=='mixed' &&
1109 sizeof($MESSAGE['attachments']) && !strstr($message_body, '<html') && strlen($GET_URL))
1111 foreach ($MESSAGE['attachments'] as $attach_prop)
1113 if (strpos($attach_prop['mimetype'], 'image/')===0)
1114 $out .= sprintf("\n<hr />\n<p align=\"center\"><img src=\"%s&_part=%s\" alt=\"%s\" title=\"%s\" /></p>\n",
1115 $GET_URL, $attach_prop['part_id'],
1116 $attach_prop['filename'],
1117 $attach_prop['filename']);
1121 // tell client that there are blocked remote objects
1122 if ($REMOTE_OBJECTS && !$safe_mode)
1123 $OUTPUT->add_script(sprintf("%s.set_env('blockedobjects', true);", $JS_OBJECT_NAME));
1131 // modify a HTML message that it can be displayed inside a HTML page
1132 function rcmail_mod_html_body($body, $container_id)
1134 // remove any null-byte characters before parsing
1135 $body = preg_replace('/\x00/', '', $body);
1137 $last_style_pos = 0;
1138 $body_lc = strtolower($body);
1141 while (($pos = strpos($body_lc, '<style', $last_style_pos)) && ($pos2 = strpos($body_lc, '</style>', $pos)))
1144 $body_pre = substr($body, 0, $pos);
1145 $styles = substr($body, $pos, $pos2-$pos);
1146 $body_post = substr($body, $pos2, strlen($body)-$pos2);
1148 // replace all css definitions with #container [def]
1149 $styles = rcmail_mod_css_styles($styles, $container_id);
1151 $body = $body_pre . $styles . $body_post;
1152 $last_style_pos = $pos2;
1156 // remove SCRIPT tags
1157 foreach (array('script', 'applet', 'object', 'embed', 'iframe') as $tag)
1159 while (($pos = strpos($body_lc, '<'.$tag)) && ($pos2 = strpos($body_lc, '</'.$tag.'>', $pos)))
1162 $body = substr($body, 0, $pos) . substr($body, $pos2, strlen($body)-$pos2);
1163 $body_lc = strtolower($body);
1167 // replace event handlers on any object
1168 while ($body != $prev_body)
1171 $body = preg_replace('/(<[^!][^>]*\s)(on[^=>]+)=([^>]+>)/im', '$1__removed=$3', $body);
1172 $body = preg_replace('/(<[^!][^>]*\shref=["\']?)(javascript:)([^>]*?>)/im', '$1null:$3', $body);
1175 // resolve <base href>
1176 $base_reg = '/(<base.*href=["\']?)([hftps]{3,5}:\/{2}[^"\'\s]+)([^<]*>)/i';
1177 if (preg_match($base_reg, $body, $regs))
1179 $base_url = $regs[2];
1180 $body = preg_replace('/(src|background|href)=(["\']?)([\.\/]+[^"\'\s]+)(\2|\s|>)/Uie', "'\\1=\"'.make_absolute_url('\\3', '$base_url').'\"'", $body);
1181 $body = preg_replace('/(url\s*\()(["\']?)([\.\/]+[^"\'\)\s]+)(\2)\)/Uie', "'\\1\''.make_absolute_url('\\3', '$base_url').'\')'", $body);
1182 $body = preg_replace($base_reg, '', $body);
1185 // modify HTML links to open a new window if clicked
1186 $body = preg_replace('/<a\s+([^>]+)>/Uie', "rcmail_alter_html_link('\\1');", $body);
1188 // add comments arround html and other tags
1189 $out = preg_replace(array('/(<\/?html[^>]*>)/i',
1190 '/(<\/?head[^>]*>)/i',
1191 '/(<title[^>]*>.*<\/title>)/Ui',
1192 '/(<\/?meta[^>]*>)/i'),
1196 $out = preg_replace(array('/(<body[^>]*>)/i',
1198 array('<div class="rcmBody">',
1206 // parse link attributes and set correct target
1207 function rcmail_alter_html_link($in)
1209 $attrib = parse_attrib_string($in);
1211 if (stristr((string)$attrib['href'], 'mailto:'))
1212 $attrib['onclick'] = sprintf("return %s.command('compose','%s',this)",
1213 $GLOBALS['JS_OBJECT_NAME'],
1214 preg_replace("/'+/i","",substr($attrib['href'], 7)));
1215 else if (!empty($attrib['href']) && $attrib['href']{0}!='#')
1216 $attrib['target'] = '_blank';
1218 return '<a' . create_attrib_string($attrib, array('href', 'name', 'target', 'onclick', 'id', 'class', 'style', 'title')) . '>';
1222 // replace all css definitions with #container [def]
1223 function rcmail_mod_css_styles($source, $container_id)
1225 $a_css_values = array();
1228 // cut out all contents between { and }
1229 while (($pos = strpos($source, '{', $last_pos)) && ($pos2 = strpos($source, '}', $pos)))
1231 $key = sizeof($a_css_values);
1232 $a_css_values[$key] = substr($source, $pos+1, $pos2-($pos+1));
1233 $source = substr($source, 0, $pos+1) . "<<str_replacement[$key]>>" . substr($source, $pos2, strlen($source)-$pos2);
1237 $styles = preg_replace('/(^\s*|,\s*)([a-z0-9\._][a-z0-9\.\-_]*)/im', "\\1#$container_id \\2", $source);
1238 $styles = preg_replace('/<<str_replacement\[([0-9]+)\]>>/e', "\$a_css_values[\\1]", $styles);
1240 // replace body definition because we also stripped off the <body> tag
1241 $styles = preg_replace("/$container_id\s+body/i", "$container_id div.rcmBody", $styles);
1248 // return first text part of a message
1249 function rcmail_first_text_part($message_parts)
1251 if (!is_array($message_parts))
1256 // check all message parts
1257 foreach ($message_parts as $pid => $part)
1259 $mimetype = strtolower($part->ctype_primary.'/'.$part->ctype_secondary);
1260 if ($mimetype=='text/plain')
1262 $body = rcube_imap::mime_decode($part->body, $part->headers['content-transfer-encoding']);
1263 $body = rcube_imap::charset_decode($body, $part->ctype_parameters);
1266 else if ($mimetype=='text/html')
1268 $html_part = rcube_imap::mime_decode($part->body, $part->headers['content-transfer-encoding']);
1269 $html_part = rcube_imap::charset_decode($html_part, $part->ctype_parameters);
1274 // convert HTML to plain text
1277 // remove special chars encoding
1278 $trans = array_flip(get_html_translation_table(HTML_ENTITIES));
1279 $html_part = strtr($html_part, $trans);
1281 // create instance of html2text class
1282 $txt = new html2text($html_part);
1283 return $txt->get_text();
1290 // get source code of a specific message and cache it
1291 function rcmail_message_source($uid)
1293 global $IMAP, $DB, $CONFIG;
1295 // get message ID if uid is given
1296 $cache_key = $IMAP->mailbox.'.msg';
1297 $cached = $IMAP->get_cached_message($cache_key, $uid, FALSE);
1299 // message is cached in database
1300 if ($cached && !empty($cached->body))
1301 return $cached->body;
1304 $headers = $IMAP->get_headers($uid);
1306 $headers = &$cached;
1308 // create unique identifier based on message_id
1309 if (!empty($headers->messageID))
1310 $message_id = md5($headers->messageID);
1312 $message_id = md5($headers->uid.'@'.$_SESSION['imap_host']);
1314 $temp_dir = $CONFIG['temp_dir'].(!eregi('\/$', $CONFIG['temp_dir']) ? '/' : '');
1315 $cache_dir = $temp_dir.$_SESSION['client_id'];
1316 $cache_path = $cache_dir.'/'.$message_id;
1318 // message is cached in temp dir
1319 if ($CONFIG['enable_caching'] && is_dir($cache_dir) && is_file($cache_path))
1321 if ($fp = fopen($cache_path, 'r'))
1323 $msg_source = fread($fp, filesize($cache_path));
1330 // get message from server
1331 $msg_source = $IMAP->get_raw_body($uid);
1333 // return message source without caching
1334 if (!$CONFIG['enable_caching'])
1338 // let's cache the message body within the database
1339 if ($cached && ($CONFIG['db_max_length'] -300) > $headers->size)
1341 $DB->query("UPDATE ".get_table_name('messages')."
1347 $_SESSION['user_id'],
1355 // create dir for caching
1356 if (!is_dir($cache_dir))
1357 $dir = mkdir($cache_dir);
1361 // attempt to write a file with the message body
1362 if ($dir && ($fp = fopen($cache_path, 'w')))
1364 fwrite($fp, $msg_source);
1369 raise_error(array('code' => 403, 'type' => 'php', 'line' => __LINE__, 'file' => __FILE__,
1370 'message' => "Failed to write to temp dir"), TRUE, FALSE);
1377 // decode address string and re-format it as HTML links
1378 function rcmail_address_string($input, $max=NULL, $addicon=NULL)
1380 global $IMAP, $PRINT_MODE, $CONFIG, $OUTPUT, $JS_OBJECT_NAME, $EMAIL_ADDRESS_PATTERN;
1382 $a_parts = $IMAP->decode_address_list($input);
1384 if (!sizeof($a_parts))
1387 $c = count($a_parts);
1391 foreach ($a_parts as $part)
1395 $out .= sprintf('%s <%s>', rep_specialchars_output($part['name']), $part['mailto']);
1396 else if (preg_match($EMAIL_ADDRESS_PATTERN, $part['mailto']))
1398 $out .= sprintf('<a href="mailto:%s" onclick="return %s.command(\'compose\',\'%s\',this)" class="rcmContactAddress" title="%s">%s</a>',
1403 rep_specialchars_output($part['name']));
1406 $out .= sprintf(' <a href="#add" onclick="return %s.command(\'add-contact\',\'%s\',this)" title="%s"><img src="%s%s" alt="add" border="0" /></a>',
1408 urlencode($part['string']),
1409 rcube_label('addtoaddressbook'),
1410 $CONFIG['skin_path'],
1416 $out .= rep_specialchars_output($part['name']);
1417 if ($part['mailto'])
1418 $out .= (strlen($out) ? ' ' : '') . sprintf('<%s>', $part['mailto']);
1422 $out .= ','.($max ? ' ' : ' ');
1424 if ($max && $j==$max && $c>$j)
1435 function rcmail_message_part_controls()
1437 global $CONFIG, $IMAP, $MESSAGE;
1439 if (!is_array($MESSAGE) || !is_array($MESSAGE['parts']) || !($_GET['_uid'] && $_GET['_part']) || !$MESSAGE['parts'][$_GET['_part']])
1442 $part = $MESSAGE['parts'][$_GET['_part']];
1444 $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'cellspacing', 'cellpadding', 'border', 'summary'));
1445 $out = '<table '. $attrib_str . ">\n";
1447 $filename = $part->d_parameters['filename'] ? $part->d_parameters['filename'] : $part->ctype_parameters['name'];
1448 $filesize = strlen($IMAP->mime_decode($part->body, $part->headers['content-transfer-encoding']));
1452 $out .= sprintf('<tr><td class="title">%s</td><td>%s</td><td>[<a href="./?%s">%s</a>]</tr>'."\n",
1453 rcube_label('filename'),
1454 rep_specialchars_output($filename),
1455 str_replace('_frame=', '_download=', $_SERVER['QUERY_STRING']),
1456 rcube_label('download'));
1460 $out .= sprintf('<tr><td class="title">%s</td><td>%s</td></tr>'."\n",
1461 rcube_label('filesize'),
1462 show_bytes($filesize));
1464 $out .= "\n</table>";
1471 function rcmail_message_part_frame($attrib)
1475 $part = $MESSAGE['parts'][$_GET['_part']];
1476 $ctype_primary = strtolower($part->ctype_primary);
1478 $attrib['src'] = './?'.str_replace('_frame=', ($ctype_primary=='text' ? '_show=' : '_preload='), $_SERVER['QUERY_STRING']);
1480 $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'src', 'width', 'height'));
1481 $out = '<iframe '. $attrib_str . "></ifame>";
1487 // create temp dir for attachments
1488 function rcmail_create_compose_tempdir()
1492 if ($_SESSION['compose']['temp_dir'])
1493 return $_SESSION['compose']['temp_dir'];
1495 if (!empty($CONFIG['temp_dir']))
1496 $temp_dir = $CONFIG['temp_dir'].(!eregi('\/$', $CONFIG['temp_dir']) ? '/' : '').$_SESSION['compose']['id'];
1498 // create temp-dir for uploaded attachments
1499 if (!empty($CONFIG['temp_dir']) && is_writeable($CONFIG['temp_dir']))
1502 $_SESSION['compose']['temp_dir'] = $temp_dir;
1505 return $_SESSION['compose']['temp_dir'];
1509 // clear message composing settings
1510 function rcmail_compose_cleanup()
1512 if (!isset($_SESSION['compose']))
1515 // remove attachment files from temp dir
1516 if (is_array($_SESSION['compose']['attachments']))
1517 foreach ($_SESSION['compose']['attachments'] as $attachment)
1518 @unlink($attachment['path']);
1521 if ($_SESSION['compose']['temp_dir'])
1522 @rmdir($_SESSION['compose']['temp_dir']);
1524 unset($_SESSION['compose']);