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