source: subversion/branches/devel-threads/program/steps/mail/func.inc @ 3106

Last change on this file since 3106 was 3106, checked in by alec, 4 years ago
  • Threads (just a beginning)
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 51.1 KB
Line 
1<?php
2
3/*
4 +-----------------------------------------------------------------------+
5 | program/steps/mail/func.inc                                           |
6 |                                                                       |
7 | This file is part of the RoundCube Webmail client                     |
8 | Copyright (C) 2005-2009, RoundCube Dev. - Switzerland                 |
9 | Licensed under the GNU GPL                                            |
10 |                                                                       |
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$EMAIL_ADDRESS_PATTERN = '([a-z0-9][a-z0-9\-\.\+\_]*@[a-z0-9][a-z0-9\-\.]*\\.[a-z]{2,5})';
23
24// actions that do not require imap connection
25$NOIMAP_ACTIONS = array('spell', 'addcontact', 'autocomplete', 'upload', 'display-attachment', 'remove-attachment');
26
27
28// log in to imap server
29if (!in_array($RCMAIL->action, $NOIMAP_ACTIONS) && !$RCMAIL->imap_connect()) {
30  $RCMAIL->kill_session();
31
32  if ($OUTPUT->ajax_call)
33    $OUTPUT->redirect(array(), 2000);
34
35  $OUTPUT->set_env('task', 'login');
36  $OUTPUT->send('login');
37}
38
39
40// set imap properties and session vars
41if ($mbox = get_input_value('_mbox', RCUBE_INPUT_GPC))
42  $IMAP->set_mailbox(($_SESSION['mbox'] = $mbox));
43else
44  $_SESSION['mbox'] = $IMAP->get_mailbox_name();
45
46if (!empty($_GET['_page']))
47  $IMAP->set_page(($_SESSION['page'] = intval($_GET['_page'])));
48
49// set default sort col/order to session
50if (!isset($_SESSION['sort_col']))
51  $_SESSION['sort_col'] = $CONFIG['message_sort_col'];
52if (!isset($_SESSION['sort_order']))
53  $_SESSION['sort_order'] = $CONFIG['message_sort_order'];
54
55// enable threads mode
56$a_message_threading = $RCMAIL->config->get('message_threading', array());
57$IMAP->set_threading($a_message_threading[$_SESSION['mbox']]);
58
59// set message set for search result
60if (!empty($_REQUEST['_search']) && isset($_SESSION['search'][$_REQUEST['_search']]))
61  {
62  $IMAP->set_search_set($_SESSION['search'][$_REQUEST['_search']]);
63  $OUTPUT->set_env('search_request', $_REQUEST['_search']);
64  $OUTPUT->set_env('search_text', $_SESSION['last_text_search']);
65  }
66
67// set main env variables, labels and page title
68if (empty($RCMAIL->action) || $RCMAIL->action == 'list')
69  {
70  $mbox_name = $IMAP->get_mailbox_name();
71
72  if (empty($RCMAIL->action))
73    {
74    // initialize searching result if search_filter is used
75    if ($_SESSION['search_filter'] && $_SESSION['search_filter'] != 'ALL')
76      {
77      $search_request = md5($mbox_name.$_SESSION['search_filter']);
78 
79      $IMAP->search($mbox_name, $_SESSION['search_filter'], RCMAIL_CHARSET, $_SESSION['sort_col']);
80      $_SESSION['search'][$search_request] = $IMAP->get_search_set();
81      $OUTPUT->set_env('search_request', $search_request);
82      }
83   
84      $OUTPUT->set_env('search_mods', $_SESSION['search_mods'] ? $_SESSION['search_mods'] : array('subject'=>'subject'));
85      // make sure the message count is refreshed (for default view)
86      $IMAP->messagecount($mbox_name, $IMAP->threading ? 'THREADS' : 'ALL', true);
87    }
88       
89  // set current mailbox in client environment
90  $OUTPUT->set_env('mailbox', $mbox_name);
91  $OUTPUT->set_env('quota', $IMAP->get_capability('quota'));
92  $OUTPUT->set_env('delimiter', $IMAP->get_hierarchy_delimiter());
93  $OUTPUT->set_env('threading', (bool) $IMAP->threading);
94
95  if ($CONFIG['flag_for_deletion'])
96    $OUTPUT->set_env('flag_for_deletion', true);
97  if ($CONFIG['read_when_deleted'])
98    $OUTPUT->set_env('read_when_deleted', true);
99  if ($CONFIG['skip_deleted'])
100    $OUTPUT->set_env('skip_deleted', true);
101  if ($CONFIG['display_next'])
102    $OUTPUT->set_env('display_next', true);
103         
104  if ($CONFIG['trash_mbox'])
105    $OUTPUT->set_env('trash_mailbox', $CONFIG['trash_mbox']);
106  if ($CONFIG['drafts_mbox'])
107    $OUTPUT->set_env('drafts_mailbox', $CONFIG['drafts_mbox']);
108  if ($CONFIG['junk_mbox'])
109    $OUTPUT->set_env('junk_mailbox', $CONFIG['junk_mbox']);
110
111  if (!$OUTPUT->ajax_call)
112    $OUTPUT->add_label('checkingmail', 'deletemessage', 'movemessagetotrash', 'movingmessage');
113
114  $OUTPUT->set_pagetitle(rcmail_localize_foldername($mbox_name));
115  }
116
117
118/**
119 * return the message list as HTML table
120 */
121function rcmail_message_list($attrib)
122  {
123  global $IMAP, $CONFIG, $COMM_PATH, $OUTPUT;
124
125  $skin_path = $CONFIG['skin_path'];
126  $image_tag = '<img src="%s%s" alt="%s" />';
127  // check to see if we have some settings for sorting
128  $sort_col   = $_SESSION['sort_col'];
129  $sort_order = $_SESSION['sort_order'];
130 
131  // add some labels to client
132  $OUTPUT->add_label('from', 'to');
133
134  // get message headers
135  $a_headers = $IMAP->list_headers('', '', $sort_col, $sort_order);
136
137  // add id to message list table if not specified
138  if (!strlen($attrib['id']))
139    $attrib['id'] = 'rcubemessagelist';
140
141  // allow the following attributes to be added to the <table> tag
142  $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border', 'summary'));
143
144  $out = '<table' . $attrib_str . ">\n";
145
146  // define list of cols to be displayed based on parameter or config
147  if (empty($attrib['columns']))
148      $a_show_cols = is_array($CONFIG['list_cols']) ? $CONFIG['list_cols'] : array('subject');
149  else
150      $a_show_cols = preg_split('/[\s,;]+/', strip_quotes($attrib['columns']));
151
152  // store column list in a session-variable
153  $_SESSION['list_columns'] = $a_show_cols;
154 
155  // define sortable columns
156  $a_sort_cols = array('subject', 'date', 'from', 'to', 'size');
157
158  $mbox = $IMAP->get_mailbox_name();
159  $delim = $IMAP->get_hierarchy_delimiter();
160
161  // show 'to' instead of 'from' in sent/draft messages
162  if ((strpos($mbox.$delim, $CONFIG['sent_mbox'].$delim)===0 || strpos($mbox.$delim, $CONFIG['drafts_mbox'].$delim)===0)
163      && ($f = array_search('from', $a_show_cols)) && !array_search('to', $a_show_cols))
164    $a_show_cols[$f] = 'to';
165 
166  // add col definition
167  $out .= '<colgroup>';
168
169  foreach ($a_show_cols as $col)
170    $out .= ($col!='attachment') ? sprintf('<col class="%s" />', $col) : '<col class="icon" />';
171
172  $out .= "</colgroup>\n";
173
174  // add table title
175  $out .= "<thead><tr>\n";
176
177  $javascript = '';
178  foreach ($a_show_cols as $col)
179    {
180    // get column name
181    switch ($col)
182      {
183      case 'flag':
184        $col_name = sprintf($image_tag, $skin_path, $attrib['unflaggedicon'], '');
185        break;
186      case 'attachment':
187        $col_name = sprintf($image_tag, $skin_path, $attrib['attachmenticon'], '');
188        break;
189      default:
190        $col_name = Q(rcube_label($col));
191    }
192
193    // make sort links
194    $sort = '';
195    if (in_array($col, $a_sort_cols))
196      {
197      // have buttons configured
198      if (!empty($attrib['sortdescbutton']) || !empty($attrib['sortascbutton']))
199        {
200        $sort = '&nbsp;&nbsp;';
201
202        // asc link
203        if (!empty($attrib['sortascbutton']))
204          {
205          $sort .= $OUTPUT->button(array(
206            'command' => 'sort',
207            'prop' => $col.'_ASC',
208            'image' => $attrib['sortascbutton'],
209            'align' => 'absmiddle',
210            'title' => 'sortasc'));
211          }       
212       
213        // desc link
214        if (!empty($attrib['sortdescbutton']))
215          {
216          $sort .= $OUTPUT->button(array(
217            'command' => 'sort',
218            'prop' => $col.'_DESC',
219            'image' => $attrib['sortdescbutton'],
220            'align' => 'absmiddle',
221            'title' => 'sortdesc'));
222          }
223        }
224      // just add a link tag to the header
225      else
226        {
227        $col_name = sprintf(
228          '<a href="./#sort" onclick="return %s.command(\'sort\',\'%s\',this)" title="%s">%s</a>',
229          JS_OBJECT_NAME,
230          $col,
231          rcube_label('sortby'),
232          $col_name);
233        }
234      }
235     
236    $sort_class = $col==$sort_col ? " sorted$sort_order" : '';
237
238    // put it all together
239    if ($col != 'attachment') {
240      // list options menu link
241      if ($col == 'subject' && !empty($attrib['optionsmenuicon'])) {
242          $list_menu = $OUTPUT->button(array(
243            'name' => 'listmenulink',
244            'id' => 'listmenulink',
245//            'command' => '',
246//            'prop' => '',
247            'image' => $attrib['optionsmenuicon'],
248            'align' => 'absmiddle',
249            'style' => 'float: left; padding-right: 5px',
250            'title' => 'listoptions'));
251        } else
252          $list_menu = '';
253
254      $out .= '<td class="'.$col.$sort_class.'" id="rcm'.$col.'">' . "$list_menu$col_name$sort</td>\n";
255      }
256    else   
257      $out .= '<td class="icon" id="rcm'.$col.'">' . "$col_name$sort</td>\n";
258    }
259
260  $out .= "</tr></thead>\n<tbody>\n";
261
262  // no messages in this mailbox
263  if (!sizeof($a_headers))
264    $OUTPUT->show_message('nomessagesfound', 'notice');
265
266  $a_js_message_arr = array();
267
268  // create row for each message
269  foreach ($a_headers as $i => $header)  //while (list($i, $header) = each($a_headers))
270    {
271    $message_icon = $attach_icon = $flagged_icon = '';
272    $js_row_arr = array();
273    $zebra_class = $i%2 ? ' even' : ' odd';
274
275    // set messag attributes to javascript array
276    if ($header->deleted)
277      $js_row_arr['deleted'] = true;
278    if (!$header->seen)
279      $js_row_arr['unread'] = true;
280    if ($header->answered)
281      $js_row_arr['replied'] = true;
282    if ($header->forwarded)
283      $js_row_arr['forwarded'] = true;
284    if ($header->flagged)
285      $js_row_arr['flagged'] = true;
286    if ($header->has_children)
287      $js_row_arr['has_children'] = true;
288    if ($header->depth)
289      $js_row_arr['depth'] = $header->depth;
290    if ($header->parent_uid)
291      $js_row_arr['parent_uid'] = $header->parent_uid;
292    if ($header->unread_children)
293      $js_row_arr['unread_children'] = $header->unread_children;
294
295    // set message icon
296    if ($header->seen && $attrib['unreadchildrenicon'] && $header->unread_children > 0)
297      $message_icon = $attrib['unreadchildrenicon'];
298    else if ($attrib['deletedicon'] && $header->deleted)
299      $message_icon = $attrib['deletedicon'];
300    else if ($attrib['repliedicon'] && $header->answered)
301      {
302      if ($attrib['forwardedrepliedicon'] && $header->forwarded)
303        $message_icon = $attrib['forwardedrepliedicon'];
304      else
305        $message_icon = $attrib['repliedicon'];
306      }
307    else if ($attrib['forwardedicon'] && $header->forwarded)
308      $message_icon = $attrib['forwardedicon'];
309    else if ($attrib['unreadicon'] && !$header->seen)
310      $message_icon = $attrib['unreadicon'];
311    else if ($attrib['messageicon'])
312      $message_icon = $attrib['messageicon'];
313
314    if ($attrib['flaggedicon'] && $header->flagged)
315      $flagged_icon = $attrib['flaggedicon'];
316    else if ($attrib['unflaggedicon'] && !$header->flagged)
317      $flagged_icon = $attrib['unflaggedicon'];
318   
319    // set attachment icon
320    if ($attrib['attachmenticon'] && preg_match("/multipart\/m/i", $header->ctype))
321      $attach_icon = $attrib['attachmenticon'];
322       
323    $out .= sprintf('<tr id="rcmrow%d" class="message%s%s%s%s"%s>'."\n",
324                    $header->uid,
325                    $header->seen ? '' : ' unread',
326                    $header->deleted ? ' deleted' : '',
327                    $header->flagged ? ' flagged' : '',
328                    $zebra_class,
329                    ($header->depth) ? ' style="display: none"' : '');
330   
331    $tree = '';
332    if ($IMAP->threading)
333      {
334      // XXX: This assumes that div width is hardcoded to 15px,
335      // Chris did it a bit differently in an original patch, he was adding so much divs as depth is
336      // I replaced logic in list.js:drag_mouse_move() so subject text is picked defferently, so
337      // either method of could be used (that was only the one problem I noted with these added divs).
338      // The same is true for an online list (program/js/app.js:add_message_row()).
339      // Bubble
340      $width = ($header->depth) * 15;
341      $tree .= '<div id="rcmtab' . $header->uid . '" class="branch" style="width:' . $width . 'px;">&nbsp</div>';
342      if ($header->has_children && !$header->depth)
343        $tree .= '<div id="rcmexpando' . $header->uid . '" class="collapsed">&nbsp;</div>';
344      else
345        $tree .= '<div class="leaf">&nbsp;</div>';
346      }
347
348    $tree .= $message_icon ? sprintf($image_tag, $skin_path, $message_icon, '') : '';
349
350    $IMAP->set_charset(!empty($header->charset) ? $header->charset : $CONFIG['default_charset']);
351 
352    // format each col
353    $first = true;
354    foreach ($a_show_cols as $col)
355      {
356      if ($col=='from' || $col=='to')
357        $cont = Q(rcmail_address_string($header->$col, 3, false, $attrib['addicon']), 'show');
358      else if ($col=='subject')
359        {
360        $action = $mbox==$CONFIG['drafts_mbox'] ? 'compose' : 'show';
361        $uid_param = $mbox==$CONFIG['drafts_mbox'] ? '_draft_uid' : '_uid';
362        $cont = abbreviate_string(trim($IMAP->decode_header($header->$col)), 160);
363        if (empty($cont)) $cont = rcube_label('nosubject');
364        $cont = $OUTPUT->browser->ie ? Q($cont) : sprintf('<a href="%s" onclick="return rcube_event.cancel(event)">%s</a>', Q(rcmail_url($action, array($uid_param=>$header->uid, '_mbox'=>$mbox))), Q($cont));
365        }
366      else if ($col=='flag')
367        $cont = $flagged_icon ? sprintf($image_tag, $skin_path, $flagged_icon, '') : '';
368      else if ($col=='size')
369        $cont = show_bytes($header->$col);
370      else if ($col=='date')
371        $cont = format_date($header->date);
372      else
373        $cont = Q($header->$col);
374       
375      if ($first) {
376        $first = false;
377        $cont = $tree . $cont;
378      }
379      if ($col!='attachment')
380        $out .= '<td class="'.$col.'">' . $cont . "</td>\n";
381      else
382        $out .= sprintf("<td class=\"icon\">%s</td>\n", $attach_icon ? sprintf($image_tag, $skin_path, $attach_icon, '') : '&nbsp;');
383      }
384
385    $out .= "</tr>\n";
386   
387    if (sizeof($js_row_arr))
388      $a_js_message_arr[$header->uid] = $js_row_arr;
389    }
390 
391  // complete message table
392  $out .= "</tbody></table>\n";
393 
394  $message_count = $IMAP->messagecount(NULL, $IMAP->threading ? 'THREADS' : 'ALL');
395 
396  // set client env
397  $OUTPUT->add_gui_object('mailcontframe', 'mailcontframe');
398  $OUTPUT->add_gui_object('messagelist', $attrib['id']);
399  $OUTPUT->set_env('autoexpand_threads', $CONFIG['autoexpand_threads']);
400  $OUTPUT->set_env('messagecount', $message_count);
401  $OUTPUT->set_env('current_page', $IMAP->list_page);
402  $OUTPUT->set_env('pagecount', ceil($message_count/$IMAP->page_size));
403  $OUTPUT->set_env('sort_col', $sort_col);
404  $OUTPUT->set_env('sort_order', $sort_order);
405 
406  if ($attrib['messageicon'])
407    $OUTPUT->set_env('messageicon', $skin_path . $attrib['messageicon']);
408  if ($attrib['deletedicon'])
409    $OUTPUT->set_env('deletedicon', $skin_path . $attrib['deletedicon']);
410  if ($attrib['unreadicon'])
411    $OUTPUT->set_env('unreadicon', $skin_path . $attrib['unreadicon']);
412  if ($attrib['repliedicon'])
413    $OUTPUT->set_env('repliedicon', $skin_path . $attrib['repliedicon']);
414  if ($attrib['forwardedicon'])
415    $OUTPUT->set_env('forwardedicon', $skin_path . $attrib['forwardedicon']);
416  if ($attrib['forwardedrepliedicon'])
417    $OUTPUT->set_env('forwardedrepliedicon', $skin_path . $attrib['forwardedrepliedicon']);
418  if ($attrib['attachmenticon'])
419    $OUTPUT->set_env('attachmenticon', $skin_path . $attrib['attachmenticon']);
420  if ($attrib['flaggedicon'])
421    $OUTPUT->set_env('flaggedicon', $skin_path . $attrib['flaggedicon']);
422  if ($attrib['unflaggedicon'])
423    $OUTPUT->set_env('unflaggedicon', $skin_path . $attrib['unflaggedicon']);
424  if ($attrib['unreadchildrenicon'])
425    $OUTPUT->set_env('unreadchildrenicon', $skin_path . $attrib['unreadchildrenicon']);
426 
427  $OUTPUT->set_env('messages', $a_js_message_arr);
428  $OUTPUT->set_env('coltypes', $a_show_cols);
429 
430  $OUTPUT->include_script('list.js');
431 
432  return $out;
433  }
434
435
436/**
437 * return javascript commands to add rows to the message list
438 * or to replace the whole list (IE only)
439 */
440function rcmail_js_message_list($a_headers, $insert_top=FALSE, $replace=TRUE)
441  {
442  global $CONFIG, $IMAP, $OUTPUT;
443
444  if (empty($_SESSION['list_columns']))
445    $a_show_cols = is_array($CONFIG['list_cols']) ? $CONFIG['list_cols'] : array('subject');
446  else
447    $a_show_cols = $_SESSION['list_columns'];
448
449  $mbox = $IMAP->get_mailbox_name();
450  $delim = $IMAP->get_hierarchy_delimiter();
451 
452  // show 'to' instead of 'from' in sent/draft messages
453  if ((strpos($mbox.$delim, $CONFIG['sent_mbox'].$delim)===0 || strpos($mbox.$delim, $CONFIG['drafts_mbox'].$delim)===0)
454      && (($f = array_search('from', $a_show_cols)) !== false) && array_search('to', $a_show_cols) === false)
455    $a_show_cols[$f] = 'to';
456
457  $browser = new rcube_browser;
458
459  $OUTPUT->command('set_message_coltypes', $a_show_cols);
460
461  // remove 'attachment' and 'flag' columns, we don't need them here
462  if(($key = array_search('attachment', $a_show_cols)) !== FALSE)
463    unset($a_show_cols[$key]);
464  if(($key = array_search('flag', $a_show_cols)) !== FALSE)
465    unset($a_show_cols[$key]);
466
467  if ($browser->ie && $replace)
468    $OUTPUT->command('offline_message_list', true);
469
470  // remove 'attachment' and 'flag' columns, we don't need them here
471  if(($key = array_search('attachment', $a_show_cols)) !== FALSE)
472    unset($a_show_cols[$key]);
473  if(($key = array_search('flag', $a_show_cols)) !== FALSE)
474    unset($a_show_cols[$key]);
475
476  // loop through message headers
477  foreach ($a_headers as $n => $header)
478    {
479    $a_msg_cols = array();
480    $a_msg_flags = array();
481   
482    if (empty($header))
483      continue;
484
485    $IMAP->set_charset(!empty($header->charset) ? $header->charset : $CONFIG['default_charset']);
486
487    // format each col; similar as in rcmail_message_list()
488    foreach ($a_show_cols as $col)
489      {
490      if ($col=='from' || $col=='to')
491        $cont = Q(rcmail_address_string($header->$col, 3), 'show');
492      else if ($col=='subject')
493        {
494        $action = $mbox==$CONFIG['drafts_mbox'] ? 'compose' : 'show';
495        $uid_param = $mbox==$CONFIG['drafts_mbox'] ? '_draft_uid' : '_uid';
496        $cont = abbreviate_string(trim($IMAP->decode_header($header->$col)), 160);
497        if (!$cont) $cont = rcube_label('nosubject');
498        $cont = $browser->ie ? Q($cont) : sprintf('<a href="%s" onclick="return rcube_event.cancel(event)">%s</a>', Q(rcmail_url($action, array($uid_param=>$header->uid, '_mbox'=>$mbox))), Q($cont));
499        }
500      else if ($col=='size')
501        $cont = show_bytes($header->$col);
502      else if ($col=='date')
503        $cont = format_date($header->date);
504      else
505        $cont = Q($header->$col);
506         
507      $a_msg_cols[$col] = $cont;
508      }
509
510    if ($header->depth)
511      $a_msg_flags['depth'] = $header->depth;
512    if ($header->parent_uid)
513      $a_msg_flags['parent_uid'] = $header->parent_uid;
514    if ($header->has_children)
515      $a_msg_flags['has_children'] = $header->has_children;
516    if ($header->unread_children)
517      $a_msg_flags['unread_children'] = $header->unread_children;
518    if ($header->deleted)
519      $a_msg_flags['deleted'] = 1;
520    if (!$header->seen)
521      $a_msg_flags['unread'] = 1;
522    if ($header->answered)
523      $a_msg_flags['replied'] = 1;
524    if ($header->forwarded)
525      $a_msg_flags['forwarded'] = 1;
526    if ($header->flagged)
527      $a_msg_flags['flagged'] = 1;
528     
529    if ($browser->ie)
530      $a_msg_cols = rc_utf8_clean($a_msg_cols);
531   
532    $OUTPUT->command('add_message_row',
533      $header->uid,
534      $a_msg_cols,
535      $a_msg_flags,
536      preg_match("/multipart\/m/i", $header->ctype),
537      $insert_top);
538    }
539
540  if ($browser->ie && $replace)
541    $OUTPUT->command('offline_message_list', false);
542  }
543
544
545/**
546 * return an HTML iframe for loading mail content
547 */
548function rcmail_messagecontent_frame($attrib)
549  {
550  global $OUTPUT;
551 
552  if (empty($attrib['id']))
553    $attrib['id'] = 'rcmailcontentwindow';
554
555  $attrib['name'] = $attrib['id'];
556
557  $OUTPUT->set_env('contentframe', $attrib['id']);
558  $OUTPUT->set_env('blankpage', $attrib['src'] ? $OUTPUT->abs_url($attrib['src']) : 'program/blank.gif');
559
560  return html::iframe($attrib);
561  }
562
563
564function rcmail_messagecount_display($attrib)
565  {
566  global $IMAP, $OUTPUT;
567 
568  if (!$attrib['id'])
569    $attrib['id'] = 'rcmcountdisplay';
570
571  $OUTPUT->add_gui_object('countdisplay', $attrib['id']);
572
573  return html::span($attrib, rcmail_get_messagecount_text());
574  }
575
576
577function rcmail_quota_display($attrib)
578  {
579  global $OUTPUT, $COMM_PATH;
580
581  if (!$attrib['id'])
582    $attrib['id'] = 'rcmquotadisplay';
583
584  if(isset($attrib['display']))
585    $_SESSION['quota_display'] = $attrib['display'];
586
587  $OUTPUT->add_gui_object('quotadisplay', $attrib['id']);
588 
589  $quota = rcmail_quota_content(NULL, $attrib);
590 
591  if (is_array($quota)) {
592    $OUTPUT->add_script('$(document).ready(function(){
593        rcmail.set_quota('.json_serialize($quota).')});', 'foot');
594    $quota = '';
595    }
596 
597  return html::span($attrib, $quota);
598  }
599
600
601function rcmail_quota_content($quota=NULL, $attrib=NULL)
602  {
603  global $IMAP, $COMM_PATH, $RCMAIL;
604
605  $display = isset($_SESSION['quota_display']) ? $_SESSION['quota_display'] : '';
606
607  if (empty($quota)) {
608    if (!$IMAP->get_capability('QUOTA'))
609      return rcube_label('unknown');
610    else
611      $quota = $IMAP->get_quota();
612    }
613
614  if ($quota && !($quota['total']==0 && $RCMAIL->config->get('quota_zero_as_unlimited')))
615    {
616    $quota_result = sprintf('%s / %s (%.0f%%)',
617        show_bytes($quota['used'] * 1024), show_bytes($quota['total'] * 1024),
618        $quota['percent']);
619
620    if ($display == 'image') {
621      $quota_result = array(       
622        'percent'       => $quota['percent'],
623        'title'         => $quota_result,
624        );
625      if ($attrib['width'])
626        $quota_result['width'] = $attrib['width'];
627      if ($attrib['height'])
628        $quota_result['height'] = $attrib['height'];
629      }
630    }
631  else
632    return rcube_label('unlimited');
633
634  return $quota_result;
635  }
636
637
638function rcmail_get_messagecount_text($count=NULL, $page=NULL)
639  {
640  global $IMAP, $MESSAGE;
641 
642  if (isset($MESSAGE->index))
643    {
644    return rcube_label(array('name' => 'messagenrof',
645        'vars' => array('nr'  => $MESSAGE->index+1,
646        'count' => $count!==NULL ? $count : $IMAP->messagecount(NULL, 'ALL')))); // Only messages, no threads here
647    }
648
649  if ($page===NULL)
650    $page = $IMAP->list_page;
651   
652  $start_msg = ($page-1) * $IMAP->page_size + 1;
653  $max = $count!==NULL ? $count : $IMAP->messagecount(NULL, $IMAP->threading ? 'THREADS' : 'ALL');
654
655  if ($max==0)
656    $out = rcube_label('mailboxempty');
657  else
658    $out = rcube_label(array('name' => $IMAP->threading ? 'threadsfromto' : 'messagesfromto',
659            'vars' => array('from'  => $start_msg,
660            'to'    => min($max, $start_msg + $IMAP->page_size - 1),
661            'count' => $max)));
662
663  return Q($out);
664  }
665
666
667function rcmail_mailbox_name_display($attrib)
668{
669  global $RCMAIL;
670
671  if (!$attrib['id'])
672    $attrib['id'] = 'rcmmailboxname';
673
674  $RCMAIL->output->add_gui_object('mailboxname', $attrib['id']);
675
676  return html::span($attrib, rcmail_get_mailbox_name_text());
677}
678
679function rcmail_get_mailbox_name_text()
680{
681  global $RCMAIL;
682  return rcmail_localize_foldername($RCMAIL->imap->get_mailbox_name());
683}
684
685
686function rcmail_send_unread_count($mbox_name, $force=false)
687{
688  global $RCMAIL;
689   
690  $old_unseen = $_SESSION['unseen_count'][$mbox_name];
691  $unseen = $RCMAIL->imap->messagecount($mbox_name, 'UNSEEN', $force);
692
693  if ($unseen != $old_unseen || ($mbox_name == 'INBOX'))
694    $RCMAIL->output->command('set_unread_count', $mbox_name, $unseen, ($mbox_name == 'INBOX'));
695
696  // @TODO: this data is doubled (session and cache tables) if caching is enabled
697  $_SESSION['unseen_count'][$mbox_name] = $unseen;
698   
699  return $unseen;
700}
701                             
702
703/**
704 * Sets message is_safe flag according to 'show_images' option value
705 *
706 * @param object rcube_message Message
707 */
708function rcmail_check_safe(&$message)
709{
710  global $RCMAIL;
711
712  $show_images = $RCMAIL->config->get('show_images');
713  if (!$message->is_safe
714    && !empty($show_images)
715    && $message->has_html_part())
716  {
717    switch($show_images) {
718      case '1': // known senders only
719        $CONTACTS = new rcube_contacts($RCMAIL->db, $_SESSION['user_id']);
720        if ($CONTACTS->search('email', $message->sender['mailto'], true, false)->count) {
721          $message->set_safe(true);
722        }
723      break;
724      case '2': // always
725        $message->set_safe(true);
726      break;
727    }
728  }
729}
730
731/**
732 * Cleans up the given message HTML Body (for displaying)
733 *
734 * @param string HTML
735 * @param array  Display parameters
736 * @param array  CID map replaces (inline images)
737 * @return string Clean HTML
738 */
739function rcmail_wash_html($html, $p = array(), $cid_replaces)
740{
741  global $REMOTE_OBJECTS;
742 
743  $p += array('safe' => false, 'inline_html' => true);
744
745  // special replacements (not properly handled by washtml class)
746  $html_search = array(
747    '/(<\/nobr>)(\s+)(<nobr>)/i',       // space(s) between <NOBR>
748    '/<title>.*<\/title>/i',            // PHP bug #32547 workaround: remove title tag
749    '/^(\0\0\xFE\xFF|\xFF\xFE\0\0|\xFE\xFF|\xFF\xFE|\xEF\xBB\xBF)/',    // byte-order mark (only outlook?)
750    '/<html\s[^>]+>/i',                 // washtml/DOMDocument cannot handle xml namespaces
751  );
752  $html_replace = array(
753    '\\1'.' &nbsp; '.'\\3',
754    '',
755    '',
756    '<html>',
757  );
758  $html = preg_replace($html_search, $html_replace, $html);
759
760  // fix (unknown/malformed) HTML tags before "wash"
761  $html = preg_replace_callback('/(<[\/]*)([^\s>]+)/', 'rcmail_html_tag_callback', $html);
762
763  // charset was converted to UTF-8 in rcube_imap::get_message_part(),
764  // -> change charset specification in HTML accordingly
765  $charset_pattern = '(<meta\s+[^>]*)(content=[\'"]?\w+\/\w+;\s*charset)=([a-z0-9-_]+)';
766  if (preg_match("/$charset_pattern/Ui", $html)) {
767    $html = preg_replace("/$charset_pattern/i", '\\1\\2='.RCMAIL_CHARSET, $html);
768  }
769  else {
770    // add meta content-type to malformed messages, washtml cannot work without that
771    if (!preg_match('/<head[^>]*>(.*)<\/head>/Uims', $html))
772      $html = '<head></head>'. $html;
773    $html = substr_replace($html, '<meta http-equiv="Content-Type" content="text/html; charset='.RCMAIL_CHARSET.'" />', intval(stripos($html, '<head>')+6), 0);
774  }
775
776  // turn relative into absolute urls
777  $html = rcmail_resolve_base($html);
778
779  // clean HTML with washhtml by Frederic Motte
780  $wash_opts = array(
781    'show_washed' => false,
782    'allow_remote' => $p['safe'],
783    'blocked_src' => "./program/blocked.gif",
784    'charset' => RCMAIL_CHARSET,
785    'cid_map' => $cid_replaces,
786    'html_elements' => array('body'),
787  );
788   
789  if (!$p['inline_html']) {
790    $wash_opts['html_elements'] = array('html','head','title','body');
791  }
792  if ($p['safe']) {
793    $wash_opts['html_elements'][] = 'link';
794    $wash_opts['html_attribs'] = array('rel','type');
795  }
796   
797  $washer = new washtml($wash_opts);
798  $washer->add_callback('form', 'rcmail_washtml_callback');
799
800  // allow CSS styles, will be sanitized by rcmail_washtml_callback()
801  $washer->add_callback('style', 'rcmail_washtml_callback');
802
803  $html = $washer->wash($html);
804  $REMOTE_OBJECTS = $washer->extlinks;
805 
806  return $html;
807}
808
809
810/**
811 * Convert the given message part to proper HTML
812 * which can be displayed the message view
813 *
814 * @param object rcube_message_part Message part
815 * @param array  Display parameters array
816 * @return string Formatted HTML string
817 */
818function rcmail_print_body($part, $p = array())
819{
820  global $RCMAIL;
821 
822  // trigger plugin hook
823  $data = $RCMAIL->plugins->exec_hook('message_part_before',
824    array('type' => $part->ctype_secondary, 'body' => $part->body) + $p + array('safe' => false, 'plain' => false, 'inline_html' => true));
825
826  // convert html to text/plain
827  if ($data['type'] == 'html' && $data['plain']) {
828    $txt = new html2text($data['body'], false, true);
829    $body = $txt->get_text();
830    $part->ctype_secondary = 'plain';
831  }
832  // text/html
833  else if ($data['type'] == 'html') {
834    $body = rcmail_wash_html($data['body'], $data, $part->replaces);
835    $part->ctype_secondary = $data['type'];
836  }
837  // text/enriched
838  else if ($data['type'] == 'enriched') {
839    $part->ctype_secondary = 'html';
840    require_once('lib/enriched.inc');
841    $body = Q(enriched_to_html($data['body']), 'show');
842  }
843  else {
844    // assert plaintext
845    $body = $part->body;
846    $part->ctype_secondary = $data['type'] = 'plain';
847  }
848 
849  // free some memory (hopefully)
850  unset($data['body']);
851
852  // plaintext postprocessing
853  if ($part->ctype_secondary == 'plain')
854    $body = rcmail_plain_body($body);
855
856  // allow post-processing of the message body
857  $data = $RCMAIL->plugins->exec_hook('message_part_after', array('type' => $part->ctype_secondary, 'body' => $body) + $data);
858
859  return $data['type'] == 'html' ? $data['body'] : html::tag('pre', array(), $data['body']);
860}
861
862/**
863 * Handle links and citation marks in plain text message
864 *
865 * @param string  Plain text string
866 * @return string Formatted HTML string
867 */
868function rcmail_plain_body($body)
869{
870  // make links and email-addresses clickable
871  $replacements = new rcube_string_replacer;
872   
873  // search for patterns like links and e-mail addresses
874  $body = preg_replace_callback($replacements->link_pattern, array($replacements, 'link_callback'), $body);
875  $body = preg_replace_callback($replacements->mailto_pattern, array($replacements, 'mailto_callback'), $body);
876
877  // split body into single lines
878  $a_lines = preg_split('/\r?\n/', $body);
879  $q_lines = array();
880  $quote_level = 0;
881
882  // find/mark quoted lines...
883  for ($n=0, $cnt=count($a_lines); $n < $cnt; $n++) {
884    $q = 0;
885
886    if ($a_lines[$n][0] == '>' && preg_match('/^(>+\s*)+/', $a_lines[$n], $regs)) {
887      $q = strlen(preg_replace('/\s/', '', $regs[0]));
888        $a_lines[$n] = substr($a_lines[$n], strlen($regs[0]));
889
890      if ($q > $quote_level)
891        $q_lines[$n]['quote'] = $q - $quote_level;
892      else if ($q < $quote_level)
893        $q_lines[$n]['endquote'] = $quote_level - $q;
894    }
895    else if ($quote_level > 0)
896      $q_lines[$n]['endquote'] = $quote_level;
897
898    $quote_level = $q;
899  }
900
901  // quote plain text
902  $body = Q(join("\n", $a_lines), 'replace', false);
903
904  // colorize signature
905  if (($sp = strrpos($body, '-- ')) !== false)
906    if (($sp == 0 || $body[$sp-1] == "\n") && $body[$sp+3] == "\n") {
907      $body = substr($body, 0, max(0, $sp))
908        .'<span class="sig">'.substr($body, $sp).'</span>';
909    }
910
911  // colorize quoted lines
912  $a_lines = preg_split('/\n/', $body);
913  foreach ($q_lines as $i => $q)
914    if ($q['quote'])
915      $a_lines[$i] = str_repeat('<blockquote>', $q['quote']) . $a_lines[$i];
916    else if ($q['endquote'])
917      $a_lines[$i] = str_repeat('</blockquote>', $q['endquote']) . $a_lines[$i];
918
919  // insert the links for urls and mailtos
920  $body = $replacements->resolve(join("\n", $a_lines));
921   
922  return $body;
923}
924
925
926/**
927 * add a string to the replacement array and return a replacement string
928 */
929function rcmail_str_replacement($str, &$rep)
930{
931  static $count = 0;
932  $rep[$count] = stripslashes($str);
933  return "##string_replacement{".($count++)."}##";
934}
935
936
937/**
938 * Callback function for washtml cleaning class
939 */
940function rcmail_washtml_callback($tagname, $attrib, $content)
941{
942  switch ($tagname) {
943    case 'form':
944      $out = html::div('form', $content);
945      break;
946     
947    case 'style':
948      // decode all escaped entities and reduce to ascii strings
949      $stripped = preg_replace('/[^a-zA-Z\(:]/', '', rcmail_xss_entity_decode($content));
950     
951      // now check for evil strings like expression, behavior or url()
952      if (!preg_match('/expression|behavior|url\(|import/', $stripped)) {
953        $out = html::tag('style', array('type' => 'text/css'), $content);
954        break;
955      }
956   
957    default:
958      $out = '';
959  }
960 
961  return $out;
962}
963
964
965/**
966 * Callback function for HTML tags fixing
967 */
968function rcmail_html_tag_callback($matches)
969{
970  $tagname = $matches[2];
971
972  $tagname = preg_replace(array(
973    '/:.*$/',           // Microsoft's Smart Tags <st1:xxxx>
974    '/[^a-z0-9_-]/i',   // forbidden characters
975    ), '', $tagname);
976
977  return $matches[1].$tagname;
978}
979
980
981/**
982 * return table with message headers
983 */
984function rcmail_message_headers($attrib, $headers=NULL)
985  {
986  global $IMAP, $OUTPUT, $MESSAGE, $PRINT_MODE, $RCMAIL;
987  static $sa_attrib;
988 
989  // keep header table attrib
990  if (is_array($attrib) && !$sa_attrib)
991    $sa_attrib = $attrib;
992  else if (!is_array($attrib) && is_array($sa_attrib))
993    $attrib = $sa_attrib;
994 
995  if (!isset($MESSAGE))
996    return FALSE;
997
998  // get associative array of headers object
999  if (!$headers)
1000    $headers = is_object($MESSAGE->headers) ? get_object_vars($MESSAGE->headers) : $MESSAGE->headers;
1001
1002  // show these headers
1003  $standard_headers = array('subject', 'from', 'to', 'cc', 'bcc', 'replyto', 'date');
1004  $output_headers = array();
1005
1006  foreach ($standard_headers as $hkey) {
1007    if (!$headers[$hkey])
1008      continue;
1009
1010    if ($hkey == 'date') {
1011      if ($PRINT_MODE)
1012        $header_value = format_date($headers[$hkey], $RCMAIL->config->get('date_long', 'x'));
1013      else
1014        $header_value = format_date($headers[$hkey]);
1015    }
1016    else if ($hkey == 'replyto') {
1017      if ($headers['replyto'] != $headers['from'])
1018        $header_value = rcmail_address_string($headers['replyto'], null, true, $attrib['addicon']);
1019      else
1020        continue;
1021    }
1022    else if (in_array($hkey, array('from', 'to', 'cc', 'bcc')))
1023      $header_value = rcmail_address_string($headers[$hkey], null, true, $attrib['addicon']);
1024    else if ($hkey == 'subject' && empty($headers[$hkey]))
1025      $header_value = rcube_label('nosubject');
1026    else
1027      $header_value = trim($IMAP->decode_header($headers[$hkey]));
1028     
1029    $output_headers[$hkey] = array('title' => rcube_label($hkey), 'value' => $header_value, 'raw' => $headers[$hkey]);
1030  }
1031   
1032  $plugin = $RCMAIL->plugins->exec_hook('message_headers_output', array('output' => $output_headers, 'headers' => $MESSAGE->headers));
1033 
1034  // compose html table
1035  $table = new html_table(array('cols' => 2));
1036 
1037  foreach ($plugin['output'] as $hkey => $row) {
1038    $table->add(array('class' => 'header-title'), Q($row['title']));
1039    $table->add(array('class' => $hkey, 'width' => "90%"), Q($row['value'], ($hkey == 'subject' ? 'strict' : 'show')));
1040  }
1041
1042  // all headers division
1043  $table->add(array('colspan' => 2, 'class' => "more-headers show-headers", 'onclick' => "return ".JS_OBJECT_NAME.".command('load-headers','',this)"), '');
1044  $table->add_row(array('id' => "all-headers"));
1045  $table->add(array('colspan' => 2, 'class' => "all"), html::div(array('id' => 'headers-source'), ''));
1046 
1047  $OUTPUT->add_gui_object('all_headers_row', 'all-headers');
1048  $OUTPUT->add_gui_object('all_headers_box', 'headers-source');
1049
1050  return $table->show($attrib);
1051  }
1052
1053
1054/**
1055 * Handler for the 'messagebody' GUI object
1056 *
1057 * @param array Named parameters
1058 * @return string HTML content showing the message body
1059 */
1060function rcmail_message_body($attrib)
1061  {
1062  global $CONFIG, $OUTPUT, $MESSAGE, $IMAP, $REMOTE_OBJECTS;
1063
1064  if (!is_array($MESSAGE->parts) && empty($MESSAGE->body))
1065    return '';
1066   
1067  if (!$attrib['id'])
1068    $attrib['id'] = 'rcmailMsgBody';
1069
1070  $safe_mode = $MESSAGE->is_safe || intval($_GET['_safe']);
1071  $out = '';
1072 
1073  $header_attrib = array();
1074  foreach ($attrib as $attr => $value)
1075    if (preg_match('/^headertable([a-z]+)$/i', $attr, $regs))
1076      $header_attrib[$regs[1]] = $value;
1077
1078  if (!empty($MESSAGE->parts))
1079    {
1080    foreach ($MESSAGE->parts as $i => $part)
1081      {
1082      if ($part->type == 'headers')
1083        $out .= rcmail_message_headers(sizeof($header_attrib) ? $header_attrib : NULL, $part->headers);
1084      else if ($part->type == 'content' && $part->size)
1085        {
1086        if (empty($part->ctype_parameters) || empty($part->ctype_parameters['charset']))
1087          $part->ctype_parameters['charset'] = $MESSAGE->headers->charset;
1088
1089        // fetch part if not available
1090        if (!isset($part->body))
1091          $part->body = $MESSAGE->get_part_content($part->mime_id);
1092
1093        $body = rcmail_print_body($part, array('safe' => $safe_mode, 'plain' => !$CONFIG['prefer_html']));
1094
1095        if ($part->ctype_secondary == 'html')
1096          $out .= html::div('message-htmlpart', rcmail_html4inline($body, $attrib['id']));
1097        else
1098          $out .= html::div('message-part', $body);
1099        }
1100      }
1101    }
1102  else
1103    $out .= html::div('message-part', html::tag('pre', array(),
1104      rcmail_plain_body(Q($MESSAGE->body, 'strict', false))));
1105
1106  $ctype_primary = strtolower($MESSAGE->structure->ctype_primary);
1107  $ctype_secondary = strtolower($MESSAGE->structure->ctype_secondary);
1108
1109  // list images after mail body
1110  if ($CONFIG['inline_images']
1111      && $ctype_primary == 'multipart'
1112      && !empty($MESSAGE->attachments)
1113      && !strstr($message_body, '<html'))
1114    {
1115    foreach ($MESSAGE->attachments as $attach_prop) {
1116      if (strpos($attach_prop->mimetype, 'image/') === 0) {
1117        $out .= html::tag('hr') . html::p(array('align' => "center"),
1118          html::img(array(
1119            'src' => $MESSAGE->get_part_url($attach_prop->mime_id),
1120            'title' => $attach_prop->filename,
1121            'alt' => $attach_prop->filename,
1122          )));
1123        }
1124    }
1125  }
1126 
1127  // tell client that there are blocked remote objects
1128  if ($REMOTE_OBJECTS && !$safe_mode)
1129    $OUTPUT->set_env('blockedobjects', true);
1130
1131  return html::div($attrib, $out);
1132  }
1133
1134
1135/**
1136 * Convert all relative URLs according to a <base> in HTML
1137 */
1138function rcmail_resolve_base($body)
1139{
1140  // check for <base href=...>
1141  if (preg_match('!(<base.*href=["\']?)([hftps]{3,5}://[a-z0-9/.%-]+)!i', $body, $regs)) {
1142    $replacer = new rcube_base_replacer($regs[2]);
1143
1144    // replace all relative paths
1145    $body = preg_replace_callback('/(src|background|href)=(["\']?)([\.\/]+[^"\'\s]+)(\2|\s|>)/Ui', array($replacer, 'callback'), $body);
1146    $body = preg_replace_callback('/(url\s*\()(["\']?)([\.\/]+[^"\'\)\s]+)(\2)\)/Ui', array($replacer, 'callback'), $body);
1147  }
1148
1149  return $body;
1150}
1151
1152/**
1153 * modify a HTML message that it can be displayed inside a HTML page
1154 */
1155function rcmail_html4inline($body, $container_id)
1156  {
1157  $last_style_pos = 0;
1158  $body_lc = strtolower($body);
1159 
1160  // find STYLE tags
1161  while (($pos = strpos($body_lc, '<style', $last_style_pos)) && ($pos2 = strpos($body_lc, '</style>', $pos)))
1162    {
1163    $pos = strpos($body_lc, '>', $pos)+1;
1164
1165    // replace all css definitions with #container [def]
1166    $styles = rcmail_mod_css_styles(substr($body, $pos, $pos2-$pos), $container_id);
1167
1168    $body = substr($body, 0, $pos) . $styles . substr($body, $pos2);
1169    $body_lc = strtolower($body);
1170    $last_style_pos = $pos2;
1171    }
1172
1173  // modify HTML links to open a new window if clicked
1174  $GLOBALS['rcmail_html_container_id'] = $container_id;
1175  $body = preg_replace_callback('/<(a|link)\s+([^>]+)>/Ui', 'rcmail_alter_html_link', $body);
1176  unset($GLOBALS['rcmail_html_container_id']);
1177
1178  // add comments arround html and other tags
1179  $out = preg_replace(array(
1180      '/(<!DOCTYPE[^>]*>)/i',
1181      '/(<\?xml[^>]*>)/i',
1182      '/(<\/?html[^>]*>)/i',
1183      '/(<\/?head[^>]*>)/i',
1184      '/(<title[^>]*>.*<\/title>)/Ui',
1185      '/(<\/?meta[^>]*>)/i'),
1186    '<!--\\1-->',
1187    $body);
1188
1189  $out = preg_replace(
1190    array('/<body([^>]*)>/i', '/<\/body>/i'),
1191    array('<div class="rcmBody"\\1>', '</div>'),
1192    $out);
1193
1194  // quote <? of php and xml files that are specified as text/html
1195  $out = preg_replace(array('/<\?/', '/\?>/'), array('&lt;?', '?&gt;'), $out);
1196
1197  return $out;
1198  }
1199
1200
1201/**
1202 * parse link attributes and set correct target
1203 */
1204function rcmail_alter_html_link($matches)
1205{
1206  global $EMAIL_ADDRESS_PATTERN;
1207 
1208  $tag = $matches[1];
1209  $attrib = parse_attrib_string($matches[2]);
1210  $end = '>';
1211
1212  if ($tag == 'link' && preg_match('/^https?:\/\//i', $attrib['href'])) {
1213    $attrib['href'] = "./bin/modcss.php?u=" . urlencode($attrib['href']) . "&amp;c=" . urlencode($GLOBALS['rcmail_html_container_id']);
1214    $end = ' />';
1215  }
1216  else if (preg_match("/^mailto:$EMAIL_ADDRESS_PATTERN/i", $attrib['href'], $mailto)) {
1217    $attrib['href'] = $mailto[0];
1218    $attrib['onclick'] = sprintf(
1219      "return %s.command('compose','%s',this)",
1220      JS_OBJECT_NAME,
1221      JQ($mailto[1]));
1222  }
1223  else if (!empty($attrib['href']) && $attrib['href'][0] != '#') {
1224    $attrib['target'] = '_blank';
1225  }
1226
1227  return "<$tag" . html::attrib_string($attrib, array('href','name','target','onclick','id','class','style','title','rel','type','media')) . $end;
1228}
1229
1230
1231/**
1232 * decode address string and re-format it as HTML links
1233 */
1234function rcmail_address_string($input, $max=null, $linked=false, $addicon=null)
1235{
1236  global $IMAP, $PRINT_MODE, $CONFIG, $OUTPUT, $EMAIL_ADDRESS_PATTERN;
1237
1238  $a_parts = $IMAP->decode_address_list($input);
1239
1240  if (!sizeof($a_parts))
1241    return $input;
1242
1243  $c = count($a_parts);
1244  $j = 0;
1245  $out = '';
1246
1247  foreach ($a_parts as $part) {
1248    $j++;
1249    if ($PRINT_MODE) {
1250      $out .= sprintf('%s &lt;%s&gt;', Q($part['name']), $part['mailto']);
1251    }
1252    else if (preg_match("/$EMAIL_ADDRESS_PATTERN/i", $part['mailto'])) {
1253      if ($linked) {
1254        $out .= html::a(array(
1255            'href' => 'mailto:'.$part['mailto'],
1256            'onclick' => sprintf("return %s.command('compose','%s',this)", JS_OBJECT_NAME, JQ($part['mailto'])),
1257            'title' => $part['mailto'],
1258            'class' => "rcmContactAddress",
1259          ),
1260        Q($part['name']));
1261      }
1262      else {
1263        $out .= html::span(array('title' => $part['mailto'], 'class' => "rcmContactAddress"), Q($part['name']));
1264      }
1265
1266      if ($addicon) {
1267        $out .= '&nbsp;' . html::a(array(
1268            'href' => "#add",
1269            'onclick' => sprintf("return %s.command('add-contact','%s',this)", JS_OBJECT_NAME, urlencode($part['string'])),
1270            'title' => rcube_label('addtoaddressbook'),
1271          ),
1272          html::img(array(
1273            'src' => $CONFIG['skin_path'] . $addicon,
1274            'alt' => "Add contact",
1275          )));
1276      }
1277    }
1278    else {
1279      if ($part['name'])
1280        $out .= Q($part['name']);
1281      if ($part['mailto'])
1282        $out .= (strlen($out) ? ' ' : '') . sprintf('&lt;%s&gt;', Q($part['mailto']));
1283    }
1284     
1285    if ($c>$j)
1286      $out .= ','.($max ? '&nbsp;' : ' ');
1287       
1288    if ($max && $j==$max && $c>$j) {
1289      $out .= '...';
1290      break;
1291    }
1292  }
1293   
1294  return $out;
1295}
1296
1297
1298/**
1299 * Wrap text to a given number of characters per line
1300 * but respect the mail quotation of replies messages (>)
1301 *
1302 * @param string Text to wrap
1303 * @param int The line width
1304 * @return string The wrapped text
1305 */
1306function rcmail_wrap_quoted($text, $max = 76)
1307{
1308  // Rebuild the message body with a maximum of $max chars, while keeping quoted message.
1309  $lines = preg_split('/\r?\n/', trim($text));
1310  $out = '';
1311
1312  foreach ($lines as $line) {
1313    if (strlen($line) > $max) {
1314      if (preg_match('/^([>\s]+)/', $line, $regs)) {
1315        $length = strlen($regs[0]);
1316        $prefix = substr($line, 0, $length);
1317
1318        // Remove '> ' from the line, then wordwrap() the line
1319        $line = rc_wordwrap(substr($line, $length), $max - $length);
1320
1321        // Rebuild the line with '> ' at the beginning of each 'subline'
1322        $newline = '';
1323        foreach (explode("\n", $line) as $l) {
1324          $newline .= $prefix . $l . "\n";
1325        }
1326
1327        // Remove the righest newline char
1328        $line = rtrim($newline);
1329      }
1330      else {
1331        $line = rc_wordwrap($line, $max);
1332      }
1333    }
1334
1335    // Append the line
1336    $out .= $line . "\n";
1337  }
1338 
1339  return $out;
1340}
1341
1342
1343function rcmail_message_part_controls()
1344  {
1345  global $MESSAGE;
1346 
1347  $part = asciiwords(get_input_value('_part', RCUBE_INPUT_GPC));
1348  if (!is_object($MESSAGE) || !is_array($MESSAGE->parts) || !($_GET['_uid'] && $_GET['_part']) || !$MESSAGE->mime_parts[$part])
1349    return '';
1350   
1351  $part = $MESSAGE->mime_parts[$part];
1352  $table = new html_table(array('cols' => 3));
1353 
1354  if (!empty($part->filename)) {
1355    $table->add('title', Q(rcube_label('filename')));
1356    $table->add(null, Q($part->filename));
1357    $table->add(null, '[' . html::a('?'.str_replace('_frame=', '_download=', $_SERVER['QUERY_STRING']), Q(rcube_label('download'))) . ']');
1358  }
1359 
1360  if (!empty($part->size)) {
1361    $table->add('title', Q(rcube_label('filesize')));
1362    $table->add(null, Q(show_bytes($part->size)));
1363  }
1364 
1365  return $table->show($attrib);
1366  }
1367
1368
1369
1370function rcmail_message_part_frame($attrib)
1371  {
1372  global $MESSAGE;
1373 
1374  $part = $MESSAGE->mime_parts[asciiwords(get_input_value('_part', RCUBE_INPUT_GPC))];
1375  $ctype_primary = strtolower($part->ctype_primary);
1376
1377  $attrib['src'] = './?' . str_replace('_frame=', ($ctype_primary=='text' ? '_show=' : '_preload='), $_SERVER['QUERY_STRING']);
1378
1379  return html::iframe($attrib);
1380  }
1381
1382
1383/**
1384 * clear message composing settings
1385 */
1386function rcmail_compose_cleanup()
1387  {
1388  if (!isset($_SESSION['compose']))
1389    return;
1390
1391  rcmail::get_instance()->plugins->exec_hook('cleanup_attachments',array());
1392 
1393  rcube_sess_unset('compose');
1394  }
1395 
1396
1397/**
1398 * Send the given message compose object using the configured method
1399 */
1400function rcmail_deliver_message(&$message, $from, $mailto, &$smtp_error)
1401{
1402  global $CONFIG, $RCMAIL;
1403
1404  $msg_body = $message->get();
1405  $headers = $message->headers();
1406
1407  // send thru SMTP server using custom SMTP library
1408  if ($CONFIG['smtp_server']) {
1409    // generate list of recipients
1410    $a_recipients = array($mailto);
1411 
1412    if (strlen($headers['Cc']))
1413      $a_recipients[] = $headers['Cc'];
1414    if (strlen($headers['Bcc']))
1415      $a_recipients[] = $headers['Bcc'];
1416 
1417    // clean Bcc from header for recipients
1418    $send_headers = $headers;
1419    unset($send_headers['Bcc']);
1420    // here too, it because txtHeaders() below use $message->_headers not only $send_headers
1421    unset($message->_headers['Bcc']);
1422
1423    // send message
1424    if (!is_object($RCMAIL->smtp))
1425      $RCMAIL->smtp_init(true);
1426     
1427    $sent = $RCMAIL->smtp->send_mail($from, $a_recipients, ($foo = $message->txtHeaders($send_headers, true)), $msg_body);
1428    $smtp_response = $RCMAIL->smtp->get_response();
1429    $smtp_error = $RCMAIL->smtp->get_error();
1430
1431    // log error
1432    if (!$sent)
1433      raise_error(array('code' => 800, 'type' => 'smtp', 'line' => __LINE__, 'file' => __FILE__,
1434                        'message' => "SMTP error: ".join("\n", $smtp_response)), TRUE, FALSE);
1435  }
1436  // send mail using PHP's mail() function
1437  else {
1438    // unset some headers because they will be added by the mail() function
1439    $headers_enc = $message->headers($headers);
1440    $headers_php = $message->_headers;
1441    unset($headers_php['To'], $headers_php['Subject']);
1442   
1443    // reset stored headers and overwrite
1444    $message->_headers = array();
1445    $header_str = $message->txtHeaders($headers_php);
1446   
1447    // #1485779
1448    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
1449      if (preg_match_all('/<([^@]+@[^>]+)>/', $headers_enc['To'], $m)) {
1450        $headers_enc['To'] = implode(', ', $m[1]);
1451        }
1452      }
1453       
1454    if (ini_get('safe_mode'))
1455      $sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str);
1456    else
1457      $sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str, "-f$from");
1458  }
1459 
1460  if ($sent) {
1461    $RCMAIL->plugins->exec_hook('message_sent', array('headers' => $headers, 'body' => $msg_body));
1462   
1463    // remove MDN headers after sending
1464    unset($headers['Return-Receipt-To'], $headers['Disposition-Notification-To']);
1465   
1466    if ($CONFIG['smtp_log']) {
1467      write_log('sendmail', sprintf("User %s [%s]; Message for %s; %s",
1468        $RCMAIL->user->get_username(),
1469        $_SERVER['REMOTE_ADDR'],
1470        $mailto,
1471        !empty($smtp_response) ? join('; ', $smtp_response) : ''));
1472    }
1473  }
1474 
1475  $message->_headers = array();
1476  $message->headers($headers);
1477 
1478  return $sent;
1479}
1480
1481
1482function rcmail_send_mdn($uid, &$smtp_error)
1483{
1484  global $RCMAIL, $IMAP;
1485
1486  $message = new rcube_message($uid);
1487 
1488  if ($message->headers->mdn_to && !$message->headers->mdn_sent &&
1489    ($IMAP->check_permflag('MDNSENT') || $IMAP->check_permflag('*')))
1490  {
1491    $identity = $RCMAIL->user->get_identity();
1492    $sender = format_email_recipient($identity['email'], $identity['name']);
1493    $recipient = array_shift($IMAP->decode_address_list($message->headers->mdn_to));
1494    $mailto = $recipient['mailto'];
1495
1496    $compose = new rcube_mail_mime($RCMAIL->config->header_delimiter());
1497    $compose->setParam(array(
1498      'text_encoding' => 'quoted-printable',
1499      'html_encoding' => 'quoted-printable',
1500      'head_encoding' => 'quoted-printable',
1501      'head_charset'  => RCMAIL_CHARSET,
1502      'html_charset'  => RCMAIL_CHARSET,
1503      'text_charset'  => RCMAIL_CHARSET,
1504    ));
1505   
1506    // compose headers array
1507    $headers = array(
1508      'Date' => date('r'),
1509      'From' => $sender,
1510      'To'   => $message->headers->mdn_to,
1511      'Subject' => rcube_label('receiptread') . ': ' . $message->subject,
1512      'Message-ID' => sprintf('<%s@%s>', md5(uniqid('rcmail'.mt_rand(),true)), $RCMAIL->config->mail_domain($_SESSION['imap_host'])),
1513      'X-Sender' => $identity['email'],
1514      'Content-Type' => 'multipart/report; report-type=disposition-notification',
1515    );
1516   
1517    if ($agent = $RCMAIL->config->get('useragent'))
1518      $headers['User-Agent'] = $agent;
1519
1520    $body = rcube_label("yourmessage") . "\r\n\r\n" .
1521      "\t" . rcube_label("to") . ': ' . rcube_imap::decode_mime_string($message->headers->to, $message->headers->charset) . "\r\n" .
1522      "\t" . rcube_label("subject") . ': ' . $message->subject . "\r\n" .
1523      "\t" . rcube_label("sent") . ': ' . format_date($message->headers->date, $RCMAIL->config->get('date_long')) . "\r\n" .
1524      "\r\n" . rcube_label("receiptnote") . "\r\n";
1525   
1526    $ua = $RCMAIL->config->get('useragent', "RoundCube Webmail (Version ".RCMAIL_VERSION.")");
1527    $report = "Reporting-UA: $ua\r\n";
1528   
1529    if ($message->headers->to)
1530        $report .= "Original-Recipient: {$message->headers->to}\r\n";
1531   
1532    $report .= "Final-Recipient: rfc822; {$identity['email']}\r\n" .
1533               "Original-Message-ID: {$message->headers->messageID}\r\n" .
1534               "Disposition: manual-action/MDN-sent-manually; displayed\r\n";
1535   
1536    $compose->headers($headers);
1537    $compose->setTXTBody(rc_wordwrap($body, 75, "\r\n"));
1538    $compose->addAttachment($report, 'message/disposition-notification', 'MDNPart2.txt', false, '7bit', 'inline');
1539
1540    $sent = rcmail_deliver_message($compose, $identity['email'], $mailto, $smtp_error);
1541
1542    if ($sent)
1543    {
1544      $IMAP->set_flag($message->uid, 'MDNSENT');
1545      return true;
1546    }
1547  }
1548 
1549  return false;
1550}
1551
1552
1553function rcmail_search_filter($attrib)
1554{
1555  global $OUTPUT, $CONFIG;
1556
1557  if (!strlen($attrib['id']))
1558    $attrib['id'] = 'rcmlistfilter';
1559
1560  $attrib['onchange'] = JS_OBJECT_NAME.'.filter_mailbox(this.value)';
1561 
1562  /*
1563    RFC3501 (6.4.4): 'ALL', 'RECENT',
1564    'ANSWERED', 'DELETED', 'FLAGGED', 'SEEN',
1565    'UNANSWERED', 'UNDELETED', 'UNFLAGGED', 'UNSEEN',
1566    'NEW', // = (RECENT UNSEEN)
1567    'OLD' // = NOT RECENT
1568  */
1569
1570  $select_filter = new html_select($attrib);
1571  $select_filter->add(rcube_label('all'), 'ALL');
1572  $select_filter->add(rcube_label('unread'), 'UNSEEN');
1573  $select_filter->add(rcube_label('flagged'), 'FLAGGED');
1574  $select_filter->add(rcube_label('unanswered'), 'UNANSWERED');
1575  if (!$CONFIG['skip_deleted'])
1576    $select_filter->add(rcube_label('deleted'), 'DELETED');
1577
1578  $out = $select_filter->show($_SESSION['search_filter']);
1579
1580  $OUTPUT->add_gui_object('search_filter', $attrib['id']);
1581
1582  return $out;                                                                         
1583}
1584
1585// register UI objects
1586$OUTPUT->add_handlers(array(
1587  'mailboxlist' => 'rcmail_mailbox_list',
1588  'messages' => 'rcmail_message_list',
1589  'messagecountdisplay' => 'rcmail_messagecount_display',
1590  'quotadisplay' => 'rcmail_quota_display',
1591  'mailboxname' => 'rcmail_mailbox_name_display',
1592  'messageheaders' => 'rcmail_message_headers',
1593  'messagebody' => 'rcmail_message_body',
1594  'messagecontentframe' => 'rcmail_messagecontent_frame',
1595  'messagepartframe' => 'rcmail_message_part_frame',
1596  'messagepartcontrols' => 'rcmail_message_part_controls',
1597  'searchfilter' => 'rcmail_search_filter',
1598  'searchform' => array($OUTPUT, 'search_form'),
1599));
1600
1601?>
Note: See TracBrowser for help on using the repository browser.