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