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