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