alecpl
2008-07-28 3ac95d5a673db544d7ceeaa9e5fca766cb738120
commit | author | age
4e17e6 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/mail/func.inc                                           |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail client                     |
8fa58e 8  | Copyright (C) 2005-2008, RoundCube Dev. - Switzerland                 |
30233b 9  | Licensed under the GNU GPL                                            |
4e17e6 10  |                                                                       |
T 11  | PURPOSE:                                                              |
12  |   Provide webmail functionality and GUI objects                       |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id$
19
20 */
21
22 require_once('lib/enriched.inc');
fba1f5 23 require_once('include/rcube_smtp.inc');
4e17e6 24
T 25
26 $EMAIL_ADDRESS_PATTERN = '/([a-z0-9][a-z0-9\-\.\+\_]*@[a-z0-9]([a-z0-9\-][.]?)*[a-z0-9]\\.[a-z]{2,5})/i';
27
f11541 28 if (empty($_SESSION['mbox']))
c5ac07 29   $_SESSION['mbox'] = $IMAP->get_mailbox_name();
S 30
4e17e6 31 // set imap properties and session vars
b3ce79 32 if ($mbox = get_input_value('_mbox', RCUBE_INPUT_GPC))
c57996 33   $IMAP->set_mailbox(($_SESSION['mbox'] = $mbox));
4e17e6 34
b3ce79 35 if (!empty($_GET['_page']))
c57996 36   $IMAP->set_page(($_SESSION['page'] = intval($_GET['_page'])));
4e17e6 37
fecb03 38 // set mailbox to INBOX if not set
T 39 if (empty($_SESSION['mbox']))
40   $_SESSION['mbox'] = $IMAP->get_mailbox_name();
4e17e6 41
6a35c8 42 // set default sort col/order to session
T 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'];
2bca6e 47
T 48 // set message set for search result
8d0758 49 if (!empty($_REQUEST['_search']) && isset($_SESSION['search'][$_REQUEST['_search']]))
1f020b 50   {
8d0758 51   $IMAP->set_search_set($_SESSION['search'][$_REQUEST['_search']]);
1f020b 52   $OUTPUT->set_env('search_request', $_REQUEST['_search']);
S 53   $OUTPUT->set_env('search_text', $_SESSION['last_text_search']);
54   }
4e17e6 55
T 56
57 // set current mailbox in client environment
f11541 58 $OUTPUT->set_env('mailbox', $IMAP->get_mailbox_name());
719804 59 $OUTPUT->set_env('quota', $IMAP->get_capability('quota'));
ddd776 60 $OUTPUT->set_env('delimiter', $IMAP->get_hierarchy_delimiter());
4e17e6 61
T 62 if ($CONFIG['trash_mbox'])
f11541 63   $OUTPUT->set_env('trash_mailbox', $CONFIG['trash_mbox']);
1966c5 64 if ($CONFIG['drafts_mbox'])
f11541 65   $OUTPUT->set_env('drafts_mailbox', $CONFIG['drafts_mbox']);
b4b081 66 if ($CONFIG['junk_mbox'])
f11541 67   $OUTPUT->set_env('junk_mailbox', $CONFIG['junk_mbox']);
T 68
69 if (!$OUTPUT->ajax_call)
e3902e 70   rcube_add_label('checkingmail', 'deletemessage', 'movemessagetotrash', 'movingmessage');
f11541 71
5eee00 72 // set page title
197601 73 if (empty($RCMAIL->action) || $RCMAIL->action == 'list')
fed22f 74   $OUTPUT->set_pagetitle(rcmail_localize_foldername($IMAP->get_mailbox_name()));
5eee00 75
4e17e6 76
45f56c 77 /**
T 78  * return the message list as HTML table
79  */
4e17e6 80 function rcmail_message_list($attrib)
T 81   {
f11541 82   global $IMAP, $CONFIG, $COMM_PATH, $OUTPUT;
b076a4 83
4e17e6 84   $skin_path = $CONFIG['skin_path'];
e189a6 85   $image_tag = '<img src="%s%s" alt="%s" />';
b076a4 86
f3b659 87   // check to see if we have some settings for sorting
6a35c8 88   $sort_col   = $_SESSION['sort_col'];
T 89   $sort_order = $_SESSION['sort_order'];
24053e 90   
T 91   // add some labels to client
92   rcube_add_label('from', 'to');
f3b659 93
4e17e6 94   // get message headers
f3b659 95   $a_headers = $IMAP->list_headers('', '', $sort_col, $sort_order);
4e17e6 96
T 97   // add id to message list table if not specified
98   if (!strlen($attrib['id']))
99     $attrib['id'] = 'rcubemessagelist';
100
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'));
103
104   $out = '<table' . $attrib_str . ">\n";
e0ddd4 105
4e17e6 106   // define list of cols to be displayed
T 107   $a_show_cols = is_array($CONFIG['list_cols']) ? $CONFIG['list_cols'] : array('subject');
c8c1e0 108   $a_sort_cols = array('subject', 'date', 'from', 'to', 'size');
41bece 109
T 110   $mbox = $IMAP->get_mailbox_name();
4e17e6 111   
T 112   // show 'to' instead of from in sent messages
41bece 113   if (($mbox==$CONFIG['sent_mbox'] || $mbox==$CONFIG['drafts_mbox']) && ($f = array_search('from', $a_show_cols))
8c2e58 114       && !array_search('to', $a_show_cols))
4e17e6 115     $a_show_cols[$f] = 'to';
8c2e58 116   
e0ddd4 117   // add col definition
T 118   $out .= '<colgroup>';
01c86f 119   $out .= '<col class="icon" />';
e0ddd4 120
T 121   foreach ($a_show_cols as $col)
01c86f 122     $out .= sprintf('<col class="%s" />', $col);
e0ddd4 123
01c86f 124   $out .= '<col class="icon" />';
e0ddd4 125   $out .= "</colgroup>\n";
4e17e6 126
T 127   // add table title
128   $out .= "<thead><tr>\n<td class=\"icon\">&nbsp;</td>\n";
b076a4 129
f3b659 130   $javascript = '';
4e17e6 131   foreach ($a_show_cols as $col)
f3b659 132     {
T 133     // get column name
e189a6 134     $col_name = $col != 'flag' ? Q(rcube_label($col)) : sprintf($image_tag, $skin_path, $attrib['unflaggedicon'], '');
f3b659 135
T 136     // make sort links
137     $sort = '';
2a9cb3 138     if (in_array($col, $a_sort_cols))
f3b659 139       {
1cded8 140       // have buttons configured
T 141       if (!empty($attrib['sortdescbutton']) || !empty($attrib['sortascbutton']))
142         {
143         $sort = '&nbsp;&nbsp;';
b076a4 144
1cded8 145         // asc link
T 146         if (!empty($attrib['sortascbutton']))
147           {
f11541 148           $sort .= $OUTPUT->button(array(
T 149             'command' => 'sort',
150             'prop' => $col.'_ASC',
151             'image' => $attrib['sortascbutton'],
152             'align' => 'absmiddle',
153             'title' => 'sortasc'));
1cded8 154           }       
b076a4 155         
1cded8 156         // desc link
T 157         if (!empty($attrib['sortdescbutton']))
158           {
f11541 159           $sort .= $OUTPUT->button(array(
T 160             'command' => 'sort',
161             'prop' => $col.'_DESC',
162             'image' => $attrib['sortdescbutton'],
163             'align' => 'absmiddle',
164             'title' => 'sortdesc'));
1cded8 165           }
T 166         }
167       // just add a link tag to the header
168       else
b076a4 169         {
41bece 170         $col_name = sprintf(
T 171           '<a href="./#sort" onclick="return %s.command(\'sort\',\'%s\',this)" title="%s">%s</a>',
172           JS_OBJECT_NAME,
173           $col,
174           rcube_label('sortby'),
175           $col_name);
b076a4 176         }
f3b659 177       }
b076a4 178       
T 179     $sort_class = $col==$sort_col ? " sorted$sort_order" : '';
f3b659 180
T 181     // put it all together
b076a4 182     $out .= '<td class="'.$col.$sort_class.'" id="rcmHead'.$col.'">' . "$col_name$sort</td>\n";    
f3b659 183     }
4e17e6 184
T 185   $out .= '<td class="icon">'.($attrib['attachmenticon'] ? sprintf($image_tag, $skin_path, $attrib['attachmenticon'], '') : '')."</td>\n";
186   $out .= "</tr></thead>\n<tbody>\n";
187
188   // no messages in this mailbox
189   if (!sizeof($a_headers))
5eee00 190     $OUTPUT->show_message('nomessagesfound', 'notice');
4e17e6 191
T 192
193   $a_js_message_arr = array();
194
195   // create row for each message
196   foreach ($a_headers as $i => $header)  //while (list($i, $header) = each($a_headers))
197     {
e189a6 198     $message_icon = $attach_icon = $flagged_icon = '';
4e17e6 199     $js_row_arr = array();
T 200     $zebra_class = $i%2 ? 'even' : 'odd';
201
202     // set messag attributes to javascript array
6ec0a8 203     if ($header->deleted)
S 204       $js_row_arr['deleted'] = true;
4e17e6 205     if (!$header->seen)
T 206       $js_row_arr['unread'] = true;
207     if ($header->answered)
208       $js_row_arr['replied'] = true;
e189a6 209     if ($header->flagged)
A 210       $js_row_arr['flagged'] = true;
211
6ec0a8 212     // set message icon  
S 213     if ($attrib['deletedicon'] && $header->deleted)
214       $message_icon = $attrib['deletedicon'];
215     else if ($attrib['unreadicon'] && !$header->seen)
4e17e6 216       $message_icon = $attrib['unreadicon'];
T 217     else if ($attrib['repliedicon'] && $header->answered)
218       $message_icon = $attrib['repliedicon'];
219     else if ($attrib['messageicon'])
220       $message_icon = $attrib['messageicon'];
e189a6 221
A 222     if ($attrib['flaggedicon'] && $header->flagged)
223       $flagged_icon = $attrib['flaggedicon'];
224     else if ($attrib['unflaggedicon'] && !$header->flagged)
225       $flagged_icon = $attrib['unflaggedicon'];
4e17e6 226     
f11541 227     // set attachment icon
8d4bcd 228     if ($attrib['attachmenticon'] && preg_match("/multipart\/[mr]/i", $header->ctype))
4e17e6 229       $attach_icon = $attrib['attachmenticon'];
T 230         
15a9d1 231     $out .= sprintf('<tr id="rcmrow%d" class="message%s%s %s">'."\n",
T 232                     $header->uid,
233                     $header->seen ? '' : ' unread',
234                     $header->deleted ? ' deleted' : '',
e189a6 235                     $header->flagged ? ' flagged' : '',
15a9d1 236                     $zebra_class);    
T 237     
4e17e6 238     $out .= sprintf("<td class=\"icon\">%s</td>\n", $message_icon ? sprintf($image_tag, $skin_path, $message_icon, '') : '');
e189a6 239
1088d6 240
583850 241     if (!empty($header->charset))
A 242       $IMAP->set_charset($header->charset);
243   
4e17e6 244     // format each col
T 245     foreach ($a_show_cols as $col)
246       {
247       if ($col=='from' || $col=='to')
583850 248         $cont = Q(rcmail_address_string($header->$col, 3, $attrib['addicon']), 'show');
4e17e6 249       else if ($col=='subject')
b4b081 250         {
41bece 251         $action = $mbox==$CONFIG['drafts_mbox'] ? 'compose' : 'show';
2b962c 252         $uid_param = $mbox==$CONFIG['drafts_mbox'] ? '_draft_uid' : '_uid';
583850 253         $cont = Q($IMAP->decode_header($header->$col));
f11541 254         if (empty($cont)) $cont = Q(rcube_label('nosubject'));
b2fb95 255         $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);
b4b081 256         }
e189a6 257       else if ($col=='flag')
A 258         $cont = $flagged_icon ? sprintf($image_tag, $skin_path, $flagged_icon, '') : '';
4e17e6 259       else if ($col=='size')
T 260         $cont = show_bytes($header->$col);
261       else if ($col=='date')
f11541 262         $cont = format_date($header->date);
4e17e6 263       else
2bca6e 264         $cont = Q($header->$col);
4e17e6 265         
2bca6e 266       $out .= '<td class="'.$col.'">' . $cont . "</td>\n";
4e17e6 267       }
T 268
269     $out .= sprintf("<td class=\"icon\">%s</td>\n", $attach_icon ? sprintf($image_tag, $skin_path, $attach_icon, '') : '');
270     $out .= "</tr>\n";
271     
272     if (sizeof($js_row_arr))
273       $a_js_message_arr[$header->uid] = $js_row_arr;
274     }
275   
276   // complete message table
277   $out .= "</tbody></table>\n";
278   
279   
280   $message_count = $IMAP->messagecount();
281   
282   // set client env
f11541 283   $OUTPUT->add_gui_object('mailcontframe', 'mailcontframe');
T 284   $OUTPUT->add_gui_object('messagelist', $attrib['id']);
285   $OUTPUT->set_env('messagecount', $message_count);
286   $OUTPUT->set_env('current_page', $IMAP->list_page);
287   $OUTPUT->set_env('pagecount', ceil($message_count/$IMAP->page_size));
288   $OUTPUT->set_env('sort_col', $sort_col);
289   $OUTPUT->set_env('sort_order', $sort_order);
4e17e6 290   
T 291   if ($attrib['messageicon'])
f11541 292     $OUTPUT->set_env('messageicon', $skin_path . $attrib['messageicon']);
6ec0a8 293   if ($attrib['deletedicon'])
f11541 294     $OUTPUT->set_env('deletedicon', $skin_path . $attrib['deletedicon']);
4e17e6 295   if ($attrib['unreadicon'])
f11541 296     $OUTPUT->set_env('unreadicon', $skin_path . $attrib['unreadicon']);
4e17e6 297   if ($attrib['repliedicon'])
f11541 298     $OUTPUT->set_env('repliedicon', $skin_path . $attrib['repliedicon']);
4e17e6 299   if ($attrib['attachmenticon'])
f11541 300     $OUTPUT->set_env('attachmenticon', $skin_path . $attrib['attachmenticon']);
e189a6 301   if ($attrib['flaggedicon'])
A 302     $OUTPUT->set_env('flaggedicon', $skin_path . $attrib['flaggedicon']);
303   if ($attrib['unflaggedicon'])
304     $OUTPUT->set_env('unflaggedicon', $skin_path . $attrib['unflaggedicon']);
4e17e6 305   
ae895a 306   $OUTPUT->set_env('messages', $a_js_message_arr);
d24d20 307   $OUTPUT->set_env('coltypes', $a_show_cols);
f11541 308   
6b47de 309   $OUTPUT->include_script('list.js');
4e17e6 310   
T 311   return $out;
312   }
313
314
45f56c 315 /**
T 316  * return javascript commands to add rows to the message list
317  */
4e17e6 318 function rcmail_js_message_list($a_headers, $insert_top=FALSE)
T 319   {
f11541 320   global $CONFIG, $IMAP, $OUTPUT;
4e17e6 321
T 322   $a_show_cols = is_array($CONFIG['list_cols']) ? $CONFIG['list_cols'] : array('subject');
41bece 323   $mbox = $IMAP->get_mailbox_name();
4e17e6 324
T 325   // show 'to' instead of from in sent messages
41bece 326   if (($mbox == $CONFIG['sent_mbox'] || $mbox == $CONFIG['drafts_mbox'])
f11541 327       && (($f = array_search('from', $a_show_cols)) !== false) && array_search('to', $a_show_cols) === false)
4e17e6 328     $a_show_cols[$f] = 'to';
T 329
f11541 330   $OUTPUT->command('set_message_coltypes', $a_show_cols);
25d8ba 331
4e17e6 332   // loop through message headers
ecd2e7 333   foreach ($a_headers as $n => $header)
4e17e6 334     {
T 335     $a_msg_cols = array();
336     $a_msg_flags = array();
ecd2e7 337     
T 338     if (empty($header))
339       continue;
f11541 340
583850 341     if (!empty($header->charset))
A 342       $IMAP->set_charset($header->charset);
343
4e17e6 344     // format each col; similar as in rcmail_message_list()
T 345     foreach ($a_show_cols as $col)
346       {
347       if ($col=='from' || $col=='to')
583850 348         $cont = Q(rcmail_address_string($header->$col, 3), 'show');
4e17e6 349       else if ($col=='subject')
7bbd5f 350         {
41bece 351         $action = $mbox==$CONFIG['drafts_mbox'] ? 'compose' : 'show';
2b962c 352         $uid_param = $mbox==$CONFIG['drafts_mbox'] ? '_draft_uid' : '_uid';
583850 353         $cont = Q($IMAP->decode_header($header->$col));
7bbd5f 354         if (!$cont) $cont = Q(rcube_label('nosubject'));
b2fb95 355         $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);
7bbd5f 356         }
4e17e6 357       else if ($col=='size')
T 358         $cont = show_bytes($header->$col);
359       else if ($col=='date')
f11541 360         $cont = format_date($header->date);
4e17e6 361       else
2bca6e 362         $cont = Q($header->$col);
4e17e6 363           
T 364       $a_msg_cols[$col] = $cont;
365       }
366
6ec0a8 367     $a_msg_flags['deleted'] = $header->deleted ? 1 : 0;
4e17e6 368     $a_msg_flags['unread'] = $header->seen ? 0 : 1;
T 369     $a_msg_flags['replied'] = $header->answered ? 1 : 0;
e189a6 370     $a_msg_flags['flagged'] = $header->flagged ? 1 : 0;
A 371     
f11541 372     $OUTPUT->command('add_message_row',
T 373       $header->uid,
374       $a_msg_cols,
375       $a_msg_flags,
376       preg_match("/multipart\/m/i", $header->ctype),
377       $insert_top);
4e17e6 378     }
T 379   }
380
381
45f56c 382 /**
T 383  * return an HTML iframe for loading mail content
384  */
b19097 385 function rcmail_messagecontent_frame($attrib)
T 386   {
f11541 387   global $OUTPUT;
b19097 388   
T 389   if (empty($attrib['id']))
390     $attrib['id'] = 'rcmailcontentwindow';
391
392   // allow the following attributes to be added to the <iframe> tag
393   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'src', 'width', 'height', 'frameborder'));
394   $framename = $attrib['id'];
395
396   $out = sprintf('<iframe name="%s"%s></iframe>'."\n",
397          $framename,
398          $attrib_str);
399
f11541 400   $OUTPUT->set_env('contentframe', $framename);
T 401   $OUTPUT->set_env('blankpage', $attrib['src'] ? $OUTPUT->abs_url($attrib['src']) : 'program/blank.gif');
b19097 402
T 403   return $out;
404   }
405
4e17e6 406
45f56c 407 /**
T 408  *
409  */
4e17e6 410 function rcmail_messagecount_display($attrib)
T 411   {
f11541 412   global $IMAP, $OUTPUT;
4e17e6 413   
T 414   if (!$attrib['id'])
415     $attrib['id'] = 'rcmcountdisplay';
416
f11541 417   $OUTPUT->add_gui_object('countdisplay', $attrib['id']);
4e17e6 418
T 419   // allow the following attributes to be added to the <span> tag
420   $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id'));
421
422   
423   $out = '<span' . $attrib_str . '>';
424   $out .= rcmail_get_messagecount_text();
425   $out .= '</span>';
426   return $out;
427   }
428
429
45f56c 430 /**
T 431  *
432  */
58e360 433 function rcmail_quota_display($attrib)
T 434   {
f11541 435   global $OUTPUT, $COMM_PATH;
58e360 436
T 437   if (!$attrib['id'])
438     $attrib['id'] = 'rcmquotadisplay';
439
6d2714 440   if(isset($attrib['display']))
A 441     $_SESSION['quota_display'] = $attrib['display'];
442
f11541 443   $OUTPUT->add_gui_object('quotadisplay', $attrib['id']);
58e360 444
T 445   // allow the following attributes to be added to the <span> tag
97f57d 446   $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'display'));
23796e 447
S 448   $out = '<span' . $attrib_str . '>';
6d2714 449   $out .= rcmail_quota_content();
23796e 450   $out .= '</span>';
S 451   return $out;
452   }
453
454
45f56c 455 /**
T 456  *
457  */
6d2714 458 function rcmail_quota_content($quota=NULL)
23796e 459   {
S 460   global $IMAP, $COMM_PATH;
3ea0e3 461
6d2714 462   $display = isset($_SESSION['quota_display']) ? $_SESSION['quota_display'] : '';
A 463
464   if (is_array($quota) && !empty($quota['used']) && !empty($quota['total']))
3ea0e3 465     {
6d2714 466       if (!isset($quota['percent']))
A 467         $quota['percent'] = $quota['used'] / $quota['total'];
468     }
469   elseif (!$IMAP->get_capability('QUOTA'))
470     return rcube_label('unknown');
471   else
472     $quota = $IMAP->get_quota();
473
474   if ($quota)
475     {
476     $quota_text = sprintf('%s / %s (%.0f%%)',
477                           show_bytes($quota['used'] * 1024),
478                           show_bytes($quota['total'] * 1024),
479                           $quota['percent']);
3ea0e3 480
T 481     // show quota as image (by Brett Patterson)
23796e 482     if ($display == 'image' && function_exists('imagegif'))
3ea0e3 483       {
23796e 484       $attrib = array('width' => 100, 'height' => 14);
f11541 485       $quota_text = sprintf('<img src="./bin/quotaimg.php?u=%s&amp;q=%d&amp;w=%d&amp;h=%d" width="%d" height="%d" alt="%s" title="%s / %s" />',
3ea0e3 486                             $quota['used'], $quota['total'],
fda695 487                             $attrib['width'], $attrib['height'],
T 488                             $attrib['width'], $attrib['height'],
489                             $quota_text,
490                             show_bytes($quota["used"] * 1024),
491                             show_bytes($quota["total"] * 1024));
3ea0e3 492       }
T 493     }
494   else
4647e1 495     $quota_text = rcube_label('unlimited');
58e360 496
23796e 497   return $quota_text;
58e360 498   }
T 499
4e17e6 500
45f56c 501 /**
T 502  *
503  */
4647e1 504 function rcmail_get_messagecount_text($count=NULL, $page=NULL)
4e17e6 505   {
T 506   global $IMAP, $MESSAGE;
507   
8fa58e 508   if (isset($MESSAGE->index))
4e17e6 509     {
T 510     return rcube_label(array('name' => 'messagenrof',
8fa58e 511                              'vars' => array('nr'  => $MESSAGE->index+1,
4647e1 512                                              'count' => $count!==NULL ? $count : $IMAP->messagecount())));
4e17e6 513     }
31b2ce 514
4647e1 515   if ($page===NULL)
T 516     $page = $IMAP->list_page;
517     
518   $start_msg = ($page-1) * $IMAP->page_size + 1;
519   $max = $count!==NULL ? $count : $IMAP->messagecount();
4e17e6 520
T 521   if ($max==0)
522     $out = rcube_label('mailboxempty');
523   else
524     $out = rcube_label(array('name' => 'messagesfromto',
525                               'vars' => array('from'  => $start_msg,
526                                               'to'    => min($max, $start_msg + $IMAP->page_size - 1),
527                                               'count' => $max)));
528
2bca6e 529   return Q($out);
4e17e6 530   }
T 531
532
45f56c 533 /**
65cc1c 534  * Convert the given message part to proper HTML
T 535  * which can be displayed the message view
45f56c 536  *
65cc1c 537  * @param object rcube_message_part Message part
T 538  * @param bool  True if external objects (ie. images ) are allowed
539  * @param bool  True if part should be converted to plaintext
540  * @return string Formatted HTML string
45f56c 541  */
21e724 542 function rcmail_print_body($part, $p = array())
45f56c 543 {
T 544   global $REMOTE_OBJECTS;
4e17e6 545   
21e724 546   $p += array('safe' => false, 'plain' => false, 'inline_html' => true);
T 547   
5cc4b1 548   // convert html to text/plain
21e724 549   if ($part->ctype_secondary == 'html' && $p['plain']) {
45f56c 550     $txt = new html2text($part->body, false, true);
5cc4b1 551     $body = $txt->get_text();
T 552     $part->ctype_secondary = 'plain';
45f56c 553   }
4e17e6 554   // text/html
45f56c 555   else if ($part->ctype_secondary == 'html') {
350459 556     // charset was converted to UTF-8 in rcube_imap::get_message_part() -> change charset specification in HTML accordingly
32b709 557     $html = $part->body; 
21e724 558     if (preg_match('/(\s+content=[\'"]\w+\/\w+;\s*charset)=([a-z0-9-]+)/i', $html)) 
T 559       $html = preg_replace('/(\s+content=[\'"]\w+\/\w+;\s*charset)=([a-z0-9-]+)/i', '\\1='.RCMAIL_CHARSET, $html); 
5f8686 560     else {
A 561       // add <head> for malformed messages, washtml cannot work without that
21e724 562       if (!preg_match('/<head>(.*)<\\/head>/Uims', $html))
5f8686 563         $html = '<head></head>' . $html;
32b709 564       $html = substr_replace($html, '<meta http-equiv="Content-Type" content="text/html; charset='.RCMAIL_CHARSET.'" />', intval(stripos($html, '</head>')), 0);
5f8686 565     }
c1b81f 566
A 567     // PHP bug #32547 workaround: remove title tag
568     $html = preg_replace('/<title>.*<\/title>/', '', $html);
569
45f56c 570     // clean HTML with washhtml by Frederic Motte
21e724 571     $wash_opts = array(
45f56c 572       'show_washed' => false,
21e724 573       'allow_remote' => $p['safe'],
45f56c 574       'blocked_src' => "./program/blocked.gif",
350459 575       'charset' => RCMAIL_CHARSET,
45f56c 576       'cid_map' => $part->replaces,
21e724 577       'html_elements' => array('body'),
T 578     );
579     
580     if (!$p['inline_html']) {
581       $wash_opts['html_elements'] = array('html','head','title','body');
582     }
583     
584     /* CSS styles need to be sanitized!
585     if ($p['safe']) {
586       $wash_opts['html_elements'][] = 'style';
587       $wash_opts['html_attribs'] = array('type');
588     }
589     */
590     
591     $washer = new washtml($wash_opts);
592     $washer->add_callback('form', 'rcmail_washtml_callback');
593     $body = $washer->wash($html);
594     $REMOTE_OBJECTS = $washer->extlinks;
4e17e6 595
45f56c 596     return $body;
T 597   }
4e17e6 598   // text/enriched
45f56c 599   else if ($part->ctype_secondary=='enriched') {
cfe4a6 600     $part->ctype_secondary = 'html';
9103d1 601     return Q(enriched_to_html($part->body), 'show');
45f56c 602   }
4e17e6 603   else
45f56c 604     $body = $part->body;
4e17e6 605
T 606
45f56c 607   /**** assert plaintext ****/
T 608
609   // make links and email-addresses clickable
610   $convert_patterns = $convert_replaces = $replace_strings = array();
611   
612   $url_chars = 'a-z0-9_\-\+\*\$\/&%=@#:;';
613   $url_chars_within = '\?\.~,!';
614
615   $convert_patterns[] = "/([\w]+):\/\/([a-z0-9\-\.]+[a-z]{2,4}([$url_chars$url_chars_within]*[$url_chars])?)/ie";
616   $convert_replaces[] = "rcmail_str_replacement('<a href=\"\\1://\\2\" target=\"_blank\">\\1://\\2</a>', \$replace_strings)";
617
618   $convert_patterns[] = "/([^\/:]|\s)(www\.)([a-z0-9\-]{2,}[a-z]{2,4}([$url_chars$url_chars_within]*[$url_chars])?)/ie";
619   $convert_replaces[] = "rcmail_str_replacement('\\1<a href=\"http://\\2\\3\" target=\"_blank\">\\2\\3</a>', \$replace_strings)";
620   
621   $convert_patterns[] = '/([a-z0-9][a-z0-9\-\.\+\_]*@[a-z0-9]([a-z0-9\-][.]?)*[a-z0-9]\\.[a-z]{2,5})/ie';
622   $convert_replaces[] = "rcmail_str_replacement('<a href=\"mailto:\\1\" onclick=\"return ".JS_OBJECT_NAME.".command(\'compose\',\'\\1\',this)\">\\1</a>', \$replace_strings)";
623   
379050 624 //    if ($part->ctype_parameters['format'] != 'flowed')
A 625 //      $body = wordwrap(trim($body), 80);
4e17e6 626
45f56c 627   // search for patterns like links and e-mail addresses
T 628   $body = preg_replace($convert_patterns, $convert_replaces, $body);
4e17e6 629
45f56c 630   // split body into single lines
T 631   $a_lines = preg_split('/\r?\n/', $body);
632   $quote_level = 0;
4e17e6 633
45f56c 634   // colorize quoted parts
T 635   for ($n=0; $n < sizeof($a_lines); $n++) {
636     $line = $a_lines[$n];
637     $quotation = '';
638     $q = 0;
4e17e6 639     
45f56c 640     if (preg_match('/^(>+\s*)+/', $line, $regs)) {
T 641       $q    = strlen(preg_replace('/\s/', '', $regs[0]));
642       $line = substr($line, strlen($regs[0]));
643
644       if ($q > $quote_level)
645         $quotation = str_repeat('<blockquote>', $q - $quote_level);
646       else if ($q < $quote_level)
647         $quotation = str_repeat("</blockquote>", $quote_level - $q);
4e17e6 648     }
45f56c 649     else if ($quote_level > 0)
T 650       $quotation = str_repeat("</blockquote>", $quote_level);
651
652     $quote_level = $q;
653     $a_lines[$n] = $quotation . Q($line, 'replace', false);  // htmlquote plaintext
654   }
655
656   // insert the links for urls and mailtos
657   $body = preg_replace("/##string_replacement\{([0-9]+)\}##/e", "\$replace_strings[\\1]", join("\n", $a_lines));
658   
659   return "<div class=\"pre\">".$body."\n</div>";
21e724 660 }
4e17e6 661
45f56c 662 /**
T 663  * add a string to the replacement array and return a replacement string
664  */
4e17e6 665 function rcmail_str_replacement($str, &$rep)
21e724 666 {
4e17e6 667   static $count = 0;
T 668   $rep[$count] = stripslashes($str);
669   return "##string_replacement{".($count++)."}##";
21e724 670 }
4e17e6 671
21e724 672
T 673 /**
674  * Callback function for washtml cleaning class
675  */
676 function rcmail_washtml_callback($tagname, $attrib, $content)
677 {
678   switch ($tagname) {
679     case 'form':
680       $out = html::div('form', $content);
681       break;
682       
683     default:
684       $out = '';
685   }
686   
687   return $out;
688 }
4e17e6 689
T 690
45f56c 691 /**
T 692  * return table with message headers
693  */
4e17e6 694 function rcmail_message_headers($attrib, $headers=NULL)
T 695   {
5b1de5 696   global $IMAP, $OUTPUT, $MESSAGE, $PRINT_MODE, $CONFIG;
4e17e6 697   static $sa_attrib;
T 698   
699   // keep header table attrib
700   if (is_array($attrib) && !$sa_attrib)
701     $sa_attrib = $attrib;
702   else if (!is_array($attrib) && is_array($sa_attrib))
703     $attrib = $sa_attrib;
704   
705   
706   if (!isset($MESSAGE))
707     return FALSE;
708
709   // get associative array of headers object
710   if (!$headers)
8fa58e 711     $headers = is_object($MESSAGE->headers) ? get_object_vars($MESSAGE->headers) : $MESSAGE->headers;
cfe4a6 712     
T 713   // add empty subject if none exsists
714   if (empty($headers['subject']))
715     $headers['subject'] = rcube_label('nosubject');
5b1de5 716
4e17e6 717   $header_count = 0;
T 718   
719   // allow the following attributes to be added to the <table> tag
720   $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border', 'summary'));
721   $out = '<table' . $attrib_str . ">\n";
722
723   // show these headers
bde645 724   $standard_headers = array('subject', 'from', 'organization', 'to', 'cc', 'bcc', 'reply-to', 'date');
4e17e6 725   
T 726   foreach ($standard_headers as $hkey)
727     {
728     if (!$headers[$hkey])
729       continue;
730
b076a4 731     if ($hkey=='date' && !empty($headers[$hkey]))
5b1de5 732       {
A 733       if ($PRINT_MODE)
734         $header_value = format_date($headers[$hkey], $CONFIG['date_long'] ? $CONFIG['date_long'] : 'x');
735       else
736         $header_value = format_date($headers[$hkey]);
737       }
bde645 738     else if (in_array($hkey, array('from', 'to', 'cc', 'bcc', 'reply-to')))
2bca6e 739       $header_value = Q(rcmail_address_string($headers[$hkey], NULL, $attrib['addicon']), 'show');
4e17e6 740     else
583850 741       $header_value = Q($IMAP->decode_header($headers[$hkey]));
4e17e6 742
T 743     $out .= "\n<tr>\n";
2bca6e 744     $out .= '<td class="header-title">'.Q(rcube_label($hkey)).":&nbsp;</td>\n";
4e17e6 745     $out .= '<td class="'.$hkey.'" width="90%">'.$header_value."</td>\n</tr>";
T 746     $header_count++;
747     }
748
749   $out .= "\n</table>\n\n";
750
751   return $header_count ? $out : '';  
752   }
753
754
45f56c 755 /**
21605c 756  * Handler for the 'messagebody' GUI object
45f56c 757  *
21605c 758  * @param array Named parameters
T 759  * @return string HTML content showing the message body
45f56c 760  */
4e17e6 761 function rcmail_message_body($attrib)
T 762   {
8fa58e 763   global $CONFIG, $OUTPUT, $MESSAGE, $IMAP, $REMOTE_OBJECTS;
5f8686 764
8fa58e 765   if (!is_array($MESSAGE->parts) && empty($MESSAGE->body))
4e17e6 766     return '';
T 767     
768   if (!$attrib['id'])
769     $attrib['id'] = 'rcmailMsgBody';
770
8fa58e 771   $safe_mode = $MESSAGE->is_safe || intval($_GET['_safe']);
21605c 772   $out = '';
4e17e6 773   
T 774   $header_attrib = array();
775   foreach ($attrib as $attr => $value)
776     if (preg_match('/^headertable([a-z]+)$/i', $attr, $regs))
777       $header_attrib[$regs[1]] = $value;
778
8fa58e 779   if (!empty($MESSAGE->parts))
4e17e6 780     {
8fa58e 781     foreach ($MESSAGE->parts as $i => $part)
4e17e6 782       {
8fa58e 783       if ($part->type == 'headers')
8d4bcd 784         $out .= rcmail_message_headers(sizeof($header_attrib) ? $header_attrib : NULL, $part->headers);
8fa58e 785       else if ($part->type == 'content')
4e17e6 786         {
8d4bcd 787         if (empty($part->ctype_parameters) || empty($part->ctype_parameters['charset']))
8fa58e 788           $part->ctype_parameters['charset'] = $MESSAGE->headers->charset;
a0109c 789
8d4bcd 790         // fetch part if not available
T 791         if (!isset($part->body))
8fa58e 792           $part->body = $MESSAGE->get_part_content($part->mime_id);
a0109c 793
21e724 794         $body = rcmail_print_body($part, array('safe' => $safe_mode, 'plain' => !$CONFIG['prefer_html']));
5f8686 795
cfe4a6 796         if ($part->ctype_secondary == 'html')
21605c 797           $out .= html::div('message-htmlpart', rcmail_html4inline($body, $attrib['id']));
a2f2c5 798         else
21605c 799           $out .= html::div('message-part', $body);
4e17e6 800         }
T 801       }
802     }
803   else
cfe4a6 804     $out .= html::div('message-part', html::div('pre', Q($MESSAGE->body)));
4e17e6 805
T 806
8fa58e 807   $ctype_primary = strtolower($MESSAGE->structure->ctype_primary);
T 808   $ctype_secondary = strtolower($MESSAGE->structure->ctype_secondary);
166b61 809
4e17e6 810   // list images after mail body
166b61 811   if (get_boolean($attrib['showimages']) 
2da368 812       && $CONFIG['inline_images']
T 813       && $ctype_primary == 'multipart'
814       && !empty($MESSAGE->attachments) 
815       && !strstr($message_body, '<html'))
166b61 816     {
8fa58e 817     foreach ($MESSAGE->attachments as $attach_prop) {
T 818       if (strpos($attach_prop->mimetype, 'image/') === 0) {
819         $out .= html::tag('hr') . html::p(array('align' => "center"),
820           html::img(array(
821             'src' => $MESSAGE->get_part_url($attach_prop->mime_id),
822             'title' => $attach_prop->filename,
823             'alt' => $attach_prop->filename,
824           )));
825         }
4e17e6 826     }
8fa58e 827   }
4e17e6 828   
T 829   // tell client that there are blocked remote objects
830   if ($REMOTE_OBJECTS && !$safe_mode)
f11541 831     $OUTPUT->set_env('blockedobjects', true);
4e17e6 832
21605c 833   return html::div($attrib, $out);
4e17e6 834   }
T 835
836
837
45f56c 838 /**
T 839  * modify a HTML message that it can be displayed inside a HTML page
840  */
841 function rcmail_html4inline($body, $container_id)
4e17e6 842   {
97bd2c 843   $base_url = "";
4e17e6 844   $last_style_pos = 0;
T 845   $body_lc = strtolower($body);
97bd2c 846   
T 847   // check for <base href>
848   if (preg_match(($base_reg = '/(<base.*href=["\']?)([hftps]{3,5}:\/{2}[^"\'\s]+)([^<]*>)/i'), $body, $base_regs))
849     $base_url = $base_regs[2];
4e17e6 850   
T 851   // find STYLE tags
852   while (($pos = strpos($body_lc, '<style', $last_style_pos)) && ($pos2 = strpos($body_lc, '</style>', $pos)))
853     {
ea206d 854     $pos = strpos($body_lc, '>', $pos)+1;
T 855
4e17e6 856     // replace all css definitions with #container [def]
97bd2c 857     $styles = rcmail_mod_css_styles(substr($body, $pos, $pos2-$pos), $container_id, $base_url);
ea206d 858
3b12ae 859     $body = substr($body, 0, $pos) . $styles . substr($body, $pos2);
S 860     $body_lc = strtolower($body);
4e17e6 861     $last_style_pos = $pos2;
5e98e1 862     }
4e17e6 863
T 864   // resolve <base href>
97bd2c 865   if ($base_url)
4e17e6 866     {
T 867     $body = preg_replace('/(src|background|href)=(["\']?)([\.\/]+[^"\'\s]+)(\2|\s|>)/Uie', "'\\1=\"'.make_absolute_url('\\3', '$base_url').'\"'", $body);
868     $body = preg_replace('/(url\s*\()(["\']?)([\.\/]+[^"\'\)\s]+)(\2)\)/Uie', "'\\1\''.make_absolute_url('\\3', '$base_url').'\')'", $body);
869     $body = preg_replace($base_reg, '', $body);
870     }
fe79b1 871     
T 872   // modify HTML links to open a new window if clicked
97bd2c 873   $body = preg_replace('/<(a|link)\s+([^>]+)>/Uie', "rcmail_alter_html_link('\\1','\\2', '$container_id');", $body);
4e17e6 874
T 875   // add comments arround html and other tags
06895c 876   $out = preg_replace(array(
d7a411 877       '/(<!DOCTYPE[^>]*>)/i',
A 878       '/(<\?xml[^>]*>)/i',
06895c 879       '/(<\/?html[^>]*>)/i',
T 880       '/(<\/?head[^>]*>)/i',
881       '/(<title[^>]*>.*<\/title>)/Ui',
882       '/(<\/?meta[^>]*>)/i'),
883     '<!--\\1-->',
884     $body);
a0109c 885
97bd2c 886   $out = preg_replace(
45f56c 887     array('/<body([^>]*)>/i', '/<\/body>/i'),
T 888     array('<div class="rcmBody"\\1>', '</div>'),
97bd2c 889     $out);
a0109c 890
86958f 891   // quote <? of php and xml files that are specified as text/html
T 892   $out = preg_replace(array('/<\?/', '/\?>/'), array('&lt;?', '?&gt;'), $out);
893
4e17e6 894   return $out;
T 895   }
896
897
45f56c 898 /**
T 899  * parse link attributes and set correct target
900  */
97bd2c 901 function rcmail_alter_html_link($tag, $attrs, $container_id)
fe79b1 902   {
97bd2c 903   $attrib = parse_attrib_string($attrs);
84f5b7 904
97bd2c 905   if ($tag == 'link' && preg_match('/^https?:\/\//i', $attrib['href']))
T 906     $attrib['href'] = "./bin/modcss.php?u=" . urlencode($attrib['href']) . "&amp;c=" . urlencode($container_id);
fe79b1 907
97bd2c 908   else if (stristr((string)$attrib['href'], 'mailto:'))
T 909     $attrib['onclick'] = sprintf(
910       "return %s.command('compose','%s',this)",
911       JS_OBJECT_NAME,
912       JQ(substr($attrib['href'], 7)));
84f5b7 913
fe79b1 914   else if (!empty($attrib['href']) && $attrib['href']{0}!='#')
T 915     $attrib['target'] = '_blank';
916
97bd2c 917   return "<$tag" . create_attrib_string($attrib, array('href','name','target','onclick','id','class','style','title','rel','type','media')) . ' />';
4e17e6 918   }
T 919
920
45f56c 921 /**
T 922  * decode address string and re-format it as HTML links
923  */
4e17e6 924 function rcmail_address_string($input, $max=NULL, $addicon=NULL)
T 925   {
f11541 926   global $IMAP, $PRINT_MODE, $CONFIG, $OUTPUT, $EMAIL_ADDRESS_PATTERN;
1088d6 927
4e17e6 928   $a_parts = $IMAP->decode_address_list($input);
T 929
930   if (!sizeof($a_parts))
931     return $input;
932
933   $c = count($a_parts);
934   $j = 0;
935   $out = '';
936
937   foreach ($a_parts as $part)
938     {
939     $j++;
940     if ($PRINT_MODE)
90cd45 941       $out .= sprintf('%s &lt;%s&gt;', Q($part['name']), $part['mailto']);
4e17e6 942     else if (preg_match($EMAIL_ADDRESS_PATTERN, $part['mailto']))
T 943       {
944       $out .= sprintf('<a href="mailto:%s" onclick="return %s.command(\'compose\',\'%s\',this)" class="rcmContactAddress" title="%s">%s</a>',
28bfe4 945                       Q($part['mailto']),
f11541 946                       JS_OBJECT_NAME,
28bfe4 947                       JQ($part['mailto']),
T 948                       Q($part['mailto']),
2bca6e 949                       Q($part['name']));
4e17e6 950                       
T 951       if ($addicon)
952         $out .= sprintf('&nbsp;<a href="#add" onclick="return %s.command(\'add-contact\',\'%s\',this)" title="%s"><img src="%s%s" alt="add" border="0" /></a>',
f11541 953                         JS_OBJECT_NAME,
4e17e6 954                         urlencode($part['string']),
T 955                         rcube_label('addtoaddressbook'),
956                         $CONFIG['skin_path'],
957                         $addicon);
958       }
959     else
960       {
961       if ($part['name'])
2bca6e 962         $out .= Q($part['name']);
4e17e6 963       if ($part['mailto'])
28bfe4 964         $out .= (strlen($out) ? ' ' : '') . sprintf('&lt;%s&gt;', Q($part['mailto']));
4e17e6 965       }
T 966       
967     if ($c>$j)
968       $out .= ','.($max ? '&nbsp;' : ' ');
969         
970     if ($max && $j==$max && $c>$j)
971       {
972       $out .= '...';
973       break;
974       }        
975     }
976     
977   return $out;
978   }
979
980
981 function rcmail_message_part_controls()
982   {
8fa58e 983   global $MESSAGE;
4e17e6 984   
d5342a 985   $part = asciiwords(get_input_value('_part', RCUBE_INPUT_GPC));
8fa58e 986   if (!is_object($MESSAGE) || !is_array($MESSAGE->parts) || !($_GET['_uid'] && $_GET['_part']) || !$MESSAGE->mime_parts[$part])
4e17e6 987     return '';
T 988     
8fa58e 989   $part = $MESSAGE->mime_parts[$part];
T 990   $table = new html_table(array('cols' => 3));
4e17e6 991   
8fa58e 992   if (!empty($part->filename)) {
T 993     $table->add('title', Q(rcube_label('filename')));
994     $table->add(null, Q($part->filename));
8dc048 995     $table->add(null, '[' . html::a('?'.str_replace('_frame=', '_download=', $_SERVER['QUERY_STRING']), Q(rcube_label('download'))) . ']');
8fa58e 996   }
4e17e6 997   
8fa58e 998   if (!empty($part->size)) {
T 999     $table->add('title', Q(rcube_label('filesize')));
1000     $table->add(null, Q(show_bytes($part->size)));
1001   }
4e17e6 1002   
8fa58e 1003   return $table->show($attrib);
4e17e6 1004   }
T 1005
1006
1007
1008 function rcmail_message_part_frame($attrib)
1009   {
1010   global $MESSAGE;
1011   
8fa58e 1012   $part = $MESSAGE->mime_parts[asciiwords(get_input_value('_part', RCUBE_INPUT_GPC))];
4e17e6 1013   $ctype_primary = strtolower($part->ctype_primary);
T 1014
719a25 1015   $attrib['src'] = Q('./?'.str_replace('_frame=', ($ctype_primary=='text' ? '_show=' : '_preload='), $_SERVER['QUERY_STRING']));
4e17e6 1016
T 1017   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'src', 'width', 'height'));
90022e 1018   $out = '<iframe '. $attrib_str . "></iframe>";
4e17e6 1019     
T 1020   return $out;
1021   }
1022
1023
45f56c 1024 /**
T 1025  * clear message composing settings
1026  */
4e17e6 1027 function rcmail_compose_cleanup()
T 1028   {
1029   if (!isset($_SESSION['compose']))
1030     return;
70d4b9 1031
4e17e6 1032   // remove attachment files from temp dir
T 1033   if (is_array($_SESSION['compose']['attachments']))
1034     foreach ($_SESSION['compose']['attachments'] as $attachment)
15a9d1 1035       @unlink($attachment['path']);
4e17e6 1036   
T 1037   unset($_SESSION['compose']);
1038   }
fba1f5 1039   
T 1040
1041 /**
1042  * Send the given message compose object using the configured method
1043  */
1044 function rcmail_deliver_message(&$message, $from, $mailto)
1045 {
1046   global $CONFIG;
1047
1048   $msg_body = $message->get();
8fa58e 1049   $headers = $message->headers();
fba1f5 1050   
T 1051   // send thru SMTP server using custom SMTP library
1052   if ($CONFIG['smtp_server'])
1053     {
1054     // generate list of recipients
1055     $a_recipients = array($mailto);
1056   
1057     if (strlen($headers['Cc']))
1058       $a_recipients[] = $headers['Cc'];
1059     if (strlen($headers['Bcc']))
1060       $a_recipients[] = $headers['Bcc'];
1061   
1062     // clean Bcc from header for recipients
1063     $send_headers = $headers;
1064     unset($send_headers['Bcc']);
7ec922 1065     // here too, it because txtHeaders() below use $message->_headers not only $send_headers
A 1066     unset($message->_headers['Bcc']);
fba1f5 1067
T 1068     // send message
1069     $smtp_response = array();
7ec922 1070     $sent = smtp_mail($from, $a_recipients, ($foo = $message->txtHeaders($send_headers, true)), $msg_body, $smtp_response);
fba1f5 1071
T 1072     // log error
1073     if (!$sent)
1074       raise_error(array('code' => 800, 'type' => 'smtp', 'line' => __LINE__, 'file' => __FILE__,
1075                         'message' => "SMTP error: ".join("\n", $smtp_response)), TRUE, FALSE);
1076     }
1077   
1078   // send mail using PHP's mail() function
1079   else
1080     {
1081     // unset some headers because they will be added by the mail() function
1082     $headers_enc = $message->headers($headers);
1083     $headers_php = $message->_headers;
1084     unset($headers_php['To'], $headers_php['Subject']);
1085     
1086     // reset stored headers and overwrite
1087     $message->_headers = array();
1088     $header_str = $message->txtHeaders($headers_php);
1089   
1090     if (ini_get('safe_mode'))
1091       $sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str);
1092     else
1093       $sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str, "-f$from");
1094     }
1095   
ae8f19 1096   if ($sent)  // remove MDN headers after sending
T 1097     unset($headers['Return-Receipt-To'], $headers['Disposition-Notification-To']);
fba1f5 1098   
T 1099   $message->_headers = array();
1100   $message->headers($headers);
1101   
1102   return $sent;
1103 }
f11541 1104
T 1105
0ea884 1106 function rcmail_send_mdn($uid)
T 1107 {
83a763 1108   global $RCMAIL, $IMAP;
8fa58e 1109
T 1110   $message = new rcube_message($uid);
0ea884 1111   
5b3dd4 1112   if ($message->headers->mdn_to && !$message->headers->mdn_sent && $IMAP->check_permflag('MDNSENT'))
0ea884 1113   {
83a763 1114     $identity = $RCMAIL->user->get_identity();
0ea884 1115     $sender = format_email_recipient($identity['email'], $identity['name']);
8fa58e 1116     $recipient = array_shift($IMAP->decode_address_list($message->headers->mdn_to));
0ea884 1117     $mailto = $recipient['mailto'];
T 1118
83a763 1119     $compose = new rcube_mail_mime($RCMAIL->config->header_delimiter());
0ea884 1120     $compose->setParam(array(
T 1121       'text_encoding' => 'quoted-printable',
1122       'html_encoding' => 'quoted-printable',
1123       'head_encoding' => 'quoted-printable',
1124       'head_charset'  => RCMAIL_CHARSET,
1125       'html_charset'  => RCMAIL_CHARSET,
1126       'text_charset'  => RCMAIL_CHARSET,
1127     ));
1128     
1129     // compose headers array
1130     $headers = array(
1131       'Date' => date('r'),
1132       'From' => $sender,
8fa58e 1133       'To'   => $message->headers->mdn_to,
T 1134       'Subject' => rcube_label('receiptread') . ': ' . $message->subject,
83a763 1135       'Message-ID' => sprintf('<%s@%s>', md5(uniqid('rcmail'.rand(),true)), $RCMAIL->config->mail_domain($_SESSION['imap_host'])),
0ea884 1136       'X-Sender' => $identity['email'],
T 1137       'Content-Type' => 'multipart/report; report-type=disposition-notification',
1138     );
1139     
83a763 1140     if ($agent = $RCMAIL->config->get('useragent'))
T 1141       $headers['User-Agent'] = $agent;
0ea884 1142
T 1143     $body = rcube_label("yourmessage") . "\r\n\r\n" .
8fa58e 1144       "\t" . rcube_label("to") . ': ' . rcube_imap::decode_mime_string($message->headers->to, $message->headers->charset) . "\r\n" .
T 1145       "\t" . rcube_label("subject") . ': ' . $message->subject . "\r\n" .
83a763 1146       "\t" . rcube_label("sent") . ': ' . format_date($message->headers->date, $RCMAIL->config->get('date_long')) . "\r\n" .
0ea884 1147       "\r\n" . rcube_label("receiptnote") . "\r\n";
T 1148     
83a763 1149     $ua = $RCMAIL->config->get('useragent', "RoundCube Webmail (Version ".RCMAIL_VERSION.")");
0ea884 1150     $report = "Reporting-UA: $ua\r\n";
T 1151     
8fa58e 1152     if ($message->headers->to)
T 1153         $report .= "Original-Recipient: {$message->headers->to}\r\n";
0ea884 1154     
T 1155     $report .= "Final-Recipient: rfc822; {$identity['email']}\r\n" .
8fa58e 1156                "Original-Message-ID: {$message->headers->messageID}\r\n" .
0ea884 1157                "Disposition: manual-action/MDN-sent-manually; displayed\r\n";
T 1158     
8fa58e 1159     $compose->headers($headers);
T 1160     $compose->setTXTBody(wordwrap($body, 75, "\r\n"));
0ea884 1161     $compose->addAttachment($report, 'message/disposition-notification', 'MDNPart2.txt', false, '7bit', 'inline');
T 1162
1163     $sent = rcmail_deliver_message($compose, $identity['email'], $mailto);
1164
1165     if ($sent)
1166     {
8fa58e 1167       $IMAP->set_flag($message->uid, 'MDNSENT');
0ea884 1168       return true;
T 1169     }
1170   }
1171   
1172   return false;
1173 }
1174
1175
f11541 1176 // register UI objects
T 1177 $OUTPUT->add_handlers(array(
1178   'mailboxlist' => 'rcmail_mailbox_list',
1179   'messages' => 'rcmail_message_list',
1180   'messagecountdisplay' => 'rcmail_messagecount_display',
1181   'quotadisplay' => 'rcmail_quota_display',
1182   'messageheaders' => 'rcmail_message_headers',
1183   'messagebody' => 'rcmail_message_body',
1184   'messagecontentframe' => 'rcmail_messagecontent_frame',
1185   'messagepartframe' => 'rcmail_message_part_frame',
1186   'messagepartcontrols' => 'rcmail_message_part_controls',
47124c 1187   'searchform' => array($OUTPUT, 'search_form'),
f11541 1188 ));
T 1189
93be5b 1190 ?>