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