Added subject retrieval when message initiated from page other than read_body.php
[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');
91f2085b 33
0b97a708 34/* --------------------- Get globals ------------------------------------- */
953fa718 35/** COOKIE VARS */
36sqgetGlobalVar('key', $key, SQ_COOKIE);
0b97a708 37
953fa718 38/** SESSION VARS */
39sqgetGlobalVar('username', $username, SQ_SESSION);
40sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
41sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
42sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
43
44sqgetGlobalVar('composesession', $composesession, SQ_SESSION);
45sqgetGlobalVar('compose_messages', $compose_messages, SQ_SESSION);
46
47/** SESSION/POST/GET VARS */
b455793d 48sqgetGlobalVar('action',$action);
49sqgetGlobalVar('session',$session);
50sqgetGlobalVar('mailbox',$mailbox);
51sqgetGlobalVar('identity',$identity);
52sqgetGlobalVar('send_to',$send_to);
53sqgetGlobalVar('send_to_cc',$send_to_cc);
54sqgetGlobalVar('send_to_bcc',$send_to_bcc);
55sqgetGlobalVar('subject',$subject);
56sqgetGlobalVar('body',$body);
57sqgetGlobalVar('mailprio',$mailprio);
58sqgetGlobalVar('request_mdn',$request_mdn);
59sqgetGlobalVar('request_dr',$request_dr);
60sqgetGlobalVar('html_addr_search',$html_addr_search);
61sqgetGlobalVar('mail_sent',$mail_sent);
62sqgetGlobalVar('passed_id',$passed_id);
63sqgetGlobalVar('passed_ent_id',$passed_ent_id);
64sqgetGlobalVar('send',$send);
0b97a708 65
b455793d 66sqgetGlobalVar('attach',$attach);
12a0ed01 67
b455793d 68sqgetGlobalVar('draft',$draft);
69sqgetGlobalVar('draft_id',$draft_id);
70sqgetGlobalVar('ent_num',$ent_num);
71sqgetGlobalVar('saved_draft',$saved_draft);
72sqgetGlobalVar('delete_draft',$delete_draft);
0b97a708 73
953fa718 74
75/** POST VARS */
76sqgetGlobalVar('sigappend', $sigappend, SQ_POST);
77sqgetGlobalVar('from_htmladdr_search', $from_htmladdr_search, SQ_POST);
78sqgetGlobalVar('addr_search_done', $html_addr_search_done, SQ_POST);
79sqgetGlobalVar('send_to_search', $send_to_search, SQ_POST);
80sqgetGlobalVar('do_delete', $do_delete, SQ_POST);
81sqgetGlobalVar('delete', $delete, SQ_POST);
b0314f04 82sqgetGlobalVar('restoremessages', $restoremessages, SQ_POST);
953fa718 83if ( sqgetGlobalVar('return', $temp, SQ_POST) ) {
84 $html_addr_search_done = 'Use Addresses';
85}
86
87/** GET VARS */
88sqgetGlobalVar('attachedmessages', $attachedmessages, SQ_GET);
0b97a708 89
09044055 90/* --------------------- Specific Functions ------------------------------ */
0b97a708 91
41b94d65 92function replyAllString($header) {
93 global $include_self_reply_all, $username, $data_dir;
98e47335 94 $excl_ar = array();
41b94d65 95 /**
96 * 1) Remove the addresses we'll be sending the message 'to'
97 */
98 $url_replytoall_avoid_addrs = '';
99 if (isset($header->replyto)) {
100 $excl_ar = $header->getAddr_a('replyto');
101 }
102 /**
103 * 2) Remove our identities from the CC list (they still can be in the
104 * TO list) only if $include_self_reply_all is turned off
105 */
106 if (!$include_self_reply_all) {
2464e20d 107 $email_address = strtolower(trim(getPref($data_dir, $username, 'email_address')));
41b94d65 108 $excl_ar[$email_address] = '';
41b94d65 109 $idents = getPref($data_dir, $username, 'identities');
110 if ($idents != '' && $idents > 1) {
a91189d6 111 $first_id = false;
41b94d65 112 for ($i = 1; $i < $idents; $i ++) {
113 $cur_email_address = getPref($data_dir, $username,
114 'email_address' . $i);
2464e20d 115 $cur_email_address = strtolower(trim($cur_email_address));
fd54bb4e 116 $excl_ar[$cur_email_address] = '';
41b94d65 117 }
118 }
119 }
120
121 /**
122 * 3) get the addresses.
123 */
124 $url_replytoall_ar = $header->getAddr_a(array('to','cc'), $excl_ar);
125
126 /**
127 * 4) generate the string.
128 */
129 $url_replytoallcc = '';
130 foreach( $url_replytoall_ar as $email => $personal) {
131 if ($personal) {
fd54bb4e 132 $url_replytoallcc .= ", \"$personal\" <$email>";
41b94d65 133 } else {
fd54bb4e 134 $url_replytoallcc .= ', '. $email;
41b94d65 135 }
136 }
137 $url_replytoallcc = substr($url_replytoallcc,2);
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':
166 $start = $reply_citation_start .
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")),
182 _("From") => strlen(_("From")),
183 _("Date") => strlen(_("Date")),
184 _("To") => strlen(_("To")),
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) .
a91189d6 198 "\n\n". $display[_("Subject")] . $subject . "\n" .
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);
204 $cc = str_replace('&nbsp;',' ',$cc);
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/*
214 * If the session is expired during a post this restores the compose session
215 * vars.
216 */
5da08ef7 217if (sqsession_is_registered('session_expired_post')) {
953fa718 218 sqgetGlobalVar('session_expired_post', $session_expired_post, SQ_SESSION);
40934000 219 /*
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');
da95c4b6 264 $session = "$composesession" +1;
91f2085b 265 $composesession = $session;
a43e4b90 266 sqsession_register($composesession,'composesession');
d7f8e6e6 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;
5628fdde 278 sqsession_register($compose_messages,'compose_messages');
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)) {
7058a2a9 302 Header("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') {
da95c4b6 308 Header("Location: compose.php?saved_draft=yes&session=$composesession");
a61878d0 309 exit();
9c3e6cd4 310 }
311 else {
a61878d0 312 Header("Location: right_main.php?mailbox=$draft_folder&sort=$sort".
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";
00793a25 363 }
364 }
365 $body = $newBody;
e02775fe 366 do_hook('compose_send');
a43e4b90 367 $composeMessage=$compose_messages[$session];
d5181a1d 368
a91189d6 369 $Result = deliverMessage($composeMessage);
00793a25 370 if (! $Result) {
da95c4b6 371 showInputForm($session);
00793a25 372 exit();
373 }
0ec1a14b 374 unset($compose_messages[$session]);
00793a25 375 if ( isset($delete_draft)) {
7058a2a9 376 Header("Location: delete_message.php?mailbox=" . urlencode( $draft_folder ).
fae72101 377 "&message=$delete_draft&sort=$sort&startMessage=1&mail_sent=yes");
00793a25 378 exit();
379 }
9c3e6cd4 380 if ($compose_new_win == '1') {
0ec1a14b 381
d7f8e6e6 382 Header("Location: compose.php?mail_sent=yes");
9c3e6cd4 383 }
384 else {
fae72101 385 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort".
386 "&startMessage=1");
9c3e6cd4 387 }
734f4ee6 388 } else {
9c3e6cd4 389 if ($compose_new_win == '1') {
390 compose_Header($color, $mailbox);
391 }
392 else {
393 displayPageHeader($color, $mailbox);
394 }
00793a25 395 if (isset($AttachFailure)) {
396 plain_error_message(_("Could not move/copy file. File not attached"),
397 $color);
398 }
00793a25 399 checkInput(true);
da95c4b6 400 showInputForm($session);
00793a25 401 /* sqimap_logout($imapConnection); */
402 }
e02775fe 403} elseif (isset($html_addr_search_done)) {
9c3e6cd4 404 if ($compose_new_win == '1') {
405 compose_Header($color, $mailbox);
406 }
407 else {
408 displayPageHeader($color, $mailbox);
409 }
00793a25 410
411 if (isset($send_to_search) && is_array($send_to_search)) {
412 foreach ($send_to_search as $k => $v) {
413 if (substr($k, 0, 1) == 'T') {
414 if ($send_to) {
415 $send_to .= ', ';
416 }
417 $send_to .= $v;
418 }
419 elseif (substr($k, 0, 1) == 'C') {
420 if ($send_to_cc) {
421 $send_to_cc .= ', ';
422 }
423 $send_to_cc .= $v;
424 }
425 elseif (substr($k, 0, 1) == 'B') {
426 if ($send_to_bcc) {
427 $send_to_bcc .= ', ';
428 }
429 $send_to_bcc .= $v;
430 }
431 }
432 }
da95c4b6 433 showInputForm($session);
e02775fe 434} elseif (isset($html_addr_search)) {
0b97a708 435 if (isset($_FILES['attachfile']) &&
436 $_FILES['attachfile']['tmp_name'] &&
437 $_FILES['attachfile']['tmp_name'] != 'none') {
438 if(saveAttachedFiles($session)) {
00793a25 439 plain_error_message(_("Could not move/copy file. File not attached"), $color);
440 }
441 }
442 /*
443 * I am using an include so as to elminiate an extra unnecessary
444 * click. If you can think of a better way, please implement it.
445 */
446 include_once('./addrbook_search_html.php');
e02775fe 447} elseif (isset($attach)) {
da95c4b6 448 if (saveAttachedFiles($session)) {
00793a25 449 plain_error_message(_("Could not move/copy file. File not attached"), $color);
450 }
9c3e6cd4 451 if ($compose_new_win == '1') {
452 compose_Header($color, $mailbox);
453 }
454 else {
455 displayPageHeader($color, $mailbox);
456 }
da95c4b6 457 showInputForm($session);
01265fba 458}
459elseif (isset($sigappend)) {
460 $idents = getPref($data_dir, $username, 'identities', 0);
461 if ($idents > 1) {
462 if ($identity == 'default') {
463 $no = 'g';
464 } else {
465 $no = $identity;
466 }
467 $signature = getSig($data_dir, $username, $no);
468 }
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
503 * (they had the same code) --> if (isset($smtpErrors))
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)) {
518 $passed_id = '';
519 }
520 if (!isset($mailbox)) {
521 $mailbox = '';
522 }
523 if (!isset($action)) {
524 $action = '';
525 }
41b94d65 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
549
48985d59 550/* This function is used when not sending or adding attachments */
44560457 551function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $session='') {
91f2085b 552 global $editor_size, $default_use_priority, $body,
44560457 553 $use_signature, $composesession, $data_dir, $username,
a43e4b90 554 $username, $key, $imapServerAddress, $imapPort, $compose_messages,
a91189d6 555 $composeMessage;
e7f1a81d 556
91f2085b 557 $send_to = $send_to_cc = $send_to_bcc = $subject = $identity = '';
bdb92db3 558 $mailprio = 3;
44560457 559
41b94d65 560 if ($passed_id) {
44560457 561 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
a61878d0 562 $imapPort, 0);
563
48985d59 564 sqimap_mailbox_select($imapConnection, $mailbox);
41b94d65 565 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
a91189d6 566
a61878d0 567 $body = '';
568 if ($passed_ent_id) {
569 /* redefine the messsage in case of message/rfc822 */
570 $message = $message->getEntity($passed_ent_id);
571 /* message is an entity which contains the envelope and type0=message
572 * and type1=rfc822. The actual entities are childs from
573 * $message->entities[0]. That's where the encoding and is located
574 */
575
576 $entities = $message->entities[0]->findDisplayEntity
577 (array(), $alt_order = array('text/plain'));
578 if (!count($entities)) {
579 $entities = $message->entities[0]->findDisplayEntity
580 (array(), $alt_order = array('text/plain','html/plain'));
581 }
582 $orig_header = $message->rfc822_header; /* here is the envelope located */
583 /* redefine the message for picking up the attachments */
584 $message = $message->entities[0];
585
586 } else {
587 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain'));
588 if (!count($entities)) {
589 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain','html/plain'));
590 }
591 $orig_header = $message->rfc822_header;
592 }
a91189d6 593
41b94d65 594 $encoding = $message->header->encoding;
a61878d0 595 $type0 = $message->type0;
596 $type1 = $message->type1;
41b94d65 597 foreach ($entities as $ent) {
a61878d0 598 $unencoded_bodypart = mime_fetch_body($imapConnection, $passed_id, $ent);
599 $body_part_entity = $message->getEntity($ent);
600 $bodypart = decodeBody($unencoded_bodypart,
601 $body_part_entity->header->encoding);
602 if ($type1 == 'html') {
12a0ed01 603 $bodypart = str_replace(array('&nbsp;','&gt','&lt'),array(' ','<','>'),$bodypart);
a61878d0 604 $bodypart = strip_tags($bodypart);
605 }
606 $body .= $bodypart;
607 }
608 if ($default_use_priority) {
609 $mailprio = substr($orig_header->priority,0,1);
610 if (!$mailprio) {
611 $mailprio = 3;
612 }
613 } else {
614 $mailprio = '';
615 }
a43e4b90 616 //ClearAttachments($session);
bdb92db3 617
618 $identity = '';
619 $idents = getPref($data_dir, $username, 'identities');
a45887d7 620 $from_o = $orig_header->from;
bdb92db3 621 if (is_object($from_o)) {
622 $orig_from = $from_o->getAddress();
623 } else {
624 $orig_from = '';
a61878d0 625 }
a91189d6 626 $identities = array();
bdb92db3 627 if (!empty($idents) && $idents > 1) {
fd54bb4e 628 $identities[] = '"'. getPref($data_dir, $username, 'full_name')
a91189d6 629 . '" <' . getPref($data_dir, $username, 'email_address') . '>';
630 for ($i = 1; $i < $idents; $i++) {
fd54bb4e 631 $enc_from_name = '"'.
a91189d6 632 getPref($data_dir, $username, 'full_name' . $i) .
633 '" <' .
634 getPref($data_dir, $username, 'email_address' . $i) . '>';
fd54bb4e 635 if ($enc_from_name == $orig_from && $i) {
a61878d0 636 $identity = $i;
637 break;
638 }
a91189d6 639 $identities[] = $enc_from_name;
640 }
641 $identity_match = $orig_header->findAddress($identities);
642 if ($identity_match) {
643 $identity = $identity_match;
a61878d0 644 }
bdb92db3 645 }
a61878d0 646
647 switch ($action) {
648 case ('draft'):
649 $use_signature = FALSE;
12a0ed01 650 $send_to = decodeHeader($orig_header->getAddr_s('to'),false,true);
651 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'),false,true);
652 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'),false,true);
653 $subject = decodeHeader($orig_header->subject,false,true);
a61878d0 654 $body_ary = explode("\n", $body);
655 $cnt = count($body_ary) ;
656 $body = '';
657 for ($i=0; $i < $cnt; $i++) {
658 if (!ereg("^[>\\s]*$", $body_ary[$i]) || !$body_ary[$i]) {
659 sqWordWrap($body_ary[$i], $editor_size );
660 $body .= $body_ary[$i] . "\n";
661 }
662 unset($body_ary[$i]);
663 }
664 sqUnWordWrap($body);
a43e4b90 665 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
a61878d0 666 break;
a45887d7 667 case ('edit_as_new'):
12a0ed01 668 $send_to = decodeHeader($orig_header->getAddr_s('to'),false,true);
669 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'),false,true);
670 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'),false,true);
671 $subject = decodeHeader($orig_header->subject,false,true);
a61878d0 672 $mailprio = $orig_header->priority;
673 $orig_from = '';
a43e4b90 674 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
a61878d0 675 sqUnWordWrap($body);
676 break;
677 case ('forward'):
678 $send_to = '';
12a0ed01 679 $subject = decodeHeader($orig_header->subject,false,true);
480b84a7 680 if ((substr(strtolower($subject), 0, 4) != 'fwd:') &&
a61878d0 681 (substr(strtolower($subject), 0, 5) != '[fwd:') &&
682 (substr(strtolower($subject), 0, 6) != '[ fwd:')) {
683 $subject = '[Fwd: ' . $subject . ']';
684 }
685 $body = getforwardHeader($orig_header) . $body;
686 sqUnWordWrap($body);
a43e4b90 687 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
a91189d6 688 $body = "\n" . $body;
a61878d0 689 break;
690 case ('forward_as_attachment'):
a43e4b90 691 $composeMessage = getMessage_RFC822_Attachment($message, $composeMessage, $passed_id, $passed_ent_id, $imapConnection);
a61878d0 692 $body = '';
693 break;
a45887d7 694 case ('reply_all'):
a61878d0 695 $send_to_cc = replyAllString($orig_header);
12a0ed01 696 $send_to_cc = decodeHeader($send_to_cc,false,true);
a61878d0 697 case ('reply'):
698 $send_to = $orig_header->reply_to;
f55207e3 699 if (is_array($send_to) && count($send_to)) {
a91189d6 700 $send_to = $orig_header->getAddr_s('reply_to');
f55207e3 701 } else if (is_object($send_to)) { /* unnessecarry, just for falesafe purpose */
a91189d6 702 $send_to = $orig_header->getAddr_s('reply_to');
703 } else {
704 $send_to = $orig_header->getAddr_s('from');
a61878d0 705 }
12a0ed01 706 $send_to = decodeHeader($send_to,false,true);
707 $subject = decodeHeader($orig_header->subject,false,true);
a61878d0 708 $subject = str_replace('"', "'", $subject);
709 $subject = trim($subject);
710 if (substr(strtolower($subject), 0, 3) != 're:') {
711 $subject = 'Re: ' . $subject;
712 }
713 /* this corrects some wrapping/quoting problems on replies */
714 $rewrap_body = explode("\n", $body);
12a0ed01 715 $from = (is_array($orig_header->from)) ? $orig_header->from[0] : $orig_header->from;
6339f68f 716 sqUnWordWrap($body);
12a0ed01 717 $body = '';
a61878d0 718 $cnt = count($rewrap_body);
719 for ($i=0;$i<$cnt;$i++) {
cf7a1725 720 sqWordWrap($rewrap_body[$i], ($editor_size));
a61878d0 721 if (preg_match("/^(>+)/", $rewrap_body[$i], $matches)) {
722 $gt = $matches[1];
cf7a1725 723 $body .= '>' . str_replace("\n", "\n>$gt ", rtrim($rewrap_body[$i])) ."\n";
a61878d0 724 } else {
cf7a1725 725 $body .= '> ' . str_replace("\n", "\n> ", rtrim($rewrap_body[$i])) . "\n";
a61878d0 726 }
727 unset($rewrap_body[$i]);
728 }
12a0ed01 729 $body = getReplyCitation($from) . $body;
a43e4b90 730 $composeMessage->reply_rfc822_header = $orig_header;
12a0ed01 731
a61878d0 732 break;
12a0ed01 733 default:
a61878d0 734 break;
41b94d65 735 }
a91189d6 736 $compose_messages[$session] = $composeMessage;
737 sqsession_register($compose_messages, 'compose_messages');
5da08ef7 738 session_write_close();
a61878d0 739 sqimap_logout($imapConnection);
41b94d65 740 }
a61878d0 741 $ret = array( 'send_to' => $send_to,
742 'send_to_cc' => $send_to_cc,
743 'send_to_bcc' => $send_to_bcc,
744 'subject' => $subject,
745 'mailprio' => $mailprio,
746 'body' => $body,
747 'identity' => $identity );
748
41b94d65 749 return ($ret);
48985d59 750} /* function newMail() */
751
a43e4b90 752function getAttachments($message, &$composeMessage, $passed_id, $entities, $imapConnection) {
59edcad6 753 global $attachment_dir, $username, $data_dir, $squirrelmail_language;
48985d59 754 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
41b94d65 755 if (!count($message->entities) ||
756 ($message->type0 == 'message' && $message->type1 == 'rfc822')) {
757 if ( !in_array($message->entity_id, $entities) && $message->entity_id) {
a91189d6 758 switch ($message->type0) {
759 case 'message':
760 if ($message->type1 == 'rfc822') {
761 $filename = $message->rfc822_header->subject.'.eml';
a43e4b90 762 if ($filename == "") {
763 $filename = "untitled-".$message->entity_id.'.eml';
764 }
a91189d6 765 } else {
766 $filename = $message->getFilename();
a43e4b90 767 }
a91189d6 768 break;
769 default:
770 $filename = $message->getFilename();
771 break;
772 }
773 $filename = decodeHeader($filename);
a43e4b90 774 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
a91189d6 775 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
a43e4b90 776 $filename = $languages[$squirrelmail_language]['XTRA_CODE']('encode', $filename);
777 }
a43e4b90 778 $localfilename = GenerateRandomString(32, '', 7);
779 $full_localfilename = "$hashed_attachment_dir/$localfilename";
780 while (file_exists($full_localfilename)) {
781 $localfilename = GenerateRandomString(32, '', 7);
782 $full_localfilename = "$hashed_attachment_dir/$localfilename";
783 }
a91189d6 784 $message->att_local_name = $full_localfilename;
785 if (!$message->mime_header) { /* temporary hack */
786 $message->mime_header = $message->header;
787 }
788
789 $composeMessage->addEntity($message);
790
a43e4b90 791 /* Write Attachment to file */
792 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'wb');
793 fputs($fp, decodeBody(mime_fetch_body($imapConnection,
794 $passed_id, $message->entity_id),
795 $message->header->encoding));
796 fclose ($fp);
48985d59 797 }
734f4ee6 798 } else {
a43e4b90 799 for ($i=0, $entCount=count($message->entities); $i<$entCount;$i++) {
800 $composeMessage=getAttachments($message->entities[$i], $composeMessage, $passed_id, $entities, $imapConnection);
48985d59 801 }
802 }
a43e4b90 803 return $composeMessage;
48985d59 804}
805
a43e4b90 806function getMessage_RFC822_Attachment($message, $composeMessage, $passed_id,
756406df 807 $passed_ent_id='', $imapConnection) {
a6ec592e 808 global $attachments, $attachment_dir, $username, $data_dir, $uid_support;
809 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
756406df 810 if (!$passed_ent_id) {
a61878d0 811 $body_a = sqimap_run_command($imapConnection,
812 'FETCH '.$passed_id.' RFC822',
813 TRUE, $response, $readmessage,
814 $uid_support);
756406df 815 } else {
816 $body_a = sqimap_run_command($imapConnection,
a61878d0 817 'FETCH '.$passed_id.' BODY['.$passed_ent_id.']',
818 TRUE, $response, $readmessage, $uid_support);
819 $message = $message->parent;
756406df 820 }
a6ec592e 821 if ($response = 'OK') {
a61878d0 822 $subject = encodeHeader($message->rfc822_header->subject);
823 array_shift($body_a);
824 $body = implode('', $body_a) . "\r\n";
825
826 $localfilename = GenerateRandomString(32, 'FILE', 7);
827 $full_localfilename = "$hashed_attachment_dir/$localfilename";
828
a43e4b90 829 $fp = fopen( $full_localfilename, 'w');
a61878d0 830 fwrite ($fp, $body);
831 fclose($fp);
a91189d6 832 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
833 $full_localfilename);
a43e4b90 834 }
835 return $composeMessage;
a6ec592e 836}
837
41b94d65 838function showInputForm ($session, $values=false) {
a94c1db1 839 global $send_to, $send_to_cc, $body,
48985d59 840 $passed_body, $color, $use_signature, $signature, $prefix_sig,
841 $editor_size, $attachments, $subject, $newmail,
41b94d65 842 $use_javascript_addr_book, $send_to_bcc, $passed_id, $mailbox,
48985d59 843 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
844 $username, $data_dir, $identity, $draft_id, $delete_draft,
9c3e6cd4 845 $mailprio, $default_use_mdn, $mdn_user_support, $compose_new_win,
44560457 846 $saved_draft, $mail_sent, $sig_first, $edit_as_new, $action,
ab4700c3 847 $username, $compose_messages, $composesession, $default_charset;
a43e4b90 848
849 $composeMessage = $compose_messages[$session];
48985d59 850
41b94d65 851 if ($values) {
852 $send_to = $values['send_to'];
853 $send_to_cc = $values['send_to_cc'];
854 $send_to_bcc = $values['send_to_bcc'];
855 $subject = $values['subject'];
856 $mailprio = $values['mailprio'];
857 $body = $values['body'];
d3c13a51 858 $identity = (int) $values['identity'];
676bb189 859 } else {
860 $send_to = decodeHeader($send_to);
861 $send_to_cc = decodeHeader($send_to_cc);
862 $send_to_bcc = decodeHeader($send_to_bcc);
41b94d65 863 }
864
48985d59 865 if ($use_javascript_addr_book) {
866 echo "\n". '<SCRIPT LANGUAGE=JavaScript><!--' . "\n" .
867 'function open_abook() { ' . "\n" .
868 ' var nwin = window.open("addrbook_popup.php","abookpopup",' .
869 '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" .
870 ' if((!nwin.opener) && (document.windows != null))' . "\n" .
871 ' nwin.opener = document.windows;' . "\n" .
872 "}\n" .
873 '// --></SCRIPT>' . "\n\n";
874 }
875
41b94d65 876 echo "\n" . '<FORM name=compose action="compose.php" METHOD=POST ' .
877 'ENCTYPE="multipart/form-data"';
48985d59 878 do_hook("compose_form");
57257333 879
48985d59 880 echo ">\n";
881
41b94d65 882 if ($action == 'draft') {
883 echo '<input type="hidden" name="delete_draft" value="' . $passed_id . "\">\n";
48985d59 884 }
885 if (isset($delete_draft)) {
886 echo '<input type="hidden" name="delete_draft" value="' . $delete_draft. "\">\n";
887 }
da95c4b6 888 if (isset($session)) {
44560457 889 echo '<input type="hidden" name="session" value="' . $session . "\">\n";
da95c4b6 890 }
08bad2b1 891
892 if (isset($passed_id)) {
893 echo '<input type="hidden" name="passed_id" value="' . $passed_id . "\">\n";
894 }
44560457 895
9c3e6cd4 896 if ($saved_draft == 'yes') {
897 echo '<BR><CENTER><B>'. _("Draft Saved").'</CENTER></B>';
898 }
899 if ($mail_sent == 'yes') {
900 echo '<BR><CENTER><B>'. _("Your Message has been sent").'</CENTER></B>';
901 }
a94c1db1 902 echo '<TABLE ALIGN=center CELLSPACING=0 BORDER=0>' . "\n";
9c3e6cd4 903 if ($compose_new_win == '1') {
a94c1db1 904 echo '<TABLE ALIGN=CENTER BGCOLOR="'.$color[0].'" WIDTH="100%" BORDER=0>'."\n" .
98fb28fd 905 ' <TR><TD></TD>'. html_tag( 'td', '', 'right' ) . '<INPUT TYPE="BUTTON" NAME="Close" onClick="return self.close()" VALUE='._("Close").'></TD></TR>'."\n";
9c3e6cd4 906 }
78a35fcd 907 if ($location_of_buttons == 'top') {
908 showComposeButtonRow();
909 }
48985d59 910
715225af 911 $idents = getPref($data_dir, $username, 'identities', 0);
912 if ($idents > 1) {
a94c1db1 913 echo ' <TR>' . "\n" .
98fb28fd 914 html_tag( 'td', '', 'right', $color[4], 'WIDTH="10%"' ) .
0ec1a14b 915 _("From:") . '</TD>' . "\n" .
98fb28fd 916 html_tag( 'td', '', 'left', $color[4], 'WIDTH="90%"' ) .
0ec1a14b 917 ' <select name=identity>' . "\n" .
918 ' <option value=default>' .
919 htmlspecialchars(getPref($data_dir, $username, 'full_name'));
48985d59 920 $em = getPref($data_dir, $username, 'email_address');
921 if ($em != '') {
248bfebb 922 echo htmlspecialchars(' <' . $em . '>') . "\n";
48985d59 923 }
924 for ($i = 1; $i < $idents; $i ++) {
248bfebb 925 echo '<option value="' . $i . '"';
48985d59 926 if (isset($identity) && $identity == $i) {
78a35fcd 927 echo ' SELECTED';
48985d59 928 }
929 echo '>' . htmlspecialchars(getPref($data_dir, $username,
930 'full_name' . $i));
248bfebb 931 $em = getPref($data_dir, $username, 'email_address' . $i);
48985d59 932 if ($em != '') {
78a35fcd 933 echo htmlspecialchars(' <' . $em . '>') . "\n";
48985d59 934 }
9f599fe3 935 echo '</option>';
48985d59 936 }
937 echo '</select>' . "\n" .
41b94d65 938 ' </TD>' . "\n" .
939 ' </TR>' . "\n";
940 }
a94c1db1 941 echo ' <TR>' . "\n" .
98fb28fd 942 html_tag( 'td', '', 'right', $color[4], 'WIDTH="10%"' ) .
0ec1a14b 943 _("To:") . '</TD>' . "\n" .
98fb28fd 944 html_tag( 'td', '', 'left', $color[4], 'WIDTH="90%"' ) .
41b94d65 945 ' <INPUT TYPE=text NAME="send_to" VALUE="' .
12a0ed01 946 $send_to . '" SIZE=60><BR>' . "\n" .
41b94d65 947 ' </TD>' . "\n" .
948 ' </TR>' . "\n" .
a94c1db1 949 ' <TR>' . "\n" .
98fb28fd 950 html_tag( 'td', '', 'right', $color[4] ) .
0ec1a14b 951 _("CC:") . '</TD>' . "\n" .
98fb28fd 952 html_tag( 'td', '', 'left', $color[4] ) .
41b94d65 953 ' <INPUT TYPE=text NAME="send_to_cc" SIZE=60 VALUE="' .
12a0ed01 954 $send_to_cc . '"><BR>' . "\n" .
41b94d65 955 ' </TD>' . "\n" .
956 ' </TR>' . "\n" .
a94c1db1 957 ' <TR>' . "\n" .
98fb28fd 958 html_tag( 'td', '', 'right', $color[4] ) .
0ec1a14b 959 _("BCC:") . '</TD>' . "\n" .
98fb28fd 960 html_tag( 'td', '', 'left', $color[4] ) .
41b94d65 961 ' <INPUT TYPE=text NAME="send_to_bcc" VALUE="' .
12a0ed01 962 $send_to_bcc . '" SIZE=60><BR>' . "\n" .
0ec1a14b 963 ' </TD>' . "\n" .
964 ' </TR>' . "\n" .
a94c1db1 965 ' <TR>' . "\n" .
98fb28fd 966 html_tag( 'td', '', 'right', $color[4] ) .
0ec1a14b 967 _("Subject:") . '</TD>' . "\n" .
98fb28fd 968 html_tag( 'td', '', 'left', $color[4] ) . "\n";
0ec1a14b 969 echo ' <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
480b84a7 970 $subject . '">' . "\n" .
0ec1a14b 971 ' </TD>' . "\n" .
972 ' </TR>' . "\n\n";
48985d59 973
78a35fcd 974 if ($location_of_buttons == 'between') {
975 showComposeButtonRow();
976 }
4dfb9db7 977
fdc83c55 978 if ($compose_new_win == '1') {
a94c1db1 979 echo ' <TR>' . "\n" .
980 ' <TD BGCOLOR="' . $color[0] . '" COLSPAN=2 ALIGN=CENTER>' . "\n" .
41b94d65 981 ' <TEXTAREA NAME=body ROWS=20 COLS="' .
0ec1a14b 982 $editor_size . '" WRAP="VIRTUAL">';
fdc83c55 983 }
984 else {
a94c1db1 985 echo ' <TR>' . "\n" .
986 ' <TD BGCOLOR="' . $color[4] . '" COLSPAN=2>' . "\n" .
41b94d65 987 ' &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS="' .
0ec1a14b 988 $editor_size . '" WRAP="VIRTUAL">';
fdc83c55 989 }
48985d59 990 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
d3c13a51 991 if ($idents > 1) {
992 if ($identity == 'default') {
993 $no = 'g';
994 } else {
995 $no = $identity;
996 }
997 $signature = getSig($data_dir, $username, $no);
998 }
999
3b17e952 1000 if ($sig_first == '1') {
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 {
a91189d6 1004 echo "\n\n".($prefix_sig==true? "-- \n":'').decodeHeader($signature,false);
83be314a 1005 }
12a0ed01 1006 echo "\n\n".decodeHeader($body,false,true);
3b17e952 1007 }
1008 else {
12a0ed01 1009 echo "\n\n".decodeHeader($body,false,true);
ab4700c3 1010 if ($default_charset == 'iso-2022-jp') {
83be314a 1011 echo "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
1012 }else{
12a0ed01 1013 echo "\n\n".($prefix_sig==true? "-- \n":'').decodeHeader($signature,false,true);
3b17e952 1014 }
1015 }
83be314a 1016 }
3b17e952 1017 else {
12a0ed01 1018 echo decodeHeader($body,false,true);
48985d59 1019 }
41b94d65 1020 echo '</TEXTAREA><BR>' . "\n" .
1021 ' </TD>' . "\n" .
1022 ' </TR>' . "\n";
48985d59 1023
12a0ed01 1024
48985d59 1025 if ($location_of_buttons == 'bottom') {
1026 showComposeButtonRow();
1027 } else {
0ec1a14b 1028 echo ' <TR>' . "\n" .
98fb28fd 1029 html_tag( 'td', '', 'right', '', 'COLSPAN=2' ) . "\n" .
0ec1a14b 1030 ' <INPUT TYPE=SUBMIT NAME=send VALUE="' . _("Send") . '">' . "\n" .
1031 ' &nbsp;&nbsp;&nbsp;&nbsp;<BR><BR>' . "\n" .
1032 ' </TD>' . "\n" .
1033 ' </TR>' . "\n";
48985d59 1034 }
46bb8da8 1035
48985d59 1036 /* This code is for attachments */
a91189d6 1037 if ((bool) ini_get('file_uploads')) {
0a2c3218 1038
1039 /* Calculate the max size for an uploaded file.
1040 * This is advisory for the user because we can't actually prevent
1041 * people to upload too large files. */
1042 $sizes = array();
1043 /* php.ini vars which influence the max for uploads */
1044 $configvars = array('post_max_size', 'memory_limit', 'upload_max_filesize');
1045 foreach($configvars as $var) {
1046 /* skip 0 or empty values */
1047 if( $size = getByteSize(ini_get($var)) ) {
1048 $sizes[] = $size;
1049 }
1050 }
1051
1052 if(count($sizes) > 0) {
1053 $maxsize = '(max.&nbsp;' . show_readable_size( min( $sizes ) ) . ')';
1054 } else {
1055 $maxsize = '';
1056 }
1057
1058 echo ' <tr>' . "\n" .
1059 ' <td colspan="2">' . "\n" .
a94c1db1 1060 ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.
0ec1a14b 1061 ' border="0" bgcolor="'.$color[9].'">' . "\n" .
0a2c3218 1062 ' <tr>' . "\n" .
1063 ' <td>' . "\n" .
a94c1db1 1064 ' <table width="100%" cellpadding="3" cellspacing="0" align="center"'.
0ec1a14b 1065 ' border="0">' . "\n" .
0a2c3218 1066 ' <tr>' . "\n" .
1067 html_tag( 'td', '', 'right', '', 'valign="middle"' ) .
1068 _("Attach:") . '</td>' . "\n" .
1069 html_tag( 'td', '', 'left', '', 'valign="middle"' ) .
1070 ' <input name="attachfile" size="48" type="file" />' . "\n" .
0ec1a14b 1071 ' &nbsp;&nbsp;<input type="submit" name="attach"' .
1072 ' value="' . _("Add") .'">' . "\n" .
0a2c3218 1073 $maxsize .
1074 ' </td>' . "\n" .
1075 ' </tr>' . "\n";
91f2085b 1076
41b94d65 1077
91f2085b 1078 $s_a = array();
4dfb9db7 1079 if ($composeMessage->entities) {
1080 foreach ($composeMessage->entities as $key => $attachment) {
a43e4b90 1081 $attached_file = $attachment->att_local_name;
a91189d6 1082 if ($attachment->att_local_name || $attachment->body_part) {
1083 $attached_filename = decodeHeader($attachment->mime_header->getParameter('name'));
1084 $type = $attachment->mime_header->type0.'/'.
1085 $attachment->mime_header->type1;
98fb28fd 1086
a91189d6 1087 $s_a[] = '<table bgcolor="'.$color[0].
1088 '" border="0"><tr><td><input type="checkbox" name="delete[]" value="' .
1089 $key . "\"></td><td>\n" . $attached_filename .
1090 '</td><td>-</td><td> ' . $type . '</td><td>('.
1091 show_readable_size( filesize( $attached_file ) ) . ')</td></tr></table>'."\n";
a43e4b90 1092 }
4dfb9db7 1093 }
91f2085b 1094 }
1095 if (count($s_a)) {
a94c1db1 1096 foreach ($s_a as $s) {
98fb28fd 1097 echo '<tr>' . html_tag( 'td', '', 'left', $color[0], 'colspan="2"' ) . $s .'</td></tr>';
a61878d0 1098 }
91f2085b 1099 echo '<tr><td colspan="2"><input type="submit" name="do_delete" value="' .
1100 _("Delete selected attachments") . "\">\n" .
1101 '</td></tr>';
1102 }
0ec1a14b 1103 echo ' </table>' . "\n" .
1104 ' </td>' . "\n" .
1105 ' </tr>' . "\n" .
1106 ' </TABLE>' . "\n" .
1107 ' </TD>' . "\n" .
1108 ' </TR>' . "\n";
a91189d6 1109 } // End of file_uploads if-block
41b94d65 1110 /* End of attachment code */
07687736 1111 if ($compose_new_win == '1') {
41b94d65 1112 echo '</TABLE>'."\n";
07687736 1113 }
a64f47e7 1114
a61878d0 1115 echo '</TABLE>' . "\n" .
1116 '<input type="hidden" name="username" value="'. $username . "\">\n" .
5da08ef7 1117 '<input type=hidden name=action value="' . $action . "\">\n" .
a61878d0 1118 '<INPUT TYPE=hidden NAME=mailbox VALUE="' . htmlspecialchars($mailbox) .
4dfb9db7 1119 "\">\n";
5da08ef7 1120 /*
0ec1a14b 1121 store the complete ComposeMessages array in a hidden input value
1122 so we can restore them in case of a session timeout.
5da08ef7 1123 */
953fa718 1124 sqgetGlobalVar('QUERY_STRING', $queryString, SQ_SERVER);
5da08ef7 1125 echo '<input type=hidden name=restoremessages value="' . urlencode(serialize($compose_messages)) . "\">\n";
1126 echo '<input type=hidden name=composesession value="' . $composesession . "\">\n";
953fa718 1127 echo '<input type=hidden name=querystring value="' . $queryString . "\">\n";
4dfb9db7 1128 echo '</FORM>';
a64f47e7 1129 if (!(bool) ini_get('file_uploads')) {
1130 /* File uploads are off, so we didn't show that part of the form.
1131 To avoid bogus bug reports, tell the user why. */
1132 echo 'Because PHP file uploads are turned off, you can not attach files ';
1133 echo "to this message. Please see your system administrator for details.\r\n";
1134 }
1135
9f599fe3 1136 do_hook('compose_bottom');
48985d59 1137 echo '</BODY></HTML>' . "\n";
1138}
1139
1140
70c4fd84 1141function showComposeButtonRow() {
78a35fcd 1142 global $use_javascript_addr_book, $save_as_draft,
a61878d0 1143 $default_use_priority, $mailprio, $default_use_mdn,
1144 $request_mdn, $request_dr,
1145 $data_dir, $username;
70c4fd84 1146
0ec1a14b 1147 echo ' <TR>' . "\n" .
1148 ' <TD></TD>' . "\n" .
1149 ' <TD>' . "\n";
ae25968c 1150 if ($default_use_priority) {
1151 if(!isset($mailprio)) {
1152 $mailprio = "3";
70c4fd84 1153 }
0ec1a14b 1154 echo ' ' . _("Priority") .': <select name="mailprio">'.
70c4fd84 1155 '<option value="1"'.($mailprio=='1'?' selected':'').'>'. _("High") .'</option>'.
1156 '<option value="3"'.($mailprio=='3'?' selected':'').'>'. _("Normal") .'</option>'.
1157 '<option value="5"'.($mailprio=='5'?' selected':'').'>'. _("Low").'</option>'.
0ec1a14b 1158 '</select>' . "\n";
ae25968c 1159 }
1160 $mdn_user_support=getPref($data_dir, $username, 'mdn_user_support',$default_use_mdn);
1161 if ($default_use_mdn) {
70c4fd84 1162 if ($mdn_user_support) {
0ec1a14b 1163 echo ' ' . _("Receipt") .': '.
b2a7e5bc 1164 '<input type="checkbox" name="request_mdn" value=1'.
a61878d0 1165 ($request_mdn=='1'?' checked':'') .'>'. _("On Read").
b2a7e5bc 1166 ' <input type="checkbox" name="request_dr" value=1'.
a61878d0 1167 ($request_dr=='1'?' checked':'') .'>'. _("On Delivery");
70c4fd84 1168 }
ae25968c 1169 }
48985d59 1170
0ec1a14b 1171 echo ' </TD>' . "\n" .
1172 ' </TR>' . "\n" .
1173 ' <TR>' . "\n" .
1174 ' <TD></TD>' . "\n" .
1175 ' <TD>' . "\n" .
1176 ' <INPUT TYPE=SUBMIT NAME="sigappend" VALUE="' . _("Signature") . '">' . "\n";
78a35fcd 1177 if ($use_javascript_addr_book) {
0ec1a14b 1178 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"".
1179 " <input type=button value=\\\""._("Addresses").
1180 "\\\" onclick='javascript:open_abook();'>\");".
1181 " // --></SCRIPT><NOSCRIPT>\n".
1182 " <input type=submit name=\"html_addr_search\" value=\"".
46bb8da8 1183 _("Addresses")."\">".
0ec1a14b 1184 " </NOSCRIPT>\n";
734f4ee6 1185 } else {
0ec1a14b 1186 echo ' <input type=submit name="html_addr_search" value="'.
1187 _("Addresses").'">' . "\n";
78a35fcd 1188 }
48985d59 1189
78a35fcd 1190 if ($save_as_draft) {
0ec1a14b 1191 echo ' <input type="submit" name ="draft" value="' . _("Save Draft") . "\">\n";
78a35fcd 1192 }
0a17f9dd 1193
0ec1a14b 1194 echo ' <INPUT TYPE=submit NAME=send VALUE="'. _("Send") . '">' . "\n";
78a35fcd 1195 do_hook('compose_button_row');
441f2d33 1196
0ec1a14b 1197 echo ' </TD>' . "\n" .
1198 ' </TR>' . "\n\n";
78a35fcd 1199}
b278172f 1200
70c4fd84 1201function checkInput ($show) {
78a35fcd 1202 /*
1203 * I implemented the $show variable because the error messages
1204 * were getting sent before the page header. So, I check once
1205 * using $show=false, and then when i'm ready to display the error
1206 * message, show=true
1207 */
6bf2a88f 1208 global $body, $send_to, $send_to_bcc, $subject, $color;
78a35fcd 1209
6bf2a88f 1210 if ($send_to == '' && $send_to_bcc == '') {
78a35fcd 1211 if ($show) {
0ad7dbda 1212 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
78a35fcd 1213 }
1214 return false;
1215 }
1216 return true;
1217} /* function checkInput() */
df15de21 1218
3806fa52 1219
00793a25 1220/* True if FAILURE */
da95c4b6 1221function saveAttachedFiles($session) {
0b97a708 1222 global $_FILES, $attachment_dir, $attachments, $username,
a43e4b90 1223 $data_dir, $compose_messages;
4c9d2242 1224
45cdd1b5 1225 /* get out of here if no file was attached at all */
1226 if (! is_uploaded_file($_FILES['attachfile']['tmp_name']) ) {
1227 return true;
1228 }
1229
4c9d2242 1230 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1231 $localfilename = GenerateRandomString(32, '', 7);
1232 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1233 while (file_exists($full_localfilename)) {
1234 $localfilename = GenerateRandomString(32, '', 7);
1235 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1236 }
1237
e6675f9a 1238 // FIXME: we SHOULD prefer move_uploaded_file over rename because
1239 // m_u_f works better with restricted PHP installes (safe_mode, open_basedir)
1240 if (!@rename($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
1241 if (!@move_uploaded_file($_FILES['attachfile']['tmp_name'],$full_localfilename)) {
a91189d6 1242 return true;
1243 }
a61878d0 1244 }
a43e4b90 1245 $message = $compose_messages[$session];
0b97a708 1246 $type = strtolower($_FILES['attachfile']['type']);
1247 $name = $_FILES['attachfile']['name'];
a43e4b90 1248 $message->initAttachment($type, $name, $full_localfilename);
1249 $compose_messages[$session] = $message;
b0314f04 1250 sqsession_register($compose_messages , 'compose_messages');
4c9d2242 1251}
1252
a43e4b90 1253function ClearAttachments($composeMessage) {
b48d3c53 1254 if ($composeMessage->att_local_name) {
1255 $attached_file = $composeMessage->att_local_name;
a43e4b90 1256 if (file_exists($attached_file)) {
1257 unlink($attached_file);
8712abea 1258 }
da95c4b6 1259 }
a43e4b90 1260 for ($i=0, $entCount=count($composeMessage->entities);$i< $entCount; ++$i) {
1261 ClearAttachments($composeMessage->entities[$i]);
1262 }
4c9d2242 1263}
1264
0a2c3218 1265/* parse values like 8M and 2k into bytes */
1266function getByteSize($ini_size) {
1267
1268 if(!$ini_size) return FALSE;
da95c4b6 1269
0a2c3218 1270 $ini_size = trim($ini_size);
1271
1272 switch(strtoupper(substr($ini_size, -1))) {
1273 case 'G':
1274 $bytesize = 1073741824;
1275 break;
1276 case 'M':
1277 $bytesize = 1048576;
1278 break;
1279 case 'K':
1280 $bytesize = 1024;
1281 break;
1282 default:
1283 $bytesize = 1;
1284 }
1285
1286 $bytesize *= (int)substr($ini_size, 0, -1);
1287
1288 return $bytesize;
1289}
a43e4b90 1290
4c9d2242 1291
a43e4b90 1292/* temporary function to make use of the deliver class.
1293 In the future the responsable backend should be automaticly loaded
1294 and conf.pl should show a list of available backends.
1295 The message also should be constructed by the message class.
1296*/
1297
b7ff469f 1298function deliverMessage($composeMessage, $draft=false) {
a43e4b90 1299 global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body,
1300 $username, $popuser, $usernamedata, $identity, $data_dir,
a91189d6 1301 $request_mdn, $request_dr, $default_charset, $color, $useSendmail,
1302 $domain, $action;
a43e4b90 1303 global $imapServerAddress, $imapPort, $sent_folder, $key;
1304
1305 $rfc822_header = $composeMessage->rfc822_header;
24192f77 1306
1307 $abook = addressbook_init(false, true);
24192f77 1308 $rfc822_header->to = $rfc822_header->parseAddress($send_to,true, array(), '', $domain, array(&$abook,'lookup'));
1309 $rfc822_header->cc = $rfc822_header->parseAddress($send_to_cc,true,array(), '',$domain, array(&$abook,'lookup'));
1310 $rfc822_header->bcc = $rfc822_header->parseAddress($send_to_bcc,true, array(), '',$domain, array(&$abook,'lookup'));
a43e4b90 1311 $rfc822_header->priority = $mailprio;
1312 $rfc822_header->subject = $subject;
1313 $special_encoding='';
1314 if (strtolower($default_charset) == 'iso-2022-jp') {
1315 if (mb_detect_encoding($body) == 'ASCII') {
a91189d6 1316 $special_encoding = '8bit';
a43e4b90 1317 } else {
1318 $body = mb_convert_encoding($body, 'JIS');
1319 $special_encoding = '7bit';
1320 }
1321 }
1322 $composeMessage->setBody($body);
1323
1324 if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
1325 $popuser = $usernamedata[1];
1326 $domain = $usernamedata[2];
1327 unset($usernamedata);
1328 } else {
1329 $popuser = $username;
1330 }
1331 $reply_to = '';
1332 if (isset($identity) && $identity != 'default') {
045714fd 1333 $from_mail = getPref($data_dir, $username,'email_address' . $identity);
1334 $full_name = getPref($data_dir, $username,'full_name' . $identity);
1335 $reply_to = getPref($data_dir, $username,'reply_to' . $identity);
a43e4b90 1336 } else {
1337 $from_mail = getPref($data_dir, $username, 'email_address');
1338 $full_name = getPref($data_dir, $username, 'full_name');
a43e4b90 1339 $reply_to = getPref($data_dir, $username,'reply_to');
1340 }
045714fd 1341 if (!$from_mail) {
1342 $from_mail = "$popuser@$domain";
1343 $full_name = '';
1344 }
1345 $rfc822_header->from = $rfc822_header->parseAddress($from_mail,true);
1346 if ($full_name) {
9783f396 1347 $from = $rfc822_header->from[0];
a91189d6 1348 if (!$from->host) $from->host = $domain;
12a0ed01 1349 $full_name_encoded = encodeHeader($full_name);
1350 if ($full_name_encoded != $full_name) {
1351 $from_addr = $full_name_encoded .' <'.$from->mailbox.'@'.$from->host.'>';
1352 } else {
1353 $from_addr = '"'.$full_name .'" <'.$from->mailbox.'@'.$from->host.'>';
1354 }
045714fd 1355 $rfc822_header->from = $rfc822_header->parseAddress($from_addr,true);
a43e4b90 1356 }
a43e4b90 1357 if ($reply_to) {
1358 $rfc822_header->reply_to = $rfc822_header->parseAddress($reply_to,true);
1359 }
1360 /* Receipt: On Read */
1361 if (isset($request_mdn) && $request_mdn) {
1362 $rfc822_header->dnt = $rfc822_header->parseAddress($from_mail,true);
1363 }
1364 /* Receipt: On Delivery */
1365 if (isset($request_dr) && $request_dr) {
1366 $rfc822_header->more_headers['Return-Receipt-To'] = $from_mail;
1367 }
1368 /* multipart messages */
1369 if (count($composeMessage->entities)) {
1370 $message_body = new Message();
a91189d6 1371 $message_body->body_part = $composeMessage->body_part;
1372 $composeMessage->body_part = '';
1373 $mime_header = new MessageHeader;
1374 $mime_header->type0 = 'text';
1375 $mime_header->type1 = 'plain';
1376 if ($special_encoding) {
1377 $mime_header->encoding = $special_encoding;
1378 } else {
12a0ed01 1379 $mime_header->encoding = '8bit';
a91189d6 1380 }
1381 if ($default_charset) {
1382 $mime_header->parameters['charset'] = $default_charset;
1383 }
1384 $message_body->mime_header = $mime_header;
a43e4b90 1385 array_unshift($composeMessage->entities, $message_body);
a91189d6 1386 $content_type = new ContentType('multipart/mixed');
a43e4b90 1387 } else {
a91189d6 1388 $content_type = new ContentType('text/plain');
a43e4b90 1389 }
1390 if ($default_charset) {
a91189d6 1391 $content_type->properties['charset']=$default_charset;
a43e4b90 1392 }
1393
1394 $rfc822_header->content_type = $content_type;
1395 $composeMessage->rfc822_header = $rfc822_header;
1396
b48d3c53 1397 if (!$useSendmail && !$draft) {
a91189d6 1398 require_once(SM_PATH . 'class/deliver/Deliver_SMTP.class.php');
1399 $deliver = new Deliver_SMTP();
1400 global $smtpServerAddress, $smtpPort, $pop_before_smtp, $smtp_auth_mech;
1401
1402 if ($smtp_auth_mech == 'none') {
1403 $user = '';
1404 $pass = '';
1405 } else {
1406 global $key, $onetimepad;
1407 $user = $username;
1408 $pass = OneTimePadDecrypt($key, $onetimepad);
1409 }
1410
1411 $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ? true : false;
1412 $stream = $deliver->initStream($composeMessage,$domain,0,
1413 $smtpServerAddress, $smtpPort, $user, $pass, $authPop);
b48d3c53 1414 } elseif (!$draft) {
86725763 1415 require_once(SM_PATH . 'class/deliver/Deliver_SendMail.class.php');
b48d3c53 1416 global $sendmail_path;
1417 $deliver = new Deliver_SendMail();
1418 $stream = $deliver->initStream($composeMessage,$sendmail_path);
1419 } elseif ($draft) {
1420 global $draft_folder;
86725763 1421 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
b48d3c53 1422 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
1423 $imapPort, 0);
1424 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
4dfb9db7 1425 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
a91189d6 1426 $imap_deliver = new Deliver_IMAP();
1427 $length = $imap_deliver->mail($composeMessage);
1428 sqimap_append ($imap_stream, $draft_folder, $length);
4dfb9db7 1429 $imap_deliver->mail($composeMessage, $imap_stream);
a91189d6 1430 sqimap_append_done ($imap_stream, $draft_folder);
1431 sqimap_logout($imap_stream);
1432 unset ($imap_deliver);
1433 return $length;
4dfb9db7 1434 } else {
a91189d6 1435 $msg = '<br>Error: '._("Draft folder")." $draft_folder" . ' does not exist.';
1436 plain_error_message($msg, $color);
1437 return false;
1438 }
a43e4b90 1439 }
1440 $succes = false;
1441 if ($stream) {
a91189d6 1442 $length = $deliver->mail($composeMessage, $stream);
1443 $succes = $deliver->finalizeStream($stream);
a43e4b90 1444 }
1445 if (!$succes) {
00ac2f42 1446 $msg = $deliver->dlv_msg . '<br>' .
1447 _("Server replied: ") . $deliver->dlv_ret_nr . ' '.
1448 $deliver->dlv_server_msg;
a43e4b90 1449 plain_error_message($msg, $color);
1450 } else {
1451 unset ($deliver);
1452 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
1453 $imapPort, 0);
a91189d6 1454 if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) {
1455 sqimap_append ($imap_stream, $sent_folder, $length);
1456 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
1457 $imap_deliver = new Deliver_IMAP();
1458 $imap_deliver->mail($composeMessage, $imap_stream);
1459 sqimap_append_done ($imap_stream, $sent_folder);
1460 unset ($imap_deliver);
1461 }
1462 global $passed_id, $mailbox, $action;
1463 ClearAttachments($composeMessage);
1464 if ($action == 'reply' || $action == 'reply_all') {
1465 sqimap_mailbox_select ($imap_stream, $mailbox);
1466 sqimap_messages_flag ($imap_stream, $passed_id, $passed_id, 'Answered', true);
1467 }
1468 sqimap_logout($imap_stream);
a43e4b90 1469 }
1470 return $succes;
1471}
1472
6bf2a88f 1473?>