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