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