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