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