thomascube
2006-08-30 107bde9cfd9a0392d18544b5a433552ce6f2f0a6
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
41fa0b 56 rcube_add_label('nosubject', 'norecipientwarning', 'nosubjectwarning', 'nobodywarning', 'notsentwarning', 'savingmessage', 'sendingmessage', 'messagesaved');
10a699 57
T 58
8d4bcd 59 // get reference message and set compose mode
T 60 if ($msg_uid = get_input_value('_reply_uid', RCUBE_INPUT_GET))
61   $compose_mode = RCUBE_COMPOSE_REPLY;
62 else if ($msg_uid = get_input_value('_forward_uid', RCUBE_INPUT_GET))
63   $compose_mode = RCUBE_COMPOSE_FORWARD;
64 else if ($msg_uid = get_input_value('_draft_uid', RCUBE_INPUT_GET))
65   $compose_mode = RCUBE_COMPOSE_DRAFT;
66
67
68 if (!empty($msg_uid))
4e17e6 69   {
T 70   // similar as in program/steps/mail/show.inc
8d4bcd 71   $MESSAGE = array('UID' => $msg_uid);
T 72   $MESSAGE['headers'] = &$IMAP->get_headers($msg_uid);
73   $MESSAGE['structure'] = &$IMAP->get_structure($msg_uid);  
4e17e6 74   $MESSAGE['subject'] = $IMAP->decode_header($MESSAGE['headers']->subject);
8d4bcd 75   $MESSAGE['parts'] = $IMAP->get_mime_numbers($MESSAGE['structure']);
1966c5 76
8d4bcd 77   if ($compose_mode == RCUBE_COMPOSE_REPLY)
4e17e6 78     {
8d4bcd 79     $_SESSION['compose']['reply_uid'] = $msg_uid;
T 80     $_SESSION['compose']['reply_msgid'] = $MESSAGE['headers']->messageID;
81     $_SESSION['compose']['references']  = $MESSAGE['headers']->reference;
82     $_SESSION['compose']['references'] .= !empty($MESSAGE['headers']->reference) ? ' ' : '';
83     $_SESSION['compose']['references'] .= $MESSAGE['headers']->messageID;
583f1c 84
8d4bcd 85     if (!empty($_GET['_all']))
T 86       $MESSAGE['reply_all'] = 1;
4e17e6 87     }
8d4bcd 88   else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
4e17e6 89     {
8d4bcd 90     $_SESSION['compose']['forward_uid'] = $msg_uid;
4e17e6 91     }
8d4bcd 92   else if ($compose_mode == RCUBE_COMPOSE_DRAFT)
1966c5 93     {
8d4bcd 94     $_SESSION['compose']['draft_uid'] = $msg_uid;
1966c5 95     }
S 96
4e17e6 97   }
T 98
99 /****** compose mode functions ********/
100
101
102 function rcmail_compose_headers($attrib)
103   {
8d4bcd 104   global $IMAP, $MESSAGE, $DB, $compose_mode;
583f1c 105   static $sa_recipients = array();
4e17e6 106
T 107   list($form_start, $form_end) = get_form_tags($attrib);
108   
109   $out = '';
110   $part = strtolower($attrib['part']);
111   
112   switch ($part)
113     {
114     case 'from':
1cded8 115       return rcmail_compose_header_from($attrib);
4e17e6 116
T 117     case 'to':
118       $fname = '_to';
119       $header = 'to';
1cded8 120
4e17e6 121       // we have contact id's as get parameters
4fd971 122       if (!empty($_GET['_to']) && preg_match('/^[0-9]+(,[0-9]+)*$/', $_GET['_to']))
4e17e6 123         {
T 124         $a_recipients = array();
d7cb77 125         $sql_result = $DB->query("SELECT name, email
1cded8 126                                   FROM ".get_table_name('contacts')."
T 127                                   WHERE user_id=?
128                                   AND    del<>1
d7cb77 129                                   AND    contact_id IN (".$_GET['_to'].")",
S 130                                   $_SESSION['user_id']);
4e17e6 131                                          
T 132         while ($sql_arr = $DB->fetch_assoc($sql_result))
133           $a_recipients[] = format_email_recipient($sql_arr['email'], $sql_arr['name']);
134           
135         if (sizeof($a_recipients))
136           $fvalue = join(', ', $a_recipients);
137         }
597170 138       else if (!empty($_GET['_to']))
8d4bcd 139         $fvalue = get_input_value('_to', RCUBE_INPUT_GET);
4e17e6 140         
T 141     case 'cc':
142       if (!$fname)
143         {
144         $fname = '_cc';
583f1c 145         $header = 'cc';
4e17e6 146         }
T 147     case 'bcc':
148       if (!$fname)
149         $fname = '_bcc';
150         
317219 151       $allow_attrib = array('id', 'class', 'style', 'cols', 'rows', 'wrap', 'tabindex');
4e17e6 152       $field_type = 'textarea';            
T 153       break;
154
155     case 'replyto':
156     case 'reply-to':
157       $fname = '_replyto';
317219 158       $allow_attrib = array('id', 'class', 'style', 'size', 'tabindex');
4e17e6 159       $field_type = 'textfield';
T 160       break;
161     
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)
4e17e6 168     {
T 169     // get recipent address(es) out of the message headers
8d4bcd 170     if ($header=='to' && !empty($MESSAGE['headers']->replyto))
T 171       $fvalue = $IMAP->decode_header($MESSAGE['headers']->replyto);
58e360 172
8d4bcd 173     else if ($header=='to' && !empty($MESSAGE['headers']->from))
T 174       $fvalue = $IMAP->decode_header($MESSAGE['headers']->from);
58e360 175
583f1c 176     // add recipent of original message if reply to all
8d4bcd 177     else if ($header=='cc' && !empty($MESSAGE['reply_all']))
583f1c 178       {
8d4bcd 179       if ($v = $IMAP->decode_header($MESSAGE['headers']->to))
T 180         $fvalue .= $v;
583f1c 181
8d4bcd 182       if ($v = $IMAP->decode_header($MESSAGE['headers']->cc))
T 183         $fvalue .= (!empty($fvalue) ? ', ' : '') . $v;
583f1c 184       }
T 185
4e17e6 186     // split recipients and put them back together in a unique way
583f1c 187     if (!empty($fvalue))
T 188       {
189       $to_addresses = $IMAP->decode_address_list($fvalue);
190       $fvalue = '';
191       foreach ($to_addresses as $addr_part)
192         {
8d4bcd 193         if (!in_array($addr_part['mailto'], $sa_recipients) && (!$MESSAGE['FROM'] || !in_array($addr_part['mailto'], $MESSAGE['FROM'])))
583f1c 194           {
T 195           $fvalue .= (strlen($fvalue) ? ', ':'').$addr_part['string'];
196           $sa_recipients[] = $addr_part['mailto'];
197           }
198         }
199       }
4e17e6 200     }
8d4bcd 201   else if ($header && $compose_mode == RCUBE_COMPOSE_DRAFT)
1966c5 202     {
S 203     // get drafted headers
8d4bcd 204     if ($header=='to' && !empty($MESSAGE['headers']->to))
T 205       $fvalue = $IMAP->decode_header($MESSAGE['headers']->to);
1966c5 206
8d4bcd 207     if ($header=='cc' && !empty($MESSAGE['headers']->cc))
T 208       $fvalue = $IMAP->decode_header($MESSAGE['headers']->cc);
1966c5 209
8d4bcd 210     if ($header=='bcc' && !empty($MESSAGE['headers']->bcc))
T 211       $fvalue = $IMAP->decode_header($MESSAGE['headers']->bcc);
1966c5 212
S 213     }
583f1c 214
4e17e6 215         
T 216   if ($fname && $field_type)
217     {
218     // pass the following attributes to the form class
219     $field_attrib = array('name' => $fname);
220     foreach ($attrib as $attr => $value)
221       if (in_array($attr, $allow_attrib))
222         $field_attrib[$attr] = $value;
223
224     // create teaxtarea object
225     $input = new $field_type($field_attrib);
226     $out = $input->show($fvalue);    
227     }
228   
229   if ($form_start)
230     $out = $form_start.$out;
1966c5 231
4e17e6 232   return $out;  
T 233   }
234
235
1cded8 236
T 237 function rcmail_compose_header_from($attrib)
4e17e6 238   {
8d4bcd 239   global $IMAP, $MESSAGE, $DB, $OUTPUT, $JS_OBJECT_NAME, $compose_mode;
1cded8 240     
T 241   // pass the following attributes to the form class
242   $field_attrib = array('name' => '_from');
243   foreach ($attrib as $attr => $value)
244     if (in_array($attr, array('id', 'class', 'style', 'size', 'tabindex')))
245       $field_attrib[$attr] = $value;
4e17e6 246
1cded8 247   // extract all recipients of the reply-message
T 248   $a_recipients = array();
8d4bcd 249   if ($compose_mode == RCUBE_COMPOSE_REPLY && is_object($MESSAGE['headers']))
1cded8 250     {
8d4bcd 251     $MESSAGE['FROM'] = array();
58e360 252
8d4bcd 253     $a_to = $IMAP->decode_address_list($MESSAGE['headers']->to);        
1cded8 254     foreach ($a_to as $addr)
T 255       {
256       if (!empty($addr['mailto']))
257         $a_recipients[] = $addr['mailto'];
258       }
4e17e6 259
8d4bcd 260     if (!empty($MESSAGE['headers']->cc))
1cded8 261       {
8d4bcd 262       $a_cc = $IMAP->decode_address_list($MESSAGE['headers']->cc);
1cded8 263       foreach ($a_cc as $addr)
T 264         {
265         if (!empty($addr['mailto']))
266           $a_recipients[] = $addr['mailto'];
267         }
268       }
269     }
4e17e6 270
1cded8 271   // get this user's identities
T 272   $sql_result = $DB->query("SELECT identity_id, name, email, signature
273                             FROM   ".get_table_name('identities')."
274                             WHERE user_id=?
275                             AND    del<>1
276                             ORDER BY ".$DB->quoteIdentifier('standard')." DESC, name ASC",
277                            $_SESSION['user_id']);
278                                    
279   if ($DB->num_rows($sql_result))
280     {
281     $from_id = 0;
282     $a_signatures = array();
283     
284     $field_attrib['onchange'] = "$JS_OBJECT_NAME.change_identity(this)";
285     $select_from = new select($field_attrib);
286     
287     while ($sql_arr = $DB->fetch_assoc($sql_result))
288       {
289       $select_from->add(format_email_recipient($sql_arr['email'], $sql_arr['name']), $sql_arr['identity_id']);
4e17e6 290
1cded8 291       // add signature to array
T 292       if (!empty($sql_arr['signature']))
293         $a_signatures[$sql_arr['identity_id']] = $sql_arr['signature'];
294       
295       // set identity if it's one of the reply-message recipients
296       if (in_array($sql_arr['email'], $a_recipients))
297         $from_id = $sql_arr['identity_id'];
58e360 298         
8d4bcd 299       if ($compose_mode == RCUBE_COMPOSE_REPLY && is_array($MESSAGE['FROM']))
T 300         $MESSAGE['FROM'][] = $sql_arr['email'];
1966c5 301
8d4bcd 302       if ($compose_mode == RCUBE_COMPOSE_DRAFT && strstr($MESSAGE['headers']->from, $sql_arr['email']))
1966c5 303         $from_id = $sql_arr['identity_id'];
S 304
1cded8 305       }
4e17e6 306
1cded8 307     // overwrite identity selection with post parameter
T 308     if (isset($_POST['_from']))
8d4bcd 309       $from_id = get_input_value('_from', RCUBE_INPUT_POST);
4e17e6 310
1cded8 311     $out = $select_from->show($from_id);
T 312     
4e17e6 313
1cded8 314     // add signatures to client
T 315     $OUTPUT->add_script(sprintf("%s.set_env('signatures', %s);", $JS_OBJECT_NAME, array2js($a_signatures)));  
316     }
317   else
318     {
319     $input_from = new textfield($field_attrib);
320     $out = $input_from->show($_POST['_from']);
321     }
1966c5 322   
1cded8 323   if ($form_start)
T 324     $out = $form_start.$out;
4e17e6 325
1cded8 326   return $out;
4e17e6 327   }
T 328
1cded8 329   
4e17e6 330
T 331 function rcmail_compose_body($attrib)
332   {
8d4bcd 333   global $CONFIG, $OUTPUT, $MESSAGE, $JS_OBJECT_NAME, $compose_mode;
4e17e6 334   
T 335   list($form_start, $form_end) = get_form_tags($attrib);
336   unset($attrib['form']);
dd53e2 337   
T 338   if (empty($attrib['id']))
339     $attrib['id'] = 'rcmComposeMessage';
4e17e6 340   
T 341   $attrib['name'] = '_message';
342   $textarea = new textarea($attrib);
343
344   $body = '';
345   
346   // use posted message body
597170 347   if (!empty($_POST['_message']))
ea7c46 348     $body = get_input_value('_message', RCUBE_INPUT_POST, TRUE);
4e17e6 349     
T 350   // compose reply-body
8d4bcd 351   else if ($compose_mode == RCUBE_COMPOSE_REPLY)
4e17e6 352     {
8d4bcd 353     $body = rcmail_first_text_part($MESSAGE);
4e17e6 354     if (strlen($body))
T 355       $body = rcmail_create_reply_body($body);
356     }
357
358   // forward message body inline
8d4bcd 359   else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
4e17e6 360     {
8d4bcd 361     $body = rcmail_first_text_part($MESSAGE);
4e17e6 362     if (strlen($body))
T 363       $body = rcmail_create_forward_body($body);
364     }
1966c5 365
S 366   // forward message body inline
8d4bcd 367   else if ($compose_mode == RCUBE_COMPOSE_DRAFT)
1966c5 368     {
8d4bcd 369     $body = rcmail_first_text_part($MESSAGE);
1966c5 370     if (strlen($body))
S 371       $body = rcmail_create_draft_body($body);
372     }
4e17e6 373   
T 374   $out = $form_start ? "$form_start\n" : '';
1966c5 375
8d4bcd 376   $saveid = new hiddenfield(array('name' => '_draft_saveid', 'value' => str_replace(array('<','>'),"",$MESSAGE['headers']->messageID) ));
f0f98f 377   $out .= $saveid->show();
1966c5 378
S 379   $drafttoggle = new hiddenfield(array('name' => '_draft', 'value' => 'yes'));
380   $out .= $drafttoggle->show();
381
4e17e6 382   $out .= $textarea->show($body);
T 383   $out .= $form_end ? "\n$form_end" : '';
dd53e2 384   
T 385   // include GoogieSpell
ed5d29 386   if (!empty($CONFIG['enable_spellcheck']))
T 387     {
996066 388     $lang_set = '';
T 389     if (!empty($CONFIG['spellcheck_languages']) && is_array($CONFIG['spellcheck_languages']))
390       $lang_set = "googie.setLanguages(".array2js($CONFIG['spellcheck_languages']).");\n";
391     
ed5d29 392     $OUTPUT->include_script('googiespell.js');
T 393     $OUTPUT->add_script(sprintf("var googie = new GoogieSpell('\$__skin_path/images/googiespell/','%s&_action=spell&lang=');\n".
394                                 "googie.lang_chck_spell = \"%s\";\n".
395                                 "googie.lang_rsm_edt = \"%s\";\n".
396                                 "googie.lang_close = \"%s\";\n".
397                                 "googie.lang_revert = \"%s\";\n".
a5d3e6 398                                 "googie.lang_no_error_found = \"%s\";\n%s".
T 399                                 "googie.setCurrentLanguage('%s');\n".
ed5d29 400                                 "googie.decorateTextarea('%s');\n".
T 401                                 "%s.set_env('spellcheck', googie);",
402                                 $GLOBALS['COMM_PATH'],
403                                 rep_specialchars_output(rcube_label('checkspelling')),
404                                 rep_specialchars_output(rcube_label('resumeediting')),
405                                 rep_specialchars_output(rcube_label('close')),
406                                 rep_specialchars_output(rcube_label('revertto')),
407                                 rep_specialchars_output(rcube_label('nospellerrors')),
996066 408                                 $lang_set,
a5d3e6 409                                 substr($_SESSION['user_lang'], 0, 2),
ed5d29 410                                 $attrib['id'],
T 411                                 $JS_OBJECT_NAME), 'foot');
412
413     rcube_add_label('checking');
414     }
f0f98f 415  
41fa0b 416   $out .= "\n".'<iframe name="savetarget" src="program/blank.gif" style="width:0;height:0;visibility:hidden;"></iframe>';
T 417
4e17e6 418   return $out;
T 419   }
420
421
422 function rcmail_create_reply_body($body)
423   {
8d4bcd 424   global $IMAP, $MESSAGE;
4e17e6 425
T 426   // soft-wrap message first
427   $body = wordwrap($body, 75);
428   
429   // split body into single lines
430   $a_lines = preg_split('/\r?\n/', $body);
431   
432   // add > to each line
433   for($n=0; $n<sizeof($a_lines); $n++)
434     {
435     if (strpos($a_lines[$n], '>')===0)
436       $a_lines[$n] = '>'.$a_lines[$n];
437     else
438       $a_lines[$n] = '> '.$a_lines[$n];
439     }
440  
441   $body = join("\n", $a_lines);
442
443   // add title line
444   $pefix = sprintf("\n\n\nOn %s, %s wrote:\n",
8d4bcd 445            $MESSAGE['headers']->date,
T 446            $IMAP->decode_header($MESSAGE['headers']->from));
1cded8 447            
T 448
449   // try to remove the signature
749b07 450   if ($sp = strrpos($body, '-- '))
1cded8 451     {
T 452     if ($body{$sp+3}==' ' || $body{$sp+3}=="\n" || $body{$sp+3}=="\r")
453       $body = substr($body, 0, $sp-1);
454     }
4e17e6 455
T 456   return $pefix.$body;
457   }
458
459
460 function rcmail_create_forward_body($body)
461   {
8d4bcd 462   global $IMAP, $MESSAGE;
4e17e6 463
T 464   // soft-wrap message first
465   $body = wordwrap($body, 80);
466   
467   $prefix = sprintf("\n\n\n-------- Original Message --------\nSubject: %s\nDate: %s\nFrom: %s\nTo: %s\n\n",
8d4bcd 468                    $MESSAGE['subject'],
T 469                    $MESSAGE['headers']->date,
470                    $IMAP->decode_header($MESSAGE['headers']->from),
471                    $IMAP->decode_header($MESSAGE['headers']->to));
472                    
597170 473   // add attachments
8d4bcd 474   if (!isset($_SESSION['compose']['forward_attachments']) &&
T 475       is_array($MESSAGE['parts']) && sizeof($MESSAGE['parts'])>1)
476     rcmail_write_compose_attachments($MESSAGE);
597170 477
4e17e6 478   return $prefix.$body;
T 479   }
480
8d4bcd 481
1966c5 482 function rcmail_create_draft_body($body)
S 483   {
8d4bcd 484   global $IMAP, $MESSAGE;
1966c5 485     
S 486   // add attachments
8d4bcd 487   if (!isset($_SESSION['compose']['forward_attachments']) &&
T 488       is_array($MESSAGE['parts']) && sizeof($MESSAGE['parts'])>1)
489     rcmail_write_compose_attachments($MESSAGE);
1966c5 490
S 491   return $body;
8d4bcd 492   }
T 493   
494   
495 function rcmail_write_compose_attachments(&$message)
496   {
497   global $IMAP;
498     
499   $temp_dir = rcmail_create_compose_tempdir();
500
501   if (!is_array($_SESSION['compose']['attachments']))
502     $_SESSION['compose']['attachments'] = array();
503   
504   foreach ($message['parts'] as $pid => $part)
505     {
506     if ($part->ctype_primary != 'message' && $part->ctype_primary != 'text' &&
507         ($part->disposition=='attachment' || $part->disposition=='inline' || $part->headers['content-id'] ||
508          (empty($part->disposition) && ($part->d_parameters['filename'] || $part->ctype_parameters['name']))))
509       {
510       $tmp_path = tempnam($temp_dir, 'rcmAttmnt');
511       if ($fp = fopen($tmp_path, 'w'))
512         {
513         fwrite($fp, $IMAP->get_message_part($message['UID'], $pid, $part->encoding));
514         fclose($fp);
515         
516         $filename = !empty($part->d_parameters['filename']) ? $part->d_parameters['filename'] :
517                      (!empty($part->ctype_parameters['name']) ? $part->ctype_parameters['name'] :
518                       (!empty($part->headers['content-description']) ? $part->headers['content-description'] : 'file'));
519
520         $_SESSION['compose']['attachments'][] = array(
521           'name' => rcube_imap::decode_mime_string($filename),
522           'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
523           'path' => $tmp_path
524           );
525         }
526       }
527     }
528     
529   $_SESSION['compose']['forward_attachments'] = TRUE;
1966c5 530   }
4e17e6 531
T 532
533 function rcmail_compose_subject($attrib)
534   {
8d4bcd 535   global $CONFIG, $MESSAGE, $compose_mode;
4e17e6 536   
T 537   list($form_start, $form_end) = get_form_tags($attrib);
538   unset($attrib['form']);
539   
540   $attrib['name'] = '_subject';
541   $textfield = new textfield($attrib);
542
543   $subject = '';
544
545   // use subject from post
597170 546   if (isset($_POST['_subject']))
01c86f 547     $subject = get_input_value('_subject', RCUBE_INPUT_POST, TRUE);
4e17e6 548     
T 549   // create a reply-subject
8d4bcd 550   else if ($compose_mode == RCUBE_COMPOSE_REPLY)
520c36 551     {
8d4bcd 552     if (eregi('^re:', $MESSAGE['subject']))
T 553       $subject = $MESSAGE['subject'];
520c36 554     else
8d4bcd 555       $subject = 'Re: '.$MESSAGE['subject'];
520c36 556     }
4e17e6 557
T 558   // create a forward-subject
8d4bcd 559   else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
09941e 560     {
8d4bcd 561     if (eregi('^fwd:', $MESSAGE['subject']))
T 562       $subject = $MESSAGE['subject'];
09941e 563     else
8d4bcd 564       $subject = 'Fwd: '.$MESSAGE['subject'];
09941e 565     }
4e17e6 566
1966c5 567   // creeate a draft-subject
8d4bcd 568   else if ($compose_mode == RCUBE_COMPOSE_DRAFT)
T 569     $subject = $MESSAGE['subject'];
4e17e6 570   
T 571   $out = $form_start ? "$form_start\n" : '';
572   $out .= $textfield->show($subject);
573   $out .= $form_end ? "\n$form_end" : '';
574          
575   return $out;
576   }
577
578
579 function rcmail_compose_attachment_list($attrib)
580   {
581   global $OUTPUT, $JS_OBJECT_NAME;
582   
583   // add ID if not given
584   if (!$attrib['id'])
585     $attrib['id'] = 'rcmAttachmentList';
586   
587   // allow the following attributes to be added to the <ul> tag
588   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style'));
589  
590   $out = '<ul'. $attrib_str . ">\n";
591   
592   if (is_array($_SESSION['compose']['attachments']))
593     {
a894ba 594     if ($attrib['deleteicon'])
aade7b 595       $button = sprintf('<img src="%s%s" alt="%s" border="0" style="padding-right:2px;vertical-align:middle" />',
508442 596                         $CONFIG['skin_path'],
T 597                         $attrib['deleteicon'],
598                         rcube_label('delete'));
a894ba 599     else
S 600       $button = rcube_label('delete');
601
aade7b 602     foreach ($_SESSION['compose']['attachments'] as $id => $a_prop)
T 603       $out .= sprintf('<li id="rcmfile%d"><a href="#delete" onclick="return %s.command(\'remove-attachment\',\'rcmfile%d\', this)" title="%s">%s</a>%s</li>',
604                       $id,
508442 605                       $JS_OBJECT_NAME,
aade7b 606                       $id,
508442 607                       rcube_label('delete'), 
aade7b 608                       $button,
T 609                       rep_specialchars_output($a_prop['name']));
4e17e6 610     }
T 611
612   $OUTPUT->add_script(sprintf("%s.gui_object('attachmentlist', '%s');", $JS_OBJECT_NAME, $attrib['id']));  
613     
614   $out .= '</ul>';
615   return $out;
616   }
617
618
619
620 function rcmail_compose_attachment_form($attrib)
621   {
622   global $OUTPUT, $JS_OBJECT_NAME, $SESS_HIDDEN_FIELD;
623
624   // add ID if not given
625   if (!$attrib['id'])
626     $attrib['id'] = 'rcmUploadbox';
627   
628   // allow the following attributes to be added to the <div> tag
629   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style'));
630   $input_field = rcmail_compose_attachment_field(array());
631   $label_send = rcube_label('upload');
632   $label_close = rcube_label('close');
633   
634   $out = <<<EOF
635 <div$attrib_str>
636 <form action="./" method="post" enctype="multipart/form-data">
637 $SESS_HIDDEN_FIELD
638 $input_field<br />
639 <input type="button" value="$label_close" class="button" onclick="document.getElementById('$attrib[id]').style.visibility='hidden'" />
640 <input type="button" value="$label_send" class="button" onclick="$JS_OBJECT_NAME.command('send-attachment', this.form)" />
641 </form>
642 </div>
643 EOF;
644
645   
646   $OUTPUT->add_script(sprintf("%s.gui_object('uploadbox', '%s');", $JS_OBJECT_NAME, $attrib['id']));  
647   return $out;
648   }
649
650
651 function rcmail_compose_attachment_field($attrib)
652   {
653   // allow the following attributes to be added to the <input> tag
654   $attrib_str = create_attrib_string($attrib, array('id', 'class', 'style', 'size'));
655  
656   $out = '<input type="file" name="_attachments[]"'. $attrib_str . " />";
657   return $out;
658   }
659
66e2bf 660
4e17e6 661 function rcmail_priority_selector($attrib)
T 662   {
663   list($form_start, $form_end) = get_form_tags($attrib);
664   unset($attrib['form']);
665   
666   $attrib['name'] = '_priority';
667   $selector = new select($attrib);
668
669   $selector->add(array(rcube_label('lowest'),
670                        rcube_label('low'),
671                        rcube_label('normal'),
672                        rcube_label('high'),
673                        rcube_label('highest')),
7902df 674                  array(5, 4, 0, 2, 1));
4e17e6 675                  
597170 676   $sel = isset($_POST['_priority']) ? $_POST['_priority'] : 0;
4e17e6 677
T 678   $out = $form_start ? "$form_start\n" : '';
679   $out .= $selector->show($sel);
680   $out .= $form_end ? "\n$form_end" : '';
681          
682   return $out;
683   }
684
685
620439 686 function rcmail_receipt_checkbox($attrib)
T 687   {
688   list($form_start, $form_end) = get_form_tags($attrib);
689   unset($attrib['form']);
66e2bf 690   
T 691   if (!isset($attrib['id']))
692     $attrib['id'] = 'receipt';  
620439 693
T 694   $attrib['name'] = '_receipt';
66e2bf 695   $attrib['value'] = '1';
T 696   $checkbox = new checkbox($attrib);
620439 697
T 698   $out = $form_start ? "$form_start\n" : '';
699   $out .= $checkbox->show(0);
700   $out .= $form_end ? "\n$form_end" : '';
701
702   return $out;
703   }
704
705
4e17e6 706 function get_form_tags($attrib)
T 707   {
708   global $CONFIG, $OUTPUT, $JS_OBJECT_NAME, $MESSAGE_FORM, $SESS_HIDDEN_FIELD;  
709
710   $form_start = '';
711   if (!strlen($MESSAGE_FORM))
712     {
713     $hiddenfields = new hiddenfield(array('name' => '_task', 'value' => $GLOBALS['_task']));
714     $hiddenfields->add(array('name' => '_action', 'value' => 'send'));
1966c5 715
597170 716     $form_start = empty($attrib['form']) ? '<form name="form" action="./" method="post">' : '';
4e17e6 717     $form_start .= "\n$SESS_HIDDEN_FIELD\n";
T 718     $form_start .= $hiddenfields->show();
719     }
720     
721   $form_end = (strlen($MESSAGE_FORM) && !strlen($attrib['form'])) ? '</form>' : '';
597170 722   $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
4e17e6 723   
T 724   if (!strlen($MESSAGE_FORM))
725     $OUTPUT->add_script("$JS_OBJECT_NAME.gui_object('messageform', '$form_name');");
726   
727   $MESSAGE_FORM = $form_name;
728
729   return array($form_start, $form_end);  
730   }
731
732
733 function format_email_recipient($email, $name='')
734   {
735   if ($name && $name != $email)
736     return sprintf('%s <%s>', strpos($name, ",") ? '"'.$name.'"' : $name, $email);
737   else
738     return $email;
739   }
740
741
fd8c50 742 function rcmail_charset_pulldown($selected='ISO-8859-1')
T 743   {
744   $select = new select();
745   
746   
747   return $select->show($selected);
748   }
749
750
4e17e6 751 /****** get contacts for this user and add them to client scripts ********/
T 752
d7cb77 753 $sql_result = $DB->query("SELECT name, email
S 754                           FROM ".get_table_name('contacts')." WHERE  user_id=?
1cded8 755                           AND  del<>1",$_SESSION['user_id']);
4e17e6 756                                    
T 757 if ($DB->num_rows($sql_result))
758   {        
759   $a_contacts = array();
760   while ($sql_arr = $DB->fetch_assoc($sql_result))
761     if ($sql_arr['email'])
762       $a_contacts[] = format_email_recipient($sql_arr['email'], rep_specialchars_output($sql_arr['name'], 'js'));
763   
764   $OUTPUT->add_script(sprintf("$JS_OBJECT_NAME.set_env('contacts', %s);", array2js($a_contacts)));
765   }
766
767
768 parse_template('compose');
1966c5 769 ?>