1<?php
2
3/**
4  * SquirrelMail Spam Buttons Plugin
5  * Copyright (c) 2005-2009 Paul Lesniewski <paul@squirrelmail.org>,
6  * Licensed under the GNU GPL. For full terms see the file COPYING.
7  *
8  * @package plugins
9  * @subpackage spam_buttons
10  *
11  */
12
13
14include_once(SM_PATH . 'functions/identity.php');
15
16
17//
18// ripped from src/compose.php
19//
20
21/* This function is used when not sending or adding attachments */
22function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $session='') {
23    global $editor_size, $default_use_priority, $body, $idents,
24        $use_signature, $data_dir, $username,
25        $key, $imapServerAddress, $imapPort,
26        $composeMessage, $body_quote, $request_mdn, $request_dr,
27        $mdn_user_support, $languages, $squirrelmail_language,
28        $default_charset;
29
30    /*
31     * Set $default_charset to correspond with the user's selection
32     * of language interface. $default_charset global is not correct,
33     * if message is composed in new window.
34     */
35    set_my_charset();
36
37    $send_to = $send_to_cc = $send_to_bcc = $subject = $identity = '';
38    $mailprio = 3;
39
40    if ($passed_id) {
41        $imapConnection = sqimap_login($username, false, $imapServerAddress,
42                $imapPort, 0);
43
44        sqimap_mailbox_select($imapConnection, $mailbox);
45        $message = sqimap_get_message($imapConnection, $passed_id, $mailbox);
46
47        $body = '';
48        if ($passed_ent_id) {
49            /* redefine the messsage in case of message/rfc822 */
50            $message = $message->getEntity($passed_ent_id);
51            /* message is an entity which contains the envelope and type0=message
52             * and type1=rfc822. The actual entities are childs from
53             * $message->entities[0]. That's where the encoding and is located
54             */
55
56            $entities = $message->entities[0]->findDisplayEntity
57                (array(), $alt_order = array('text/plain'));
58            if (!count($entities)) {
59                $entities = $message->entities[0]->findDisplayEntity
60                    (array(), $alt_order = array('text/plain','text/html'));
61            }
62            $orig_header = $message->rfc822_header; /* here is the envelope located */
63            /* redefine the message for picking up the attachments */
64            $message = $message->entities[0];
65
66        } else {
67            $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain'));
68            if (!count($entities)) {
69                $entities = $message->findDisplayEntity (array(), $alt_order = array('text/plain','text/html'));
70            }
71            $orig_header = $message->rfc822_header;
72        }
73
74        $type0 = $message->type0;
75        $type1 = $message->type1;
76        foreach ($entities as $ent) {
77            $msg = $message->getEntity($ent);
78            $type0 = $msg->type0;
79            $type1 = $msg->type1;
80            $unencoded_bodypart = mime_fetch_body($imapConnection, $passed_id, $ent);
81            $body_part_entity = $message->getEntity($ent);
82            $bodypart = decodeBody($unencoded_bodypart,
83                    $body_part_entity->header->encoding);
84            if ($type1 == 'html') {
85                $bodypart = str_replace("\n", ' ', $bodypart);
86                $bodypart = preg_replace(array('/<\/?p>/i','/<div><\/div>/i','/<br\s*(\/)*>/i','/<\/?div>/i'), "\n", $bodypart);
87                $bodypart = str_replace(array('&nbsp;','&gt;','&lt;'),array(' ','>','<'),$bodypart);
88                $bodypart = strip_tags($bodypart);
89            }
90            if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
91                    function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode')) {
92                if (mb_detect_encoding($bodypart) != 'ASCII') {
93                    $bodypart = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_decode', $bodypart);
94                }
95            }
96
97            // charset encoding in compose form stuff
98            if (isset($body_part_entity->header->parameters['charset'])) {
99                $actual = $body_part_entity->header->parameters['charset'];
100            } else {
101                $actual = 'us-ascii';
102            }
103
104            if ( $actual && is_conversion_safe($actual) && $actual != $default_charset){
105                $bodypart = charset_convert($actual,$bodypart,$default_charset,false);
106            }
107            // end of charset encoding in compose
108
109            $body .= $bodypart;
110        }
111        if ($default_use_priority) {
112            $mailprio = substr($orig_header->priority,0,1);
113            if (!$mailprio) {
114                $mailprio = 3;
115            }
116        } else {
117            $mailprio = '';
118        }
119
120        $from_o = $orig_header->from;
121        if (is_array($from_o)) {
122            if (isset($from_o[0])) {
123                $from_o = $from_o[0];
124            }
125        }
126        if (is_object($from_o)) {
127            $orig_from = $from_o->getAddress();
128        } else {
129            $orig_from = '';
130        }
131
132        $identities = array();
133        if (count($idents) > 1) {
134            foreach($idents as $nr=>$data) {
135                $enc_from_name = '"'.$data['full_name'].'" <'. $data['email_address'].'>';
136                if(strtolower($enc_from_name) == strtolower($orig_from)) {
137                    $identity = $nr;
138                    break;
139                }
140                $identities[] = $enc_from_name;
141            }
142
143            $identity_match = $orig_header->findAddress($identities);
144            if ($identity_match) {
145                $identity = $identity_match;
146            }
147        }
148
149        switch ($action) {
150            case ('draft'):
151                $use_signature = FALSE;
152                $composeMessage->rfc822_header = $orig_header;
153                $send_to = decodeHeader($orig_header->getAddr_s('to'),false,false,true);
154                $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'),false,false,true);
155                $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'),false,false,true);
156                $send_from = $orig_header->getAddr_s('from');
157                $send_from_parts = new AddressStructure();
158                $send_from_parts = $orig_header->parseAddress($send_from);
159                $send_from_add = $send_from_parts->mailbox . '@' . $send_from_parts->host;
160                $identity = find_identity(array($send_from_add));
161                $subject = decodeHeader($orig_header->subject,false,false,true);
162
163                // Remember the receipt settings
164                $request_mdn = $mdn_user_support && !empty($orig_header->dnt) ? '1' : '0';
165                $request_dr = $mdn_user_support && !empty($orig_header->drnt) ? '1' : '0';
166
167                /* remember the references and in-reply-to headers in case of an reply */
168//FIXME: it would be better to fiddle with headers inside of the message object or possibly when delivering the message to its destination (drafts folder?); is this possible?
169                $composeMessage->rfc822_header->more_headers['References'] = $orig_header->references;
170                $composeMessage->rfc822_header->more_headers['In-Reply-To'] = $orig_header->in_reply_to;
171                // rewrap the body to clean up quotations and line lengths
172                sqBodyWrap($body, $editor_size);
173                $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
174                break;
175            case ('edit_as_new'):
176                $send_to = decodeHeader($orig_header->getAddr_s('to'),false,false,true);
177                $send_to_cc = decodeHeader($orig_header->getAddr_s('cc'),false,false,true);
178                $send_to_bcc = decodeHeader($orig_header->getAddr_s('bcc'),false,false,true);
179                $subject = decodeHeader($orig_header->subject,false,false,true);
180                $mailprio = $orig_header->priority;
181                $orig_from = '';
182                $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
183                // rewrap the body to clean up quotations and line lengths
184                sqBodyWrap($body, $editor_size);
185                break;
186            case ('forward'):
187                $send_to = '';
188                $subject = getforwardSubject(decodeHeader($orig_header->subject,false,false,true));
189                $body = getforwardHeader($orig_header) . $body;
190                // the logic for calling sqUnWordWrap here would be to allow the browser to wrap the lines
191                // forwarded message text should be as undisturbed as possible, so commenting out this call
192                // sqUnWordWrap($body);
193                $composeMessage = getAttachments($message, $composeMessage, $passed_id, $entities, $imapConnection);
194
195                //add a blank line after the forward headers
196                $body = "\n" . $body;
197                break;
198            case ('forward_as_attachment'):
199                $subject = getforwardSubject(decodeHeader($orig_header->subject,false,false,true));
200                $composeMessage = getMessage_RFC822_Attachment($message, $composeMessage, $passed_id, $passed_ent_id, $imapConnection);
201                $body = '';
202                break;
203            case ('reply_all'):
204                if(isset($orig_header->mail_followup_to) && $orig_header->mail_followup_to) {
205                    $send_to = $orig_header->getAddr_s('mail_followup_to');
206                } else {
207                    $send_to_cc = replyAllString($orig_header);
208                    $send_to_cc = decodeHeader($send_to_cc,false,false,true);
209                }
210            case ('reply'):
211                // skip this if send_to was already set right above here
212                if(!$send_to) {
213                    $send_to = $orig_header->reply_to;
214                    if (is_array($send_to) && count($send_to)) {
215                        $send_to = $orig_header->getAddr_s('reply_to');
216                    } else if (is_object($send_to)) { /* unneccesarry, just for failsafe purpose */
217                        $send_to = $orig_header->getAddr_s('reply_to');
218                    } else {
219                        $send_to = $orig_header->getAddr_s('from');
220                    }
221                }
222                $send_to = decodeHeader($send_to,false,false,true);
223                $subject = decodeHeader($orig_header->subject,false,false,true);
224                $subject = str_replace('"', "'", $subject);
225                $subject = trim($subject);
226                if (substr(strtolower($subject), 0, 3) != 're:') {
227                    $subject = 'Re: ' . $subject;
228                }
229                /* this corrects some wrapping/quoting problems on replies */
230                $rewrap_body = explode("\n", $body);
231                $from = (is_array($orig_header->from) && !empty($orig_header->from)) ? $orig_header->from[0] : $orig_header->from;
232                $body = '';
233                $strip_sigs = getPref($data_dir, $username, 'strip_sigs');
234                foreach ($rewrap_body as $line) {
235                    if ($strip_sigs && substr($line,0,3) == '-- ') {
236                        break;
237                    }
238                    if (preg_match("/^(>+)/", $line, $matches)) {
239                        $gt = $matches[1];
240                        $body .= $body_quote . str_replace("\n", "\n$body_quote$gt ", rtrim($line)) ."\n";
241                    } else {
242                        $body .= $body_quote . (!empty($body_quote) ? ' ' : '') . str_replace("\n", "\n$body_quote" . (!empty($body_quote) ? ' ' : ''), rtrim($line)) . "\n";
243                    }
244                }
245
246                //rewrap the body to clean up quotations and line lengths
247                $body = sqBodyWrap ($body, $editor_size);
248
249                $body = getReplyCitation($from , $orig_header->date) . $body;
250                $composeMessage->reply_rfc822_header = $orig_header;
251
252                break;
253            default:
254                break;
255        }
256//FIXME: we used to register $compose_messages in the session here, but not any more - so do we still need the session_write_close() and sqimap_logout() here?  We probably need the IMAP logout, but what about the session closure?
257/// CHANGE FOR SPAM BUTTONS PLUGIN -- comment out next 2 lines
258///        session_write_close();
259///        sqimap_logout($imapConnection);
260    }
261    $ret = array( 'send_to' => $send_to,
262            'send_to_cc' => $send_to_cc,
263            'send_to_bcc' => $send_to_bcc,
264            'subject' => $subject,
265            'mailprio' => $mailprio,
266            'body' => $body,
267            'identity' => $identity );
268
269    return ($ret);
270} /* function newMail() */
271
272
273function getforwardSubject($subject)
274{
275    if ((substr(strtolower($subject), 0, 4) != 'fwd:') &&
276            (substr(strtolower($subject), 0, 5) != '[fwd:') &&
277            (substr(strtolower($subject), 0, 6) != '[ fwd:')) {
278        $subject = '[Fwd: ' . $subject . ']';
279    }
280    return $subject;
281}
282
283
284function getMessage_RFC822_Attachment($message, $composeMessage, $passed_id,
285        $passed_ent_id='', $imapConnection) {
286    if (!$passed_ent_id) {
287        $body_a = sqimap_run_command($imapConnection,
288                'FETCH '.$passed_id.' RFC822',
289                TRUE, $response, $readmessage,
290                TRUE);
291    } else {
292        $body_a = sqimap_run_command($imapConnection,
293                'FETCH '.$passed_id.' BODY['.$passed_ent_id.']',
294                TRUE, $response, $readmessage, TRUE);
295        $message = $message->parent;
296    }
297    if ($response == 'OK') {
298        $subject = encodeHeader($message->rfc822_header->subject);
299        array_shift($body_a);
300        array_pop($body_a);
301        $body = implode('', $body_a) . "\r\n";
302
303        global $username, $attachment_dir;
304        $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
305        $localfilename = sq_get_attach_tempfile();
306        $fp = fopen($hashed_attachment_dir . '/' . $localfilename, 'wb');
307        fwrite ($fp, $body);
308        fclose($fp);
309        $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
310                $localfilename);
311    }
312    return $composeMessage;
313}
314
315
316/**
317 * temporary function to make use of the deliver class.
318 * In the future the responsible backend should be automaticly loaded
319 * and conf.pl should show a list of available backends.
320 * The message also should be constructed by the message class.
321 *
322 * @param object $composeMessage The message being sent.  Please note
323 *                               that it is passed by reference and
324 *                               will be returned modified, with additional
325 *                               headers, such as Message-ID, Date, In-Reply-To,
326 *                               References, and so forth.
327 *
328 * @return boolean FALSE if delivery failed, or some non-FALSE value
329 *                 upon success.
330 *
331 */
332function deliverMessage(&$composeMessage, $draft=false) {
333    global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body,
334        $username, $identity, $idents, $data_dir,
335        $request_mdn, $request_dr, $default_charset, $useSendmail,
336        $domain, $action, $default_move_to_sent, $move_to_sent,
337        $imapServerAddress, $imapPort, $sent_folder, $key;
338/// CHANGE FOR SPAM_BUTTONS PLUGIN
339/* --- do we need to do overrides again here?  should not need to, but
340//     someone reported problems with the default not being overriden
341//     when using this reporting method ---
342   global $spam_report_email_method, $spam_report_smtpServerAddress,
343          $spam_report_smtpPort, $spam_report_useSendmail,
344          $spam_report_smtp_auth_mech, $spam_report_use_smtp_tls,
345          $smtpServerAddress, $smtpPort, $useSendmail, $smtp_auth_mech,
346          $use_smtp_tls, $sb_debug;
347
348   spam_buttons_init();
349
350
351   // take care of overrides for SMTP server
352   //
353   if (!empty($spam_report_smtpServerAddress))
354      $smtpServerAddress = $spam_report_smtpServerAddress;
355   if (!empty($spam_report_smtpPort))
356      $smtpPort = $spam_report_smtpPort;
357   if ($spam_report_useSendmail !== '')
358      $useSendmail = $spam_report_useSendmail;
359   if (!empty($spam_report_smtp_auth_mech))
360      $smtp_auth_mech = $spam_report_smtp_auth_mech;
361   if (!empty($spam_report_use_smtp_tls))
362      $use_smtp_tls = $spam_report_use_smtp_tls;
363--- */
364
365    $rfc822_header = $composeMessage->rfc822_header;
366
367    $abook = addressbook_init(false, true);
368    $rfc822_header->to = $rfc822_header->parseAddress($send_to,true, array(), '', $domain, array(&$abook,'lookup'));
369    $rfc822_header->cc = $rfc822_header->parseAddress($send_to_cc,true,array(), '',$domain, array(&$abook,'lookup'));
370    $rfc822_header->bcc = $rfc822_header->parseAddress($send_to_bcc,true, array(), '',$domain, array(&$abook,'lookup'));
371    $rfc822_header->priority = $mailprio;
372    $rfc822_header->subject = $subject;
373
374    $special_encoding='';
375    if (strtolower($default_charset) == 'iso-2022-jp') {
376        if (mb_detect_encoding($body) == 'ASCII') {
377            $special_encoding = '8bit';
378        } else {
379            $body = mb_convert_encoding($body, 'JIS');
380            $special_encoding = '7bit';
381        }
382    }
383    $composeMessage->setBody($body);
384
385    $reply_to = '';
386    $reply_to  = $idents[$identity]['reply_to'];
387
388    $from_addr = build_from_header($identity);
389    $rfc822_header->from = $rfc822_header->parseAddress($from_addr,true);
390    if ($reply_to) {
391        $rfc822_header->reply_to = $rfc822_header->parseAddress($reply_to,true);
392    }
393    /* Receipt: On Read */
394    if (isset($request_mdn) && $request_mdn) {
395        $rfc822_header->dnt = $rfc822_header->parseAddress($from_addr,true);
396    } elseif (isset($rfc822_header->dnt)) {
397        unset($rfc822_header->dnt);
398    }
399
400    /* Receipt: On Delivery */
401    if (!empty($request_dr)) {
402//FIXME: it would be better to fiddle with headers inside of the message object or possibly when delivering the message to its destination; is this possible?
403        $rfc822_header->more_headers['Return-Receipt-To'] = $from->mailbox.'@'.$from->domain;
404    } elseif (isset($rfc822_header->more_headers['Return-Receipt-To'])) {
405        unset($rfc822_header->more_headers['Return-Receipt-To']);
406    }
407
408    /* multipart messages */
409    if (count($composeMessage->entities)) {
410        $message_body = new Message();
411        $message_body->body_part = $composeMessage->body_part;
412        $composeMessage->body_part = '';
413        $mime_header = new MessageHeader;
414        $mime_header->type0 = 'text';
415        $mime_header->type1 = 'plain';
416        if ($special_encoding) {
417            $mime_header->encoding = $special_encoding;
418        } else {
419            $mime_header->encoding = '8bit';
420        }
421        if ($default_charset) {
422            $mime_header->parameters['charset'] = $default_charset;
423        }
424        $message_body->mime_header = $mime_header;
425        array_unshift($composeMessage->entities, $message_body);
426        $content_type = new ContentType('multipart/mixed');
427    } else {
428        $content_type = new ContentType('text/plain');
429        if ($special_encoding) {
430            $rfc822_header->encoding = $special_encoding;
431        } else {
432            $rfc822_header->encoding = '8bit';
433        }
434        if ($default_charset) {
435            $content_type->properties['charset']=$default_charset;
436        }
437    }
438
439    $rfc822_header->content_type = $content_type;
440    $composeMessage->rfc822_header = $rfc822_header;
441    if ($action == 'reply' || $action == 'reply_all') {
442        global $passed_id, $passed_ent_id;
443        $reply_id = $passed_id;
444        $reply_ent_id = $passed_ent_id;
445    } else {
446        $reply_id = '';
447        $reply_ent_id = '';
448    }
449
450    /* Here you can modify the message structure just before we hand
451       it over to deliver; plugin authors note that $composeMessage
452       is sent and modified by reference since 1.5.2 */
453/// CHANGE FOR SPAM BUTTONS PLUGIN -- comment out next line
454///    do_hook('compose_send', $composeMessage);
455
456    if (!$useSendmail && !$draft) {
457        require_once(SM_PATH . 'class/deliver/Deliver_SMTP.class.php');
458        $deliver = new Deliver_SMTP();
459        global $smtpServerAddress, $smtpPort, $pop_before_smtp;
460
461        $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ? true : false;
462        get_smtp_user($user, $pass);
463        $stream = $deliver->initStream($composeMessage,$domain,0,
464                $smtpServerAddress, $smtpPort, $user, $pass, $authPop);
465    } elseif (!$draft) {
466        require_once(SM_PATH . 'class/deliver/Deliver_SendMail.class.php');
467        global $sendmail_path, $sendmail_args;
468        // Check for outdated configuration
469        if (!isset($sendmail_args)) {
470            if ($sendmail_path=='/var/qmail/bin/qmail-inject') {
471                $sendmail_args = '';
472            } else {
473                $sendmail_args = '-i -t';
474            }
475        }
476        $deliver = new Deliver_SendMail(array('sendmail_args'=>$sendmail_args));
477        $stream = $deliver->initStream($composeMessage,$sendmail_path);
478    } elseif ($draft) {
479        global $draft_folder;
480        $imap_stream = sqimap_login($username, false, $imapServerAddress,
481                $imapPort, 0);
482        if (sqimap_mailbox_exists ($imap_stream, $draft_folder)) {
483            require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
484            $imap_deliver = new Deliver_IMAP();
485            $success = $imap_deliver->mail($composeMessage, $imap_stream, $reply_id, $reply_ent_id, $imap_stream, $draft_folder);
486            sqimap_logout($imap_stream);
487            unset ($imap_deliver);
488            $composeMessage->purgeAttachments();
489            return $success;
490        } else {
491//FIXME: htmlspecialchars is applied when msg is assigned to template; is it safe to remove it from the following line?
492//SPAM BUTTONS NOTE: htmlspecialchars is not applied to $draft_folder below in anticipation of the core using sq_htmlspecialchars when strings are assinged to the template
493            $msg  = "\n"
494                  . sprintf(_("Error: Draft folder %s does not exist."), $draft_folder);
495            plain_error_message($msg);
496            return false;
497        }
498    }
499    $success = false;
500    if ($stream) {
501        $deliver->mail($composeMessage, $stream, $reply_id, $reply_ent_id);
502        $success = $deliver->finalizeStream($stream);
503    }
504    if (!$success) {
505        // $deliver->dlv_server_msg is not always server's reply
506        $msg = _("Message not sent.") . "\n" .
507            $deliver->dlv_msg;
508        if (!empty($deliver->dlv_server_msg)) {
509            // add 'server replied' part only when it is not empty.
510            // Delivery error can be generated by delivery class itself
511            $msg .= "\n"
512                 . _("Server replied:") . ' ' . $deliver->dlv_ret_nr . ' ' .
513//SPAM BUTTONS NOTE: htmlspecialchars is not applied to $deliver->dlv_server_msg below in anticipation of the core using sq_htmlspecialchars when strings are assinged to the template
514                nl2br($deliver->dlv_server_msg);
515        }
516        plain_error_message($msg);
517    } else {
518        unset ($deliver);
519        $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
520
521
522        // mark as replied or forwarded if applicable
523        //
524        global $what, $iAccount, $startMessage, $passed_id, $mailbox;
525
526        if ($action=='reply' || $action=='reply_all' || $action=='forward' || $action=='forward_as_attachment') {
527            require(SM_PATH . 'functions/mailbox_display.php');
528            $aMailbox = sqm_api_mailbox_select($imap_stream, $iAccount, $mailbox,array('setindex' => $what, 'offset' => $startMessage),array());
529            switch($action) {
530            case 'reply':
531            case 'reply_all':
532                // check if we are allowed to set the \\Answered flag
533                if (in_array('\\answered',$aMailbox['PERMANENTFLAGS'], true)) {
534                    $aUpdatedMsgs = sqimap_toggle_flag($imap_stream, array($passed_id), '\\Answered', true, false);
535                    if (isset($aUpdatedMsgs[$passed_id]['FLAGS'])) {
536                        /**
537                        * Only update the cached headers if the header is
538                        * cached.
539                        */
540                        if (isset($aMailbox['MSG_HEADERS'][$passed_id])) {
541                            $aMailbox['MSG_HEADERS'][$passed_id]['FLAGS'] = $aMsg['FLAGS'];
542                        }
543                    }
544                }
545                break;
546            case 'forward':
547            case 'forward_as_attachment':
548                // check if we are allowed to set the $Forwarded flag (RFC 4550 paragraph 2.8)
549                if (in_array('$forwarded',$aMailbox['PERMANENTFLAGS'], true) ||
550                    in_array('\\*',$aMailbox['PERMANENTFLAGS'])) {
551
552                    $aUpdatedMsgs = sqimap_toggle_flag($imap_stream, array($passed_id), '$Forwarded', true, false);
553                    if (isset($aUpdatedMsgs[$passed_id]['FLAGS'])) {
554                        if (isset($aMailbox['MSG_HEADERS'][$passed_id])) {
555                            $aMailbox['MSG_HEADERS'][$passed_id]['FLAGS'] = $aMsg['FLAGS'];
556                        }
557                    }
558                }
559                break;
560            }
561
562            /**
563             * Write mailbox with updated seen flag information back to cache.
564             */
565            if(isset($aUpdatedMsgs[$passed_id])) {
566                $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox;
567                sqsession_register($mailbox_cache,'mailbox_cache');
568            }
569
570        }
571
572
573        // move to sent folder
574        //
575/// CHANGE FOR SPAM_BUTTONS PLUGIN (added next 2 lines)
576global $sb_keep_copy_in_sent;
577if ($sb_keep_copy_in_sent) {
578        $move_to_sent = getPref($data_dir,$username,'move_to_sent');
579        if (isset($default_move_to_sent) && ($default_move_to_sent != 0)) {
580            $svr_allow_sent = true;
581        } else {
582            $svr_allow_sent = false;
583        }
584
585        if (isset($sent_folder) && (($sent_folder != '') || ($sent_folder != 'none'))
586                && sqimap_mailbox_exists( $imap_stream, $sent_folder)) {
587            $fld_sent = true;
588        } else {
589            $fld_sent = false;
590        }
591
592        if ((isset($move_to_sent) && ($move_to_sent != 0)) || (!isset($move_to_sent))) {
593            $lcl_allow_sent = true;
594        } else {
595            $lcl_allow_sent = false;
596        }
597
598        if (($fld_sent && $svr_allow_sent && !$lcl_allow_sent) || ($fld_sent && $lcl_allow_sent)) {
599            if ($action == 'reply' || $action == 'reply_all') {
600                $save_reply_with_orig=getPref($data_dir,$username,'save_reply_with_orig');
601                if ($save_reply_with_orig) {
602                    $sent_folder = $mailbox;
603                }
604            }
605            require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
606            $imap_deliver = new Deliver_IMAP();
607            $imap_deliver->mail($composeMessage, $imap_stream, $reply_id, $reply_ent_id, $imap_stream, $sent_folder);
608            unset ($imap_deliver);
609        }
610/// CHANGE FOR SPAM_BUTTONS PLUGIN (added next line)
611}
612
613
614        // final cleanup
615        //
616        $composeMessage->purgeAttachments();
617        sqimap_logout($imap_stream);
618
619    }
620    return $success;
621}
622