added recover mechanism in case of expired sessions
[squirrelmail.git] / src / compose.php
CommitLineData
59177427 1<?php
895905c0 2
35586184 3/**
4 * compose.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This code sends a mail.
10 *
11 * There are 4 modes of operation:
12 * - Start new mail
13 * - Add an attachment
14 * - Send mail
15 * - Save As Draft
16 *
17 * $Id$
18 */
f7fb20fe 19
35586184 20require_once('../src/validate.php');
21require_once('../functions/imap.php');
22require_once('../functions/date.php');
23require_once('../functions/mime.php');
24require_once('../functions/smtp.php');
35586184 25require_once('../functions/plugin.php');
41b94d65 26require_once('../functions/display_messages.php');
09044055 27/* --------------------- Specific Functions ------------------------------ */
28
41b94d65 29function replyAllString($header) {
30 global $include_self_reply_all, $username, $data_dir;
31 $excl_arr = array();
32 /**
33 * 1) Remove the addresses we'll be sending the message 'to'
34 */
35 $url_replytoall_avoid_addrs = '';
36 if (isset($header->replyto)) {
37 $excl_ar = $header->getAddr_a('replyto');
38 }
39 /**
40 * 2) Remove our identities from the CC list (they still can be in the
41 * TO list) only if $include_self_reply_all is turned off
42 */
43 if (!$include_self_reply_all) {
44 $email_address = trim(getPref($data_dir, $username, 'email_address'));
45 $excl_ar[$email_address] = '';
46
47 $idents = getPref($data_dir, $username, 'identities');
48 if ($idents != '' && $idents > 1) {
49 for ($i = 1; $i < $idents; $i ++) {
50 $cur_email_address = getPref($data_dir, $username,
51 'email_address' . $i);
52 $cur_email_address = strtolower($cur_email_address);
53 $excl_ar[$cur_email_address] = '';
54 }
55 }
56 }
57
58 /**
59 * 3) get the addresses.
60 */
61 $url_replytoall_ar = $header->getAddr_a(array('to','cc'), $excl_ar);
62
63 /**
64 * 4) generate the string.
65 */
66 $url_replytoallcc = '';
67 foreach( $url_replytoall_ar as $email => $personal) {
68 if ($personal) {
69 $url_replytoallcc .= ", \"$personal\" <$email>";
70 } else {
71 $url_replytoallcc .= ', '. $email;
72 }
73 }
74 $url_replytoallcc = substr($url_replytoallcc,2);
75 return $url_replytoallcc;
09044055 76}
77
41b94d65 78function getforwardHeader($orig_header) {
79 $bodyTop = '-------- ' . _("Original Message") . " --------\n" .
80 _("Subject") . ': ' . $orig_header->subject . "\n" .
81 _("From") . ': ' . $orig_header->from->getAddress() . "\n" .
82 _("Date") . ': ' .
83 getLongDateString( $orig_header->date ). "\n" .
84 _("To") . ': ' . $orig_header->to[0]->getAddress() . "\n";
85 if (count($orig_header->to) > 1) {
86 for ($x=1; $x < count($orig_header->to); $x++) {
87 $bodyTop .= ' ' . $orig_header->to[$x]->getAddress() . "\n";
88 }
89 }
90 if ($orig_header->cc != array() && $orig_header->cc !='') {
91 $bodyTop .= _("Cc") . ': ' . $orig_header->cc[0]->getAddress() . "\n";
92 if (count($orig_header->cc) > 1) {
93 for ($x = 1; $x < count($orig_header->cc); $x++) {
94 $bodyTop .= ' ' . $orig_header->cc[$x]->getAddress() . "\n";
95 }
96 }
97 }
98 $bodyTop .= "\n";
99 return $bodyTop;
100}
09044055 101/* ----------------------------------------------------------------------- */
102
44560457 103/*
104 * If the session is expired during a post this restores the compose session
105 * vars.
106 */
107//$session_expired = false;
108if (session_is_registered('session_expired_post')) {
109 global $session_expired_post, $session_expired;
110 /*
111 * extra check for username so we don't display previous post data from
112 * another user during this session.
113 */
114 if ($session_expired_post['username'] != $username) {
115 session_unregister('session_expired_post');
116 session_unregister('session_expired');
117 } else {
118 foreach ($session_expired_post as $postvar => $val) {
119 if (isset($val)) {
120 $$postvar = $val;
121 } else {
122 $$postvar = '';
123 }
124 }
125 if (isset($send)) {
126 unset($send);
127 }
128 $session_expired = true;
129 }
130 session_unregister('session_expired_post');
131 session_unregister('session_expired');
132 if ($compose_new_win == '1') {
133 compose_Header($color, $mailbox);
134 } else {
135 displayPageHeader($color, $mailbox);
136 }
137 showInputForm($session, false);
138 exit();
139}
140
48985d59 141if (!isset($attachments)) {
142 $attachments = array();
143 session_register('attachments');
144}
145
da95c4b6 146if (!isset($composesession)) {
147 $composesession = 0;
148 session_register('composesession');
149}
150
d7f8e6e6 151if (!isset($session) || (isset($newmessage) && $newmessage)) {
da95c4b6 152 $session = "$composesession" +1;
153 $composesession = $session;
d7f8e6e6 154}
da95c4b6 155
00793a25 156if (!isset($mailbox) || $mailbox == '' || ($mailbox == 'None')) {
157 $mailbox = 'INBOX';
158}
159
160if (isset($draft)) {
715225af 161 include_once ('../src/draft_actions.php');
113e5e9d 162 if (! isset($reply_id)) {
163 $reply_id = 0;
164 }
7058a2a9 165 if (! isset($MDN)) {
166 $MDN = 'False';
113e5e9d 167 }
fa77d354 168 if (! isset($mailprio)) {
169 $mailprio = '';
170 }
171 if (!saveMessageAsDraft($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $reply_id, $mailprio, $session)) {
da95c4b6 172 showInputForm($session);
00793a25 173 exit();
734f4ee6 174 } else {
00793a25 175 $draft_message = _("Draft Email Saved");
176 /* If this is a resumed draft, then delete the original */
177 if(isset($delete_draft)) {
7058a2a9 178 Header("Location: delete_message.php?mailbox=" . urlencode($draft_folder) .
fae72101 179 "&message=$delete_draft&sort=$sort&startMessage=1&saved_draft=yes");
00793a25 180 exit();
7058a2a9 181 }
9c3e6cd4 182 else {
183 if ($compose_new_win == '1') {
da95c4b6 184 Header("Location: compose.php?saved_draft=yes&session=$composesession");
9c3e6cd4 185 exit();
186 }
187 else {
fae72101 188 Header("Location: right_main.php?mailbox=$draft_folder&sort=$sort".
2017ebeb 189 "&startMessage=1&note=".urlencode($draft_message));
00793a25 190 exit();
9c3e6cd4 191 }
00793a25 192 }
193 }
194}
195
196if (isset($send)) {
197 if (isset($HTTP_POST_FILES['attachfile']) &&
198 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
199 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none') {
da95c4b6 200 $AttachFailure = saveAttachedFiles($session);
00793a25 201 }
202 if (checkInput(false) && !isset($AttachFailure)) {
203 $urlMailbox = urlencode (trim($mailbox));
204 if (! isset($reply_id)) {
205 $reply_id = 0;
206 }
207 /*
208 * Set $default_charset to correspond with the user's selection
7058a2a9 209 * of language interface.
00793a25 210 */
211 set_my_charset();
212
213 /*
214 * This is to change all newlines to \n
7058a2a9 215 * We'll change them to \r\n later (in the sendMessage function)
00793a25 216 */
217 $body = str_replace("\r\n", "\n", $body);
218 $body = str_replace("\r", "\n", $body);
219
220 /*
221 * Rewrap $body so that no line is bigger than $editor_size
222 * This should only really kick in the sqWordWrap function
f302d704 223 * if the browser doesn't support "VIRTUAL" as the wrap type.
00793a25 224 */
225 $body = explode("\n", $body);
226 $newBody = '';
227 foreach ($body as $line) {
228 if( $line <> '-- ' ) {
229 $line = rtrim($line);
230 }
231 if (strlen($line) <= $editor_size + 1) {
232 $newBody .= $line . "\n";
734f4ee6 233 } else {
e0858036 234 sqWordWrap($line, $editor_size);
235 $newBody .= $line . "\n";
00793a25 236 }
237 }
238 $body = $newBody;
239
e02775fe 240 do_hook('compose_send');
241
57257333 242 $MDN = False; // we are not sending a mdn response
00793a25 243 if (! isset($mailprio)) {
244 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
da95c4b6 245 $subject, $body, $reply_id, $MDN, '', $session);
734f4ee6 246 } else {
00793a25 247 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
da95c4b6 248 $subject, $body, $reply_id, $MDN, $mailprio, $session);
00793a25 249 }
250 if (! $Result) {
da95c4b6 251 showInputForm($session);
00793a25 252 exit();
253 }
254 if ( isset($delete_draft)) {
7058a2a9 255 Header("Location: delete_message.php?mailbox=" . urlencode( $draft_folder ).
fae72101 256 "&message=$delete_draft&sort=$sort&startMessage=1&mail_sent=yes");
00793a25 257 exit();
258 }
9c3e6cd4 259 if ($compose_new_win == '1') {
d7f8e6e6 260 Header("Location: compose.php?mail_sent=yes");
9c3e6cd4 261 }
262 else {
fae72101 263 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort".
264 "&startMessage=1");
9c3e6cd4 265 }
734f4ee6 266 } else {
00793a25 267 /*
268 *$imapConnection = sqimap_login($username, $key, $imapServerAddress,
269 * $imapPort, 0);
270 */
9c3e6cd4 271 if ($compose_new_win == '1') {
272 compose_Header($color, $mailbox);
273 }
274 else {
275 displayPageHeader($color, $mailbox);
276 }
00793a25 277 if (isset($AttachFailure)) {
278 plain_error_message(_("Could not move/copy file. File not attached"),
279 $color);
280 }
00793a25 281 checkInput(true);
da95c4b6 282 showInputForm($session);
00793a25 283 /* sqimap_logout($imapConnection); */
284 }
e02775fe 285} elseif (isset($html_addr_search_done)) {
9c3e6cd4 286 if ($compose_new_win == '1') {
287 compose_Header($color, $mailbox);
288 }
289 else {
290 displayPageHeader($color, $mailbox);
291 }
00793a25 292
293 if (isset($send_to_search) && is_array($send_to_search)) {
294 foreach ($send_to_search as $k => $v) {
295 if (substr($k, 0, 1) == 'T') {
296 if ($send_to) {
297 $send_to .= ', ';
298 }
299 $send_to .= $v;
300 }
301 elseif (substr($k, 0, 1) == 'C') {
302 if ($send_to_cc) {
303 $send_to_cc .= ', ';
304 }
305 $send_to_cc .= $v;
306 }
307 elseif (substr($k, 0, 1) == 'B') {
308 if ($send_to_bcc) {
309 $send_to_bcc .= ', ';
310 }
311 $send_to_bcc .= $v;
312 }
313 }
314 }
da95c4b6 315 showInputForm($session);
e02775fe 316} elseif (isset($html_addr_search)) {
00793a25 317 if (isset($HTTP_POST_FILES['attachfile']) &&
318 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
319 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none') {
da95c4b6 320 if (saveAttachedFiles($session)) {
00793a25 321 plain_error_message(_("Could not move/copy file. File not attached"), $color);
322 }
323 }
324 /*
325 * I am using an include so as to elminiate an extra unnecessary
326 * click. If you can think of a better way, please implement it.
327 */
328 include_once('./addrbook_search_html.php');
e02775fe 329} elseif (isset($attach)) {
da95c4b6 330 if (saveAttachedFiles($session)) {
00793a25 331 plain_error_message(_("Could not move/copy file. File not attached"), $color);
332 }
9c3e6cd4 333 if ($compose_new_win == '1') {
334 compose_Header($color, $mailbox);
335 }
336 else {
337 displayPageHeader($color, $mailbox);
338 }
da95c4b6 339 showInputForm($session);
01265fba 340}
341elseif (isset($sigappend)) {
342 $idents = getPref($data_dir, $username, 'identities', 0);
343 if ($idents > 1) {
344 if ($identity == 'default') {
345 $no = 'g';
346 } else {
347 $no = $identity;
348 }
349 $signature = getSig($data_dir, $username, $no);
350 }
351 $body .= "\n\n".($prefix_sig==true? "-- \n":'').$signature;
352 if ($compose_new_win == '1') {
353 compose_Header($color, $mailbox);
354 } else {
355 displayPageHeader($color, $mailbox);
356 }
da95c4b6 357 showInputForm($session);
e02775fe 358} elseif (isset($do_delete)) {
9c3e6cd4 359 if ($compose_new_win == '1') {
360 compose_Header($color, $mailbox);
361 }
362 else {
363 displayPageHeader($color, $mailbox);
364 }
00793a25 365
366 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
367 if (isset($delete) && is_array($delete)) {
368 foreach($delete as $index) {
369 $attached_file = $hashed_attachment_dir . '/'
370 . $attachments[$index]['localfilename'];
da95c4b6 371 unlink ($attached_file);
372 unset ($attachments[$index]);
00793a25 373 }
374 }
375
da95c4b6 376 showInputForm($session);
377
378} elseif (isset($attachedmessages)) {
379
380 /*
381 * This handles the case if we attache message
382 */
44560457 383 if ($compose_new_win == '1') {
384 compose_Header($color, $mailbox);
385 } else {
386 displayPageHeader($color, $mailbox);
387 }
da95c4b6 388
389 $newmail = true;
390
41b94d65 391 if (!isset($passed_ent_id)) $passed_ent_id = '';
392 if (!isset($passed_id)) $passed_id = '';
393 if (!isset($mailbox)) $mailbox = '';
394 if (!isset($action)) $action = '';
395
44560457 396 $values = newMail($mailbox,$passed_id,$passed_ent_id, $action, $session);
b9928adc 397 /* in case the origin is not read_body.php */
398 if (isset($send_to)) {
399 $values['send_to'] = $send_to;
400 }
401 if (isset($send_to_cc)) {
402 $values['send_to_cc'] = $send_cc;
403 }
404 if (isset($send_to_bcc)) {
405 $values['send_to_bcc'] = $send_bcc;
406 }
407
41b94d65 408 showInputForm($session, $values);
734f4ee6 409} else {
00793a25 410 /*
411 * This handles the default case as well as the error case
412 * (they had the same code) --> if (isset($smtpErrors))
413 */
44560457 414
415 if ($compose_new_win == '1') {
416 compose_Header($color, $mailbox);
417 } else {
418 displayPageHeader($color, $mailbox);
419 }
00793a25 420
421 $newmail = true;
422
41b94d65 423 if (!isset($passed_ent_id)) $passed_ent_id = '';
424 if (!isset($passed_id)) $passed_id = '';
425 if (!isset($mailbox)) $mailbox = '';
426 if (!isset($action)) $action = '';
427
44560457 428 $values = newMail($mailbox,$passed_id,$passed_ent_id, $action, $session);
b9928adc 429
430 /* in case the origin is not read_body.php */
431 if (isset($send_to)) {
432 $values['send_to'] = $send_to;
433 }
434 if (isset($send_to_cc)) {
44560457 435 $values['send_to_cc'] = $send_to_cc;
b9928adc 436 }
437 if (isset($send_to_bcc)) {
44560457 438 $values['send_to_bcc'] = $send_to_bcc;
b9928adc 439 }
41b94d65 440 showInputForm($session, $values);
00793a25 441}
442
443exit();
444
00793a25 445/**************** Only function definitions go below *************/
446
447
48985d59 448/* This function is used when not sending or adding attachments */
44560457 449function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $session='') {
41b94d65 450 global $editor_size, $default_use_priority,
44560457 451 $use_signature, $composesession, $data_dir, $username,
452 $username, $key, $imapServerAddress, $imapPort;
e7f1a81d 453
bdb92db3 454 $send_to = $send_to_cc = $send_to_bcc = $subject = $body = $identity = '';
455 $mailprio = 3;
44560457 456
41b94d65 457 if ($passed_id) {
44560457 458 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
459 $imapPort, 0);
460
48985d59 461 sqimap_mailbox_select($imapConnection, $mailbox);
41b94d65 462 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
463 $body = '';
464 if ($passed_ent_id) {
465 /* redefine the messsage in case of message/rfc822 */
466 $message = $message->getEntity($passed_ent_id);
467 /* message is an entity which contains the envelope and type0=message
468 * and type1=rfc822. The actual entities are childs from
469 * $message->entities[0]. That's where the encoding and is located
470 */
471
472 $entities = $message->entities[0]->findDisplayEntity
473 (array(), $alt_order = array('text/plain'));
474 if (!count($entities)) {
475 $entities = $message->entities[0]->findDisplayEntity
476 (array(), $alt_order = array('text/plain','html/plain'));
477 }
478 $orig_header = $message->header; /* here is the envelope located */
479 /* redefine the message for picking up the attachments */
480 $message = $message->entities[0];
481
482 } else {
483 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain'));
484 if (!count($entities)) {
485 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain','html/plain'));
486 }
487 $orig_header = $message->header;
488 }
489 $encoding = $message->header->encoding;
490 $type0 = $message->type0;
491 $type1 = $message->type1;
492
493 foreach ($entities as $ent) {
494 $bodypart = decodeBody(
495 mime_fetch_body($imapConnection, $passed_id, $ent),
496 $encoding);
497 if ($type1 == 'html') {
498 $bodypart = strip_tags($bodypart);
499 }
500 $body .= $bodypart;
501 }
502 if ($default_use_priority) {
503 $mailprio = substr($orig_header->priority,0,1);
504 } else {
505 $mailprio = '';
506 }
507 ClearAttachments($session);
bdb92db3 508
509 $identity = '';
510 $idents = getPref($data_dir, $username, 'identities');
511 $from_o = $message->header->from;
512 if (is_object($from_o)) {
513 $orig_from = $from_o->getAddress();
514 } else {
515 $orig_from = '';
516 }
517 if (!empty($idents) && $idents > 1) {
518 for ($i = 1; $i < $idents; $i++) {
519 $enc_from_name = '"'.
520 getPref($data_dir,
521 $username,
522 'full_name' . $i) .
523 '" <' . getPref($data_dir, $username,
524 'email_address' . $i) . '>';
525 if ($enc_from_name == $orig_from) {
526 $identity = $i;
527 break;
528 }
529 }
530 }
531
41b94d65 532 switch ($action) {
533 case ('draft'):
534 $use_signature = FALSE;
535 $send_to = $orig_header->getAddr_s('to');
536 $send_to_cc = $orig_header->getAddr_s('cc');
537 $send_to_bcc = $orig_header->getAddr_s('bcc');
538 $subject = $orig_header->subject;
539
540 $body_ary = explode("\n", $body);
541 $cnt = count($body_ary) ;
542 $body = '';
543 for ($i=0; $i < $cnt; $i++) {
544 if (!ereg("^[>\\s]*$", $body_ary[$i])) {
545 sqWordWrap($body_ary[$i], $editor_size );
546 $body .= $body_ary[$i] . "\n";
dc197c8a 547 }
41b94d65 548 unset($body_ary[$i]);
549 }
550 sqUnWordWrap($body);
551 getAttachments($message, $session, $passed_id, $entities, $imapConnection);
552 break;
553 case ('edit_as_new'):
554 $send_to = $orig_header->getAddr_s('to');
555 $send_to_cc = $orig_header->getAddr_s('cc');
556 $send_to_bcc = $orig_header->getAddr_s('bcc');
557 $subject = $orig_header->subject;
558 $mailprio = $orig_header->priority;
bdb92db3 559 $orig_from = '';
41b94d65 560 getAttachments($message, $session, $passed_id, $entities, $imapConnection);
561 sqUnWordWrap($body);
562 break;
563 case ('forward'):
564 $send_to = '';
41b94d65 565 $subject = $orig_header->subject;
566 if ((substr(strtolower($subject), 0, 4) != 'fwd:') &&
567 (substr(strtolower($subject), 0, 5) != '[fwd:') &&
568 (substr(strtolower($subject), 0, 6) != '[ fwd:')) {
569 $subject = '[Fwd: ' . $subject . ']';
570 }
571 $body = getforwardHeader($orig_header) . $body;
572 sqUnWordWrap($body);
573 getAttachments($message, $session, $passed_id, $entities, $imapConnection);
574 break;
575 case ('reply' || 'reply_all'):
576 $send_to = $orig_header->reply_to;
577 if ($send_to) {
578 $send_to = $send_to->getAddress();
579 } else {
580 $send_to = $orig_header->from->getAddress();
581 }
41b94d65 582 $subject = $orig_header->subject;
583 $subject = str_replace('"', "'", $subject);
584 $subject = trim($subject);
585 if (substr(strtolower($subject), 0, 3) != 're:') {
586 $subject = 'Re: ' . $subject;
587 }
588 if ($action == 'reply_all') {
589 $send_to_cc = replyAllString($orig_header);
590 }
591 /* this corrects some wrapping/quoting problems on replies */
592 $rewrap_body = explode("\n", $body);
593 $body = getReplyCitation($orig_header->from->personal);
594 $cnt = count($rewrap_body);
595 for ($i=0;$i<$cnt;$i++) {
3f606b3f 596 sqWordWrap($rewrap_body[$i], ($editor_size - 2));
597 if (preg_match("/^(>+)/", $rewrap_body[$i], $matches)) {
598 $gt = $matches[1];
41b94d65 599 $body .= '>' . str_replace("\n", "\n$gt ", $rewrap_body[$i]) ."\n";
734f4ee6 600 } else {
41b94d65 601 $body .= '> ' . $rewrap_body[$i] . "\n";
602 }
603 unset($rewrap_body[$i]);
604 }
605 break;
606 default:
607 break;
608 }
44560457 609 sqimap_logout($imapConnection);
41b94d65 610 }
611 $ret = array(
612 'send_to' => $send_to,
613 'send_to_cc' => $send_to_cc,
614 'send_to_bcc' => $send_to_bcc,
615 'subject' => $subject,
616 'mailprio' => $mailprio,
bdb92db3 617 'body' => $body,
618 'identity' => $identity
41b94d65 619 );
620
621 return ($ret);
48985d59 622} /* function newMail() */
623
78509c54 624
41b94d65 625function getAttachments($message, $session, $passed_id, $entities, $imapConnection) {
626 global $attachments, $attachment_dir, $username;
627
48985d59 628 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
41b94d65 629 if (!count($message->entities) ||
630 ($message->type0 == 'message' && $message->type1 == 'rfc822')) {
631 if ( !in_array($message->entity_id, $entities) && $message->entity_id) {
632 if ($message->type0 == 'message' && $message->type1 == 'rfc822') {
633 $filename = decodeHeader($message->header->subject.'.eml');
634 if ($filename == "") {
635 $filename = "untitled-".$message->entity_id.'.eml';
636 }
637 } else {
638 $filename = decodeHeader($message->header->filename);
639 if ($filename == "") {
640 $filename = "untitled-".$message->entity_id;
641 }
48985d59 642 }
48985d59 643 $localfilename = GenerateRandomString(32, '', 7);
644 $full_localfilename = "$hashed_attachment_dir/$localfilename";
645 while (file_exists($full_localfilename)) {
646 $localfilename = GenerateRandomString(32, '', 7);
647 $full_localfilename = "$hashed_attachment_dir/$localfilename";
648 }
0a17f9dd 649
48985d59 650 $newAttachment = array();
651 $newAttachment['localfilename'] = $localfilename;
652 $newAttachment['remotefilename'] = $filename;
41b94d65 653 $newAttachment['type'] = strtolower($message->type0 .
654 '/' . $message->type1);
da95c4b6 655 $newAttachment['id'] = strtolower($message->header->id);
656 $newAttachment['session'] = $session;
48985d59 657
658 /* Write Attachment to file */
659 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'w');
660 fputs($fp, decodeBody(mime_fetch_body($imapConnection,
41b94d65 661 $passed_id, $message->entity_id),
48985d59 662 $message->header->encoding));
663 fclose ($fp);
664
665 $attachments[] = $newAttachment;
666 }
734f4ee6 667 } else {
48985d59 668 for ($i = 0; $i < count($message->entities); $i++) {
41b94d65 669 getAttachments($message->entities[$i], $session, $passed_id, $entities, $imapConnection);
48985d59 670 }
671 }
672 return;
673}
674
41b94d65 675function showInputForm ($session, $values=false) {
676 global $send_to, $send_to_cc, $body,
48985d59 677 $passed_body, $color, $use_signature, $signature, $prefix_sig,
678 $editor_size, $attachments, $subject, $newmail,
41b94d65 679 $use_javascript_addr_book, $send_to_bcc, $passed_id, $mailbox,
48985d59 680 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
681 $username, $data_dir, $identity, $draft_id, $delete_draft,
9c3e6cd4 682 $mailprio, $default_use_mdn, $mdn_user_support, $compose_new_win,
44560457 683 $saved_draft, $mail_sent, $sig_first, $edit_as_new, $action,
684 $username;
48985d59 685
3b487216 686 $subject = decodeHeader($subject, false);
41b94d65 687 if ($values) {
688 $send_to = $values['send_to'];
689 $send_to_cc = $values['send_to_cc'];
690 $send_to_bcc = $values['send_to_bcc'];
691 $subject = $values['subject'];
692 $mailprio = $values['mailprio'];
693 $body = $values['body'];
bdb92db3 694 $identity = $values['identity'];
41b94d65 695 }
696
48985d59 697 if ($use_javascript_addr_book) {
698 echo "\n". '<SCRIPT LANGUAGE=JavaScript><!--' . "\n" .
699 'function open_abook() { ' . "\n" .
700 ' var nwin = window.open("addrbook_popup.php","abookpopup",' .
701 '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" .
702 ' if((!nwin.opener) && (document.windows != null))' . "\n" .
703 ' nwin.opener = document.windows;' . "\n" .
704 "}\n" .
705 '// --></SCRIPT>' . "\n\n";
706 }
707
41b94d65 708 echo "\n" . '<FORM name=compose action="compose.php" METHOD=POST ' .
709 'ENCTYPE="multipart/form-data"';
48985d59 710 do_hook("compose_form");
e02775fe 711
57257333 712
48985d59 713 echo ">\n";
714
41b94d65 715 if ($action == 'draft') {
716 echo '<input type="hidden" name="delete_draft" value="' . $passed_id . "\">\n";
48985d59 717 }
718 if (isset($delete_draft)) {
719 echo '<input type="hidden" name="delete_draft" value="' . $delete_draft. "\">\n";
720 }
da95c4b6 721 if (isset($session)) {
44560457 722 echo '<input type="hidden" name="session" value="' . $session . "\">\n";
da95c4b6 723 }
724
44560457 725
9c3e6cd4 726 if ($saved_draft == 'yes') {
727 echo '<BR><CENTER><B>'. _("Draft Saved").'</CENTER></B>';
728 }
729 if ($mail_sent == 'yes') {
730 echo '<BR><CENTER><B>'. _("Your Message has been sent").'</CENTER></B>';
731 }
41b94d65 732 echo '<TABLE WIDTH="100%" ALIGN=center CELLSPACING=0 BORDER=0>' . "\n";
9c3e6cd4 733 if ($compose_new_win == '1') {
41b94d65 734 echo '<TABLE ALIGN=CENTER BGCOLOR="'.$color[0].'" WIDTH="100%" BORDER=0>'."\n";
735 echo ' <TR><TD></TD><TD ALIGN="RIGHT"><INPUT TYPE="BUTTON" NAME="Close" onClick="return self.close()" VALUE='._("Close").'></TD></TR>'."\n";
9c3e6cd4 736 }
78a35fcd 737 if ($location_of_buttons == 'top') {
738 showComposeButtonRow();
739 }
48985d59 740
715225af 741 $idents = getPref($data_dir, $username, 'identities', 0);
742 if ($idents > 1) {
41b94d65 743 echo ' <TR>' . "\n" .
744 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' .
745 "\n" .
746 _("From:") .
747 ' </TD><TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
48985d59 748 '<select name=identity>' . "\n" .
749 '<option value=default>' .
750 htmlspecialchars(getPref($data_dir, $username, 'full_name'));
751 $em = getPref($data_dir, $username, 'email_address');
752 if ($em != '') {
248bfebb 753 echo htmlspecialchars(' <' . $em . '>') . "\n";
48985d59 754 }
755 for ($i = 1; $i < $idents; $i ++) {
248bfebb 756 echo '<option value="' . $i . '"';
48985d59 757 if (isset($identity) && $identity == $i) {
78a35fcd 758 echo ' SELECTED';
48985d59 759 }
760 echo '>' . htmlspecialchars(getPref($data_dir, $username,
761 'full_name' . $i));
248bfebb 762 $em = getPref($data_dir, $username, 'email_address' . $i);
48985d59 763 if ($em != '') {
78a35fcd 764 echo htmlspecialchars(' <' . $em . '>') . "\n";
48985d59 765 }
9f599fe3 766 echo '</option>';
48985d59 767 }
768 echo '</select>' . "\n" .
41b94d65 769 ' </TD>' . "\n" .
770 ' </TR>' . "\n";
771 }
772 echo ' <TR>' . "\n" .
773 ' <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' . "\n" .
774 _("To:") .
775 ' </TD><TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
776 ' <INPUT TYPE=text NAME="send_to" VALUE="' .
777 htmlspecialchars($send_to) . '" SIZE=60><BR>' . "\n" .
778 ' </TD>' . "\n" .
779 ' </TR>' . "\n" .
780 ' <TR>' . "\n" .
781 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
782 _("CC:") .
783 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
784 ' <INPUT TYPE=text NAME="send_to_cc" SIZE=60 VALUE="' .
785 htmlspecialchars($send_to_cc) . '"><BR>' . "\n" .
786 ' </TD>' . "\n" .
787 ' </TR>' . "\n" .
788 ' <TR>' . "\n" .
789 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
790 _("BCC:") .
791 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
792 ' <INPUT TYPE=text NAME="send_to_bcc" VALUE="' .
793 htmlspecialchars($send_to_bcc) . '" SIZE=60><BR>' . "\n" .
794 '</TD></TR>' . "\n" .
795 ' <TR>' . "\n" .
796 ' <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' . "\n" .
797 _("Subject:") .
798 ' </TD><TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n";
799 echo ' <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
800 htmlspecialchars($subject) . '">';
801 echo '</td></tr>' . "\n\n";
48985d59 802
78a35fcd 803 if ($location_of_buttons == 'between') {
804 showComposeButtonRow();
805 }
fdc83c55 806 if ($compose_new_win == '1') {
41b94d65 807 echo ' <TR>' . "\n" .
808 ' <TD BGCOLOR="' . $color[0] . '" COLSPAN=2 ALIGN=CENTER>' . "\n" .
809 ' <TEXTAREA NAME=body ROWS=20 COLS="' .
810 $editor_size . '" WRAP="VIRTUAL">';
fdc83c55 811 }
812 else {
41b94d65 813 echo ' <TR>' . "\n" .
814 ' <TD BGCOLOR="' . $color[4] . '" COLSPAN=2>' . "\n" .
815 ' &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS="' .
816 $editor_size . '" WRAP="VIRTUAL">';
fdc83c55 817 }
48985d59 818 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
3b17e952 819 if ($sig_first == '1') {
820 echo "\n\n".($prefix_sig==true? "-- \n":'').htmlspecialchars($signature);
821 echo "\n\n".htmlspecialchars($body);
822 }
823 else {
824 echo "\n\n".htmlspecialchars($body);
825 echo "\n\n".($prefix_sig==true? "-- \n":'').htmlspecialchars($signature);
826 }
827 }
828 else {
829 echo htmlspecialchars($body);
48985d59 830 }
41b94d65 831 echo '</TEXTAREA><BR>' . "\n" .
832 ' </TD>' . "\n" .
833 ' </TR>' . "\n";
48985d59 834
835 if ($location_of_buttons == 'bottom') {
836 showComposeButtonRow();
837 } else {
41b94d65 838 echo ' <TR><TD COLSPAN=2 ALIGN=LEFT>';
839 echo ' &nbsp; <INPUT TYPE=SUBMIT NAME=send VALUE="' . _("Send") . '"></TD></TR>' . "\n";
48985d59 840 }
46bb8da8 841
48985d59 842 /* This code is for attachments */
41b94d65 843 echo ' <TR>' . "\n" .
844 ' <TD VALIGN=MIDDLE ALIGN=RIGHT>' . "\n" .
845 _("Attach:") .
846 ' </TD>' . "\n" .
847 ' <TD VALIGN=MIDDLE ALIGN=LEFT>' . "\n" .
848 ' <INPUT NAME="attachfile" SIZE=48 TYPE="file">' . "\n" .
849 ' &nbsp;&nbsp;<input type="submit" name="attach"' .
850 ' value="' . _("Add") .'">' . "\n" .
851 ' </TD>' . "\n" .
852 ' </TR>' . "\n";
853
854 if (count($attachments)) {
855 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
856 echo '<tr><td bgcolor="' . $color[0] . '" align=right>' . "\n" .
857 '&nbsp;' .
858 '</td><td align=left bgcolor="' . $color[0] . '">';
859 foreach ($attachments as $key => $info) {
860 if ($info['session'] == $session) {
861 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
862 echo '<input type="checkbox" name="delete[]" value="' . $key . "\">\n" .
863 $info['remotefilename'] . ' - ' . $info['type'] . ' (' .
864 show_readable_size( filesize( $attached_file ) ) . ")<br>\n";
865 }
866 }
867
868 echo '<input type="submit" name="do_delete" value="' .
869 _("Delete selected attachments") . "\">\n" .
870 '</td></tr>';
871 }
872 /* End of attachment code */
07687736 873 if ($compose_new_win == '1') {
41b94d65 874 echo '</TABLE>'."\n";
07687736 875 }
41b94d65 876 echo '</TABLE>' . "\n";
44560457 877
878 echo '<input type="hidden" name="username" value="'. $username . "\">\n";
879 echo '<input type=hidden name=action value=' . $action . ">\n";
48985d59 880 echo '<INPUT TYPE=hidden NAME=mailbox VALUE="' . htmlspecialchars($mailbox) .
881 "\">\n" .
882 '</FORM>';
9f599fe3 883 do_hook('compose_bottom');
48985d59 884 echo '</BODY></HTML>' . "\n";
885}
886
887
70c4fd84 888function showComposeButtonRow() {
78a35fcd 889 global $use_javascript_addr_book, $save_as_draft,
70c4fd84 890 $default_use_priority, $mailprio, $default_use_mdn,
b2a7e5bc 891 $request_mdn, $request_dr,
70c4fd84 892 $data_dir, $username;
893
41b94d65 894 echo " <TR><TD>\n</TD><TD>\n";
ae25968c 895 if ($default_use_priority) {
896 if(!isset($mailprio)) {
897 $mailprio = "3";
70c4fd84 898 }
899 echo _("Priority") .': <select name="mailprio">'.
900 '<option value="1"'.($mailprio=='1'?' selected':'').'>'. _("High") .'</option>'.
901 '<option value="3"'.($mailprio=='3'?' selected':'').'>'. _("Normal") .'</option>'.
902 '<option value="5"'.($mailprio=='5'?' selected':'').'>'. _("Low").'</option>'.
903 "</select>";
ae25968c 904 }
905 $mdn_user_support=getPref($data_dir, $username, 'mdn_user_support',$default_use_mdn);
906 if ($default_use_mdn) {
70c4fd84 907 if ($mdn_user_support) {
908 echo "\n\t". _("Receipt") .': '.
b2a7e5bc 909 '<input type="checkbox" name="request_mdn" value=1'.
910 ($request_mdn=='1'?' checked':'') .'>'. _("On read").
911 ' <input type="checkbox" name="request_dr" value=1'.
912 ($request_dr=='1'?' checked':'') .'>'. _("On Delivery");
70c4fd84 913 }
ae25968c 914 }
48985d59 915
41b94d65 916 echo " </td></tr>\n <TR><td>\n </td><td>\n";
01265fba 917 echo "\n <INPUT TYPE=SUBMIT NAME=\"sigappend\" VALUE=\"". _("Signature") . "\">\n";
78a35fcd 918 if ($use_javascript_addr_book) {
46bb8da8 919 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"".
920 " <input type=button value=\\\""._("Addresses").
921 "\\\" onclick='javascript:open_abook();'>\");".
922 " // --></SCRIPT><NOSCRIPT>\n".
923 " <input type=submit name=\"html_addr_search\" value=\"".
924 _("Addresses")."\">".
925 " </NOSCRIPT>\n";
734f4ee6 926 } else {
78a35fcd 927 echo " <input type=submit name=\"html_addr_search\" value=\"".
928 _("Addresses")."\">";
929 }
930 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
48985d59 931
78a35fcd 932 if ($save_as_draft) {
933 echo '<input type="submit" name ="draft" value="' . _("Save Draft") . "\">\n";
934 }
0a17f9dd 935
78a35fcd 936 do_hook('compose_button_row');
441f2d33 937
41b94d65 938 echo " </TD></TR>\n\n";
78a35fcd 939}
b278172f 940
70c4fd84 941function checkInput ($show) {
78a35fcd 942 /*
943 * I implemented the $show variable because the error messages
944 * were getting sent before the page header. So, I check once
945 * using $show=false, and then when i'm ready to display the error
946 * message, show=true
947 */
948 global $body, $send_to, $subject, $color;
949
950 if ($send_to == "") {
951 if ($show) {
0ad7dbda 952 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
78a35fcd 953 }
954 return false;
955 }
956 return true;
957} /* function checkInput() */
df15de21 958
3806fa52 959
00793a25 960/* True if FAILURE */
da95c4b6 961function saveAttachedFiles($session) {
4c9d2242 962 global $HTTP_POST_FILES, $attachment_dir, $attachments, $username;
963
964 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
965 $localfilename = GenerateRandomString(32, '', 7);
966 $full_localfilename = "$hashed_attachment_dir/$localfilename";
967 while (file_exists($full_localfilename)) {
968 $localfilename = GenerateRandomString(32, '', 7);
969 $full_localfilename = "$hashed_attachment_dir/$localfilename";
970 }
971
972 if (!@rename($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
ceca62d3 973 if (function_exists("move_uploaded_file")) {
974 if (!@move_uploaded_file($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
056ddad7 975 return true;
ceca62d3 976 }
977 } else {
978
979 if (!@copy($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
980 return true;
981 }
982 }
983
4c9d2242 984 }
4c9d2242 985 $newAttachment['localfilename'] = $localfilename;
986 $newAttachment['remotefilename'] = $HTTP_POST_FILES['attachfile']['name'];
987 $newAttachment['type'] = strtolower($HTTP_POST_FILES['attachfile']['type']);
da95c4b6 988 $newAttachment['session'] = $session;
8ef72f33 989
4c9d2242 990 if ($newAttachment['type'] == "") {
8ef72f33 991 $newAttachment['type'] = 'application/octet-stream';
056ddad7 992 }
9487c2ff 993
4c9d2242 994 $attachments[] = $newAttachment;
995}
996
4c9d2242 997
da95c4b6 998function ClearAttachments($session)
4c9d2242 999{
1000 global $username, $attachments, $attachment_dir;
1001 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1002
da95c4b6 1003 $rem_attachments = array();
8712abea 1004 if (is_array($attachments)) {
1005 foreach ($attachments as $info) {
1006 if ($info['session'] == $session) {
1007 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
1008 if (file_exists($attached_file)) {
1009 unlink($attached_file);
1010 }
1011 }
1012 else {
1013 $rem_attachments[] = $info;
1014 }
1015 }
da95c4b6 1016 }
1017 $attachments = $rem_attachments;
4c9d2242 1018}
1019
da95c4b6 1020
4c9d2242 1021function getReplyCitation($orig_from)
1022{
1023 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
1024
1025 /* First, return an empty string when no citation style selected. */
1026 if (($reply_citation_style == '') || ($reply_citation_style == 'none')) {
1027 return '';
1028 }
1029
4c9d2242 1030 /* Make sure our final value isn't an empty string. */
1031 if ($orig_from == '') {
1032 return '';
1033 }
1034
1035 /* Otherwise, try to select the desired citation style. */
1036 switch ($reply_citation_style) {
1037 case 'author_said':
1038 $start = '';
1039 $end = ' ' . _("said") . ':';
1040 break;
1041 case 'quote_who':
1042 $start = '<' . _("quote") . ' ' . _("who") . '="';
1043 $end = '">';
1044 break;
1045 case 'user-defined':
1ecabe54 1046 $start = $reply_citation_start . ' ';
4c9d2242 1047 $end = $reply_citation_end;
1048 break;
1049 default:
1050 return '';
1051 }
1052
1053 /* Build and return the citation string. */
1054 return ($start . $orig_from . $end . "\n");
1055}
1056
5e9e90fd 1057?>