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