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