Missed a spelling change
[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);
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;
09044055 77}
78
41b94d65 79function getforwardHeader($orig_header) {
19c6f7a7 80 global $editor_size;
81
a45887d7 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 }
19c6f7a7 94 $bodyTop = str_pad(' '._("Original Message").' ',$editor_size -2,'-',STR_PAD_BOTH);
c4d02444 95 $bodyTop .= "\n". $display[_("Subject")] . decodeHeader($orig_header->subject) . "\n" .
96 $display[_("From")] . decodeHeader($orig_header->getAddr_s('from',"\n$indent")) . "\n" .
a45887d7 97 $display[_("Date")] . getLongDateString( $orig_header->date ). "\n" .
c4d02444 98 $display[_("To")] . decodeHeader($orig_header->getAddr_s('to',"\n$indent")) ."\n";
41b94d65 99 if ($orig_header->cc != array() && $orig_header->cc !='') {
c4d02444 100 $bodyTop .= $display[_("Cc")] . decodeHeader($orig_header->getAddr_s('cc',"\n$indent")) . "\n";
41b94d65 101 }
19c6f7a7 102 $bodyTop .= str_pad('', $editor_size -2 , '-');
41b94d65 103 $bodyTop .= "\n";
104 return $bodyTop;
105}
09044055 106/* ----------------------------------------------------------------------- */
107
44560457 108/*
109 * If the session is expired during a post this restores the compose session
110 * vars.
111 */
112//$session_expired = false;
113if (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');
3f6b1b6f 137 if (!isset($mailbox)) {
138 $mailbox = '';
139 }
44560457 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
48985d59 149if (!isset($attachments)) {
150 $attachments = array();
151 session_register('attachments');
152}
153
da95c4b6 154if (!isset($composesession)) {
155 $composesession = 0;
156 session_register('composesession');
157}
158
d7f8e6e6 159if (!isset($session) || (isset($newmessage) && $newmessage)) {
da95c4b6 160 $session = "$composesession" +1;
91f2085b 161 $composesession = $session;
162 session_register('composesession');
d7f8e6e6 163}
da95c4b6 164
00793a25 165if (!isset($mailbox) || $mailbox == '' || ($mailbox == 'None')) {
166 $mailbox = 'INBOX';
167}
168
169if (isset($draft)) {
715225af 170 include_once ('../src/draft_actions.php');
3f6b1b6f 171 if (! isset($passed_id)) {
172 $passed_id = 0;
113e5e9d 173 }
7058a2a9 174 if (! isset($MDN)) {
175 $MDN = 'False';
113e5e9d 176 }
fa77d354 177 if (! isset($mailprio)) {
178 $mailprio = '';
179 }
3f6b1b6f 180 if (!saveMessageAsDraft($send_to, $send_to_cc, $send_to_bcc, $subject, $body, $passed_id, $mailprio, $session)) {
da95c4b6 181 showInputForm($session);
00793a25 182 exit();
734f4ee6 183 } else {
00793a25 184 $draft_message = _("Draft Email Saved");
185 /* If this is a resumed draft, then delete the original */
186 if(isset($delete_draft)) {
7058a2a9 187 Header("Location: delete_message.php?mailbox=" . urlencode($draft_folder) .
fae72101 188 "&message=$delete_draft&sort=$sort&startMessage=1&saved_draft=yes");
00793a25 189 exit();
7058a2a9 190 }
9c3e6cd4 191 else {
192 if ($compose_new_win == '1') {
da95c4b6 193 Header("Location: compose.php?saved_draft=yes&session=$composesession");
9c3e6cd4 194 exit();
195 }
196 else {
fae72101 197 Header("Location: right_main.php?mailbox=$draft_folder&sort=$sort".
2017ebeb 198 "&startMessage=1&note=".urlencode($draft_message));
00793a25 199 exit();
9c3e6cd4 200 }
00793a25 201 }
202 }
203}
204
205if (isset($send)) {
206 if (isset($HTTP_POST_FILES['attachfile']) &&
207 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
208 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none') {
da95c4b6 209 $AttachFailure = saveAttachedFiles($session);
00793a25 210 }
211 if (checkInput(false) && !isset($AttachFailure)) {
212 $urlMailbox = urlencode (trim($mailbox));
3f6b1b6f 213 if (! isset($passed_id)) {
214 $passed_id = 0;
00793a25 215 }
216 /*
217 * Set $default_charset to correspond with the user's selection
7058a2a9 218 * of language interface.
00793a25 219 */
220 set_my_charset();
221
222 /*
223 * This is to change all newlines to \n
7058a2a9 224 * We'll change them to \r\n later (in the sendMessage function)
00793a25 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
f302d704 232 * if the browser doesn't support "VIRTUAL" as the wrap type.
00793a25 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";
734f4ee6 242 } else {
e0858036 243 sqWordWrap($line, $editor_size);
244 $newBody .= $line . "\n";
00793a25 245 }
246 }
247 $body = $newBody;
248
e02775fe 249 do_hook('compose_send');
250
57257333 251 $MDN = False; // we are not sending a mdn response
00793a25 252 if (! isset($mailprio)) {
253 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
3f6b1b6f 254 $subject, $body, $passed_id, $MDN, '', $session);
734f4ee6 255 } else {
00793a25 256 $Result = sendMessage($send_to, $send_to_cc, $send_to_bcc,
3f6b1b6f 257 $subject, $body, $passed_id, $MDN, $mailprio, $session);
00793a25 258 }
259 if (! $Result) {
da95c4b6 260 showInputForm($session);
00793a25 261 exit();
262 }
263 if ( isset($delete_draft)) {
7058a2a9 264 Header("Location: delete_message.php?mailbox=" . urlencode( $draft_folder ).
fae72101 265 "&message=$delete_draft&sort=$sort&startMessage=1&mail_sent=yes");
00793a25 266 exit();
267 }
9c3e6cd4 268 if ($compose_new_win == '1') {
d7f8e6e6 269 Header("Location: compose.php?mail_sent=yes");
9c3e6cd4 270 }
271 else {
fae72101 272 Header("Location: right_main.php?mailbox=$urlMailbox&sort=$sort".
273 "&startMessage=1");
9c3e6cd4 274 }
734f4ee6 275 } else {
00793a25 276 /*
277 *$imapConnection = sqimap_login($username, $key, $imapServerAddress,
278 * $imapPort, 0);
279 */
9c3e6cd4 280 if ($compose_new_win == '1') {
281 compose_Header($color, $mailbox);
282 }
283 else {
284 displayPageHeader($color, $mailbox);
285 }
00793a25 286 if (isset($AttachFailure)) {
287 plain_error_message(_("Could not move/copy file. File not attached"),
288 $color);
289 }
00793a25 290 checkInput(true);
da95c4b6 291 showInputForm($session);
00793a25 292 /* sqimap_logout($imapConnection); */
293 }
e02775fe 294} elseif (isset($html_addr_search_done)) {
9c3e6cd4 295 if ($compose_new_win == '1') {
296 compose_Header($color, $mailbox);
297 }
298 else {
299 displayPageHeader($color, $mailbox);
300 }
00793a25 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 }
da95c4b6 324 showInputForm($session);
e02775fe 325} elseif (isset($html_addr_search)) {
00793a25 326 if (isset($HTTP_POST_FILES['attachfile']) &&
327 $HTTP_POST_FILES['attachfile']['tmp_name'] &&
328 $HTTP_POST_FILES['attachfile']['tmp_name'] != 'none') {
da95c4b6 329 if (saveAttachedFiles($session)) {
00793a25 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');
e02775fe 338} elseif (isset($attach)) {
da95c4b6 339 if (saveAttachedFiles($session)) {
00793a25 340 plain_error_message(_("Could not move/copy file. File not attached"), $color);
341 }
9c3e6cd4 342 if ($compose_new_win == '1') {
343 compose_Header($color, $mailbox);
344 }
345 else {
346 displayPageHeader($color, $mailbox);
347 }
da95c4b6 348 showInputForm($session);
01265fba 349}
350elseif (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 }
da95c4b6 366 showInputForm($session);
e02775fe 367} elseif (isset($do_delete)) {
9c3e6cd4 368 if ($compose_new_win == '1') {
369 compose_Header($color, $mailbox);
370 }
371 else {
372 displayPageHeader($color, $mailbox);
373 }
00793a25 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'];
da95c4b6 380 unlink ($attached_file);
381 unset ($attachments[$index]);
00793a25 382 }
3f6b1b6f 383 setPref($data_dir, $username, 'attachments', serialize($attachments));
00793a25 384 }
385
da95c4b6 386 showInputForm($session);
734f4ee6 387} else {
00793a25 388 /*
389 * This handles the default case as well as the error case
390 * (they had the same code) --> if (isset($smtpErrors))
391 */
44560457 392
393 if ($compose_new_win == '1') {
394 compose_Header($color, $mailbox);
395 } else {
396 displayPageHeader($color, $mailbox);
397 }
00793a25 398
399 $newmail = true;
400
41b94d65 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
44560457 406 $values = newMail($mailbox,$passed_id,$passed_ent_id, $action, $session);
b9928adc 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)) {
44560457 413 $values['send_to_cc'] = $send_to_cc;
b9928adc 414 }
415 if (isset($send_to_bcc)) {
44560457 416 $values['send_to_bcc'] = $send_to_bcc;
b9928adc 417 }
41b94d65 418 showInputForm($session, $values);
00793a25 419}
420
421exit();
422
00793a25 423/**************** Only function definitions go below *************/
424
425
48985d59 426/* This function is used when not sending or adding attachments */
44560457 427function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $session='') {
91f2085b 428 global $editor_size, $default_use_priority, $body,
44560457 429 $use_signature, $composesession, $data_dir, $username,
430 $username, $key, $imapServerAddress, $imapPort;
e7f1a81d 431
91f2085b 432 $send_to = $send_to_cc = $send_to_bcc = $subject = $identity = '';
bdb92db3 433 $mailprio = 3;
44560457 434
41b94d65 435 if ($passed_id) {
44560457 436 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
437 $imapPort, 0);
438
48985d59 439 sqimap_mailbox_select($imapConnection, $mailbox);
41b94d65 440 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
91f2085b 441 $body = '';
41b94d65 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 }
a45887d7 456 $orig_header = $message->rfc822_header; /* here is the envelope located */
41b94d65 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 }
a45887d7 465 $orig_header = $message->rfc822_header;
41b94d65 466 }
467 $encoding = $message->header->encoding;
468 $type0 = $message->type0;
469 $type1 = $message->type1;
41b94d65 470 foreach ($entities as $ent) {
c17daaba 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);
41b94d65 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);
a45887d7 482 if (!$mailprio) {
483 $mailprio = 3;
484 }
41b94d65 485 } else {
486 $mailprio = '';
487 }
488 ClearAttachments($session);
bdb92db3 489
490 $identity = '';
491 $idents = getPref($data_dir, $username, 'identities');
a45887d7 492 $from_o = $orig_header->from;
bdb92db3 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
41b94d65 513 switch ($action) {
a45887d7 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++) {
d1ae0f19 525 if (!ereg("^[>\\s]*$", $body_ary[$i]) || !$body_ary[$i]) {
a45887d7 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) . $body;
553 sqUnWordWrap($body);
a6ec592e 554 getAttachments($message, $session, $passed_id, $entities, $imapConnection);
555 break;
556 case ('forward_as_attachment'):
756406df 557 getMessage_RFC822_Attachment($message, $session, $passed_id, $passed_ent_id, $imapConnection);
a6ec592e 558 $body = '';
a45887d7 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", $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;
41b94d65 592 }
44560457 593 sqimap_logout($imapConnection);
41b94d65 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,
bdb92db3 601 'body' => $body,
602 'identity' => $identity
41b94d65 603 );
604
605 return ($ret);
48985d59 606} /* function newMail() */
607
78509c54 608
41b94d65 609function getAttachments($message, $session, $passed_id, $entities, $imapConnection) {
3f6b1b6f 610 global $attachments, $attachment_dir, $username, $data_dir;
41b94d65 611
48985d59 612 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
41b94d65 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') {
a45887d7 617 $filename = decodeHeader($message->rfc822_header->subject.'.eml');
41b94d65 618 if ($filename == "") {
619 $filename = "untitled-".$message->entity_id.'.eml';
620 }
621 } else {
532160a5 622 $filename = decodeHeader($message->header->disposition->getProperty('filename'));
54555d00 623 if ($filename == '') {
532160a5 624 $name = decodeHeader($message->header->disposition->getProperty('name'));
54555d00 625 if ($name == '') {
626 $filename = "untitled-".$message->entity_id;
627 } else {
628 $filename = $name;
629 }
41b94d65 630 }
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);
da95c4b6 644 $newAttachment['id'] = strtolower($message->header->id);
645 $newAttachment['session'] = $session;
48985d59 646
647 /* Write Attachment to file */
648 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'w');
649 fputs($fp, decodeBody(mime_fetch_body($imapConnection,
41b94d65 650 $passed_id, $message->entity_id),
48985d59 651 $message->header->encoding));
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) {
669 $body_a = sqimap_run_command($imapConnection,
670 'FETCH '.$passed_id.' RFC822',
671 true, $response, $readmessage, $uid_support);
672 } else {
673 $body_a = sqimap_run_command($imapConnection,
674 'FETCH '.$passed_id.' BODY['.$passed_ent_id.']',
675 true, $response, $readmessage, $uid_support);
676 $message = $message->parent;
677 }
a6ec592e 678 if ($response = 'OK') {
679 $subject = encodeHeader($message->rfc822_header->subject);
680 array_shift($body_a);
681 $body = implode('', $body_a);
682 $body .= "\r\n";
683
684 $localfilename = GenerateRandomString(32, 'FILE', 7);
685 $full_localfilename = "$hashed_attachment_dir/$localfilename";
686
687 $fp = fopen( $full_localfilename, 'w');
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;
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,
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 }
41b94d65 761 echo '<TABLE WIDTH="100%" ALIGN=center CELLSPACING=0 BORDER=0>' . "\n";
9c3e6cd4 762 if ($compose_new_win == '1') {
41b94d65 763 echo '<TABLE ALIGN=CENTER BGCOLOR="'.$color[0].'" WIDTH="100%" BORDER=0>'."\n";
764 echo ' <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') {
849 echo "\n\n".($prefix_sig==true? "-- \n":'').htmlspecialchars($signature);
850 echo "\n\n".htmlspecialchars($body);
851 }
852 else {
853 echo "\n\n".htmlspecialchars($body);
854 echo "\n\n".($prefix_sig==true? "-- \n":'').htmlspecialchars($signature);
855 }
856 }
857 else {
858 echo htmlspecialchars($body);
48985d59 859 }
41b94d65 860 echo '</TEXTAREA><BR>' . "\n" .
861 ' </TD>' . "\n" .
862 ' </TR>' . "\n";
48985d59 863
864 if ($location_of_buttons == 'bottom') {
865 showComposeButtonRow();
866 } else {
41b94d65 867 echo ' <TR><TD COLSPAN=2 ALIGN=LEFT>';
868 echo ' &nbsp; <INPUT TYPE=SUBMIT NAME=send VALUE="' . _("Send") . '"></TD></TR>' . "\n";
48985d59 869 }
46bb8da8 870
48985d59 871 /* This code is for attachments */
91f2085b 872 echo '<table width="100%" cellpadding="0" cellspacing="4" align="center" border="0">';
873 echo ' <tr><td>';
874 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
875 echo ' <tr><td>';
876 echo ' <table width="100%" cellpadding="3" cellspacing="0" align="center" border="0">';
877
878
41b94d65 879 echo ' <TR>' . "\n" .
880 ' <TD VALIGN=MIDDLE ALIGN=RIGHT>' . "\n" .
881 _("Attach:") .
882 ' </TD>' . "\n" .
883 ' <TD VALIGN=MIDDLE ALIGN=LEFT>' . "\n" .
884 ' <INPUT NAME="attachfile" SIZE=48 TYPE="file">' . "\n" .
885 ' &nbsp;&nbsp;<input type="submit" name="attach"' .
886 ' value="' . _("Add") .'">' . "\n" .
887 ' </TD>' . "\n" .
888 ' </TR>' . "\n";
91f2085b 889
41b94d65 890
91f2085b 891 $s_a = array();
892 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
893 foreach ($attachments as $key => $info) {
894 if ($info['session'] == $session) {
895 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
896 $s_a[] = '<input type="checkbox" name="delete[]" value="' . $key . "\">\n" .
897 $info['remotefilename'] . ' - ' . $info['type'] . ' (' .
898 show_readable_size( filesize( $attached_file ) ) . ")<br>\n";
899 }
900 }
901 if (count($s_a)) {
902 foreach ($s_a as $s) {
903 echo '<tr><td align=left colspan="2" bgcolor="' . $color[0] . '">'.$s.'</td></tr>';
904 }
905 echo '<tr><td colspan="2"><input type="submit" name="do_delete" value="' .
906 _("Delete selected attachments") . "\">\n" .
907 '</td></tr>';
908 }
909 echo ' </table></td></tr>';
910 echo ' </table>';
911 echo ' </td></tr>';
41b94d65 912
41b94d65 913 /* End of attachment code */
07687736 914 if ($compose_new_win == '1') {
41b94d65 915 echo '</TABLE>'."\n";
07687736 916 }
41b94d65 917 echo '</TABLE>' . "\n";
44560457 918
919 echo '<input type="hidden" name="username" value="'. $username . "\">\n";
920 echo '<input type=hidden name=action value=' . $action . ">\n";
48985d59 921 echo '<INPUT TYPE=hidden NAME=mailbox VALUE="' . htmlspecialchars($mailbox) .
922 "\">\n" .
923 '</FORM>';
9f599fe3 924 do_hook('compose_bottom');
48985d59 925 echo '</BODY></HTML>' . "\n";
926}
927
928
70c4fd84 929function showComposeButtonRow() {
78a35fcd 930 global $use_javascript_addr_book, $save_as_draft,
70c4fd84 931 $default_use_priority, $mailprio, $default_use_mdn,
b2a7e5bc 932 $request_mdn, $request_dr,
70c4fd84 933 $data_dir, $username;
934
41b94d65 935 echo " <TR><TD>\n</TD><TD>\n";
ae25968c 936 if ($default_use_priority) {
937 if(!isset($mailprio)) {
938 $mailprio = "3";
70c4fd84 939 }
940 echo _("Priority") .': <select name="mailprio">'.
941 '<option value="1"'.($mailprio=='1'?' selected':'').'>'. _("High") .'</option>'.
942 '<option value="3"'.($mailprio=='3'?' selected':'').'>'. _("Normal") .'</option>'.
943 '<option value="5"'.($mailprio=='5'?' selected':'').'>'. _("Low").'</option>'.
944 "</select>";
ae25968c 945 }
946 $mdn_user_support=getPref($data_dir, $username, 'mdn_user_support',$default_use_mdn);
947 if ($default_use_mdn) {
70c4fd84 948 if ($mdn_user_support) {
949 echo "\n\t". _("Receipt") .': '.
b2a7e5bc 950 '<input type="checkbox" name="request_mdn" value=1'.
951 ($request_mdn=='1'?' checked':'') .'>'. _("On read").
952 ' <input type="checkbox" name="request_dr" value=1'.
953 ($request_dr=='1'?' checked':'') .'>'. _("On Delivery");
70c4fd84 954 }
ae25968c 955 }
48985d59 956
41b94d65 957 echo " </td></tr>\n <TR><td>\n </td><td>\n";
01265fba 958 echo "\n <INPUT TYPE=SUBMIT NAME=\"sigappend\" VALUE=\"". _("Signature") . "\">\n";
78a35fcd 959 if ($use_javascript_addr_book) {
46bb8da8 960 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"".
961 " <input type=button value=\\\""._("Addresses").
962 "\\\" onclick='javascript:open_abook();'>\");".
963 " // --></SCRIPT><NOSCRIPT>\n".
964 " <input type=submit name=\"html_addr_search\" value=\"".
965 _("Addresses")."\">".
966 " </NOSCRIPT>\n";
734f4ee6 967 } else {
78a35fcd 968 echo " <input type=submit name=\"html_addr_search\" value=\"".
969 _("Addresses")."\">";
970 }
971 echo "\n <INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">\n";
48985d59 972
78a35fcd 973 if ($save_as_draft) {
974 echo '<input type="submit" name ="draft" value="' . _("Save Draft") . "\">\n";
975 }
0a17f9dd 976
78a35fcd 977 do_hook('compose_button_row');
441f2d33 978
41b94d65 979 echo " </TD></TR>\n\n";
78a35fcd 980}
b278172f 981
70c4fd84 982function checkInput ($show) {
78a35fcd 983 /*
984 * I implemented the $show variable because the error messages
985 * were getting sent before the page header. So, I check once
986 * using $show=false, and then when i'm ready to display the error
987 * message, show=true
988 */
989 global $body, $send_to, $subject, $color;
990
991 if ($send_to == "") {
992 if ($show) {
0ad7dbda 993 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
78a35fcd 994 }
995 return false;
996 }
997 return true;
998} /* function checkInput() */
df15de21 999
3806fa52 1000
00793a25 1001/* True if FAILURE */
da95c4b6 1002function saveAttachedFiles($session) {
3f6b1b6f 1003 global $HTTP_POST_FILES, $attachment_dir, $attachments, $username,
1004 $data_dir;
4c9d2242 1005
1006 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1007 $localfilename = GenerateRandomString(32, '', 7);
1008 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1009 while (file_exists($full_localfilename)) {
1010 $localfilename = GenerateRandomString(32, '', 7);
1011 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1012 }
1013
1014 if (!@rename($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
ceca62d3 1015 if (function_exists("move_uploaded_file")) {
1016 if (!@move_uploaded_file($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
056ddad7 1017 return true;
ceca62d3 1018 }
1019 } else {
ceca62d3 1020 if (!@copy($HTTP_POST_FILES['attachfile']['tmp_name'], $full_localfilename)) {
1021 return true;
1022 }
1023 }
1024
4c9d2242 1025 }
4c9d2242 1026 $newAttachment['localfilename'] = $localfilename;
1027 $newAttachment['remotefilename'] = $HTTP_POST_FILES['attachfile']['name'];
1028 $newAttachment['type'] = strtolower($HTTP_POST_FILES['attachfile']['type']);
da95c4b6 1029 $newAttachment['session'] = $session;
8ef72f33 1030
4c9d2242 1031 if ($newAttachment['type'] == "") {
8ef72f33 1032 $newAttachment['type'] = 'application/octet-stream';
056ddad7 1033 }
4c9d2242 1034 $attachments[] = $newAttachment;
3f6b1b6f 1035 setPref($data_dir, $username, 'attachments', serialize($attachments));
4c9d2242 1036}
1037
4c9d2242 1038
da95c4b6 1039function ClearAttachments($session)
4c9d2242 1040{
3f6b1b6f 1041 global $username, $attachments, $attachment_dir, $data_dir;
4c9d2242 1042 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1043
da95c4b6 1044 $rem_attachments = array();
8712abea 1045 if (is_array($attachments)) {
1046 foreach ($attachments as $info) {
1047 if ($info['session'] == $session) {
1048 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
1049 if (file_exists($attached_file)) {
1050 unlink($attached_file);
1051 }
1052 }
1053 else {
1054 $rem_attachments[] = $info;
1055 }
1056 }
da95c4b6 1057 }
1058 $attachments = $rem_attachments;
3f6b1b6f 1059 setPref($data_dir, $username, 'attachments', serialize($attachments));
4c9d2242 1060}
1061
da95c4b6 1062
4c9d2242 1063function getReplyCitation($orig_from)
1064{
1065 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
1066
1067 /* First, return an empty string when no citation style selected. */
1068 if (($reply_citation_style == '') || ($reply_citation_style == 'none')) {
1069 return '';
1070 }
1071
4c9d2242 1072 /* Make sure our final value isn't an empty string. */
1073 if ($orig_from == '') {
1074 return '';
1075 }
1076
1077 /* Otherwise, try to select the desired citation style. */
1078 switch ($reply_citation_style) {
1079 case 'author_said':
1080 $start = '';
1081 $end = ' ' . _("said") . ':';
1082 break;
1083 case 'quote_who':
1084 $start = '<' . _("quote") . ' ' . _("who") . '="';
1085 $end = '">';
1086 break;
1087 case 'user-defined':
55b321f2 1088 $start = $reply_citation_start .
1089 ($reply_citation_start == '' ? '' : ' ');
4c9d2242 1090 $end = $reply_citation_end;
1091 break;
1092 default:
1093 return '';
1094 }
1095
1096 /* Build and return the citation string. */
1097 return ($start . $orig_from . $end . "\n");
1098}
1099
5e9e90fd 1100?>