svncommit
2006-11-21 e901f948f25db35a2639130a4d95df34dba58db2
commit | author | age
4e17e6 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/mail/compose.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  |   Compose a new mail message with all headers and attachments         |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id$
19
20 */
21
22 require_once('Mail/mimeDecode.php');
8d4bcd 23
T 24 // define constants for message compose mode
25 define('RCUBE_COMPOSE_REPLY', 0x0106);
26 define('RCUBE_COMPOSE_FORWARD', 0x0107);
27 define('RCUBE_COMPOSE_DRAFT', 0x0108);
28
4e17e6 29
a894ba 30 // remove an attachment
aade7b 31 if ($_action=='remove-attachment' && preg_match('/^rcmfile([0-9]+)$/', $_GET['_file'], $regs))
a894ba 32   {
aade7b 33   $id = $regs[1];
T 34   if (is_array($_SESSION['compose']['attachments'][$id]))
35     {
36     @unlink($_SESSION['compose']['attachments'][$id]['path']);
37     $_SESSION['compose']['attachments'][$id] = NULL;
38     $commands = sprintf("parent.%s.remove_from_attachment_list('rcmfile%d');\n", $JS_OBJECT_NAME, $id);
39     rcube_remote_response($commands);  
40     exit;
41     }
a894ba 42   }
aade7b 43
f0f98f 44
S 45 $MESSAGE_FORM = NULL;
8d4bcd 46 $MESSAGE = NULL;
f0f98f 47
S 48 // nothing below is called during message composition, only at "new/forward/reply/draft" initialization
49 // since there are many ways to leave the compose page improperly, it seems necessary to clean-up an old
50 // compose when a "new/forward/reply/draft" is called - otherwise the old session attachments will appear
51
52 rcmail_compose_cleanup();
53 $_SESSION['compose'] = array('id' => uniqid(rand()));
4e17e6 54
10a699 55 // add some labels to client
a0109c 56 rcube_add_label('nosubject', 'norecipientwarning', 'nosubjectwarning', 'nobodywarning', 'notsentwarning', 'savingmessage', 'sendingmessage', 'messagesaved', 'converting');
10a699 57
d656f1 58 // add config parameter to client script
T 59 $OUTPUT->add_script(sprintf("%s.set_env('draft_autosave', %d);", $JS_OBJECT_NAME, !empty($CONFIG['drafts_mbox']) ? $CONFIG['draft_autosave'] : 0));
60
10a699 61
8d4bcd 62 // get reference message and set compose mode
T 63 if ($msg_uid = get_input_value('_reply_uid', RCUBE_INPUT_GET))
64   $compose_mode = RCUBE_COMPOSE_REPLY;
65 else if ($msg_uid = get_input_value('_forward_uid', RCUBE_INPUT_GET))
66   $compose_mode = RCUBE_COMPOSE_FORWARD;
67 else if ($msg_uid = get_input_value('_draft_uid', RCUBE_INPUT_GET))
68   $compose_mode = RCUBE_COMPOSE_DRAFT;
69
70
71 if (!empty($msg_uid))
4e17e6 72   {
T 73   // similar as in program/steps/mail/show.inc
8d4bcd 74   $MESSAGE = array('UID' => $msg_uid);
T 75   $MESSAGE['headers'] = &$IMAP->get_headers($msg_uid);
76   $MESSAGE['structure'] = &$IMAP->get_structure($msg_uid);  
4e17e6 77   $MESSAGE['subject'] = $IMAP->decode_header($MESSAGE['headers']->subject);
8d4bcd 78   $MESSAGE['parts'] = $IMAP->get_mime_numbers($MESSAGE['structure']);
03ac21 79   
8d4bcd 80   if ($compose_mode == RCUBE_COMPOSE_REPLY)
4e17e6 81     {
8d4bcd 82     $_SESSION['compose']['reply_uid'] = $msg_uid;
T 83     $_SESSION['compose']['reply_msgid'] = $MESSAGE['headers']->messageID;
84     $_SESSION['compose']['references']  = $MESSAGE['headers']->reference;
85     $_SESSION['compose']['references'] .= !empty($MESSAGE['headers']->reference) ? ' ' : '';
86     $_SESSION['compose']['references'] .= $MESSAGE['headers']->messageID;
583f1c 87
8d4bcd 88     if (!empty($_GET['_all']))
T 89       $MESSAGE['reply_all'] = 1;
4e17e6 90     }
8d4bcd 91   else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
4e17e6 92     {
8d4bcd 93     $_SESSION['compose']['forward_uid'] = $msg_uid;
4e17e6 94     }
8d4bcd 95   else if ($compose_mode == RCUBE_COMPOSE_DRAFT)
1966c5 96     {
8d4bcd 97     $_SESSION['compose']['draft_uid'] = $msg_uid;
1966c5 98     }
S 99
4e17e6 100   }
T 101
102 /****** compose mode functions ********/
103
104
105 function rcmail_compose_headers($attrib)
106   {
8d4bcd 107   global $IMAP, $MESSAGE, $DB, $compose_mode;
583f1c 108   static $sa_recipients = array();
4e17e6 109
T 110   list($form_start, $form_end) = get_form_tags($attrib);
111   
112   $out = '';
113   $part = strtolower($attrib['part']);
114   
115   switch ($part)
116     {
117     case 'from':
1cded8 118       return rcmail_compose_header_from($attrib);
4e17e6 119
T 120     case 'to':
121       $fname = '_to';
122       $header = 'to';
1cded8 123
4e17e6 124       // we have contact id's as get parameters
4fd971 125       if (!empty($_GET['_to']) && preg_match('/^[0-9]+(,[0-9]+)*$/', $_GET['_to']))
4e17e6 126         {
T 127         $a_recipients = array();
d7cb77 128         $sql_result = $DB->query("SELECT name, email
1cded8 129                                   FROM ".get_table_name('contacts')."
T 130                                   WHERE user_id=?
131                                   AND    del<>1
d7cb77 132                                   AND    contact_id IN (".$_GET['_to'].")",
S 133                                   $_SESSION['user_id']);
4e17e6 134                                          
T 135         while ($sql_arr = $DB->fetch_assoc($sql_result))
136           $a_recipients[] = format_email_recipient($sql_arr['email'], $sql_arr['name']);
137           
138         if (sizeof($a_recipients))
139           $fvalue = join(', ', $a_recipients);
140         }
597170 141       else if (!empty($_GET['_to']))
8d4bcd 142         $fvalue = get_input_value('_to', RCUBE_INPUT_GET);
4e17e6 143         
T 144     case 'cc':
145       if (!$fname)
146         {
147         $fname = '_cc';
583f1c 148         $header = 'cc';
4e17e6 149         }
T 150     case 'bcc':
151       if (!$fname)
152         $fname = '_bcc';
153         
317219 154       $allow_attrib = array('id', 'class', 'style', 'cols', 'rows', 'wrap', 'tabindex');
4e17e6 155       $field_type = 'textarea';            
T 156       break;
157
158     case 'replyto':
159     case 'reply-to':
160       $fname = '_replyto';
317219 161       $allow_attrib = array('id', 'class', 'style', 'size', 'tabindex');
4e17e6 162       $field_type = 'textfield';
T 163       break;
164     
165     }
1966c5 166  
597170 167   if ($fname && !empty($_POST[$fname]))
01c86f 168     $fvalue = get_input_value($fname, RCUBE_INPUT_POST, TRUE);
8d4bcd 169
T 170   else if ($header && $compose_mode == RCUBE_COMPOSE_REPLY)
4e17e6 171     {
T 172     // get recipent address(es) out of the message headers
8d4bcd 173     if ($header=='to' && !empty($MESSAGE['headers']->replyto))
03ac21 174       $fvalue = $MESSAGE['headers']->replyto;
58e360 175
8d4bcd 176     else if ($header=='to' && !empty($MESSAGE['headers']->from))
03ac21 177       $fvalue = $MESSAGE['headers']->from;
58e360 178
583f1c 179     // add recipent of original message if reply to all
8d4bcd 180     else if ($header=='cc' && !empty($MESSAGE['reply_all']))
583f1c 181       {
03ac21 182       if ($v = $MESSAGE['headers']->to)
8d4bcd 183         $fvalue .= $v;
583f1c 184
03ac21 185       if ($v = $MESSAGE['headers']->cc)
8d4bcd 186         $fvalue .= (!empty($fvalue) ? ', ' : '') . $v;
583f1c 187       }
T 188
4e17e6 189     // split recipients and put them back together in a unique way
583f1c 190     if (!empty($fvalue))
T 191       {
192       $to_addresses = $IMAP->decode_address_list($fvalue);
193       $fvalue = '';
194       foreach ($to_addresses as $addr_part)
195         {
8d4bcd 196         if (!in_array($addr_part['mailto'], $sa_recipients) && (!$MESSAGE['FROM'] || !in_array($addr_part['mailto'], $MESSAGE['FROM'])))
583f1c 197           {
T 198           $fvalue .= (strlen($fvalue) ? ', ':'').$addr_part['string'];
199           $sa_recipients[] = $addr_part['mailto'];
200           }
201         }
202       }
4e17e6 203     }
8d4bcd 204   else if ($header && $compose_mode == RCUBE_COMPOSE_DRAFT)
1966c5 205     {
S 206     // get drafted headers
8d4bcd 207     if ($header=='to' && !empty($MESSAGE['headers']->to))
T 208       $fvalue = $IMAP->decode_header($MESSAGE['headers']->to);
1966c5 209
8d4bcd 210     if ($header=='cc' && !empty($MESSAGE['headers']->cc))
T 211       $fvalue = $IMAP->decode_header($MESSAGE['headers']->cc);
1966c5 212
8d4bcd 213     if ($header=='bcc' && !empty($MESSAGE['headers']->bcc))
T 214       $fvalue = $IMAP->decode_header($MESSAGE['headers']->bcc);
1966c5 215     }
583f1c 216
4e17e6 217         
T 218   if ($fname && $field_type)
219     {
220     // pass the following attributes to the form class
221     $field_attrib = array('name' => $fname);
222     foreach ($attrib as $attr => $value)
223       if (in_array($attr, $allow_attrib))
224         $field_attrib[$attr] = $value;
225
226     // create teaxtarea object
227     $input = new $field_type($field_attrib);
228     $out = $input->show($fvalue);    
229     }
230   
231   if ($form_start)
232     $out = $form_start.$out;
1966c5 233
4e17e6 234   return $out;  
T 235   }
236
237
1cded8 238
T 239 function rcmail_compose_header_from($attrib)
4e17e6 240   {
8d4bcd 241   global $IMAP, $MESSAGE, $DB, $OUTPUT, $JS_OBJECT_NAME, $compose_mode;
1cded8 242     
T 243   // pass the following attributes to the form class
244   $field_attrib = array('name' => '_from');
245   foreach ($attrib as $attr => $value)
246     if (in_array($attr, array('id', 'class', 'style', 'size', 'tabindex')))
247       $field_attrib[$attr] = $value;
4e17e6 248
1cded8 249   // extract all recipients of the reply-message
T 250   $a_recipients = array();
8d4bcd 251   if ($compose_mode == RCUBE_COMPOSE_REPLY && is_object($MESSAGE['headers']))
1cded8 252     {
8d4bcd 253     $MESSAGE['FROM'] = array();
58e360 254
03ac21 255     $a_to = $IMAP->decode_address_list($MESSAGE['headers']->to);
1cded8 256     foreach ($a_to as $addr)
T 257       {
258       if (!empty($addr['mailto']))
259         $a_recipients[] = $addr['mailto'];
260       }
4e17e6 261
8d4bcd 262     if (!empty($MESSAGE['headers']->cc))
1cded8 263       {
8d4bcd 264       $a_cc = $IMAP->decode_address_list($MESSAGE['headers']->cc);
1cded8 265       foreach ($a_cc as $addr)
T 266         {
267         if (!empty($addr['mailto']))
268           $a_recipients[] = $addr['mailto'];
269         }
270       }
271     }
4e17e6 272
1cded8 273   // get this user's identities
a0109c 274   $sql_result = $DB->query("SELECT identity_id, name, email, signature, html_signature
1cded8 275                             FROM   ".get_table_name('identities')."
T 276                             WHERE user_id=?
277                             AND    del<>1
278                             ORDER BY ".$DB->quoteIdentifier('standard')." DESC, name ASC",
279                            $_SESSION['user_id']);
a0109c 280
1cded8 281   if ($DB->num_rows($sql_result))
T 282     {
283     $from_id = 0;
284     $a_signatures = array();
a0109c 285
1cded8 286     $field_attrib['onchange'] = "$JS_OBJECT_NAME.change_identity(this)";
T 287     $select_from = new select($field_attrib);
a0109c 288
1cded8 289     while ($sql_arr = $DB->fetch_assoc($sql_result))
T 290       {
a0109c 291       $identity_id = $sql_arr['identity_id'];
S 292       $select_from->add(format_email_recipient($sql_arr['email'], $sql_arr['name']), $identity_id);
4e17e6 293
1cded8 294       // add signature to array
T 295       if (!empty($sql_arr['signature']))
a0109c 296         {
S 297         $a_signatures[$identity_id]['text'] = $sql_arr['signature'];
298         $a_signatures[$identity_id]['is_html'] = ($sql_arr['html_signature'] == 1) ? true : false;
299         }
300
1cded8 301       // set identity if it's one of the reply-message recipients
T 302       if (in_array($sql_arr['email'], $a_recipients))
303         $from_id = $sql_arr['identity_id'];
a0109c 304
8d4bcd 305       if ($compose_mode == RCUBE_COMPOSE_REPLY && is_array($MESSAGE['FROM']))
T 306         $MESSAGE['FROM'][] = $sql_arr['email'];
1966c5 307
8d4bcd 308       if ($compose_mode == RCUBE_COMPOSE_DRAFT && strstr($MESSAGE['headers']->from, $sql_arr['email']))
1966c5 309         $from_id = $sql_arr['identity_id'];
1cded8 310       }
4e17e6 311
1cded8 312     // overwrite identity selection with post parameter
T 313     if (isset($_POST['_from']))
8d4bcd 314       $from_id = get_input_value('_from', RCUBE_INPUT_POST);
4e17e6 315
1cded8 316     $out = $select_from->show($from_id);
4e17e6 317
1cded8 318     // add signatures to client
T 319     $OUTPUT->add_script(sprintf("%s.set_env('signatures', %s);", $JS_OBJECT_NAME, array2js($a_signatures)));  
320     }
321   else
322     {
323     $input_from = new textfield($field_attrib);
324     $out = $input_from->show($_POST['_from']);
325     }
1966c5 326   
1cded8 327   if ($form_start)
T 328     $out = $form_start.$out;
4e17e6 329
1cded8 330   return $out;
4e17e6 331   }
T 332
1cded8 333   
4e17e6 334
T 335 function rcmail_compose_body($attrib)
336   {
8d4bcd 337   global $CONFIG, $OUTPUT, $MESSAGE, $JS_OBJECT_NAME, $compose_mode;
4e17e6 338   
T 339   list($form_start, $form_end) = get_form_tags($attrib);
340   unset($attrib['form']);
dd53e2 341   
T 342   if (empty($attrib['id']))
343     $attrib['id'] = 'rcmComposeMessage';
a0109c 344
4e17e6 345   $attrib['name'] = '_message';
a0109c 346
S 347   if ($CONFIG['htmleditor'])
348     $isHtml = true;
349   else
350     $isHtml = false;
4e17e6 351
T 352   $body = '';
a0109c 353
4e17e6 354   // use posted message body
597170 355   if (!empty($_POST['_message']))
a0109c 356     {
ea7c46 357     $body = get_input_value('_message', RCUBE_INPUT_POST, TRUE);
a0109c 358     }
4e17e6 359   // compose reply-body
8d4bcd 360   else if ($compose_mode == RCUBE_COMPOSE_REPLY)
4e17e6 361     {
a0109c 362     $hasHtml = rcmail_has_html_part($MESSAGE['parts']); 
S 363     if ($hasHtml && $CONFIG['htmleditor'])
364       {
365       $body = rcmail_first_html_part($MESSAGE);
366       $isHtml = true;
367       }
368     else
369       {
370       $body = rcmail_first_text_part($MESSAGE);
371       $isHtml = false;
372       }
4e17e6 373     if (strlen($body))
a0109c 374       $body = rcmail_create_reply_body($body, $isHtml);
4e17e6 375     }
T 376   // forward message body inline
8d4bcd 377   else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
4e17e6 378     {
a0109c 379     $hasHtml = rcmail_has_html_part($MESSAGE['parts']);
S 380     if ($hasHtml && $CONFIG['htmleditor'])
381       {
382       $body = rcmail_first_html_part($MESSAGE);
383       $isHtml = true;
384       }
385     else
386       {
387       $body = rcmail_first_text_part($MESSAGE);
388       $isHtml = false;
389       }
4e17e6 390     if (strlen($body))
a0109c 391       $body = rcmail_create_forward_body($body, $isHtml);
4e17e6 392     }
8d4bcd 393   else if ($compose_mode == RCUBE_COMPOSE_DRAFT)
1966c5 394     {
a0109c 395     $hasHtml = rcmail_has_html_part($MESSAGE['parts']);
S 396     if ($hasHtml && $CONFIG['htmleditor'])
397       {
398       $body = rcmail_first_html_part($MESSAGE);
399       $isHtml = true;
400       }
401     else
402       {
403       $body = rcmail_first_text_part($MESSAGE);
404       $isHtml = false;
405       }
1966c5 406     if (strlen($body))
a0109c 407       $body = rcmail_create_draft_body($body, $isHtml);
1966c5 408     }
a0109c 409
S 410   $OUTPUT->include_script('tiny_mce/tiny_mce.js');
411   $OUTPUT->include_script("editor.js");
380aed 412   $OUTPUT->add_script('rcmail_editor_init("$__skin_path");');
a0109c 413
4e17e6 414   $out = $form_start ? "$form_start\n" : '';
1966c5 415
03ac21 416   $saveid = new hiddenfield(array('name' => '_draft_saveid', 'value' => $compose_mode==RCUBE_COMPOSE_DRAFT ? str_replace(array('<','>'), "", $MESSAGE['headers']->messageID) : ''));
f0f98f 417   $out .= $saveid->show();
1966c5 418
S 419   $drafttoggle = new hiddenfield(array('name' => '_draft', 'value' => 'yes'));
420   $out .= $drafttoggle->show();
421
a0109c 422   $msgtype = new hiddenfield(array('name' => '_is_html', 'value' => ($isHtml?"1":"0")));
S 423   $out .= $msgtype->show();
424
425   // If desired, set this text area to be editable by TinyMCE
426   if ($isHtml)
427     $attrib['mce_editable'] = "true";
428   $textarea = new textarea($attrib);
4e17e6 429   $out .= $textarea->show($body);
T 430   $out .= $form_end ? "\n$form_end" : '';
a0109c 431
dd53e2 432   // include GoogieSpell
a0109c 433   if (!empty($CONFIG['enable_spellcheck']) && !$isHtml)
ed5d29 434     {
996066 435     $lang_set = '';
T 436     if (!empty($CONFIG['spellcheck_languages']) && is_array($CONFIG['spellcheck_languages']))
437       $lang_set = "googie.setLanguages(".array2js($CONFIG['spellcheck_languages']).");\n";
438     
ed5d29 439     $OUTPUT->include_script('googiespell.js');
T 440     $OUTPUT->add_script(sprintf("var googie = new GoogieSpell('\$__skin_path/images/googiespell/','%s&_action=spell&lang=');\n".
441                                 "googie.lang_chck_spell = \"%s\";\n".
442                                 "googie.lang_rsm_edt = \"%s\";\n".
443                                 "googie.lang_close = \"%s\";\n".
444                                 "googie.lang_revert = \"%s\";\n".
a5d3e6 445                                 "googie.lang_no_error_found = \"%s\";\n%s".
T 446                                 "googie.setCurrentLanguage('%s');\n".
ed5d29 447                                 "googie.decorateTextarea('%s');\n".
T 448                                 "%s.set_env('spellcheck', googie);",
449                                 $GLOBALS['COMM_PATH'],
450                                 rep_specialchars_output(rcube_label('checkspelling')),
451                                 rep_specialchars_output(rcube_label('resumeediting')),
452                                 rep_specialchars_output(rcube_label('close')),
453                                 rep_specialchars_output(rcube_label('revertto')),
454                                 rep_specialchars_output(rcube_label('nospellerrors')),
996066 455                                 $lang_set,
a5d3e6 456                                 substr($_SESSION['user_lang'], 0, 2),
ed5d29 457                                 $attrib['id'],
T 458                                 $JS_OBJECT_NAME), 'foot');
459
460     rcube_add_label('checking');
461     }
f0f98f 462  
41fa0b 463   $out .= "\n".'<iframe name="savetarget" src="program/blank.gif" style="width:0;height:0;visibility:hidden;"></iframe>';
T 464
4e17e6 465   return $out;
T 466   }
467
468
a0109c 469 function rcmail_create_reply_body($body, $bodyIsHtml)
4e17e6 470   {
8d4bcd 471   global $IMAP, $MESSAGE;
4e17e6 472
a0109c 473   if (! $bodyIsHtml)
S 474   {
475     // soft-wrap message first
476     $body = wordwrap($body, 75);
4e17e6 477   
a0109c 478     // split body into single lines
S 479     $a_lines = preg_split('/\r?\n/', $body);
4e17e6 480   
a0109c 481     // add > to each line
S 482     for($n=0; $n<sizeof($a_lines); $n++)
483       {
484       if (strpos($a_lines[$n], '>')===0)
485         $a_lines[$n] = '>'.$a_lines[$n];
486       else
487         $a_lines[$n] = '> '.$a_lines[$n];
488       }
4e17e6 489  
a0109c 490     $body = join("\n", $a_lines);
4e17e6 491
a0109c 492     // add title line
S 493     $prefix = sprintf("\n\n\nOn %s, %s wrote:\n",
494              $MESSAGE['headers']->date,
495              $IMAP->decode_header($MESSAGE['headers']->from));
1cded8 496
a0109c 497     // try to remove the signature
S 498     if ($sp = strrstr($body, '-- '))
499       {
500       if ($body{$sp+3}==' ' || $body{$sp+3}=="\n" || $body{$sp+3}=="\r")
501         $body = substr($body, 0, $sp-1);
502       }
503     $suffix = '';
504   }
505   else
506   {
507      $prefix = sprintf("<br><br>On %s, %s wrote:<br><blockquote type=\"cite\" " .
508                        "style=\"padding-left: 5px; border-left: #1010ff 2px solid; " .
509                        "margin-left: 5px; width: 100%%\">",
510                        $MESSAGE['headers']->date,
511                        $IMAP->decode_header($MESSAGE['headers']->from));
4e17e6 512
a0109c 513      $suffix = "</blockquote>";
S 514   }
515
516   return $prefix.$body.$suffix;
4e17e6 517   }
T 518
519
a0109c 520 function rcmail_create_forward_body($body, $bodyIsHtml)
4e17e6 521   {
8d4bcd 522   global $IMAP, $MESSAGE;
4e17e6 523
a0109c 524   if (! $bodyIsHtml)
S 525   {
526     // soft-wrap message first
527     $body = wordwrap($body, 80);
4e17e6 528   
a0109c 529     $prefix = sprintf("\n\n\n-------- Original Message --------\nSubject: %s\nDate: %s\nFrom: %s\nTo: %s\n\n",
S 530                      $MESSAGE['subject'],
531                      $MESSAGE['headers']->date,
532                      $IMAP->decode_header($MESSAGE['headers']->from),
533                      $IMAP->decode_header($MESSAGE['headers']->to));
534   }
535   else
536   {
537       $prefix = sprintf(
538         "<br><br>-------- Original Message --------" .
539         "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody>" .
540         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Subject: </th><td>%s</td></tr>" .
541         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Date: </th><td>%s</td></tr>" .
542         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">From: </th><td>%s</td></tr>" .
543         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">To: </th><td>%s</td></tr>" .
544         "</tbody></table><br>",
19bef0 545                      rep_specialchars_output($MESSAGE['subject']),
S 546                      rep_specialchars_output($MESSAGE['headers']->date),
547                      rep_specialchars_output($IMAP->decode_header($MESSAGE['headers']->from)),
548                      rep_specialchars_output($IMAP->decode_header($MESSAGE['headers']->to)));
a0109c 549   }
S 550
597170 551   // add attachments
8d4bcd 552   if (!isset($_SESSION['compose']['forward_attachments']) &&
T 553       is_array($MESSAGE['parts']) && sizeof($MESSAGE['parts'])>1)
554     rcmail_write_compose_attachments($MESSAGE);
597170 555
4e17e6 556   return $prefix.$body;
T 557   }
558
8d4bcd 559
a0109c 560 function rcmail_create_draft_body($body, $bodyIsHtml)
1966c5 561   {
8d4bcd 562   global $IMAP, $MESSAGE;
1966c5 563     
S 564   // add attachments
8d4bcd 565   if (!isset($_SESSION['compose']['forward_attachments']) &&
T 566       is_array($MESSAGE['parts']) && sizeof($MESSAGE['parts'])>1)
567     rcmail_write_compose_attachments($MESSAGE);
1966c5 568
S 569   return $body;
8d4bcd 570   }
T 571   
572   
573 function rcmail_write_compose_attachments(&$message)
574   {
70d4b9 575   global $IMAP, $CONFIG;
T 576
577   $temp_dir = unslashify($CONFIG['temp_dir']);
8d4bcd 578
T 579   if (!is_array($_SESSION['compose']['attachments']))
580     $_SESSION['compose']['attachments'] = array();
581   
582   foreach ($message['parts'] as $pid => $part)
583     {
584     if ($part->ctype_primary != 'message' && $part->ctype_primary != 'text' &&
585         ($part->disposition=='attachment' || $part->disposition=='inline' || $part->headers['content-id'] ||
586          (empty($part->disposition) && ($part->d_parameters['filename'] || $part->ctype_parameters['name']))))
587       {
588       $tmp_path = tempnam($temp_dir, 'rcmAttmnt');
589       if ($fp = fopen($tmp_path, 'w'))
590         {
591         fwrite($fp, $IMAP->get_message_part($message['UID'], $pid, $part->encoding));
592         fclose($fp);
593         
594         $filename = !empty($part->d_parameters['filename']) ? $part->d_parameters['filename'] :
595                      (!empty($part->ctype_parameters['name']) ? $part->ctype_parameters['name'] :
596                       (!empty($part->headers['content-description']) ? $part->headers['content-description'] : 'file'));
597
598         $_SESSION['compose']['attachments'][] = array(
599           'name' => rcube_imap::decode_mime_string($filename),
600           'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
601           'path' => $tmp_path
602           );
603         }
604       }
605     }
606     
607   $_SESSION['compose']['forward_attachments'] = TRUE;
1966c5 608   }
4e17e6 609
T 610
611 function rcmail_compose_subject($attrib)
612   {
8d4bcd 613   global $CONFIG, $MESSAGE, $compose_mode;
4e17e6 614   
T 615   list($form_start, $form_end) = get_form_tags($attrib);
616   unset($attrib['form']);
617   
618   $attrib['name'] = '_subject';
619   $textfield = new textfield($attrib);
620
621   $subject = '';
622
623   // use subject from post
597170 624   if (isset($_POST['_subject']))
01c86f 625     $subject = get_input_value('_subject', RCUBE_INPUT_POST, TRUE);
4e17e6 626     
T 627   // create a reply-subject
8d4bcd 628   else if ($compose_mode == RCUBE_COMPOSE_REPLY)
520c36 629     {
8d4bcd 630     if (eregi('^re:', $MESSAGE['subject']))
T 631       $subject = $MESSAGE['subject'];
520c36 632     else
8d4bcd 633       $subject = 'Re: '.$MESSAGE['subject'];
520c36 634     }
4e17e6 635
T 636   // create a forward-subject
8d4bcd 637   else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
09941e 638     {
8d4bcd 639     if (eregi('^fwd:', $MESSAGE['subject']))
T 640       $subject = $MESSAGE['subject'];
09941e 641     else
8d4bcd 642       $subject = 'Fwd: '.$MESSAGE['subject'];
09941e 643     }
4e17e6 644
1966c5 645   // creeate a draft-subject
8d4bcd 646   else if ($compose_mode == RCUBE_COMPOSE_DRAFT)
T 647     $subject = $MESSAGE['subject'];
4e17e6 648   
T 649   $out = $form_start ? "$form_start\n" : '';
650   $out .= $textfield->show($subject);
651   $out .= $form_end ? "\n$form_end" : '';
652          
653   return $out;
654   }
655
656
657 function rcmail_compose_attachment_list($attrib)
658   {
a0109c 659   global $OUTPUT, $JS_OBJECT_NAME, $CONFIG;
4e17e6 660   
T 661   // add ID if not given
662   if (!$attrib['id'])
663     $attrib['id'] = 'rcmAttachmentList';
664   
665   // allow the following attributes to be added to the <ul> tag
666   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style'));
667  
668   $out = '<ul'. $attrib_str . ">\n";
669   
670   if (is_array($_SESSION['compose']['attachments']))
671     {
a894ba 672     if ($attrib['deleteicon'])
aade7b 673       $button = sprintf('<img src="%s%s" alt="%s" border="0" style="padding-right:2px;vertical-align:middle" />',
508442 674                         $CONFIG['skin_path'],
T 675                         $attrib['deleteicon'],
676                         rcube_label('delete'));
a894ba 677     else
S 678       $button = rcube_label('delete');
679
aade7b 680     foreach ($_SESSION['compose']['attachments'] as $id => $a_prop)
T 681       $out .= sprintf('<li id="rcmfile%d"><a href="#delete" onclick="return %s.command(\'remove-attachment\',\'rcmfile%d\', this)" title="%s">%s</a>%s</li>',
682                       $id,
508442 683                       $JS_OBJECT_NAME,
aade7b 684                       $id,
508442 685                       rcube_label('delete'), 
aade7b 686                       $button,
T 687                       rep_specialchars_output($a_prop['name']));
4e17e6 688     }
T 689
690   $OUTPUT->add_script(sprintf("%s.gui_object('attachmentlist', '%s');", $JS_OBJECT_NAME, $attrib['id']));  
691     
692   $out .= '</ul>';
693   return $out;
694   }
695
696
697
698 function rcmail_compose_attachment_form($attrib)
699   {
700   global $OUTPUT, $JS_OBJECT_NAME, $SESS_HIDDEN_FIELD;
701
702   // add ID if not given
703   if (!$attrib['id'])
704     $attrib['id'] = 'rcmUploadbox';
705   
706   // allow the following attributes to be added to the <div> tag
707   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style'));
708   $input_field = rcmail_compose_attachment_field(array());
709   $label_send = rcube_label('upload');
710   $label_close = rcube_label('close');
711   
712   $out = <<<EOF
713 <div$attrib_str>
714 <form action="./" method="post" enctype="multipart/form-data">
715 $SESS_HIDDEN_FIELD
716 $input_field<br />
717 <input type="button" value="$label_close" class="button" onclick="document.getElementById('$attrib[id]').style.visibility='hidden'" />
718 <input type="button" value="$label_send" class="button" onclick="$JS_OBJECT_NAME.command('send-attachment', this.form)" />
719 </form>
720 </div>
721 EOF;
722
723   
724   $OUTPUT->add_script(sprintf("%s.gui_object('uploadbox', '%s');", $JS_OBJECT_NAME, $attrib['id']));  
725   return $out;
726   }
727
728
729 function rcmail_compose_attachment_field($attrib)
730   {
731   // allow the following attributes to be added to the <input> tag
732   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'size'));
733  
734   $out = '<input type="file" name="_attachments[]"'. $attrib_str . " />";
735   return $out;
736   }
737
66e2bf 738
4e17e6 739 function rcmail_priority_selector($attrib)
T 740   {
741   list($form_start, $form_end) = get_form_tags($attrib);
742   unset($attrib['form']);
743   
744   $attrib['name'] = '_priority';
745   $selector = new select($attrib);
746
747   $selector->add(array(rcube_label('lowest'),
748                        rcube_label('low'),
749                        rcube_label('normal'),
750                        rcube_label('high'),
751                        rcube_label('highest')),
7902df 752                  array(5, 4, 0, 2, 1));
4e17e6 753                  
597170 754   $sel = isset($_POST['_priority']) ? $_POST['_priority'] : 0;
4e17e6 755
T 756   $out = $form_start ? "$form_start\n" : '';
757   $out .= $selector->show($sel);
758   $out .= $form_end ? "\n$form_end" : '';
759          
760   return $out;
761   }
762
763
620439 764 function rcmail_receipt_checkbox($attrib)
T 765   {
766   list($form_start, $form_end) = get_form_tags($attrib);
767   unset($attrib['form']);
66e2bf 768   
T 769   if (!isset($attrib['id']))
770     $attrib['id'] = 'receipt';  
620439 771
T 772   $attrib['name'] = '_receipt';
66e2bf 773   $attrib['value'] = '1';
T 774   $checkbox = new checkbox($attrib);
620439 775
T 776   $out = $form_start ? "$form_start\n" : '';
777   $out .= $checkbox->show(0);
778   $out .= $form_end ? "\n$form_end" : '';
779
780   return $out;
781   }
782
783
a0109c 784 function rcmail_editor_selector($attrib)
S 785 {
786   global $CONFIG, $MESSAGE, $compose_mode;
787
788   $choices = array(
6b1fc0 789     'html'  => 'htmltoggle',
S 790     'plain' => 'plaintoggle'
a0109c 791   );
S 792
793   // determine whether HTML or plain text should be checked 
794   if ($CONFIG['htmleditor'])
795     $useHtml = true;
796   else
797     $useHtml = false;
798
799   if ($compose_mode == RCUBE_COMPOSE_REPLY ||
800       $compose_mode == RCUBE_COMPOSE_FORWARD ||
801       $compose_mode == RCUBE_COMPOSE_DRAFT)
802     {
803     $hasHtml = rcmail_has_html_part($MESSAGE['parts']);
804     $useHtml = ($hasHtml && $CONFIG['htmleditor']);
805     }
806
807   $selector = '';
6b1fc0 808   
S 809   $attrib['name'] = '_editorSelect';
810   $attrib['onclick'] = 'return rcmail_toggle_editor(this)';
a0109c 811   foreach ($choices as $value => $text)
S 812     {
813     $checked = '';
6b1fc0 814     if ((($value == 'html') && $useHtml) ||
S 815         (($value != 'html') && !$useHtml))
816       $attrib['checked'] = 'true';
817     else
818       unset($attrib['checked']);
a0109c 819
6b1fc0 820     $attrib['id'] = '_' . $value;
S 821     $rb = new radiobutton($attrib);
822     $selector .= sprintf("<td>%s</td><td class=\"title\"><label for=\"%s\">%s</label></td>",
823                          $rb->show($value),
824                          $attrib['id'],
825                          rcube_label($text));
a0109c 826     }
S 827
828   return $selector;
829 }
830
831
4e17e6 832 function get_form_tags($attrib)
T 833   {
834   global $CONFIG, $OUTPUT, $JS_OBJECT_NAME, $MESSAGE_FORM, $SESS_HIDDEN_FIELD;  
835
836   $form_start = '';
837   if (!strlen($MESSAGE_FORM))
838     {
839     $hiddenfields = new hiddenfield(array('name' => '_task', 'value' => $GLOBALS['_task']));
840     $hiddenfields->add(array('name' => '_action', 'value' => 'send'));
1966c5 841
597170 842     $form_start = empty($attrib['form']) ? '<form name="form" action="./" method="post">' : '';
4e17e6 843     $form_start .= "\n$SESS_HIDDEN_FIELD\n";
T 844     $form_start .= $hiddenfields->show();
845     }
846     
847   $form_end = (strlen($MESSAGE_FORM) && !strlen($attrib['form'])) ? '</form>' : '';
597170 848   $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
4e17e6 849   
T 850   if (!strlen($MESSAGE_FORM))
851     $OUTPUT->add_script("$JS_OBJECT_NAME.gui_object('messageform', '$form_name');");
852   
853   $MESSAGE_FORM = $form_name;
854
855   return array($form_start, $form_end);  
856   }
857
858
859 function format_email_recipient($email, $name='')
860   {
861   if ($name && $name != $email)
862     return sprintf('%s <%s>', strpos($name, ",") ? '"'.$name.'"' : $name, $email);
863   else
864     return $email;
865   }
866
867
fd8c50 868 function rcmail_charset_pulldown($selected='ISO-8859-1')
T 869   {
870   $select = new select();
871   
872   
873   return $select->show($selected);
874   }
875
876
4e17e6 877 /****** get contacts for this user and add them to client scripts ********/
T 878
d7cb77 879 $sql_result = $DB->query("SELECT name, email
S 880                           FROM ".get_table_name('contacts')." WHERE  user_id=?
1cded8 881                           AND  del<>1",$_SESSION['user_id']);
4e17e6 882                                    
T 883 if ($DB->num_rows($sql_result))
884   {        
885   $a_contacts = array();
886   while ($sql_arr = $DB->fetch_assoc($sql_result))
887     if ($sql_arr['email'])
888       $a_contacts[] = format_email_recipient($sql_arr['email'], rep_specialchars_output($sql_arr['name'], 'js'));
889   
890   $OUTPUT->add_script(sprintf("$JS_OBJECT_NAME.set_env('contacts', %s);", array2js($a_contacts)));
891   }
892
893
894 parse_template('compose');
1966c5 895 ?>