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