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