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