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