svncommit
2005-10-27 66773789e392305bba4cdf7ed8e6ae3b8380de51
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                     |
8  | Copyright (C) 2005, 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');
a95e0e 24 require_once('lib/utf8.inc');
T 25 require_once('lib/utf7.inc');
4e17e6 26
T 27
28 $EMAIL_ADDRESS_PATTERN = '/([a-z0-9][a-z0-9\-\.\+\_]*@[a-z0-9]([a-z0-9\-][.]?)*[a-z0-9]\\.[a-z]{2,5})/i';
29
30 // set imap properties and session vars
31 if (strlen($_GET['_mbox']))
32   {
33   $IMAP->set_mailbox($_GET['_mbox']);
34   $_SESSION['mbox'] = $_GET['_mbox'];
35   }
36
37 if (strlen($_GET['_page']))
38   {
39   $IMAP->set_page($_GET['_page']);
40   $_SESSION['page'] = $_GET['_page'];
41   }
42
43
44 // define url for getting message parts
45 if (strlen($_GET['_uid']))
46   $GET_URL = sprintf('%s&_action=get&_mbox=%s&_uid=%d', $COMM_PATH, $IMAP->get_mailbox_name(), $_GET['_uid']);
47
48
49 // set current mailbox in client environment
50 $OUTPUT->add_script(sprintf("%s.set_env('mailbox', '%s');", $JS_OBJECT_NAME, $IMAP->get_mailbox_name()));
51
52
53 if ($CONFIG['trash_mbox'])
54   $OUTPUT->add_script(sprintf("%s.set_env('trash_mailbox', '%s');", $JS_OBJECT_NAME, $CONFIG['trash_mbox']));
55
56
57
58 // return the mailboxlist in HTML
59 function rcmail_mailbox_list($attrib)
60   {
61   global $IMAP, $CONFIG, $OUTPUT, $JS_OBJECT_NAME, $COMM_PATH;
62   static $s_added_script = FALSE;
597170 63   static $a_mailboxes;
4e17e6 64   
T 65   $type = $attrib['type'] ? $attrib['type'] : 'ul';
66   $add_attrib = $type=='select' ? array('style', 'class', 'id', 'name', 'onchange') :
67                                   array('style', 'class', 'id');
68                                   
69   if ($type=='ul' && !$attrib['id'])
70     $attrib['id'] = 'rcmboxlist';
71
72   // allow the following attributes to be added to the <ul> tag
73   $attrib_str = create_attrib_string($attrib, $add_attrib);
74  
75   $out = '<' . $type . $attrib_str . ">\n";
76   
77   // add no-selection option
78   if ($type=='select' && $attrib['noselection'])
79     $out .= sprintf('<option value="0">%s</option>'."\n",
80                     rcube_label($attrib['noselection']));
81   
82   // get mailbox list
83   $mbox = $IMAP->get_mailbox_name();
84   
85   // for these mailboxes we have localized labels
86   $special_mailboxes = array('inbox', 'sent', 'drafts', 'trash', 'junk');
87
597170 88
T 89   // build the folders tree
90   if (empty($a_mailboxes))
91     {
92     // get mailbox list
93     $a_folders = $IMAP->list_mailboxes();
94     $delimiter = $IMAP->get_hierarchy_delimiter();
95     $a_mailboxes = array();
96     
97     foreach ($a_folders as $folder)
98       rcmail_build_folder_tree($a_mailboxes, $folder, $delimiter);
99     }
100
101 // var_dump($a_mailboxes);
102
103   if ($type=='select')
cd900d 104     $out .= rcmail_render_folder_tree_select($a_mailboxes, $special_mailboxes, $mbox, $attrib['maxlength']);
597170 105    else
cd900d 106     $out .= rcmail_render_folder_tree_html($a_mailboxes, $special_mailboxes, $mbox, $attrib['maxlength']);
597170 107
4e17e6 108
T 109   if ($type=='ul')
110     $OUTPUT->add_script(sprintf("%s.gui_object('mailboxlist', '%s');", $JS_OBJECT_NAME, $attrib['id']));
111
112   return $out . "</$type>";
597170 113   }
T 114
115
116
117
118 // create a hierarchical array of the mailbox list
119 function rcmail_build_folder_tree(&$arrFolders, $folder, $delm='/', $path='')
120   {
121   $pos = strpos($folder, $delm);
122   if ($pos !== false)
123     {
124     $subFolders = substr($folder, $pos+1);
125     $currentFolder = substr($folder, 0, $pos);
126     }
127   else
128     {
129     $subFolders = false;
130     $currentFolder = $folder;
131     }
132
133   $path .= $currentFolder;
134
135   if (!isset($arrFolders[$currentFolder]))
136     {
137     $arrFolders[$currentFolder] = array('id' => $path,
138                                         'name' => $currentFolder,
139                                         'folders' => array());
140     }
141
142   if (!empty($subFolders))
143     rcmail_build_folder_tree($arrFolders[$currentFolder]['folders'], $subFolders, $delm, $path.$delm);
144   }
145   
146
147 // return html for a structured list <ul> for the mailbox tree
cd900d 148 function rcmail_render_folder_tree_html(&$arrFolders, &$special, &$mbox, $maxlength, $nestLevel=0)
597170 149   {
T 150   global $JS_OBJECT_NAME, $IMAP;
151
152   $idx = 0;
153   $out = '';
154   foreach ($arrFolders as $key => $folder)
155     {
156     $zebra_class = ($nestLevel*$idx)%2 ? 'even' : 'odd';
157
158     $folder_lc = strtolower($folder['id']);
159     if (in_array($folder_lc, $special))
160       $foldername = rcube_label($folder_lc);
161     else
a95e0e 162       {
T 163       $foldername = UTF7DecodeString($folder['name']);
597170 164
a95e0e 165       // shorten the folder name to a given length
T 166       if ($maxlength && $maxlength>1)
167         $foldername = abbrevate_string($foldername, $maxlength);
168       }
cd900d 169
a95e0e 170     // add unread message count display
cd900d 171     if ($unread_count = $IMAP->messagecount($folder['id'], 'UNSEEN', ($folder['id']==$mbox)))
597170 172       $foldername .= sprintf(' (%d)', $unread_count);
T 173
174     $out .= sprintf('<li class="mailbox %s %s%s%s"><a href="#%s" onclick="return %s.command(\'list\',\'%s\')" onmouseup="return %s.mbox_mouse_up(\'%s\')">%s</a>'."\n",
175                     preg_replace('/[^a-z0-9\-_]/', '', $folder_lc),
176                     $zebra_class,
177                     $unread_count ? ' unread' : '',
178                     $folder['id']==$mbox ? ' selected' : '',
179                     $folder['id'],
180                     $JS_OBJECT_NAME,
181                     $folder['id'],
182                     $JS_OBJECT_NAME,
183                     $folder['id'],
a95e0e 184                     rep_specialchars_output($foldername, 'html', 'all'));
597170 185
T 186     if (!empty($folder['folders']))
cd900d 187       $out .= '<ul>' . rcmail_render_folder_tree_html($folder['folders'], $special, $mbox, $maxlength, $nestLevel+1) . "</ul>\n";
597170 188
T 189     $out .= "</li>\n";
190     $idx++;
191     }
192
193   return $out;
194   }
195
196
197 // return html for a flat list <select> for the mailbox tree
cd900d 198 function rcmail_render_folder_tree_select(&$arrFolders, &$special, &$mbox, $maxlength, $nestLevel=0)
597170 199   {
T 200   global $IMAP;
201
202   $idx = 0;
203   $out = '';
204   foreach ($arrFolders as $key=>$folder)
205     {
7902df 206     $folder_lc = strtolower($folder['id']);
T 207     if (in_array($folder_lc, $special))
208       $foldername = rcube_label($folder_lc);
cd900d 209     else
a95e0e 210       {
T 211       $foldername = UTF7DecodeString($folder['name']);
212       
213       // shorten the folder name to a given length
214       if ($maxlength && $maxlength>1)
215         $foldername = abbrevate_string($foldername, $maxlength);
216       }
cd900d 217
597170 218     $out .= sprintf('<option value="%s">%s%s</option>'."\n",
T 219                     $folder['id'],
220                     str_repeat('&nbsp;', $nestLevel*4),
a95e0e 221                     rep_specialchars_output($foldername, 'html', 'all'));
597170 222
T 223     if (!empty($folder['folders']))
cd900d 224       $out .= rcmail_render_folder_tree_select($folder['folders'], $special, $mbox, $maxlength, $nestLevel+1);
597170 225
T 226     $idx++;
227     }
228
229   return $out;
4e17e6 230   }
T 231
232
233 // return the message list as HTML table
234 function rcmail_message_list($attrib)
235   {
236   global $IMAP, $CONFIG, $COMM_PATH, $OUTPUT, $JS_OBJECT_NAME;
b076a4 237
4e17e6 238   $skin_path = $CONFIG['skin_path'];
T 239   $image_tag = '<img src="%s%s" alt="%s" border="0" />';
b076a4 240
f3b659 241   // check to see if we have some settings for sorting
b076a4 242   $sort_col   = isset($_SESSION['sort_col'])   ? $_SESSION['sort_col']   : $CONFIG['message_sort_col'];
T 243   $sort_order = isset($_SESSION['sort_order']) ? $_SESSION['sort_order'] : $CONFIG['message_sort_order'];
f3b659 244
4e17e6 245   // get message headers
f3b659 246   $a_headers = $IMAP->list_headers('', '', $sort_col, $sort_order);
4e17e6 247
T 248   // add id to message list table if not specified
249   if (!strlen($attrib['id']))
250     $attrib['id'] = 'rcubemessagelist';
251
252   // allow the following attributes to be added to the <table> tag
253   $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border', 'summary'));
254
255   $out = '<table' . $attrib_str . ">\n";
256   
257   // define list of cols to be displayed
258   $a_show_cols = is_array($CONFIG['list_cols']) ? $CONFIG['list_cols'] : array('subject');
b076a4 259   $a_sort_cols = array('subject', 'date', 'from', 'to');
4e17e6 260   
T 261   // show 'to' instead of from in sent messages
262   if (strtolower($IMAP->get_mailbox_name())=='sent' && ($f = array_search('from', $a_show_cols)))
263     $a_show_cols[$f] = 'to';
264
265
266   // add table title
267   $out .= "<thead><tr>\n<td class=\"icon\">&nbsp;</td>\n";
b076a4 268
f3b659 269   $javascript = '';
4e17e6 270   foreach ($a_show_cols as $col)
f3b659 271     {
T 272     // get column name
273     $col_name = rep_specialchars_output(rcube_label($col));
274
275     // make sort links
276     $sort = '';
b076a4 277     if (in_array($col, $a_sort_cols) && (!empty($attrib['sortdescbutton']) || !empty($attrib['sortascbutton'])))
f3b659 278       {
b076a4 279       $sort = '&nbsp;&nbsp;';
T 280
f3b659 281       // asc link
b076a4 282       if (!empty($attrib['sortascbutton']))
T 283         {
284         $sort .= rcube_button(array('command' => 'sort',
285                                     'prop' => $col.'_ASC',
286                                     'image' => $attrib['sortascbutton'],
287                                     'title' => 'sortasc'));
288         }        
289         
f3b659 290       // desc link
b076a4 291       if (!empty($attrib['sortdescbutton']))
T 292         {
293         $sort .= rcube_button(array('command' => 'sort',
294                                     'prop' => $col.'_DESC',
295                                     'image' => $attrib['sortdescbutton'],
296                                     'title' => 'sortdesc'));        
297         }
f3b659 298       }
b076a4 299       
T 300     $sort_class = $col==$sort_col ? " sorted$sort_order" : '';
f3b659 301
T 302     // put it all together
b076a4 303     $out .= '<td class="'.$col.$sort_class.'" id="rcmHead'.$col.'">' . "$col_name$sort</td>\n";    
f3b659 304     }
4e17e6 305
T 306   $out .= '<td class="icon">'.($attrib['attachmenticon'] ? sprintf($image_tag, $skin_path, $attrib['attachmenticon'], '') : '')."</td>\n";
307   $out .= "</tr></thead>\n<tbody>\n";
308
309
310   // no messages in this mailbox
311   if (!sizeof($a_headers))
312     {
122329 313     $out .= rep_specialchars_output(
S 314                 sprintf('<tr><td colspan="%d">%s</td></tr>',
4e17e6 315                    sizeof($a_show_cols)+2,
122329 316                    rcube_label('nomessagesfound')));
4e17e6 317     }
T 318
319
320   $a_js_message_arr = array();
321
322   // create row for each message
323   foreach ($a_headers as $i => $header)  //while (list($i, $header) = each($a_headers))
324     {
325     $message_icon = $attach_icon = '';
326     $js_row_arr = array();
327     $zebra_class = $i%2 ? 'even' : 'odd';
328
329     // set messag attributes to javascript array
330     if (!$header->seen)
331       $js_row_arr['unread'] = true;
332     if ($header->answered)
333       $js_row_arr['replied'] = true;
334
335     // set message icon    
336     if ($attrib['unreadicon'] && !$header->seen)
337       $message_icon = $attrib['unreadicon'];
338     else if ($attrib['repliedicon'] && $header->answered)
339       $message_icon = $attrib['repliedicon'];
340     else if ($attrib['messageicon'])
341       $message_icon = $attrib['messageicon'];
342     
343     // set attachment icon
344     if ($attrib['attachmenticon'] && preg_match("/multipart\/m/i", $header->ctype))
345       $attach_icon = $attrib['attachmenticon'];
346         
347     $out .= sprintf('<tr id="rcmrow%d" class="message'.($header->seen ? '' : ' unread').' '.$zebra_class.'">'."\n", $header->uid);
348     $out .= sprintf("<td class=\"icon\">%s</td>\n", $message_icon ? sprintf($image_tag, $skin_path, $message_icon, '') : '');
349         
350     // format each col
351     foreach ($a_show_cols as $col)
352       {
353       if ($col=='from' || $col=='to')
354         $cont = rep_specialchars_output(rcmail_address_string($header->$col, 3, $attrib['addicon']));
355       else if ($col=='subject')
7902df 356         $cont = rep_specialchars_output($IMAP->decode_header($header->$col), 'html', 'all');
4e17e6 357       else if ($col=='size')
T 358         $cont = show_bytes($header->$col);
359       else if ($col=='date')
360         $cont = format_date($header->date); //date('m.d.Y G:i:s', strtotime($header->date));
361       else
7902df 362         $cont = rep_specialchars_output($header->$col, 'html', 'all');
4e17e6 363         
T 364       $out .= '<td class="'.$col.'">' . $cont . "</td>\n";
365       }
366
367     $out .= sprintf("<td class=\"icon\">%s</td>\n", $attach_icon ? sprintf($image_tag, $skin_path, $attach_icon, '') : '');
368     $out .= "</tr>\n";
369     
370     if (sizeof($js_row_arr))
371       $a_js_message_arr[$header->uid] = $js_row_arr;
372     }
373   
374   // complete message table
375   $out .= "</tbody></table>\n";
376   
377   
378   $message_count = $IMAP->messagecount();
379   
380   // set client env
f3b659 381   $javascript .= sprintf("%s.gui_object('messagelist', '%s');\n", $JS_OBJECT_NAME, $attrib['id']);
4e17e6 382   $javascript .= sprintf("%s.set_env('messagecount', %d);\n", $JS_OBJECT_NAME, $message_count);
T 383   $javascript .= sprintf("%s.set_env('current_page', %d);\n", $JS_OBJECT_NAME, $IMAP->list_page);
384   $javascript .= sprintf("%s.set_env('pagecount', %d);\n", $JS_OBJECT_NAME, ceil($message_count/$IMAP->page_size));
b076a4 385   $javascript .= sprintf("%s.set_env('sort_col', '%s');\n", $JS_OBJECT_NAME, $sort_col);
T 386   $javascript .= sprintf("%s.set_env('sort_order', '%s');\n", $JS_OBJECT_NAME, $sort_order);
4e17e6 387   
T 388   if ($attrib['messageicon'])
389     $javascript .= sprintf("%s.set_env('messageicon', '%s%s');\n", $JS_OBJECT_NAME, $skin_path, $attrib['messageicon']);
390   if ($attrib['unreadicon'])
391     $javascript .= sprintf("%s.set_env('unreadicon', '%s%s');\n", $JS_OBJECT_NAME, $skin_path, $attrib['unreadicon']);
392   if ($attrib['repliedicon'])
393     $javascript .= sprintf("%s.set_env('repliedicon', '%s%s');\n", $JS_OBJECT_NAME, $skin_path, $attrib['repliedicon']);
394   if ($attrib['attachmenticon'])
395     $javascript .= sprintf("%s.set_env('attachmenticon', '%s%s');\n", $JS_OBJECT_NAME, $skin_path, $attrib['attachmenticon']);
396     
397   $javascript .= sprintf("%s.set_env('messages', %s);", $JS_OBJECT_NAME, array2js($a_js_message_arr));
398   
399   $OUTPUT->add_script($javascript);  
400   
401   return $out;
402   }
403
404
405
406
407 // return javascript commands to add rows to the message list
408 function rcmail_js_message_list($a_headers, $insert_top=FALSE)
409   {
410   global $CONFIG, $IMAP;
411
412   $commands = '';
413   $a_show_cols = is_array($CONFIG['list_cols']) ? $CONFIG['list_cols'] : array('subject');
414
415   // show 'to' instead of from in sent messages
416   if (strtolower($IMAP->get_mailbox_name())=='sent' && ($f = array_search('from', $a_show_cols)))
417     $a_show_cols[$f] = 'to';
418
419   // loop through message headers
420   for ($n=0; $a_headers[$n]; $n++)
421     {
422     $header = $a_headers[$n];
423     $a_msg_cols = array();
424     $a_msg_flags = array();
425       
426     // format each col; similar as in rcmail_message_list()
427     foreach ($a_show_cols as $col)
428       {
429       if ($col=='from' || $col=='to')
430         $cont = rep_specialchars_output(rcmail_address_string($header->$col, 3));
431       else if ($col=='subject')
7902df 432         $cont = rep_specialchars_output($IMAP->decode_header($header->$col), 'html', 'all');
4e17e6 433       else if ($col=='size')
T 434         $cont = show_bytes($header->$col);
435       else if ($col=='date')
436         $cont = format_date($header->date); //date('m.d.Y G:i:s', strtotime($header->date));
437       else
7902df 438         $cont = rep_specialchars_output($header->$col, 'html', 'all');
4e17e6 439           
T 440       $a_msg_cols[$col] = $cont;
441       }
442
443     $a_msg_flags['unread'] = $header->seen ? 0 : 1;
444     $a_msg_flags['replied'] = $header->answered ? 1 : 0;
445   
446     $commands .= sprintf("this.add_message_row(%s, %s, %s, %b);\n",
447                          $header->uid,
448                          array2js($a_msg_cols),
449                          array2js($a_msg_flags),
450                          preg_match("/multipart\/m/i", $header->ctype));
451     }
452
453   return $commands;
454   }
455
456
457
458 function rcmail_messagecount_display($attrib)
459   {
460   global $IMAP, $OUTPUT, $JS_OBJECT_NAME;
461   
462   if (!$attrib['id'])
463     $attrib['id'] = 'rcmcountdisplay';
464
465   $OUTPUT->add_script(sprintf("%s.gui_object('countdisplay', '%s');", $JS_OBJECT_NAME, $attrib['id']));
466
467   // allow the following attributes to be added to the <span> tag
468   $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id'));
469
470   
471   $out = '<span' . $attrib_str . '>';
472   $out .= rcmail_get_messagecount_text();
473   $out .= '</span>';
474   return $out;
475   }
476
477
478
479 function rcmail_get_messagecount_text()
480   {
481   global $IMAP, $MESSAGE;
482   
483   if (isset($MESSAGE['index']))
484     {
485     $a_msg_index = $IMAP->message_index();
486     return rcube_label(array('name' => 'messagenrof',
487                              'vars' => array('nr'  => $MESSAGE['index']+1,
488                                              'count' => sizeof($a_msg_index))));
489     }
490   
491   $start_msg = ($IMAP->list_page-1) * $IMAP->page_size + 1;
492   $max = $IMAP->messagecount();
493
494   if ($max==0)
495     $out = rcube_label('mailboxempty');
496   else
497     $out = rcube_label(array('name' => 'messagesfromto',
498                               'vars' => array('from'  => $start_msg,
499                                               'to'    => min($max, $start_msg + $IMAP->page_size - 1),
500                                               'count' => $max)));
501
cd900d 502   return rep_specialchars_output($out);
4e17e6 503   }
T 504
505
506 function rcmail_print_body($part, $safe=FALSE, $plain=FALSE) // $body, $ctype_primary='text', $ctype_secondary='plain', $encoding='7bit', $safe=FALSE, $plain=FALSE)
507   {
508   global $IMAP, $REMOTE_OBJECTS, $JS_OBJECT_NAME;
509
510   // extract part properties: body, ctype_primary, ctype_secondary, encoding, parameters
511   extract($part);
512   
513   $block = $plain ? '%s' : '%s'; //'<div style="display:block;">%s</div>';
514   $body = $IMAP->mime_decode($body, $encoding);
515   $body = $IMAP->charset_decode($body, $parameters);
516
517
518   // text/html
519   if ($ctype_secondary=='html')
520     {
521     if (!$safe)  // remove remote images and scripts
522       {
523       $remote_patterns = array('/(src|background)=(["\']?)([hftps]{3,5}:\/{2}[^"\'\s]+)(\2|\s|>)/Ui',
524                            //  '/(src|background)=(["\']?)([\.\/]+[^"\'\s]+)(\2|\s|>)/Ui',
525                                '/(<base.*href=["\']?)([hftps]{3,5}:\/{2}[^"\'\s]+)([^<]*>)/i',
526                                '/(<link.*href=["\']?)([hftps]{3,5}:\/{2}[^"\'\s]+)([^<]*>)/i',
527                                '/url\s*\(["\']?([hftps]{3,5}:\/{2}[^"\'\s]+)["\']?\)/i',
528                                '/url\s*\(["\']?([\.\/]+[^"\'\s]+)["\']?\)/i',
529                                '/<script.+<\/script>/Umis');
530
531       $remote_replaces = array('\\1=\\2#\\4',
532                             // '\\1=\\2#\\4',
533                                '',
534                                '\\1#\\3',
535                                'none',
536                                'none',
537                                '');
538       
539       // set flag if message containes remote obejcts that where blocked
540       foreach ($remote_patterns as $pattern)
541         {
542         if (preg_match($pattern, $body))
543           {
544           $REMOTE_OBJECTS = TRUE;
545           break;
546           }
547         }
548
549       $body = preg_replace($remote_patterns, $remote_replaces, $body);
550       }
551
552     return sprintf($block, rep_specialchars_output($body, 'html', '', FALSE));
553     }
554
555   // text/enriched
556   if ($ctype_secondary=='enriched')
557     {
558     $body = enriched_to_html($body);
559     return sprintf($block, rep_specialchars_output($body, 'html'));
560     }
561   else
562     {
563     // make links and email-addresses clickable
564     $convert_patterns = $convert_replaces = $replace_strings = array();
565     
09941e 566     $url_chars = 'a-z0-9_\-\+\*\$\/&%=@#:';
4e17e6 567     $url_chars_within = '\?\.~,!';
T 568
569     $convert_patterns[] = "/([\w]+):\/\/([a-z0-9\-\.]+[a-z]{2,4}([$url_chars$url_chars_within]*[$url_chars])?)/ie";
20a1b3 570     $convert_replaces[] = "rcmail_str_replacement('<a href=\"\\1://\\2\" target=\"_blank\">\\1://\\2</a>', \$replace_strings)";
4e17e6 571
T 572     $convert_patterns[] = "/([^\/:]|\s)(www\.)([a-z0-9\-]{2,}[a-z]{2,4}([$url_chars$url_chars_within]*[$url_chars])?)/ie";
20a1b3 573     $convert_replaces[] = "rcmail_str_replacement('\\1<a href=\"http://\\2\\3\" target=\"_blank\">\\2\\3</a>', \$replace_strings)";
4e17e6 574     
T 575     $convert_patterns[] = '/([a-z0-9][a-z0-9\-\.\+\_]*@[a-z0-9]([a-z0-9\-][.]?)*[a-z0-9]\\.[a-z]{2,5})/ie';
20a1b3 576     $convert_replaces[] = "rcmail_str_replacement('<a href=\"mailto:\\1\" onclick=\"return $JS_OBJECT_NAME.command(\'compose\',\'\\1\',this)\">\\1</a>', \$replace_strings)";
4e17e6 577
T 578     $body = wordwrap(trim($body), 80);
579     $body = preg_replace($convert_patterns, $convert_replaces, $body);
580
581     // split body into single lines
582     $a_lines = preg_split('/\r?\n/', $body);
583
584     // colorize quoted parts
585     for($n=0; $n<sizeof($a_lines); $n++)
586       {
587       $line = $a_lines[$n];
588
589       if ($line{2}=='>')
590         $color = 'red';
591       else if ($line{1}=='>')
592         $color = 'green';
593       else if ($line{0}=='>')
594         $color = 'blue';
595       else
596         $color = FALSE;
597
598       $line = rep_specialchars_output($line, 'html', 'replace', FALSE);
599         
600       if ($color)
601         $a_lines[$n] = sprintf('<font color="%s">%s</font>', $color, $line);
602       else
603         $a_lines[$n] = $line;
604       }
605
606     // insert the links for urls and mailtos
607     $body = preg_replace("/##string_replacement\{([0-9]+)\}##/e", "\$replace_strings[\\1]", join("\n", $a_lines));
608     
609     return sprintf($block, "<pre>\n".$body."\n</pre>");
610     }
611   }
612
613
614
615 // add a string to the replacement array and return a replacement string
616 function rcmail_str_replacement($str, &$rep)
617   {
618   static $count = 0;
619   $rep[$count] = stripslashes($str);
620   return "##string_replacement{".($count++)."}##";
621   }
622
623
624 function rcmail_parse_message($structure, $arg=array(), $recursive=FALSE)
625   {
626   global $IMAP;
627   static $sa_inline_objects = array();
628
629   // arguments are: (bool)$prefer_html, (string)$get_url
630   extract($arg);
631
632   $a_attachments = array();
633   $a_return_parts = array();
634   $out = '';
635
636   $message_ctype_primary = strtolower($structure->ctype_primary);
637   $message_ctype_secondary = strtolower($structure->ctype_secondary);
638
639   // show message headers
640   if ($recursive && is_array($structure->headers) && isset($structure->headers['subject']))
641     $a_return_parts[] = array('type' => 'headers',
642                               'headers' => $structure->headers);
643
644   // print body if message doesn't have multiple parts
645   if ($message_ctype_primary=='text')
646     {
647     $a_return_parts[] = array('type' => 'content',
648                               'body' => $structure->body,
649                               'ctype_primary' => $message_ctype_primary,
650                               'ctype_secondary' => $message_ctype_secondary,
a95e0e 651                               'parameters' => $structure->ctype_parameters,
4e17e6 652                               'encoding' => $structure->headers['content-transfer-encoding']);
T 653     }
654
655   // message contains alternative parts
656   else if ($message_ctype_primary=='multipart' && $message_ctype_secondary=='alternative' && is_array($structure->parts))
657     {
658     // get html/plaintext parts
659     $plain_part = $html_part = $print_part = $related_part = NULL;
660     
661     foreach ($structure->parts as $p => $sub_part)
662       {
663       $sub_ctype_primary = strtolower($sub_part->ctype_primary);
664       $sub_ctype_secondary = strtolower($sub_part->ctype_secondary);
665
666       // check if sub part is 
667       if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='plain')
668         $plain_part = $p;
669       else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='html')
670         $html_part = $p;
671       else if ($sub_ctype_primary=='text' && $sub_ctype_secondary=='enriched')
672         $enriched_part = $p;
673       else if ($sub_ctype_primary=='multipart' && $sub_ctype_secondary=='related')
674         $related_part = $p;
675       }
676
677     // parse related part (alternative part could be in here)
678     if ($related_part!==NULL && $prefer_html)
679       {
680       list($parts, $attachmnts) = rcmail_parse_message($structure->parts[$related_part], $arg, TRUE);
681       $a_return_parts = array_merge($a_return_parts, $parts);
682       $a_attachments = array_merge($a_attachments, $attachmnts);
683       }
684
685     // print html/plain part
686     else if ($html_part!==NULL && $prefer_html)
687       $print_part = $structure->parts[$html_part];
688     else if ($enriched_part!==NULL)
689       $print_part = $structure->parts[$enriched_part];
690     else if ($plain_part!==NULL)
691       $print_part = $structure->parts[$plain_part];
692
693     // show message body
694     if (is_object($print_part))
695       $a_return_parts[] = array('type' => 'content',
696                                 'body' => $print_part->body,
697                                 'ctype_primary' => strtolower($print_part->ctype_primary),
698                                 'ctype_secondary' => strtolower($print_part->ctype_secondary),
699                                 'parameters' => $print_part->ctype_parameters,
700                                 'encoding' => $print_part->headers['content-transfer-encoding']);
701     // show plaintext warning
702     else if ($html_part!==NULL)
703       $a_return_parts[] = array('type' => 'content',
704                                 'body' => rcube_label('htmlmessage'),
705                                 'ctype_primary' => 'text',
706                                 'ctype_secondary' => 'plain');
707                                 
708     // add html part as attachment
709     if ($html_part!==NULL && $structure->parts[$html_part]!==$print_part)
710       {
711       $html_part = $structure->parts[$html_part];
712       $a_attachments[] = array('filename' => rcube_label('htmlmessage'),
713                                'encoding' => $html_part->headers['content-transfer-encoding'],
714                                'mimetype' => 'text/html',
715                                'part_id'  => $html_part->mime_id,
716                                'size'     => strlen($IMAP->mime_decode($html_part->body, $html_part->headers['content-transfer-encoding'])));
717       }
718     }
719
720   // message contains multiple parts
721   else if ($message_ctype_primary=='multipart' && is_array($structure->parts))
722     {
723     foreach ($structure->parts as $mail_part)
724       {
725       $primary_type = strtolower($mail_part->ctype_primary);
726       $secondary_type = strtolower($mail_part->ctype_secondary);
727
728       // multipart/alternative
729       if ($primary_type=='multipart') // && ($secondary_type=='alternative' || $secondary_type=='mixed' || $secondary_type=='related'))
730         {
731         list($parts, $attachmnts) = rcmail_parse_message($mail_part, $arg, TRUE);
732
733         $a_return_parts = array_merge($a_return_parts, $parts);
734         $a_attachments = array_merge($a_attachments, $attachmnts);
735         }
736
737       // part text/[plain|html] OR message/delivery-status
738       else if (($primary_type=='text' && ($secondary_type=='plain' || $secondary_type=='html')) ||
739                ($primary_type=='message' && $secondary_type=='delivery-status'))
740         {
741         $a_return_parts[] = array('type' => 'content',
742                                   'body' => $mail_part->body,
743                                   'ctype_primary' => $primary_type,
744                                   'ctype_secondary' => $secondary_type,
a95e0e 745                                   'parameters' => $mail_part->ctype_parameters,
4e17e6 746                                   'encoding' => $mail_part->headers['content-transfer-encoding']);
T 747         }
748
749       // part message/*
750       else if ($primary_type=='message')
751         {
752         /* don't parse headers here; they're parsed within the recursive call to rcmail_parse_message()
753         if ($mail_part->parts[0]->headers)
754           $a_return_parts[] = array('type' => 'headers',
755                                     'headers' => $mail_part->parts[0]->headers);
756         */
757                                       
758         list($parts, $attachmnts) = rcmail_parse_message($mail_part->parts[0], $arg, TRUE);
759
760         $a_return_parts = array_merge($a_return_parts, $parts);
761         $a_attachments = array_merge($a_attachments, $attachmnts);
762         }
763
764       // part is file/attachment
765       else if ($mail_part->disposition=='attachment' || $mail_part->disposition=='inline' || $mail_part->headers['content-id'])
766         {
767         if ($message_ctype_secondary=='related' && $mail_part->headers['content-id'])
768           $sa_inline_objects[] = array('filename' => $mail_part->d_parameters['filename'],
769                                        'mimetype' => strtolower("$primary_type/$secondary_type"),
770                                        'part_id'  => $mail_part->mime_id,
771                                        'content_id' => preg_replace(array('/^</', '/>$/'), '', $mail_part->headers['content-id']));
772
773         else if ($mail_part->d_parameters['filename'])
774           $a_attachments[] = array('filename' => $mail_part->d_parameters['filename'],
775                                    'encoding' => strtolower($mail_part->headers['content-transfer-encoding']),
776                                    'mimetype' => strtolower("$primary_type/$secondary_type"),
777                                    'part_id'  => $mail_part->mime_id,
778                                    'size'     => strlen($IMAP->mime_decode($mail_part->body, $mail_part->headers['content-transfer-encoding'])) /*,
779                                    'content'  => $mail_part->body */);
780                                    
781         else if ($mail_part->ctype_parameters['name'])
782           $a_attachments[] = array('filename' => $mail_part->ctype_parameters['name'],
783                                    'encoding' => strtolower($mail_part->headers['content-transfer-encoding']),
784                                    'mimetype' => strtolower("$primary_type/$secondary_type"),
785                                    'part_id'  => $mail_part->mime_id,
786                                    'size'     => strlen($IMAP->mime_decode($mail_part->body, $mail_part->headers['content-transfer-encoding'])) /*,
787                                    'content'  => $mail_part->body */);
788                                    
789                                    
790         }
791       }
792
793
794     // if this was a related part try to resolve references
795     if ($message_ctype_secondary=='related' && sizeof($sa_inline_objects))
796       {
797       $a_replace_patters = array();
798       $a_replace_strings = array();
799         
800       foreach ($sa_inline_objects as $inline_object)
801         {
802         $a_replace_patters[] = 'cid:'.$inline_object['content_id'];
803         $a_replace_strings[] = sprintf($get_url, $inline_object['part_id']);
804         }
805       
806       foreach ($a_return_parts as $i => $return_part)
807         {
808         if ($return_part['type']!='content')
809           continue;
810
811         // decode body and replace cid:...
812         $a_return_parts[$i]['body'] = str_replace($a_replace_patters, $a_replace_strings, $IMAP->mime_decode($return_part['body'], $return_part['encoding']));
813         $a_return_parts[$i]['encoding'] = '7bit';
814         }
815       }
816     }
817     
818
819   // join all parts together
820   //$out .= join($part_delimiter, $a_return_parts);
821
822   return array($a_return_parts, $a_attachments);
823   }
824
825
826
827
828 // return table with message headers
829 function rcmail_message_headers($attrib, $headers=NULL)
830   {
831   global $IMAP, $OUTPUT, $MESSAGE;
832   static $sa_attrib;
833   
834   // keep header table attrib
835   if (is_array($attrib) && !$sa_attrib)
836     $sa_attrib = $attrib;
837   else if (!is_array($attrib) && is_array($sa_attrib))
838     $attrib = $sa_attrib;
839   
840   
841   if (!isset($MESSAGE))
842     return FALSE;
843
844   // get associative array of headers object
845   if (!$headers)
846     $headers = is_object($MESSAGE['headers']) ? get_object_vars($MESSAGE['headers']) : $MESSAGE['headers'];
847     
848   $header_count = 0;
849   
850   // allow the following attributes to be added to the <table> tag
851   $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id', 'cellpadding', 'cellspacing', 'border', 'summary'));
852   $out = '<table' . $attrib_str . ">\n";
853
854   // show these headers
855   $standard_headers = array('subject', 'from', 'organization', 'to', 'cc', 'reply-to', 'date');
856   
857   foreach ($standard_headers as $hkey)
858     {
859     if (!$headers[$hkey])
860       continue;
861
b076a4 862     if ($hkey=='date' && !empty($headers[$hkey]))
4e17e6 863       $header_value = format_date(strtotime($headers[$hkey]));
T 864     else if (in_array($hkey, array('from', 'to', 'cc', 'reply-to')))
865       $header_value = rep_specialchars_output(rcmail_address_string($IMAP->decode_header($headers[$hkey]), NULL, $attrib['addicon']));
866     else
867       $header_value = rep_specialchars_output($IMAP->decode_header($headers[$hkey]), '', 'all');
868
869     $out .= "\n<tr>\n";
1038d5 870     $out .= '<td class="header-title">'.rep_specialchars_output(rcube_label($hkey)).":&nbsp;</td>\n";
4e17e6 871     $out .= '<td class="'.$hkey.'" width="90%">'.$header_value."</td>\n</tr>";
T 872     $header_count++;
873     }
874
875   $out .= "\n</table>\n\n";
876
877   return $header_count ? $out : '';  
878   }
879
880
881
882 function rcmail_message_body($attrib)
883   {
884   global $CONFIG, $OUTPUT, $MESSAGE, $GET_URL, $REMOTE_OBJECTS, $JS_OBJECT_NAME;
885   
886   if (!is_array($MESSAGE['parts']) && !$MESSAGE['body'])
887     return '';
888     
889   if (!$attrib['id'])
890     $attrib['id'] = 'rcmailMsgBody';
891
892   $safe_mode = (bool)$_GET['_safe'];
893   $attrib_str = create_attrib_string($attrib, array('style', 'class', 'id'));
894   $out = '<div '. $attrib_str . ">\n";
895   
896   $header_attrib = array();
897   foreach ($attrib as $attr => $value)
898     if (preg_match('/^headertable([a-z]+)$/i', $attr, $regs))
899       $header_attrib[$regs[1]] = $value;
900
901
902   // this is an ecrypted message
903   // -> create a plaintext body with the according message
904   if (!sizeof($MESSAGE['parts']) && $MESSAGE['headers']->ctype=='multipart/encrypted')
905     {
906     $MESSAGE['parts'][0] = array('type' => 'content',
907                                  'ctype_primary' => 'text',
908                                  'ctype_secondary' => 'plain',
909                                  'body' => rcube_label('encryptedmessage'));
910     }
911   
912   if ($MESSAGE['parts'])
913     {
914     foreach ($MESSAGE['parts'] as $i => $part)
915       {
916       if ($part['type']=='headers')
917         $out .= rcmail_message_headers(sizeof($header_attrib) ? $header_attrib : NULL, $part['headers']);
918       else if ($part['type']=='content')
919         {
a95e0e 920         if (empty($part['parameters']) || empty($part['parameters']['charset']))
T 921           $part['parameters']['charset'] = $MESSAGE['headers']->charset;
922         
4e17e6 923         // $body = rcmail_print_body($part['body'], $part['ctype_primary'], $part['ctype_secondary'], $part['encoding'], $safe_mode);
T 924         $body = rcmail_print_body($part, $safe_mode);
925         $out .= '<div class="message-part">';
926         $out .= rcmail_mod_html_body($body, $attrib['id']);
927         $out .= "</div>\n";
928         }
929       }
930     }
931   else
932     $out .= $MESSAGE['body'];
933
934
935   $ctype_primary = strtolower($MESSAGE['structure']->ctype_primary);
936   $ctype_secondary = strtolower($MESSAGE['structure']->ctype_secondary);
937   
938   // list images after mail body
939   if (get_boolean($attrib['showimages']) && $ctype_primary=='multipart' && $ctype_secondary=='mixed' &&
940       sizeof($MESSAGE['attachments']) && !strstr($message_body, '<html') && strlen($GET_URL))
941     {
942     foreach ($MESSAGE['attachments'] as $attach_prop)
943       {
944       if (strpos($attach_prop['mimetype'], 'image/')===0)
945         $out .= sprintf("\n<hr />\n<p align=\"center\"><img src=\"%s&_part=%s\" alt=\"%s\" title=\"%s\" /></p>\n",
946                         $GET_URL, $attach_prop['part_id'],
947                         $attach_prop['filename'],
948                         $attach_prop['filename']);
949       }
950     }
951   
952   // tell client that there are blocked remote objects
953   if ($REMOTE_OBJECTS && !$safe_mode)
954     $OUTPUT->add_script(sprintf("%s.set_env('blockedobjects', true);", $JS_OBJECT_NAME));
955
956   $out .= "\n</div>";
957   return $out;
958   }
959
960
961
962 // modify a HTML message that it can be displayed inside a HTML page
963 function rcmail_mod_html_body($body, $container_id)
964   {
965   $last_style_pos = 0;
966   $body_lc = strtolower($body);
967   
968   // find STYLE tags
969   while (($pos = strpos($body_lc, '<style', $last_style_pos)) && ($pos2 = strpos($body_lc, '</style>', $pos)))
970     {
971     $pos2 += 8;
972     $body_pre = substr($body, 0, $pos);
973     $styles = substr($body, $pos, $pos2-$pos);
974     $body_post = substr($body, $pos2, strlen($body)-$pos2);
975     
976     // replace all css definitions with #container [def]
977     $styles = rcmail_mod_css_styles($styles, $container_id);
978     
979     $body = $body_pre . $styles . $body_post;
980     $last_style_pos = $pos2;
981     }
982
983
984   // remove SCRIPT tags
985   while (($pos = strpos($body_lc, '<script')) && ($pos2 = strpos($body_lc, '</script>', $pos)))
986     {
987     $pos2 += 8;
988     $body = substr($body, 0, $pos) . substr($body, $pos2, strlen($body)-$pos2);
989     $body_lc = strtolower($body);
990     }
991   
992
993   // resolve <base href>
994   $base_reg = '/(<base.*href=["\']?)([hftps]{3,5}:\/{2}[^"\'\s]+)([^<]*>)/i';
995   if (preg_match($base_reg, $body, $regs))
996     {
997     $base_url = $regs[2];
998     $body = preg_replace('/(src|background|href)=(["\']?)([\.\/]+[^"\'\s]+)(\2|\s|>)/Uie', "'\\1=\"'.make_absolute_url('\\3', '$base_url').'\"'", $body);
999     $body = preg_replace('/(url\s*\()(["\']?)([\.\/]+[^"\'\)\s]+)(\2)\)/Uie', "'\\1\''.make_absolute_url('\\3', '$base_url').'\')'", $body);
1000     $body = preg_replace($base_reg, '', $body);
1001     }
1002
1003
1004   // add comments arround html and other tags
1005   $out = preg_replace(array('/(<\/?html[^>]*>)/i',
1006                             '/(<\/?head[^>]*>)/i',
1007                             '/(<title[^>]*>.+<\/title>)/ui',
1008                             '/(<\/?meta[^>]*>)/i'),
1009                       '<!--\\1-->',
1010                       $body);
1011                       
1012   $out = preg_replace(array('/(<body[^>]*>)/i',
1013                             '/(<\/body>)/i'),
1014                       array('<div class="rcmBody">',
1015                             '</div>'),
1016                       $out);
1017
1018   
1019   return $out;
1020   }
1021
1022
1023
1024 // replace all css definitions with #container [def]
1025 function rcmail_mod_css_styles($source, $container_id)
1026   {
1027   $a_css_values = array();
1028   $last_pos = 0;
1029   
1030   // cut out all contents between { and }
1031   while (($pos = strpos($source, '{', $last_pos)) && ($pos2 = strpos($source, '}', $pos)))
1032     {
1033     $key = sizeof($a_css_values);
1034     $a_css_values[$key] = substr($source, $pos+1, $pos2-($pos+1));
1035     $source = substr($source, 0, $pos+1) . "<<str_replacement[$key]>>" . substr($source, $pos2, strlen($source)-$pos2);
1036     $last_pos = $pos+2;
1037     }
1038   
1039   $styles = preg_replace('/(^\s*|,\s*)([a-z0-9\._][a-z0-9\.\-_]*)/im', "\\1#$container_id \\2", $source);
1040   $styles = preg_replace('/<<str_replacement\[([0-9]+)\]>>/e', "\$a_css_values[\\1]", $styles);
1041   
1042   // replace body definition because we also stripped off the <body> tag
1043   $styles = preg_replace("/$container_id\s+body/i", "$container_id div.rcmBody", $styles);
1044   
1045   return $styles;
1046   }
1047
1048
1049
1050 // return first text part of a message
1051 function rcmail_first_text_part($message_parts)
1052   {
1053   if (!is_array($message_parts))
1054     return FALSE;
1055     
1056   $html_part = NULL;
1057       
1058   // check all message parts
1059   foreach ($message_parts as $pid => $part)
1060     {
1061     $mimetype = strtolower($part->ctype_primary.'/'.$part->ctype_secondary);
1062     if ($mimetype=='text/plain')
1063       {
1064       $body = rcube_imap::mime_decode($part->body, $part->headers['content-transfer-encoding']);
1065       $body = rcube_imap::charset_decode($body, $part->ctype_parameters);
1066       return $body;
1067       }
1068     else if ($mimetype=='text/html')
1069       {
1070       $html_part = rcube_imap::mime_decode($part->body, $part->headers['content-transfer-encoding']);
1071       $html_part = rcube_imap::charset_decode($html_part, $part->ctype_parameters);
1072       }
1073     }
1074     
1075
1076   // convert HTML to plain text
1077   if ($html_part)
1078     {    
1079     // remove special chars encoding
1080     $trans = array_flip(get_html_translation_table(HTML_ENTITIES));
1081     $html_part = strtr($html_part, $trans);
1082
1083     // create instance of html2text class
1084     $txt = new html2text($html_part);
1085     return $txt->get_text();
1086     }
1087
1088   return FALSE;
1089   }
1090
1091
1092 // get source code of a specific message and cache it
1093 function rcmail_message_source($uid)
1094   {
1095   global $IMAP, $DB;
1096
1097   // get message ID if uid is given  
1098   $headers = $IMAP->get_headers($uid);
1099   $message_id = $headers->messageID;
1100   
1101   // get cached message source
1102   $msg_source = rcube_read_cache($message_id);
1103
1104   // get message from server and cache it
1105   if (!$msg_source)
1106     {
1107     $msg_source = $IMAP->get_raw_body($uid);
1108     rcube_write_cache($message_id, $msg_source, TRUE);
1109     }
1110
1111   return $msg_source;
1112   }
1113
1114
1115 // decode address string and re-format it as HTML links
1116 function rcmail_address_string($input, $max=NULL, $addicon=NULL)
1117   {
1118   global $IMAP, $PRINT_MODE, $CONFIG, $OUTPUT, $JS_OBJECT_NAME, $EMAIL_ADDRESS_PATTERN;
1119   
1120   $a_parts = $IMAP->decode_address_list($input);
1121
1122   if (!sizeof($a_parts))
1123     return $input;
1124
1125   $c = count($a_parts);
1126   $j = 0;
1127   $out = '';
1128
1129   foreach ($a_parts as $part)
1130     {
1131     $j++;
1132     if ($PRINT_MODE)
a95e0e 1133       $out .= sprintf('%s &lt;%s&gt;', rep_specialchars_output($part['name']), $part['mailto']);
4e17e6 1134     else if (preg_match($EMAIL_ADDRESS_PATTERN, $part['mailto']))
T 1135       {
1136       $out .= sprintf('<a href="mailto:%s" onclick="return %s.command(\'compose\',\'%s\',this)" class="rcmContactAddress" title="%s">%s</a>',
1137                       $part['mailto'],
1138                       $JS_OBJECT_NAME,
1139                       $part['mailto'],
1140                       $part['mailto'],
a95e0e 1141                       rep_specialchars_output($part['name']));
4e17e6 1142                       
T 1143       if ($addicon)
1144         $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>',
1145                         $JS_OBJECT_NAME,
1146                         urlencode($part['string']),
1147                         rcube_label('addtoaddressbook'),
1148                         $CONFIG['skin_path'],
1149                         $addicon);
1150       }
1151     else
1152       {
1153       if ($part['name'])
a95e0e 1154         $out .= rep_specialchars_output($part['name']);
4e17e6 1155       if ($part['mailto'])
T 1156         $out .= (strlen($out) ? ' ' : '') . sprintf('&lt;%s&gt;', $part['mailto']);
1157       }
1158       
1159     if ($c>$j)
1160       $out .= ','.($max ? '&nbsp;' : ' ');
1161         
1162     if ($max && $j==$max && $c>$j)
1163       {
1164       $out .= '...';
1165       break;
1166       }        
1167     }
1168     
1169   return $out;
1170   }
1171
1172
1173 function rcmail_message_part_controls()
1174   {
1175   global $CONFIG, $IMAP, $MESSAGE;
1176   
1177   if (!is_array($MESSAGE) || !is_array($MESSAGE['parts']) || !($_GET['_uid'] && $_GET['_part']) || !$MESSAGE['parts'][$_GET['_part']])
1178     return '';
1179     
1180   $part = $MESSAGE['parts'][$_GET['_part']];
1181   
1182   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'cellspacing', 'cellpadding', 'border', 'summary'));
1183   $out = '<table '. $attrib_str . ">\n";
1184   
1185   $filename = $part->d_parameters['filename'] ? $part->d_parameters['filename'] : $part->ctype_parameters['name'];
1186   $filesize = strlen($IMAP->mime_decode($part->body, $part->headers['content-transfer-encoding']));
1187   
1188   if ($filename)
1189     {
1190     $out .= sprintf('<tr><td class="title">%s</td><td>%s</td><td>[<a href="./?%s">%s</a>]</tr>'."\n",
1191                     rcube_label('filename'),
1192                     rep_specialchars_output($filename),
1193                     str_replace('_frame=', '_download=', $_SERVER['QUERY_STRING']),
1194                     rcube_label('download'));
1195     }
1196     
1197   if ($filesize)
1198     $out .= sprintf('<tr><td class="title">%s</td><td>%s</td></tr>'."\n",
1199                     rcube_label('filesize'),
1200                     show_bytes($filesize));
1201   
1202   $out .= "\n</table>";
1203   
1204   return $out;
1205   }
1206
1207
1208
1209 function rcmail_message_part_frame($attrib)
1210   {
1211   global $MESSAGE;
1212   
1213   $part = $MESSAGE['parts'][$_GET['_part']];
1214   $ctype_primary = strtolower($part->ctype_primary);
1215
1216   $attrib['src'] = './?'.str_replace('_frame=', ($ctype_primary=='text' ? '_show=' : '_preload='), $_SERVER['QUERY_STRING']);
1217
1218   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'src', 'width', 'height'));
1219   $out = '<iframe '. $attrib_str . "></ifame>";
1220     
1221   return $out;
1222   }
1223
1224
597170 1225 // create temp dir for attachments
T 1226 function rcmail_create_compose_tempdir()
1227   {
1228   global $CONFIG;
1229   
1230   if ($_SESSION['compose']['temp_dir'])
1231     return $_SESSION['compose']['temp_dir'];
1232   
1233   if (!empty($CONFIG['temp_dir']))
1234     $temp_dir = $CONFIG['temp_dir'].(!eregi('\/$', $CONFIG['temp_dir']) ? '/' : '').$_SESSION['compose']['id'];
1235
1236   // create temp-dir for uploaded attachments
1237   if (!empty($CONFIG['temp_dir']) && is_writeable($CONFIG['temp_dir']))
1238     {
1239     mkdir($temp_dir);
1240     $_SESSION['compose']['temp_dir'] = $temp_dir;
1241     }
1242
1243   return $_SESSION['compose']['temp_dir'];
1244   }
1245
4e17e6 1246
T 1247 // clear message composing settings
1248 function rcmail_compose_cleanup()
1249   {
1250   if (!isset($_SESSION['compose']))
1251     return;
1252   
1253   // remove attachment files from temp dir
1254   if (is_array($_SESSION['compose']['attachments']))
1255     foreach ($_SESSION['compose']['attachments'] as $attachment)
1256       unlink($attachment['path']);
1257
1258   // kill temp dir
1259   if ($_SESSION['compose']['temp_dir'])
1260     rmdir($_SESSION['compose']['temp_dir']);
1261   
1262   unset($_SESSION['compose']);
1263   }
1264   
1265   
1266 ?>