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