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