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