svncommit
2008-01-31 1f020b588c1bd215f1d3c713f5dc04b131375b9b
commit | author | age
4e17e6 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/mail/func.inc                                           |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail client                     |
f11541 8  | Copyright (C) 2005-2007, RoundCube Dev. - Switzerland                 |
30233b 9  | Licensed under the GNU GPL                                            |
4e17e6 10  |                                                                       |
T 11  | PURPOSE:                                                              |
12  |   Provide webmail functionality and GUI objects                       |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id$
19
20 */
21
22 require_once('lib/html2text.inc');
23 require_once('lib/enriched.inc');
fba1f5 24 require_once('include/rcube_smtp.inc');
4e17e6 25
T 26
27 $EMAIL_ADDRESS_PATTERN = '/([a-z0-9][a-z0-9\-\.\+\_]*@[a-z0-9]([a-z0-9\-][.]?)*[a-z0-9]\\.[a-z]{2,5})/i';
28
f11541 29 if (empty($_SESSION['mbox']))
c5ac07 30   $_SESSION['mbox'] = $IMAP->get_mailbox_name();
S 31
4e17e6 32 // set imap properties and session vars
b3ce79 33 if ($mbox = get_input_value('_mbox', RCUBE_INPUT_GPC))
c57996 34   $IMAP->set_mailbox(($_SESSION['mbox'] = $mbox));
4e17e6 35
b3ce79 36 if (!empty($_GET['_page']))
c57996 37   $IMAP->set_page(($_SESSION['page'] = intval($_GET['_page'])));
4e17e6 38
fecb03 39 // set mailbox to INBOX if not set
T 40 if (empty($_SESSION['mbox']))
41   $_SESSION['mbox'] = $IMAP->get_mailbox_name();
4e17e6 42
6a35c8 43 // set default sort col/order to session
T 44 if (!isset($_SESSION['sort_col']))
45   $_SESSION['sort_col'] = $CONFIG['message_sort_col'];
46 if (!isset($_SESSION['sort_order']))
47   $_SESSION['sort_order'] = $CONFIG['message_sort_order'];
2bca6e 48
T 49 // set message set for search result
8d0758 50 if (!empty($_REQUEST['_search']) && isset($_SESSION['search'][$_REQUEST['_search']]))
1f020b 51   {
8d0758 52   $IMAP->set_search_set($_SESSION['search'][$_REQUEST['_search']]);
1f020b 53   $OUTPUT->set_env('search_request', $_REQUEST['_search']);
S 54   $OUTPUT->set_env('search_text', $_SESSION['last_text_search']);
55   }
2bca6e 56
6a35c8 57
4e17e6 58 // define url for getting message parts
T 59 if (strlen($_GET['_uid']))
41bece 60   $GET_URL = rcmail_url('get', array('_mbox'=>$IMAP->get_mailbox_name(), '_uid'=>get_input_value('_uid', RCUBE_INPUT_GET)));
4e17e6 61
T 62
63 // set current mailbox in client environment
f11541 64 $OUTPUT->set_env('mailbox', $IMAP->get_mailbox_name());
719804 65 $OUTPUT->set_env('quota', $IMAP->get_capability('quota'));
4e17e6 66
T 67 if ($CONFIG['trash_mbox'])
f11541 68   $OUTPUT->set_env('trash_mailbox', $CONFIG['trash_mbox']);
1966c5 69 if ($CONFIG['drafts_mbox'])
f11541 70   $OUTPUT->set_env('drafts_mailbox', $CONFIG['drafts_mbox']);
b4b081 71 if ($CONFIG['junk_mbox'])
f11541 72   $OUTPUT->set_env('junk_mailbox', $CONFIG['junk_mbox']);
T 73
74 if (!$OUTPUT->ajax_call)
eb6842 75   rcube_add_label('checkingmail', 'deletemessage', 'movemessagetotrash');
f11541 76
5eee00 77 // set page title
T 78 if (empty($_action) || $_action == 'list')
79   $OUTPUT->set_pagetitle(rcube_charset_convert($IMAP->get_mailbox_name(), 'UTF-7'));
80
4e17e6 81
T 82
83 // return the message list as HTML table
84 function rcmail_message_list($attrib)
85   {
f11541 86   global $IMAP, $CONFIG, $COMM_PATH, $OUTPUT;
b076a4 87
4e17e6 88   $skin_path = $CONFIG['skin_path'];
T 89   $image_tag = '<img src="%s%s" alt="%s" border="0" />';
b076a4 90
f3b659 91   // check to see if we have some settings for sorting
6a35c8 92   $sort_col   = $_SESSION['sort_col'];
T 93   $sort_order = $_SESSION['sort_order'];
24053e 94   
T 95   // add some labels to client
96   rcube_add_label('from', 'to');
f3b659 97
4e17e6 98   // get message headers
f3b659 99   $a_headers = $IMAP->list_headers('', '', $sort_col, $sort_order);
4e17e6 100
T 101   // add id to message list table if not specified
102   if (!strlen($attrib['id']))
103     $attrib['id'] = 'rcubemessagelist';
104
105   // allow the following attributes to be added to the <table> tag
106   $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border', 'summary'));
107
108   $out = '<table' . $attrib_str . ">\n";
e0ddd4 109
T 110
4e17e6 111   // define list of cols to be displayed
T 112   $a_show_cols = is_array($CONFIG['list_cols']) ? $CONFIG['list_cols'] : array('subject');
c8c1e0 113   $a_sort_cols = array('subject', 'date', 'from', 'to', 'size');
41bece 114
T 115   $mbox = $IMAP->get_mailbox_name();
4e17e6 116   
T 117   // show 'to' instead of from in sent messages
41bece 118   if (($mbox==$CONFIG['sent_mbox'] || $mbox==$CONFIG['drafts_mbox']) && ($f = array_search('from', $a_show_cols))
8c2e58 119       && !array_search('to', $a_show_cols))
4e17e6 120     $a_show_cols[$f] = 'to';
8c2e58 121   
e0ddd4 122   // add col definition
T 123   $out .= '<colgroup>';
01c86f 124   $out .= '<col class="icon" />';
e0ddd4 125
T 126   foreach ($a_show_cols as $col)
01c86f 127     $out .= sprintf('<col class="%s" />', $col);
e0ddd4 128
01c86f 129   $out .= '<col class="icon" />';
e0ddd4 130   $out .= "</colgroup>\n";
4e17e6 131
T 132   // add table title
133   $out .= "<thead><tr>\n<td class=\"icon\">&nbsp;</td>\n";
b076a4 134
f3b659 135   $javascript = '';
4e17e6 136   foreach ($a_show_cols as $col)
f3b659 137     {
T 138     // get column name
2bca6e 139     $col_name = Q(rcube_label($col));
f3b659 140
T 141     // make sort links
142     $sort = '';
1cded8 143     if ($IMAP->get_capability('sort') && in_array($col, $a_sort_cols))
f3b659 144       {
1cded8 145       // have buttons configured
T 146       if (!empty($attrib['sortdescbutton']) || !empty($attrib['sortascbutton']))
147         {
148         $sort = '&nbsp;&nbsp;';
b076a4 149
1cded8 150         // asc link
T 151         if (!empty($attrib['sortascbutton']))
152           {
f11541 153           $sort .= $OUTPUT->button(array(
T 154             'command' => 'sort',
155             'prop' => $col.'_ASC',
156             'image' => $attrib['sortascbutton'],
157             'align' => 'absmiddle',
158             'title' => 'sortasc'));
1cded8 159           }       
b076a4 160         
1cded8 161         // desc link
T 162         if (!empty($attrib['sortdescbutton']))
163           {
f11541 164           $sort .= $OUTPUT->button(array(
T 165             'command' => 'sort',
166             'prop' => $col.'_DESC',
167             'image' => $attrib['sortdescbutton'],
168             'align' => 'absmiddle',
169             'title' => 'sortdesc'));
1cded8 170           }
T 171         }
172       // just add a link tag to the header
173       else
b076a4 174         {
41bece 175         $col_name = sprintf(
T 176           '<a href="./#sort" onclick="return %s.command(\'sort\',\'%s\',this)" title="%s">%s</a>',
177           JS_OBJECT_NAME,
178           $col,
179           rcube_label('sortby'),
180           $col_name);
b076a4 181         }
f3b659 182       }
b076a4 183       
T 184     $sort_class = $col==$sort_col ? " sorted$sort_order" : '';
f3b659 185
T 186     // put it all together
b076a4 187     $out .= '<td class="'.$col.$sort_class.'" id="rcmHead'.$col.'">' . "$col_name$sort</td>\n";    
f3b659 188     }
4e17e6 189
T 190   $out .= '<td class="icon">'.($attrib['attachmenticon'] ? sprintf($image_tag, $skin_path, $attrib['attachmenticon'], '') : '')."</td>\n";
191   $out .= "</tr></thead>\n<tbody>\n";
192
193   // no messages in this mailbox
194   if (!sizeof($a_headers))
5eee00 195     $OUTPUT->show_message('nomessagesfound', 'notice');
4e17e6 196
T 197
198   $a_js_message_arr = array();
199
200   // create row for each message
201   foreach ($a_headers as $i => $header)  //while (list($i, $header) = each($a_headers))
202     {
203     $message_icon = $attach_icon = '';
204     $js_row_arr = array();
205     $zebra_class = $i%2 ? 'even' : 'odd';
206
207     // set messag attributes to javascript array
6ec0a8 208     if ($header->deleted)
S 209       $js_row_arr['deleted'] = true;
4e17e6 210     if (!$header->seen)
T 211       $js_row_arr['unread'] = true;
212     if ($header->answered)
213       $js_row_arr['replied'] = true;
6ec0a8 214     // set message icon  
S 215     if ($attrib['deletedicon'] && $header->deleted)
216       $message_icon = $attrib['deletedicon'];
217     else if ($attrib['unreadicon'] && !$header->seen)
4e17e6 218       $message_icon = $attrib['unreadicon'];
T 219     else if ($attrib['repliedicon'] && $header->answered)
220       $message_icon = $attrib['repliedicon'];
221     else if ($attrib['messageicon'])
222       $message_icon = $attrib['messageicon'];
223     
f11541 224     // set attachment icon
8d4bcd 225     if ($attrib['attachmenticon'] && preg_match("/multipart\/[mr]/i", $header->ctype))
4e17e6 226       $attach_icon = $attrib['attachmenticon'];
T 227         
15a9d1 228     $out .= sprintf('<tr id="rcmrow%d" class="message%s%s %s">'."\n",
T 229                     $header->uid,
230                     $header->seen ? '' : ' unread',
231                     $header->deleted ? ' deleted' : '',
232                     $zebra_class);    
233     
4e17e6 234     $out .= sprintf("<td class=\"icon\">%s</td>\n", $message_icon ? sprintf($image_tag, $skin_path, $message_icon, '') : '');
f11541 235     
4e17e6 236     // format each col
T 237     foreach ($a_show_cols as $col)
238       {
239       if ($col=='from' || $col=='to')
2bca6e 240         $cont = Q(rcmail_address_string($header->$col, 3, $attrib['addicon']), 'show');
4e17e6 241       else if ($col=='subject')
b4b081 242         {
41bece 243         $action = $mbox==$CONFIG['drafts_mbox'] ? 'compose' : 'show';
T 244         $uid_param = $mbox==$CONFIG['drafts_mbox'] ? '_draf_uid' : '_uid';
f11541 245         $cont = Q(rcube_imap::decode_mime_string($header->$col, $header->charset));
T 246         if (empty($cont)) $cont = Q(rcube_label('nosubject'));
b2fb95 247         $cont = sprintf('<a href="%s" onclick="return rcube_event.cancel(event)">%s</a>', Q(rcmail_url($action, array($uid_param=>$header->uid, '_mbox'=>$mbox))), $cont);
b4b081 248         }
4e17e6 249       else if ($col=='size')
T 250         $cont = show_bytes($header->$col);
251       else if ($col=='date')
f11541 252         $cont = format_date($header->date);
4e17e6 253       else
2bca6e 254         $cont = Q($header->$col);
4e17e6 255         
2bca6e 256       $out .= '<td class="'.$col.'">' . $cont . "</td>\n";
4e17e6 257       }
T 258
259     $out .= sprintf("<td class=\"icon\">%s</td>\n", $attach_icon ? sprintf($image_tag, $skin_path, $attach_icon, '') : '');
260     $out .= "</tr>\n";
261     
262     if (sizeof($js_row_arr))
263       $a_js_message_arr[$header->uid] = $js_row_arr;
264     }
265   
266   // complete message table
267   $out .= "</tbody></table>\n";
268   
269   
270   $message_count = $IMAP->messagecount();
271   
272   // set client env
f11541 273   $OUTPUT->add_gui_object('mailcontframe', 'mailcontframe');
T 274   $OUTPUT->add_gui_object('messagelist', $attrib['id']);
275   $OUTPUT->set_env('messagecount', $message_count);
276   $OUTPUT->set_env('current_page', $IMAP->list_page);
277   $OUTPUT->set_env('pagecount', ceil($message_count/$IMAP->page_size));
278   $OUTPUT->set_env('sort_col', $sort_col);
279   $OUTPUT->set_env('sort_order', $sort_order);
4e17e6 280   
T 281   if ($attrib['messageicon'])
f11541 282     $OUTPUT->set_env('messageicon', $skin_path . $attrib['messageicon']);
6ec0a8 283   if ($attrib['deletedicon'])
f11541 284     $OUTPUT->set_env('deletedicon', $skin_path . $attrib['deletedicon']);
4e17e6 285   if ($attrib['unreadicon'])
f11541 286     $OUTPUT->set_env('unreadicon', $skin_path . $attrib['unreadicon']);
4e17e6 287   if ($attrib['repliedicon'])
f11541 288     $OUTPUT->set_env('repliedicon', $skin_path . $attrib['repliedicon']);
4e17e6 289   if ($attrib['attachmenticon'])
f11541 290     $OUTPUT->set_env('attachmenticon', $skin_path . $attrib['attachmenticon']);
4e17e6 291   
ae895a 292   $OUTPUT->set_env('messages', $a_js_message_arr);
f11541 293   
6b47de 294   $OUTPUT->include_script('list.js');
4e17e6 295   
T 296   return $out;
297   }
298
299
300 // return javascript commands to add rows to the message list
301 function rcmail_js_message_list($a_headers, $insert_top=FALSE)
302   {
f11541 303   global $CONFIG, $IMAP, $OUTPUT;
4e17e6 304
T 305   $a_show_cols = is_array($CONFIG['list_cols']) ? $CONFIG['list_cols'] : array('subject');
41bece 306   $mbox = $IMAP->get_mailbox_name();
4e17e6 307
T 308   // show 'to' instead of from in sent messages
41bece 309   if (($mbox == $CONFIG['sent_mbox'] || $mbox == $CONFIG['drafts_mbox'])
f11541 310       && (($f = array_search('from', $a_show_cols)) !== false) && array_search('to', $a_show_cols) === false)
4e17e6 311     $a_show_cols[$f] = 'to';
T 312
f11541 313   $OUTPUT->command('set_message_coltypes', $a_show_cols);
25d8ba 314
4e17e6 315   // loop through message headers
ecd2e7 316   foreach ($a_headers as $n => $header)
4e17e6 317     {
T 318     $a_msg_cols = array();
319     $a_msg_flags = array();
ecd2e7 320     
T 321     if (empty($header))
322       continue;
f11541 323
4e17e6 324     // format each col; similar as in rcmail_message_list()
T 325     foreach ($a_show_cols as $col)
326       {
327       if ($col=='from' || $col=='to')
2bca6e 328         $cont = Q(rcmail_address_string($header->$col, 3), 'show');
4e17e6 329       else if ($col=='subject')
7bbd5f 330         {
41bece 331         $action = $mbox==$CONFIG['drafts_mbox'] ? 'compose' : 'show';
T 332         $uid_param = $mbox==$CONFIG['drafts_mbox'] ? '_draf_uid' : '_uid';
f11541 333         $cont = Q(rcube_imap::decode_mime_string($header->$col, $header->charset));
7bbd5f 334         if (!$cont) $cont = Q(rcube_label('nosubject'));
b2fb95 335         $cont = sprintf('<a href="%s" onclick="return rcube_event.cancel(event)">%s</a>', Q(rcmail_url($action, array($uid_param=>$header->uid, '_mbox'=>$mbox))), $cont);
7bbd5f 336         }
4e17e6 337       else if ($col=='size')
T 338         $cont = show_bytes($header->$col);
339       else if ($col=='date')
f11541 340         $cont = format_date($header->date);
4e17e6 341       else
2bca6e 342         $cont = Q($header->$col);
4e17e6 343           
T 344       $a_msg_cols[$col] = $cont;
345       }
346
6ec0a8 347     $a_msg_flags['deleted'] = $header->deleted ? 1 : 0;
4e17e6 348     $a_msg_flags['unread'] = $header->seen ? 0 : 1;
T 349     $a_msg_flags['replied'] = $header->answered ? 1 : 0;
f11541 350     $OUTPUT->command('add_message_row',
T 351       $header->uid,
352       $a_msg_cols,
353       $a_msg_flags,
354       preg_match("/multipart\/m/i", $header->ctype),
355       $insert_top);
4e17e6 356     }
T 357   }
358
359
b19097 360 // return an HTML iframe for loading mail content
T 361 function rcmail_messagecontent_frame($attrib)
362   {
f11541 363   global $OUTPUT;
b19097 364   
T 365   if (empty($attrib['id']))
366     $attrib['id'] = 'rcmailcontentwindow';
367
368   // allow the following attributes to be added to the <iframe> tag
369   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'src', 'width', 'height', 'frameborder'));
370   $framename = $attrib['id'];
371
372   $out = sprintf('<iframe name="%s"%s></iframe>'."\n",
373          $framename,
374          $attrib_str);
375
f11541 376   $OUTPUT->set_env('contentframe', $framename);
T 377   $OUTPUT->set_env('blankpage', $attrib['src'] ? $OUTPUT->abs_url($attrib['src']) : 'program/blank.gif');
b19097 378
T 379   return $out;
380   }
381
4e17e6 382
T 383 function rcmail_messagecount_display($attrib)
384   {
f11541 385   global $IMAP, $OUTPUT;
4e17e6 386   
T 387   if (!$attrib['id'])
388     $attrib['id'] = 'rcmcountdisplay';
389
f11541 390   $OUTPUT->add_gui_object('countdisplay', $attrib['id']);
4e17e6 391
T 392   // allow the following attributes to be added to the <span> tag
393   $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id'));
394
395   
396   $out = '<span' . $attrib_str . '>';
397   $out .= rcmail_get_messagecount_text();
398   $out .= '</span>';
399   return $out;
400   }
401
402
58e360 403 function rcmail_quota_display($attrib)
T 404   {
f11541 405   global $OUTPUT, $COMM_PATH;
58e360 406
T 407   if (!$attrib['id'])
408     $attrib['id'] = 'rcmquotadisplay';
409
f11541 410   $OUTPUT->add_gui_object('quotadisplay', $attrib['id']);
58e360 411
T 412   // allow the following attributes to be added to the <span> tag
2bca6e 413   $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id'));
23796e 414
S 415   $out = '<span' . $attrib_str . '>';
416   $out .= rcmail_quota_content($attrib['display']);
417   $out .= '</span>';
418   return $out;
419   }
420
421
422 function rcmail_quota_content($display)
423   {
424   global $IMAP, $COMM_PATH;
3ea0e3 425
4647e1 426   if (!$IMAP->get_capability('QUOTA'))
T 427     $quota_text = rcube_label('unknown');
3ea0e3 428   else if ($quota = $IMAP->get_quota())
T 429     {
430     $quota_text = sprintf("%s / %s (%.0f%%)",
431                           show_bytes($quota["used"] * 1024),
432                           show_bytes($quota["total"] * 1024),
433                           $quota["percent"]);
434
435     // show quota as image (by Brett Patterson)
23796e 436     if ($display == 'image' && function_exists('imagegif'))
3ea0e3 437       {
23796e 438       $attrib = array('width' => 100, 'height' => 14);
f11541 439       $quota_text = sprintf('<img src="./bin/quotaimg.php?u=%s&amp;q=%d&amp;w=%d&amp;h=%d" width="%d" height="%d" alt="%s" title="%s / %s" />',
3ea0e3 440                             $quota['used'], $quota['total'],
fda695 441                             $attrib['width'], $attrib['height'],
T 442                             $attrib['width'], $attrib['height'],
443                             $quota_text,
444                             show_bytes($quota["used"] * 1024),
445                             show_bytes($quota["total"] * 1024));
3ea0e3 446       }
T 447     }
448   else
4647e1 449     $quota_text = rcube_label('unlimited');
58e360 450
23796e 451   return $quota_text;
58e360 452   }
T 453
4e17e6 454
4647e1 455 function rcmail_get_messagecount_text($count=NULL, $page=NULL)
4e17e6 456   {
T 457   global $IMAP, $MESSAGE;
458   
459   if (isset($MESSAGE['index']))
460     {
461     return rcube_label(array('name' => 'messagenrof',
462                              'vars' => array('nr'  => $MESSAGE['index']+1,
4647e1 463                                              'count' => $count!==NULL ? $count : $IMAP->messagecount())));
4e17e6 464     }
31b2ce 465
4647e1 466   if ($page===NULL)
T 467     $page = $IMAP->list_page;
468     
469   $start_msg = ($page-1) * $IMAP->page_size + 1;
470   $max = $count!==NULL ? $count : $IMAP->messagecount();
4e17e6 471
T 472   if ($max==0)
473     $out = rcube_label('mailboxempty');
474   else
475     $out = rcube_label(array('name' => 'messagesfromto',
476                               'vars' => array('from'  => $start_msg,
477                                               'to'    => min($max, $start_msg + $IMAP->page_size - 1),
478                                               'count' => $max)));
479
2bca6e 480   return Q($out);
4e17e6 481   }
T 482
483
8d4bcd 484 function rcmail_print_body($part, $safe=FALSE, $plain=FALSE)
4e17e6 485   {
f11541 486   global $IMAP, $REMOTE_OBJECTS;
4e17e6 487   
8d4bcd 488   $body = is_array($part->replaces) ? strtr($part->body, $part->replaces) : $part->body;
a0109c 489
5cc4b1 490   // convert html to text/plain
T 491   if ($part->ctype_secondary=='html' && $plain)
492     {
493     $txt = new html2text($body, false, true);
494     $body = $txt->get_text();
495     $part->ctype_secondary = 'plain';
496     }
497     
4e17e6 498   // text/html
8d4bcd 499   if ($part->ctype_secondary=='html')
4e17e6 500     {
f7bfec 501     // remove charset specification in HTML message
T 502     $body = preg_replace('/charset=[a-z0-9\-]+/i', '', $body);
503
4e17e6 504     if (!$safe)  // remove remote images and scripts
T 505       {
ea206d 506       $remote_patterns = array('/<img\s+(.*)src=(["\']?)([hftps]{3,5}:\/{2}[^"\'\s]+)(\2|\s|>)/Ui',
T 507                                '/(src|background)=(["\']?)([hftps]{3,5}:\/{2}[^"\'\s]+)(\2|\s|>)/Ui',
4e17e6 508                                '/(<base.*href=["\']?)([hftps]{3,5}:\/{2}[^"\'\s]+)([^<]*>)/i',
T 509                                '/(<link.*href=["\']?)([hftps]{3,5}:\/{2}[^"\'\s]+)([^<]*>)/i',
510                                '/url\s*\(["\']?([hftps]{3,5}:\/{2}[^"\'\s]+)["\']?\)/i',
511                                '/url\s*\(["\']?([\.\/]+[^"\'\s]+)["\']?\)/i',
512                                '/<script.+<\/script>/Umis');
513
ee883a 514       $remote_replaces = array('<img \\1src=\\2./program/blocked.gif\\4',
4e17e6 515                                '',
ea206d 516                                '',
T 517                                '',
4e17e6 518                                'none',
T 519                                'none',
520                                '');
521       
522       // set flag if message containes remote obejcts that where blocked
523       foreach ($remote_patterns as $pattern)
524         {
525         if (preg_match($pattern, $body))
526           {
527           $REMOTE_OBJECTS = TRUE;
528           break;
529           }
530         }
531
532       $body = preg_replace($remote_patterns, $remote_replaces, $body);
533       }
534
2bca6e 535     return Q($body, 'show', FALSE);
4e17e6 536     }
T 537
538   // text/enriched
8d4bcd 539   if ($part->ctype_secondary=='enriched')
4e17e6 540     {
2bca6e 541     return Q(enriched_to_html($body), 'show');
4e17e6 542     }
T 543   else
544     {
545     // make links and email-addresses clickable
546     $convert_patterns = $convert_replaces = $replace_strings = array();
547     
949dea 548     $url_chars = 'a-z0-9_\-\+\*\$\/&%=@#:;';
4e17e6 549     $url_chars_within = '\?\.~,!';
T 550
551     $convert_patterns[] = "/([\w]+):\/\/([a-z0-9\-\.]+[a-z]{2,4}([$url_chars$url_chars_within]*[$url_chars])?)/ie";
20a1b3 552     $convert_replaces[] = "rcmail_str_replacement('<a href=\"\\1://\\2\" target=\"_blank\">\\1://\\2</a>', \$replace_strings)";
4e17e6 553
T 554     $convert_patterns[] = "/([^\/:]|\s)(www\.)([a-z0-9\-]{2,}[a-z]{2,4}([$url_chars$url_chars_within]*[$url_chars])?)/ie";
20a1b3 555     $convert_replaces[] = "rcmail_str_replacement('\\1<a href=\"http://\\2\\3\" target=\"_blank\">\\2\\3</a>', \$replace_strings)";
4e17e6 556     
T 557     $convert_patterns[] = '/([a-z0-9][a-z0-9\-\.\+\_]*@[a-z0-9]([a-z0-9\-][.]?)*[a-z0-9]\\.[a-z]{2,5})/ie';
f11541 558     $convert_replaces[] = "rcmail_str_replacement('<a href=\"mailto:\\1\" onclick=\"return ".JS_OBJECT_NAME.".command(\'compose\',\'\\1\',this)\">\\1</a>', \$replace_strings)";
10c92b 559     
T 560     if ($part->ctype_parameters['format'] != 'flowed')
561       $body = wordwrap(trim($body), 80);
4e17e6 562
T 563     $body = preg_replace($convert_patterns, $convert_replaces, $body);
564
565     // split body into single lines
566     $a_lines = preg_split('/\r?\n/', $body);
10c92b 567     $quote_level = 0;
4e17e6 568
T 569     // colorize quoted parts
570     for($n=0; $n<sizeof($a_lines); $n++)
571       {
572       $line = $a_lines[$n];
10c92b 573       $quotation = '';
T 574       $q = 0;
575       
576       if (preg_match('/^(>+\s*)/', $line, $regs))
577         {
578         $q = strlen(preg_replace('/\s/', '', $regs[1]));
579         $line = substr($line, strlen($regs[1]));
4e17e6 580
10c92b 581         if ($q > $quote_level)
T 582           $quotation = str_repeat('<blockquote>', $q - $quote_level);
583         else if ($q < $quote_level)
584           $quotation = str_repeat("</blockquote>", $quote_level - $q);
585         }
586       else if ($quote_level > 0)
587         $quotation = str_repeat("</blockquote>", $quote_level);
4e17e6 588
10c92b 589       $quote_level = $q;
2bca6e 590       $a_lines[$n] = $quotation . Q($line, 'replace', FALSE);
4e17e6 591       }
T 592
593     // insert the links for urls and mailtos
594     $body = preg_replace("/##string_replacement\{([0-9]+)\}##/e", "\$replace_strings[\\1]", join("\n", $a_lines));
595     
ea206d 596     return "<div class=\"pre\">".$body."\n</div>";
4e17e6 597     }
T 598   }
599
600
601
602 // add a string to the replacement array and return a replacement string
603 function rcmail_str_replacement($str, &$rep)
604   {
605   static $count = 0;
606   $rep[$count] = stripslashes($str);
607   return "##string_replacement{".($count++)."}##";
608   }
609
610
8d4bcd 611 function rcmail_parse_message(&$structure, $arg=array(), $recursive=FALSE)
4e17e6 612   {
T 613   global $IMAP;
614   static $sa_inline_objects = array();
615
616   // arguments are: (bool)$prefer_html, (string)$get_url
617   extract($arg);
618
619   $a_attachments = array();
620   $a_return_parts = array();
621   $out = '';
622
623   $message_ctype_primary = strtolower($structure->ctype_primary);
624   $message_ctype_secondary = strtolower($structure->ctype_secondary);
625
626   // show message headers
627   if ($recursive && is_array($structure->headers) && isset($structure->headers['subject']))
8d4bcd 628     {
T 629     $c = new stdClass;
630     $c->type = 'headers';
631     $c->headers = &$structure->headers;
632     $a_return_parts[] = $c;
633     }
4e17e6 634
T 635   // print body if message doesn't have multiple parts
636   if ($message_ctype_primary=='text')
637     {
8d4bcd 638     $structure->type = 'content';
T 639     $a_return_parts[] = &$structure;
4e17e6 640     }
T 641
642   // message contains alternative parts
643   else if ($message_ctype_primary=='multipart' && $message_ctype_secondary=='alternative' && is_array($structure->parts))
644     {
645     // get html/plaintext parts
646     $plain_part = $html_part = $print_part = $related_part = NULL;
647     
648     foreach ($structure->parts as $p => $sub_part)
649       {
719a25 650       $rel_parts = $attachmnts = null;
4e17e6 651       $sub_ctype_primary = strtolower($sub_part->ctype_primary);
T 652       $sub_ctype_secondary = strtolower($sub_part->ctype_secondary);
653
654       // check if sub part is 
655       if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='plain')
656         $plain_part = $p;
657       else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='html')
658         $html_part = $p;
659       else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='enriched')
660         $enriched_part = $p;
719a25 661       else if ($sub_ctype_primary=='multipart' && ($sub_ctype_secondary=='related' || $sub_ctype_secondary=='mixed'))
4e17e6 662         $related_part = $p;
T 663       }
719a25 664       
4e17e6 665     // parse related part (alternative part could be in here)
719a25 666     if ($related_part!==NULL)
T 667     {
668       list($rel_parts, $attachmnts) = rcmail_parse_message($structure->parts[$related_part], $arg, TRUE);
4e17e6 669       $a_attachments = array_merge($a_attachments, $attachmnts);
719a25 670     }
T 671     
672     // merge related parts if any
673     if ($rel_parts && $prefer_html && !$html_part)
674       $a_return_parts = array_merge($a_return_parts, $rel_parts);
4e17e6 675
719a25 676     // choose html/plain part to print
4e17e6 677     else if ($html_part!==NULL && $prefer_html)
8d4bcd 678       $print_part = &$structure->parts[$html_part];
4e17e6 679     else if ($enriched_part!==NULL)
8d4bcd 680       $print_part = &$structure->parts[$enriched_part];
4e17e6 681     else if ($plain_part!==NULL)
8d4bcd 682       $print_part = &$structure->parts[$plain_part];
4e17e6 683
T 684     // show message body
685     if (is_object($print_part))
8d4bcd 686       {
T 687       $print_part->type = 'content';
688       $a_return_parts[] = $print_part;
689       }
4e17e6 690     // show plaintext warning
719a25 691     else if ($html_part!==NULL && empty($a_return_parts))
8d4bcd 692       {
T 693       $c = new stdClass;
694       $c->type = 'content';
695       $c->body = rcube_label('htmlmessage');
696       $c->ctype_primary = 'text';
697       $c->ctype_secondary = 'plain';
698       
699       $a_return_parts[] = $c;
700       }
4e17e6 701                                 
T 702     // add html part as attachment
703     if ($html_part!==NULL && $structure->parts[$html_part]!==$print_part)
704       {
8d4bcd 705       $html_part = &$structure->parts[$html_part];
T 706       $html_part->filename = rcube_label('htmlmessage');
707       $html_part->mimetype = 'text/html';
708       
709       $a_attachments[] = $html_part;
4e17e6 710       }
T 711     }
712
713   // message contains multiple parts
8d4bcd 714   else if (is_array($structure->parts) && !empty($structure->parts))
4e17e6 715     {
8d4bcd 716     for ($i=0; $i<count($structure->parts); $i++)
4e17e6 717       {
8d4bcd 718       $mail_part = &$structure->parts[$i];
4e17e6 719       $primary_type = strtolower($mail_part->ctype_primary);
T 720       $secondary_type = strtolower($mail_part->ctype_secondary);
721
722       // multipart/alternative
8d4bcd 723       if ($primary_type=='multipart')
4e17e6 724         {
T 725         list($parts, $attachmnts) = rcmail_parse_message($mail_part, $arg, TRUE);
726
727         $a_return_parts = array_merge($a_return_parts, $parts);
728         $a_attachments = array_merge($a_attachments, $attachmnts);
729         }
730
731       // part text/[plain|html] OR message/delivery-status
58e360 732       else if (($primary_type=='text' && ($secondary_type=='plain' || $secondary_type=='html') && $mail_part->disposition!='attachment') ||
fba1f5 733                ($primary_type=='message' && ($secondary_type=='delivery-status' || $secondary_type=='disposition-notification')))
4e17e6 734         {
8d4bcd 735         $mail_part->type = 'content';
T 736         $a_return_parts[] = $mail_part;
4e17e6 737         }
T 738
739       // part message/*
740       else if ($primary_type=='message')
741         {
8d4bcd 742         list($parts, $attachmnts) = rcmail_parse_message($mail_part, $arg, TRUE);
T 743           
4e17e6 744         $a_return_parts = array_merge($a_return_parts, $parts);
T 745         $a_attachments = array_merge($a_attachments, $attachmnts);
746         }
747
748       // part is file/attachment
b595c9 749       else if ($mail_part->disposition=='attachment' || $mail_part->disposition=='inline' || $mail_part->headers['content-id'] ||
5cc4b1 750                (empty($mail_part->disposition) && $mail_part->filename))
4e17e6 751         {
bb8562 752         // skip apple resource forks
8d4bcd 753         if ($message_ctype_secondary=='appledouble' && $secondary_type=='applefile')
T 754           continue;
4e17e6 755
8d4bcd 756         // part belongs to a related message
T 757         if ($message_ctype_secondary=='related' && $mail_part->headers['content-id'])
758           {
759           $mail_part->content_id = preg_replace(array('/^</', '/>$/'), '', $mail_part->headers['content-id']);
760           $sa_inline_objects[] = $mail_part;
761           }
762         // is regular attachment
fd3934 763         else
T 764           {
765           if (!$mail_part->filename)
fba1f5 766             $mail_part->filename = 'Part '.$mail_part->mime_id;
8d4bcd 767           $a_attachments[] = $mail_part;
fd3934 768           }
4e17e6 769         }
T 770       }
771
772     // if this was a related part try to resolve references
773     if ($message_ctype_secondary=='related' && sizeof($sa_inline_objects))
774       {
8d4bcd 775       $a_replaces = array();
4e17e6 776         
T 777       foreach ($sa_inline_objects as $inline_object)
ea206d 778         $a_replaces['cid:'.$inline_object->content_id] = htmlspecialchars(sprintf($get_url, $inline_object->mime_id));
4e17e6 779       
8d4bcd 780       // add replace array to each content part
T 781       // (will be applied later when part body is available)
782       for ($i=0; $i<count($a_return_parts); $i++)
4e17e6 783         {
8d4bcd 784         if ($a_return_parts[$i]->type=='content')
T 785           $a_return_parts[$i]->replaces = $a_replaces;
4e17e6 786         }
T 787       }
788     }
789
04d630 790   // message is single part non-text
5cc4b1 791   else if ($structure->filename)
T 792     $a_attachments[] = $structure;
04d630 793
4e17e6 794   return array($a_return_parts, $a_attachments);
T 795   }
796
797
798
799
800 // return table with message headers
801 function rcmail_message_headers($attrib, $headers=NULL)
802   {
803   global $IMAP, $OUTPUT, $MESSAGE;
804   static $sa_attrib;
805   
806   // keep header table attrib
807   if (is_array($attrib) && !$sa_attrib)
808     $sa_attrib = $attrib;
809   else if (!is_array($attrib) && is_array($sa_attrib))
810     $attrib = $sa_attrib;
811   
812   
813   if (!isset($MESSAGE))
814     return FALSE;
815
816   // get associative array of headers object
817   if (!$headers)
818     $headers = is_object($MESSAGE['headers']) ? get_object_vars($MESSAGE['headers']) : $MESSAGE['headers'];
f11541 819   
4e17e6 820   $header_count = 0;
T 821   
822   // allow the following attributes to be added to the <table> tag
823   $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border', 'summary'));
824   $out = '<table' . $attrib_str . ">\n";
825
826   // show these headers
bde645 827   $standard_headers = array('subject', 'from', 'organization', 'to', 'cc', 'bcc', 'reply-to', 'date');
4e17e6 828   
T 829   foreach ($standard_headers as $hkey)
830     {
831     if (!$headers[$hkey])
832       continue;
833
b076a4 834     if ($hkey=='date' && !empty($headers[$hkey]))
4e17e6 835       $header_value = format_date(strtotime($headers[$hkey]));
bde645 836     else if (in_array($hkey, array('from', 'to', 'cc', 'bcc', 'reply-to')))
2bca6e 837       $header_value = Q(rcmail_address_string($headers[$hkey], NULL, $attrib['addicon']), 'show');
4e17e6 838     else
f11541 839       $header_value = Q(rcube_imap::decode_mime_string($headers[$hkey], $headers['charset']));
4e17e6 840
T 841     $out .= "\n<tr>\n";
2bca6e 842     $out .= '<td class="header-title">'.Q(rcube_label($hkey)).":&nbsp;</td>\n";
4e17e6 843     $out .= '<td class="'.$hkey.'" width="90%">'.$header_value."</td>\n</tr>";
T 844     $header_count++;
845     }
846
847   $out .= "\n</table>\n\n";
848
849   return $header_count ? $out : '';  
850   }
851
852
853
854 function rcmail_message_body($attrib)
855   {
f11541 856   global $CONFIG, $OUTPUT, $MESSAGE, $IMAP, $GET_URL, $REMOTE_OBJECTS;
4e17e6 857   
T 858   if (!is_array($MESSAGE['parts']) && !$MESSAGE['body'])
859     return '';
860     
861   if (!$attrib['id'])
862     $attrib['id'] = 'rcmailMsgBody';
863
c57996 864   $safe_mode = intval($_GET['_safe']);
4e17e6 865   $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id'));
T 866   $out = '<div '. $attrib_str . ">\n";
867   
868   $header_attrib = array();
869   foreach ($attrib as $attr => $value)
870     if (preg_match('/^headertable([a-z]+)$/i', $attr, $regs))
871       $header_attrib[$regs[1]] = $value;
872
873
874   // this is an ecrypted message
875   // -> create a plaintext body with the according message
876   if (!sizeof($MESSAGE['parts']) && $MESSAGE['headers']->ctype=='multipart/encrypted')
877     {
8d4bcd 878     $p = new stdClass;
T 879     $p->type = 'content';
880     $p->ctype_primary = 'text';
881     $p->ctype_secondary = 'plain';
882     $p->body = rcube_label('encryptedmessage');
883     $MESSAGE['parts'][0] = $p;
4e17e6 884     }
T 885   
886   if ($MESSAGE['parts'])
887     {
888     foreach ($MESSAGE['parts'] as $i => $part)
889       {
8d4bcd 890       if ($part->type=='headers')
T 891         $out .= rcmail_message_headers(sizeof($header_attrib) ? $header_attrib : NULL, $part->headers);
892       else if ($part->type=='content')
4e17e6 893         {
8d4bcd 894         if (empty($part->ctype_parameters) || empty($part->ctype_parameters['charset']))
2c9298 895           $part->ctype_parameters['charset'] = $MESSAGE['headers']->charset;
a0109c 896
8d4bcd 897         // fetch part if not available
T 898         if (!isset($part->body))
899           $part->body = $IMAP->get_message_part($MESSAGE['UID'], $part->mime_id, $part);
a0109c 900
5cc4b1 901         $body = rcmail_print_body($part, $safe_mode, !$CONFIG['prefer_html']);
4e17e6 902         $out .= '<div class="message-part">';
a2f2c5 903         
8d4bcd 904         if ($part->ctype_secondary != 'plain')
5cc4b1 905           $out .= rcmail_sanitize_html($body, $attrib['id']);
a2f2c5 906         else
T 907           $out .= $body;
908
4e17e6 909         $out .= "</div>\n";
T 910         }
911       }
912     }
913   else
914     $out .= $MESSAGE['body'];
915
916
917   $ctype_primary = strtolower($MESSAGE['structure']->ctype_primary);
918   $ctype_secondary = strtolower($MESSAGE['structure']->ctype_secondary);
919   
920   // list images after mail body
719a25 921   if (get_boolean($attrib['showimages']) && $ctype_primary=='multipart' &&
T 922       !empty($MESSAGE['attachments']) && !strstr($message_body, '<html') && strlen($GET_URL))
4e17e6 923     {
T 924     foreach ($MESSAGE['attachments'] as $attach_prop)
925       {
8d4bcd 926       if (strpos($attach_prop->mimetype, 'image/')===0)
ea206d 927         $out .= sprintf("\n<hr />\n<p align=\"center\"><img src=\"%s&amp;_part=%s\" alt=\"%s\" title=\"%s\" /></p>\n",
T 928                         htmlspecialchars($GET_URL), $attach_prop->mime_id,
8d4bcd 929                         $attach_prop->filename,
T 930                         $attach_prop->filename);
4e17e6 931       }
T 932     }
933   
934   // tell client that there are blocked remote objects
935   if ($REMOTE_OBJECTS && !$safe_mode)
f11541 936     $OUTPUT->set_env('blockedobjects', true);
4e17e6 937
T 938   $out .= "\n</div>";
939   return $out;
940   }
941
942
943
944 // modify a HTML message that it can be displayed inside a HTML page
5cc4b1 945 function rcmail_sanitize_html($body, $container_id)
4e17e6 946   {
749b07 947   // remove any null-byte characters before parsing
T 948   $body = preg_replace('/\x00/', '', $body);
949   
97bd2c 950   $base_url = "";
4e17e6 951   $last_style_pos = 0;
T 952   $body_lc = strtolower($body);
97bd2c 953   
T 954   // check for <base href>
955   if (preg_match(($base_reg = '/(<base.*href=["\']?)([hftps]{3,5}:\/{2}[^"\'\s]+)([^<]*>)/i'), $body, $base_regs))
956     $base_url = $base_regs[2];
4e17e6 957   
T 958   // find STYLE tags
959   while (($pos = strpos($body_lc, '<style', $last_style_pos)) && ($pos2 = strpos($body_lc, '</style>', $pos)))
960     {
ea206d 961     $pos = strpos($body_lc, '>', $pos)+1;
T 962
4e17e6 963     // replace all css definitions with #container [def]
97bd2c 964     $styles = rcmail_mod_css_styles(substr($body, $pos, $pos2-$pos), $container_id, $base_url);
ea206d 965
3b12ae 966     $body = substr($body, 0, $pos) . $styles . substr($body, $pos2);
S 967     $body_lc = strtolower($body);
4e17e6 968     $last_style_pos = $pos2;
T 969     }
970
971
972   // remove SCRIPT tags
6a35c8 973   foreach (array('script', 'applet', 'object', 'embed', 'iframe') as $tag)
4e17e6 974     {
a08a60 975     while (($pos = strpos($body_lc, '<'.$tag)) && (($pos2 = strpos($body_lc, '</'.$tag.'>', $pos)) || ($pos3 = strpos($body_lc, '>', $pos))))
6a35c8 976       {
a08a60 977       $end = $pos2 ? $pos2 + strlen('</'.$tag.'>') : $pos3 + 1;
T 978       $body = substr($body, 0, $pos) . substr($body, $end, strlen($body)-$end);
6a35c8 979       $body_lc = strtolower($body);
T 980       }
4e17e6 981     }
6a35c8 982
T 983   // replace event handlers on any object
5e98e1 984   while ($body != $prev_body)
S 985     {
986     $prev_body = $body;
18e2a3 987     $body = preg_replace('/(<[^!][^>]*\s)(on[^=>]+)=([^>]+>)/im', '$1__removed=$3', $body);
T 988     $body = preg_replace('/(<[^!][^>]*\shref=["\']?)(javascript:)([^>]*?>)/im', '$1null:$3', $body);
5e98e1 989     }
4e17e6 990
T 991   // resolve <base href>
97bd2c 992   if ($base_url)
4e17e6 993     {
T 994     $body = preg_replace('/(src|background|href)=(["\']?)([\.\/]+[^"\'\s]+)(\2|\s|>)/Uie', "'\\1=\"'.make_absolute_url('\\3', '$base_url').'\"'", $body);
995     $body = preg_replace('/(url\s*\()(["\']?)([\.\/]+[^"\'\)\s]+)(\2)\)/Uie', "'\\1\''.make_absolute_url('\\3', '$base_url').'\')'", $body);
996     $body = preg_replace($base_reg, '', $body);
997     }
fe79b1 998     
T 999   // modify HTML links to open a new window if clicked
97bd2c 1000   $body = preg_replace('/<(a|link)\s+([^>]+)>/Uie', "rcmail_alter_html_link('\\1','\\2', '$container_id');", $body);
4e17e6 1001
T 1002   // add comments arround html and other tags
06895c 1003   $out = preg_replace(array(
T 1004       '/(<!DOCTYPE.+)/i',
1005       '/(<\/?html[^>]*>)/i',
1006       '/(<\/?head[^>]*>)/i',
1007       '/(<title[^>]*>.*<\/title>)/Ui',
1008       '/(<\/?meta[^>]*>)/i'),
1009     '<!--\\1-->',
1010     $body);
a0109c 1011
97bd2c 1012   $out = preg_replace(
T 1013     array(
1014       '/<body([^>]*)>/i',
1015       '/<\/body>/i',
1016     ),
1017     array(
1018       '<div class="rcmBody"\\1>',
1019       '</div>',
1020     ),
1021     $out);
a0109c 1022
86958f 1023   // quote <? of php and xml files that are specified as text/html
T 1024   $out = preg_replace(array('/<\?/', '/\?>/'), array('&lt;?', '?&gt;'), $out);
1025
4e17e6 1026   return $out;
T 1027   }
1028
1029
fe79b1 1030 // parse link attributes and set correct target
97bd2c 1031 function rcmail_alter_html_link($tag, $attrs, $container_id)
fe79b1 1032   {
3cf664 1033   $in = preg_replace('/=([^("|\'|\s)]+)(\s|$)/', '="\1"', $in);
97bd2c 1034   $attrib = parse_attrib_string($attrs);
T 1035   
1036   if ($tag == 'link' && preg_match('/^https?:\/\//i', $attrib['href']))
1037     $attrib['href'] = "./bin/modcss.php?u=" . urlencode($attrib['href']) . "&amp;c=" . urlencode($container_id);
fe79b1 1038
97bd2c 1039   else if (stristr((string)$attrib['href'], 'mailto:'))
T 1040     $attrib['onclick'] = sprintf(
1041       "return %s.command('compose','%s',this)",
1042       JS_OBJECT_NAME,
1043       JQ(substr($attrib['href'], 7)));
1044   
fe79b1 1045   else if (!empty($attrib['href']) && $attrib['href']{0}!='#')
T 1046     $attrib['target'] = '_blank';
1047
97bd2c 1048   return "<$tag" . create_attrib_string($attrib, array('href','name','target','onclick','id','class','style','title','rel','type','media')) . ' />';
4e17e6 1049   }
T 1050
1051
a0109c 1052 function rcmail_has_html_part($message_parts)
S 1053 {
1054    if (!is_array($message_parts))
1055       return FALSE;
1056
1057    // check all message parts
1058    foreach ($message_parts as $pid => $part)
1059    {
1060       $mimetype = strtolower($part->ctype_primary.'/'.$part->ctype_secondary);
1061       if ($mimetype=='text/html')
1062       {
1063          return TRUE;
1064       }
1065    }
1066     
1067    return FALSE;
1068 }
1069
1070 // return first HTML part of a message
1071 function rcmail_first_html_part($message_struct)
1072   {
1073   global $IMAP;
1074
1075   if (!is_array($message_struct['parts']))
1076     return FALSE;
1077     
1078   $html_part = NULL;
1079
1080   // check all message parts
1081   foreach ($message_struct['parts'] as $pid => $part)
1082     {
1083     $mimetype = strtolower($part->ctype_primary.'/'.$part->ctype_secondary);
1084     if ($mimetype=='text/html')
1085       {
1086       $html_part = $IMAP->get_message_part($message_struct['UID'], $pid, $part);
1087       }
1088     }
1089
1090   if ($html_part)
1091     {
1092     // remove special chars encoding
1093     //$trans = array_flip(get_html_translation_table(HTML_ENTITIES));
1094     //$html_part = strtr($html_part, $trans);
1095
1096     return $html_part;
1097     }
1098
1099   return FALSE;
1100 }
1101
4e17e6 1102
T 1103 // return first text part of a message
8d4bcd 1104 function rcmail_first_text_part($message_struct)
4e17e6 1105   {
8d4bcd 1106   global $IMAP;
T 1107
e170b4 1108   if (empty($message_struct['parts']))
T 1109     return $message_struct['UID'] ? $IMAP->get_body($message_struct['UID']) : false;
1110
4e17e6 1111   // check all message parts
8d4bcd 1112   foreach ($message_struct['parts'] as $pid => $part)
4e17e6 1113     {
T 1114     $mimetype = strtolower($part->ctype_primary.'/'.$part->ctype_secondary);
8d4bcd 1115
4e17e6 1116     if ($mimetype=='text/plain')
8d4bcd 1117       return $IMAP->get_message_part($message_struct['UID'], $pid, $part);
T 1118
4e17e6 1119     else if ($mimetype=='text/html')
T 1120       {
8d4bcd 1121       $html_part = $IMAP->get_message_part($message_struct['UID'], $pid, $part);
T 1122       
1123       // remove special chars encoding
1124       $trans = array_flip(get_html_translation_table(HTML_ENTITIES));
1125       $html_part = strtr($html_part, $trans);
1126
1127       // create instance of html2text class
1128       $txt = new html2text($html_part);
1129       return $txt->get_text();
4e17e6 1130       }
T 1131     }
1132
1133   return FALSE;
1134   }
1135
1136
1137 // decode address string and re-format it as HTML links
1138 function rcmail_address_string($input, $max=NULL, $addicon=NULL)
1139   {
f11541 1140   global $IMAP, $PRINT_MODE, $CONFIG, $OUTPUT, $EMAIL_ADDRESS_PATTERN;
4e17e6 1141   
T 1142   $a_parts = $IMAP->decode_address_list($input);
1143
1144   if (!sizeof($a_parts))
1145     return $input;
1146
1147   $c = count($a_parts);
1148   $j = 0;
1149   $out = '';
1150
1151   foreach ($a_parts as $part)
1152     {
1153     $j++;
1154     if ($PRINT_MODE)
2bca6e 1155       $out .= sprintf('%s &lt;%s&gt;', Q($part['name']), $part['mailto']);
4e17e6 1156     else if (preg_match($EMAIL_ADDRESS_PATTERN, $part['mailto']))
T 1157       {
1158       $out .= sprintf('<a href="mailto:%s" onclick="return %s.command(\'compose\',\'%s\',this)" class="rcmContactAddress" title="%s">%s</a>',
28bfe4 1159                       Q($part['mailto']),
f11541 1160                       JS_OBJECT_NAME,
28bfe4 1161                       JQ($part['mailto']),
T 1162                       Q($part['mailto']),
2bca6e 1163                       Q($part['name']));
4e17e6 1164                       
T 1165       if ($addicon)
1166         $out .= sprintf('&nbsp;<a href="#add" onclick="return %s.command(\'add-contact\',\'%s\',this)" title="%s"><img src="%s%s" alt="add" border="0" /></a>',
f11541 1167                         JS_OBJECT_NAME,
4e17e6 1168                         urlencode($part['string']),
T 1169                         rcube_label('addtoaddressbook'),
1170                         $CONFIG['skin_path'],
1171                         $addicon);
1172       }
1173     else
1174       {
1175       if ($part['name'])
2bca6e 1176         $out .= Q($part['name']);
4e17e6 1177       if ($part['mailto'])
28bfe4 1178         $out .= (strlen($out) ? ' ' : '') . sprintf('&lt;%s&gt;', Q($part['mailto']));
4e17e6 1179       }
T 1180       
1181     if ($c>$j)
1182       $out .= ','.($max ? '&nbsp;' : ' ');
1183         
1184     if ($max && $j==$max && $c>$j)
1185       {
1186       $out .= '...';
1187       break;
1188       }        
1189     }
1190     
1191   return $out;
1192   }
1193
1194
1195 function rcmail_message_part_controls()
1196   {
1197   global $CONFIG, $IMAP, $MESSAGE;
1198   
d5342a 1199   $part = asciiwords(get_input_value('_part', RCUBE_INPUT_GPC));
c57996 1200   if (!is_array($MESSAGE) || !is_array($MESSAGE['parts']) || !($_GET['_uid'] && $_GET['_part']) || !$MESSAGE['parts'][$part])
4e17e6 1201     return '';
T 1202     
c57996 1203   $part = &$MESSAGE['parts'][$part];
4e17e6 1204   
T 1205   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'cellspacing', 'cellpadding', 'border', 'summary'));
1206   $out = '<table '. $attrib_str . ">\n";
1207   
1208   if ($filename)
1209     {
1210     $out .= sprintf('<tr><td class="title">%s</td><td>%s</td><td>[<a href="./?%s">%s</a>]</tr>'."\n",
2bca6e 1211                     Q(rcube_label('filename')),
5cc4b1 1212                     Q($part->filename),
4e17e6 1213                     str_replace('_frame=', '_download=', $_SERVER['QUERY_STRING']),
2bca6e 1214                     Q(rcube_label('download')));
4e17e6 1215     }
T 1216     
5cc4b1 1217   if ($part->size)
4e17e6 1218     $out .= sprintf('<tr><td class="title">%s</td><td>%s</td></tr>'."\n",
2bca6e 1219                     Q(rcube_label('filesize')),
5cc4b1 1220                     show_bytes($part->size));
4e17e6 1221   
T 1222   $out .= "\n</table>";
1223   
1224   return $out;
1225   }
1226
1227
1228
1229 function rcmail_message_part_frame($attrib)
1230   {
1231   global $MESSAGE;
1232   
d5342a 1233   $part = $MESSAGE['parts'][asciiwords(get_input_value('_part', RCUBE_INPUT_GPC))];
4e17e6 1234   $ctype_primary = strtolower($part->ctype_primary);
T 1235
719a25 1236   $attrib['src'] = Q('./?'.str_replace('_frame=', ($ctype_primary=='text' ? '_show=' : '_preload='), $_SERVER['QUERY_STRING']));
4e17e6 1237
T 1238   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'src', 'width', 'height'));
90022e 1239   $out = '<iframe '. $attrib_str . "></iframe>";
4e17e6 1240     
T 1241   return $out;
1242   }
1243
1244
1245 // clear message composing settings
1246 function rcmail_compose_cleanup()
1247   {
1248   if (!isset($_SESSION['compose']))
1249     return;
70d4b9 1250
4e17e6 1251   // remove attachment files from temp dir
T 1252   if (is_array($_SESSION['compose']['attachments']))
1253     foreach ($_SESSION['compose']['attachments'] as $attachment)
15a9d1 1254       @unlink($attachment['path']);
4e17e6 1255   
T 1256   unset($_SESSION['compose']);
1257   }
fba1f5 1258   
T 1259
1260 /**
1261  * Send the given message compose object using the configured method
1262  */
1263 function rcmail_deliver_message(&$message, $from, $mailto)
1264 {
1265   global $CONFIG;
1266
1267   $headers = $message->headers();
1268   $msg_body = $message->get();
1269   
1270   // send thru SMTP server using custom SMTP library
1271   if ($CONFIG['smtp_server'])
1272     {
1273     // generate list of recipients
1274     $a_recipients = array($mailto);
1275   
1276     if (strlen($headers['Cc']))
1277       $a_recipients[] = $headers['Cc'];
1278     if (strlen($headers['Bcc']))
1279       $a_recipients[] = $headers['Bcc'];
1280   
1281     // clean Bcc from header for recipients
1282     $send_headers = $headers;
1283     unset($send_headers['Bcc']);
1284
1285     // send message
1286     $smtp_response = array();
1287     $sent = smtp_mail($from, $a_recipients, ($foo = $message->txtHeaders($send_headers)), $msg_body, $smtp_response);
1288
1289     // log error
1290     if (!$sent)
1291       raise_error(array('code' => 800, 'type' => 'smtp', 'line' => __LINE__, 'file' => __FILE__,
1292                         'message' => "SMTP error: ".join("\n", $smtp_response)), TRUE, FALSE);
1293     }
1294   
1295   // send mail using PHP's mail() function
1296   else
1297     {
1298     // unset some headers because they will be added by the mail() function
1299     $headers_enc = $message->headers($headers);
1300     $headers_php = $message->_headers;
1301     unset($headers_php['To'], $headers_php['Subject']);
1302     
1303     // reset stored headers and overwrite
1304     $message->_headers = array();
1305     $header_str = $message->txtHeaders($headers_php);
1306   
1307     if (ini_get('safe_mode'))
1308       $sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str);
1309     else
1310       $sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str, "-f$from");
1311     }
1312   
1313   
1314   $message->_headers = array();
1315   $message->headers($headers);
1316   
1317   return $sent;
1318 }
f11541 1319
T 1320
1321 // register UI objects
1322 $OUTPUT->add_handlers(array(
1323   'mailboxlist' => 'rcmail_mailbox_list',
1324   'messages' => 'rcmail_message_list',
1325   'messagecountdisplay' => 'rcmail_messagecount_display',
1326   'quotadisplay' => 'rcmail_quota_display',
1327   'messageheaders' => 'rcmail_message_headers',
1328   'messagebody' => 'rcmail_message_body',
1329   'messagecontentframe' => 'rcmail_messagecontent_frame',
1330   'messagepartframe' => 'rcmail_message_part_frame',
1331   'messagepartcontrols' => 'rcmail_message_part_controls',
1332   'searchform' => 'rcmail_search_form'
1333 ));
1334
93be5b 1335 ?>