thomascube
2010-12-17 db1a87cd6c506f2afbd1a37c64cb56ae11120b49
commit | author | age
4e17e6 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/mail/func.inc                                           |
6  |                                                                       |
e019f2 7  | This file is part of the Roundcube Webmail client                     |
A 8  | Copyright (C) 2005-2010, 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
7910c0 22 // setup some global vars used by mail steps
T 23 $SENT_MBOX = $RCMAIL->config->get('sent_mbox');
24 $DRAFTS_MBOX = $RCMAIL->config->get('drafts_mbox');
25 $SEARCH_MODS_DEFAULT = array('*' => array('subject'=>1, 'from'=>1), $SENT_MBOX => array('subject'=>1, 'to'=>1), $DRAFTS_MBOX => array('subject'=>1, 'to'=>1));
26
e99991 27 // Simplified for IDN in Unicode
A 28 //$EMAIL_ADDRESS_PATTERN = '([a-z0-9][a-z0-9\-\.\+\_]*@[a-z0-9][a-z0-9\-\.]*\\.[a-z]{2,5})';
1f49ce 29 $EMAIL_ADDRESS_PATTERN = '([a-z0-9][a-z0-9\-\.\+\_]*@[^&@"\'.][^@&"\']*\\.[a-z]{2,5})';
39cd51 30
431234 31 // actions that do not require imap connection here
A 32 $NOIMAP_ACTIONS = array('addcontact', 'autocomplete', 'upload', 'display-attachment', 'remove-attachment', 'get');
39cd51 33
47d8d3 34 // always instantiate imap object (but not yet connect to server)
T 35 $RCMAIL->imap_init();
36
39cd51 37 // log in to imap server
A 38 if (!in_array($RCMAIL->action, $NOIMAP_ACTIONS) && !$RCMAIL->imap_connect()) {
39   $RCMAIL->kill_session();
40
41   if ($OUTPUT->ajax_call)
42     $OUTPUT->redirect(array(), 2000);
43
44   $OUTPUT->set_env('task', 'login');
45   $OUTPUT->send('login');
46 }
47
4e17e6 48 // set imap properties and session vars
b72e2f 49 if (strlen(trim($mbox = get_input_value('_mbox', RCUBE_INPUT_GPC, true))))
c57996 50   $IMAP->set_mailbox(($_SESSION['mbox'] = $mbox));
48bc52 51 else if ($IMAP)
4e5b11 52   $_SESSION['mbox'] = $IMAP->get_mailbox_name();
4e17e6 53
b3ce79 54 if (!empty($_GET['_page']))
c57996 55   $IMAP->set_page(($_SESSION['page'] = intval($_GET['_page'])));
4e17e6 56
6a35c8 57 // set default sort col/order to session
T 58 if (!isset($_SESSION['sort_col']))
59   $_SESSION['sort_col'] = $CONFIG['message_sort_col'];
60 if (!isset($_SESSION['sort_order']))
61   $_SESSION['sort_order'] = $CONFIG['message_sort_order'];
2bca6e 62
f52c93 63 // set threads mode
T 64 $a_threading = $RCMAIL->config->get('message_threading', array());
65 if (isset($_GET['_threads'])) {
66   if ($_GET['_threads'])
67     $a_threading[$_SESSION['mbox']] = true;
68   else
69     unset($a_threading[$_SESSION['mbox']]);
70   $RCMAIL->user->save_prefs(array('message_threading' => $a_threading));
71 }
72 $IMAP->set_threading($a_threading[$_SESSION['mbox']]);
73
2bca6e 74 // set message set for search result
f6aac3 75 if (!empty($_REQUEST['_search']) && isset($_SESSION['search'])
A 76     && $_SESSION['search_request'] == $_REQUEST['_search']
77 ) {
78   $IMAP->set_search_set($_SESSION['search']);
1f020b 79   $OUTPUT->set_env('search_request', $_REQUEST['_search']);
S 80   $OUTPUT->set_env('search_text', $_SESSION['last_text_search']);
f6aac3 81 }
4e17e6 82
528514 83 // set main env variables, labels and page title
f6aac3 84 if (empty($RCMAIL->action) || $RCMAIL->action == 'list') {
8abda5 85   $mbox_name = $IMAP->get_mailbox_name();
A 86
f6aac3 87   if (empty($RCMAIL->action)) {
8abda5 88     // initialize searching result if search_filter is used
f6aac3 89     if ($_SESSION['search_filter'] && $_SESSION['search_filter'] != 'ALL') {
8abda5 90       $search_request = md5($mbox_name.$_SESSION['search_filter']);
9800a8 91
8abda5 92       $IMAP->search($mbox_name, $_SESSION['search_filter'], RCMAIL_CHARSET, $_SESSION['sort_col']);
f6aac3 93       $_SESSION['search'] = $IMAP->get_search_set();
A 94       $_SESSION['search_request'] = $search_request;
8abda5 95       $OUTPUT->set_env('search_request', $search_request);
A 96       }
9800a8 97
7910c0 98       $search_mods = $RCMAIL->config->get('search_mods', $SEARCH_MODS_DEFAULT);
T 99       $OUTPUT->set_env('search_mods', $search_mods);
f6aac3 100   }
9800a8 101
f52c93 102   // set current mailbox and some other vars in client environment
8abda5 103   $OUTPUT->set_env('mailbox', $mbox_name);
f52c93 104   $OUTPUT->set_env('pagesize', $IMAP->page_size);
528514 105   $OUTPUT->set_env('quota', $IMAP->get_capability('quota'));
A 106   $OUTPUT->set_env('delimiter', $IMAP->get_hierarchy_delimiter());
f52c93 107   $OUTPUT->set_env('threading', (bool) $IMAP->threading);
T 108   $OUTPUT->set_env('threads', $IMAP->threading
109     || $IMAP->get_capability('thread=references')
110         || $IMAP->get_capability('thread=orderedsubject')
111         || $IMAP->get_capability('thread=refs')  
112   );
528514 113
0b2ce9 114   if ($CONFIG['flag_for_deletion'])
A 115     $OUTPUT->set_env('flag_for_deletion', true);
116   if ($CONFIG['read_when_deleted'])
117     $OUTPUT->set_env('read_when_deleted', true);
118   if ($CONFIG['skip_deleted'])
119     $OUTPUT->set_env('skip_deleted', true);
e54bb7 120   if ($CONFIG['display_next'])
A 121     $OUTPUT->set_env('display_next', true);
bc4960 122
T 123   $OUTPUT->set_env('preview_pane_mark_read', $RCMAIL->config->get('preview_pane_mark_read', 0));
124
528514 125   if ($CONFIG['trash_mbox'])
A 126     $OUTPUT->set_env('trash_mailbox', $CONFIG['trash_mbox']);
127   if ($CONFIG['drafts_mbox'])
128     $OUTPUT->set_env('drafts_mailbox', $CONFIG['drafts_mbox']);
129   if ($CONFIG['junk_mbox'])
130     $OUTPUT->set_env('junk_mailbox', $CONFIG['junk_mbox']);
131
132   if (!$OUTPUT->ajax_call)
9b3fdc 133     $OUTPUT->add_label('checkingmail', 'deletemessage', 'movemessagetotrash',
c50d88 134       'movingmessage', 'copyingmessage', 'deletingmessage', 'markingmessage',
A 135       'copy', 'move', 'quota');
528514 136
4e5b11 137   $OUTPUT->set_pagetitle(rcmail_localize_foldername($mbox_name));
f6aac3 138 }
5eee00 139
4e17e6 140
45f56c 141 /**
T 142  * return the message list as HTML table
143  */
4e17e6 144 function rcmail_message_list($attrib)
f52c93 145 {
T 146   global $IMAP, $CONFIG, $OUTPUT;
b076a4 147
24053e 148   // add some labels to client
112c91 149   $OUTPUT->add_label('from', 'to');
4e17e6 150
T 151   // add id to message list table if not specified
152   if (!strlen($attrib['id']))
153     $attrib['id'] = 'rcubemessagelist';
e0ddd4 154
d59aaa 155   // define list of cols to be displayed based on parameter or config
b62c48 156   if (empty($attrib['columns'])) {
A 157     $a_show_cols = is_array($CONFIG['list_cols']) ? $CONFIG['list_cols'] : array('subject');
158     $OUTPUT->set_env('col_movable', !in_array('list_cols', (array)$CONFIG['dont_override']));
159   }
160   else {
161     $a_show_cols = preg_split('/[\s,;]+/', strip_quotes($attrib['columns']));
162     $attrib['columns'] = $a_show_cols;
163   }
d59aaa 164
f52c93 165   // save some variables for use in ajax list
T 166   $_SESSION['list_attrib'] = $attrib;
9800a8 167
41bece 168   $mbox = $IMAP->get_mailbox_name();
4906eb 169   $delim = $IMAP->get_hierarchy_delimiter();
A 170
171   // show 'to' instead of 'from' in sent/draft messages
172   if ((strpos($mbox.$delim, $CONFIG['sent_mbox'].$delim)===0 || strpos($mbox.$delim, $CONFIG['drafts_mbox'].$delim)===0)
ffaea6 173       && (($f = array_search('from', $a_show_cols)) !== false) && array_search('to', $a_show_cols) === false)
4e17e6 174     $a_show_cols[$f] = 'to';
e0ddd4 175
614c64 176   // make sure 'threads' and 'subject' columns are present
A 177   if (!in_array('subject', $a_show_cols))
178     array_unshift($a_show_cols, 'subject');
6c9d49 179   if (!in_array('threads', $a_show_cols))
A 180     array_unshift($a_show_cols, 'threads');
181
f52c93 182   $skin_path = $_SESSION['skin_path'] = $CONFIG['skin_path'];
9800a8 183
4e17e6 184   // set client env
f11541 185   $OUTPUT->add_gui_object('messagelist', $attrib['id']);
f52c93 186   $OUTPUT->set_env('autoexpand_threads', intval($CONFIG['autoexpand_threads']));
T 187   $OUTPUT->set_env('sort_col', $_SESSION['sort_col']);
188   $OUTPUT->set_env('sort_order', $_SESSION['sort_order']);
189   $OUTPUT->set_env('messages', array());
d24d20 190   $OUTPUT->set_env('coltypes', $a_show_cols);
9800a8 191
6b47de 192   $OUTPUT->include_script('list.js');
9800a8 193
f52c93 194   $thead = '';
T 195   foreach (rcmail_message_list_head($attrib, $a_show_cols) as $cell)
196     $thead .= html::tag('td', array('class' => $cell['className'], 'id' => $cell['id']), $cell['html']);
9800a8 197
f52c93 198   return html::tag('table',
T 199     $attrib,
200     html::tag('thead', null, html::tag('tr', null, $thead)) .
201       html::tag('tbody', null, ''),
b62c48 202         array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border', 'summary'));
f52c93 203 }
4e17e6 204
T 205
45f56c 206 /**
T 207  * return javascript commands to add rows to the message list
208  */
614c64 209 function rcmail_js_message_list($a_headers, $insert_top=FALSE, $a_show_cols=null)
f52c93 210 {
5bde17 211   global $CONFIG, $IMAP, $RCMAIL, $OUTPUT;
4e17e6 212
614c64 213   if (empty($a_show_cols)) {
A 214     if (!empty($_SESSION['list_attrib']['columns']))
215       $a_show_cols = $_SESSION['list_attrib']['columns'];
216     else
217       $a_show_cols = is_array($CONFIG['list_cols']) ? $CONFIG['list_cols'] : array('subject');
218   }
219   else {
220     if (!is_array($a_show_cols))
221       $a_show_cols = preg_split('/[\s,;]+/', strip_quotes($a_show_cols));
222     $head_replace = true;
223   }
d59aaa 224
41bece 225   $mbox = $IMAP->get_mailbox_name();
4906eb 226   $delim = $IMAP->get_hierarchy_delimiter();
614c64 227
A 228   // make sure 'threads' and 'subject' columns are present
229   if (!in_array('subject', $a_show_cols))
230     array_unshift($a_show_cols, 'subject');
231   if (!in_array('threads', $a_show_cols))
232     array_unshift($a_show_cols, 'threads');
233
234   $_SESSION['list_attrib']['columns'] = $a_show_cols;
9800a8 235
4906eb 236   // show 'to' instead of 'from' in sent/draft messages
A 237   if ((strpos($mbox.$delim, $CONFIG['sent_mbox'].$delim)===0 || strpos($mbox.$delim, $CONFIG['drafts_mbox'].$delim)===0)
f11541 238       && (($f = array_search('from', $a_show_cols)) !== false) && array_search('to', $a_show_cols) === false)
4e17e6 239     $a_show_cols[$f] = 'to';
T 240
614c64 241   // Make sure there are no duplicated columns (#1486999)
A 242   $a_show_cols = array_unique($a_show_cols);
6c9d49 243
5bde17 244   // Plugins may set header's list_cols/list_flags and other rcube_mail_header variables
A 245   // and list columns
246   $plugin = $RCMAIL->plugins->exec_hook('messages_list',
247     array('messages' => $a_headers, 'cols' => $a_show_cols));
248
249   $a_show_cols = $plugin['cols'];
250   $a_headers   = $plugin['messages'];
251
f52c93 252   $thead = $head_replace ? rcmail_message_list_head($_SESSION['list_attrib'], $a_show_cols) : NULL;
9800a8 253
f52c93 254   $OUTPUT->command('set_message_coltypes', $a_show_cols, $thead);
c4b819 255
f52c93 256   if (empty($a_headers))
T 257     return;
91354e 258
4438d6 259   // remove 'threads', 'attachment', 'flag', 'status' columns, we don't need them here
A 260   foreach (array('threads', 'attachment', 'flag', 'status') as $col) {
261     if (($key = array_search($col, $a_show_cols)) !== FALSE)
262       unset($a_show_cols[$key]);
263   }
5bde17 264
4438d6 265   // loop through message headers
A 266   foreach ($a_headers as $n => $header) {
ecd2e7 267     if (empty($header))
T 268       continue;
5bde17 269
A 270     $a_msg_cols = array();
271     $a_msg_flags = array();
f11541 272
5faac0 273     $IMAP->set_charset(!empty($header->charset) ? $header->charset : $CONFIG['default_charset']);
d59aaa 274
4e17e6 275     // format each col; similar as in rcmail_message_list()
4438d6 276     foreach ($a_show_cols as $col) {
5cef5b 277       if (in_array($col, array('from', 'to', 'cc', 'replyto')))
583850 278         $cont = Q(rcmail_address_string($header->$col, 3), 'show');
4438d6 279       else if ($col=='subject') {
f52c93 280         $cont = abbreviate_string(trim($IMAP->decode_header($header->$col)), 160);
44385f 281         if (!$cont) $cont = rcube_label('nosubject');
f52c93 282         $cont = Q($cont);
4438d6 283       }
4e17e6 284       else if ($col=='size')
T 285         $cont = show_bytes($header->$col);
286       else if ($col=='date')
f11541 287         $cont = format_date($header->date);
4e17e6 288       else
2bca6e 289         $cont = Q($header->$col);
9800a8 290
4e17e6 291       $a_msg_cols[$col] = $cont;
4438d6 292     }
4e17e6 293
f52c93 294     if ($header->depth)
T 295       $a_msg_flags['depth'] = $header->depth;
0e7b66 296     else if ($header->has_children)
A 297       $roots[] = $header->uid;
f52c93 298     if ($header->parent_uid)
T 299       $a_msg_flags['parent_uid'] = $header->parent_uid;
300     if ($header->has_children)
301       $a_msg_flags['has_children'] = $header->has_children;
302     if ($header->unread_children)
303       $a_msg_flags['unread_children'] = $header->unread_children;
c4b819 304     if ($header->deleted)
A 305       $a_msg_flags['deleted'] = 1;
306     if (!$header->seen)
307       $a_msg_flags['unread'] = 1;
308     if ($header->answered)
309       $a_msg_flags['replied'] = 1;
310     if ($header->forwarded)
311       $a_msg_flags['forwarded'] = 1;
312     if ($header->flagged)
313       $a_msg_flags['flagged'] = 1;
e25a35 314     if ($header->others['list-post'])
A 315       $a_msg_flags['ml'] = 1;
6b4929 316
A 317     $a_msg_flags['ctype'] = Q($header->ctype);
f52c93 318     $a_msg_flags['mbox'] = $mbox;
T 319
5bde17 320     // merge with plugin result
A 321     if (!empty($header->list_flags) && is_array($header->list_flags))
322       $a_msg_flags = array_merge($a_msg_flags, $header->list_flags);
323     if (!empty($header->list_cols) && is_array($header->list_cols))
324       $a_msg_cols = array_merge($a_msg_cols, $header->list_cols);
325
f11541 326     $OUTPUT->command('add_message_row',
T 327       $header->uid,
328       $a_msg_cols,
329       $a_msg_flags,
330       $insert_top);
4438d6 331   }
0e7b66 332
4438d6 333   if ($IMAP->threading) {
A 334     $OUTPUT->command('init_threads', (array) $roots);
335   }
b62c48 336 }
f52c93 337
T 338
339 /*
340  * Creates <THEAD> for message list table
341  */
342 function rcmail_message_list_head($attrib, $a_show_cols)
343 {
344   global $CONFIG;
345
346   $skin_path = $_SESSION['skin_path'];
347   $image_tag = html::img(array('src' => "%s%s", 'alt' => "%s"));
348
349   // check to see if we have some settings for sorting
350   $sort_col   = $_SESSION['sort_col'];
351   $sort_order = $_SESSION['sort_order'];
352
353   // define sortable columns
354   $a_sort_cols = array('subject', 'date', 'from', 'to', 'size', 'cc');
9800a8 355
1716d5 356   if (!empty($attrib['optionsmenuicon'])) {
A 357     $onclick = 'return ' . JS_OBJECT_NAME . ".command('menu-open', 'messagelistmenu')";
358     if ($attrib['optionsmenuicon'] === true || $attrib['optionsmenuicon'] == 'true')
359       $list_menu = html::div(array('onclick' => $onclick, 'class' => 'listmenu',
360         'id' => 'listmenulink', 'title' => rcube_label('listoptions')));
361     else
362       $list_menu = html::a(array('href' => '#', 'onclick' => $onclick),
363         html::img(array('src' => $skin_path . $attrib['optionsmenuicon'],
364           'id' => 'listmenulink', 'title' => rcube_label('listoptions')))
365       );
366   }
f52c93 367   else
T 368     $list_menu = '';
369
6c9d49 370   $cells = array();
f52c93 371
T 372   foreach ($a_show_cols as $col) {
373     // get column name
374     switch ($col) {
375       case 'flag':
e94706 376         $col_name = '<span class="flagged">&nbsp;</span>';
f52c93 377         break;
T 378       case 'attachment':
4438d6 379       case 'status':
A 380         $col_name = '<span class="' . $col .'">&nbsp;</span>';
f52c93 381         break;
6c9d49 382       case 'threads':
A 383         $col_name = $list_menu;
384         break;
f52c93 385       default:
T 386         $col_name = Q(rcube_label($col));
387     }
388
389     // make sort links
390     if (in_array($col, $a_sort_cols))
391       $col_name = html::a(array('href'=>"./#sort", 'onclick' => 'return '.JS_OBJECT_NAME.".command('sort','".$col."',this)", 'title' => rcube_label('sortby')), $col_name);
392
393     $sort_class = $col == $sort_col ? " sorted$sort_order" : '';
e94706 394     $class_name = $col.$sort_class;
f52c93 395
T 396     // put it all together
397     $cells[] = array('className' => $class_name, 'id' => "rcm$col", 'html' => $col_name);
398   }
399
400   return $cells;
401 }
4e17e6 402
T 403
45f56c 404 /**
T 405  * return an HTML iframe for loading mail content
406  */
b19097 407 function rcmail_messagecontent_frame($attrib)
T 408   {
ce06d3 409   global $OUTPUT, $RCMAIL;
9800a8 410
b19097 411   if (empty($attrib['id']))
T 412     $attrib['id'] = 'rcmailcontentwindow';
413
e2c610 414   $attrib['name'] = $attrib['id'];
b19097 415
ce06d3 416   if ($RCMAIL->config->get('preview_pane'))
A 417     $OUTPUT->set_env('contentframe', $attrib['id']);
f11541 418   $OUTPUT->set_env('blankpage', $attrib['src'] ? $OUTPUT->abs_url($attrib['src']) : 'program/blank.gif');
b19097 419
95fcc3 420   return html::iframe($attrib);
b19097 421   }
T 422
4e17e6 423
T 424 function rcmail_messagecount_display($attrib)
425   {
29b397 426   global $RCMAIL;
9800a8 427
4e17e6 428   if (!$attrib['id'])
T 429     $attrib['id'] = 'rcmcountdisplay';
430
29b397 431   $RCMAIL->output->add_gui_object('countdisplay', $attrib['id']);
4e17e6 432
29b397 433   $content =  $RCMAIL->action != 'show' ? rcmail_get_messagecount_text() : rcube_label('loading');
A 434
435   return html::span($attrib, $content);
4e17e6 436   }
T 437
438
4647e1 439 function rcmail_get_messagecount_text($count=NULL, $page=NULL)
4e17e6 440   {
29b397 441   global $RCMAIL, $IMAP;
31b2ce 442
4647e1 443   if ($page===NULL)
T 444     $page = $IMAP->list_page;
9800a8 445
4647e1 446   $start_msg = ($page-1) * $IMAP->page_size + 1;
9800a8 447
A 448   if ($count!==NULL)
449     $max = $count;
450   else if ($RCMAIL->action)
451     $max = $IMAP->messagecount(NULL, $IMAP->threading ? 'THREADS' : 'ALL');
4e17e6 452
T 453   if ($max==0)
454     $out = rcube_label('mailboxempty');
455   else
f52c93 456     $out = rcube_label(array('name' => $IMAP->threading ? 'threadsfromto' : 'messagesfromto',
T 457             'vars' => array('from'  => $start_msg,
458             'to'    => min($max, $start_msg + $IMAP->page_size - 1),
459             'count' => $max)));
4e17e6 460
2bca6e 461   return Q($out);
4e17e6 462   }
T 463
cbeea3 464
ac5d15 465 function rcmail_mailbox_name_display($attrib)
T 466 {
cbeea3 467   global $RCMAIL;
ac5d15 468
cbeea3 469   if (!$attrib['id'])
A 470     $attrib['id'] = 'rcmmailboxname';
ac5d15 471
cbeea3 472   $RCMAIL->output->add_gui_object('mailboxname', $attrib['id']);
ac5d15 473
cbeea3 474   return html::span($attrib, rcmail_get_mailbox_name_text());
ac5d15 475 }
T 476
f96ffd 477
ac5d15 478 function rcmail_get_mailbox_name_text()
T 479 {
cbeea3 480   global $RCMAIL;
A 481   return rcmail_localize_foldername($RCMAIL->imap->get_mailbox_name());
ac5d15 482 }
T 483
cbeea3 484
2144f9 485 function rcmail_send_unread_count($mbox_name, $force=false, $count=null)
cbeea3 486 {
A 487   global $RCMAIL;
9800a8 488
db1a87 489   $old_unseen = rcmail_get_unseen_count($mbox_name);
2144f9 490
A 491   if ($count === null)
492     $unseen = $RCMAIL->imap->messagecount($mbox_name, 'UNSEEN', $force);
493   else
494     $unseen = $count;
cbeea3 495
7d1db8 496   if ($unseen != $old_unseen || ($mbox_name == 'INBOX'))
cbeea3 497     $RCMAIL->output->command('set_unread_count', $mbox_name, $unseen, ($mbox_name == 'INBOX'));
A 498
db1a87 499   rcmail_set_unseen_count($mbox_name, $unseen);
9800a8 500
cbeea3 501   return $unseen;
db1a87 502 }
T 503
504
505 function rcmail_set_unseen_count($mbox_name, $count)
506 {
507   // @TODO: this data is doubled (session and cache tables) if caching is enabled
508
509   // Make sure we have an array here (#1487066)
510   if (!is_array($_SESSION['unseen_count']))
511     $_SESSION['unseen_count'] = array();
512
513   $_SESSION['unseen_count'][$mbox_name] = $count;
514 }
515
516
517 function rcmail_get_unseen_count($mbox_name)
518 {
519   if (is_array($_SESSION['unseen_count']) && array_key_exists($mbox_name, $_SESSION['unseen_count']))
520     return $_SESSION['unseen_count'][$mbox_name];
521   else
522     return null;
cbeea3 523 }
f96ffd 524
cbeea3 525
ec603f 526 /**
A 527  * Sets message is_safe flag according to 'show_images' option value
528  *
529  * @param object rcube_message Message
530  */
531 function rcmail_check_safe(&$message)
532 {
533   global $RCMAIL;
534
535   $show_images = $RCMAIL->config->get('show_images');
536   if (!$message->is_safe
537     && !empty($show_images)
538     && $message->has_html_part())
539   {
540     switch($show_images) {
541       case '1': // known senders only
8a78a1 542         $CONTACTS = new rcube_contacts($RCMAIL->db, $_SESSION['user_id']);
ec603f 543         if ($CONTACTS->search('email', $message->sender['mailto'], true, false)->count) {
A 544           $message->set_safe(true);
545         }
546       break;
547       case '2': // always
548         $message->set_safe(true);
549       break;
550     }
551   }
552 }
f96ffd 553
ec603f 554
A 555 /**
556  * Cleans up the given message HTML Body (for displaying)
557  *
558  * @param string HTML
559  * @param array  Display parameters 
560  * @param array  CID map replaces (inline images)
561  * @return string Clean HTML
562  */
563 function rcmail_wash_html($html, $p = array(), $cid_replaces)
564 {
565   global $REMOTE_OBJECTS;
69a7d3 566
ec603f 567   $p += array('safe' => false, 'inline_html' => true);
2337a8 568
ec603f 569   // special replacements (not properly handled by washtml class)
A 570   $html_search = array(
571     '/(<\/nobr>)(\s+)(<nobr>)/i',    // space(s) between <NOBR>
69a7d3 572     '/<title[^>]*>.*<\/title>/i',    // PHP bug #32547 workaround: remove title tag
3bde30 573     '/^(\0\0\xFE\xFF|\xFF\xFE\0\0|\xFE\xFF|\xFF\xFE|\xEF\xBB\xBF)/',    // byte-order mark (only outlook?)
4f7aa8 574     '/<html\s[^>]+>/i',            // washtml/DOMDocument cannot handle xml namespaces
ec603f 575   );
A 576   $html_replace = array(
577     '\\1'.' &nbsp; '.'\\3',
578     '',
579     '',
6c2d7e 580     '<html>',
ec603f 581   );
A 582   $html = preg_replace($html_search, $html_replace, $html);
2337a8 583
b9ec2b 584   // PCRE errors handling (#1486856), should we use something like for every preg_* use?
A 585   if ($html === null && ($preg_error = preg_last_error()) != PREG_NO_ERROR) {
586     $errstr = "Could not clean up HTML message! PCRE Error: $preg_error.";
587
588     if ($preg_error == PREG_BACKTRACK_LIMIT_ERROR)
589       $errstr .= " Consider raising pcre.backtrack_limit!";
590     if ($preg_error == PREG_RECURSION_LIMIT_ERROR)
591       $errstr .= " Consider raising pcre.recursion_limit!";
592
593     raise_error(array('code' => 600, 'type' => 'php',
594         'line' => __LINE__, 'file' => __FILE__,
595         'message' => $errstr), true, false);
596     return '';
597   }
598
2337a8 599   // fix (unknown/malformed) HTML tags before "wash"
b6673c 600   $html = preg_replace_callback('/(<[\/]*)([^\s>]+)/', 'rcmail_html_tag_callback', $html);
ec603f 601
a80b7d 602   // charset was converted to UTF-8 in rcube_imap::get_message_part(),
269fb8 603   // -> change charset specification in HTML accordingly
66c674 604   $charset_pattern = '(<meta\s+[^>]*content=)[\'"]?(\w+\/\w+;\s*charset=)([a-z0-9-_]+[\'"]?)';
f4bf20 605   if (preg_match("/$charset_pattern/Ui", $html)) {
69a7d3 606     $html = preg_replace("/$charset_pattern/i", '\\1"\\2'.RCMAIL_CHARSET.'"', $html);
ec603f 607   }
269fb8 608   else {
A 609     // add meta content-type to malformed messages, washtml cannot work without that
610     if (!preg_match('/<head[^>]*>(.*)<\/head>/Uims', $html))
611       $html = '<head></head>'. $html;
612     $html = substr_replace($html, '<meta http-equiv="Content-Type" content="text/html; charset='.RCMAIL_CHARSET.'" />', intval(stripos($html, '<head>')+6), 0);
613   }
ec603f 614   // turn relative into absolute urls
A 615   $html = rcmail_resolve_base($html);
616
617   // clean HTML with washhtml by Frederic Motte
618   $wash_opts = array(
619     'show_washed' => false,
620     'allow_remote' => $p['safe'],
621     'blocked_src' => "./program/blocked.gif",
622     'charset' => RCMAIL_CHARSET,
623     'cid_map' => $cid_replaces,
624     'html_elements' => array('body'),
625   );
ce4673 626
ec603f 627   if (!$p['inline_html']) {
A 628     $wash_opts['html_elements'] = array('html','head','title','body');
629   }
630   if ($p['safe']) {
631     $wash_opts['html_elements'][] = 'link';
632     $wash_opts['html_attribs'] = array('rel','type');
633   }
9800a8 634
33da0b 635   // overwrite washer options with options from plugins
A 636   if (isset($p['html_elements']))
637     $wash_opts['html_elements'] = $p['html_elements'];
638   if (isset($p['html_attribs']))
639     $wash_opts['html_attribs'] = $p['html_attribs'];
640
641   // initialize HTML washer
ec603f 642   $washer = new washtml($wash_opts);
33da0b 643
A 644   if (!$p['skip_washer_form_callback'])
645     $washer->add_callback('form', 'rcmail_washtml_callback');
ec603f 646
2337a8 647   // allow CSS styles, will be sanitized by rcmail_washtml_callback()
33da0b 648   if (!$p['skip_washer_style_callback'])
A 649     $washer->add_callback('style', 'rcmail_washtml_callback');
bf1b66 650
ec603f 651   $html = $washer->wash($html);
A 652   $REMOTE_OBJECTS = $washer->extlinks;
5b3ed5 653
ec603f 654   return $html;
A 655 }
656
4e17e6 657
45f56c 658 /**
65cc1c 659  * Convert the given message part to proper HTML
T 660  * which can be displayed the message view
45f56c 661  *
65cc1c 662  * @param object rcube_message_part Message part
ec603f 663  * @param array  Display parameters array 
65cc1c 664  * @return string Formatted HTML string
45f56c 665  */
21e724 666 function rcmail_print_body($part, $p = array())
45f56c 667 {
cc97ea 668   global $RCMAIL;
9800a8 669
cc97ea 670   // trigger plugin hook
T 671   $data = $RCMAIL->plugins->exec_hook('message_part_before',
672     array('type' => $part->ctype_secondary, 'body' => $part->body) + $p + array('safe' => false, 'plain' => false, 'inline_html' => true));
ec603f 673
5cc4b1 674   // convert html to text/plain
cc97ea 675   if ($data['type'] == 'html' && $data['plain']) {
T 676     $txt = new html2text($data['body'], false, true);
5cc4b1 677     $body = $txt->get_text();
T 678     $part->ctype_secondary = 'plain';
45f56c 679   }
4e17e6 680   // text/html
cc97ea 681   else if ($data['type'] == 'html') {
T 682     $body = rcmail_wash_html($data['body'], $data, $part->replaces);
683     $part->ctype_secondary = $data['type'];
45f56c 684   }
4e17e6 685   // text/enriched
cc97ea 686   else if ($data['type'] == 'enriched') {
cfe4a6 687     $part->ctype_secondary = 'html';
ec603f 688     require_once('lib/enriched.inc');
cc97ea 689     $body = Q(enriched_to_html($data['body']), 'show');
45f56c 690   }
cc97ea 691   else {
T 692     // assert plaintext
45f56c 693     $body = $part->body;
cc97ea 694     $part->ctype_secondary = $data['type'] = 'plain';
45f56c 695   }
9800a8 696
cc97ea 697   // free some memory (hopefully)
T 698   unset($data['body']);
45f56c 699
cc97ea 700   // plaintext postprocessing
4f6932 701   if ($part->ctype_secondary == 'plain')
99b8c1 702     $body = rcmail_plain_body($body, $part->ctype_parameters['format'] == 'flowed');
88ed23 703
cc97ea 704   // allow post-processing of the message body
T 705   $data = $RCMAIL->plugins->exec_hook('message_part_after', array('type' => $part->ctype_secondary, 'body' => $body) + $data);
706
707   return $data['type'] == 'html' ? $data['body'] : html::tag('pre', array(), $data['body']);
4f6932 708 }
A 709
f96ffd 710
4f6932 711 /**
A 712  * Handle links and citation marks in plain text message
713  *
99b8c1 714  * @param string  Plain text string
A 715  * @param boolean Text uses format=flowed
716  *
4f6932 717  * @return string Formatted HTML string
A 718  */
99b8c1 719 function rcmail_plain_body($body, $flowed=false)
4f6932 720 {
33dfdd 721   global $RCMAIL;
A 722
4f6932 723   // make links and email-addresses clickable
99b8c1 724   $replacer = new rcube_string_replacer;
9800a8 725
4f6932 726   // search for patterns like links and e-mail addresses
99b8c1 727   $body = preg_replace_callback($replacer->link_pattern, array($replacer, 'link_callback'), $body);
A 728   $body = preg_replace_callback($replacer->mailto_pattern, array($replacer, 'mailto_callback'), $body);
4f6932 729
A 730   // split body into single lines
731   $a_lines = preg_split('/\r?\n/', $body);
732   $quote_level = 0;
99b8c1 733   $last = -1;
4f6932 734
A 735   // find/mark quoted lines...
736   for ($n=0, $cnt=count($a_lines); $n < $cnt; $n++) {
737     if ($a_lines[$n][0] == '>' && preg_match('/^(>+\s*)+/', $a_lines[$n], $regs)) {
738       $q = strlen(preg_replace('/\s/', '', $regs[0]));
99b8c1 739       $a_lines[$n] = substr($a_lines[$n], strlen($regs[0]));
4f6932 740
A 741       if ($q > $quote_level)
99b8c1 742         $a_lines[$n] = $replacer->get_replacement($replacer->add(
A 743           str_repeat('<blockquote>', $q - $quote_level))) . $a_lines[$n];
4f6932 744       else if ($q < $quote_level)
99b8c1 745         $a_lines[$n] = $replacer->get_replacement($replacer->add(
A 746           str_repeat('</blockquote>', $quote_level - $q))) . $a_lines[$n];
747       else if ($flowed) {
748         // previous line is flowed
33dfdd 749         if (isset($a_lines[$last]) && $a_lines[$n]
99b8c1 750           && $a_lines[$last][strlen($a_lines[$last])-1] == ' ') {
33dfdd 751           // merge lines
99b8c1 752           $a_lines[$last] .= $a_lines[$n];
A 753           unset($a_lines[$n]);
754         }
755         else
756           $last = $n;
757       }
4f6932 758     }
99b8c1 759     else {
A 760       $q = 0;
761       if ($flowed) {
762         // sig separator - line is fixed
763         if ($a_lines[$n] == '-- ') {
764           $last = $n;
765         }
766         else {
767           // remove space-stuffing
768           if ($a_lines[$n][0] == ' ')
769             $a_lines[$n] = substr($a_lines[$n], 1);
770
771           // previous line is flowed?
33dfdd 772           if (isset($a_lines[$last]) && $a_lines[$n]
99b8c1 773             && $a_lines[$last] != '-- '
A 774             && $a_lines[$last][strlen($a_lines[$last])-1] == ' '
775           ) {
776             $a_lines[$last] .= $a_lines[$n];
777             unset($a_lines[$n]);
778           }
779           else {
780             $last = $n;
781           }
782         }
783         if ($quote_level > 0)
784           $a_lines[$last] = $replacer->get_replacement($replacer->add(
785             str_repeat('</blockquote>', $quote_level))) . $a_lines[$last];
786       }
787       else if ($quote_level > 0)
788         $a_lines[$n] = $replacer->get_replacement($replacer->add(
789           str_repeat('</blockquote>', $quote_level))) . $a_lines[$n];
790     }
4f6932 791
A 792     $quote_level = $q;
793   }
794
a4c970 795   $body = join("\n", $a_lines);
A 796
797   // quote plain text (don't use Q() here, to display entities "as is")
798   $table = get_html_translation_table(HTML_SPECIALCHARS);
799   unset($table['?']);
800   $body = strtr($body, $table);
4f6932 801
ba12c7 802   // colorize signature (up to <sig_max_lines> lines)
33dfdd 803   $len = strlen($body);
ba12c7 804   $sig_max_lines = $RCMAIL->config->get('sig_max_lines', 15);
33dfdd 805   while (($sp = strrpos($body, "-- \n", $sp ? -$len+$sp-1 : 0)) !== false) {
A 806     if ($sp == 0 || $body[$sp-1] == "\n") {
807       // do not touch blocks with more that X lines
ba12c7 808       if (substr_count($body, "\n", $sp) < $sig_max_lines)
99b8c1 809         $body = substr($body, 0, max(0, $sp))
A 810           .'<span class="sig">'.substr($body, $sp).'</span>';
33dfdd 811       break;
4f6932 812     }
99b8c1 813   }
4f6932 814
99b8c1 815   // insert url/mailto links and citation tags
A 816   $body = $replacer->resolve($body);
4f6932 817
A 818   return $body;
21e724 819 }
4e17e6 820
21e724 821
T 822 /**
823  * Callback function for washtml cleaning class
824  */
825 function rcmail_washtml_callback($tagname, $attrib, $content)
826 {
827   switch ($tagname) {
828     case 'form':
829       $out = html::div('form', $content);
f54a3a 830       break;
9800a8 831
1c499a 832     case 'style':
T 833       // decode all escaped entities and reduce to ascii strings
c5ee03 834       $stripped = preg_replace('/[^a-zA-Z\(:]/', '', rcmail_xss_entity_decode($content));
9800a8 835
36c236 836       // now check for evil strings like expression, behavior or url()
T 837       if (!preg_match('/expression|behavior|url\(|import/', $stripped)) {
1c499a 838         $out = html::tag('style', array('type' => 'text/css'), $content);
T 839         break;
840       }
9800a8 841
21e724 842     default:
T 843       $out = '';
844   }
9800a8 845
21e724 846   return $out;
T 847 }
4e17e6 848
T 849
45f56c 850 /**
2337a8 851  * Callback function for HTML tags fixing
A 852  */
853 function rcmail_html_tag_callback($matches)
854 {
855   $tagname = $matches[2];
856
857   $tagname = preg_replace(array(
f96ffd 858     '/:.*$/',            // Microsoft's Smart Tags <st1:xxxx>
A 859     '/[^a-z0-9_\[\]\!-]/i',    // forbidden characters
2337a8 860     ), '', $tagname);
A 861
862   return $matches[1].$tagname;
863 }
864
865
866 /**
45f56c 867  * return table with message headers
T 868  */
4e17e6 869 function rcmail_message_headers($attrib, $headers=NULL)
T 870   {
cc97ea 871   global $IMAP, $OUTPUT, $MESSAGE, $PRINT_MODE, $RCMAIL;
4e17e6 872   static $sa_attrib;
9800a8 873
4e17e6 874   // keep header table attrib
T 875   if (is_array($attrib) && !$sa_attrib)
876     $sa_attrib = $attrib;
877   else if (!is_array($attrib) && is_array($sa_attrib))
878     $attrib = $sa_attrib;
9800a8 879
4e17e6 880   if (!isset($MESSAGE))
T 881     return FALSE;
882
883   // get associative array of headers object
884   if (!$headers)
8fa58e 885     $headers = is_object($MESSAGE->headers) ? get_object_vars($MESSAGE->headers) : $MESSAGE->headers;
7c60ff 886
4e17e6 887   // show these headers
e25a35 888   $standard_headers = array('subject', 'from', 'to', 'cc', 'bcc', 'replyto',
A 889     'mail-reply-to', 'mail-followup-to', 'date');
cc97ea 890   $output_headers = array();
e5686f 891
cc97ea 892   foreach ($standard_headers as $hkey) {
e25a35 893     if ($headers[$hkey])
A 894       $value = $headers[$hkey];
895     else if ($headers['others'][$hkey])
896       $value = $headers['others'][$hkey];
897     else
4e17e6 898       continue;
T 899
cc97ea 900     if ($hkey == 'date') {
5b1de5 901       if ($PRINT_MODE)
e25a35 902         $header_value = format_date($value, $RCMAIL->config->get('date_long', 'x'));
5b1de5 903       else
e25a35 904         $header_value = format_date($value);
cc97ea 905     }
T 906     else if ($hkey == 'replyto') {
700320 907       if ($headers['replyto'] != $headers['from'])
e25a35 908         $header_value = rcmail_address_string($value, null, true, $attrib['addicon']);
700320 909       else
A 910         continue;
4e17e6 911     }
e25a35 912     else if ($hkey == 'mail-reply-to') {
A 913       if ($headers['mail-replyto'] != $headers['reply-to']
914         && $headers['reply-to'] != $headers['from']
915       )
916         $header_value = rcmail_address_string($value, null, true, $attrib['addicon']);
917       else
918         continue;
919     }
920     else if ($hkey == 'mail-followup-to') {
921       $header_value = rcmail_address_string($value, null, true, $attrib['addicon']);
922     }
cc97ea 923     else if (in_array($hkey, array('from', 'to', 'cc', 'bcc')))
e25a35 924       $header_value = rcmail_address_string($value, null, true, $attrib['addicon']);
A 925     else if ($hkey == 'subject' && empty($value))
cc97ea 926       $header_value = rcube_label('nosubject');
T 927     else
e25a35 928       $header_value = trim($IMAP->decode_header($value));
9800a8 929
db1a87 930     $output_headers[$hkey] = array(
T 931         'title' => rcube_label(preg_replace('/(^mail-|-)/', '', $hkey)),
932         'value' => $header_value, 'raw' => $value
933     );
cc97ea 934   }
9800a8 935
db1a87 936   $plugin = $RCMAIL->plugins->exec_hook('message_headers_output',
T 937     array('output' => $output_headers, 'headers' => $MESSAGE->headers));
9800a8 938
cc97ea 939   // compose html table
T 940   $table = new html_table(array('cols' => 2));
9800a8 941
cc97ea 942   foreach ($plugin['output'] as $hkey => $row) {
T 943     $table->add(array('class' => 'header-title'), Q($row['title']));
cb105a 944     $table->add(array('class' => 'header '.$hkey), Q($row['value'], ($hkey == 'subject' ? 'strict' : 'show')));
cc97ea 945   }
4e17e6 946
e5686f 947   // all headers division
cc97ea 948   $table->add(array('colspan' => 2, 'class' => "more-headers show-headers", 'onclick' => "return ".JS_OBJECT_NAME.".command('load-headers','',this)"), '');
T 949   $table->add_row(array('id' => "all-headers"));
950   $table->add(array('colspan' => 2, 'class' => "all"), html::div(array('id' => 'headers-source'), ''));
951   
e5686f 952   $OUTPUT->add_gui_object('all_headers_row', 'all-headers');
A 953   $OUTPUT->add_gui_object('all_headers_box', 'headers-source');
954
cc97ea 955   return $table->show($attrib);
4e17e6 956   }
T 957
958
45f56c 959 /**
21605c 960  * Handler for the 'messagebody' GUI object
45f56c 961  *
21605c 962  * @param array Named parameters
T 963  * @return string HTML content showing the message body
45f56c 964  */
4e17e6 965 function rcmail_message_body($attrib)
T 966   {
3c3433 967   global $CONFIG, $OUTPUT, $MESSAGE, $IMAP, $RCMAIL, $REMOTE_OBJECTS;
5f8686 968
8fa58e 969   if (!is_array($MESSAGE->parts) && empty($MESSAGE->body))
4e17e6 970     return '';
9800a8 971
4e17e6 972   if (!$attrib['id'])
T 973     $attrib['id'] = 'rcmailMsgBody';
974
8fa58e 975   $safe_mode = $MESSAGE->is_safe || intval($_GET['_safe']);
21605c 976   $out = '';
9800a8 977
4e17e6 978   $header_attrib = array();
T 979   foreach ($attrib as $attr => $value)
980     if (preg_match('/^headertable([a-z]+)$/i', $attr, $regs))
981       $header_attrib[$regs[1]] = $value;
982
8fa58e 983   if (!empty($MESSAGE->parts))
4e17e6 984     {
8fa58e 985     foreach ($MESSAGE->parts as $i => $part)
4e17e6 986       {
8fa58e 987       if ($part->type == 'headers')
8d4bcd 988         $out .= rcmail_message_headers(sizeof($header_attrib) ? $header_attrib : NULL, $part->headers);
3b7e00 989       else if ($part->type == 'content' && $part->size)
4e17e6 990         {
8d4bcd 991         if (empty($part->ctype_parameters) || empty($part->ctype_parameters['charset']))
8fa58e 992           $part->ctype_parameters['charset'] = $MESSAGE->headers->charset;
a0109c 993
8d4bcd 994         // fetch part if not available
T 995         if (!isset($part->body))
8fa58e 996           $part->body = $MESSAGE->get_part_content($part->mime_id);
a0109c 997
64e3e8 998         // message is cached but not exists (#1485443), or other error
A 999         if ($part->body === false) {
1000           rcmail_message_error($MESSAGE->uid);
1001         }
1002
3c3433 1003         $plugin = $RCMAIL->plugins->exec_hook('message_body_prefix', array(
A 1004           'part' => $part, 'prefix' => ''));
6b6f2e 1005
21e724 1006         $body = rcmail_print_body($part, array('safe' => $safe_mode, 'plain' => !$CONFIG['prefer_html']));
5f8686 1007
7b808b 1008         if ($part->ctype_secondary == 'html') {
fb995a 1009           $body = rcmail_html4inline($body, $attrib['id'], 'rcmBody', $attrs);
7b808b 1010           $div_attr = array('class' => 'message-htmlpart');
fb995a 1011           $style = array();
A 1012
cb3dfd 1013           if (!empty($attrs)) {
A 1014             foreach ($attrs as $a_idx => $a_val)
1015               $style[] = $a_idx . ': ' . $a_val;
1016             if (!empty($style))
1017               $div_attr['style'] = implode('; ', $style);
1018           }
7b808b 1019
3c3433 1020           $out .= html::div($div_attr, $plugin['prefix'] . $body);
7b808b 1021         }
a2f2c5 1022         else
3c3433 1023           $out .= html::div('message-part', $plugin['prefix'] . $body);
4e17e6 1024         }
T 1025       }
1026     }
3c3433 1027   else {
A 1028     $plugin = $RCMAIL->plugins->exec_hook('message_body_prefix', array(
1029       'part' => $MESSAGE, 'prefix' => ''));
1030
1031     $out .= html::div('message-part', $plugin['prefix'] . html::tag('pre', array(),
4f6932 1032       rcmail_plain_body(Q($MESSAGE->body, 'strict', false))));
3c3433 1033     }
4e17e6 1034
8fa58e 1035   $ctype_primary = strtolower($MESSAGE->structure->ctype_primary);
T 1036   $ctype_secondary = strtolower($MESSAGE->structure->ctype_secondary);
166b61 1037
4e17e6 1038   // list images after mail body
be5f03 1039   if ($CONFIG['inline_images']
2da368 1040       && $ctype_primary == 'multipart'
38d930 1041       && !empty($MESSAGE->attachments))
166b61 1042     {
8fa58e 1043     foreach ($MESSAGE->attachments as $attach_prop) {
47d06e 1044       // Content-Type: image/*...
A 1045       if (preg_match('/^image\//i', $attach_prop->mimetype) ||
1046         // ...or known file extension: many clients are using application/octet-stream
1047         ($attach_prop->filename &&
1048           preg_match('/^application\/octet-stream$/i', $attach_prop->mimetype) &&
1049           preg_match('/\.(jpg|jpeg|png|gif|bmp)$/i', $attach_prop->filename))
1050       ) {
8fa58e 1051         $out .= html::tag('hr') . html::p(array('align' => "center"),
T 1052           html::img(array(
1053             'src' => $MESSAGE->get_part_url($attach_prop->mime_id),
1054             'title' => $attach_prop->filename,
1055             'alt' => $attach_prop->filename,
1056           )));
1057         }
4e17e6 1058     }
8fa58e 1059   }
9800a8 1060
4e17e6 1061   // tell client that there are blocked remote objects
T 1062   if ($REMOTE_OBJECTS && !$safe_mode)
f11541 1063     $OUTPUT->set_env('blockedobjects', true);
4e17e6 1064
21605c 1065   return html::div($attrib, $out);
4e17e6 1066   }
T 1067
1068
aa055c 1069 /**
T 1070  * Convert all relative URLs according to a <base> in HTML
1071  */
1072 function rcmail_resolve_base($body)
1073 {
1074   // check for <base href=...>
1075   if (preg_match('!(<base.*href=["\']?)([hftps]{3,5}://[a-z0-9/.%-]+)!i', $body, $regs)) {
1076     $replacer = new rcube_base_replacer($regs[2]);
1077
1078     // replace all relative paths
1079     $body = preg_replace_callback('/(src|background|href)=(["\']?)([\.\/]+[^"\'\s]+)(\2|\s|>)/Ui', array($replacer, 'callback'), $body);
1080     $body = preg_replace_callback('/(url\s*\()(["\']?)([\.\/]+[^"\'\)\s]+)(\2)\)/Ui', array($replacer, 'callback'), $body);
1081   }
1082
1083   return $body;
1084 }
4e17e6 1085
45f56c 1086 /**
T 1087  * modify a HTML message that it can be displayed inside a HTML page
1088  */
fb995a 1089 function rcmail_html4inline($body, $container_id, $body_id='', &$attributes=null)
cb3dfd 1090 {
4e17e6 1091   $last_style_pos = 0;
T 1092   $body_lc = strtolower($body);
cb3dfd 1093   $cont_id = $container_id.($body_id ? ' div.'.$body_id : '');
9800a8 1094
4e17e6 1095   // find STYLE tags
T 1096   while (($pos = strpos($body_lc, '<style', $last_style_pos)) && ($pos2 = strpos($body_lc, '</style>', $pos)))
cb3dfd 1097   {
ea206d 1098     $pos = strpos($body_lc, '>', $pos)+1;
T 1099
4e17e6 1100     // replace all css definitions with #container [def]
cb3dfd 1101     $styles = rcmail_mod_css_styles(
A 1102       substr($body, $pos, $pos2-$pos), $cont_id);
ea206d 1103
3b12ae 1104     $body = substr($body, 0, $pos) . $styles . substr($body, $pos2);
S 1105     $body_lc = strtolower($body);
4e17e6 1106     $last_style_pos = $pos2;
cb3dfd 1107   }
4e17e6 1108
fe79b1 1109   // modify HTML links to open a new window if clicked
115263 1110   $GLOBALS['rcmail_html_container_id'] = $container_id;
T 1111   $body = preg_replace_callback('/<(a|link)\s+([^>]+)>/Ui', 'rcmail_alter_html_link', $body);
1112   unset($GLOBALS['rcmail_html_container_id']);
4e17e6 1113
cb3dfd 1114   $body = preg_replace(array(
b488c1 1115       // add comments arround html and other tags
d7a411 1116       '/(<!DOCTYPE[^>]*>)/i',
A 1117       '/(<\?xml[^>]*>)/i',
06895c 1118       '/(<\/?html[^>]*>)/i',
T 1119       '/(<\/?head[^>]*>)/i',
1120       '/(<title[^>]*>.*<\/title>)/Ui',
b488c1 1121       '/(<\/?meta[^>]*>)/i',
A 1122       // quote <? of php and xml files that are specified as text/html
1123       '/<\?/',
1124       '/\?>/',
1125       // replace <body> with <div>
1126       '/<body([^>]*)>/i',
1127       '/<\/body>/i',
1128       ),
1129     array(
1130       '<!--\\1-->',
1131       '<!--\\1-->',
1132       '<!--\\1-->',
1133       '<!--\\1-->',
1134       '<!--\\1-->',
1135       '<!--\\1-->',
1136       '&lt;?',
1137       '?&gt;',
7b808b 1138       '<div class="'.$body_id.'"\\1>',
b488c1 1139       '</div>',
A 1140       ),
06895c 1141     $body);
a0109c 1142
fb995a 1143   $attributes = array();
A 1144
1145   // Handle body attributes that doesn't play nicely with div elements
cb3dfd 1146   if (preg_match('/<div class="' . preg_quote($body_id, '/') . '" ([^>]+)/', $body, $m)) {
fb995a 1147     $attrs = $m[0];
A 1148     // Get bgcolor, we'll set it as background-color of the message container
1149     if (preg_match('/bgcolor=["\']*([a-z0-9#]+)["\']*/', $attrs, $mb)) {
cb3dfd 1150       $attributes['background-color'] = $mb[1];
fb995a 1151       $attrs = preg_replace('/bgcolor=["\']*([a-z0-9#]+)["\']*/', '', $attrs);
7b808b 1152     }
fb995a 1153     // Get background, we'll set it as background-image of the message container
A 1154     if (preg_match('/background=["\']*([^"\'>\s]+)["\']*/', $attrs, $mb)) {
cb3dfd 1155       $attributes['background-image'] = 'url('.$mb[1].')';
fb995a 1156       $attrs = preg_replace('/background=["\']*([^"\'>\s]+)["\']*/', '', $attrs);
A 1157     }
1158     if (!empty($attributes))
cb3dfd 1159       $body = preg_replace('/<div class="' . preg_quote($body_id, '/') . '" [^>]+/', rtrim($attrs), $body, 1);
A 1160
1161     // handle body styles related to background image
1162     if ($attributes['background-image']) {
1163       // get body style
1164       if (preg_match('/#'.preg_quote($cont_id, '/').'\s+\{([^}]+)}/i', $body, $m)) {
1165         // get background related style
1166         if (preg_match_all('/(background-position|background-repeat)\s*:\s*([^;]+);/i', $m[1], $ma, PREG_SET_ORDER)) {
1167           foreach ($ma as $style)
1168             $attributes[$style[1]] = $style[2];
1169         }
1170       }
1171     }
7b808b 1172   }
b488c1 1173   // make sure there's 'rcmBody' div, we need it for proper css modification
A 1174   // its name is hardcoded in rcmail_message_body() also
7b808b 1175   else
cb3dfd 1176     $body = '<div class="' . $body_id . '">' . $body . '</div>';
86958f 1177
cb3dfd 1178   return $body;
A 1179 }
4e17e6 1180
T 1181
45f56c 1182 /**
T 1183  * parse link attributes and set correct target
1184  */
115263 1185 function rcmail_alter_html_link($matches)
e5af2f 1186 {
115263 1187   global $EMAIL_ADDRESS_PATTERN;
9800a8 1188
115263 1189   $tag = $matches[1];
T 1190   $attrib = parse_attrib_string($matches[2]);
e5af2f 1191   $end = '>';
84f5b7 1192
e5af2f 1193   if ($tag == 'link' && preg_match('/^https?:\/\//i', $attrib['href'])) {
677e1f 1194     $attrib['href'] = "?_task=utils&amp;_action=modcss&amp;u=" . urlencode($attrib['href'])
A 1195         . "&amp;c=" . urlencode($GLOBALS['rcmail_html_container_id']);
e5af2f 1196     $end = ' />';
T 1197   }
ad18d6 1198   else if (preg_match('/^mailto:'.$EMAIL_ADDRESS_PATTERN.'(\?[^"\'>]+)?/i', $attrib['href'], $mailto)) {
115263 1199     $attrib['href'] = $mailto[0];
97bd2c 1200     $attrib['onclick'] = sprintf(
T 1201       "return %s.command('compose','%s',this)",
1202       JS_OBJECT_NAME,
ad18d6 1203       JQ($mailto[1].$mailto[2]));
4e17e6 1204   }
e5af2f 1205   else if (!empty($attrib['href']) && $attrib['href'][0] != '#') {
T 1206     $attrib['target'] = '_blank';
1207   }
1208
1209   return "<$tag" . html::attrib_string($attrib, array('href','name','target','onclick','id','class','style','title','rel','type','media')) . $end;
1210 }
4e17e6 1211
T 1212
45f56c 1213 /**
T 1214  * decode address string and re-format it as HTML links
1215  */
8e44f4 1216 function rcmail_address_string($input, $max=null, $linked=false, $addicon=null)
T 1217 {
e99991 1218   global $IMAP, $RCMAIL, $PRINT_MODE, $CONFIG;
55243b 1219   static $got_writable_abook = null;
1088d6 1220
4e17e6 1221   $a_parts = $IMAP->decode_address_list($input);
T 1222
1223   if (!sizeof($a_parts))
1224     return $input;
1225
1226   $c = count($a_parts);
1227   $j = 0;
1228   $out = '';
1229
55243b 1230   if ($got_writable_abook === null && $books = $RCMAIL->get_address_sources(true)) {
A 1231     $got_writable_abook = true;
1232   }
b579f4 1233
8e44f4 1234   foreach ($a_parts as $part) {
4e17e6 1235     $j++;
e99991 1236
A 1237     $name   = $part['name'];
1238     $mailto = $part['mailto'];
1239     $string = $part['string'];
1240
1241     // IDNA ASCII to Unicode
1242     if ($name == $mailto)
1243       $name = idn_to_utf8($name);
1244     if ($string == $mailto)
1245       $string = idn_to_utf8($string);
1246     $mailto = idn_to_utf8($mailto);
1247
8e44f4 1248     if ($PRINT_MODE) {
e99991 1249       $out .= sprintf('%s &lt;%s&gt;', Q($name), $mailto);
8e44f4 1250     }
1baeb6 1251     else if (check_email($part['mailto'], false)) {
8e44f4 1252       if ($linked) {
T 1253         $out .= html::a(array(
e99991 1254             'href' => 'mailto:'.$mailto,
A 1255             'onclick' => sprintf("return %s.command('compose','%s',this)", JS_OBJECT_NAME, JQ($mailto)),
1256             'title' => $mailto,
8e44f4 1257             'class' => "rcmContactAddress",
T 1258           ),
e99991 1259         Q($name ? $name : $mailto));
4e17e6 1260       }
8e44f4 1261       else {
e99991 1262         $out .= html::span(array('title' => $mailto, 'class' => "rcmContactAddress"),
A 1263           Q($name ? $name : $mailto));
8e44f4 1264       }
T 1265
55243b 1266       if ($addicon && $got_writable_abook) {
8e44f4 1267         $out .= '&nbsp;' . html::a(array(
T 1268             'href' => "#add",
e99991 1269             'onclick' => sprintf("return %s.command('add-contact','%s',this)", JS_OBJECT_NAME, urlencode($string)),
8e44f4 1270             'title' => rcube_label('addtoaddressbook'),
T 1271           ),
1272           html::img(array(
1273             'src' => $CONFIG['skin_path'] . $addicon,
1274             'alt' => "Add contact",
1275           )));
1276       }
1277     }
1278     else {
e99991 1279       if ($name)
A 1280         $out .= Q($name);
1281       if ($mailto)
1282         $out .= (strlen($out) ? ' ' : '') . sprintf('&lt;%s&gt;', Q($mailto));
8e44f4 1283     }
9800a8 1284
4e17e6 1285     if ($c>$j)
T 1286       $out .= ','.($max ? '&nbsp;' : ' ');
9800a8 1287
8e44f4 1288     if ($max && $j==$max && $c>$j) {
4e17e6 1289       $out .= '...';
T 1290       break;
1291     }
8e44f4 1292   }
9800a8 1293
4e17e6 1294   return $out;
8e44f4 1295 }
4e17e6 1296
T 1297
ccd63c 1298 /**
T 1299  * Wrap text to a given number of characters per line
6b6f2e 1300  * but respect the mail quotation of replies messages (>).
T 1301  * Finally add another quotation level by prpending the lines
1302  * with >
ccd63c 1303  *
T 1304  * @param string Text to wrap
1305  * @param int The line width
1306  * @return string The wrapped text
1307  */
6b6f2e 1308 function rcmail_wrap_and_quote($text, $length = 72)
ccd63c 1309 {
T 1310   // Rebuild the message body with a maximum of $max chars, while keeping quoted message.
dffcaa 1311   $max = min(77, $length + 8);
ccd63c 1312   $lines = preg_split('/\r?\n/', trim($text));
T 1313   $out = '';
1314
1315   foreach ($lines as $line) {
6b6f2e 1316     // don't wrap already quoted lines
T 1317     if ($line[0] == '>')
1318       $line = '>' . rtrim($line);
1319     else if (mb_strlen($line) > $max) {
1320       $newline = '';
1321       foreach(explode("\n", rc_wordwrap($line, $length - 2)) as $l) {
1322         if (strlen($l))
1323           $newline .= '> ' . $l . "\n";
1324         else
1325           $newline .= ">\n";
ccd63c 1326       }
6b6f2e 1327       $line = rtrim($newline);
ccd63c 1328     }
6b6f2e 1329     else
T 1330       $line = '> ' . $line;
ccd63c 1331
T 1332     // Append the line
1333     $out .= $line . "\n";
1334   }
9800a8 1335
ccd63c 1336   return $out;
T 1337 }
1338
1339
bc404f 1340 function rcmail_draftinfo_encode($p)
T 1341 {
1342   $parts = array();
1343   foreach ($p as $key => $val)
1344     $parts[] = $key . '=' . ($key == 'folder' ? base64_encode($val) : $val);
9800a8 1345
bc404f 1346   return join('; ', $parts);
T 1347 }
1348
1349
1350 function rcmail_draftinfo_decode($str)
1351 {
1352   $info = array();
1353   foreach (preg_split('/;\s+/', $str) as $part) {
1354     list($key, $val) = explode('=', $part, 2);
1355     if ($key == 'folder')
1356       $val = base64_decode($val);
1357     $info[$key] = $val;
1358   }
9800a8 1359
bc404f 1360   return $info;
T 1361 }
1362
1363
4e17e6 1364 function rcmail_message_part_controls()
e99991 1365 {
8fa58e 1366   global $MESSAGE;
9800a8 1367
d5342a 1368   $part = asciiwords(get_input_value('_part', RCUBE_INPUT_GPC));
8fa58e 1369   if (!is_object($MESSAGE) || !is_array($MESSAGE->parts) || !($_GET['_uid'] && $_GET['_part']) || !$MESSAGE->mime_parts[$part])
4e17e6 1370     return '';
9800a8 1371
8fa58e 1372   $part = $MESSAGE->mime_parts[$part];
T 1373   $table = new html_table(array('cols' => 3));
9800a8 1374
8fa58e 1375   if (!empty($part->filename)) {
T 1376     $table->add('title', Q(rcube_label('filename')));
1377     $table->add(null, Q($part->filename));
8dc048 1378     $table->add(null, '[' . html::a('?'.str_replace('_frame=', '_download=', $_SERVER['QUERY_STRING']), Q(rcube_label('download'))) . ']');
8fa58e 1379   }
9800a8 1380
8fa58e 1381   if (!empty($part->size)) {
T 1382     $table->add('title', Q(rcube_label('filesize')));
1383     $table->add(null, Q(show_bytes($part->size)));
1384   }
9800a8 1385
8fa58e 1386   return $table->show($attrib);
e99991 1387 }
4e17e6 1388
T 1389
1390
1391 function rcmail_message_part_frame($attrib)
e99991 1392 {
4e17e6 1393   global $MESSAGE;
9800a8 1394
8fa58e 1395   $part = $MESSAGE->mime_parts[asciiwords(get_input_value('_part', RCUBE_INPUT_GPC))];
4e17e6 1396   $ctype_primary = strtolower($part->ctype_primary);
T 1397
95fcc3 1398   $attrib['src'] = './?' . str_replace('_frame=', ($ctype_primary=='text' ? '_show=' : '_preload='), $_SERVER['QUERY_STRING']);
4e17e6 1399
95fcc3 1400   return html::iframe($attrib);
e99991 1401 }
4e17e6 1402
T 1403
45f56c 1404 /**
T 1405  * clear message composing settings
1406  */
4e17e6 1407 function rcmail_compose_cleanup()
e99991 1408 {
4e17e6 1409   if (!isset($_SESSION['compose']))
T 1410     return;
70d4b9 1411
929a50 1412   $rcmail = rcmail::get_instance();
e6ce00 1413   $rcmail->plugins->exec_hook('attachments_cleanup', array());
929a50 1414   $rcmail->session->remove('compose');
e99991 1415 }
9800a8 1416
fba1f5 1417
T 1418 /**
91790e 1419  * Send the given message using the configured method
A 1420  *
1421  * @param object $message    Reference to Mail_MIME object
1422  * @param string $from       Sender address string
1423  * @param array  $mailto     Array of recipient address strings
1424  * @param array  $smtp_error SMTP error array (reference)
1425  * @param string $body_file  Location of file with saved message body (reference)
f22ea7 1426  * @param array  $smtp_opts  SMTP options (e.g. DSN request)
91790e 1427  *
A 1428  * @return boolean Send status.
fba1f5 1429  */
f22ea7 1430 function rcmail_deliver_message(&$message, $from, $mailto, &$smtp_error, &$body_file, $smtp_opts=null)
fba1f5 1431 {
a22cb6 1432   global $CONFIG, $RCMAIL;
fba1f5 1433
8fa58e 1434   $headers = $message->headers();
79aeb3 1435
fba1f5 1436   // send thru SMTP server using custom SMTP library
e54f6c 1437   if ($CONFIG['smtp_server']) {
fba1f5 1438     // generate list of recipients
T 1439     $a_recipients = array($mailto);
9800a8 1440
fba1f5 1441     if (strlen($headers['Cc']))
T 1442       $a_recipients[] = $headers['Cc'];
1443     if (strlen($headers['Bcc']))
1444       $a_recipients[] = $headers['Bcc'];
9800a8 1445
fba1f5 1446     // clean Bcc from header for recipients
T 1447     $send_headers = $headers;
1448     unset($send_headers['Bcc']);
7ec922 1449     // here too, it because txtHeaders() below use $message->_headers not only $send_headers
A 1450     unset($message->_headers['Bcc']);
fba1f5 1451
91790e 1452     $smtp_headers = $message->txtHeaders($send_headers, true);
A 1453
1454     if ($message->getParam('delay_file_io')) {
1455       // use common temp dir
1456       $temp_dir = $RCMAIL->config->get('temp_dir');
1457       $body_file = tempnam($temp_dir, 'rcmMsg');
1458       if (PEAR::isError($mime_result = $message->saveMessageBody($body_file))) {
1459         raise_error(array('code' => 600, 'type' => 'php',
1460             'file' => __FILE__, 'line' => __LINE__,
1461             'message' => "Could not create message: ".$mime_result->getMessage()),
1462             TRUE, FALSE);
1463         return false;
1464       }
1465       $msg_body = fopen($body_file, 'r');
1466     } else {
1467       $msg_body = $message->get();
1468     }
1469
fba1f5 1470     // send message
2c3d81 1471     if (!is_object($RCMAIL->smtp))
A 1472       $RCMAIL->smtp_init(true);
9800a8 1473
f22ea7 1474     $sent = $RCMAIL->smtp->send_mail($from, $a_recipients, $smtp_headers, $msg_body, $smtp_opts);
2c3d81 1475     $smtp_response = $RCMAIL->smtp->get_response();
A 1476     $smtp_error = $RCMAIL->smtp->get_error();
91790e 1477
fba1f5 1478     // log error
T 1479     if (!$sent)
1480       raise_error(array('code' => 800, 'type' => 'smtp', 'line' => __LINE__, 'file' => __FILE__,
1481                         'message' => "SMTP error: ".join("\n", $smtp_response)), TRUE, FALSE);
e54f6c 1482   }
fba1f5 1483   // send mail using PHP's mail() function
e54f6c 1484   else {
fba1f5 1485     // unset some headers because they will be added by the mail() function
T 1486     $headers_enc = $message->headers($headers);
1487     $headers_php = $message->_headers;
1488     unset($headers_php['To'], $headers_php['Subject']);
9800a8 1489
fba1f5 1490     // reset stored headers and overwrite
T 1491     $message->_headers = array();
1492     $header_str = $message->txtHeaders($headers_php);
9800a8 1493
5d1101 1494     // #1485779
A 1495     if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
1496       if (preg_match_all('/<([^@]+@[^>]+)>/', $headers_enc['To'], $m)) {
1497         $headers_enc['To'] = implode(', ', $m[1]);
1498       }
ac8edb 1499     }
9800a8 1500
91790e 1501     $msg_body = $message->get();
A 1502
1503     if (PEAR::isError($msg_body))
1504       raise_error(array('code' => 600, 'type' => 'php',
1505             'file' => __FILE__, 'line' => __LINE__,
1506             'message' => "Could not create message: ".$msg_body->getMessage()),
1507             TRUE, FALSE);
ac8edb 1508     else {
A 1509       $delim   = $RCMAIL->config->header_delimiter();
1510       $to      = $headers_enc['To'];
1511       $subject = $headers_enc['Subject'];
93681d 1512       $header_str = rtrim($header_str);
ac8edb 1513
A 1514       if ($delim != "\r\n") {
1515         $header_str = str_replace("\r\n", $delim, $header_str);
1516         $msg_body   = str_replace("\r\n", $delim, $msg_body);
1517         $to         = str_replace("\r\n", $delim, $to);
1518         $subject    = str_replace("\r\n", $delim, $subject);
1519       }
1520
1521       if (ini_get('safe_mode'))
1522         $sent = mail($to, $subject, $msg_body, $header_str);
1523       else
1524         $sent = mail($to, $subject, $msg_body, $header_str, "-f$from");
1525     }
e54f6c 1526   }
9800a8 1527
e54f6c 1528   if ($sent) {
T 1529     $RCMAIL->plugins->exec_hook('message_sent', array('headers' => $headers, 'body' => $msg_body));
9800a8 1530
79aeb3 1531     // remove MDN headers after sending
ae8f19 1532     unset($headers['Return-Receipt-To'], $headers['Disposition-Notification-To']);
9800a8 1533
db1f1e 1534     // get all recipients
A 1535     if ($headers['Cc'])
1536       $mailto .= $headers['Cc'];
1537     if ($headers['Bcc'])
1538       $mailto .= $headers['Bcc'];
1539     if (preg_match_all('/<([^@]+@[^>]+)>/', $mailto, $m))
1540       $mailto = implode(', ', array_unique($m[1]));
1541
e54f6c 1542     if ($CONFIG['smtp_log']) {
146977 1543       write_log('sendmail', sprintf("User %s [%s]; Message for %s; %s",
e54f6c 1544         $RCMAIL->user->get_username(),
T 1545         $_SERVER['REMOTE_ADDR'],
1546         $mailto,
1547         !empty($smtp_response) ? join('; ', $smtp_response) : ''));
1548     }
79aeb3 1549   }
91790e 1550
4eb849 1551   if (is_resource($msg_body)) {
A 1552     fclose($msg_body);
1553   }
1554
fba1f5 1555   $message->_headers = array();
T 1556   $message->headers($headers);
9800a8 1557
fba1f5 1558   return $sent;
T 1559 }
f11541 1560
a99968 1561 /**
A 1562  * Send the MDN response
1563  *
1564  * @param mixed $message    Original message object (rcube_message) or UID
1565  * @param array $smtp_error SMTP error array (reference)
1566  *
1567  * @return boolean Send status
1568  */
1569 function rcmail_send_mdn($message, &$smtp_error)
0ea884 1570 {
83a763 1571   global $RCMAIL, $IMAP;
8fa58e 1572
a99968 1573   if (!is_a($message, rcube_message))
A 1574     $message = new rcube_message($message);
9800a8 1575
5c771c 1576   if ($message->headers->mdn_to && !$message->headers->mdn_sent &&
A 1577     ($IMAP->check_permflag('MDNSENT') || $IMAP->check_permflag('*')))
0ea884 1578   {
83a763 1579     $identity = $RCMAIL->user->get_identity();
0ea884 1580     $sender = format_email_recipient($identity['email'], $identity['name']);
8fa58e 1581     $recipient = array_shift($IMAP->decode_address_list($message->headers->mdn_to));
0ea884 1582     $mailto = $recipient['mailto'];
T 1583
ac8edb 1584     $compose = new Mail_mime("\r\n");
91790e 1585
A 1586     $compose->setParam('text_encoding', 'quoted-printable');
1587     $compose->setParam('html_encoding', 'quoted-printable');
1588     $compose->setParam('head_encoding', 'quoted-printable');
1589     $compose->setParam('head_charset', RCMAIL_CHARSET);
1590     $compose->setParam('html_charset', RCMAIL_CHARSET);
1591     $compose->setParam('text_charset', RCMAIL_CHARSET);
9800a8 1592
0ea884 1593     // compose headers array
T 1594     $headers = array(
2bf3cc 1595       'Date' => rcmail_user_date(),
0ea884 1596       'From' => $sender,
8fa58e 1597       'To'   => $message->headers->mdn_to,
T 1598       'Subject' => rcube_label('receiptread') . ': ' . $message->subject,
1d8cbc 1599       'Message-ID' => rcmail_gen_message_id(),
0ea884 1600       'X-Sender' => $identity['email'],
ea50e7 1601       'References' => trim($message->headers->references . ' ' . $message->headers->messageID),
0ea884 1602     );
9800a8 1603
83a763 1604     if ($agent = $RCMAIL->config->get('useragent'))
T 1605       $headers['User-Agent'] = $agent;
0ea884 1606
T 1607     $body = rcube_label("yourmessage") . "\r\n\r\n" .
8fa58e 1608       "\t" . rcube_label("to") . ': ' . rcube_imap::decode_mime_string($message->headers->to, $message->headers->charset) . "\r\n" .
T 1609       "\t" . rcube_label("subject") . ': ' . $message->subject . "\r\n" .
83a763 1610       "\t" . rcube_label("sent") . ': ' . format_date($message->headers->date, $RCMAIL->config->get('date_long')) . "\r\n" .
0ea884 1611       "\r\n" . rcube_label("receiptnote") . "\r\n";
9800a8 1612
e019f2 1613     $ua = $RCMAIL->config->get('useragent', "Roundcube Webmail (Version ".RCMAIL_VERSION.")");
0ea884 1614     $report = "Reporting-UA: $ua\r\n";
9800a8 1615
8fa58e 1616     if ($message->headers->to)
T 1617         $report .= "Original-Recipient: {$message->headers->to}\r\n";
9800a8 1618
0ea884 1619     $report .= "Final-Recipient: rfc822; {$identity['email']}\r\n" .
8fa58e 1620                "Original-Message-ID: {$message->headers->messageID}\r\n" .
0ea884 1621                "Disposition: manual-action/MDN-sent-manually; displayed\r\n";
9800a8 1622
8fa58e 1623     $compose->headers($headers);
6b0113 1624     $compose->setContentType('multipart/report', array('report-type'=> 'disposition-notification'));
7145e0 1625     $compose->setTXTBody(rc_wordwrap($body, 75, "\r\n"));
0ea884 1626     $compose->addAttachment($report, 'message/disposition-notification', 'MDNPart2.txt', false, '7bit', 'inline');
T 1627
91790e 1628     $sent = rcmail_deliver_message($compose, $identity['email'], $mailto, $smtp_error, $body_file);
0ea884 1629
T 1630     if ($sent)
1631     {
8fa58e 1632       $IMAP->set_flag($message->uid, 'MDNSENT');
0ea884 1633       return true;
T 1634     }
1635   }
9800a8 1636
0ea884 1637   return false;
T 1638 }
1639
1d8cbc 1640 // Returns unique Message-ID
A 1641 function rcmail_gen_message_id()
1642 {
1643   global $RCMAIL;
1644
1645   $local_part  = md5(uniqid('rcmail'.mt_rand(),true));
1646   $domain_part = $RCMAIL->user->get_username('domain');
1647
1648   // Try to find FQDN, some spamfilters doesn't like 'localhost' (#1486924)
1649   if (!preg_match('/\.[a-z]+$/i', $domain_part)) {
1650     if (($host = preg_replace('/:[0-9]+$/', '', $_SERVER['HTTP_HOST']))
1651       && preg_match('/\.[a-z]+$/i', $host)) {
1652         $domain_part = $host;
1653     }
1654     else if (($host = preg_replace('/:[0-9]+$/', '', $_SERVER['SERVER_NAME']))
1655       && preg_match('/\.[a-z]+$/i', $host)) {
1656         $domain_part = $host;
1657     }
1658   }
1659
1660   return sprintf('<%s@%s>', $local_part, $domain_part);
1661 }
1662
2bf3cc 1663 // Returns RFC2822 formatted current date in user's timezone
A 1664 function rcmail_user_date()
1665 {
1666   global $CONFIG;
1667
1668   // get user's timezone
1669   if ($CONFIG['timezone'] === 'auto') {
1670     $tz = isset($_SESSION['timezone']) ? $_SESSION['timezone'] : date('Z')/3600;
1671   }
1672   else {
1673     $tz = $CONFIG['timezone'];
1674     if ($CONFIG['dst_active'])
1675       $tz++;
1676   }
1677
1678   $date = time() + $tz * 60 * 60;
1679   $date = gmdate('r', $date);
64233d 1680   $tz   = sprintf('%+05d', intval($tz) * 100 + ($tz - intval($tz)) * 60);
A 1681   $date = preg_replace('/[+-][0-9]{4}$/', $tz, $date);
2bf3cc 1682
A 1683   return $date;
1684 }
1685
e0bd70 1686 // Fixes some content-type names
A 1687 function rcmail_fix_mimetype($name)
1688 {
1689   // Some versions of Outlook create garbage Content-Type:
1690   // application/pdf.A520491B_3BF7_494D_8855_7FAC2C6C0608
1691   if (preg_match('/^application\/pdf.+/', $name))
1692     $name = 'application/pdf';
1693
1694   return $name;
1695 }
2bf3cc 1696
e538b3 1697 function rcmail_search_filter($attrib)
A 1698 {
119cd1 1699   global $OUTPUT, $CONFIG;
e538b3 1700
A 1701   if (!strlen($attrib['id']))
1702     $attrib['id'] = 'rcmlistfilter';
1703
1704   $attrib['onchange'] = JS_OBJECT_NAME.'.filter_mailbox(this.value)';
9800a8 1705
e538b3 1706   /*
A 1707     RFC3501 (6.4.4): 'ALL', 'RECENT', 
1708     'ANSWERED', 'DELETED', 'FLAGGED', 'SEEN',
1709     'UNANSWERED', 'UNDELETED', 'UNFLAGGED', 'UNSEEN',
1710     'NEW', // = (RECENT UNSEEN)
1711     'OLD' // = NOT RECENT
1712   */
1713
1714   $select_filter = new html_select($attrib);
1715   $select_filter->add(rcube_label('all'), 'ALL');
1716   $select_filter->add(rcube_label('unread'), 'UNSEEN');
1717   $select_filter->add(rcube_label('flagged'), 'FLAGGED');
1718   $select_filter->add(rcube_label('unanswered'), 'UNANSWERED');
119cd1 1719   if (!$CONFIG['skip_deleted'])
A 1720     $select_filter->add(rcube_label('deleted'), 'DELETED');
e538b3 1721
A 1722   $out = $select_filter->show($_SESSION['search_filter']);
1723
1724   $OUTPUT->add_gui_object('search_filter', $attrib['id']);
1725
9800a8 1726   return $out;
e538b3 1727 }
A 1728
64e3e8 1729 function rcmail_message_error($uid=null)
A 1730 {
1731   global $RCMAIL;
1732
1733   // Set env variables for messageerror.html template
1734   if ($RCMAIL->action == 'show') {
1735     $mbox_name = $RCMAIL->imap->get_mailbox_name();
1736     $RCMAIL->output->set_env('mailbox', $mbox_name);
1737     $RCMAIL->output->set_env('uid', null);
1738   }
1739   // display error message
1740   $RCMAIL->output->show_message('messageopenerror', 'error');
1741   // ... display message error page
1742   $RCMAIL->output->send('messageerror');
1743 }
9b3fdc 1744
f11541 1745 // register UI objects
T 1746 $OUTPUT->add_handlers(array(
1747   'mailboxlist' => 'rcmail_mailbox_list',
1748   'messages' => 'rcmail_message_list',
1749   'messagecountdisplay' => 'rcmail_messagecount_display',
1750   'quotadisplay' => 'rcmail_quota_display',
ac5d15 1751   'mailboxname' => 'rcmail_mailbox_name_display',
f11541 1752   'messageheaders' => 'rcmail_message_headers',
T 1753   'messagebody' => 'rcmail_message_body',
1754   'messagecontentframe' => 'rcmail_messagecontent_frame',
1755   'messagepartframe' => 'rcmail_message_part_frame',
1756   'messagepartcontrols' => 'rcmail_message_part_controls',
e538b3 1757   'searchfilter' => 'rcmail_search_filter',
47124c 1758   'searchform' => array($OUTPUT, 'search_form'),
f11541 1759 ));
T 1760
b25dfd 1761