alecpl
2011-02-28 29c54229cfbc104930e7743cecc212f53aed8a15
commit | author | age
4e17e6 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/mail/compose.inc                                        |
6  |                                                                       |
e019f2 7  | This file is part of the Roundcube Webmail client                     |
f5e7b3 8  | Copyright (C) 2005-2009, The Roundcube Dev Team                       |
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);
069704 26 define('RCUBE_COMPOSE_EDIT', 0x0109);
8d4bcd 27
f0f98f 28 $MESSAGE_FORM = NULL;
8d4bcd 29 $MESSAGE = NULL;
f0f98f 30
86df15 31 // Nothing below is called during message composition, only at "new/forward/reply/draft" initialization or
T 32 // if a compose-ID is given (i.e. when the compose step is opened in a new window/tab).
33 // Since there are many ways to leave the compose page improperly, it seems necessary to clean-up an old
f0f98f 34 // compose when a "new/forward/reply/draft" is called - otherwise the old session attachments will appear
S 35
ace851 36 $MESSAGE_ID = get_input_value('_id', RCUBE_INPUT_GET);
A 37 if (!is_array($_SESSION['compose']) || $_SESSION['compose']['id'] != $MESSAGE_ID)
4315b0 38 {
86df15 39   rcmail_compose_cleanup();
ace851 40
A 41   // Infinite redirect prevention in case of broken session (#1487028)
42   if ($MESSAGE_ID)
43     raise_error(array('code' => 500, 'type' => 'php',
44       'file' => __FILE__, 'line' => __LINE__,
45       'message' => "Invalid session"), true, true);
46
48958e 47   $_SESSION['compose'] = array(
b48d9b 48     'id' => uniqid(mt_rand()),
759696 49     'param' => request2param(RCUBE_INPUT_GET),
7d8e16 50     'mailbox' => $IMAP->get_mailbox_name(),
48958e 51   );
c719f3 52   
5f314d 53   // process values like "mailto:foo@bar.com?subject=new+message&cc=another"
759696 54   if ($_SESSION['compose']['param']['to']) {
3e8898 55     // #1486037: remove "mailto:" prefix
A 56     $_SESSION['compose']['param']['to'] = preg_replace('/^mailto:/i', '', $_SESSION['compose']['param']['to']);
759696 57     $mailto = explode('?', $_SESSION['compose']['param']['to']);
5f314d 58     if (count($mailto) > 1) {
759696 59       $_SESSION['compose']['param']['to'] = $mailto[0];
5f314d 60       parse_str($mailto[1], $query);
T 61       foreach ($query as $f => $val)
759696 62         $_SESSION['compose']['param'][$f] = $val;
5f314d 63     }
T 64   }
759696 65   
814905 66   // select folder where to save the sent message
T 67   $_SESSION['compose']['param']['sent_mbox'] = $RCMAIL->config->get('sent_mbox');
68   
759696 69   // pipe compose parameters thru plugins
T 70   $plugin = $RCMAIL->plugins->exec_hook('message_compose', $_SESSION['compose']);
814905 71   $_SESSION['compose']['param'] = array_merge($_SESSION['compose']['param'], $plugin['param']);
ceeab9 72
76791c 73   // add attachments listed by message_compose hook
T 74   if (is_array($plugin['attachments'])) {
75     foreach ($plugin['attachments'] as $attach) {
76       // we have structured data
77       if (is_array($attach)) {
78         $attachment = $attach;
79       }
80       // only a file path is given
81       else {
82         $filename = basename($attach);
83         $attachment = array(
84           'name' => $filename,
85           'mimetype' => rc_mime_content_type($attach, $filename),
86           'path' => $attach
87         );
88       }
89       
90       // save attachment if valid
91       if (($attachment['data'] && $attachment['name']) || ($attachment['path'] && file_exists($attachment['path']))) {
e6ce00 92         $attachment = rcmail::get_instance()->plugins->exec_hook('attachment_save', $attachment);
76791c 93       }
T 94       
95       if ($attachment['status'] && !$attachment['abort']) {
96         unset($attachment['data'], $attachment['status'], $attachment['abort']);
97         $_SESSION['compose']['attachments'][$attachment['id']] = $attachment;
98       }
99     }
100   }
5f314d 101
90e708 102   // check if folder for saving sent messages exists and is subscribed (#1486802)
eeb85f 103   if ($sent_folder = $_SESSION['compose']['param']['sent_mbox']) {
A 104     rcmail_check_sent_folder($sent_folder, true);
90e708 105   }
T 106
c719f3 107   // redirect to a unique URL with all parameters stored in session
T 108   $OUTPUT->redirect(array('_action' => 'compose', '_id' => $_SESSION['compose']['id']));
4315b0 109 }
76791c 110
4e17e6 111
10a699 112 // add some labels to client
3f9712 113 $OUTPUT->add_label('nosubject', 'nosenderwarning', 'norecipientwarning', 'nosubjectwarning', 'cancel',
V 114     'nobodywarning', 'notsentwarning', 'notuploadedwarning', 'savingmessage', 'sendingmessage', 
c296b8 115     'messagesaved', 'converting', 'editorwarning', 'searching', 'uploading', 'fileuploaderror',
A 116     'autocompletechars');
10a699 117
272705 118 // add config parameters to client script
A 119 if (!empty($CONFIG['drafts_mbox'])) {
120   $OUTPUT->set_env('drafts_mailbox', $CONFIG['drafts_mbox']);
121   $OUTPUT->set_env('draft_autosave', $CONFIG['draft_autosave']);
122 }
cf6a83 123 // set current mailbox in client environment
A 124 $OUTPUT->set_env('mailbox', $IMAP->get_mailbox_name());
0207c4 125 $OUTPUT->set_env('sig_above', $CONFIG['sig_above']);
50f56d 126 $OUTPUT->set_env('top_posting', $CONFIG['top_posting']);
c296b8 127 $OUTPUT->set_env('autocomplete_min_length', $CONFIG['autocomplete_min_length']);
10a699 128
8d4bcd 129 // get reference message and set compose mode
759696 130 if ($msg_uid = $_SESSION['compose']['param']['reply_uid'])
8d4bcd 131   $compose_mode = RCUBE_COMPOSE_REPLY;
759696 132 else if ($msg_uid = $_SESSION['compose']['param']['forward_uid'])
8d4bcd 133   $compose_mode = RCUBE_COMPOSE_FORWARD;
759696 134 else if ($msg_uid = $_SESSION['compose']['param']['uid'])
069704 135   $compose_mode = RCUBE_COMPOSE_EDIT;
759696 136 else if ($msg_uid = $_SESSION['compose']['param']['draft_uid']) {
59ee68 137   $RCMAIL->imap->set_mailbox($CONFIG['drafts_mbox']);
8d4bcd 138   $compose_mode = RCUBE_COMPOSE_DRAFT;
0b21c8 139 }
50f56d 140
0207c4 141 $config_show_sig = $RCMAIL->config->get('show_sig', 1);
T 142 if ($config_show_sig == 1)
50f56d 143   $OUTPUT->set_env('show_sig', true);
0207c4 144 else if ($config_show_sig == 2 && (empty($compose_mode) || $compose_mode == RCUBE_COMPOSE_EDIT || $compose_mode == RCUBE_COMPOSE_DRAFT))
50f56d 145   $OUTPUT->set_env('show_sig', true);
0207c4 146 else if ($config_show_sig == 3 && ($compose_mode == RCUBE_COMPOSE_REPLY || $compose_mode == RCUBE_COMPOSE_FORWARD))
50f56d 147   $OUTPUT->set_env('show_sig', true);
0207c4 148 else
T 149   $OUTPUT->set_env('show_sig', false);
8d4bcd 150
b62049 151 // set line length for body wrapping
6b6f2e 152 $LINE_LENGTH = $RCMAIL->config->get('line_length', 72);
b62049 153
8d4bcd 154 if (!empty($msg_uid))
4315b0 155 {
4e17e6 156   // similar as in program/steps/mail/show.inc
aae0ad 157   // re-set 'prefer_html' to have possibility to use html part for compose
b62049 158   $CONFIG['prefer_html'] = $CONFIG['prefer_html'] || $CONFIG['htmleditor'] || $compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT;
8fa58e 159   $MESSAGE = new rcube_message($msg_uid);
17b5fb 160   
bc4960 161   // make sure message is marked as read
T 162   if ($MESSAGE && $MESSAGE->headers && !$MESSAGE->headers->seen)
163     $IMAP->set_flag($msg_uid, 'SEEN');
164
8fa58e 165   if (!empty($MESSAGE->headers->charset))
T 166     $IMAP->set_charset($MESSAGE->headers->charset);
17b5fb 167     
8d4bcd 168   if ($compose_mode == RCUBE_COMPOSE_REPLY)
4315b0 169   {
8d4bcd 170     $_SESSION['compose']['reply_uid'] = $msg_uid;
8fa58e 171     $_SESSION['compose']['reply_msgid'] = $MESSAGE->headers->messageID;
T 172     $_SESSION['compose']['references']  = trim($MESSAGE->headers->references . " " . $MESSAGE->headers->messageID);
583f1c 173
759696 174     if (!empty($_SESSION['compose']['param']['all']))
e25a35 175       $MESSAGE->reply_all = $_SESSION['compose']['param']['all'];
bc404f 176
a96183 177     $OUTPUT->set_env('compose_mode', 'reply');
eeb85f 178
A 179     // Save the sent message in the same folder of the message being replied to
180     if ($RCMAIL->config->get('reply_same_folder') && ($sent_folder = $_SESSION['compose']['mailbox'])
181       && rcmail_check_sent_folder($sent_folder, false)
182     ) {
183       $_SESSION['compose']['param']['sent_mbox'] = $sent_folder;
184     }
4e17e6 185   }
95fd38 186   else if ($compose_mode == RCUBE_COMPOSE_DRAFT)
A 187   {
bc404f 188     if ($MESSAGE->headers->others['x-draft-info'])
95fd38 189     {
bbc856 190       // get reply_uid/forward_uid to flag the original message when sending
bc404f 191       $info = rcmail_draftinfo_decode($MESSAGE->headers->others['x-draft-info']);
T 192
193       if ($info['type'] == 'reply')
194         $_SESSION['compose']['reply_uid'] = $info['uid'];
195       else if ($info['type'] == 'forward')
196         $_SESSION['compose']['forward_uid'] = $info['uid'];
197
198       $_SESSION['compose']['mailbox'] = $info['folder'];
eeb85f 199
A 200       // Save the sent message in the same folder of the message being replied to
201       if ($RCMAIL->config->get('reply_same_folder') && ($sent_folder = $info['folder'])
202         && rcmail_check_sent_folder($sent_folder, false)
203       ) {
204         $_SESSION['compose']['param']['sent_mbox'] = $sent_folder;
205       }
95fd38 206     }
eeb85f 207
bc404f 208     if ($MESSAGE->headers->in_reply_to)
T 209       $_SESSION['compose']['reply_msgid'] = '<'.$MESSAGE->headers->in_reply_to.'>';
210
95fd38 211     $_SESSION['compose']['references']  = $MESSAGE->headers->references;
A 212   }
4315b0 213   else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
S 214   {
215     $_SESSION['compose']['forward_uid'] = $msg_uid;
a96183 216     $OUTPUT->set_env('compose_mode', 'forward');
4315b0 217   }
S 218 }
4e17e6 219
da142b 220 $MESSAGE->compose = array();
A 221
222 // get user's identities
223 $MESSAGE->identities = $USER->list_identities();
224 if (count($MESSAGE->identities))
225 {
226   foreach ($MESSAGE->identities as $idx => $sql_arr) {
227     $email = mb_strtolower(rcube_idn_to_utf8($sql_arr['email']));
228     $MESSAGE->identities[$idx]['email_ascii'] = $sql_arr['email'];
229     $MESSAGE->identities[$idx]['email']       = $email;
230   }
231 }
232
233 // Set From field value
234 if (!empty($_POST['_from'])) {
235   $MESSAGE->compose['from'] = get_input_value('_from', RCUBE_INPUT_POST);
236 }
237 else if (!empty($_SESSION['compose']['param']['from'])) {
238   $MESSAGE->compose['from'] = $_SESSION['compose']['param']['from'];
239 }
240 else if (count($MESSAGE->identities)) {
241   // extract all recipients of the reply-message
242   $a_recipients = array();
243   if ($compose_mode == RCUBE_COMPOSE_REPLY && is_object($MESSAGE->headers))
244   {
245     $a_to = $IMAP->decode_address_list($MESSAGE->headers->to);
246     foreach ($a_to as $addr) {
247       if (!empty($addr['mailto']))
248         $a_recipients[] = strtolower($addr['mailto']);
249     }
250
251     if (!empty($MESSAGE->headers->cc)) {
252       $a_cc = $IMAP->decode_address_list($MESSAGE->headers->cc);
253       foreach ($a_cc as $addr) {
254         if (!empty($addr['mailto']))
255           $a_recipients[] = strtolower($addr['mailto']);
256       }
257     }
258   }
259
260   $from_idx         = null;
261   $default_identity = 0;
262   $return_path      = $MESSAGE->headers->others['return-path'];
263
264   // Select identity
265   foreach ($MESSAGE->identities as $idx => $sql_arr) {
266     // save default identity ID
267     if ($sql_arr['standard']) {
268       $default_identity = $idx;
269     }
270     // we need ascii here
271     $email = $sql_arr['email_ascii'];
272     $ident = format_email_recipient($email, $sql_arr['name']);
273
274     // select identity
275     if (in_array($compose_mode, array(RCUBE_COMPOSE_DRAFT, RCUBE_COMPOSE_EDIT))) {
276       if ($MESSAGE->headers->from == $ident) {
277         $from_idx = $idx;
278         break;
279       }
280     }
281     // reply to self, force To header value
282     else if ($compose_mode == RCUBE_COMPOSE_REPLY && $MESSAGE->headers->from == $ident) {
283       $from_idx = $idx;
284       $MESSAGE->compose['to'] = $MESSAGE->headers->to;
285       break;
286     }
287     // set identity if it's one of the reply-message recipients
288     else if (in_array($email, $a_recipients) && ($from_idx === null || $sql_arr['standard'])) {
289       $from_idx = $idx;
290     }
291     // set identity when replying to mailing list
292     else if (strpos($return_path, str_replace('@', '=', $email).'@') !== false) {
293       $from_idx = $idx;
294     }
295   }
296
297   // Still no ID, use first identity
298   if ($from_idx === null) {
299     $from_idx = $default_identity;
300   }
301
302   $ident   = $MESSAGE->identities[$from_idx];
303   $from_id = $ident['identity_id'];
304
305   $MESSAGE->compose['from_email'] = $ident['email'];
306   $MESSAGE->compose['from']       = $from_id;
307 }
308
309 // Set other headers
310 $a_recipients = array();
311 $parts        = array('to', 'cc', 'bcc', 'replyto', 'followupto');
312
313 foreach ($parts as $header) {
314   $fvalue = '';
315
316   // we have a set of recipients stored is session
317   if ($header == 'to' && ($mailto_id = $_SESSION['compose']['param']['mailto'])
318       && $_SESSION['mailto'][$mailto_id]
319   ) {
320     $fvalue = urldecode($_SESSION['mailto'][$mailto_id]);
321   }
322   else if (!empty($_POST['_'.$header])) {
323     $fvalue = get_input_value('_'.$header, RCUBE_INPUT_POST, TRUE);
324   }
325   else if (!empty($_SESSION['compose']['param'][$header])) {
326     $fvalue = $_SESSION['compose']['param'][$header];
327   }
328   else if ($compose_mode == RCUBE_COMPOSE_REPLY) {
329     // get recipent address(es) out of the message headers
330     if ($header == 'to') {
331       $mailfollowup = $MESSAGE->headers->others['mail-followup-to'];
332       $mailreplyto  = $MESSAGE->headers->others['mail-reply-to'];
333
334       if ($MESSAGE->compose['to'])
335         $fvalue = $MESSAGE->compose['to'];
336       else if ($MESSAGE->reply_all == 'list' && $mailfollowup)
337         $fvalue = $mailfollowup;
338       else if ($MESSAGE->reply_all == 'list'
339         && preg_match('/<mailto:([^>]+)>/i', $MESSAGE->headers->others['list-post'], $m))
340         $fvalue = $m[1];
341       else if ($mailreplyto)
342         $fvalue = $mailreplyto;
343       else if (!empty($MESSAGE->headers->replyto))
344         $fvalue = $MESSAGE->headers->replyto;
345       else if (!empty($MESSAGE->headers->from))
346         $fvalue = $MESSAGE->headers->from;
347     }
348     // add recipient of original message if reply to all
349     else if ($header == 'cc' && !empty($MESSAGE->reply_all) && $MESSAGE->reply_all != 'list') {
350       if ($v = $MESSAGE->headers->to)
351         $fvalue .= $v;
352       if ($v = $MESSAGE->headers->cc)
353         $fvalue .= (!empty($fvalue) ? ', ' : '') . $v;
354     }
355   }
356   else if (in_array($compose_mode, array(RCUBE_COMPOSE_DRAFT, RCUBE_COMPOSE_EDIT))) {
357     // get drafted headers
358     if ($header=='to' && !empty($MESSAGE->headers->to))
359       $fvalue = $MESSAGE->get_header('to');
360     else if ($header=='cc' && !empty($MESSAGE->headers->cc))
361       $fvalue = $MESSAGE->get_header('cc');
362     else if ($header=='bcc' && !empty($MESSAGE->headers->bcc))
363       $fvalue = $MESSAGE->get_header('bcc');
364     else if ($header=='replyto' && !empty($MESSAGE->headers->others['mail-reply-to']))
365       $fvalue = $MESSAGE->get_header('mail-reply-to');
366     else if ($header=='replyto' && !empty($MESSAGE->headers->replyto))
367       $fvalue = $MESSAGE->get_header('reply-to');
368     else if ($header=='followupto' && !empty($MESSAGE->headers->others['mail-followup-to']))
369       $fvalue = $MESSAGE->get_header('mail-followup-to');
370   }
371
372   // split recipients and put them back together in a unique way
373   if (!empty($fvalue) && in_array($header, array('to', 'cc', 'bcc'))) {
374     $to_addresses = $IMAP->decode_address_list($fvalue);
375     $fvalue = array();
376
377     foreach ($to_addresses as $addr_part) {
378       if (empty($addr_part['mailto']))
379         continue;
380
381       $mailto = mb_strtolower(rcube_idn_to_utf8($addr_part['mailto']));
382
383       if (!in_array($mailto, $a_recipients)
384         && (empty($MESSAGE->compose['from_email']) || $mailto != $MESSAGE->compose['from_email'])
385       ) {
386         if ($addr_part['name'] && $addr_part['mailto'] != $addr_part['name'])
387           $string = format_email_recipient($mailto, $addr_part['name']);
388         else
389           $string = $mailto;
390
391         $fvalue[] = $string;
392         $a_recipients[] = $addr_part['mailto'];
393       }
394     }
395     
396     $fvalue = implode(', ', $fvalue);
397   }
398
399   $MESSAGE->compose[$header] = $fvalue;
400 }
401 unset($a_recipients);
402
087c7d 403 // process $MESSAGE body/attachments, set $MESSAGE_BODY/$HTML_MODE vars and some session data
A 404 $MESSAGE_BODY = rcmail_prepare_message_body();
4e17e6 405
087c7d 406
A 407 /****** compose mode functions ********/
4e17e6 408
T 409 function rcmail_compose_headers($attrib)
4315b0 410 {
da142b 411   global $MESSAGE;
4e17e6 412
T 413   list($form_start, $form_end) = get_form_tags($attrib);
8958d0 414
da142b 415   $out  = '';
4e17e6 416   $part = strtolower($attrib['part']);
8958d0 417
4e17e6 418   switch ($part)
4315b0 419   {
4e17e6 420     case 'from':
8958d0 421       return $form_start . rcmail_compose_header_from($attrib);
4e17e6 422
T 423     case 'to':
424     case 'cc':
425     case 'bcc':
da142b 426       $fname = '_' . $part;
A 427       $header = $param = $part;
8958d0 428
bd4209 429       $allow_attrib = array('id', 'class', 'style', 'cols', 'rows', 'tabindex');
47124c 430       $field_type = 'html_textarea';
4e17e6 431       break;
T 432
433     case 'replyto':
434     case 'reply-to':
435       $fname = '_replyto';
759696 436       $param = 'replyto';
e25a35 437       $header = 'reply-to';
A 438
3ee5a7 439     case 'followupto':
A 440     case 'followup-to':
e25a35 441       if (!$fname) {
3ee5a7 442         $fname = '_followupto';
A 443         $param = 'followupto';
cb105a 444         $header = 'mail-followup-to';
e25a35 445       }
A 446
317219 447       $allow_attrib = array('id', 'class', 'style', 'size', 'tabindex');
47124c 448       $field_type = 'html_inputfield';
5f314d 449       break;
4315b0 450   }
e99991 451
4e17e6 452   if ($fname && $field_type)
4315b0 453   {
4e17e6 454     // pass the following attributes to the form class
491a6e 455     $field_attrib = array('name' => $fname, 'spellcheck' => 'false');
4e17e6 456     foreach ($attrib as $attr => $value)
T 457       if (in_array($attr, $allow_attrib))
458         $field_attrib[$attr] = $value;
459
460     // create teaxtarea object
461     $input = new $field_type($field_attrib);
da142b 462     $out = $input->show($MESSAGE->compose[$param]);
4315b0 463   }
4e17e6 464   
T 465   if ($form_start)
466     $out = $form_start.$out;
1966c5 467
e99991 468   return $out;
4315b0 469 }
4e17e6 470
T 471
1cded8 472 function rcmail_compose_header_from($attrib)
4315b0 473 {
da142b 474   global $MESSAGE, $OUTPUT;
A 475
1cded8 476   // pass the following attributes to the form class
T 477   $field_attrib = array('name' => '_from');
478   foreach ($attrib as $attr => $value)
479     if (in_array($attr, array('id', 'class', 'style', 'size', 'tabindex')))
480       $field_attrib[$attr] = $value;
4e17e6 481
da142b 482   if (count($MESSAGE->identities))
4315b0 483   {
1cded8 484     $a_signatures = array();
a0109c 485
f11541 486     $field_attrib['onchange'] = JS_OBJECT_NAME.".change_identity(this)";
47124c 487     $select_from = new html_select($field_attrib);
a0109c 488
e25a35 489     // create SELECT element
da142b 490     foreach ($MESSAGE->identities as $sql_arr)
4315b0 491     {
a0109c 492       $identity_id = $sql_arr['identity_id'];
da142b 493       $select_from->add(format_email_recipient($sql_arr['email'], $sql_arr['name']), $identity_id);
4e17e6 494
1cded8 495       // add signature to array
759696 496       if (!empty($sql_arr['signature']) && empty($_SESSION['compose']['param']['nosig']))
4315b0 497       {
a0109c 498         $a_signatures[$identity_id]['text'] = $sql_arr['signature'];
S 499         $a_signatures[$identity_id]['is_html'] = ($sql_arr['html_signature'] == 1) ? true : false;
dd792e 500         if ($a_signatures[$identity_id]['is_html'])
4315b0 501         {
dd792e 502             $h2t = new html2text($a_signatures[$identity_id]['text'], false, false);
300fc6 503             $a_signatures[$identity_id]['plain_text'] = trim($h2t->get_text());
a0109c 504         }
4315b0 505       }
S 506     }
e25a35 507
da142b 508     $out = $select_from->show($MESSAGE->compose['from']);
4e17e6 509
1cded8 510     // add signatures to client
f11541 511     $OUTPUT->set_env('signatures', $a_signatures);
4315b0 512   }
d2b884 513   // no identities, display text input field
A 514   else {
515     $field_attrib['class'] = 'from_address';
47124c 516     $input_from = new html_inputfield($field_attrib);
da142b 517     $out = $input_from->show($MESSAGE->compose['from']);
4315b0 518   }
4e17e6 519
1cded8 520   return $out;
4315b0 521 }
4e17e6 522
T 523
868deb 524 function rcmail_compose_editor_mode()
A 525 {
526   global $RCMAIL, $MESSAGE, $compose_mode;
527   static $useHtml;
528
529   if ($useHtml !== null)
530     return $useHtml;
531
532   $html_editor = intval($RCMAIL->config->get('htmleditor'));
533
534   if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT) {
535     $useHtml = $MESSAGE->has_html_part();
536   }
537   else if ($compose_mode == RCUBE_COMPOSE_REPLY) {
538     $useHtml = ($html_editor == 1 || ($html_editor == 2 && $MESSAGE->has_html_part()));
539   }
540   else { // RCUBE_COMPOSE_FORWARD or NEW
541     $useHtml = ($html_editor == 1);
542   }
543
544   return $useHtml;
545 }
546
547
087c7d 548 function rcmail_prepare_message_body()
4315b0 549 {
868deb 550   global $RCMAIL, $MESSAGE, $compose_mode, $LINE_LENGTH, $HTML_MODE;
a0109c 551
4e17e6 552   // use posted message body
868deb 553   if (!empty($_POST['_message'])) {
8fa58e 554     $body = get_input_value('_message', RCUBE_INPUT_POST, true);
868deb 555     $isHtml = (bool) get_input_value('_is_html', RCUBE_INPUT_POST);
8fa58e 556   }
868deb 557   else if ($_SESSION['compose']['param']['body']) {
759696 558     $body = $_SESSION['compose']['param']['body'];
T 559     $isHtml = false;
560   }
868deb 561   // reply/edit/draft/forward
A 562   else if ($compose_mode) {
b62049 563     $has_html_part = $MESSAGE->has_html_part();
868deb 564     $isHtml = rcmail_compose_editor_mode();
A 565
566     if ($isHtml) {
567       if ($has_html_part) {
568         $body = $MESSAGE->first_html_part();
569       }
570       else {
ba12c7 571         $body = $MESSAGE->first_text_part();
A 572         // try to remove the signature
573         if ($RCMAIL->config->get('strip_existing_sig', true))
574           $body = rcmail_remove_signature($body);
575         // add HTML formatting
576         $body = rcmail_plain_body($body);
868deb 577         if ($body)
A 578           $body = '<pre>' . $body . '</pre>';
579       }
b62049 580     }
868deb 581     else {
A 582       if ($has_html_part) {
583         // use html part if it has been used for message (pre)viewing
584         // decrease line length for quoting
585         $len = $compose_mode == RCUBE_COMPOSE_REPLY ? $LINE_LENGTH-2 : $LINE_LENGTH;
586         $txt = new html2text($MESSAGE->first_html_part(), false, true, $len);
587         $body = $txt->get_text();
588       }
589       else {
590         $body = $MESSAGE->first_text_part($part);
591         if ($body && $part && $part->ctype_secondary == 'plain'
592             && $part->ctype_parameters['format'] == 'flowed'
593         ) {
594           $body = rcube_message::unfold_flowed($body);
595         }
596       }
4e17e6 597     }
80815d 598
8fa58e 599     // compose reply-body
T 600     if ($compose_mode == RCUBE_COMPOSE_REPLY)
601       $body = rcmail_create_reply_body($body, $isHtml);
602     // forward message body inline
603     else if ($compose_mode == RCUBE_COMPOSE_FORWARD)
604       $body = rcmail_create_forward_body($body, $isHtml);
605     // load draft message body
069704 606     else if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT)
8fa58e 607       $body = rcmail_create_draft_body($body, $isHtml);
868deb 608   }
A 609   else { // new message
610     $isHtml = rcmail_compose_editor_mode();
8fa58e 611   }
a0109c 612
8abe54 613   $plugin = $RCMAIL->plugins->exec_hook('message_compose_body',
A 614     array('body' => $body, 'html' => $isHtml, 'mode' => $compose_mode));
b575fa 615   $body = $plugin['body'];
A 616   unset($plugin);
8abe54 617
b575fa 618   // add blocked.gif attachment (#1486516)
A 619   if ($isHtml && preg_match('#<img src="\./program/blocked\.gif"#', $body)) {
620     if ($attachment = rcmail_save_image('program/blocked.gif', 'image/gif')) {
621       $_SESSION['compose']['attachments'][$attachment['id']] = $attachment;
622       $body = preg_replace('#\./program/blocked\.gif#',
623         $RCMAIL->comm_path.'&_action=display-attachment&_file=rcmfile'.$attachment['id'],
624         $body);
625     }
626   }
627   
087c7d 628   $HTML_MODE = $isHtml;
A 629   
630   return $body;
631 }
632
633 function rcmail_compose_body($attrib)
634 {
635   global $RCMAIL, $CONFIG, $OUTPUT, $MESSAGE, $compose_mode, $LINE_LENGTH, $HTML_MODE, $MESSAGE_BODY;
636   
637   list($form_start, $form_end) = get_form_tags($attrib);
638   unset($attrib['form']);
639   
640   if (empty($attrib['id']))
641     $attrib['id'] = 'rcmComposeBody';
642
643   $attrib['name'] = '_message';
644
645   $isHtml = $HTML_MODE;
646   
4e17e6 647   $out = $form_start ? "$form_start\n" : '';
1966c5 648
8fa58e 649   $saveid = new html_hiddenfield(array('name' => '_draft_saveid', 'value' => $compose_mode==RCUBE_COMPOSE_DRAFT ? str_replace(array('<','>'), "", $MESSAGE->headers->messageID) : ''));
f0f98f 650   $out .= $saveid->show();
1966c5 651
47124c 652   $drafttoggle = new html_hiddenfield(array('name' => '_draft', 'value' => 'yes'));
1966c5 653   $out .= $drafttoggle->show();
S 654
47124c 655   $msgtype = new html_hiddenfield(array('name' => '_is_html', 'value' => ($isHtml?"1":"0")));
a0109c 656   $out .= $msgtype->show();
S 657
2f746d 658   // If desired, set this textarea to be editable by TinyMCE
6084d7 659   if ($isHtml) {
A 660     $attrib['class'] = 'mce_editor';
661     $textarea = new html_textarea($attrib);
662     $out .= $textarea->show($MESSAGE_BODY);
663   }
664   else {
665     $textarea = new html_textarea($attrib);
666     $out .= $textarea->show('');
667     // quote plain text, inject into textarea
668     $table = get_html_translation_table(HTML_SPECIALCHARS);
669     $MESSAGE_BODY = strtr($MESSAGE_BODY, $table);
670     $out = substr($out, 0, -11) . $MESSAGE_BODY . '</textarea>';
671   }
672
4e17e6 673   $out .= $form_end ? "\n$form_end" : '';
a01b3b 674
A 675   $OUTPUT->set_env('composebody', $attrib['id']);
a0109c 676
3e20c4 677   // include HTML editor
A 678   rcube_html_editor();
679   
dd53e2 680   // include GoogieSpell
4ca10b 681   if (!empty($CONFIG['enable_spellcheck'])) {
3e20c4 682
c287f3 683     $engine = $RCMAIL->config->get('spellcheck_engine','googie');
A 684     $spellcheck_langs = (array) $RCMAIL->config->get('spellcheck_languages',
685       array('da'=>'Dansk', 'de'=>'Deutsch', 'en' => 'English', 'es'=>'Español',
686             'fr'=>'Français', 'it'=>'Italiano', 'nl'=>'Nederlands', 'pl'=>'Polski',
687             'pt'=>'Português', 'fi'=>'Suomi', 'sv'=>'Svenska'));
688
689     // googie works only with two-letter codes
690     if ($engine == 'googie') {
691       $lang = strtolower(substr($_SESSION['language'], 0, 2));
692
693       $spellcheck_langs_googie = array();
694       foreach ($spellcheck_langs as $key => $name)
695         $spellcheck_langs_googie[strtolower(substr($key,0,2))] = $name;
696         $spellcheck_langs = $spellcheck_langs_googie;
697     }
698     else {
699       $lang = $_SESSION['language'];
700
701       // if not found in the list, try with two-letter code
702       if (!$spellcheck_langs[$lang])
703         $lang = strtolower(substr($lang, 0, 2));
704     }
705
326f3d 706     if (!$spellcheck_langs[$lang])
T 707       $lang = 'en';
c287f3 708
326f3d 709     $editor_lang_set = array();
T 710     foreach ($spellcheck_langs as $key => $name) {
711       $editor_lang_set[] = ($key == $lang ? '+' : '') . JQ($name).'='.JQ($key);
c287f3 712     }
996066 713     
ed5d29 714     $OUTPUT->include_script('googiespell.js');
2bca6e 715     $OUTPUT->add_script(sprintf(
677e1f 716       "var googie = new GoogieSpell('\$__skin_path/images/googiespell/','?_task=utils&_action=spell&lang=');\n".
2bca6e 717       "googie.lang_chck_spell = \"%s\";\n".
T 718       "googie.lang_rsm_edt = \"%s\";\n".
719       "googie.lang_close = \"%s\";\n".
720       "googie.lang_revert = \"%s\";\n".
326f3d 721       "googie.lang_no_error_found = \"%s\";\n".
T 722       "googie.setLanguages(%s);\n".
2bca6e 723       "googie.setCurrentLanguage('%s');\n".
309d2f 724       "googie.setSpellContainer('spellcheck-control');\n".
2bca6e 725       "googie.decorateTextarea('%s');\n".
T 726       "%s.set_env('spellcheck', googie);",
727       JQ(Q(rcube_label('checkspelling'))),
728       JQ(Q(rcube_label('resumeediting'))),
729       JQ(Q(rcube_label('close'))),
730       JQ(Q(rcube_label('revertto'))),
731       JQ(Q(rcube_label('nospellerrors'))),
2717f9 732       json_serialize($spellcheck_langs),
326f3d 733       $lang,
2bca6e 734       $attrib['id'],
f11541 735       JS_OBJECT_NAME), 'foot');
ed5d29 736
112c91 737     $OUTPUT->add_label('checking');
326f3d 738     $OUTPUT->set_env('spellcheck_langs', join(',', $editor_lang_set));
4ca10b 739   }
f0f98f 740  
027af3 741   $out .= "\n".'<iframe name="savetarget" src="program/blank.gif" style="width:0;height:0;border:none;visibility:hidden;"></iframe>';
41fa0b 742
4e17e6 743   return $out;
4315b0 744 }
4e17e6 745
T 746
a0109c 747 function rcmail_create_reply_body($body, $bodyIsHtml)
4315b0 748 {
b62049 749   global $RCMAIL, $MESSAGE, $LINE_LENGTH;
4e17e6 750
9f9664 751   // build reply prefix
8c57f5 752   $from = array_pop($RCMAIL->imap->decode_address_list($MESSAGE->get_header('from'), 1, false));
9f9664 753   $prefix = sprintf("On %s, %s wrote:",
e8d5bd 754     $MESSAGE->headers->date, $from['name'] ? $from['name'] : rcube_idn_to_utf8($from['mailto']));
9f9664 755
0207c4 756   if (!$bodyIsHtml) {
33dfdd 757     $body = preg_replace('/\r?\n/', "\n", $body);
9f9664 758
2b180b 759     // try to remove the signature
ba12c7 760     if ($RCMAIL->config->get('strip_existing_sig', true))
A 761       $body = rcmail_remove_signature($body);
2b180b 762
6b6f2e 763     // soft-wrap and quote message text
33dfdd 764     $body = rcmail_wrap_and_quote(rtrim($body, "\n"), $LINE_LENGTH);
4e17e6 765
9f9664 766     $prefix .= "\n";
a0109c 767     $suffix = '';
9f9664 768
0207c4 769     if ($RCMAIL->config->get('top_posting'))
50f56d 770       $prefix = "\n\n\n" . $prefix;
a0109c 771   }
0207c4 772   else {
ec603f 773     // save inline images to files
A 774     $cid_map = rcmail_write_inline_attachments($MESSAGE);
775     // set is_safe flag (we need this for html body washing)
776     rcmail_check_safe($MESSAGE);
777     // clean up html tags
778     $body = rcmail_wash_html($body, array('safe' => $MESSAGE->is_safe), $cid_map);
779
780     // build reply (quote content)
9f9664 781     $prefix = '<p>' . Q($prefix) . "</p>\n";
7a48e5 782     $prefix .= '<blockquote>';
ce4673 783
0207c4 784     if ($RCMAIL->config->get('top_posting')) {
ce4673 785       $prefix = '<br>' . $prefix;
A 786       $suffix = '</blockquote>';
50f56d 787     }
A 788     else {
ce4673 789       $suffix = '</blockquote><p></p>';
50f56d 790     }
a0109c 791   }
S 792
793   return $prefix.$body.$suffix;
4315b0 794 }
4e17e6 795
T 796
a0109c 797 function rcmail_create_forward_body($body, $bodyIsHtml)
4315b0 798 {
8c0b9e 799   global $IMAP, $MESSAGE, $OUTPUT;
ec603f 800
A 801   // add attachments
802   if (!isset($_SESSION['compose']['forward_attachments']) && is_array($MESSAGE->mime_parts))
803     $cid_map = rcmail_write_compose_attachments($MESSAGE, $bodyIsHtml);
4e17e6 804
8fa58e 805   if (!$bodyIsHtml)
a0109c 806   {
5758b9 807     $prefix = "\n\n\n-------- Original Message --------\n";
A 808     $prefix .= 'Subject: ' . $MESSAGE->subject . "\n";
809     $prefix .= 'Date: ' . $MESSAGE->headers->date . "\n";
810     $prefix .= 'From: ' . $MESSAGE->get_header('from') . "\n";
811     $prefix .= 'To: ' . $MESSAGE->get_header('to') . "\n";
a4cf45 812
A 813     if ($MESSAGE->headers->cc)
814       $prefix .= 'Cc: ' . $MESSAGE->get_header('cc') . "\n";
5758b9 815     if ($MESSAGE->headers->replyto && $MESSAGE->headers->replyto != $MESSAGE->headers->from)
A 816       $prefix .= 'Reply-To: ' . $MESSAGE->get_header('replyto') . "\n";
a4cf45 817
5758b9 818     $prefix .= "\n";
a0109c 819   }
S 820   else
821   {
ec603f 822     // set is_safe flag (we need this for html body washing)
A 823     rcmail_check_safe($MESSAGE);
824     // clean up html tags
825     $body = rcmail_wash_html($body, array('safe' => $MESSAGE->is_safe), $cid_map);
826
4315b0 827     $prefix = sprintf(
ce4673 828       "<br /><p>-------- Original Message --------</p>" .
a0109c 829         "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody>" .
S 830         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Subject: </th><td>%s</td></tr>" .
831         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Date: </th><td>%s</td></tr>" .
832         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">From: </th><td>%s</td></tr>" .
5758b9 833         "<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">To: </th><td>%s</td></tr>",
8fa58e 834       Q($MESSAGE->subject),
T 835       Q($MESSAGE->headers->date),
3e8d89 836       htmlspecialchars(Q($MESSAGE->get_header('from'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset()),
7d8e16 837       htmlspecialchars(Q($MESSAGE->get_header('to'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset()));
5758b9 838
a4cf45 839     if ($MESSAGE->headers->cc)
A 840       $prefix .= sprintf("<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Cc: </th><td>%s</td></tr>",
841         htmlspecialchars(Q($MESSAGE->get_header('cc'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset()));
842
5758b9 843     if ($MESSAGE->headers->replyto && $MESSAGE->headers->replyto != $MESSAGE->headers->from)
A 844       $prefix .= sprintf("<tr><th align=\"right\" nowrap=\"nowrap\" valign=\"baseline\">Reply-To: </th><td>%s</td></tr>",
7d8e16 845         htmlspecialchars(Q($MESSAGE->get_header('replyto'), 'replace'), ENT_COMPAT, $OUTPUT->get_charset()));
5758b9 846
A 847     $prefix .= "</tbody></table><br>";
a0109c 848   }
5cc4b1 849     
4e17e6 850   return $prefix.$body;
4315b0 851 }
4e17e6 852
8d4bcd 853
a0109c 854 function rcmail_create_draft_body($body, $bodyIsHtml)
4315b0 855 {
ec603f 856   global $MESSAGE, $OUTPUT;
8ecb0e 857   
T 858   /**
859    * add attachments
8fa58e 860    * sizeof($MESSAGE->mime_parts can be 1 - e.g. attachment, but no text!
8ecb0e 861    */
7d8e16 862   if (empty($_SESSION['compose']['forward_attachments'])
8fa58e 863       && is_array($MESSAGE->mime_parts)
T 864       && count($MESSAGE->mime_parts) > 0)
ec603f 865   {
A 866     $cid_map = rcmail_write_compose_attachments($MESSAGE, $bodyIsHtml);
1966c5 867
ec603f 868     // replace cid with href in inline images links
A 869     if ($cid_map)
870       $body = str_replace(array_keys($cid_map), array_values($cid_map), $body);
871   }
872   
1966c5 873   return $body;
4315b0 874 }
ba12c7 875
A 876
877 function rcmail_remove_signature($body)
878 {
879   global $RCMAIL;
880
881   $len = strlen($body);
882   $sig_max_lines = $RCMAIL->config->get('sig_max_lines', 15);
883
884   while (($sp = strrpos($body, "-- \n", $sp ? -$len+$sp-1 : 0)) !== false) {
885     if ($sp == 0 || $body[$sp-1] == "\n") {
886       // do not touch blocks with more that X lines
887       if (substr_count($body, "\n", $sp) < $sig_max_lines) {
888         $body = substr($body, 0, max(0, $sp-1));
889       }
890       break;
891     }
892   }
893
894   return $body;
895 }
896
897
1bc48e 898 function rcmail_write_compose_attachments(&$message, $bodyIsHtml)
4315b0 899 {
bde421 900   global $RCMAIL;
5503cc 901
021ef4 902   $cid_map = $messages = array();
8fa58e 903   foreach ((array)$message->mime_parts as $pid => $part)
4315b0 904   {
7d8e16 905     if (($part->ctype_primary != 'message' || !$bodyIsHtml) && $part->ctype_primary != 'multipart' && 
d311d8 906         ($part->disposition == 'attachment' || ($part->disposition == 'inline' && $bodyIsHtml) || $part->filename)
A 907         && $part->mimetype != 'application/ms-tnef'
908     ) {
021ef4 909       $skip = false;
A 910       if ($part->mimetype == 'message/rfc822') {
911         $messages[] = $part->mime_id;
912       } else if ($messages) {
913         // skip attachments included in message/rfc822 attachment (#1486487)
914         foreach ($messages as $mimeid)
915           if (strpos($part->mime_id, $mimeid.'.') === 0) {
916             $skip = true;
917             break;
918           }
919       }
920
921       if (!$skip && ($attachment = rcmail_save_attachment($message, $pid))) {
cc97ea 922         $_SESSION['compose']['attachments'][$attachment['id']] = $attachment;
8f2b46 923         if ($bodyIsHtml && ($part->content_id || $part->content_location)) {
bde421 924           $url = $RCMAIL->comm_path.'&_action=display-attachment&_file=rcmfile'.$attachment['id'];
8f2b46 925           if ($part->content_id)
A 926             $cid_map['cid:'.$part->content_id] = $url;
927           else
928             $cid_map[$part->content_location] = $url;
ec603f 929         }
A 930       }
8d4bcd 931     }
4315b0 932   }
cc97ea 933
8fa58e 934   $_SESSION['compose']['forward_attachments'] = true;
ec603f 935
A 936   return $cid_map;
4315b0 937 }
4e17e6 938
T 939
2f746d 940 function rcmail_write_inline_attachments(&$message)
A 941 {
bde421 942   global $RCMAIL;
ec603f 943
A 944   $cid_map = array();
945   foreach ((array)$message->mime_parts as $pid => $part) {
8f2b46 946     if (($part->content_id || $part->content_location) && $part->filename) {
ec603f 947       if ($attachment = rcmail_save_attachment($message, $pid)) {
cc97ea 948         $_SESSION['compose']['attachments'][$attachment['id']] = $attachment;
bde421 949         $url = $RCMAIL->comm_path.'&_action=display-attachment&_file=rcmfile'.$attachment['id'];
8f2b46 950         if ($part->content_id)
A 951           $cid_map['cid:'.$part->content_id] = $url;
952         else
953           $cid_map[$part->content_location] = $url;
ec603f 954       }
2f746d 955     }
A 956   }
8f2b46 957
ec603f 958   return $cid_map;
2f746d 959 }
A 960
961 function rcmail_save_attachment(&$message, $pid)
962 {
963   $part = $message->mime_parts[$pid];
a64064 964   $mem_limit = parse_bytes(ini_get('memory_limit'));
A 965   $curr_mem = function_exists('memory_get_usage') ? memory_get_usage() : 16*1024*1024; // safe value: 16MB
966   $data = $path = null;
967
968   // don't load too big attachments into memory
969   if ($mem_limit > 0 && $part->size > $mem_limit - $curr_mem) {
970     $rcmail = rcmail::get_instance();
971     $temp_dir = unslashify($rcmail->config->get('temp_dir'));
972     $path = tempnam($temp_dir, 'rcmAttmnt');
973     if ($fp = fopen($path, 'w')) {
974       $message->get_part_content($pid, $fp);
975       fclose($fp);
976     } else
977       return false;
978   } else {
979     $data = $message->get_part_content($pid);
980   }
981
cc97ea 982   $attachment = array(
7d8e16 983     'name' => $part->filename ? $part->filename : 'Part_'.$pid.'.'.$part->ctype_secondary,
cc97ea 984     'mimetype' => $part->ctype_primary . '/' . $part->ctype_secondary,
T 985     'content_id' => $part->content_id,
a64064 986     'data' => $data,
3b1426 987     'path' => $path,
A 988     'size' => $path ? filesize($path) : strlen($data),
cc97ea 989   );
3b1426 990
e6ce00 991   $attachment = rcmail::get_instance()->plugins->exec_hook('attachment_save', $attachment);
a64064 992
cc97ea 993   if ($attachment['status']) {
76791c 994     unset($attachment['data'], $attachment['status'], $attachment['content_id'], $attachment['abort']);
cc97ea 995     return $attachment;
a64064 996   } else if ($path) {
A 997     @unlink($path);
2f746d 998   }
a64064 999   
cc97ea 1000   return false;
2f746d 1001 }
A 1002
b575fa 1003 function rcmail_save_image($path, $mimetype='')
A 1004 {
1005   // handle attachments in memory
1006   $data = file_get_contents($path);
1007
1008   $attachment = array(
1009     'name' => rcmail_basename($path),
1010     'mimetype' => $mimetype ? $mimetype : rc_mime_content_type($path, $name),
1011     'data' => $data,
1012     'size' => strlen($data),
1013   );
1014
e6ce00 1015   $attachment = rcmail::get_instance()->plugins->exec_hook('attachment_save', $attachment);
b575fa 1016
A 1017   if ($attachment['status']) {
1018     unset($attachment['data'], $attachment['status'], $attachment['content_id'], $attachment['abort']);
1019     return $attachment;
1020   }
1021   
1022   return false;
1023 }
1024
1025 function rcmail_basename($filename)
1026 {
1027   // basename() is not unicode safe and locale dependent
1028   if (stristr(PHP_OS, 'win') || stristr(PHP_OS, 'netware')) {
1029     return preg_replace('/^.*[\\\\\\/]/', '', $filename);
1030   } else {
1031     return preg_replace('/^.*[\/]/', '', $filename);
1032   }
1033 }
2f746d 1034
4e17e6 1035 function rcmail_compose_subject($attrib)
4315b0 1036 {
8fa58e 1037   global $MESSAGE, $compose_mode;
4e17e6 1038   
T 1039   list($form_start, $form_end) = get_form_tags($attrib);
1040   unset($attrib['form']);
1041   
1042   $attrib['name'] = '_subject';
491a6e 1043   $attrib['spellcheck'] = 'true';
47124c 1044   $textfield = new html_inputfield($attrib);
4e17e6 1045
T 1046   $subject = '';
1047
1048   // use subject from post
5f314d 1049   if (isset($_POST['_subject'])) {
01c86f 1050     $subject = get_input_value('_subject', RCUBE_INPUT_POST, TRUE);
5f314d 1051   }
4e17e6 1052   // create a reply-subject
5f314d 1053   else if ($compose_mode == RCUBE_COMPOSE_REPLY) {
23a2ee 1054     if (preg_match('/^re:/i', $MESSAGE->subject))
8fa58e 1055       $subject = $MESSAGE->subject;
520c36 1056     else
8fa58e 1057       $subject = 'Re: '.$MESSAGE->subject;
4315b0 1058   }
4e17e6 1059   // create a forward-subject
5f314d 1060   else if ($compose_mode == RCUBE_COMPOSE_FORWARD) {
23a2ee 1061     if (preg_match('/^fwd:/i', $MESSAGE->subject))
8fa58e 1062       $subject = $MESSAGE->subject;
09941e 1063     else
8fa58e 1064       $subject = 'Fwd: '.$MESSAGE->subject;
4315b0 1065   }
1966c5 1066   // creeate a draft-subject
069704 1067   else if ($compose_mode == RCUBE_COMPOSE_DRAFT || $compose_mode == RCUBE_COMPOSE_EDIT) {
8fa58e 1068     $subject = $MESSAGE->subject;
5f314d 1069   }
759696 1070   else if (!empty($_SESSION['compose']['param']['subject'])) {
T 1071     $subject = $_SESSION['compose']['param']['subject'];
5f314d 1072   }
4e17e6 1073   
T 1074   $out = $form_start ? "$form_start\n" : '';
1075   $out .= $textfield->show($subject);
1076   $out .= $form_end ? "\n$form_end" : '';
e99991 1077
4e17e6 1078   return $out;
4315b0 1079 }
4e17e6 1080
T 1081
1082 function rcmail_compose_attachment_list($attrib)
4315b0 1083 {
f11541 1084   global $OUTPUT, $CONFIG;
4e17e6 1085   
T 1086   // add ID if not given
1087   if (!$attrib['id'])
1088     $attrib['id'] = 'rcmAttachmentList';
1089   
8fa58e 1090   $out = "\n";
01ffe0 1091   $jslist = array();
087c7d 1092
4e17e6 1093   if (is_array($_SESSION['compose']['attachments']))
4315b0 1094   {
991a25 1095     if ($attrib['deleteicon']) {
8fa58e 1096       $button = html::img(array(
T 1097         'src' => $CONFIG['skin_path'] . $attrib['deleteicon'],
1fb587 1098         'alt' => rcube_label('delete')
991a25 1099       ));
T 1100     }
a894ba 1101     else
8fa58e 1102       $button = Q(rcube_label('delete'));
a894ba 1103
aade7b 1104     foreach ($_SESSION['compose']['attachments'] as $id => $a_prop)
6d5dba 1105     {
T 1106       if (empty($a_prop))
1107         continue;
1108       
01ffe0 1109       $out .= html::tag('li', array('id' => 'rcmfile'.$id),
8fa58e 1110         html::a(array(
T 1111             'href' => "#delete",
1112             'title' => rcube_label('delete'),
cc97ea 1113             'onclick' => sprintf("return %s.command('remove-attachment','rcmfile%s', this)", JS_OBJECT_NAME, $id)),
8fa58e 1114           $button) . Q($a_prop['name']));
01ffe0 1115         
T 1116         $jslist['rcmfile'.$id] = array('name' => $a_prop['name'], 'complete' => true, 'mimetype' => $a_prop['mimetype']);
6d5dba 1117     }
4315b0 1118   }
4e17e6 1119
21d682 1120   if ($attrib['deleteicon'])
A 1121     $_SESSION['compose']['deleteicon'] = $CONFIG['skin_path'] . $attrib['deleteicon'];
3f9712 1122   if ($attrib['cancelicon'])
V 1123     $OUTPUT->set_env('cancelicon', $CONFIG['skin_path'] . $attrib['cancelicon']);
ebf872 1124   if ($attrib['loadingicon'])
A 1125     $OUTPUT->set_env('loadingicon', $CONFIG['skin_path'] . $attrib['loadingicon']);
21d682 1126
01ffe0 1127   $OUTPUT->set_env('attachments', $jslist);
f11541 1128   $OUTPUT->add_gui_object('attachmentlist', $attrib['id']);
4e17e6 1129     
8fa58e 1130   return html::tag('ul', $attrib, $out, html::$common_attrib);
4315b0 1131 }
4e17e6 1132
T 1133
1134 function rcmail_compose_attachment_form($attrib)
4315b0 1135 {
197601 1136   global $OUTPUT;
4e17e6 1137
T 1138   // add ID if not given
1139   if (!$attrib['id'])
1140     $attrib['id'] = 'rcmUploadbox';
7df0e3 1141
A 1142   // find max filesize value
1143   $max_filesize = parse_bytes(ini_get('upload_max_filesize'));
1144   $max_postsize = parse_bytes(ini_get('post_max_size'));
1145   if ($max_postsize && $max_postsize < $max_filesize)
1146     $max_filesize = $max_postsize;
1147   $max_filesize = show_bytes($max_filesize);
4e17e6 1148   
2b77e8 1149   $button = new html_inputfield(array('type' => 'button'));
4e17e6 1150   
197601 1151   $out = html::div($attrib,
d37e1e 1152     $OUTPUT->form_tag(array('name' => 'uploadform', 'method' => 'post', 'enctype' => 'multipart/form-data'),
0501b6 1153       html::div(null, rcmail_compose_attachment_field(array('size' => $attrib['attachmentfieldsize']))) .
7df0e3 1154       html::div('hint', rcube_label(array('name' => 'maxuploadsize', 'vars' => array('size' => $max_filesize)))) .
c17dc6 1155       html::div('buttons',
71f60c 1156         $button->show(rcube_label('close'), array('class' => 'button', 'onclick' => "$('#$attrib[id]').hide()")) . ' ' .
2b77e8 1157         $button->show(rcube_label('upload'), array('class' => 'button mainaction', 'onclick' => JS_OBJECT_NAME . ".command('send-attachment', this.form)"))
c17dc6 1158       )
A 1159     )
197601 1160   );
4e17e6 1161   
f11541 1162   $OUTPUT->add_gui_object('uploadbox', $attrib['id']);
4e17e6 1163   return $out;
4315b0 1164 }
4e17e6 1165
T 1166
1167 function rcmail_compose_attachment_field($attrib)
4315b0 1168 {
e2c610 1169   $attrib['type'] = 'file';
A 1170   $attrib['name'] = '_attachments[]';
1171   $field = new html_inputfield($attrib);
1172   return $field->show();
4315b0 1173 }
4e17e6 1174
66e2bf 1175
4e17e6 1176 function rcmail_priority_selector($attrib)
4315b0 1177 {
ff08ee 1178   global $MESSAGE;
T 1179   
4e17e6 1180   list($form_start, $form_end) = get_form_tags($attrib);
T 1181   unset($attrib['form']);
8958d0 1182
4e17e6 1183   $attrib['name'] = '_priority';
47124c 1184   $selector = new html_select($attrib);
4e17e6 1185
T 1186   $selector->add(array(rcube_label('lowest'),
1187                        rcube_label('low'),
1188                        rcube_label('normal'),
1189                        rcube_label('high'),
1190                        rcube_label('highest')),
7902df 1191                  array(5, 4, 0, 2, 1));
8958d0 1192
79eb4e 1193   if (isset($_POST['_priority']))
A 1194     $sel = $_POST['_priority'];
1195   else if (intval($MESSAGE->headers->priority) != 3)
1196     $sel = intval($MESSAGE->headers->priority);
1197   else
1198     $sel = 0;
4e17e6 1199
T 1200   $out = $form_start ? "$form_start\n" : '';
1201   $out .= $selector->show($sel);
1202   $out .= $form_end ? "\n$form_end" : '';
8958d0 1203
4e17e6 1204   return $out;
4315b0 1205 }
4e17e6 1206
T 1207
620439 1208 function rcmail_receipt_checkbox($attrib)
4315b0 1209 {
b3660b 1210   global $RCMAIL, $MESSAGE, $compose_mode;
8958d0 1211
620439 1212   list($form_start, $form_end) = get_form_tags($attrib);
T 1213   unset($attrib['form']);
8958d0 1214
66e2bf 1215   if (!isset($attrib['id']))
T 1216     $attrib['id'] = 'receipt';  
620439 1217
T 1218   $attrib['name'] = '_receipt';
66e2bf 1219   $attrib['value'] = '1';
47124c 1220   $checkbox = new html_checkbox($attrib);
620439 1221
b3660b 1222   if ($MESSAGE && in_array($compose_mode, array(RCUBE_COMPOSE_DRAFT, RCUBE_COMPOSE_EDIT)))
A 1223     $mdn_default = (bool) $MESSAGE->headers->mdn_to;
1224   else
1225     $mdn_default = $RCMAIL->config->get('mdn_default');
1226
620439 1227   $out = $form_start ? "$form_start\n" : '';
b3660b 1228   $out .= $checkbox->show($mdn_default);
620439 1229   $out .= $form_end ? "\n$form_end" : '';
T 1230
1231   return $out;
4315b0 1232 }
620439 1233
T 1234
f22ea7 1235 function rcmail_dsn_checkbox($attrib)
A 1236 {
1237   global $RCMAIL;
1238
1239   list($form_start, $form_end) = get_form_tags($attrib);
1240   unset($attrib['form']);
1241
1242   if (!isset($attrib['id']))
1243     $attrib['id'] = 'dsn';
1244
1245   $attrib['name'] = '_dsn';
1246   $attrib['value'] = '1';
1247   $checkbox = new html_checkbox($attrib);
1248
1249   $out = $form_start ? "$form_start\n" : '';
1250   $out .= $checkbox->show($RCMAIL->config->get('dsn_default'));
1251   $out .= $form_end ? "\n$form_end" : '';
1252
1253   return $out;
1254 }
1255
1256
a0109c 1257 function rcmail_editor_selector($attrib)
S 1258 {
1259   global $CONFIG, $MESSAGE, $compose_mode;
1260
8fa58e 1261   // determine whether HTML or plain text should be checked
868deb 1262   $useHtml = rcmail_compose_editor_mode();
d9344f 1263
309d2f 1264   if (empty($attrib['editorid']))
a01b3b 1265     $attrib['editorid'] = 'rcmComposeBody';
79af0b 1266
309d2f 1267   if (empty($attrib['name']))
A 1268     $attrib['name'] = 'editorSelect';
8958d0 1269
5821ff 1270   $attrib['onchange'] = "return rcmail_toggle_editor(this, '".$attrib['editorid']."', '_is_html')";
309d2f 1271
A 1272   $select = new html_select($attrib);
1273
1274   $select->add(Q(rcube_label('htmltoggle')), 'html');
1275   $select->add(Q(rcube_label('plaintoggle')), 'plain');
1276
1277   return $select->show($useHtml ? 'html' : 'plain');
79af0b 1278
868deb 1279   foreach ($choices as $value => $text) {
6b1fc0 1280     $attrib['id'] = '_' . $value;
d9344f 1281     $attrib['value'] = $value;
8fa58e 1282     $selector .= $radio->show($chosenvalue, $attrib) . html::label($attrib['id'], Q(rcube_label($text)));
4315b0 1283   }
a0109c 1284
S 1285   return $selector;
1286 }
1287
1288
faf876 1289 function rcmail_store_target_selection($attrib)
T 1290 {
1291   $attrib['name'] = '_store_target';
a81be1 1292   $select = rcmail_mailbox_select(array_merge($attrib, array('noselection' => '- '.rcube_label('dontsave').' -')));
814905 1293   return $select->show($_SESSION['compose']['param']['sent_mbox'], $attrib);
faf876 1294 }
T 1295
1296
eeb85f 1297 function rcmail_check_sent_folder($folder, $create=false)
A 1298 {
1299   global $IMAP;
1300
1301   if ($IMAP->mailbox_exists($folder, true)) {
1302     return true;
1303   }
1304
1305   // folder may exist but isn't subscribed (#1485241)
1306   if ($create) {
1307     if (!$IMAP->mailbox_exists($folder))
1308       return $IMAP->create_mailbox($folder, true);
1309     else
1310       return $IMAP->subscribe($folder);
1311   }
1312
1313   return false;
1314 }
1315
1316
4e17e6 1317 function get_form_tags($attrib)
4315b0 1318 {
197601 1319   global $RCMAIL, $MESSAGE_FORM;
4e17e6 1320
T 1321   $form_start = '';
8958d0 1322   if (!$MESSAGE_FORM)
4315b0 1323   {
197601 1324     $hiddenfields = new html_hiddenfield(array('name' => '_task', 'value' => $RCMAIL->task));
4e17e6 1325     $hiddenfields->add(array('name' => '_action', 'value' => 'send'));
1966c5 1326
197601 1327     $form_start = empty($attrib['form']) ? $RCMAIL->output->form_tag(array('name' => "form", 'method' => "post")) : '';
4e17e6 1328     $form_start .= $hiddenfields->show();
4315b0 1329   }
eeb85f 1330
8958d0 1331   $form_end = ($MESSAGE_FORM && !strlen($attrib['form'])) ? '</form>' : '';
597170 1332   $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
eeb85f 1333
8958d0 1334   if (!$MESSAGE_FORM)
197601 1335     $RCMAIL->output->add_gui_object('messageform', $form_name);
eeb85f 1336
4e17e6 1337   $MESSAGE_FORM = $form_name;
T 1338
47124c 1339   return array($form_start, $form_end);
4315b0 1340 }
4e17e6 1341
T 1342
f11541 1343 // register UI objects
T 1344 $OUTPUT->add_handlers(array(
1345   'composeheaders' => 'rcmail_compose_headers',
1346   'composesubject' => 'rcmail_compose_subject',
1347   'composebody' => 'rcmail_compose_body',
1348   'composeattachmentlist' => 'rcmail_compose_attachment_list',
1349   'composeattachmentform' => 'rcmail_compose_attachment_form',
1350   'composeattachment' => 'rcmail_compose_attachment_field',
1351   'priorityselector' => 'rcmail_priority_selector',
1352   'editorselector' => 'rcmail_editor_selector',
1353   'receiptcheckbox' => 'rcmail_receipt_checkbox',
f22ea7 1354   'dsncheckbox' => 'rcmail_dsn_checkbox',
faf876 1355   'storetarget' => 'rcmail_store_target_selection',
f11541 1356 ));
a0530a 1357
47124c 1358 $OUTPUT->send('compose');
a0530a 1359
b25dfd 1360