Make the default element called 0 again in preparation of my next commit
[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 */
426e0b72 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':
181538ac 777 if ($message->type1 == 'rfc822') {
778 $filename = $message->rfc822_header->subject.'.eml';
779 if ($filename == "") {
780 $filename = "untitled-".$message->entity_id.'.eml';
781 }
782 } else {
783 $filename = $message->getFilename();
784 }
a91189d6 785 break;
786 default:
181538ac 787 if (!$message->mime_header) { /* temporary hack */
788 $message->mime_header = $message->header;
789 }
a91189d6 790 $filename = $message->getFilename();
791 break;
792 }
793 $filename = decodeHeader($filename);
a43e4b90 794 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
a91189d6 795 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
a43e4b90 796 $filename = $languages[$squirrelmail_language]['XTRA_CODE']('encode', $filename);
797 }
a43e4b90 798 $localfilename = GenerateRandomString(32, '', 7);
799 $full_localfilename = "$hashed_attachment_dir/$localfilename";
800 while (file_exists($full_localfilename)) {
801 $localfilename = GenerateRandomString(32, '', 7);
802 $full_localfilename = "$hashed_attachment_dir/$localfilename";
803 }
a91189d6 804 $message->att_local_name = $full_localfilename;
181538ac 805
806 $composeMessage->initAttachment($message->type0.'/'.$message->type1,$filename,
807 $full_localfilename);
a91189d6 808
a43e4b90 809 /* Write Attachment to file */
810 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'wb');
811 fputs($fp, decodeBody(mime_fetch_body($imapConnection,
812 $passed_id, $message->entity_id),
813 $message->header->encoding));
814 fclose ($fp);
48985d59 815 }
734f4ee6 816 } else {
a43e4b90 817 for ($i=0, $entCount=count($message->entities); $i<$entCount;$i++) {
818 $composeMessage=getAttachments($message->entities[$i], $composeMessage, $passed_id, $entities, $imapConnection);
48985d59 819 }
820 }
a43e4b90 821 return $composeMessage;
48985d59 822}
823
a43e4b90 824function getMessage_RFC822_Attachment($message, $composeMessage, $passed_id,
756406df 825 $passed_ent_id='', $imapConnection) {
a6ec592e 826 global $attachments, $attachment_dir, $username, $data_dir, $uid_support;
827 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
756406df 828 if (!$passed_ent_id) {
a61878d0 829 $body_a = sqimap_run_command($imapConnection,
830 'FETCH '.$passed_id.' RFC822',
831 TRUE, $response, $readmessage,
832 $uid_support);
756406df 833 } else {
834 $body_a = sqimap_run_command($imapConnection,
a61878d0 835 'FETCH '.$passed_id.' BODY['.$passed_ent_id.']',
836 TRUE, $response, $readmessage, $uid_support);
837 $message = $message->parent;
756406df 838 }
d0519c03 839 if ($response == 'OK') {
a61878d0 840 $subject = encodeHeader($message->rfc822_header->subject);
841 array_shift($body_a);
842 $body = implode('', $body_a) . "\r\n";
843
844 $localfilename = GenerateRandomString(32, 'FILE', 7);
845 $full_localfilename = "$hashed_attachment_dir/$localfilename";
846
a43e4b90 847 $fp = fopen( $full_localfilename, 'w');
a61878d0 848 fwrite ($fp, $body);
849 fclose($fp);
a91189d6 850 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
851 $full_localfilename);
a43e4b90 852 }
853 return $composeMessage;
a6ec592e 854}
855
41b94d65 856function showInputForm ($session, $values=false) {
a94c1db1 857 global $send_to, $send_to_cc, $body,
48985d59 858 $passed_body, $color, $use_signature, $signature, $prefix_sig,
859 $editor_size, $attachments, $subject, $newmail,
41b94d65 860 $use_javascript_addr_book, $send_to_bcc, $passed_id, $mailbox,
48985d59 861 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
862 $username, $data_dir, $identity, $draft_id, $delete_draft,
9c3e6cd4 863 $mailprio, $default_use_mdn, $mdn_user_support, $compose_new_win,
44560457 864 $saved_draft, $mail_sent, $sig_first, $edit_as_new, $action,
ab4700c3 865 $username, $compose_messages, $composesession, $default_charset;
a43e4b90 866
867 $composeMessage = $compose_messages[$session];
48985d59 868
41b94d65 869 if ($values) {
870 $send_to = $values['send_to'];
871 $send_to_cc = $values['send_to_cc'];
872 $send_to_bcc = $values['send_to_bcc'];
873 $subject = $values['subject'];
874 $mailprio = $values['mailprio'];
875 $body = $values['body'];
d3c13a51 876 $identity = (int) $values['identity'];
676bb189 877 } else {
878 $send_to = decodeHeader($send_to);
879 $send_to_cc = decodeHeader($send_to_cc);
880 $send_to_bcc = decodeHeader($send_to_bcc);
41b94d65 881 }
882
48985d59 883 if ($use_javascript_addr_book) {
181538ac 884 echo "\n". '<SCRIPT LANGUAGE=JavaScript>'."\n<!--\n" .
48985d59 885 'function open_abook() { ' . "\n" .
886 ' var nwin = window.open("addrbook_popup.php","abookpopup",' .
887 '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" .
888 ' if((!nwin.opener) && (document.windows != null))' . "\n" .
889 ' nwin.opener = document.windows;' . "\n" .
890 "}\n" .
181538ac 891 "// -->\n</SCRIPT>\n\n";
48985d59 892 }
893
41b94d65 894 echo "\n" . '<FORM name=compose action="compose.php" METHOD=POST ' .
895 'ENCTYPE="multipart/form-data"';
48985d59 896 do_hook("compose_form");
57257333 897
48985d59 898 echo ">\n";
899
41b94d65 900 if ($action == 'draft') {
901 echo '<input type="hidden" name="delete_draft" value="' . $passed_id . "\">\n";
48985d59 902 }
903 if (isset($delete_draft)) {
904 echo '<input type="hidden" name="delete_draft" value="' . $delete_draft. "\">\n";
905 }
da95c4b6 906 if (isset($session)) {
44560457 907 echo '<input type="hidden" name="session" value="' . $session . "\">\n";
da95c4b6 908 }
08bad2b1 909
910 if (isset($passed_id)) {
911 echo '<input type="hidden" name="passed_id" value="' . $passed_id . "\">\n";
912 }
44560457 913
9c3e6cd4 914 if ($saved_draft == 'yes') {
915 echo '<BR><CENTER><B>'. _("Draft Saved").'</CENTER></B>';
916 }
917 if ($mail_sent == 'yes') {
918 echo '<BR><CENTER><B>'. _("Your Message has been sent").'</CENTER></B>';
919 }
a94c1db1 920 echo '<TABLE ALIGN=center CELLSPACING=0 BORDER=0>' . "\n";
9c3e6cd4 921 if ($compose_new_win == '1') {
a94c1db1 922 echo '<TABLE ALIGN=CENTER BGCOLOR="'.$color[0].'" WIDTH="100%" BORDER=0>'."\n" .
98fb28fd 923 ' <TR><TD></TD>'. html_tag( 'td', '', 'right' ) . '<INPUT TYPE="BUTTON" NAME="Close" onClick="return self.close()" VALUE='._("Close").'></TD></TR>'."\n";
9c3e6cd4 924 }
78a35fcd 925 if ($location_of_buttons == 'top') {
926 showComposeButtonRow();
927 }
48985d59 928
0f257091 929 /* display select list for identities */
715225af 930 $idents = getPref($data_dir, $username, 'identities', 0);
931 if ($idents > 1) {
0f257091 932 $fn = getPref($data_dir, $username, 'full_name');
48985d59 933 $em = getPref($data_dir, $username, 'email_address');
0f257091 934 echo ' <tr>' . "\n" .
935 html_tag( 'td', '', 'right', $color[4], 'width="10%"' ) .
936 _("From:") . '</td>' . "\n" .
937 html_tag( 'td', '', 'left', $color[4], 'width="90%"' ) .
938 ' <select name="identity">' . "\n" .
939 ' <option value="default">' .
940 htmlspecialchars($fn);
48985d59 941 if ($em != '') {
0f257091 942 if($fn != '') {
943 echo htmlspecialchars(' <' . $em . '>') . "\n";
944 } else {
945 echo htmlspecialchars($em) . "\n";
946 }
48985d59 947 }
a869e065 948 echo '</option>';
48985d59 949 for ($i = 1; $i < $idents; $i ++) {
0f257091 950 $fn = getPref($data_dir, $username, 'full_name' . $i);
951 $em = getPref($data_dir, $username, 'email_address' . $i);
952
248bfebb 953 echo '<option value="' . $i . '"';
48985d59 954 if (isset($identity) && $identity == $i) {
0f257091 955 echo ' selected';
48985d59 956 }
0f257091 957 echo '>' . htmlspecialchars($fn);
48985d59 958 if ($em != '') {
0f257091 959 if($fn != '') {
960 echo htmlspecialchars(' <' . $em . '>') . "\n";
961 } else {
962 echo htmlspecialchars($em) . "\n";
963 }
48985d59 964 }
9f599fe3 965 echo '</option>';
48985d59 966 }
967 echo '</select>' . "\n" .
0f257091 968 ' </td>' . "\n" .
969 ' </tr>' . "\n";
41b94d65 970 }
0f257091 971 echo ' <tr>' . "\n" .
972 html_tag( 'td', '', 'right', $color[4], 'width="10%"' ) .
0ec1a14b 973 _("To:") . '</TD>' . "\n" .
0f257091 974 html_tag( 'td', '', 'left', $color[4], 'width="90%"' ) .
975 ' <input type="text" name="send_to" value="' .
976 $send_to . '" size="60" /><br />' . "\n" .
977 ' </td>' . "\n" .
978 ' </tr>' . "\n" .
979 ' <tr>' . "\n" .
98fb28fd 980 html_tag( 'td', '', 'right', $color[4] ) .
0f257091 981 _("CC:") . '</td>' . "\n" .
98fb28fd 982 html_tag( 'td', '', 'left', $color[4] ) .
0f257091 983 ' <input type="text" name="send_to_cc" size="60" value="' .
984 $send_to_cc . '" /><br />' . "\n" .
985 ' </td>' . "\n" .
986 ' </tr>' . "\n" .
987 ' <tr>' . "\n" .
98fb28fd 988 html_tag( 'td', '', 'right', $color[4] ) .
0f257091 989 _("BCC:") . '</td>' . "\n" .
98fb28fd 990 html_tag( 'td', '', 'left', $color[4] ) .
0f257091 991 ' <input type="text" name="send_to_bcc" value="' .
992 $send_to_bcc . '" size="60" /><br />' . "\n" .
993 ' </td>' . "\n" .
994 ' </tr>' . "\n" .
995 ' <tr>' . "\n" .
98fb28fd 996 html_tag( 'td', '', 'right', $color[4] ) .
0f257091 997 _("Subject:") . '</td>' . "\n" .
98fb28fd 998 html_tag( 'td', '', 'left', $color[4] ) . "\n";
0f257091 999 echo ' <input type="text" name="subject" size="60" value="' .
1000 $subject . '" />' . "\n" .
1001 ' </td>' . "\n" .
1002 ' </tr>' . "\n\n";
48985d59 1003
78a35fcd 1004 if ($location_of_buttons == 'between') {
1005 showComposeButtonRow();
1006 }
4dfb9db7 1007
0f257091 1008 /* why this distinction? */
fdc83c55 1009 if ($compose_new_win == '1') {
a94c1db1 1010 echo ' <TR>' . "\n" .
1011 ' <TD BGCOLOR="' . $color[0] . '" COLSPAN=2 ALIGN=CENTER>' . "\n" .
41b94d65 1012 ' <TEXTAREA NAME=body ROWS=20 COLS="' .
0ec1a14b 1013 $editor_size . '" WRAP="VIRTUAL">';
fdc83c55 1014 }
1015 else {
a94c1db1 1016 echo ' <TR>' . "\n" .
1017 ' <TD BGCOLOR="' . $color[4] . '" COLSPAN=2>' . "\n" .
41b94d65 1018 ' &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS="' .
0ec1a14b 1019 $editor_size . '" WRAP="VIRTUAL">';
fdc83c55 1020 }
0f257091 1021
48985d59 1022 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
d3c13a51 1023 if ($idents > 1) {
1024 if ($identity == 'default') {
1025 $no = 'g';
1026 } else {
1027 $no = $identity;
1028 }
1029 $signature = getSig($data_dir, $username, $no);
1030 }
1031
3b17e952 1032 if ($sig_first == '1') {
ab4700c3 1033 if ($default_charset == 'iso-2022-jp') {
83be314a 1034 echo "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
1035 } else {
0a06275a 1036 echo "\n\n".($prefix_sig==true? "-- \n":'').decodeHeader($signature,false,false);
83be314a 1037 }
0a06275a 1038 echo "\n\n".decodeHeader($body,false,false);
3b17e952 1039 }
1040 else {
0a06275a 1041 echo "\n\n".decodeHeader($body,false,false);
ab4700c3 1042 if ($default_charset == 'iso-2022-jp') {
83be314a 1043 echo "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
1044 }else{
0a06275a 1045 echo "\n\n".($prefix_sig==true? "-- \n":'').decodeHeader($signature,false,false);
3b17e952 1046 }
1047 }
83be314a 1048 }
3b17e952 1049 else {
0a06275a 1050 echo decodeHeader($body,false,false);
48985d59 1051 }
0f257091 1052 echo '</textarea><br />' . "\n" .
1053 ' </td>' . "\n" .
1054 ' </tr>' . "\n";
48985d59 1055
12a0ed01 1056
48985d59 1057 if ($location_of_buttons == 'bottom') {
1058 showComposeButtonRow();
1059 } else {
0f257091 1060 echo ' <tr>' . "\n" .
1061 html_tag( 'td', '', 'right', '', 'colspan="2"' ) . "\n" .
1062 ' <input type="submit" name="send" value="' . _("Send") . '" />' . "\n" .
1063 ' &nbsp;&nbsp;&nbsp;&nbsp;<br /><br />' . "\n" .
1064 ' </td>' . "\n" .
1065 ' </tr>' . "\n";
48985d59 1066 }
46bb8da8 1067
48985d59 1068 /* This code is for attachments */
a91189d6 1069 if ((bool) ini_get('file_uploads')) {
0a2c3218 1070
1071 /* Calculate the max size for an uploaded file.
1072 * This is advisory for the user because we can't actually prevent
1073 * people to upload too large files. */
1074 $sizes = array();
1075 /* php.ini vars which influence the max for uploads */
1076 $configvars = array('post_max_size', 'memory_limit', 'upload_max_filesize');
1077 foreach($configvars as $var) {
1078 /* skip 0 or empty values */
1079 if( $size = getByteSize(ini_get($var)) ) {
1080 $sizes[] = $size;
1081 }
1082 }
1083
1084 if(count($sizes) > 0) {
1085 $maxsize = '(max.&nbsp;' . show_readable_size( min( $sizes ) ) . ')';
1086 } else {
1087 $maxsize = '';
1088 }
181538ac 1089 echo '<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="'.min( $sizes ).'">';
0a2c3218 1090 echo ' <tr>' . "\n" .
1091 ' <td colspan="2">' . "\n" .
a94c1db1 1092 ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.
0ec1a14b 1093 ' border="0" bgcolor="'.$color[9].'">' . "\n" .
0a2c3218 1094 ' <tr>' . "\n" .
1095 ' <td>' . "\n" .
a94c1db1 1096 ' <table width="100%" cellpadding="3" cellspacing="0" align="center"'.
0ec1a14b 1097 ' border="0">' . "\n" .
0a2c3218 1098 ' <tr>' . "\n" .
1099 html_tag( 'td', '', 'right', '', 'valign="middle"' ) .
1100 _("Attach:") . '</td>' . "\n" .
1101 html_tag( 'td', '', 'left', '', 'valign="middle"' ) .
1102 ' <input name="attachfile" size="48" type="file" />' . "\n" .
0ec1a14b 1103 ' &nbsp;&nbsp;<input type="submit" name="attach"' .
1104 ' value="' . _("Add") .'">' . "\n" .
0a2c3218 1105 $maxsize .
1106 ' </td>' . "\n" .
1107 ' </tr>' . "\n";
91f2085b 1108
41b94d65 1109
91f2085b 1110 $s_a = array();
4dfb9db7 1111 if ($composeMessage->entities) {
1112 foreach ($composeMessage->entities as $key => $attachment) {
a43e4b90 1113 $attached_file = $attachment->att_local_name;
a91189d6 1114 if ($attachment->att_local_name || $attachment->body_part) {
1115 $attached_filename = decodeHeader($attachment->mime_header->getParameter('name'));
1116 $type = $attachment->mime_header->type0.'/'.
1117 $attachment->mime_header->type1;
98fb28fd 1118
a91189d6 1119 $s_a[] = '<table bgcolor="'.$color[0].
1120 '" border="0"><tr><td><input type="checkbox" name="delete[]" value="' .
1121 $key . "\"></td><td>\n" . $attached_filename .
1122 '</td><td>-</td><td> ' . $type . '</td><td>('.
1123 show_readable_size( filesize( $attached_file ) ) . ')</td></tr></table>'."\n";
a43e4b90 1124 }
4dfb9db7 1125 }
91f2085b 1126 }
1127 if (count($s_a)) {
a94c1db1 1128 foreach ($s_a as $s) {
98fb28fd 1129 echo '<tr>' . html_tag( 'td', '', 'left', $color[0], 'colspan="2"' ) . $s .'</td></tr>';
a61878d0 1130 }
91f2085b 1131 echo '<tr><td colspan="2"><input type="submit" name="do_delete" value="' .
1132 _("Delete selected attachments") . "\">\n" .
1133 '</td></tr>';
1134 }
0ec1a14b 1135 echo ' </table>' . "\n" .
1136 ' </td>' . "\n" .
1137 ' </tr>' . "\n" .
1138 ' </TABLE>' . "\n" .
1139 ' </TD>' . "\n" .
1140 ' </TR>' . "\n";
a91189d6 1141 } // End of file_uploads if-block
41b94d65 1142 /* End of attachment code */
07687736 1143 if ($compose_new_win == '1') {
41b94d65 1144 echo '</TABLE>'."\n";
07687736 1145 }
a64f47e7 1146
a61878d0 1147 echo '</TABLE>' . "\n" .
1148 '<input type="hidden" name="username" value="'. $username . "\">\n" .
5da08ef7 1149 '<input type=hidden name=action value="' . $action . "\">\n" .
a61878d0 1150 '<INPUT TYPE=hidden NAME=mailbox VALUE="' . htmlspecialchars($mailbox) .
4dfb9db7 1151 "\">\n";
5da08ef7 1152 /*
0ec1a14b 1153 store the complete ComposeMessages array in a hidden input value
1154 so we can restore them in case of a session timeout.
5da08ef7 1155 */
953fa718 1156 sqgetGlobalVar('QUERY_STRING', $queryString, SQ_SERVER);
5da08ef7 1157 echo '<input type=hidden name=restoremessages value="' . urlencode(serialize($compose_messages)) . "\">\n";
1158 echo '<input type=hidden name=composesession value="' . $composesession . "\">\n";
953fa718 1159 echo '<input type=hidden name=querystring value="' . $queryString . "\">\n";
4dfb9db7 1160 echo '</FORM>';
a64f47e7 1161 if (!(bool) ini_get('file_uploads')) {
1162 /* File uploads are off, so we didn't show that part of the form.
1163 To avoid bogus bug reports, tell the user why. */
1164 echo 'Because PHP file uploads are turned off, you can not attach files ';
1165 echo "to this message. Please see your system administrator for details.\r\n";
1166 }
1167
9f599fe3 1168 do_hook('compose_bottom');
48985d59 1169 echo '</BODY></HTML>' . "\n";
1170}
1171
1172
70c4fd84 1173function showComposeButtonRow() {
78a35fcd 1174 global $use_javascript_addr_book, $save_as_draft,
a61878d0 1175 $default_use_priority, $mailprio, $default_use_mdn,
1176 $request_mdn, $request_dr,
1177 $data_dir, $username;
70c4fd84 1178
0ec1a14b 1179 echo ' <TR>' . "\n" .
1180 ' <TD></TD>' . "\n" .
1181 ' <TD>' . "\n";
ae25968c 1182 if ($default_use_priority) {
1183 if(!isset($mailprio)) {
1184 $mailprio = "3";
70c4fd84 1185 }
0ec1a14b 1186 echo ' ' . _("Priority") .': <select name="mailprio">'.
70c4fd84 1187 '<option value="1"'.($mailprio=='1'?' selected':'').'>'. _("High") .'</option>'.
1188 '<option value="3"'.($mailprio=='3'?' selected':'').'>'. _("Normal") .'</option>'.
1189 '<option value="5"'.($mailprio=='5'?' selected':'').'>'. _("Low").'</option>'.
0ec1a14b 1190 '</select>' . "\n";
ae25968c 1191 }
1192 $mdn_user_support=getPref($data_dir, $username, 'mdn_user_support',$default_use_mdn);
1193 if ($default_use_mdn) {
70c4fd84 1194 if ($mdn_user_support) {
0ec1a14b 1195 echo ' ' . _("Receipt") .': '.
b2a7e5bc 1196 '<input type="checkbox" name="request_mdn" value=1'.
a61878d0 1197 ($request_mdn=='1'?' checked':'') .'>'. _("On Read").
b2a7e5bc 1198 ' <input type="checkbox" name="request_dr" value=1'.
a61878d0 1199 ($request_dr=='1'?' checked':'') .'>'. _("On Delivery");
70c4fd84 1200 }
ae25968c 1201 }
48985d59 1202
0ec1a14b 1203 echo ' </TD>' . "\n" .
1204 ' </TR>' . "\n" .
1205 ' <TR>' . "\n" .
1206 ' <TD></TD>' . "\n" .
1207 ' <TD>' . "\n" .
1208 ' <INPUT TYPE=SUBMIT NAME="sigappend" VALUE="' . _("Signature") . '">' . "\n";
78a35fcd 1209 if ($use_javascript_addr_book) {
0ec1a14b 1210 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"".
1211 " <input type=button value=\\\""._("Addresses").
1212 "\\\" onclick='javascript:open_abook();'>\");".
1213 " // --></SCRIPT><NOSCRIPT>\n".
1214 " <input type=submit name=\"html_addr_search\" value=\"".
46bb8da8 1215 _("Addresses")."\">".
0ec1a14b 1216 " </NOSCRIPT>\n";
734f4ee6 1217 } else {
0ec1a14b 1218 echo ' <input type=submit name="html_addr_search" value="'.
1219 _("Addresses").'">' . "\n";
78a35fcd 1220 }
48985d59 1221
78a35fcd 1222 if ($save_as_draft) {
0ec1a14b 1223 echo ' <input type="submit" name ="draft" value="' . _("Save Draft") . "\">\n";
78a35fcd 1224 }
0a17f9dd 1225
0ec1a14b 1226 echo ' <INPUT TYPE=submit NAME=send VALUE="'. _("Send") . '">' . "\n";
78a35fcd 1227 do_hook('compose_button_row');
441f2d33 1228
0ec1a14b 1229 echo ' </TD>' . "\n" .
1230 ' </TR>' . "\n\n";
78a35fcd 1231}
b278172f 1232
70c4fd84 1233function checkInput ($show) {
78a35fcd 1234 /*
1235 * I implemented the $show variable because the error messages
1236 * were getting sent before the page header. So, I check once
1237 * using $show=false, and then when i'm ready to display the error
1238 * message, show=true
1239 */
6bf2a88f 1240 global $body, $send_to, $send_to_bcc, $subject, $color;
78a35fcd 1241
6bf2a88f 1242 if ($send_to == '' && $send_to_bcc == '') {
78a35fcd 1243 if ($show) {
0ad7dbda 1244 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
78a35fcd 1245 }
1246 return false;
1247 }
1248 return true;
1249} /* function checkInput() */
df15de21 1250
3806fa52 1251
00793a25 1252/* True if FAILURE */
da95c4b6 1253function saveAttachedFiles($session) {
0b97a708 1254 global $_FILES, $attachment_dir, $attachments, $username,
a43e4b90 1255 $data_dir, $compose_messages;
4c9d2242 1256
45cdd1b5 1257 /* get out of here if no file was attached at all */
1258 if (! is_uploaded_file($_FILES['attachfile']['tmp_name']) ) {
1259 return true;
1260 }
1261
4c9d2242 1262 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1263 $localfilename = GenerateRandomString(32, '', 7);
1264 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1265 while (file_exists($full_localfilename)) {
1266 $localfilename = GenerateRandomString(32, '', 7);
1267 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1268 }
1269
e6675f9a 1270 // FIXME: we SHOULD prefer move_uploaded_file over rename because
1271 // m_u_f works better with restricted PHP installes (safe_mode, open_basedir)
1272 if (!@rename($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
1273 if (!@move_uploaded_file($_FILES['attachfile']['tmp_name'],$full_localfilename)) {
a91189d6 1274 return true;
1275 }
a61878d0 1276 }
a43e4b90 1277 $message = $compose_messages[$session];
0b97a708 1278 $type = strtolower($_FILES['attachfile']['type']);
1279 $name = $_FILES['attachfile']['name'];
a43e4b90 1280 $message->initAttachment($type, $name, $full_localfilename);
1281 $compose_messages[$session] = $message;
b0314f04 1282 sqsession_register($compose_messages , 'compose_messages');
4c9d2242 1283}
1284
a43e4b90 1285function ClearAttachments($composeMessage) {
b48d3c53 1286 if ($composeMessage->att_local_name) {
1287 $attached_file = $composeMessage->att_local_name;
a43e4b90 1288 if (file_exists($attached_file)) {
1289 unlink($attached_file);
8712abea 1290 }
da95c4b6 1291 }
a43e4b90 1292 for ($i=0, $entCount=count($composeMessage->entities);$i< $entCount; ++$i) {
1293 ClearAttachments($composeMessage->entities[$i]);
1294 }
4c9d2242 1295}
1296
0a2c3218 1297/* parse values like 8M and 2k into bytes */
1298function getByteSize($ini_size) {
1299
4d30dc83 1300 if(!$ini_size) {
1301 return FALSE;
1302 }
da95c4b6 1303
0a2c3218 1304 $ini_size = trim($ini_size);
1305
5b9716de 1306 // if there's some kind of letter at the end of the string we need to multiply.
1307 if(!is_numeric(substr($ini_size, -1))) {
1308
1309 switch(strtoupper(substr($ini_size, -1))) {
1310 case 'G':
1311 $bytesize = 1073741824;
1312 break;
1313 case 'M':
1314 $bytesize = 1048576;
1315 break;
1316 case 'K':
1317 $bytesize = 1024;
1318 break;
1319 }
1320
4d30dc83 1321 return ($bytesize * (int)substr($ini_size, 0, -1));
0a2c3218 1322 }
5b9716de 1323
4d30dc83 1324 return $ini_size;
0a2c3218 1325}
a43e4b90 1326
4c9d2242 1327
a43e4b90 1328/* temporary function to make use of the deliver class.
1329 In the future the responsable backend should be automaticly loaded
1330 and conf.pl should show a list of available backends.
1331 The message also should be constructed by the message class.
1332*/
1333
b7ff469f 1334function deliverMessage($composeMessage, $draft=false) {
a43e4b90 1335 global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body,
1336 $username, $popuser, $usernamedata, $identity, $data_dir,
a91189d6 1337 $request_mdn, $request_dr, $default_charset, $color, $useSendmail,
20152d80 1338 $domain, $action, $default_move_to_sent, $move_to_sent;
a43e4b90 1339 global $imapServerAddress, $imapPort, $sent_folder, $key;
1340
b0a3a738 1341 /* some browsers replace <space> by nonbreaking spaces &nbsp;
1342 by replacing them back to spaces addressparsing works */
1343 /* FIXME: How to handle in case of other charsets where "\240"
1344 is not a non breaking space ??? */
1345
1346 $send_to = str_replace("\240",' ',$send_to);
1347 $send_to_cc = str_replace("\240",' ',$send_to_cc);
1348 $send_to_bcc = str_replace("\240",' ',$send_to_bcc);
1349
a43e4b90 1350 $rfc822_header = $composeMessage->rfc822_header;
24192f77 1351
1352 $abook = addressbook_init(false, true);
24192f77 1353 $rfc822_header->to = $rfc822_header->parseAddress($send_to,true, array(), '', $domain, array(&$abook,'lookup'));
1354 $rfc822_header->cc = $rfc822_header->parseAddress($send_to_cc,true,array(), '',$domain, array(&$abook,'lookup'));
1355 $rfc822_header->bcc = $rfc822_header->parseAddress($send_to_bcc,true, array(), '',$domain, array(&$abook,'lookup'));
a43e4b90 1356 $rfc822_header->priority = $mailprio;
1357 $rfc822_header->subject = $subject;
1358 $special_encoding='';
1359 if (strtolower($default_charset) == 'iso-2022-jp') {
1360 if (mb_detect_encoding($body) == 'ASCII') {
a91189d6 1361 $special_encoding = '8bit';
a43e4b90 1362 } else {
1363 $body = mb_convert_encoding($body, 'JIS');
1364 $special_encoding = '7bit';
1365 }
1366 }
1367 $composeMessage->setBody($body);
1368
1369 if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
1370 $popuser = $usernamedata[1];
1371 $domain = $usernamedata[2];
1372 unset($usernamedata);
1373 } else {
1374 $popuser = $username;
1375 }
1376 $reply_to = '';
1377 if (isset($identity) && $identity != 'default') {
045714fd 1378 $from_mail = getPref($data_dir, $username,'email_address' . $identity);
1379 $full_name = getPref($data_dir, $username,'full_name' . $identity);
1380 $reply_to = getPref($data_dir, $username,'reply_to' . $identity);
a43e4b90 1381 } else {
1382 $from_mail = getPref($data_dir, $username, 'email_address');
1383 $full_name = getPref($data_dir, $username, 'full_name');
a43e4b90 1384 $reply_to = getPref($data_dir, $username,'reply_to');
1385 }
045714fd 1386 if (!$from_mail) {
1387 $from_mail = "$popuser@$domain";
045714fd 1388 }
1389 $rfc822_header->from = $rfc822_header->parseAddress($from_mail,true);
1390 if ($full_name) {
9783f396 1391 $from = $rfc822_header->from[0];
a91189d6 1392 if (!$from->host) $from->host = $domain;
12a0ed01 1393 $full_name_encoded = encodeHeader($full_name);
1394 if ($full_name_encoded != $full_name) {
1395 $from_addr = $full_name_encoded .' <'.$from->mailbox.'@'.$from->host.'>';
1396 } else {
1397 $from_addr = '"'.$full_name .'" <'.$from->mailbox.'@'.$from->host.'>';
1398 }
045714fd 1399 $rfc822_header->from = $rfc822_header->parseAddress($from_addr,true);
a43e4b90 1400 }
a43e4b90 1401 if ($reply_to) {
1402 $rfc822_header->reply_to = $rfc822_header->parseAddress($reply_to,true);
1403 }
1404 /* Receipt: On Read */
1405 if (isset($request_mdn) && $request_mdn) {
1406 $rfc822_header->dnt = $rfc822_header->parseAddress($from_mail,true);
1407 }
1408 /* Receipt: On Delivery */
1409 if (isset($request_dr) && $request_dr) {
1410 $rfc822_header->more_headers['Return-Receipt-To'] = $from_mail;
1411 }
1412 /* multipart messages */
1413 if (count($composeMessage->entities)) {
1414 $message_body = new Message();
a91189d6 1415 $message_body->body_part = $composeMessage->body_part;
1416 $composeMessage->body_part = '';
1417 $mime_header = new MessageHeader;
1418 $mime_header->type0 = 'text';
1419 $mime_header->type1 = 'plain';
1420 if ($special_encoding) {
1421 $mime_header->encoding = $special_encoding;
1422 } else {
12a0ed01 1423 $mime_header->encoding = '8bit';
a91189d6 1424 }
1425 if ($default_charset) {
1426 $mime_header->parameters['charset'] = $default_charset;
1427 }
1428 $message_body->mime_header = $mime_header;
a43e4b90 1429 array_unshift($composeMessage->entities, $message_body);
a91189d6 1430 $content_type = new ContentType('multipart/mixed');
a43e4b90 1431 } else {
1e2026df 1432 $content_type = new ContentType('text/plain');
1433 if ($special_encoding) {
1434 $rfc822_header->encoding = $special_encoding;
1435 } else {
1436 $rfc822_header->encoding = '8bit';
1437 }
426e0b72 1438 if ($default_charset) {
1439 $content_type->properties['charset']=$default_charset;
1440 }
181538ac 1441 }
1442
a43e4b90 1443 $rfc822_header->content_type = $content_type;
1444 $composeMessage->rfc822_header = $rfc822_header;
181538ac 1445
5618924b 1446 /* Here you can modify the message structure just before we hand
1447 it over to deliver */
1448 do_hook('compose_send');
a43e4b90 1449
b48d3c53 1450 if (!$useSendmail && !$draft) {
a91189d6 1451 require_once(SM_PATH . 'class/deliver/Deliver_SMTP.class.php');
1452 $deliver = new Deliver_SMTP();
1453 global $smtpServerAddress, $smtpPort, $pop_before_smtp, $smtp_auth_mech;
1454
1455 if ($smtp_auth_mech == 'none') {
1456 $user = '';
1457 $pass = '';
1458 } else {
1459 global $key, $onetimepad;
1460 $user = $username;
1461 $pass = OneTimePadDecrypt($key, $onetimepad);
1462 }
1463
1464 $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ? true : false;
1465 $stream = $deliver->initStream($composeMessage,$domain,0,
1466 $smtpServerAddress, $smtpPort, $user, $pass, $authPop);
b48d3c53 1467 } elseif (!$draft) {
86725763 1468 require_once(SM_PATH . 'class/deliver/Deliver_SendMail.class.php');
b48d3c53 1469 global $sendmail_path;
1470 $deliver = new Deliver_SendMail();
1471 $stream = $deliver->initStream($composeMessage,$sendmail_path);
1472 } elseif ($draft) {
1473 global $draft_folder;
86725763 1474 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
b48d3c53 1475 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
1476 $imapPort, 0);
1477 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
4dfb9db7 1478 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
a91189d6 1479 $imap_deliver = new Deliver_IMAP();
1480 $length = $imap_deliver->mail($composeMessage);
1481 sqimap_append ($imap_stream, $draft_folder, $length);
4dfb9db7 1482 $imap_deliver->mail($composeMessage, $imap_stream);
a91189d6 1483 sqimap_append_done ($imap_stream, $draft_folder);
1484 sqimap_logout($imap_stream);
1485 unset ($imap_deliver);
1486 return $length;
4dfb9db7 1487 } else {
a91189d6 1488 $msg = '<br>Error: '._("Draft folder")." $draft_folder" . ' does not exist.';
1489 plain_error_message($msg, $color);
1490 return false;
1491 }
a43e4b90 1492 }
1493 $succes = false;
1494 if ($stream) {
a91189d6 1495 $length = $deliver->mail($composeMessage, $stream);
1496 $succes = $deliver->finalizeStream($stream);
a43e4b90 1497 }
1498 if (!$succes) {
00ac2f42 1499 $msg = $deliver->dlv_msg . '<br>' .
1500 _("Server replied: ") . $deliver->dlv_ret_nr . ' '.
1501 $deliver->dlv_server_msg;
a43e4b90 1502 plain_error_message($msg, $color);
1503 } else {
1504 unset ($deliver);
20152d80 1505 $move_to_sent = getPref($data_dir,$username,'move_to_sent');
1506 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
e4a1f097 1507
1508 /* Move to sent code */
1509 if (isset($default_move_to_sent) && ($default_move_to_sent != 0)) {
1510 $svr_allow_sent = true;
1511 } else {
1512 $svr_allow_sent = false;
1513 }
1514
1515 if (isset($sent_folder) && (($sent_folder != '') || ($sent_folder != 'none'))
1516 && sqimap_mailbox_exists( $imap_stream, $sent_folder)) {
1517 $fld_sent = true;
1518 } else {
1519 $fld_sent = false;
1520 }
1521
1522 if ((isset($move_to_sent) && ($move_to_sent != 0)) || (!isset($move_to_sent))) {
1523 $lcl_allow_sent = true;
1524 } else {
1525 $lcl_allow_sent = false;
1526 }
1527
1528 if (($fld_sent && $svr_allow_sent && !$lcl_allow_sent) || ($fld_sent && $lcl_allow_sent)) {
1529 sqimap_append ($imap_stream, $sent_folder, $length);
a91189d6 1530 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
1531 $imap_deliver = new Deliver_IMAP();
1532 $imap_deliver->mail($composeMessage, $imap_stream);
e4a1f097 1533 sqimap_append_done ($imap_stream, $sent_folder);
a91189d6 1534 unset ($imap_deliver);
1535 }
1536 global $passed_id, $mailbox, $action;
1537 ClearAttachments($composeMessage);
1538 if ($action == 'reply' || $action == 'reply_all') {
1539 sqimap_mailbox_select ($imap_stream, $mailbox);
1540 sqimap_messages_flag ($imap_stream, $passed_id, $passed_id, 'Answered', true);
1541 }
1542 sqimap_logout($imap_stream);
a43e4b90 1543 }
1544 return $succes;
1545}
1546
6bf2a88f 1547?>