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