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