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