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