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