svncommit
2006-09-23 6649b1f0a5db6160d197a13ca79cfd67fbb02d77
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']);
1966c5 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))
T 174       $fvalue = $IMAP->decode_header($MESSAGE['headers']->replyto);
58e360 175
8d4bcd 176     else if ($header=='to' && !empty($MESSAGE['headers']->from))
T 177       $fvalue = $IMAP->decode_header($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       {
8d4bcd 182       if ($v = $IMAP->decode_header($MESSAGE['headers']->to))
T 183         $fvalue .= $v;
583f1c 184
8d4bcd 185       if ($v = $IMAP->decode_header($MESSAGE['headers']->cc))
T 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
S 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
8d4bcd 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;
300         }
301
1cded8 302       // set identity if it's one of the reply-message recipients
T 303       if (in_array($sql_arr['email'], $a_recipients))
304         $from_id = $sql_arr['identity_id'];
a0109c 305
8d4bcd 306       if ($compose_mode == RCUBE_COMPOSE_REPLY && is_array($MESSAGE['FROM']))
T 307         $MESSAGE['FROM'][] = $sql_arr['email'];
1966c5 308
8d4bcd 309       if ($compose_mode == RCUBE_COMPOSE_DRAFT && strstr($MESSAGE['headers']->from, $sql_arr['email']))
1966c5 310         $from_id = $sql_arr['identity_id'];
1cded8 311       }
4e17e6 312
1cded8 313     // overwrite identity selection with post parameter
T 314     if (isset($_POST['_from']))
8d4bcd 315       $from_id = get_input_value('_from', RCUBE_INPUT_POST);
4e17e6 316
1cded8 317     $out = $select_from->show($from_id);
4e17e6 318
1cded8 319     // add signatures to client
T 320     $OUTPUT->add_script(sprintf("%s.set_env('signatures', %s);", $JS_OBJECT_NAME, array2js($a_signatures)));  
321     }
322   else
323     {
324     $input_from = new textfield($field_attrib);
325     $out = $input_from->show($_POST['_from']);
326     }
1966c5 327   
1cded8 328   if ($form_start)
T 329     $out = $form_start.$out;
4e17e6 330
1cded8 331   return $out;
4e17e6 332   }
T 333
1cded8 334   
4e17e6 335
T 336 function rcmail_compose_body($attrib)
337   {
8d4bcd 338   global $CONFIG, $OUTPUT, $MESSAGE, $JS_OBJECT_NAME, $compose_mode;
4e17e6 339   
T 340   list($form_start, $form_end) = get_form_tags($attrib);
341   unset($attrib['form']);
dd53e2 342   
T 343   if (empty($attrib['id']))
344     $attrib['id'] = 'rcmComposeMessage';
a0109c 345
4e17e6 346   $attrib['name'] = '_message';
a0109c 347
S 348   if ($CONFIG['htmleditor'])
349     $isHtml = true;
350   else
351     $isHtml = false;
4e17e6 352
T 353   $body = '';
a0109c 354
4e17e6 355   // use posted message body
597170 356   if (!empty($_POST['_message']))
a0109c 357     {
ea7c46 358     $body = get_input_value('_message', RCUBE_INPUT_POST, TRUE);
a0109c 359     }
4e17e6 360   // compose reply-body
8d4bcd 361   else if ($compose_mode == RCUBE_COMPOSE_REPLY)
4e17e6 362     {
a0109c 363     $hasHtml = rcmail_has_html_part($MESSAGE['parts']); 
S 364     if ($hasHtml && $CONFIG['htmleditor'])
365       {
366       $body = rcmail_first_html_part($MESSAGE);
367       $isHtml = true;
368       }
369     else
370       {
371       $body = rcmail_first_text_part($MESSAGE);
372       $isHtml = false;
373       }
4e17e6 374     if (strlen($body))
a0109c 375       $body = rcmail_create_reply_body($body, $isHtml);
4e17e6 376     }
T 377   // forward message body inline
8d4bcd 378   else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
4e17e6 379     {
a0109c 380     $hasHtml = rcmail_has_html_part($MESSAGE['parts']);
S 381     if ($hasHtml && $CONFIG['htmleditor'])
382       {
383       $body = rcmail_first_html_part($MESSAGE);
384       $isHtml = true;
385       }
386     else
387       {
388       $body = rcmail_first_text_part($MESSAGE);
389       $isHtml = false;
390       }
4e17e6 391     if (strlen($body))
a0109c 392       $body = rcmail_create_forward_body($body, $isHtml);
4e17e6 393     }
8d4bcd 394   else if ($compose_mode == RCUBE_COMPOSE_DRAFT)
1966c5 395     {
a0109c 396     $hasHtml = rcmail_has_html_part($MESSAGE['parts']);
S 397     if ($hasHtml && $CONFIG['htmleditor'])
398       {
399       $body = rcmail_first_html_part($MESSAGE);
400       $isHtml = true;
401       }
402     else
403       {
404       $body = rcmail_first_text_part($MESSAGE);
405       $isHtml = false;
406       }
1966c5 407     if (strlen($body))
a0109c 408       $body = rcmail_create_draft_body($body, $isHtml);
1966c5 409     }
a0109c 410
S 411   $OUTPUT->include_script('tiny_mce/tiny_mce.js');
412   $OUTPUT->include_script("editor.js");
380aed 413   $OUTPUT->add_script('rcmail_editor_init("$__skin_path");');
a0109c 414
4e17e6 415   $out = $form_start ? "$form_start\n" : '';
1966c5 416
8d4bcd 417   $saveid = new hiddenfield(array('name' => '_draft_saveid', 'value' => str_replace(array('<','>'),"",$MESSAGE['headers']->messageID) ));
f0f98f 418   $out .= $saveid->show();
1966c5 419
S 420   $drafttoggle = new hiddenfield(array('name' => '_draft', 'value' => 'yes'));
421   $out .= $drafttoggle->show();
422
a0109c 423   $msgtype = new hiddenfield(array('name' => '_is_html', 'value' => ($isHtml?"1":"0")));
S 424   $out .= $msgtype->show();
425
426   // If desired, set this text area to be editable by TinyMCE
427   if ($isHtml)
428     $attrib['mce_editable'] = "true";
429   $textarea = new textarea($attrib);
4e17e6 430   $out .= $textarea->show($body);
T 431   $out .= $form_end ? "\n$form_end" : '';
a0109c 432
dd53e2 433   // include GoogieSpell
a0109c 434   if (!empty($CONFIG['enable_spellcheck']) && !$isHtml)
ed5d29 435     {
996066 436     $lang_set = '';
T 437     if (!empty($CONFIG['spellcheck_languages']) && is_array($CONFIG['spellcheck_languages']))
438       $lang_set = "googie.setLanguages(".array2js($CONFIG['spellcheck_languages']).");\n";
439     
ed5d29 440     $OUTPUT->include_script('googiespell.js');
T 441     $OUTPUT->add_script(sprintf("var googie = new GoogieSpell('\$__skin_path/images/googiespell/','%s&_action=spell&lang=');\n".
442                                 "googie.lang_chck_spell = \"%s\";\n".
443                                 "googie.lang_rsm_edt = \"%s\";\n".
444                                 "googie.lang_close = \"%s\";\n".
445                                 "googie.lang_revert = \"%s\";\n".
a5d3e6 446                                 "googie.lang_no_error_found = \"%s\";\n%s".
T 447                                 "googie.setCurrentLanguage('%s');\n".
ed5d29 448                                 "googie.decorateTextarea('%s');\n".
T 449                                 "%s.set_env('spellcheck', googie);",
450                                 $GLOBALS['COMM_PATH'],
451                                 rep_specialchars_output(rcube_label('checkspelling')),
452                                 rep_specialchars_output(rcube_label('resumeediting')),
453                                 rep_specialchars_output(rcube_label('close')),
454                                 rep_specialchars_output(rcube_label('revertto')),
455                                 rep_specialchars_output(rcube_label('nospellerrors')),
996066 456                                 $lang_set,
a5d3e6 457                                 substr($_SESSION['user_lang'], 0, 2),
ed5d29 458                                 $attrib['id'],
T 459                                 $JS_OBJECT_NAME), 'foot');
460
461     rcube_add_label('checking');
462     }
f0f98f 463  
41fa0b 464   $out .= "\n".'<iframe name="savetarget" src="program/blank.gif" style="width:0;height:0;visibility:hidden;"></iframe>';
T 465
4e17e6 466   return $out;
T 467   }
468
469
a0109c 470 function rcmail_create_reply_body($body, $bodyIsHtml)
4e17e6 471   {
8d4bcd 472   global $IMAP, $MESSAGE;
4e17e6 473
a0109c 474   if (! $bodyIsHtml)
S 475   {
476     // soft-wrap message first
477     $body = wordwrap($body, 75);
4e17e6 478   
a0109c 479     // split body into single lines
S 480     $a_lines = preg_split('/\r?\n/', $body);
4e17e6 481   
a0109c 482     // add > to each line
S 483     for($n=0; $n<sizeof($a_lines); $n++)
484       {
485       if (strpos($a_lines[$n], '>')===0)
486         $a_lines[$n] = '>'.$a_lines[$n];
487       else
488         $a_lines[$n] = '> '.$a_lines[$n];
489       }
4e17e6 490  
a0109c 491     $body = join("\n", $a_lines);
4e17e6 492
a0109c 493     // add title line
S 494     $prefix = sprintf("\n\n\nOn %s, %s wrote:\n",
495              $MESSAGE['headers']->date,
496              $IMAP->decode_header($MESSAGE['headers']->from));
1cded8 497
a0109c 498     // try to remove the signature
S 499     if ($sp = strrstr($body, '-- '))
500       {
501       if ($body{$sp+3}==' ' || $body{$sp+3}=="\n" || $body{$sp+3}=="\r")
502         $body = substr($body, 0, $sp-1);
503       }
504     $suffix = '';
505   }
506   else
507   {
508      $prefix = sprintf("<br><br>On %s, %s wrote:<br><blockquote type=\"cite\" " .
509                        "style=\"padding-left: 5px; border-left: #1010ff 2px solid; " .
510                        "margin-left: 5px; width: 100%%\">",
511                        $MESSAGE['headers']->date,
512                        $IMAP->decode_header($MESSAGE['headers']->from));
4e17e6 513
a0109c 514      $suffix = "</blockquote>";
S 515   }
516
517   return $prefix.$body.$suffix;
4e17e6 518   }
T 519
520
a0109c 521 function rcmail_create_forward_body($body, $bodyIsHtml)
4e17e6 522   {
8d4bcd 523   global $IMAP, $MESSAGE;
4e17e6 524
a0109c 525   if (! $bodyIsHtml)
S 526   {
527     // soft-wrap message first
528     $body = wordwrap($body, 80);
4e17e6 529   
a0109c 530     $prefix = sprintf("\n\n\n-------- Original Message --------\nSubject: %s\nDate: %s\nFrom: %s\nTo: %s\n\n",
S 531                      $MESSAGE['subject'],
532                      $MESSAGE['headers']->date,
533                      $IMAP->decode_header($MESSAGE['headers']->from),
534                      $IMAP->decode_header($MESSAGE['headers']->to));
535   }
536   else
537   {
538       $prefix = sprintf(
539         "<br><br>-------- Original Message --------" .
540         "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody>" .
541         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Subject: </th><td>%s</td></tr>" .
542         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Date: </th><td>%s</td></tr>" .
543         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">From: </th><td>%s</td></tr>" .
544         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">To: </th><td>%s</td></tr>" .
545         "</tbody></table><br>",
19bef0 546                      rep_specialchars_output($MESSAGE['subject']),
S 547                      rep_specialchars_output($MESSAGE['headers']->date),
548                      rep_specialchars_output($IMAP->decode_header($MESSAGE['headers']->from)),
549                      rep_specialchars_output($IMAP->decode_header($MESSAGE['headers']->to)));
a0109c 550   }
S 551
597170 552   // add attachments
8d4bcd 553   if (!isset($_SESSION['compose']['forward_attachments']) &&
T 554       is_array($MESSAGE['parts']) && sizeof($MESSAGE['parts'])>1)
555     rcmail_write_compose_attachments($MESSAGE);
597170 556
4e17e6 557   return $prefix.$body;
T 558   }
559
8d4bcd 560
a0109c 561 function rcmail_create_draft_body($body, $bodyIsHtml)
1966c5 562   {
8d4bcd 563   global $IMAP, $MESSAGE;
1966c5 564     
S 565   // add attachments
8d4bcd 566   if (!isset($_SESSION['compose']['forward_attachments']) &&
T 567       is_array($MESSAGE['parts']) && sizeof($MESSAGE['parts'])>1)
568     rcmail_write_compose_attachments($MESSAGE);
1966c5 569
S 570   return $body;
8d4bcd 571   }
T 572   
573   
574 function rcmail_write_compose_attachments(&$message)
575   {
70d4b9 576   global $IMAP, $CONFIG;
T 577
578   $temp_dir = unslashify($CONFIG['temp_dir']);
8d4bcd 579
T 580   if (!is_array($_SESSION['compose']['attachments']))
581     $_SESSION['compose']['attachments'] = array();
582   
583   foreach ($message['parts'] as $pid => $part)
584     {
585     if ($part->ctype_primary != 'message' && $part->ctype_primary != 'text' &&
586         ($part->disposition=='attachment' || $part->disposition=='inline' || $part->headers['content-id'] ||
587          (empty($part->disposition) && ($part->d_parameters['filename'] || $part->ctype_parameters['name']))))
588       {
589       $tmp_path = tempnam($temp_dir, 'rcmAttmnt');
590       if ($fp = fopen($tmp_path, 'w'))
591         {
592         fwrite($fp, $IMAP->get_message_part($message['UID'], $pid, $part->encoding));
593         fclose($fp);
594         
595         $filename = !empty($part->d_parameters['filename']) ? $part->d_parameters['filename'] :
596                      (!empty($part->ctype_parameters['name']) ? $part->ctype_parameters['name'] :
597                       (!empty($part->headers['content-description']) ? $part->headers['content-description'] : 'file'));
598
599         $_SESSION['compose']['attachments'][] = array(
600           'name' => rcube_imap::decode_mime_string($filename),
601           'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
602           'path' => $tmp_path
603           );
604         }
605       }
606     }
607     
608   $_SESSION['compose']['forward_attachments'] = TRUE;
1966c5 609   }
4e17e6 610
T 611
612 function rcmail_compose_subject($attrib)
613   {
8d4bcd 614   global $CONFIG, $MESSAGE, $compose_mode;
4e17e6 615   
T 616   list($form_start, $form_end) = get_form_tags($attrib);
617   unset($attrib['form']);
618   
619   $attrib['name'] = '_subject';
620   $textfield = new textfield($attrib);
621
622   $subject = '';
623
624   // use subject from post
597170 625   if (isset($_POST['_subject']))
01c86f 626     $subject = get_input_value('_subject', RCUBE_INPUT_POST, TRUE);
4e17e6 627     
T 628   // create a reply-subject
8d4bcd 629   else if ($compose_mode == RCUBE_COMPOSE_REPLY)
520c36 630     {
8d4bcd 631     if (eregi('^re:', $MESSAGE['subject']))
T 632       $subject = $MESSAGE['subject'];
520c36 633     else
8d4bcd 634       $subject = 'Re: '.$MESSAGE['subject'];
520c36 635     }
4e17e6 636
T 637   // create a forward-subject
8d4bcd 638   else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
09941e 639     {
8d4bcd 640     if (eregi('^fwd:', $MESSAGE['subject']))
T 641       $subject = $MESSAGE['subject'];
09941e 642     else
8d4bcd 643       $subject = 'Fwd: '.$MESSAGE['subject'];
09941e 644     }
4e17e6 645
1966c5 646   // creeate a draft-subject
8d4bcd 647   else if ($compose_mode == RCUBE_COMPOSE_DRAFT)
T 648     $subject = $MESSAGE['subject'];
4e17e6 649   
T 650   $out = $form_start ? "$form_start\n" : '';
651   $out .= $textfield->show($subject);
652   $out .= $form_end ? "\n$form_end" : '';
653          
654   return $out;
655   }
656
657
658 function rcmail_compose_attachment_list($attrib)
659   {
a0109c 660   global $OUTPUT, $JS_OBJECT_NAME, $CONFIG;
4e17e6 661   
T 662   // add ID if not given
663   if (!$attrib['id'])
664     $attrib['id'] = 'rcmAttachmentList';
665   
666   // allow the following attributes to be added to the <ul> tag
667   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style'));
668  
669   $out = '<ul'. $attrib_str . ">\n";
670   
671   if (is_array($_SESSION['compose']['attachments']))
672     {
a894ba 673     if ($attrib['deleteicon'])
aade7b 674       $button = sprintf('<img src="%s%s" alt="%s" border="0" style="padding-right:2px;vertical-align:middle" />',
508442 675                         $CONFIG['skin_path'],
T 676                         $attrib['deleteicon'],
677                         rcube_label('delete'));
a894ba 678     else
S 679       $button = rcube_label('delete');
680
aade7b 681     foreach ($_SESSION['compose']['attachments'] as $id => $a_prop)
T 682       $out .= sprintf('<li id="rcmfile%d"><a href="#delete" onclick="return %s.command(\'remove-attachment\',\'rcmfile%d\', this)" title="%s">%s</a>%s</li>',
683                       $id,
508442 684                       $JS_OBJECT_NAME,
aade7b 685                       $id,
508442 686                       rcube_label('delete'), 
aade7b 687                       $button,
T 688                       rep_specialchars_output($a_prop['name']));
4e17e6 689     }
T 690
691   $OUTPUT->add_script(sprintf("%s.gui_object('attachmentlist', '%s');", $JS_OBJECT_NAME, $attrib['id']));  
692     
693   $out .= '</ul>';
694   return $out;
695   }
696
697
698
699 function rcmail_compose_attachment_form($attrib)
700   {
701   global $OUTPUT, $JS_OBJECT_NAME, $SESS_HIDDEN_FIELD;
702
703   // add ID if not given
704   if (!$attrib['id'])
705     $attrib['id'] = 'rcmUploadbox';
706   
707   // allow the following attributes to be added to the <div> tag
708   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style'));
709   $input_field = rcmail_compose_attachment_field(array());
710   $label_send = rcube_label('upload');
711   $label_close = rcube_label('close');
712   
713   $out = <<<EOF
714 <div$attrib_str>
715 <form action="./" method="post" enctype="multipart/form-data">
716 $SESS_HIDDEN_FIELD
717 $input_field<br />
718 <input type="button" value="$label_close" class="button" onclick="document.getElementById('$attrib[id]').style.visibility='hidden'" />
719 <input type="button" value="$label_send" class="button" onclick="$JS_OBJECT_NAME.command('send-attachment', this.form)" />
720 </form>
721 </div>
722 EOF;
723
724   
725   $OUTPUT->add_script(sprintf("%s.gui_object('uploadbox', '%s');", $JS_OBJECT_NAME, $attrib['id']));  
726   return $out;
727   }
728
729
730 function rcmail_compose_attachment_field($attrib)
731   {
732   // allow the following attributes to be added to the <input> tag
733   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'size'));
734  
735   $out = '<input type="file" name="_attachments[]"'. $attrib_str . " />";
736   return $out;
737   }
738
66e2bf 739
4e17e6 740 function rcmail_priority_selector($attrib)
T 741   {
742   list($form_start, $form_end) = get_form_tags($attrib);
743   unset($attrib['form']);
744   
745   $attrib['name'] = '_priority';
746   $selector = new select($attrib);
747
748   $selector->add(array(rcube_label('lowest'),
749                        rcube_label('low'),
750                        rcube_label('normal'),
751                        rcube_label('high'),
752                        rcube_label('highest')),
7902df 753                  array(5, 4, 0, 2, 1));
4e17e6 754                  
597170 755   $sel = isset($_POST['_priority']) ? $_POST['_priority'] : 0;
4e17e6 756
T 757   $out = $form_start ? "$form_start\n" : '';
758   $out .= $selector->show($sel);
759   $out .= $form_end ? "\n$form_end" : '';
760          
761   return $out;
762   }
763
764
620439 765 function rcmail_receipt_checkbox($attrib)
T 766   {
767   list($form_start, $form_end) = get_form_tags($attrib);
768   unset($attrib['form']);
66e2bf 769   
T 770   if (!isset($attrib['id']))
771     $attrib['id'] = 'receipt';  
620439 772
T 773   $attrib['name'] = '_receipt';
66e2bf 774   $attrib['value'] = '1';
T 775   $checkbox = new checkbox($attrib);
620439 776
T 777   $out = $form_start ? "$form_start\n" : '';
778   $out .= $checkbox->show(0);
779   $out .= $form_end ? "\n$form_end" : '';
780
781   return $out;
782   }
783
784
a0109c 785 function rcmail_editor_selector($attrib)
S 786 {
787   global $CONFIG, $MESSAGE, $compose_mode;
788
789   $choices = array(
790     'html'  => 'HTML',
791     'plain' => 'Plain text'
792   );
793
794   // determine whether HTML or plain text should be checked 
795   if ($CONFIG['htmleditor'])
796     $useHtml = true;
797   else
798     $useHtml = false;
799
800   if ($compose_mode == RCUBE_COMPOSE_REPLY ||
801       $compose_mode == RCUBE_COMPOSE_FORWARD ||
802       $compose_mode == RCUBE_COMPOSE_DRAFT)
803     {
804     $hasHtml = rcmail_has_html_part($MESSAGE['parts']);
805     $useHtml = ($hasHtml && $CONFIG['htmleditor']);
806     }
807
808   $selector = '';
809   foreach ($choices as $value => $text)
810     {
811     $checked = '';
812     if ((($text == 'HTML') && $useHtml) ||
813         (($text != 'HTML') && !$useHtml))
814       $checked = 'checked';
815
816     $selector .= sprintf("<input type='radio' name='_editorSelect' value='%s' %s onclick='return rcmail_toggle_editor(this)'>%s</input>\n",
817                           $value, $checked, $text);
818     }
819
820   return $selector;
821 }
822
823
4e17e6 824 function get_form_tags($attrib)
T 825   {
826   global $CONFIG, $OUTPUT, $JS_OBJECT_NAME, $MESSAGE_FORM, $SESS_HIDDEN_FIELD;  
827
828   $form_start = '';
829   if (!strlen($MESSAGE_FORM))
830     {
831     $hiddenfields = new hiddenfield(array('name' => '_task', 'value' => $GLOBALS['_task']));
832     $hiddenfields->add(array('name' => '_action', 'value' => 'send'));
1966c5 833
597170 834     $form_start = empty($attrib['form']) ? '<form name="form" action="./" method="post">' : '';
4e17e6 835     $form_start .= "\n$SESS_HIDDEN_FIELD\n";
T 836     $form_start .= $hiddenfields->show();
837     }
838     
839   $form_end = (strlen($MESSAGE_FORM) && !strlen($attrib['form'])) ? '</form>' : '';
597170 840   $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
4e17e6 841   
T 842   if (!strlen($MESSAGE_FORM))
843     $OUTPUT->add_script("$JS_OBJECT_NAME.gui_object('messageform', '$form_name');");
844   
845   $MESSAGE_FORM = $form_name;
846
847   return array($form_start, $form_end);  
848   }
849
850
851 function format_email_recipient($email, $name='')
852   {
853   if ($name && $name != $email)
854     return sprintf('%s <%s>', strpos($name, ",") ? '"'.$name.'"' : $name, $email);
855   else
856     return $email;
857   }
858
859
fd8c50 860 function rcmail_charset_pulldown($selected='ISO-8859-1')
T 861   {
862   $select = new select();
863   
864   
865   return $select->show($selected);
866   }
867
868
4e17e6 869 /****** get contacts for this user and add them to client scripts ********/
T 870
d7cb77 871 $sql_result = $DB->query("SELECT name, email
S 872                           FROM ".get_table_name('contacts')." WHERE  user_id=?
1cded8 873                           AND  del<>1",$_SESSION['user_id']);
4e17e6 874                                    
T 875 if ($DB->num_rows($sql_result))
876   {        
877   $a_contacts = array();
878   while ($sql_arr = $DB->fetch_assoc($sql_result))
879     if ($sql_arr['email'])
880       $a_contacts[] = format_email_recipient($sql_arr['email'], rep_specialchars_output($sql_arr['name'], 'js'));
881   
882   $OUTPUT->add_script(sprintf("$JS_OBJECT_NAME.set_env('contacts', %s);", array2js($a_contacts)));
883   }
884
885
886 parse_template('compose');
1966c5 887 ?>