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