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