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