Remove newline from end of file
[squirrelmail.git] / src / compose.php
CommitLineData
59177427 1<?php
895905c0 2
35586184 3/**
4 * compose.php
5 *
82d304a0 6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This code sends a mail.
10 *
11 * There are 4 modes of operation:
12 * - Start new mail
13 * - Add an attachment
14 * - Send mail
15 * - Save As Draft
16 *
30967a1e 17 * @version $Id$
8f6f9ba5 18 * @package squirrelmail
35586184 19 */
f7fb20fe 20
30967a1e 21/**
22 * Path for SquirrelMail required files.
23 * @ignore
24 */
86725763 25define('SM_PATH','../');
26
27/* SquirrelMail required files. */
08185f2a 28require_once(SM_PATH . 'include/validate.php');
953fa718 29require_once(SM_PATH . 'functions/global.php');
86725763 30require_once(SM_PATH . 'functions/imap.php');
31require_once(SM_PATH . 'functions/date.php');
32require_once(SM_PATH . 'functions/mime.php');
86725763 33require_once(SM_PATH . 'functions/plugin.php');
34require_once(SM_PATH . 'functions/display_messages.php');
35require_once(SM_PATH . 'class/deliver/Deliver.class.php');
24192f77 36require_once(SM_PATH . 'functions/addressbook.php');
1e2a6ff6 37require_once(SM_PATH . 'functions/identity.php');
df96b37a 38require_once(SM_PATH . 'functions/forms.php');
91f2085b 39
0b97a708 40/* --------------------- Get globals ------------------------------------- */
953fa718 41/** COOKIE VARS */
42sqgetGlobalVar('key', $key, SQ_COOKIE);
0b97a708 43
953fa718 44/** SESSION VARS */
45sqgetGlobalVar('username', $username, SQ_SESSION);
46sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
47sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
48sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
49
50sqgetGlobalVar('composesession', $composesession, SQ_SESSION);
51sqgetGlobalVar('compose_messages', $compose_messages, SQ_SESSION);
52
53/** SESSION/POST/GET VARS */
b455793d 54sqgetGlobalVar('session',$session);
55sqgetGlobalVar('mailbox',$mailbox);
1e2a6ff6 56if(!sqgetGlobalVar('identity',$identity)) {
57 $identity=0;
58}
edd82fcf 59sqgetGlobalVar('send_to',$send_to);
60sqgetGlobalVar('send_to_cc',$send_to_cc);
61sqgetGlobalVar('send_to_bcc',$send_to_bcc);
62sqgetGlobalVar('subject',$subject);
63sqgetGlobalVar('body',$body);
b455793d 64sqgetGlobalVar('mailprio',$mailprio);
65sqgetGlobalVar('request_mdn',$request_mdn);
66sqgetGlobalVar('request_dr',$request_dr);
67sqgetGlobalVar('html_addr_search',$html_addr_search);
68sqgetGlobalVar('mail_sent',$mail_sent);
69sqgetGlobalVar('passed_id',$passed_id);
70sqgetGlobalVar('passed_ent_id',$passed_ent_id);
71sqgetGlobalVar('send',$send);
0b97a708 72
b455793d 73sqgetGlobalVar('attach',$attach);
12a0ed01 74
b455793d 75sqgetGlobalVar('draft',$draft);
76sqgetGlobalVar('draft_id',$draft_id);
77sqgetGlobalVar('ent_num',$ent_num);
78sqgetGlobalVar('saved_draft',$saved_draft);
79sqgetGlobalVar('delete_draft',$delete_draft);
7772382e 80sqgetGlobalVar('startMessage',$startMessage);
953fa718 81
82/** POST VARS */
83sqgetGlobalVar('sigappend', $sigappend, SQ_POST);
84sqgetGlobalVar('from_htmladdr_search', $from_htmladdr_search, SQ_POST);
85sqgetGlobalVar('addr_search_done', $html_addr_search_done, SQ_POST);
86sqgetGlobalVar('send_to_search', $send_to_search, SQ_POST);
87sqgetGlobalVar('do_delete', $do_delete, SQ_POST);
88sqgetGlobalVar('delete', $delete, SQ_POST);
b0314f04 89sqgetGlobalVar('restoremessages', $restoremessages, SQ_POST);
953fa718 90if ( sqgetGlobalVar('return', $temp, SQ_POST) ) {
73ad81bf 91 $html_addr_search_done = 'Use Addresses';
953fa718 92}
93
94/** GET VARS */
95sqgetGlobalVar('attachedmessages', $attachedmessages, SQ_GET);
0b97a708 96
98a9cc03 97/** get smaction */
98if ( !sqgetGlobalVar('smaction',$action) )
99{
73ad81bf 100 if ( sqgetGlobalVar('smaction_reply',$tmp) ) $action = 'reply';
101 if ( sqgetGlobalVar('smaction_reply_all',$tmp) ) $action = 'reply_all';
102 if ( sqgetGlobalVar('smaction_forward',$tmp) ) $action = 'forward';
103 if ( sqgetGlobalVar('smaction_attache',$tmp) ) $action = 'forward_as_attachment';
104 if ( sqgetGlobalVar('smaction_draft',$tmp) ) $action = 'draft';
105 if ( sqgetGlobalVar('smaction_edit_new',$tmp) ) $action = 'edit_as_new';
98a9cc03 106}
107
3461167c 108/* Location (For HTTP 1.1 Header("Location: ...") redirects) */
109$location = get_location();
1e2a6ff6 110/* Identities (fetch only once) */
111$idents = get_identities();
3461167c 112
09044055 113/* --------------------- Specific Functions ------------------------------ */
0b97a708 114
41b94d65 115function replyAllString($header) {
73ad81bf 116 global $include_self_reply_all, $idents;
117 $excl_ar = array();
118 /**
119 * 1) Remove the addresses we'll be sending the message 'to'
120 */
121 $url_replytoall_avoid_addrs = '';
122 if (isset($header->replyto)) {
123 $excl_ar = $header->getAddr_a('replyto');
124 }
125 /**
126 * 2) Remove our identities from the CC list (they still can be in the
127 * TO list) only if $include_self_reply_all is turned off
128 */
129 if (!$include_self_reply_all) {
130 foreach($idents as $id) {
131 $excl_ar[strtolower(trim($id['email_address']))] = '';
132 }
133 }
134
135 /**
136 * 3) get the addresses.
137 */
138 $url_replytoall_ar = $header->getAddr_a(array('to','cc'), $excl_ar);
139
140 /**
141 * 4) generate the string.
142 */
143 $url_replytoallcc = '';
144 foreach( $url_replytoall_ar as $email => $personal) {
145 if ($personal) {
146 // if personal name contains address separator then surround
147 // the personal name with double quotes.
148 if (strpos($personal,',') !== false) {
149 $personal = '"'.$personal.'"';
150 }
151 $url_replytoallcc .= ", $personal <$email>";
152 } else {
153 $url_replytoallcc .= ', '. $email;
1e2a6ff6 154 }
73ad81bf 155 }
156 $url_replytoallcc = substr($url_replytoallcc,2);
157
158 return $url_replytoallcc;
09044055 159}
160
b0323712 161function getReplyCitation($orig_from, $orig_date) {
12a0ed01 162 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
790ad2f5 163 $orig_from = decodeHeader($orig_from->getAddress(false),false,false,true);
12a0ed01 164// $from = decodeHeader($orig_header->getAddr_s('from',"\n$indent"),false,false);
165 /* First, return an empty string when no citation style selected. */
166 if (($reply_citation_style == '') || ($reply_citation_style == 'none')) {
167 return '';
168 }
169
170 /* Make sure our final value isn't an empty string. */
171 if ($orig_from == '') {
172 return '';
173 }
174
175 /* Otherwise, try to select the desired citation style. */
176 switch ($reply_citation_style) {
73ad81bf 177 case 'author_said':
178 $start = '';
179 $end = ' ' . _("said") . ':';
180 break;
181 case 'quote_who':
182 $start = '<' . _("quote") . ' ' . _("who") . '="';
183 $end = '">';
184 break;
185 case 'date_time_author':
186 $start = 'On ' . getLongDateString($orig_date) . ', ';
187 $end = ' ' . _("said") . ':';
188 break;
189 case 'user-defined':
190 $start = $reply_citation_start .
191 ($reply_citation_start == '' ? '' : ' ');
192 $end = $reply_citation_end;
193 break;
194 default:
195 return '';
12a0ed01 196 }
197
198 /* Build and return the citation string. */
199 return ($start . $orig_from . $end . "\n");
200}
201
41b94d65 202function getforwardHeader($orig_header) {
19c6f7a7 203 global $editor_size;
204
73ad81bf 205 $display = array( _("Subject") => strlen(_("Subject")),
206 _("From") => strlen(_("From")),
207 _("Date") => strlen(_("Date")),
208 _("To") => strlen(_("To")),
209 _("Cc") => strlen(_("Cc")) );
210 $maxsize = max($display);
211 $indent = str_pad('',$maxsize+2);
212 foreach($display as $key => $val) {
213 $display[$key] = $key .': '. str_pad('', $maxsize - $val);
214 }
215 $from = decodeHeader($orig_header->getAddr_s('from',"\n$indent"),false,false,true);
216 $from = str_replace('&nbsp;',' ',$from);
217 $to = decodeHeader($orig_header->getAddr_s('to',"\n$indent"),false,false,true);
218 $to = str_replace('&nbsp;',' ',$to);
219 $subject = decodeHeader($orig_header->subject,false,false,true);
220 $subject = str_replace('&nbsp;',' ',$subject);
221 $bodyTop = str_pad(' '._("Original Message").' ',$editor_size -2,'-',STR_PAD_BOTH) .
222 "\n". $display[_("Subject")] . $subject . "\n" .
223 $display[_("From")] . $from . "\n" .
224 $display[_("Date")] . getLongDateString( $orig_header->date ). "\n" .
225 $display[_("To")] . $to . "\n";
226 if ($orig_header->cc != array() && $orig_header->cc !='') {
227 $cc = decodeHeader($orig_header->getAddr_s('cc',"\n$indent"),false,false,true);
228 $cc = str_replace('&nbsp;',' ',$cc);
229 $bodyTop .= $display[_("Cc")] .$cc . "\n";
230 }
231 $bodyTop .= str_pad('', $editor_size -2 , '-') .
232 "\n\n";
233 return $bodyTop;
41b94d65 234}
09044055 235/* ----------------------------------------------------------------------- */
236
44560457 237/*
1c044820 238 * If the session is expired during a post this restores the compose session
44560457 239 * vars.
240 */
5da08ef7 241if (sqsession_is_registered('session_expired_post')) {
953fa718 242 sqgetGlobalVar('session_expired_post', $session_expired_post, SQ_SESSION);
1c044820 243 /*
40934000 244 * extra check for username so we don't display previous post data from
245 * another user during this session.
246 */
247 if ($session_expired_post['username'] != $username) {
0ec1a14b 248 unset($session_expired_post);
0b97a708 249 sqsession_unregister('session_expired_post');
0ec1a14b 250 session_write_close();
40934000 251 } else {
252 foreach ($session_expired_post as $postvar => $val) {
253 if (isset($val)) {
254 $$postvar = $val;
255 } else {
256 $$postvar = '';
257 }
258 }
0ec1a14b 259 $compose_messages = unserialize(urldecode($restoremessages));
260 sqsession_register($compose_messages,'compose_messages');
261 sqsession_register($composesession,'composesession');
40934000 262 if (isset($send)) {
263 unset($send);
264 }
265 $session_expired = true;
266 }
5da08ef7 267 unset($session_expired_post);
0b97a708 268 sqsession_unregister('session_expired_post');
5da08ef7 269 session_write_close();
40934000 270 if (!isset($mailbox)) {
271 $mailbox = '';
272 }
273 if ($compose_new_win == '1') {
274 compose_Header($color, $mailbox);
275 } else {
276 displayPageHeader($color, $mailbox);
277 }
278 showInputForm($session, false);
279 exit();
44560457 280}
da95c4b6 281if (!isset($composesession)) {
282 $composesession = 0;
a43e4b90 283 sqsession_register(0,'composesession');
da95c4b6 284}
285
d7f8e6e6 286if (!isset($session) || (isset($newmessage) && $newmessage)) {
0b97a708 287 sqsession_unregister('composesession');
1c044820 288 $session = "$composesession" +1;
91f2085b 289 $composesession = $session;
a43e4b90 290 sqsession_register($composesession,'composesession');
1c044820 291}
a43e4b90 292if (!isset($compose_messages)) {
73ad81bf 293 $compose_messages = array();
a43e4b90 294}
40934000 295if (!isset($compose_messages[$session]) || ($compose_messages[$session] == NULL)) {
73ad81bf 296 /* if (!array_key_exists($session, $compose_messages)) { /* We can only do this in PHP >= 4.1 */
297 $composeMessage = new Message();
298 $rfc822_header = new Rfc822Header();
299 $composeMessage->rfc822_header = $rfc822_header;
300 $composeMessage->reply_rfc822_header = '';
301 $compose_messages[$session] = $composeMessage;
302 sqsession_register($compose_messages,'compose_messages');
5628fdde 303} else {
73ad81bf 304 $composeMessage=$compose_messages[$session];
a43e4b90 305}
a43e4b90 306
00793a25 307if (!isset($mailbox) || $mailbox == '' || ($mailbox == 'None')) {
308 $mailbox = 'INBOX';
309}
310
4dfb9db7 311if ($draft) {
312 /*
313 * Set $default_charset to correspond with the user's selection
314 * of language interface.
315 */
316 set_my_charset();
317 $composeMessage=$compose_messages[$session];
b7ff469f 318 if (! deliverMessage($composeMessage, true)) {
da95c4b6 319 showInputForm($session);
00793a25 320 exit();
734f4ee6 321 } else {
5da08ef7 322 unset($compose_messages[$session]);
00793a25 323 $draft_message = _("Draft Email Saved");
324 /* If this is a resumed draft, then delete the original */
325 if(isset($delete_draft)) {
3461167c 326 Header("Location: $location/delete_message.php?mailbox=" . urlencode($draft_folder) .
73ad81bf 327 "&message=$delete_draft&startMessage=1&saved_draft=yes");
00793a25 328 exit();
7058a2a9 329 }
9c3e6cd4 330 else {
331 if ($compose_new_win == '1') {
3461167c 332 Header("Location: $location/compose.php?saved_draft=yes&session=$composesession");
a61878d0 333 exit();
9c3e6cd4 334 }
335 else {
304c159b 336 Header("Location: $location/right_main.php?mailbox=" . urlencode($draft_folder) .
73ad81bf 337 "&startMessage=1&note=".urlencode($draft_message));
a61878d0 338 exit();
9c3e6cd4 339 }
00793a25 340 }
341 }
342}
343
4dfb9db7 344if ($send) {
0b97a708 345 if (isset($_FILES['attachfile']) &&
73ad81bf 346 $_FILES['attachfile']['tmp_name'] &&
347 $_FILES['attachfile']['tmp_name'] != 'none') {
da95c4b6 348 $AttachFailure = saveAttachedFiles($session);
00793a25 349 }
350 if (checkInput(false) && !isset($AttachFailure)) {
73ad81bf 351 if ($mailbox == "All Folders") {
352 /* We entered compose via the search results page */
353 $mailbox="INBOX"; /* Send 'em to INBOX, that's safe enough */
354 }
00793a25 355 $urlMailbox = urlencode (trim($mailbox));
3f6b1b6f 356 if (! isset($passed_id)) {
357 $passed_id = 0;
00793a25 358 }
d4c5c50c 359 /**
00793a25 360 * Set $default_charset to correspond with the user's selection
7058a2a9 361 * of language interface.
00793a25 362 */
363 set_my_charset();
d4c5c50c 364 /**
00793a25 365 * This is to change all newlines to \n
7058a2a9 366 * We'll change them to \r\n later (in the sendMessage function)
00793a25 367 */
368 $body = str_replace("\r\n", "\n", $body);
369 $body = str_replace("\r", "\n", $body);
370
d4c5c50c 371 /**
18c9998a 372 * Rewrap $body so that no line is bigger than $editor_size
00793a25 373 */
18c9998a 374 $body = explode("\n", $body);
375 $newBody = '';
376 foreach ($body as $line) {
377 if( $line <> '-- ' ) {
73ad81bf 378 $line = rtrim($line);
18c9998a 379 }
380 if (strlen($line) <= $editor_size + 1) {
381 $newBody .= $line . "\n";
382 } else {
383 sqWordWrap($line, $editor_size);
384 $newBody .= $line . "\n";
385
386 }
387
388 }
389 $body = $newBody;
1c044820 390
a43e4b90 391 $composeMessage=$compose_messages[$session];
d5181a1d 392
a91189d6 393 $Result = deliverMessage($composeMessage);
00793a25 394 if (! $Result) {
da95c4b6 395 showInputForm($session);
00793a25 396 exit();
397 }
dd4a44cd 398 unset($compose_messages[$session]);
00793a25 399 if ( isset($delete_draft)) {
3461167c 400 Header("Location: $location/delete_message.php?mailbox=" . urlencode( $draft_folder ).
73ad81bf 401 "&message=$delete_draft&startMessage=1&mail_sent=yes");
00793a25 402 exit();
403 }
9c3e6cd4 404 if ($compose_new_win == '1') {
0ec1a14b 405
3461167c 406 Header("Location: $location/compose.php?mail_sent=yes");
9c3e6cd4 407 }
408 else {
4a44d6e7 409 Header("Location: $location/right_main.php?mailbox=$urlMailbox".
73ad81bf 410 "&startMessage=$startMessage&mail_sent=yes");
9c3e6cd4 411 }
734f4ee6 412 } else {
9c3e6cd4 413 if ($compose_new_win == '1') {
414 compose_Header($color, $mailbox);
415 }
416 else {
417 displayPageHeader($color, $mailbox);
418 }
00793a25 419 if (isset($AttachFailure)) {
73ad81bf 420 plain_error_message(_("Could not move/copy file. File not attached"),
421 $color);
00793a25 422 }
00793a25 423 checkInput(true);
da95c4b6 424 showInputForm($session);
00793a25 425 /* sqimap_logout($imapConnection); */
426 }
e02775fe 427} elseif (isset($html_addr_search_done)) {
73ad81bf 428 if ($compose_new_win == '1') {
429 compose_Header($color, $mailbox);
430 }
431 else {
432 displayPageHeader($color, $mailbox);
433 }
00793a25 434
435 if (isset($send_to_search) && is_array($send_to_search)) {
436 foreach ($send_to_search as $k => $v) {
437 if (substr($k, 0, 1) == 'T') {
438 if ($send_to) {
439 $send_to .= ', ';
440 }
441 $send_to .= $v;
442 }
443 elseif (substr($k, 0, 1) == 'C') {
444 if ($send_to_cc) {
445 $send_to_cc .= ', ';
446 }
447 $send_to_cc .= $v;
448 }
449 elseif (substr($k, 0, 1) == 'B') {
450 if ($send_to_bcc) {
451 $send_to_bcc .= ', ';
452 }
453 $send_to_bcc .= $v;
454 }
455 }
456 }
da95c4b6 457 showInputForm($session);
e02775fe 458} elseif (isset($html_addr_search)) {
0b97a708 459 if (isset($_FILES['attachfile']) &&
73ad81bf 460 $_FILES['attachfile']['tmp_name'] &&
461 $_FILES['attachfile']['tmp_name'] != 'none') {
0b97a708 462 if(saveAttachedFiles($session)) {
00793a25 463 plain_error_message(_("Could not move/copy file. File not attached"), $color);
464 }
465 }
466 /*
467 * I am using an include so as to elminiate an extra unnecessary
468 * click. If you can think of a better way, please implement it.
469 */
470 include_once('./addrbook_search_html.php');
e02775fe 471} elseif (isset($attach)) {
da95c4b6 472 if (saveAttachedFiles($session)) {
00793a25 473 plain_error_message(_("Could not move/copy file. File not attached"), $color);
474 }
73ad81bf 475 if ($compose_new_win == '1') {
476 compose_Header($color, $mailbox);
477 } else {
478 displayPageHeader($color, $mailbox);
479 }
da95c4b6 480 showInputForm($session);
01265fba 481}
482elseif (isset($sigappend)) {
1e2a6ff6 483 $signature = $idents[$identity]['signature'];
484
01265fba 485 $body .= "\n\n".($prefix_sig==true? "-- \n":'').$signature;
486 if ($compose_new_win == '1') {
73ad81bf 487 compose_Header($color, $mailbox);
01265fba 488 } else {
489 displayPageHeader($color, $mailbox);
490 }
da95c4b6 491 showInputForm($session);
e02775fe 492} elseif (isset($do_delete)) {
73ad81bf 493 if ($compose_new_win == '1') {
494 compose_Header($color, $mailbox);
495 } else {
496 displayPageHeader($color, $mailbox);
497 }
00793a25 498
00793a25 499 if (isset($delete) && is_array($delete)) {
a43e4b90 500 $composeMessage = $compose_messages[$session];
00793a25 501 foreach($delete as $index) {
a91189d6 502 $attached_file = $composeMessage->entities[$index]->att_local_name;
a61878d0 503 unlink ($attached_file);
a91189d6 504 unset ($composeMessage->entities[$index]);
505 }
506 $new_entities = array();
507 foreach ($composeMessage->entities as $entity) {
508 $new_entities[] = $entity;
00793a25 509 }
a91189d6 510 $composeMessage->entities = $new_entities;
511 $compose_messages[$session] = $composeMessage;
512 sqsession_register($compose_messages, 'compose_messages');
00793a25 513 }
da95c4b6 514 showInputForm($session);
734f4ee6 515} else {
00793a25 516 /*
517 * This handles the default case as well as the error case
1c044820 518 * (they had the same code) --> if (isset($smtpErrors))
00793a25 519 */
44560457 520
521 if ($compose_new_win == '1') {
73ad81bf 522 compose_Header($color, $mailbox);
44560457 523 } else {
73ad81bf 524 displayPageHeader($color, $mailbox);
44560457 525 }
00793a25 526
527 $newmail = true;
528
a61878d0 529 if (!isset($passed_ent_id)) {
530 $passed_ent_id = '';
531 }
532 if (!isset($passed_id)) {
1c044820 533 $passed_id = '';
a61878d0 534 }
535 if (!isset($mailbox)) {
536 $mailbox = '';
1c044820 537 }
a61878d0 538 if (!isset($action)) {
539 $action = '';
540 }
1c044820 541
44560457 542 $values = newMail($mailbox,$passed_id,$passed_ent_id, $action, $session);
b9928adc 543
544 /* in case the origin is not read_body.php */
545 if (isset($send_to)) {
73ad81bf 546 $values['send_to'] = $send_to;
b9928adc 547 }
548 if (isset($send_to_cc)) {
73ad81bf 549 $values['send_to_cc'] = $send_to_cc;
b9928adc 550 }
551 if (isset($send_to_bcc)) {
73ad81bf 552 $values['send_to_bcc'] = $send_to_bcc;
b9928adc 553 }
2a2f2185 554 if (isset($subject)) {
73ad81bf 555 $values['subject'] = $subject;
2a2f2185 556 }
41b94d65 557 showInputForm($session, $values);
00793a25 558}
559
560exit();
561
00793a25 562/**************** Only function definitions go below *************/
563
92c6f757 564function getforwardSubject($subject)
565{
566 if ((substr(strtolower($subject), 0, 4) != 'fwd:') &&
73ad81bf 567 (substr(strtolower($subject), 0, 5) != '[fwd:') &&
568 (substr(strtolower($subject), 0, 6) != '[ fwd:')) {
92c6f757 569 $subject = '[Fwd: ' . $subject . ']';
570 }
571 return $subject;
572}
00793a25 573
48985d59 574/* This function is used when not sending or adding attachments */
44560457 575function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $session='') {
1e2a6ff6 576 global $editor_size, $default_use_priority, $body, $idents,
73ad81bf 577 $use_signature, $composesession, $data_dir, $username,
578 $username, $key, $imapServerAddress, $imapPort, $compose_messages,
579 $composeMessage, $body_quote;
4e519821 580 global $languages, $squirrelmail_language, $default_charset;
e7f1a81d 581
d4f20027 582 /*
583 * Set $default_charset to correspond with the user's selection
584 * of language interface. $default_charset global is not correct,
585 * if message is composed in new window.
586 */
587 set_my_charset();
588
91f2085b 589 $send_to = $send_to_cc = $send_to_bcc = $subject = $identity = '';
bdb92db3 590 $mailprio = 3;
44560457 591
41b94d65 592 if ($passed_id) {
44560457 593 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
73ad81bf 594 $imapPort, 0);
a61878d0 595
48985d59 596 sqimap_mailbox_select($imapConnection, $mailbox);
41b94d65 597 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
1c044820 598
a61878d0 599 $body = '';
600 if ($passed_ent_id) {
601 /* redefine the messsage in case of message/rfc822 */
602 $message = $message->getEntity($passed_ent_id);
603 /* message is an entity which contains the envelope and type0=message
73ad81bf 604 * and type1=rfc822. The actual entities are childs from
605 * $message->entities[0]. That's where the encoding and is located
606 */
a61878d0 607
608 $entities = $message->entities[0]->findDisplayEntity
73ad81bf 609 (array(), $alt_order = array('text/plain'));
a61878d0 610 if (!count($entities)) {
611 $entities = $message->entities[0]->findDisplayEntity
73ad81bf 612 (array(), $alt_order = array('text/plain','html/plain'));
a61878d0 613 }
614 $orig_header = $message->rfc822_header; /* here is the envelope located */
615 /* redefine the message for picking up the attachments */
616 $message = $message->entities[0];
617
618 } else {
619 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain'));
620 if (!count($entities)) {
621 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain','html/plain'));
622 }
623 $orig_header = $message->rfc822_header;
624 }
1c044820 625
41b94d65 626 $encoding = $message->header->encoding;
a61878d0 627 $type0 = $message->type0;
628 $type1 = $message->type1;
41b94d65 629 foreach ($entities as $ent) {
b455e47b 630 $msg = $message->getEntity($ent);
631 $type0 = $msg->type0;
632 $type1 = $msg->type1;
a61878d0 633 $unencoded_bodypart = mime_fetch_body($imapConnection, $passed_id, $ent);
634 $body_part_entity = $message->getEntity($ent);
635 $bodypart = decodeBody($unencoded_bodypart,
73ad81bf 636 $body_part_entity->header->encoding);
a61878d0 637 if ($type1 == 'html') {
5b755d9f 638 $bodypart = str_replace("\n", ' ', $bodypart);
bb977394 639 $bodypart = preg_replace(array('/<\/?p>/i','/<div><\/div>/i','/<br\s*(\/)*>/i','/<\/?div>/i'), "\n", $bodypart);
5b755d9f 640 $bodypart = str_replace(array('&nbsp;','&gt;','&lt;'),array(' ','>','<'),$bodypart);
a61878d0 641 $bodypart = strip_tags($bodypart);
642 }
e842b215 643 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
73ad81bf 644 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode')) {
e842b215 645 if (mb_detect_encoding($bodypart) != 'ASCII') {
f4bb5d22 646 $bodypart = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode', $bodypart);
e842b215 647 }
648 }
eceefdfe 649
73ad81bf 650 if (isset($body_part_entity->header->parameters['charset'])) {
651 $actual = $body_part_entity->header->parameters['charset'];
652 } else {
653 $actual = 'us-ascii';
654 }
beca818e 655
73ad81bf 656 if ( $actual && is_conversion_safe($actual) && $actual != $default_charset){
657 $bodypart = charset_convert($actual,$bodypart,$default_charset,false);
658 }
eceefdfe 659
a61878d0 660 $body .= $bodypart;
661 }
662 if ($default_use_priority) {
663 $mailprio = substr($orig_header->priority,0,1);
664 if (!$mailprio) {
665 $mailprio = 3;
666 }
667 } else {
668 $mailprio = '';
669 }
a43e4b90 670 //ClearAttachments($session);
bdb92db3 671
672 $identity = '';
a45887d7 673 $from_o = $orig_header->from;
fe868193 674 if (is_array($from_o)) {
675 if (isset($from_o[0])) {
676 $from_o = $from_o[0];
677 }
678 }
bdb92db3 679 if (is_object($from_o)) {
680 $orig_from = $from_o->getAddress();
681 } else {
682 $orig_from = '';
a61878d0 683 }
1e2a6ff6 684
a91189d6 685 $identities = array();
1e2a6ff6 686 if (count($idents) > 1) {
687 foreach($idents as $nr=>$data) {
688 $enc_from_name = '"'.$data['full_name'].'" <'. $data['email_address'].'>';
689 if($enc_from_name == $orig_from) {
690 $identity = $nr;
a61878d0 691 break;
692 }
a91189d6 693 $identities[] = $enc_from_name;
694 }
1e2a6ff6 695
a91189d6 696 $identity_match = $orig_header->findAddress($identities);
697 if ($identity_match) {
698 $identity = $identity_match;
a61878d0 699 }
bdb92db3 700 }
a61878d0 701
702 switch ($action) {
73ad81bf 703 case ('draft'):
704 $use_signature = FALSE;
705 $composeMessage->rfc822_header = $orig_header;
706 $send_to = decodeHeader($orig_header->getAddr_s('to'),false,false,true);
707 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'),false,false,true);
708 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'),false,false,true);
709 $send_from = $orig_header->getAddr_s('from');
710 $send_from_parts = new AddressStructure();
711 $send_from_parts = $orig_header->parseAddress($send_from);
712 $send_from_add = $send_from_parts->mailbox . '@' . $send_from_parts->host;
713 $identities = get_identities();
714 if (count($identities) > 0) {
715 foreach($identities as $iddata) {
716 if ($send_from_add == $iddata['email_address']) {
717 $identity = $iddata['index'];
718 break;
719 }
a656569f 720 }
721 }
73ad81bf 722 $subject = decodeHeader($orig_header->subject,false,false,true);
723 /* remember the references and in-reply-to headers in case of an reply */
724 $composeMessage->rfc822_header->more_headers['References'] = $orig_header->references;
725 $composeMessage->rfc822_header->more_headers['In-Reply-To'] = $orig_header->in_reply_to;
726 // rewrap the body to clean up quotations and line lengths
727 sqBodyWrap($body, $editor_size);
728 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
729 break;
730 case ('edit_as_new'):
731 $send_to = decodeHeader($orig_header->getAddr_s('to'),false,false,true);
732 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'),false,false,true);
733 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'),false,false,true);
734 $subject = decodeHeader($orig_header->subject,false,false,true);
735 $mailprio = $orig_header->priority;
736 $orig_from = '';
737 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
738 // rewrap the body to clean up quotations and line lengths
739 sqBodyWrap($body, $editor_size);
740 break;
741 case ('forward'):
742 $send_to = '';
743 $subject = getforwardSubject(decodeHeader($orig_header->subject,false,false,true));
744 $body = getforwardHeader($orig_header) . $body;
745 // the logic for calling sqUnWordWrap here would be to allow the browser to wrap the lines
746 // forwarded message text should be as undisturbed as possible, so commenting out this call
747 // sqUnWordWrap($body);
748 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
749 //add a blank line after the forward headers
750 $body = "\n" . $body;
751 break;
752 case ('forward_as_attachment'):
753 $subject = getforwardSubject(decodeHeader($orig_header->subject,false,false,true));
754 $composeMessage = getMessage_RFC822_Attachment($message, $composeMessage, $passed_id, $passed_ent_id, $imapConnection);
755 $body = '';
756 break;
757 case ('reply_all'):
758 if(isset($orig_header->mail_followup_to) && $orig_header->mail_followup_to) {
759 $send_to = $orig_header->getAddr_s('mail_followup_to');
b268e66b 760 } else {
73ad81bf 761 $send_to_cc = replyAllString($orig_header);
762 $send_to_cc = decodeHeader($send_to_cc,false,false,true);
b268e66b 763 }
73ad81bf 764 case ('reply'):
765 // skip this if send_to was already set right above here
766 if(!$send_to) {
767 $send_to = $orig_header->reply_to;
768 if (is_array($send_to) && count($send_to)) {
769 $send_to = $orig_header->getAddr_s('reply_to');
770 } else if (is_object($send_to)) { /* unneccesarry, just for failsafe purpose */
771 $send_to = $orig_header->getAddr_s('reply_to');
772 } else {
773 $send_to = $orig_header->getAddr_s('from');
774 }
dd4a44cd 775 }
73ad81bf 776 $send_to = decodeHeader($send_to,false,false,true);
777 $subject = decodeHeader($orig_header->subject,false,false,true);
778 $subject = str_replace('"', "'", $subject);
779 $subject = trim($subject);
780 if (substr(strtolower($subject), 0, 3) != 're:') {
781 $subject = 'Re: ' . $subject;
782 }
783 /* this corrects some wrapping/quoting problems on replies */
784 $rewrap_body = explode("\n", $body);
785 $from = (is_array($orig_header->from)) ? $orig_header->from[0] : $orig_header->from;
786 $body = '';
787 $strip_sigs = getPref($data_dir, $username, 'strip_sigs');
788 foreach ($rewrap_body as $line) {
789 if ($strip_sigs && substr($line,0,3) == '-- ') {
790 break;
791 }
792 if (preg_match("/^(>+)/", $line, $matches)) {
793 $gt = $matches[1];
794 $body .= $body_quote . str_replace("\n", "\n$body_quote$gt ", rtrim($line)) ."\n";
795 } else {
796 $body .= $body_quote . (!empty($body_quote) ? ' ' : '') . str_replace("\n", "\n$body_quote" . (!empty($body_quote) ? ' ' : ''), rtrim($line)) . "\n";
797 }
a61878d0 798 }
c9d61baf 799
73ad81bf 800 //rewrap the body to clean up quotations and line lengths
801 $body = sqBodyWrap ($body, $editor_size);
c9d61baf 802
73ad81bf 803 $body = getReplyCitation($from , $orig_header->date) . $body;
804 $composeMessage->reply_rfc822_header = $orig_header;
12a0ed01 805
73ad81bf 806 break;
807 default:
808 break;
41b94d65 809 }
a91189d6 810 $compose_messages[$session] = $composeMessage;
811 sqsession_register($compose_messages, 'compose_messages');
5da08ef7 812 session_write_close();
a61878d0 813 sqimap_logout($imapConnection);
41b94d65 814 }
a61878d0 815 $ret = array( 'send_to' => $send_to,
73ad81bf 816 'send_to_cc' => $send_to_cc,
817 'send_to_bcc' => $send_to_bcc,
818 'subject' => $subject,
819 'mailprio' => $mailprio,
820 'body' => $body,
821 'identity' => $identity );
a61878d0 822
41b94d65 823 return ($ret);
48985d59 824} /* function newMail() */
825
a43e4b90 826function getAttachments($message, &$composeMessage, $passed_id, $entities, $imapConnection) {
59edcad6 827 global $attachment_dir, $username, $data_dir, $squirrelmail_language;
48985d59 828 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1c044820 829 if (!count($message->entities) ||
73ad81bf 830 ($message->type0 == 'message' && $message->type1 == 'rfc822')) {
41b94d65 831 if ( !in_array($message->entity_id, $entities) && $message->entity_id) {
73ad81bf 832 switch ($message->type0) {
833 case 'message':
834 if ($message->type1 == 'rfc822') {
835 $filename = $message->rfc822_header->subject;
836 if ($filename == "") {
837 $filename = "untitled-".$message->entity_id;
838 }
839 $filename .= '.msg';
840 } else {
841 $filename = $message->getFilename();
181538ac 842 }
73ad81bf 843 break;
844 default:
845 if (!$message->mime_header) { /* temporary hack */
846 $message->mime_header = $message->header;
847 }
848 $filename = $message->getFilename();
849 break;
850 }
851 $filename = str_replace('&#32;', ' ', decodeHeader($filename));
852 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
853 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_encode')) {
f4bb5d22 854 $filename = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_encode', $filename);
73ad81bf 855 }
856 $localfilename = GenerateRandomString(32, '', 7);
857 $full_localfilename = "$hashed_attachment_dir/$localfilename";
858 while (file_exists($full_localfilename)) {
859 $localfilename = GenerateRandomString(32, '', 7);
860 $full_localfilename = "$hashed_attachment_dir/$localfilename";
861 }
862 $message->att_local_name = $full_localfilename;
863
864 $composeMessage->initAttachment($message->type0.'/'.$message->type1,$filename,
865 $full_localfilename);
866
867 /* Write Attachment to file */
868 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'wb');
869 fputs($fp, decodeBody(mime_fetch_body($imapConnection,
870 $passed_id, $message->entity_id),
871 $message->header->encoding));
872 fclose ($fp);
48985d59 873 }
734f4ee6 874 } else {
a43e4b90 875 for ($i=0, $entCount=count($message->entities); $i<$entCount;$i++) {
876 $composeMessage=getAttachments($message->entities[$i], $composeMessage, $passed_id, $entities, $imapConnection);
48985d59 877 }
878 }
a43e4b90 879 return $composeMessage;
48985d59 880}
881
1c044820 882function getMessage_RFC822_Attachment($message, $composeMessage, $passed_id,
73ad81bf 883 $passed_ent_id='', $imapConnection) {
6201339c 884 global $attachments, $attachment_dir, $username, $data_dir;
a6ec592e 885 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
756406df 886 if (!$passed_ent_id) {
1c044820 887 $body_a = sqimap_run_command($imapConnection,
73ad81bf 888 'FETCH '.$passed_id.' RFC822',
889 TRUE, $response, $readmessage,
890 TRUE);
756406df 891 } else {
1c044820 892 $body_a = sqimap_run_command($imapConnection,
73ad81bf 893 'FETCH '.$passed_id.' BODY['.$passed_ent_id.']',
894 TRUE, $response, $readmessage, TRUE);
a61878d0 895 $message = $message->parent;
756406df 896 }
d0519c03 897 if ($response == 'OK') {
a61878d0 898 $subject = encodeHeader($message->rfc822_header->subject);
899 array_shift($body_a);
1c044820 900 array_pop($body_a);
a61878d0 901 $body = implode('', $body_a) . "\r\n";
1c044820 902
a61878d0 903 $localfilename = GenerateRandomString(32, 'FILE', 7);
904 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1c044820 905
906 $fp = fopen($full_localfilename, 'w');
a61878d0 907 fwrite ($fp, $body);
908 fclose($fp);
5a1f1da3 909 $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
73ad81bf 910 $full_localfilename);
a43e4b90 911 }
912 return $composeMessage;
a6ec592e 913}
914
41b94d65 915function showInputForm ($session, $values=false) {
4a1788b3 916 global $send_to, $send_to_cc, $body, $startMessage,
73ad81bf 917 $passed_body, $color, $use_signature, $signature, $prefix_sig,
918 $editor_size, $editor_height, $attachments, $subject, $newmail,
919 $use_javascript_addr_book, $send_to_bcc, $passed_id, $mailbox,
920 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
921 $username, $data_dir, $identity, $idents, $draft_id, $delete_draft,
922 $mailprio, $default_use_mdn, $mdn_user_support, $compose_new_win,
923 $saved_draft, $mail_sent, $sig_first, $edit_as_new, $action,
924 $username, $compose_messages, $composesession, $default_charset;
a43e4b90 925
926 $composeMessage = $compose_messages[$session];
41b94d65 927 if ($values) {
73ad81bf 928 $send_to = $values['send_to'];
929 $send_to_cc = $values['send_to_cc'];
930 $send_to_bcc = $values['send_to_bcc'];
931 $subject = $values['subject'];
932 $mailprio = $values['mailprio'];
933 $body = $values['body'];
934 $identity = (int) $values['identity'];
676bb189 935 } else {
73ad81bf 936 $send_to = decodeHeader($send_to, true, false);
937 $send_to_cc = decodeHeader($send_to_cc, true, false);
938 $send_to_bcc = decodeHeader($send_to_bcc, true, false);
41b94d65 939 }
1c044820 940
48985d59 941 if ($use_javascript_addr_book) {
39bfea8f 942 echo "\n". '<script language="JavaScript">'."\n<!--\n" .
73ad81bf 943 'function open_abook() { ' . "\n" .
944 ' var nwin = window.open("addrbook_popup.php","abookpopup",' .
945 '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" .
946 ' if((!nwin.opener) && (document.windows != null))' . "\n" .
947 ' nwin.opener = document.windows;' . "\n" .
948 "}\n" .
949 "// -->\n</script>\n\n";
48985d59 950 }
951
4a1788b3 952 echo "\n" . '<form name="compose" action="compose.php" method="post" ' .
73ad81bf 953 'enctype="multipart/form-data"';
4a1788b3 954 do_hook('compose_form');
1c044820 955
48985d59 956 echo ">\n";
957
df96b37a 958 echo addHidden('startMessage', $startMessage);
4a1788b3 959
41b94d65 960 if ($action == 'draft') {
df96b37a 961 echo addHidden('delete_draft', $passed_id);
48985d59 962 }
963 if (isset($delete_draft)) {
df96b37a 964 echo addHidden('delete_draft', $delete_draft);
48985d59 965 }
da95c4b6 966 if (isset($session)) {
df96b37a 967 echo addHidden('session', $session);
da95c4b6 968 }
1c044820 969
08bad2b1 970 if (isset($passed_id)) {
df96b37a 971 echo addHidden('passed_id', $passed_id);
08bad2b1 972 }
44560457 973
9c3e6cd4 974 if ($saved_draft == 'yes') {
39bfea8f 975 echo '<br /><center><b>'. _("Draft Saved").'</center></b>';
9c3e6cd4 976 }
977 if ($mail_sent == 'yes') {
39bfea8f 978 echo '<br /><center><b>'. _("Your Message has been sent.").'</center></b>';
9c3e6cd4 979 }
4a1788b3 980 echo '<table align="center" cellspacing="0" border="0">' . "\n";
9c3e6cd4 981 if ($compose_new_win == '1') {
39bfea8f 982 echo '<table align="center" bgcolor="'.$color[0].'" width="100%" border="0">'."\n" .
73ad81bf 983 ' <tr><td></td>'.html_tag( 'td', '', 'right' ).
984 '<input type="button" name="Close" onClick="return self.close()" value="'.
985 _("Close").'" /></td></tr>'."\n";
9c3e6cd4 986 }
78a35fcd 987 if ($location_of_buttons == 'top') {
988 showComposeButtonRow();
989 }
48985d59 990
0f257091 991 /* display select list for identities */
1e2a6ff6 992 if (count($idents) > 1) {
73ad81bf 993 $ident_list = array();
994 foreach($idents as $id => $data) {
995 $ident_list[$id] =
996 $data['full_name'].' <'.$data['email_address'].'>';
997 }
0f257091 998 echo ' <tr>' . "\n" .
73ad81bf 999 html_tag( 'td', '', 'right', $color[4], 'width="10%"' ) .
1000 _("From:") . '</td>' . "\n" .
1001 html_tag( 'td', '', 'left', $color[4], 'width="90%"' ) .
1002 ' '.
1003 addSelect('identity', $ident_list, $identity, TRUE);
1e2a6ff6 1004
df96b37a 1005 echo ' </td>' . "\n" .
73ad81bf 1006 ' </tr>' . "\n";
41b94d65 1007 }
46f2284f 1008
0f257091 1009 echo ' <tr>' . "\n" .
73ad81bf 1010 html_tag( 'td', '', 'right', $color[4], 'width="10%"' ) .
1011 _("To") . ':</td>' . "\n" .
1012 html_tag( 'td', '', 'left', $color[4], 'width="90%"' ) .
1013 addInput('send_to', $send_to, 60). '<br />' . "\n" .
1014 ' </td>' . "\n" .
1015 ' </tr>' . "\n" .
1016 ' <tr>' . "\n" .
1017 html_tag( 'td', '', 'right', $color[4] ) .
1018 _("Cc") . ':</td>' . "\n" .
1019 html_tag( 'td', '', 'left', $color[4] ) .
1020 addInput('send_to_cc', $send_to_cc, 60). '<br />' . "\n" .
1021 ' </td>' . "\n" .
1022 ' </tr>' . "\n" .
1023 ' <tr>' . "\n" .
1024 html_tag( 'td', '', 'right', $color[4] ) .
1025 _("Bcc") . ':</td>' . "\n" .
1026 html_tag( 'td', '', 'left', $color[4] ) .
1027 addInput('send_to_bcc', $send_to_bcc, 60).'<br />' . "\n" .
1028 ' </td>' . "\n" .
1029 ' </tr>' . "\n" .
1030 ' <tr>' . "\n" .
1031 html_tag( 'td', '', 'right', $color[4] ) .
1032 _("Subject") . ':</td>' . "\n" .
1033 html_tag( 'td', '', 'left', $color[4] ) . "\n";
df96b37a 1034 echo ' '.addInput('subject', $subject, 60).
73ad81bf 1035 ' </td>' . "\n" .
1036 ' </tr>' . "\n\n";
48985d59 1037
78a35fcd 1038 if ($location_of_buttons == 'between') {
1039 showComposeButtonRow();
1040 }
4dfb9db7 1041
0f257091 1042 /* why this distinction? */
fdc83c55 1043 if ($compose_new_win == '1') {
39bfea8f 1044 echo ' <tr>' . "\n" .
73ad81bf 1045 ' <td bgcolor="' . $color[0] . '" colspan="2" align="center">' . "\n" .
1046 ' <textarea name="body" id="body" rows="' . (int)$editor_height .
1047 '" cols="' . (int)$editor_size . '" wrap="virtual">';
fdc83c55 1048 }
1049 else {
39bfea8f 1050 echo ' <tr>' . "\n" .
1051 ' <td bgcolor="' . $color[4] . '" colspan="2">' . "\n" .
1052 ' &nbsp;&nbsp;<textarea name="body" id="body" rows="' . (int)$editor_height .
1053 '" cols="' . (int)$editor_size . '" wrap="virtual">';
fdc83c55 1054 }
0f257091 1055
48985d59 1056 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
1e2a6ff6 1057 $signature = $idents[$identity]['signature'];
d3c13a51 1058
3b17e952 1059 if ($sig_first == '1') {
ab4700c3 1060 if ($default_charset == 'iso-2022-jp') {
83be314a 1061 echo "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
1062 } else {
73ad81bf 1063 echo "\n\n".($prefix_sig==true? "-- \n":'').decodeHeader($signature,false,false);
83be314a 1064 }
df96b37a 1065 echo "\n\n".htmlspecialchars(decodeHeader($body,false,false));
3b17e952 1066 }
1067 else {
df96b37a 1068 echo "\n\n".htmlspecialchars(decodeHeader($body,false,false));
ab4700c3 1069 if ($default_charset == 'iso-2022-jp') {
83be314a 1070 echo "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
1071 }else{
73ad81bf 1072 echo "\n\n".($prefix_sig==true? "-- \n":'').decodeHeader($signature,false,false);
1073 }
3b17e952 1074 }
73ad81bf 1075 } else {
1076 echo htmlspecialchars(decodeHeader($body,false,false));
48985d59 1077 }
0f257091 1078 echo '</textarea><br />' . "\n" .
73ad81bf 1079 ' </td>' . "\n" .
1080 ' </tr>' . "\n";
48985d59 1081
12a0ed01 1082
48985d59 1083 if ($location_of_buttons == 'bottom') {
1084 showComposeButtonRow();
1085 } else {
0f257091 1086 echo ' <tr>' . "\n" .
73ad81bf 1087 html_tag( 'td', '', 'right', '', 'colspan="2"' ) . "\n" .
1088 ' ' . addSubmit(_("Send"), 'send').
1089 ' &nbsp;&nbsp;&nbsp;&nbsp;<br /><br />' . "\n" .
1090 ' </td>' . "\n" .
1091 ' </tr>' . "\n";
48985d59 1092 }
46bb8da8 1093
48985d59 1094 /* This code is for attachments */
73ad81bf 1095 if ((bool) ini_get('file_uploads')) {
1096
1097 /* Calculate the max size for an uploaded file.
1098 * This is advisory for the user because we can't actually prevent
1099 * people to upload too large files. */
1100 $sizes = array();
1101 /* php.ini vars which influence the max for uploads */
1102 $configvars = array('post_max_size', 'memory_limit', 'upload_max_filesize');
1103 foreach($configvars as $var) {
1104 /* skip 0 or empty values */
1105 if( $size = getByteSize(ini_get($var)) ) {
1106 $sizes[] = $size;
1107 }
0a2c3218 1108 }
0a2c3218 1109
73ad81bf 1110 if(count($sizes) > 0) {
1111 $maxsize = '(max.&nbsp;' . show_readable_size( min( $sizes ) ) . ')';
1112 } else {
1113 $maxsize = '';
1114 }
1115 echo addHidden('MAX_FILE_SIZE', min( $sizes )).
1116 ' <tr>' . "\n" .
1117 ' <td colspan="2">' . "\n" .
1118 ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.
1119 ' border="0" bgcolor="'.$color[9].'">' . "\n" .
1120 ' <tr>' . "\n" .
1121 ' <td>' . "\n" .
1122 ' <table width="100%" cellpadding="3" cellspacing="0" align="center"'.
1123 ' border="0">' . "\n" .
1124 ' <tr>' . "\n" .
1125 html_tag( 'td', '', 'right', '', 'valign="middle"' ) .
1126 _("Attach:") . '</td>' . "\n" .
1127 html_tag( 'td', '', 'left', '', 'valign="middle"' ) .
1128 ' <input name="attachfile" size="48" type="file" />' . "\n" .
1129 ' &nbsp;&nbsp;<input type="submit" name="attach"' .
1130 ' value="' . _("Add") .'" />' . "\n" .
1131 $maxsize .
1132 ' </td>' . "\n" .
1133 ' </tr>' . "\n";
1134
1135 $s_a = array();
1136 if ($composeMessage->entities) {
1137 foreach ($composeMessage->entities as $key => $attachment) {
1138 $attached_file = $attachment->att_local_name;
1139 if ($attachment->att_local_name || $attachment->body_part) {
1140 $attached_filename = decodeHeader($attachment->mime_header->getParameter('name'));
1141 $type = $attachment->mime_header->type0.'/'.
a91189d6 1142 $attachment->mime_header->type1;
1c044820 1143
73ad81bf 1144 $s_a[] = '<table bgcolor="'.$color[0].
1145 '" border="0"><tr><td>'.
1146 addCheckBox('delete[]', FALSE, $key).
1147 "</td><td>\n" . $attached_filename .
1148 '</td><td>-</td><td> ' . $type . '</td><td>('.
1149 show_readable_size( filesize( $attached_file ) ) . ')</td></tr></table>'."\n";
1150 }
1151 }
4dfb9db7 1152 }
73ad81bf 1153 if (count($s_a)) {
1154 foreach ($s_a as $s) {
1155 echo '<tr>' . html_tag( 'td', '', 'left', $color[0], 'colspan="2"' ) . $s .'</td></tr>';
1156 }
1157 echo '<tr><td colspan="2"><input type="submit" name="do_delete" value="' .
1158 _("Delete selected attachments") . "\" />\n" .
1159 '</td></tr>';
1160 }
1161 echo ' </table>' . "\n" .
1162 ' </td>' . "\n" .
1163 ' </tr>' . "\n" .
1164 ' </table>' . "\n" .
1165 ' </td>' . "\n" .
1166 ' </tr>' . "\n";
1167 } // End of file_uploads if-block
41b94d65 1168 /* End of attachment code */
07687736 1169 if ($compose_new_win == '1') {
39bfea8f 1170 echo '</table>'."\n";
07687736 1171 }
a64f47e7 1172
39bfea8f 1173 echo '</table>' . "\n" .
0fa24cb6 1174 addHidden('username', $username).
1175 addHidden('smaction', $action).
1176 addHidden('mailbox', $mailbox);
1c044820 1177 /*
1178 store the complete ComposeMessages array in a hidden input value
0ec1a14b 1179 so we can restore them in case of a session timeout.
73ad81bf 1180 */
953fa718 1181 sqgetGlobalVar('QUERY_STRING', $queryString, SQ_SERVER);
df96b37a 1182 echo addHidden('restoremessages', serialize($compose_messages)).
73ad81bf 1183 addHidden('composesession', $composesession).
1184 addHidden('querystring', $queryString).
1185 "</form>\n";
a64f47e7 1186 if (!(bool) ini_get('file_uploads')) {
73ad81bf 1187 /* File uploads are off, so we didn't show that part of the form.
1188 To avoid bogus bug reports, tell the user why. */
1189 echo 'Because PHP file uploads are turned off, you can not attach files ';
1190 echo "to this message. Please see your system administrator for details.\r\n";
a64f47e7 1191 }
1192
9f599fe3 1193 do_hook('compose_bottom');
39bfea8f 1194 echo '</body></html>' . "\n";
48985d59 1195}
1196
1197
70c4fd84 1198function showComposeButtonRow() {
78a35fcd 1199 global $use_javascript_addr_book, $save_as_draft,
73ad81bf 1200 $default_use_priority, $mailprio, $default_use_mdn,
1201 $request_mdn, $request_dr,
1202 $data_dir, $username;
70c4fd84 1203
39bfea8f 1204 echo ' <tr>' . "\n" .
73ad81bf 1205 ' <td></td>' . "\n" .
1206 ' <td>' . "\n";
ae25968c 1207 if ($default_use_priority) {
1208 if(!isset($mailprio)) {
df96b37a 1209 $mailprio = '3';
1210 }
1211 echo ' ' . _("Priority") .
73ad81bf 1212 addSelect('mailprio', array(
1213 '1' => _("High"),
1214 '3' => _("Normal"),
1215 '5' => _("Low") ), $mailprio, TRUE);
ae25968c 1216 }
1217 $mdn_user_support=getPref($data_dir, $username, 'mdn_user_support',$default_use_mdn);
1218 if ($default_use_mdn) {
70c4fd84 1219 if ($mdn_user_support) {
0ec1a14b 1220 echo ' ' . _("Receipt") .': '.
73ad81bf 1221 addCheckBox('request_mdn', $request_mdn == '1', '1'). _("On Read").
1222 addCheckBox('request_dr', $request_dr == '1', '1'). _("On Delivery");
70c4fd84 1223 }
ae25968c 1224 }
48985d59 1225
39bfea8f 1226 echo ' </td>' . "\n" .
73ad81bf 1227 ' </tr>' . "\n" .
1228 ' <tr>' . "\n" .
1229 ' <td></td>' . "\n" .
1230 ' <td>' . "\n" .
1231 ' <input type="submit" name="sigappend" value="' . _("Signature") . '" />' . "\n";
78a35fcd 1232 if ($use_javascript_addr_book) {
39bfea8f 1233 echo " <script language=\"JavaScript\"><!--\n document.write(\"".
73ad81bf 1234 " <input type=button value=\\\""._("Addresses").
1235 "\\\" onclick=\\\"javascript:open_abook();\\\" />\");".
1236 " // --></script><noscript>\n".
1237 ' <input type="submit" name="html_addr_search" value="'.
1238 _("Addresses").'" />'.
1239 " </noscript>\n";
734f4ee6 1240 } else {
39bfea8f 1241 echo ' <input type="submit" name="html_addr_search" value="'.
73ad81bf 1242 _("Addresses").'" />' . "\n";
78a35fcd 1243 }
48985d59 1244
78a35fcd 1245 if ($save_as_draft) {
39bfea8f 1246 echo ' <input type="submit" name ="draft" value="' . _("Save Draft") . "\" />\n";
78a35fcd 1247 }
0a17f9dd 1248
39bfea8f 1249 echo ' <input type="submit" name="send" value="'. _("Send") . '" />' . "\n";
78a35fcd 1250 do_hook('compose_button_row');
441f2d33 1251
39bfea8f 1252 echo ' </td>' . "\n" .
73ad81bf 1253 ' </tr>' . "\n\n";
78a35fcd 1254}
b278172f 1255
70c4fd84 1256function checkInput ($show) {
78a35fcd 1257 /*
1258 * I implemented the $show variable because the error messages
1259 * were getting sent before the page header. So, I check once
1260 * using $show=false, and then when i'm ready to display the error
1261 * message, show=true
1262 */
6bf2a88f 1263 global $body, $send_to, $send_to_bcc, $subject, $color;
78a35fcd 1264
6bf2a88f 1265 if ($send_to == '' && $send_to_bcc == '') {
78a35fcd 1266 if ($show) {
0ad7dbda 1267 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
78a35fcd 1268 }
1269 return false;
1270 }
1271 return true;
1272} /* function checkInput() */
df15de21 1273
3806fa52 1274
00793a25 1275/* True if FAILURE */
da95c4b6 1276function saveAttachedFiles($session) {
0b97a708 1277 global $_FILES, $attachment_dir, $attachments, $username,
73ad81bf 1278 $data_dir, $compose_messages;
4c9d2242 1279
45cdd1b5 1280 /* get out of here if no file was attached at all */
1281 if (! is_uploaded_file($_FILES['attachfile']['tmp_name']) ) {
1282 return true;
1283 }
1284
4c9d2242 1285 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1286 $localfilename = GenerateRandomString(32, '', 7);
1287 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1288 while (file_exists($full_localfilename)) {
1289 $localfilename = GenerateRandomString(32, '', 7);
1290 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1291 }
1292
e6675f9a 1293 // FIXME: we SHOULD prefer move_uploaded_file over rename because
1294 // m_u_f works better with restricted PHP installes (safe_mode, open_basedir)
1295 if (!@rename($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
73ad81bf 1296 if (!@move_uploaded_file($_FILES['attachfile']['tmp_name'],$full_localfilename)) {
1297 return true;
1298 }
a61878d0 1299 }
a43e4b90 1300 $message = $compose_messages[$session];
0b97a708 1301 $type = strtolower($_FILES['attachfile']['type']);
1302 $name = $_FILES['attachfile']['name'];
a43e4b90 1303 $message->initAttachment($type, $name, $full_localfilename);
1304 $compose_messages[$session] = $message;
b0314f04 1305 sqsession_register($compose_messages , 'compose_messages');
4c9d2242 1306}
1307
a43e4b90 1308function ClearAttachments($composeMessage) {
b48d3c53 1309 if ($composeMessage->att_local_name) {
1310 $attached_file = $composeMessage->att_local_name;
a43e4b90 1311 if (file_exists($attached_file)) {
1312 unlink($attached_file);
8712abea 1313 }
da95c4b6 1314 }
a43e4b90 1315 for ($i=0, $entCount=count($composeMessage->entities);$i< $entCount; ++$i) {
1316 ClearAttachments($composeMessage->entities[$i]);
1317 }
4c9d2242 1318}
1319
0a2c3218 1320/* parse values like 8M and 2k into bytes */
1321function getByteSize($ini_size) {
1322
4d30dc83 1323 if(!$ini_size) {
1324 return FALSE;
1325 }
da95c4b6 1326
0a2c3218 1327 $ini_size = trim($ini_size);
1328
5b9716de 1329 // if there's some kind of letter at the end of the string we need to multiply.
1330 if(!is_numeric(substr($ini_size, -1))) {
1331
1332 switch(strtoupper(substr($ini_size, -1))) {
1333 case 'G':
73ad81bf 1334 $bytesize = 1073741824;
1335 break;
5b9716de 1336 case 'M':
73ad81bf 1337 $bytesize = 1048576;
1338 break;
5b9716de 1339 case 'K':
73ad81bf 1340 $bytesize = 1024;
1341 break;
5b9716de 1342 }
1343
4d30dc83 1344 return ($bytesize * (int)substr($ini_size, 0, -1));
0a2c3218 1345 }
1c044820 1346
4d30dc83 1347 return $ini_size;
0a2c3218 1348}
a43e4b90 1349
4c9d2242 1350
a43e4b90 1351/* temporary function to make use of the deliver class.
1352 In the future the responsable backend should be automaticly loaded
1353 and conf.pl should show a list of available backends.
1354 The message also should be constructed by the message class.
73ad81bf 1355 */
a43e4b90 1356
b7ff469f 1357function deliverMessage($composeMessage, $draft=false) {
a43e4b90 1358 global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body,
73ad81bf 1359 $username, $popuser, $usernamedata, $identity, $idents, $data_dir,
1360 $request_mdn, $request_dr, $default_charset, $color, $useSendmail,
1361 $domain, $action, $default_move_to_sent, $move_to_sent;
a43e4b90 1362 global $imapServerAddress, $imapPort, $sent_folder, $key;
1363
1364 $rfc822_header = $composeMessage->rfc822_header;
24192f77 1365
1366 $abook = addressbook_init(false, true);
310dfeb6 1367 $rfc822_header->to = $rfc822_header->parseAddress($send_to,true, array(), '', $domain, array(&$abook,'lookup'));
1368 $rfc822_header->cc = $rfc822_header->parseAddress($send_to_cc,true,array(), '',$domain, array(&$abook,'lookup'));
1369 $rfc822_header->bcc = $rfc822_header->parseAddress($send_to_bcc,true, array(), '',$domain, array(&$abook,'lookup'));
a43e4b90 1370 $rfc822_header->priority = $mailprio;
1371 $rfc822_header->subject = $subject;
310dfeb6 1372
a43e4b90 1373 $special_encoding='';
1374 if (strtolower($default_charset) == 'iso-2022-jp') {
1375 if (mb_detect_encoding($body) == 'ASCII') {
a91189d6 1376 $special_encoding = '8bit';
a43e4b90 1377 } else {
1378 $body = mb_convert_encoding($body, 'JIS');
1379 $special_encoding = '7bit';
1380 }
1381 }
1382 $composeMessage->setBody($body);
1383
1384 if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
73ad81bf 1385 $popuser = $usernamedata[1];
1386 $domain = $usernamedata[2];
1387 unset($usernamedata);
a43e4b90 1388 } else {
73ad81bf 1389 $popuser = $username;
a43e4b90 1390 }
1391 $reply_to = '';
1e2a6ff6 1392 $from_mail = $idents[$identity]['email_address'];
1393 $full_name = $idents[$identity]['full_name'];
1394 $reply_to = $idents[$identity]['reply_to'];
9ca455db 1395 if (!$from_mail) {
73ad81bf 1396 $from_mail = "$popuser@$domain";
045714fd 1397 }
1398 $rfc822_header->from = $rfc822_header->parseAddress($from_mail,true);
1399 if ($full_name) {
9783f396 1400 $from = $rfc822_header->from[0];
a91189d6 1401 if (!$from->host) $from->host = $domain;
12a0ed01 1402 $full_name_encoded = encodeHeader($full_name);
1403 if ($full_name_encoded != $full_name) {
1404 $from_addr = $full_name_encoded .' <'.$from->mailbox.'@'.$from->host.'>';
1405 } else {
1406 $from_addr = '"'.$full_name .'" <'.$from->mailbox.'@'.$from->host.'>';
1407 }
045714fd 1408 $rfc822_header->from = $rfc822_header->parseAddress($from_addr,true);
a43e4b90 1409 }
a43e4b90 1410 if ($reply_to) {
73ad81bf 1411 $rfc822_header->reply_to = $rfc822_header->parseAddress($reply_to,true);
a43e4b90 1412 }
1413 /* Receipt: On Read */
1414 if (isset($request_mdn) && $request_mdn) {
73ad81bf 1415 $rfc822_header->dnt = $rfc822_header->parseAddress($from_mail,true);
a43e4b90 1416 }
1417 /* Receipt: On Delivery */
1418 if (isset($request_dr) && $request_dr) {
73ad81bf 1419 $rfc822_header->more_headers['Return-Receipt-To'] = $from_mail;
a43e4b90 1420 }
1421 /* multipart messages */
1422 if (count($composeMessage->entities)) {
1423 $message_body = new Message();
a91189d6 1424 $message_body->body_part = $composeMessage->body_part;
1425 $composeMessage->body_part = '';
1426 $mime_header = new MessageHeader;
1427 $mime_header->type0 = 'text';
1428 $mime_header->type1 = 'plain';
1429 if ($special_encoding) {
1430 $mime_header->encoding = $special_encoding;
1c044820 1431 } else {
12a0ed01 1432 $mime_header->encoding = '8bit';
a91189d6 1433 }
1434 if ($default_charset) {
1435 $mime_header->parameters['charset'] = $default_charset;
1436 }
1c044820 1437 $message_body->mime_header = $mime_header;
a43e4b90 1438 array_unshift($composeMessage->entities, $message_body);
a91189d6 1439 $content_type = new ContentType('multipart/mixed');
a43e4b90 1440 } else {
1e2026df 1441 $content_type = new ContentType('text/plain');
1442 if ($special_encoding) {
1443 $rfc822_header->encoding = $special_encoding;
1c044820 1444 } else {
1e2026df 1445 $rfc822_header->encoding = '8bit';
1c044820 1446 }
426e0b72 1447 if ($default_charset) {
1448 $content_type->properties['charset']=$default_charset;
73ad81bf 1449 }
181538ac 1450 }
1c044820 1451
a43e4b90 1452 $rfc822_header->content_type = $content_type;
1453 $composeMessage->rfc822_header = $rfc822_header;
181538ac 1454
1c044820 1455 /* Here you can modify the message structure just before we hand
5618924b 1456 it over to deliver */
5255585d 1457 $hookReturn = do_hook('compose_send', $composeMessage);
1458 /* Get any changes made by plugins to $composeMessage. */
1459 if ( is_object($hookReturn[1]) ) {
1460 $composeMessage = $hookReturn[1];
1461 }
a43e4b90 1462
b48d3c53 1463 if (!$useSendmail && !$draft) {
a91189d6 1464 require_once(SM_PATH . 'class/deliver/Deliver_SMTP.class.php');
1465 $deliver = new Deliver_SMTP();
1466 global $smtpServerAddress, $smtpPort, $pop_before_smtp, $smtp_auth_mech;
1467
a91189d6 1468 $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ? true : false;
9bd3b1e6 1469 get_smtp_user($user, $pass);
a91189d6 1470 $stream = $deliver->initStream($composeMessage,$domain,0,
73ad81bf 1471 $smtpServerAddress, $smtpPort, $user, $pass, $authPop);
b48d3c53 1472 } elseif (!$draft) {
73ad81bf 1473 require_once(SM_PATH . 'class/deliver/Deliver_SendMail.class.php');
1474 global $sendmail_path;
1475 $deliver = new Deliver_SendMail();
1476 $stream = $deliver->initStream($composeMessage,$sendmail_path);
b48d3c53 1477 } elseif ($draft) {
73ad81bf 1478 global $draft_folder;
1479 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
1480 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
1481 $imapPort, 0);
1482 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
1483 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
1484 $imap_deliver = new Deliver_IMAP();
1485 $length = $imap_deliver->mail($composeMessage);
1486 sqimap_append ($imap_stream, $draft_folder, $length);
1487 $imap_deliver->mail($composeMessage, $imap_stream);
1488 sqimap_append_done ($imap_stream, $draft_folder);
1489 sqimap_logout($imap_stream);
1490 unset ($imap_deliver);
1491 return $length;
4dfb9db7 1492 } else {
73ad81bf 1493 $msg = '<br />'.sprintf(_("Error: Draft folder %s does not exist."), $draft_folder);
1494 plain_error_message($msg, $color);
1495 return false;
a91189d6 1496 }
a43e4b90 1497 }
1498 $succes = false;
1499 if ($stream) {
a91189d6 1500 $length = $deliver->mail($composeMessage, $stream);
1501 $succes = $deliver->finalizeStream($stream);
a43e4b90 1502 }
1503 if (!$succes) {
46f2284f 1504 $msg = $deliver->dlv_msg . '<br />' .
73ad81bf 1505 _("Server replied: ") . $deliver->dlv_ret_nr . ' '.
1506 $deliver->dlv_server_msg;
a43e4b90 1507 plain_error_message($msg, $color);
1508 } else {
1509 unset ($deliver);
20152d80 1510 $move_to_sent = getPref($data_dir,$username,'move_to_sent');
1511 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
e4a1f097 1512
1513 /* Move to sent code */
1514 if (isset($default_move_to_sent) && ($default_move_to_sent != 0)) {
1515 $svr_allow_sent = true;
1516 } else {
1517 $svr_allow_sent = false;
1518 }
1519
1c044820 1520 if (isset($sent_folder) && (($sent_folder != '') || ($sent_folder != 'none'))
73ad81bf 1521 && sqimap_mailbox_exists( $imap_stream, $sent_folder)) {
e4a1f097 1522 $fld_sent = true;
1523 } else {
1524 $fld_sent = false;
1525 }
1526
1527 if ((isset($move_to_sent) && ($move_to_sent != 0)) || (!isset($move_to_sent))) {
1528 $lcl_allow_sent = true;
1529 } else {
1530 $lcl_allow_sent = false;
1531 }
1532
1533 if (($fld_sent && $svr_allow_sent && !$lcl_allow_sent) || ($fld_sent && $lcl_allow_sent)) {
eceefdfe 1534 global $passed_id, $mailbox, $action;
1535 if ($action == 'reply' || $action == 'reply_all') {
1536 $save_reply_with_orig=getPref($data_dir,$username,'save_reply_with_orig');
1537 if ($save_reply_with_orig) {
1538 $sent_folder = $mailbox;
1539 }
1540 }
e4a1f097 1541 sqimap_append ($imap_stream, $sent_folder, $length);
a91189d6 1542 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
1543 $imap_deliver = new Deliver_IMAP();
1544 $imap_deliver->mail($composeMessage, $imap_stream);
e4a1f097 1545 sqimap_append_done ($imap_stream, $sent_folder);
a91189d6 1546 unset ($imap_deliver);
1547 }
1548 global $passed_id, $mailbox, $action;
1549 ClearAttachments($composeMessage);
1550 if ($action == 'reply' || $action == 'reply_all') {
1551 sqimap_mailbox_select ($imap_stream, $mailbox);
fbdc7315 1552 sqimap_messages_flag ($imap_stream, $passed_id, $passed_id, 'Answered', false);
a91189d6 1553 }
73ad81bf 1554 sqimap_logout($imap_stream);
a43e4b90 1555 }
1556 return $succes;
1557}
1558
0fa24cb6 1559// vim: et ts=4
73ad81bf 1560?>