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