Allow for stripping sigs when replying, if enabled will drop everything
[squirrelmail.git] / src / compose.php
1 <?php
2
3 /**
4 * compose.php
5 *
6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This code sends a mail.
10 *
11 * There are 4 modes of operation:
12 * - Start new mail
13 * - Add an attachment
14 * - Send mail
15 * - Save As Draft
16 *
17 * $Id$
18 */
19
20 /* Path for SquirrelMail required files. */
21 define('SM_PATH','../');
22
23 /* SquirrelMail required files. */
24 require_once(SM_PATH . 'include/validate.php');
25 require_once(SM_PATH . 'functions/global.php');
26 require_once(SM_PATH . 'functions/imap.php');
27 require_once(SM_PATH . 'functions/date.php');
28 require_once(SM_PATH . 'functions/mime.php');
29 require_once(SM_PATH . 'functions/plugin.php');
30 require_once(SM_PATH . 'functions/display_messages.php');
31 require_once(SM_PATH . 'class/deliver/Deliver.class.php');
32 require_once(SM_PATH . 'functions/addressbook.php');
33 require_once(SM_PATH . 'functions/identity.php');
34
35 /* --------------------- Get globals ------------------------------------- */
36 /** COOKIE VARS */
37 sqgetGlobalVar('key', $key, SQ_COOKIE);
38
39 /** SESSION VARS */
40 sqgetGlobalVar('username', $username, SQ_SESSION);
41 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
42 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
43 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
44
45 sqgetGlobalVar('composesession', $composesession, SQ_SESSION);
46 sqgetGlobalVar('compose_messages', $compose_messages, SQ_SESSION);
47
48 /** SESSION/POST/GET VARS */
49 sqgetGlobalVar('smaction',$action);
50 sqgetGlobalVar('session',$session);
51 sqgetGlobalVar('mailbox',$mailbox);
52 if(!sqgetGlobalVar('identity',$identity)) {
53 $identity=0;
54 }
55 sqgetGlobalVar('send_to',$send_to);
56 sqgetGlobalVar('send_to_cc',$send_to_cc);
57 sqgetGlobalVar('send_to_bcc',$send_to_bcc);
58 sqgetGlobalVar('subject',$subject);
59 sqgetGlobalVar('body',$body);
60 sqgetGlobalVar('mailprio',$mailprio);
61 sqgetGlobalVar('request_mdn',$request_mdn);
62 sqgetGlobalVar('request_dr',$request_dr);
63 sqgetGlobalVar('html_addr_search',$html_addr_search);
64 sqgetGlobalVar('mail_sent',$mail_sent);
65 sqgetGlobalVar('passed_id',$passed_id);
66 sqgetGlobalVar('passed_ent_id',$passed_ent_id);
67 sqgetGlobalVar('send',$send);
68
69 sqgetGlobalVar('attach',$attach);
70
71 sqgetGlobalVar('draft',$draft);
72 sqgetGlobalVar('draft_id',$draft_id);
73 sqgetGlobalVar('ent_num',$ent_num);
74 sqgetGlobalVar('saved_draft',$saved_draft);
75 sqgetGlobalVar('delete_draft',$delete_draft);
76 sqgetGlobalVar('startmessage',$startMessage);
77
78 /** POST VARS */
79 sqgetGlobalVar('sigappend', $sigappend, SQ_POST);
80 sqgetGlobalVar('from_htmladdr_search', $from_htmladdr_search, SQ_POST);
81 sqgetGlobalVar('addr_search_done', $html_addr_search_done, SQ_POST);
82 sqgetGlobalVar('send_to_search', $send_to_search, SQ_POST);
83 sqgetGlobalVar('do_delete', $do_delete, SQ_POST);
84 sqgetGlobalVar('delete', $delete, SQ_POST);
85 sqgetGlobalVar('restoremessages', $restoremessages, SQ_POST);
86 if ( sqgetGlobalVar('return', $temp, SQ_POST) ) {
87 $html_addr_search_done = 'Use Addresses';
88 }
89
90 /** GET VARS */
91 sqgetGlobalVar('attachedmessages', $attachedmessages, SQ_GET);
92
93 /* Location (For HTTP 1.1 Header("Location: ...") redirects) */
94 $location = get_location();
95 /* Identities (fetch only once) */
96 $idents = get_identities();
97
98 /* --------------------- Specific Functions ------------------------------ */
99
100 function replyAllString($header) {
101 global $include_self_reply_all, $idents;
102 $excl_ar = array();
103 /**
104 * 1) Remove the addresses we'll be sending the message 'to'
105 */
106 $url_replytoall_avoid_addrs = '';
107 if (isset($header->replyto)) {
108 $excl_ar = $header->getAddr_a('replyto');
109 }
110 /**
111 * 2) Remove our identities from the CC list (they still can be in the
112 * TO list) only if $include_self_reply_all is turned off
113 */
114 if (!$include_self_reply_all) {
115 foreach($idents as $id) {
116 $excl_ar[strtolower(trim($id['email_address']))] = '';
117 }
118 }
119
120 /**
121 * 3) get the addresses.
122 */
123 $url_replytoall_ar = $header->getAddr_a(array('to','cc'), $excl_ar);
124
125 /**
126 * 4) generate the string.
127 */
128 $url_replytoallcc = '';
129 foreach( $url_replytoall_ar as $email => $personal) {
130 if ($personal) {
131 $url_replytoallcc .= ", $personal <$email>";
132 } else {
133 $url_replytoallcc .= ', '. $email;
134 }
135 }
136 $url_replytoallcc = substr($url_replytoallcc,2);
137
138 return $url_replytoallcc;
139 }
140
141 function getReplyCitation($orig_from) {
142 global $reply_citation_style, $reply_citation_start, $reply_citation_end;
143 $orig_from = decodeHeader($orig_from->getAddress(false),false,false);
144 // $from = decodeHeader($orig_header->getAddr_s('from',"\n$indent"),false,false);
145 /* First, return an empty string when no citation style selected. */
146 if (($reply_citation_style == '') || ($reply_citation_style == 'none')) {
147 return '';
148 }
149
150 /* Make sure our final value isn't an empty string. */
151 if ($orig_from == '') {
152 return '';
153 }
154
155 /* Otherwise, try to select the desired citation style. */
156 switch ($reply_citation_style) {
157 case 'author_said':
158 $start = '';
159 $end = ' ' . _("said") . ':';
160 break;
161 case 'quote_who':
162 $start = '<' . _("quote") . ' ' . _("who") . '="';
163 $end = '">';
164 break;
165 case 'user-defined':
166 $start = $reply_citation_start .
167 ($reply_citation_start == '' ? '' : ' ');
168 $end = $reply_citation_end;
169 break;
170 default:
171 return '';
172 }
173
174 /* Build and return the citation string. */
175 return ($start . $orig_from . $end . "\n");
176 }
177
178 function getforwardHeader($orig_header) {
179 global $editor_size;
180
181 $display = array( _("Subject") => strlen(_("Subject")),
182 _("From") => strlen(_("From")),
183 _("Date") => strlen(_("Date")),
184 _("To") => strlen(_("To")),
185 _("Cc") => strlen(_("Cc")) );
186 $maxsize = max($display);
187 $indent = str_pad('',$maxsize+2);
188 foreach($display as $key => $val) {
189 $display[$key] = $key .': '. str_pad('', $maxsize - $val);
190 }
191 $from = decodeHeader($orig_header->getAddr_s('from',"\n$indent"),false,false);
192 $from = str_replace('&nbsp;',' ',$from);
193 $to = decodeHeader($orig_header->getAddr_s('to',"\n$indent"),false,false);
194 $to = str_replace('&nbsp;',' ',$to);
195 $subject = decodeHeader($orig_header->subject,false,false);
196 $subject = str_replace('&nbsp;',' ',$subject);
197 $bodyTop = str_pad(' '._("Original Message").' ',$editor_size -2,'-',STR_PAD_BOTH) .
198 "\n". $display[_("Subject")] . $subject . "\n" .
199 $display[_("From")] . $from . "\n" .
200 $display[_("Date")] . getLongDateString( $orig_header->date ). "\n" .
201 $display[_("To")] . $to . "\n";
202 if ($orig_header->cc != array() && $orig_header->cc !='') {
203 $cc = decodeHeader($orig_header->getAddr_s('cc',"\n$indent"),false,false);
204 $cc = str_replace('&nbsp;',' ',$cc);
205 $bodyTop .= $display[_("Cc")] .$cc . "\n";
206 }
207 $bodyTop .= str_pad('', $editor_size -2 , '-') .
208 "\n\n";
209 return $bodyTop;
210 }
211 /* ----------------------------------------------------------------------- */
212
213 /*
214 * If the session is expired during a post this restores the compose session
215 * vars.
216 */
217 if (sqsession_is_registered('session_expired_post')) {
218 sqgetGlobalVar('session_expired_post', $session_expired_post, SQ_SESSION);
219 /*
220 * extra check for username so we don't display previous post data from
221 * another user during this session.
222 */
223 if ($session_expired_post['username'] != $username) {
224 unset($session_expired_post);
225 sqsession_unregister('session_expired_post');
226 session_write_close();
227 } else {
228 foreach ($session_expired_post as $postvar => $val) {
229 if (isset($val)) {
230 $$postvar = $val;
231 } else {
232 $$postvar = '';
233 }
234 }
235 $compose_messages = unserialize(urldecode($restoremessages));
236 sqsession_register($compose_messages,'compose_messages');
237 sqsession_register($composesession,'composesession');
238 if (isset($send)) {
239 unset($send);
240 }
241 $session_expired = true;
242 }
243 unset($session_expired_post);
244 sqsession_unregister('session_expired_post');
245 session_write_close();
246 if (!isset($mailbox)) {
247 $mailbox = '';
248 }
249 if ($compose_new_win == '1') {
250 compose_Header($color, $mailbox);
251 } else {
252 displayPageHeader($color, $mailbox);
253 }
254 showInputForm($session, false);
255 exit();
256 }
257 if (!isset($composesession)) {
258 $composesession = 0;
259 sqsession_register(0,'composesession');
260 }
261
262 if (!isset($session) || (isset($newmessage) && $newmessage)) {
263 sqsession_unregister('composesession');
264 $session = "$composesession" +1;
265 $composesession = $session;
266 sqsession_register($composesession,'composesession');
267 }
268 if (!isset($compose_messages)) {
269 $compose_messages = array();
270 }
271 if (!isset($compose_messages[$session]) || ($compose_messages[$session] == NULL)) {
272 /* if (!array_key_exists($session, $compose_messages)) { /* We can only do this in PHP >= 4.1 */
273 $composeMessage = new Message();
274 $rfc822_header = new Rfc822Header();
275 $composeMessage->rfc822_header = $rfc822_header;
276 $composeMessage->reply_rfc822_header = '';
277 $compose_messages[$session] = $composeMessage;
278 sqsession_register($compose_messages,'compose_messages');
279 } else {
280 $composeMessage=$compose_messages[$session];
281 }
282
283 if (!isset($mailbox) || $mailbox == '' || ($mailbox == 'None')) {
284 $mailbox = 'INBOX';
285 }
286
287 if ($draft) {
288 /*
289 * Set $default_charset to correspond with the user's selection
290 * of language interface.
291 */
292 set_my_charset();
293 $composeMessage=$compose_messages[$session];
294 if (! deliverMessage($composeMessage, true)) {
295 showInputForm($session);
296 exit();
297 } else {
298 unset($compose_messages[$session]);
299 $draft_message = _("Draft Email Saved");
300 /* If this is a resumed draft, then delete the original */
301 if(isset($delete_draft)) {
302 Header("Location: $location/delete_message.php?mailbox=" . urlencode($draft_folder) .
303 "&message=$delete_draft&sort=$sort&startMessage=1&saved_draft=yes");
304 exit();
305 }
306 else {
307 if ($compose_new_win == '1') {
308 Header("Location: $location/compose.php?saved_draft=yes&session=$composesession");
309 exit();
310 }
311 else {
312 Header("Location: $location/right_main.php?mailbox=$draft_folder&sort=$sort".
313 "&startMessage=1&note=".urlencode($draft_message));
314 exit();
315 }
316 }
317 }
318 }
319
320 if ($send) {
321 if (isset($_FILES['attachfile']) &&
322 $_FILES['attachfile']['tmp_name'] &&
323 $_FILES['attachfile']['tmp_name'] != 'none') {
324 $AttachFailure = saveAttachedFiles($session);
325 }
326 if (checkInput(false) && !isset($AttachFailure)) {
327 if ($mailbox == "All Folders") {
328 /* We entered compose via the search results page */
329 $mailbox="INBOX"; /* Send 'em to INBOX, that's safe enough */
330 }
331 $urlMailbox = urlencode (trim($mailbox));
332 if (! isset($passed_id)) {
333 $passed_id = 0;
334 }
335 /*
336 * Set $default_charset to correspond with the user's selection
337 * of language interface.
338 */
339 set_my_charset();
340 /*
341 * This is to change all newlines to \n
342 * We'll change them to \r\n later (in the sendMessage function)
343 */
344 $body = str_replace("\r\n", "\n", $body);
345 $body = str_replace("\r", "\n", $body);
346
347 /*
348 * Rewrap $body so that no line is bigger than $editor_size
349 * This should only really kick in the sqWordWrap function
350 * if the browser doesn't support "VIRTUAL" as the wrap type.
351 */
352 $body = explode("\n", $body);
353 $newBody = '';
354 foreach ($body as $line) {
355 if( $line <> '-- ' ) {
356 $line = rtrim($line);
357 }
358 if (strlen($line) <= $editor_size + 1) {
359 $newBody .= $line . "\n";
360 } else {
361 sqWordWrap($line, $editor_size);
362 $newBody .= $line . "\n";
363
364 }
365
366 }
367 $body = $newBody;
368
369 $composeMessage=$compose_messages[$session];
370
371 $Result = deliverMessage($composeMessage);
372 if (! $Result) {
373 showInputForm($session);
374 exit();
375 }
376 unset($compose_messages[$session]);
377 if ( isset($delete_draft)) {
378 Header("Location: $location/delete_message.php?mailbox=" . urlencode( $draft_folder ).
379 "&message=$delete_draft&sort=$sort&startMessage=1&mail_sent=yes");
380 exit();
381 }
382 if ($compose_new_win == '1') {
383
384 Header("Location: $location/compose.php?mail_sent=yes");
385 }
386 else {
387 Header("Location: $location/right_main.php?mailbox=$urlMailbox&sort=$sort".
388 "&startMessage=$startMessage&mail_sent=yes");
389 }
390 } else {
391 if ($compose_new_win == '1') {
392 compose_Header($color, $mailbox);
393 }
394 else {
395 displayPageHeader($color, $mailbox);
396 }
397 if (isset($AttachFailure)) {
398 plain_error_message(_("Could not move/copy file. File not attached"),
399 $color);
400 }
401 checkInput(true);
402 showInputForm($session);
403 /* sqimap_logout($imapConnection); */
404 }
405 } elseif (isset($html_addr_search_done)) {
406 if ($compose_new_win == '1') {
407 compose_Header($color, $mailbox);
408 }
409 else {
410 displayPageHeader($color, $mailbox);
411 }
412
413 if (isset($send_to_search) && is_array($send_to_search)) {
414 foreach ($send_to_search as $k => $v) {
415 if (substr($k, 0, 1) == 'T') {
416 if ($send_to) {
417 $send_to .= ', ';
418 }
419 $send_to .= $v;
420 }
421 elseif (substr($k, 0, 1) == 'C') {
422 if ($send_to_cc) {
423 $send_to_cc .= ', ';
424 }
425 $send_to_cc .= $v;
426 }
427 elseif (substr($k, 0, 1) == 'B') {
428 if ($send_to_bcc) {
429 $send_to_bcc .= ', ';
430 }
431 $send_to_bcc .= $v;
432 }
433 }
434 }
435 showInputForm($session);
436 } elseif (isset($html_addr_search)) {
437 if (isset($_FILES['attachfile']) &&
438 $_FILES['attachfile']['tmp_name'] &&
439 $_FILES['attachfile']['tmp_name'] != 'none') {
440 if(saveAttachedFiles($session)) {
441 plain_error_message(_("Could not move/copy file. File not attached"), $color);
442 }
443 }
444 /*
445 * I am using an include so as to elminiate an extra unnecessary
446 * click. If you can think of a better way, please implement it.
447 */
448 include_once('./addrbook_search_html.php');
449 } elseif (isset($attach)) {
450 if (saveAttachedFiles($session)) {
451 plain_error_message(_("Could not move/copy file. File not attached"), $color);
452 }
453 if ($compose_new_win == '1') {
454 compose_Header($color, $mailbox);
455 }
456 else {
457 displayPageHeader($color, $mailbox);
458 }
459 showInputForm($session);
460 }
461 elseif (isset($sigappend)) {
462 $signature = $idents[$identity]['signature'];
463
464 $body .= "\n\n".($prefix_sig==true? "-- \n":'').$signature;
465 if ($compose_new_win == '1') {
466 compose_Header($color, $mailbox);
467 } else {
468 displayPageHeader($color, $mailbox);
469 }
470 showInputForm($session);
471 } elseif (isset($do_delete)) {
472 if ($compose_new_win == '1') {
473 compose_Header($color, $mailbox);
474 }
475 else {
476 displayPageHeader($color, $mailbox);
477 }
478
479 if (isset($delete) && is_array($delete)) {
480 $composeMessage = $compose_messages[$session];
481 foreach($delete as $index) {
482 $attached_file = $composeMessage->entities[$index]->att_local_name;
483 unlink ($attached_file);
484 unset ($composeMessage->entities[$index]);
485 }
486 $new_entities = array();
487 foreach ($composeMessage->entities as $entity) {
488 $new_entities[] = $entity;
489 }
490 $composeMessage->entities = $new_entities;
491 $compose_messages[$session] = $composeMessage;
492 sqsession_register($compose_messages, 'compose_messages');
493 }
494 showInputForm($session);
495 } else {
496 /*
497 * This handles the default case as well as the error case
498 * (they had the same code) --> if (isset($smtpErrors))
499 */
500
501 if ($compose_new_win == '1') {
502 compose_Header($color, $mailbox);
503 } else {
504 displayPageHeader($color, $mailbox);
505 }
506
507 $newmail = true;
508
509 if (!isset($passed_ent_id)) {
510 $passed_ent_id = '';
511 }
512 if (!isset($passed_id)) {
513 $passed_id = '';
514 }
515 if (!isset($mailbox)) {
516 $mailbox = '';
517 }
518 if (!isset($action)) {
519 $action = '';
520 }
521
522 $values = newMail($mailbox,$passed_id,$passed_ent_id, $action, $session);
523
524 /* in case the origin is not read_body.php */
525 if (isset($send_to)) {
526 $values['send_to'] = $send_to;
527 }
528 if (isset($send_to_cc)) {
529 $values['send_to_cc'] = $send_to_cc;
530 }
531 if (isset($send_to_bcc)) {
532 $values['send_to_bcc'] = $send_to_bcc;
533 }
534 if (isset($subject)) {
535 $values['subject'] = $subject;
536 }
537 showInputForm($session, $values);
538 }
539
540 exit();
541
542 /**************** Only function definitions go below *************/
543
544
545 /* This function is used when not sending or adding attachments */
546 function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $session='') {
547 global $editor_size, $default_use_priority, $body, $idents,
548 $use_signature, $composesession, $data_dir, $username,
549 $username, $key, $imapServerAddress, $imapPort, $compose_messages,
550 $composeMessage;
551 global $languages, $squirrelmail_language;
552
553 $send_to = $send_to_cc = $send_to_bcc = $subject = $identity = '';
554 $mailprio = 3;
555
556 if ($passed_id) {
557 $imapConnection = sqimap_login($username, $key, $imapServerAddress,
558 $imapPort, 0);
559
560 sqimap_mailbox_select($imapConnection, $mailbox);
561 $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
562
563 $body = '';
564 if ($passed_ent_id) {
565 /* redefine the messsage in case of message/rfc822 */
566 $message = $message->getEntity($passed_ent_id);
567 /* message is an entity which contains the envelope and type0=message
568 * and type1=rfc822. The actual entities are childs from
569 * $message->entities[0]. That's where the encoding and is located
570 */
571
572 $entities = $message->entities[0]->findDisplayEntity
573 (array(), $alt_order = array('text/plain'));
574 if (!count($entities)) {
575 $entities = $message->entities[0]->findDisplayEntity
576 (array(), $alt_order = array('text/plain','html/plain'));
577 }
578 $orig_header = $message->rfc822_header; /* here is the envelope located */
579 /* redefine the message for picking up the attachments */
580 $message = $message->entities[0];
581
582 } else {
583 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain'));
584 if (!count($entities)) {
585 $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain','html/plain'));
586 }
587 $orig_header = $message->rfc822_header;
588 }
589
590 $encoding = $message->header->encoding;
591 $type0 = $message->type0;
592 $type1 = $message->type1;
593 foreach ($entities as $ent) {
594 $unencoded_bodypart = mime_fetch_body($imapConnection, $passed_id, $ent);
595 $body_part_entity = $message->getEntity($ent);
596 $bodypart = decodeBody($unencoded_bodypart,
597 $body_part_entity->header->encoding);
598 if ($type1 == 'html') {
599 $bodypart = str_replace(array('&nbsp;','&gt','&lt'),array(' ','<','>'),$bodypart);
600 $bodypart = strip_tags($bodypart);
601 }
602 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
603 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
604 if (mb_detect_encoding($bodypart) != 'ASCII') {
605 $bodypart = $languages[$squirrelmail_language]['XTRA_CODE']('decode', $bodypart);
606 }
607 }
608 $body .= $bodypart;
609 }
610 if ($default_use_priority) {
611 $mailprio = substr($orig_header->priority,0,1);
612 if (!$mailprio) {
613 $mailprio = 3;
614 }
615 } else {
616 $mailprio = '';
617 }
618 //ClearAttachments($session);
619
620 $identity = '';
621 $from_o = $orig_header->from;
622 if (is_object($from_o)) {
623 $orig_from = $from_o->getAddress();
624 } else {
625 $orig_from = '';
626 }
627
628 $identities = array();
629 if (count($idents) > 1) {
630 foreach($idents as $nr=>$data) {
631 $enc_from_name = '"'.$data['full_name'].'" <'. $data['email_address'].'>';
632 if($enc_from_name == $orig_from) {
633 $identity = $nr;
634 break;
635 }
636 $identities[] = $enc_from_name;
637 }
638
639 $identity_match = $orig_header->findAddress($identities);
640 if ($identity_match) {
641 $identity = $identity_match;
642 }
643 }
644
645 switch ($action) {
646 case ('draft'):
647 $use_signature = FALSE;
648 $composeMessage->rfc822_header = $orig_header;
649 $send_to = decodeHeader($orig_header->getAddr_s('to'),false,true);
650 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'),false,true);
651 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'),false,true);
652 $subject = decodeHeader($orig_header->subject,false,true);
653 // /* remember the references and in-reply-to headers in case of an reply */
654 $composeMessage->rfc822_header->more_headers['References'] = $orig_header->references;
655 $composeMessage->rfc822_header->more_headers['In-Reply-To'] = $orig_header->in_reply_to;
656 $body_ary = explode("\n", $body);
657 $cnt = count($body_ary) ;
658 $body = '';
659 for ($i=0; $i < $cnt; $i++) {
660 if (!ereg("^[>\\s]*$", $body_ary[$i]) || !$body_ary[$i]) {
661 sqWordWrap($body_ary[$i], $editor_size );
662 $body .= $body_ary[$i] . "\n";
663 }
664 unset($body_ary[$i]);
665 }
666 sqUnWordWrap($body);
667 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
668 break;
669 case ('edit_as_new'):
670 $send_to = decodeHeader($orig_header->getAddr_s('to'),false,true);
671 $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'),false,true);
672 $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'),false,true);
673 $subject = decodeHeader($orig_header->subject,false,true);
674 $mailprio = $orig_header->priority;
675 $orig_from = '';
676 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
677 sqUnWordWrap($body);
678 break;
679 case ('forward'):
680 $send_to = '';
681 $subject = decodeHeader($orig_header->subject,false,true);
682 if ((substr(strtolower($subject), 0, 4) != 'fwd:') &&
683 (substr(strtolower($subject), 0, 5) != '[fwd:') &&
684 (substr(strtolower($subject), 0, 6) != '[ fwd:')) {
685 $subject = '[Fwd: ' . $subject . ']';
686 }
687 $body = getforwardHeader($orig_header) . $body;
688 sqUnWordWrap($body);
689 $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
690 $body = "\n" . $body;
691 break;
692 case ('forward_as_attachment'):
693 $composeMessage = getMessage_RFC822_Attachment($message, $composeMessage, $passed_id, $passed_ent_id, $imapConnection);
694 $body = '';
695 break;
696 case ('reply_all'):
697 if(isset($orig_header->mail_followup_to) && $orig_header->mail_followup_to) {
698 $send_to = $orig_header->getAddr_s('mail_followup_to');
699 } else {
700 $send_to_cc = replyAllString($orig_header);
701 $send_to_cc = decodeHeader($send_to_cc,false,true);
702 }
703 case ('reply'):
704 // skip this if send_to was already set right above here
705 if(!$send_to) {
706 $send_to = $orig_header->reply_to;
707 if (is_array($send_to) && count($send_to)) {
708 $send_to = $orig_header->getAddr_s('reply_to');
709 } else if (is_object($send_to)) { /* unneccesarry, just for failsafe purpose */
710 $send_to = $orig_header->getAddr_s('reply_to');
711 } else {
712 $send_to = $orig_header->getAddr_s('from');
713 }
714 }
715 $send_to = decodeHeader($send_to,false,true);
716 $subject = decodeHeader($orig_header->subject,false,true);
717 $subject = str_replace('"', "'", $subject);
718 $subject = trim($subject);
719 if (substr(strtolower($subject), 0, 3) != 're:') {
720 $subject = 'Re: ' . $subject;
721 }
722 /* this corrects some wrapping/quoting problems on replies */
723 $rewrap_body = explode("\n", $body);
724 $from = (is_array($orig_header->from)) ? $orig_header->from[0] : $orig_header->from;
725 sqUnWordWrap($body); // unwrap and then reset it?!
726 $body = '';
727 $strip_sigs = getPref($data_dir, $username, 'strip_sigs');
728 foreach ($rewrap_body as $line) {
729 if ($strip_sigs && substr($line,0,3) == '-- ') {
730 break;
731 }
732 sqWordWrap($line, ($editor_size));
733 if (preg_match("/^(>+)/", $line, $matches)) {
734 $gt = $matches[1];
735 $body .= '>' . str_replace("\n", "\n>$gt ", rtrim($line)) ."\n";
736 } else {
737 $body .= '> ' . str_replace("\n", "\n> ", rtrim($line)) . "\n";
738 }
739 }
740 $body = getReplyCitation($from) . $body;
741 $composeMessage->reply_rfc822_header = $orig_header;
742
743 break;
744 default:
745 break;
746 }
747 $compose_messages[$session] = $composeMessage;
748 sqsession_register($compose_messages, 'compose_messages');
749 session_write_close();
750 sqimap_logout($imapConnection);
751 }
752 $ret = array( 'send_to' => $send_to,
753 'send_to_cc' => $send_to_cc,
754 'send_to_bcc' => $send_to_bcc,
755 'subject' => $subject,
756 'mailprio' => $mailprio,
757 'body' => $body,
758 'identity' => $identity );
759
760 return ($ret);
761 } /* function newMail() */
762
763 function getAttachments($message, &$composeMessage, $passed_id, $entities, $imapConnection) {
764 global $attachment_dir, $username, $data_dir, $squirrelmail_language;
765 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
766 if (!count($message->entities) ||
767 ($message->type0 == 'message' && $message->type1 == 'rfc822')) {
768 if ( !in_array($message->entity_id, $entities) && $message->entity_id) {
769 switch ($message->type0) {
770 case 'message':
771 if ($message->type1 == 'rfc822') {
772 $filename = $message->rfc822_header->subject.'.eml';
773 if ($filename == "") {
774 $filename = "untitled-".$message->entity_id.'.eml';
775 }
776 } else {
777 $filename = $message->getFilename();
778 }
779 break;
780 default:
781 if (!$message->mime_header) { /* temporary hack */
782 $message->mime_header = $message->header;
783 }
784 $filename = $message->getFilename();
785 break;
786 }
787 $filename = decodeHeader($filename);
788 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
789 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
790 $filename = $languages[$squirrelmail_language]['XTRA_CODE']('encode', $filename);
791 }
792 $localfilename = GenerateRandomString(32, '', 7);
793 $full_localfilename = "$hashed_attachment_dir/$localfilename";
794 while (file_exists($full_localfilename)) {
795 $localfilename = GenerateRandomString(32, '', 7);
796 $full_localfilename = "$hashed_attachment_dir/$localfilename";
797 }
798 $message->att_local_name = $full_localfilename;
799
800 $composeMessage->initAttachment($message->type0.'/'.$message->type1,$filename,
801 $full_localfilename);
802
803 /* Write Attachment to file */
804 $fp = fopen ("$hashed_attachment_dir/$localfilename", 'wb');
805 fputs($fp, decodeBody(mime_fetch_body($imapConnection,
806 $passed_id, $message->entity_id),
807 $message->header->encoding));
808 fclose ($fp);
809 }
810 } else {
811 for ($i=0, $entCount=count($message->entities); $i<$entCount;$i++) {
812 $composeMessage=getAttachments($message->entities[$i], $composeMessage, $passed_id, $entities, $imapConnection);
813 }
814 }
815 return $composeMessage;
816 }
817
818 function getMessage_RFC822_Attachment($message, $composeMessage, $passed_id,
819 $passed_ent_id='', $imapConnection) {
820 global $attachments, $attachment_dir, $username, $data_dir, $uid_support;
821 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
822 if (!$passed_ent_id) {
823 $body_a = sqimap_run_command($imapConnection,
824 'FETCH '.$passed_id.' RFC822',
825 TRUE, $response, $readmessage,
826 $uid_support);
827 } else {
828 $body_a = sqimap_run_command($imapConnection,
829 'FETCH '.$passed_id.' BODY['.$passed_ent_id.']',
830 TRUE, $response, $readmessage, $uid_support);
831 $message = $message->parent;
832 }
833 if ($response == 'OK') {
834 $subject = encodeHeader($message->rfc822_header->subject);
835 array_shift($body_a);
836 array_pop($body_a);
837 $body = implode('', $body_a) . "\r\n";
838
839 $localfilename = GenerateRandomString(32, 'FILE', 7);
840 $full_localfilename = "$hashed_attachment_dir/$localfilename";
841
842 $fp = fopen($full_localfilename, 'w');
843 fwrite ($fp, $body);
844 fclose($fp);
845 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
846 $full_localfilename);
847 }
848 return $composeMessage;
849 }
850
851 function showInputForm ($session, $values=false) {
852 global $send_to, $send_to_cc, $body, $startMessage,
853 $passed_body, $color, $use_signature, $signature, $prefix_sig,
854 $editor_size, $attachments, $subject, $newmail,
855 $use_javascript_addr_book, $send_to_bcc, $passed_id, $mailbox,
856 $from_htmladdr_search, $location_of_buttons, $attachment_dir,
857 $username, $data_dir, $identity, $idents, $draft_id, $delete_draft,
858 $mailprio, $default_use_mdn, $mdn_user_support, $compose_new_win,
859 $saved_draft, $mail_sent, $sig_first, $edit_as_new, $action,
860 $username, $compose_messages, $composesession, $default_charset;
861
862 $composeMessage = $compose_messages[$session];
863
864 if ($values) {
865 $send_to = $values['send_to'];
866 $send_to_cc = $values['send_to_cc'];
867 $send_to_bcc = $values['send_to_bcc'];
868 $subject = $values['subject'];
869 $mailprio = $values['mailprio'];
870 $body = $values['body'];
871 $identity = (int) $values['identity'];
872 } else {
873 $send_to = decodeHeader($send_to);
874 $send_to_cc = decodeHeader($send_to_cc);
875 $send_to_bcc = decodeHeader($send_to_bcc);
876 }
877
878 if ($use_javascript_addr_book) {
879 echo "\n". '<SCRIPT LANGUAGE=JavaScript>'."\n<!--\n" .
880 'function open_abook() { ' . "\n" .
881 ' var nwin = window.open("addrbook_popup.php","abookpopup",' .
882 '"width=670,height=300,resizable=yes,scrollbars=yes");' . "\n" .
883 ' if((!nwin.opener) && (document.windows != null))' . "\n" .
884 ' nwin.opener = document.windows;' . "\n" .
885 "}\n" .
886 "// -->\n</SCRIPT>\n\n";
887 }
888
889 echo "\n" . '<form name="compose" action="compose.php" method="post" ' .
890 'enctype="multipart/form-data"';
891 do_hook('compose_form');
892
893 echo ">\n";
894
895 echo '<input type="hidden" name="startMessage" value="' . $startMessage . "\">\n";
896
897 if ($action == 'draft') {
898 echo '<input type="hidden" name="delete_draft" value="' . $passed_id . "\">\n";
899 }
900 if (isset($delete_draft)) {
901 echo '<input type="hidden" name="delete_draft" value="' . $delete_draft. "\">\n";
902 }
903 if (isset($session)) {
904 echo '<input type="hidden" name="session" value="' . $session . "\">\n";
905 }
906
907 if (isset($passed_id)) {
908 echo '<input type="hidden" name="passed_id" value="' . $passed_id . "\">\n";
909 }
910
911 if ($saved_draft == 'yes') {
912 echo '<BR><CENTER><B>'. _("Draft Saved").'</CENTER></B>';
913 }
914 if ($mail_sent == 'yes') {
915 echo '<BR><CENTER><B>'. _("Your Message has been sent").'</CENTER></B>';
916 }
917 echo '<table align="center" cellspacing="0" border="0">' . "\n";
918 if ($compose_new_win == '1') {
919 echo '<TABLE ALIGN=CENTER BGCOLOR="'.$color[0].'" WIDTH="100%" BORDER=0>'."\n" .
920 ' <TR><TD></TD>'. html_tag( 'td', '', 'right' ) . '<INPUT TYPE="BUTTON" NAME="Close" onClick="return self.close()" VALUE='._("Close").'></TD></TR>'."\n";
921 }
922 if ($location_of_buttons == 'top') {
923 showComposeButtonRow();
924 }
925
926 /* display select list for identities */
927 if (count($idents) > 1) {
928 echo ' <tr>' . "\n" .
929 html_tag( 'td', '', 'right', $color[4], 'width="10%"' ) .
930 _("From:") . '</td>' . "\n" .
931 html_tag( 'td', '', 'left', $color[4], 'width="90%"' ) .
932 ' <select name="identity">' . "\n" ;
933 foreach($idents as $id=>$data) {
934 echo '<option value="'.$id.'"';
935 if($id == $identity) {
936 echo ' selected';
937 }
938 echo '>'.htmlspecialchars($data['full_name'].' <'.$data['email_address'].'>').
939 "</option>\n";
940 }
941
942 echo '</select>' . "\n" .
943 ' </td>' . "\n" .
944 ' </tr>' . "\n";
945 }
946 echo ' <tr>' . "\n" .
947 html_tag( 'td', '', 'right', $color[4], 'width="10%"' ) .
948 _("To:") . '</TD>' . "\n" .
949 html_tag( 'td', '', 'left', $color[4], 'width="90%"' ) .
950 ' <input type="text" name="send_to" value="' .
951 $send_to . '" size="60" /><br />' . "\n" .
952 ' </td>' . "\n" .
953 ' </tr>' . "\n" .
954 ' <tr>' . "\n" .
955 html_tag( 'td', '', 'right', $color[4] ) .
956 _("CC:") . '</td>' . "\n" .
957 html_tag( 'td', '', 'left', $color[4] ) .
958 ' <input type="text" name="send_to_cc" size="60" value="' .
959 $send_to_cc . '" /><br />' . "\n" .
960 ' </td>' . "\n" .
961 ' </tr>' . "\n" .
962 ' <tr>' . "\n" .
963 html_tag( 'td', '', 'right', $color[4] ) .
964 _("BCC:") . '</td>' . "\n" .
965 html_tag( 'td', '', 'left', $color[4] ) .
966 ' <input type="text" name="send_to_bcc" value="' .
967 $send_to_bcc . '" size="60" /><br />' . "\n" .
968 ' </td>' . "\n" .
969 ' </tr>' . "\n" .
970 ' <tr>' . "\n" .
971 html_tag( 'td', '', 'right', $color[4] ) .
972 _("Subject:") . '</td>' . "\n" .
973 html_tag( 'td', '', 'left', $color[4] ) . "\n";
974 echo ' <input type="text" name="subject" size="60" value="' .
975 $subject . '" />' . "\n" .
976 ' </td>' . "\n" .
977 ' </tr>' . "\n\n";
978
979 if ($location_of_buttons == 'between') {
980 showComposeButtonRow();
981 }
982
983 /* why this distinction? */
984 if ($compose_new_win == '1') {
985 echo ' <TR>' . "\n" .
986 ' <TD BGCOLOR="' . $color[0] . '" COLSPAN=2 ALIGN=CENTER>' . "\n" .
987 ' <TEXTAREA NAME=body ROWS=20 COLS="' .
988 $editor_size . '" WRAP="VIRTUAL">';
989 }
990 else {
991 echo ' <TR>' . "\n" .
992 ' <TD BGCOLOR="' . $color[4] . '" COLSPAN=2>' . "\n" .
993 ' &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS="' .
994 $editor_size . '" WRAP="VIRTUAL">';
995 }
996
997 if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
998 $signature = $idents[$identity]['signature'];
999
1000 if ($sig_first == '1') {
1001 if ($default_charset == 'iso-2022-jp') {
1002 echo "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
1003 } else {
1004 echo "\n\n".($prefix_sig==true? "-- \n":'').decodeHeader($signature,false,false);
1005 }
1006 echo "\n\n".decodeHeader($body,false,false);
1007 }
1008 else {
1009 echo "\n\n".decodeHeader($body,false,false);
1010 if ($default_charset == 'iso-2022-jp') {
1011 echo "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
1012 }else{
1013 echo "\n\n".($prefix_sig==true? "-- \n":'').decodeHeader($signature,false,false);
1014 }
1015 }
1016 }
1017 else {
1018 echo decodeHeader($body,false,false);
1019 }
1020 echo '</textarea><br />' . "\n" .
1021 ' </td>' . "\n" .
1022 ' </tr>' . "\n";
1023
1024
1025 if ($location_of_buttons == 'bottom') {
1026 showComposeButtonRow();
1027 } else {
1028 echo ' <tr>' . "\n" .
1029 html_tag( 'td', '', 'right', '', 'colspan="2"' ) . "\n" .
1030 ' <input type="submit" name="send" value="' . _("Send") . '" />' . "\n" .
1031 ' &nbsp;&nbsp;&nbsp;&nbsp;<br /><br />' . "\n" .
1032 ' </td>' . "\n" .
1033 ' </tr>' . "\n";
1034 }
1035
1036 /* This code is for attachments */
1037 if ((bool) ini_get('file_uploads')) {
1038
1039 /* Calculate the max size for an uploaded file.
1040 * This is advisory for the user because we can't actually prevent
1041 * people to upload too large files. */
1042 $sizes = array();
1043 /* php.ini vars which influence the max for uploads */
1044 $configvars = array('post_max_size', 'memory_limit', 'upload_max_filesize');
1045 foreach($configvars as $var) {
1046 /* skip 0 or empty values */
1047 if( $size = getByteSize(ini_get($var)) ) {
1048 $sizes[] = $size;
1049 }
1050 }
1051
1052 if(count($sizes) > 0) {
1053 $maxsize = '(max.&nbsp;' . show_readable_size( min( $sizes ) ) . ')';
1054 } else {
1055 $maxsize = '';
1056 }
1057 echo '<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="'.min( $sizes ).'">';
1058 echo ' <tr>' . "\n" .
1059 ' <td colspan="2">' . "\n" .
1060 ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.
1061 ' border="0" bgcolor="'.$color[9].'">' . "\n" .
1062 ' <tr>' . "\n" .
1063 ' <td>' . "\n" .
1064 ' <table width="100%" cellpadding="3" cellspacing="0" align="center"'.
1065 ' border="0">' . "\n" .
1066 ' <tr>' . "\n" .
1067 html_tag( 'td', '', 'right', '', 'valign="middle"' ) .
1068 _("Attach:") . '</td>' . "\n" .
1069 html_tag( 'td', '', 'left', '', 'valign="middle"' ) .
1070 ' <input name="attachfile" size="48" type="file" />' . "\n" .
1071 ' &nbsp;&nbsp;<input type="submit" name="attach"' .
1072 ' value="' . _("Add") .'">' . "\n" .
1073 $maxsize .
1074 ' </td>' . "\n" .
1075 ' </tr>' . "\n";
1076
1077
1078 $s_a = array();
1079 if ($composeMessage->entities) {
1080 foreach ($composeMessage->entities as $key => $attachment) {
1081 $attached_file = $attachment->att_local_name;
1082 if ($attachment->att_local_name || $attachment->body_part) {
1083 $attached_filename = decodeHeader($attachment->mime_header->getParameter('name'));
1084 $type = $attachment->mime_header->type0.'/'.
1085 $attachment->mime_header->type1;
1086
1087 $s_a[] = '<table bgcolor="'.$color[0].
1088 '" border="0"><tr><td><input type="checkbox" name="delete[]" value="' .
1089 $key . "\"></td><td>\n" . $attached_filename .
1090 '</td><td>-</td><td> ' . $type . '</td><td>('.
1091 show_readable_size( filesize( $attached_file ) ) . ')</td></tr></table>'."\n";
1092 }
1093 }
1094 }
1095 if (count($s_a)) {
1096 foreach ($s_a as $s) {
1097 echo '<tr>' . html_tag( 'td', '', 'left', $color[0], 'colspan="2"' ) . $s .'</td></tr>';
1098 }
1099 echo '<tr><td colspan="2"><input type="submit" name="do_delete" value="' .
1100 _("Delete selected attachments") . "\">\n" .
1101 '</td></tr>';
1102 }
1103 echo ' </table>' . "\n" .
1104 ' </td>' . "\n" .
1105 ' </tr>' . "\n" .
1106 ' </TABLE>' . "\n" .
1107 ' </TD>' . "\n" .
1108 ' </TR>' . "\n";
1109 } // End of file_uploads if-block
1110 /* End of attachment code */
1111 if ($compose_new_win == '1') {
1112 echo '</TABLE>'."\n";
1113 }
1114
1115 echo '</TABLE>' . "\n" .
1116 '<input type="hidden" name="username" value="'. $username . "\">\n" .
1117 '<input type=hidden name=smaction value="' . $action . "\">\n" .
1118 '<INPUT TYPE=hidden NAME=mailbox VALUE="' . htmlspecialchars($mailbox) .
1119 "\">\n";
1120 /*
1121 store the complete ComposeMessages array in a hidden input value
1122 so we can restore them in case of a session timeout.
1123 */
1124 sqgetGlobalVar('QUERY_STRING', $queryString, SQ_SERVER);
1125 echo '<input type=hidden name=restoremessages value="' . urlencode(serialize($compose_messages)) . "\">\n";
1126 echo '<input type=hidden name=composesession value="' . $composesession . "\">\n";
1127 echo '<input type=hidden name=querystring value="' . $queryString . "\">\n";
1128 echo '</FORM>';
1129 if (!(bool) ini_get('file_uploads')) {
1130 /* File uploads are off, so we didn't show that part of the form.
1131 To avoid bogus bug reports, tell the user why. */
1132 echo 'Because PHP file uploads are turned off, you can not attach files ';
1133 echo "to this message. Please see your system administrator for details.\r\n";
1134 }
1135
1136 do_hook('compose_bottom');
1137 echo '</BODY></HTML>' . "\n";
1138 }
1139
1140
1141 function showComposeButtonRow() {
1142 global $use_javascript_addr_book, $save_as_draft,
1143 $default_use_priority, $mailprio, $default_use_mdn,
1144 $request_mdn, $request_dr,
1145 $data_dir, $username;
1146
1147 echo ' <TR>' . "\n" .
1148 ' <TD></TD>' . "\n" .
1149 ' <TD>' . "\n";
1150 if ($default_use_priority) {
1151 if(!isset($mailprio)) {
1152 $mailprio = "3";
1153 }
1154 echo ' ' . _("Priority") .': <select name="mailprio">'.
1155 '<option value="1"'.($mailprio=='1'?' selected':'').'>'. _("High") .'</option>'.
1156 '<option value="3"'.($mailprio=='3'?' selected':'').'>'. _("Normal") .'</option>'.
1157 '<option value="5"'.($mailprio=='5'?' selected':'').'>'. _("Low").'</option>'.
1158 '</select>' . "\n";
1159 }
1160 $mdn_user_support=getPref($data_dir, $username, 'mdn_user_support',$default_use_mdn);
1161 if ($default_use_mdn) {
1162 if ($mdn_user_support) {
1163 echo ' ' . _("Receipt") .': '.
1164 '<input type="checkbox" name="request_mdn" value=1'.
1165 ($request_mdn=='1'?' checked':'') .'>'. _("On Read").
1166 ' <input type="checkbox" name="request_dr" value=1'.
1167 ($request_dr=='1'?' checked':'') .'>'. _("On Delivery");
1168 }
1169 }
1170
1171 echo ' </TD>' . "\n" .
1172 ' </TR>' . "\n" .
1173 ' <TR>' . "\n" .
1174 ' <TD></TD>' . "\n" .
1175 ' <TD>' . "\n" .
1176 ' <INPUT TYPE=SUBMIT NAME="sigappend" VALUE="' . _("Signature") . '">' . "\n";
1177 if ($use_javascript_addr_book) {
1178 echo " <SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"".
1179 " <input type=button value=\\\""._("Addresses").
1180 "\\\" onclick='javascript:open_abook();'>\");".
1181 " // --></SCRIPT><NOSCRIPT>\n".
1182 " <input type=submit name=\"html_addr_search\" value=\"".
1183 _("Addresses")."\">".
1184 " </NOSCRIPT>\n";
1185 } else {
1186 echo ' <input type=submit name="html_addr_search" value="'.
1187 _("Addresses").'">' . "\n";
1188 }
1189
1190 if ($save_as_draft) {
1191 echo ' <input type="submit" name ="draft" value="' . _("Save Draft") . "\">\n";
1192 }
1193
1194 echo ' <INPUT TYPE=submit NAME=send VALUE="'. _("Send") . '">' . "\n";
1195 do_hook('compose_button_row');
1196
1197 echo ' </TD>' . "\n" .
1198 ' </TR>' . "\n\n";
1199 }
1200
1201 function checkInput ($show) {
1202 /*
1203 * I implemented the $show variable because the error messages
1204 * were getting sent before the page header. So, I check once
1205 * using $show=false, and then when i'm ready to display the error
1206 * message, show=true
1207 */
1208 global $body, $send_to, $send_to_bcc, $subject, $color;
1209
1210 if ($send_to == '' && $send_to_bcc == '') {
1211 if ($show) {
1212 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
1213 }
1214 return false;
1215 }
1216 return true;
1217 } /* function checkInput() */
1218
1219
1220 /* True if FAILURE */
1221 function saveAttachedFiles($session) {
1222 global $_FILES, $attachment_dir, $attachments, $username,
1223 $data_dir, $compose_messages;
1224
1225 /* get out of here if no file was attached at all */
1226 if (! is_uploaded_file($_FILES['attachfile']['tmp_name']) ) {
1227 return true;
1228 }
1229
1230 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
1231 $localfilename = GenerateRandomString(32, '', 7);
1232 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1233 while (file_exists($full_localfilename)) {
1234 $localfilename = GenerateRandomString(32, '', 7);
1235 $full_localfilename = "$hashed_attachment_dir/$localfilename";
1236 }
1237
1238 // FIXME: we SHOULD prefer move_uploaded_file over rename because
1239 // m_u_f works better with restricted PHP installes (safe_mode, open_basedir)
1240 if (!@rename($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
1241 if (!@move_uploaded_file($_FILES['attachfile']['tmp_name'],$full_localfilename)) {
1242 return true;
1243 }
1244 }
1245 $message = $compose_messages[$session];
1246 $type = strtolower($_FILES['attachfile']['type']);
1247 $name = $_FILES['attachfile']['name'];
1248 $message->initAttachment($type, $name, $full_localfilename);
1249 $compose_messages[$session] = $message;
1250 sqsession_register($compose_messages , 'compose_messages');
1251 }
1252
1253 function ClearAttachments($composeMessage) {
1254 if ($composeMessage->att_local_name) {
1255 $attached_file = $composeMessage->att_local_name;
1256 if (file_exists($attached_file)) {
1257 unlink($attached_file);
1258 }
1259 }
1260 for ($i=0, $entCount=count($composeMessage->entities);$i< $entCount; ++$i) {
1261 ClearAttachments($composeMessage->entities[$i]);
1262 }
1263 }
1264
1265 /* parse values like 8M and 2k into bytes */
1266 function getByteSize($ini_size) {
1267
1268 if(!$ini_size) {
1269 return FALSE;
1270 }
1271
1272 $ini_size = trim($ini_size);
1273
1274 // if there's some kind of letter at the end of the string we need to multiply.
1275 if(!is_numeric(substr($ini_size, -1))) {
1276
1277 switch(strtoupper(substr($ini_size, -1))) {
1278 case 'G':
1279 $bytesize = 1073741824;
1280 break;
1281 case 'M':
1282 $bytesize = 1048576;
1283 break;
1284 case 'K':
1285 $bytesize = 1024;
1286 break;
1287 }
1288
1289 return ($bytesize * (int)substr($ini_size, 0, -1));
1290 }
1291
1292 return $ini_size;
1293 }
1294
1295
1296 /* temporary function to make use of the deliver class.
1297 In the future the responsable backend should be automaticly loaded
1298 and conf.pl should show a list of available backends.
1299 The message also should be constructed by the message class.
1300 */
1301
1302 function deliverMessage($composeMessage, $draft=false) {
1303 global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body,
1304 $username, $popuser, $usernamedata, $identity, $idents, $data_dir,
1305 $request_mdn, $request_dr, $default_charset, $color, $useSendmail,
1306 $domain, $action, $default_move_to_sent, $move_to_sent;
1307 global $imapServerAddress, $imapPort, $sent_folder, $key;
1308
1309 /* some browsers replace <space> by nonbreaking spaces &nbsp;
1310 by replacing them back to spaces addressparsing works */
1311 /* FIXME: How to handle in case of other charsets where "\240"
1312 is not a non breaking space ??? */
1313
1314 $send_to = str_replace("\240",' ',$send_to);
1315 $send_to_cc = str_replace("\240",' ',$send_to_cc);
1316 $send_to_bcc = str_replace("\240",' ',$send_to_bcc);
1317
1318 $rfc822_header = $composeMessage->rfc822_header;
1319
1320 $abook = addressbook_init(false, true);
1321 $rfc822_header->to = $rfc822_header->parseAddress($send_to,true, array(), '', $domain, array(&$abook,'lookup'));
1322 $rfc822_header->cc = $rfc822_header->parseAddress($send_to_cc,true,array(), '',$domain, array(&$abook,'lookup'));
1323 $rfc822_header->bcc = $rfc822_header->parseAddress($send_to_bcc,true, array(), '',$domain, array(&$abook,'lookup'));
1324 $rfc822_header->priority = $mailprio;
1325 $rfc822_header->subject = $subject;
1326 $special_encoding='';
1327 if (strtolower($default_charset) == 'iso-2022-jp') {
1328 if (mb_detect_encoding($body) == 'ASCII') {
1329 $special_encoding = '8bit';
1330 } else {
1331 $body = mb_convert_encoding($body, 'JIS');
1332 $special_encoding = '7bit';
1333 }
1334 }
1335 $composeMessage->setBody($body);
1336
1337 if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
1338 $popuser = $usernamedata[1];
1339 $domain = $usernamedata[2];
1340 unset($usernamedata);
1341 } else {
1342 $popuser = $username;
1343 }
1344 $reply_to = '';
1345 $from_mail = $idents[$identity]['email_address'];
1346 $full_name = $idents[$identity]['full_name'];
1347 $reply_to = $idents[$identity]['reply_to'];
1348 if (!$from_mail) {
1349 $from_mail = "$popuser@$domain";
1350 }
1351 $rfc822_header->from = $rfc822_header->parseAddress($from_mail,true);
1352 if ($full_name) {
1353 $from = $rfc822_header->from[0];
1354 if (!$from->host) $from->host = $domain;
1355 $full_name_encoded = encodeHeader($full_name);
1356 if ($full_name_encoded != $full_name) {
1357 $from_addr = $full_name_encoded .' <'.$from->mailbox.'@'.$from->host.'>';
1358 } else {
1359 $from_addr = '"'.$full_name .'" <'.$from->mailbox.'@'.$from->host.'>';
1360 }
1361 $rfc822_header->from = $rfc822_header->parseAddress($from_addr,true);
1362 }
1363 if ($reply_to) {
1364 $rfc822_header->reply_to = $rfc822_header->parseAddress($reply_to,true);
1365 }
1366 /* Receipt: On Read */
1367 if (isset($request_mdn) && $request_mdn) {
1368 $rfc822_header->dnt = $rfc822_header->parseAddress($from_mail,true);
1369 }
1370 /* Receipt: On Delivery */
1371 if (isset($request_dr) && $request_dr) {
1372 $rfc822_header->more_headers['Return-Receipt-To'] = $from_mail;
1373 }
1374 /* multipart messages */
1375 if (count($composeMessage->entities)) {
1376 $message_body = new Message();
1377 $message_body->body_part = $composeMessage->body_part;
1378 $composeMessage->body_part = '';
1379 $mime_header = new MessageHeader;
1380 $mime_header->type0 = 'text';
1381 $mime_header->type1 = 'plain';
1382 if ($special_encoding) {
1383 $mime_header->encoding = $special_encoding;
1384 } else {
1385 $mime_header->encoding = '8bit';
1386 }
1387 if ($default_charset) {
1388 $mime_header->parameters['charset'] = $default_charset;
1389 }
1390 $message_body->mime_header = $mime_header;
1391 array_unshift($composeMessage->entities, $message_body);
1392 $content_type = new ContentType('multipart/mixed');
1393 } else {
1394 $content_type = new ContentType('text/plain');
1395 if ($special_encoding) {
1396 $rfc822_header->encoding = $special_encoding;
1397 } else {
1398 $rfc822_header->encoding = '8bit';
1399 }
1400 if ($default_charset) {
1401 $content_type->properties['charset']=$default_charset;
1402 }
1403 }
1404
1405 $rfc822_header->content_type = $content_type;
1406 $composeMessage->rfc822_header = $rfc822_header;
1407
1408 /* Here you can modify the message structure just before we hand
1409 it over to deliver */
1410 $hookReturn = do_hook('compose_send', $composeMessage);
1411 /* Get any changes made by plugins to $composeMessage. */
1412 if ( is_object($hookReturn[1]) ) {
1413 $composeMessage = $hookReturn[1];
1414 }
1415
1416 if (!$useSendmail && !$draft) {
1417 require_once(SM_PATH . 'class/deliver/Deliver_SMTP.class.php');
1418 $deliver = new Deliver_SMTP();
1419 global $smtpServerAddress, $smtpPort, $pop_before_smtp, $smtp_auth_mech;
1420
1421 if ($smtp_auth_mech == 'none') {
1422 $user = '';
1423 $pass = '';
1424 } else {
1425 global $key, $onetimepad;
1426 $user = $username;
1427 $pass = OneTimePadDecrypt($key, $onetimepad);
1428 }
1429
1430 $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ? true : false;
1431 $stream = $deliver->initStream($composeMessage,$domain,0,
1432 $smtpServerAddress, $smtpPort, $user, $pass, $authPop);
1433 } elseif (!$draft) {
1434 require_once(SM_PATH . 'class/deliver/Deliver_SendMail.class.php');
1435 global $sendmail_path;
1436 $deliver = new Deliver_SendMail();
1437 $stream = $deliver->initStream($composeMessage,$sendmail_path);
1438 } elseif ($draft) {
1439 global $draft_folder;
1440 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
1441 $imap_stream = sqimap_login($username, $key, $imapServerAddress,
1442 $imapPort, 0);
1443 if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
1444 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
1445 $imap_deliver = new Deliver_IMAP();
1446 $length = $imap_deliver->mail($composeMessage);
1447 sqimap_append ($imap_stream, $draft_folder, $length);
1448 $imap_deliver->mail($composeMessage, $imap_stream);
1449 sqimap_append_done ($imap_stream, $draft_folder);
1450 sqimap_logout($imap_stream);
1451 unset ($imap_deliver);
1452 return $length;
1453 } else {
1454 $msg = '<br>Error: '._("Draft folder")." $draft_folder" . ' does not exist.';
1455 plain_error_message($msg, $color);
1456 return false;
1457 }
1458 }
1459 $succes = false;
1460 if ($stream) {
1461 $length = $deliver->mail($composeMessage, $stream);
1462 $succes = $deliver->finalizeStream($stream);
1463 }
1464 if (!$succes) {
1465 $msg = $deliver->dlv_msg . '<br>' .
1466 _("Server replied: ") . $deliver->dlv_ret_nr . ' '.
1467 $deliver->dlv_server_msg;
1468 plain_error_message($msg, $color);
1469 } else {
1470 unset ($deliver);
1471 $move_to_sent = getPref($data_dir,$username,'move_to_sent');
1472 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
1473
1474 /* Move to sent code */
1475 if (isset($default_move_to_sent) && ($default_move_to_sent != 0)) {
1476 $svr_allow_sent = true;
1477 } else {
1478 $svr_allow_sent = false;
1479 }
1480
1481 if (isset($sent_folder) && (($sent_folder != '') || ($sent_folder != 'none'))
1482 && sqimap_mailbox_exists( $imap_stream, $sent_folder)) {
1483 $fld_sent = true;
1484 } else {
1485 $fld_sent = false;
1486 }
1487
1488 if ((isset($move_to_sent) && ($move_to_sent != 0)) || (!isset($move_to_sent))) {
1489 $lcl_allow_sent = true;
1490 } else {
1491 $lcl_allow_sent = false;
1492 }
1493
1494 if (($fld_sent && $svr_allow_sent && !$lcl_allow_sent) || ($fld_sent && $lcl_allow_sent)) {
1495 sqimap_append ($imap_stream, $sent_folder, $length);
1496 require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
1497 $imap_deliver = new Deliver_IMAP();
1498 $imap_deliver->mail($composeMessage, $imap_stream);
1499 sqimap_append_done ($imap_stream, $sent_folder);
1500 unset ($imap_deliver);
1501 }
1502 global $passed_id, $mailbox, $action;
1503 ClearAttachments($composeMessage);
1504 if ($action == 'reply' || $action == 'reply_all') {
1505 sqimap_mailbox_select ($imap_stream, $mailbox);
1506 sqimap_messages_flag ($imap_stream, $passed_id, $passed_id, 'Answered', true);
1507 }
1508 sqimap_logout($imap_stream);
1509 }
1510 return $succes;
1511 }
1512
1513 ?>