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