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