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