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