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