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