function sqimap_mailbox_tree($imap_stream)
{
    global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
    if (!isset($boxesnew)) {
        global $data_dir, $username, $list_special_folders_first, $folder_prefix, $delimiter, $trash_folder, $move_to_trash;
        $inbox_in_list = false;
        $inbox_subscribed = false;
        require_once SM_PATH . 'include/load_prefs.php';
        /* LSUB array */
        $lsub_ary = sqimap_run_command($imap_stream, "LSUB \"{$folder_prefix}\" \"*\"", true, $response, $message);
        /*
         * Section about removing the last element was removed 
         * We don't return "* OK" anymore from sqimap_read_data
         */
        $sorted_lsub_ary = array();
        $cnt = count($lsub_ary);
        for ($i = 0; $i < $cnt; $i++) {
            /*
             * Workaround for EIMS
             * Doesn't work if the mailbox name is multiple lines
             */
            if (isset($lsub_ary[$i + 1]) && ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)\$", $lsub_ary[$i], $regs)) {
                $i++;
                $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2];
            }
            /*
            if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$lsub_ary[$i],$regs)) {
                $flag = $regs[1];
                $mbx = trim($regs[3]);
                $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag); 
            }
            */
            $mbx = find_mailbox_name($lsub_ary[$i]);
            $noselect = check_is_noselect($lsub_ary[$i]);
            if (substr($mbx, -1) == $delimiter) {
                $mbx = substr($mbx, 0, strlen($mbx) - 1);
            }
            $sorted_lsub_ary[] = array('mbx' => $mbx, 'noselect' => $noselect);
        }
        array_multisort($sorted_lsub_ary, SORT_ASC, SORT_REGULAR);
        for ($i = 0; $i < $cnt; $i++) {
            if ($sorted_lsub_ary[$i]['mbx'] == 'INBOX') {
                $inbox_in_list = true;
                break;
            }
        }
        /*
         * Just in case they're not subscribed to their inbox,
         * we'll get it for them anyway
         */
        if (!$inbox_in_list) {
            $inbox_ary = sqimap_run_command($imap_stream, "LIST \"\" \"INBOX\"", true, $response, $message);
            /* Another workaround for EIMS */
            if (isset($inbox_ary[1]) && ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)\$", $inbox_ary[0], $regs)) {
                $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) . '"' . $regs[2];
            }
            $mbx = find_mailbox_name($inbox_ary[0]);
            if (substr($mbx, -1) == $delimiter) {
                $mbx = substr($mbx, 0, strlen($mbx) - 1);
            }
            if ($mbx == 'INBOX') {
                $sorted_lsub_ary[] = array('mbx' => $mbx, 'flag' => '');
                sqimap_subscribe($imap_stream, 'INBOX');
                $cnt++;
            }
            /*
            if (preg_match("/^\*\s+LIST\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$inbox_ary[0],$regs)) {
                $flag = $regs[1];
                $mbx = trim($regs[3]);
                if (substr($mbx, -1) == $delimiter) {
                    $mbx = substr($mbx, 0, strlen($mbx) - 1);
                }
                $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag); 
            }
            */
        }
        for ($i = 0; $i < $cnt; $i++) {
            $mbx = $sorted_lsub_ary[$i]['mbx'];
            if ($unseen_notify == 2 && $mbx == 'INBOX' || $unseen_notify == 3 || $move_to_trash && $mbx == $trash_folder) {
                if ($sorted_lsub_ary[$i]['noselect']) {
                    $sorted_lsub_ary[$i]['unseen'] = 0;
                } else {
                    $sorted_lsub_ary[$i]['unseen'] = sqimap_unseen_messages($imap_stream, $mbx);
                }
                if ($unseen_type == 2 || $move_to_trash && $mbx == $trash_folder || $mbx == $trash_folder) {
                    if ($sorted_lsub_ary[$i]['noselect']) {
                        $sorted_lsub_ary[$i]['nummessages'] = 0;
                    } else {
                        $sorted_lsub_ary[$i]['nummessages'] = sqimap_get_num_messages($imap_stream, $mbx);
                    }
                }
            }
        }
        $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary);
        return $boxesnew;
    }
}
function bayesspam_filters($imap_stream, $use_mailbox = 0)
{
    $spam_stats = 0;
    $ham_stats = 0;
    $unsure_stats = 0;
    $total_messages = 0;
    $run_folders = array();
    $boxes = sqimap_mailbox_list($imap_stream);
    foreach ($boxes as $box) {
        if ($GLOBALS['bayesspam_inboxonly'] != 1 || $GLOBALS['bayesspam_inboxonly'] == 1 && 'INBOX' == $box['unformatted-dm']) {
            if ($use_mailbox == 1 && $box['unformatted-dm'] == $GLOBALS['mailbox'] && !in_array($box['unformatted-dm'], $GLOBALS['bayesspam_ignore_folders']) || $use_mailbox == 0 && !in_array($box['unformatted-dm'], $GLOBALS['bayesspam_ignore_folders'])) {
                if ((array_search('noselect', $box['flags']) === FALSE || array_search('noselect', $box['flags']) === NULL) && $GLOBALS['sent_folder'] != $box['unformatted-dm'] && sqimap_unseen_messages($imap_stream, $box['unformatted-dm']) > 0) {
                    $run_folders[] = $box['unformatted-dm'];
                }
            }
        }
    }
    foreach ($run_folders as $box) {
        $spam_messages = array();
        $uncertain_messages = array();
        $mbxresponse = sqimap_mailbox_select($imap_stream, $box);
        $messages = array();
        $search = "SEARCH UNSEEN UNDELETED";
        if (isset($_SESSION['bayesspam_last_filter'])) {
            $search .= " SINCE " . date('d-M-Y', $_SESSION['bayesspam_last_filter']);
        }
        $_SESSION['bayesspam_last_filter'] = time();
        $read = sqimap_run_command($imap_stream, $search, TRUE, $response, $message, TRUE);
        if (isset($read[0])) {
            for ($i = 0, $iCnt = count($read); $i < $iCnt; ++$i) {
                if (preg_match("/^\\* SEARCH (.+)\$/", $read[$i], $regs)) {
                    $messages = preg_split("/ /", trim($regs[1]));
                    break;
                }
            }
        }
        foreach ($messages as $passed_id) {
            bayesspam_set_message_id($imap_stream, $passed_id);
            $bayesspam_check_messageid = bayesspam_check_messageid();
            if ($GLOBALS['bayesspam_do_stats'] && $GLOBALS['bayesspam_do_user_stats']) {
                $bayesspam_old_message_score = bayesspam_get_old_message_score();
            }
            $is_spam = bayesspam_get_probability($imap_stream, $passed_id, 1);
            if ($is_spam > 0.9) {
                $spam_messages[] = $passed_id;
            } elseif ($is_spam >= 0.1) {
                $uncertain_messages[] = $passed_id;
            }
            if ($GLOBALS['bayesspam_do_stats'] && $GLOBALS['bayesspam_do_user_stats'] && $bayesspam_old_message_score === FALSE) {
                if ($is_spam > 0.9) {
                    $spam_stats++;
                    $total_messages++;
                }
                if ($is_spam <= 0.9 && $is_spam >= 0.1) {
                    $unsure_stats++;
                    $total_messages++;
                }
                if ($is_spam < 0.1) {
                    $ham_stats++;
                    $total_messages++;
                }
            }
        }
        if ($spam_messages) {
            $message_str = sqimap_message_list_squisher($spam_messages);
            if ($GLOBALS['bayesspam_delete']) {
                sqimap_run_command($imap_stream, 'STORE ' . $message_str . ' +FLAGS (\\Deleted)', true, $response, $message, $GLOBALS['uid_support']);
            } elseif (sqimap_mailbox_exists($imap_stream, $GLOBALS['bayesspam_folder'])) {
                sqimap_run_command($imap_stream, 'COPY ' . $message_str . ' "' . $GLOBALS['bayesspam_folder'] . '"', true, $response, $message, $GLOBALS['uid_support']);
                sqimap_run_command($imap_stream, 'STORE ' . $message_str . ' +FLAGS (\\Deleted)', true, $response, $message, $GLOBALS['uid_support']);
            }
            sqimap_mailbox_expunge($imap_stream, $box);
        }
        if ($uncertain_messages && $GLOBALS['bayesspam_do_uncertain_filtering']) {
            $message_str = sqimap_message_list_squisher($uncertain_messages);
            if (sqimap_mailbox_exists($imap_stream, $GLOBALS['bayesspam_uncertain_folder'])) {
                sqimap_run_command($imap_stream, 'COPY ' . $message_str . ' "' . $GLOBALS['bayesspam_uncertain_folder'] . '"', true, $response, $message, $GLOBALS['uid_support']);
                sqimap_run_command($imap_stream, 'STORE ' . $message_str . ' +FLAGS (\\Deleted)', true, $response, $message, $GLOBALS['uid_support']);
            }
            sqimap_mailbox_expunge($imap_stream, $box);
        }
    }
    if ($GLOBALS['bayesspam_do_stats'] && $GLOBALS['bayesspam_do_user_stats'] && $total_messages) {
        $res = $GLOBALS['bayesdbhandle']->query('SELECT UserName FROM ' . $GLOBALS['bayesdbprefix'] . 'stats WHERE UserName=\'' . $GLOBALS['bayes_username'] . '\'');
        if (!DB::isError($res) && !($row = $res->fetchRow())) {
            $GLOBALS['bayesdbhandle']->query('INSERT INTO ' . $GLOBALS['bayesdbprefix'] . 'stats SET StatsStart=NOW(),UserName=\'' . $GLOBALS['bayes_username'] . '\',TotalMessages=' . $total_messages . ',HamMessages=' . $ham_stats . ',SpamMessages=' . $spam_stats . ',UnsureMessages=' . $unsure_stats . ($GLOBALS['bayesspam_do_timing'] ? ',TimedMessages=' . $GLOBALS['bayes_parsed_messages'] . ',TotalParseTime=' . $GLOBALS['bayes_parse_time'] : ''));
        } else {
            $GLOBALS['bayesdbhandle']->query('UPDATE ' . $GLOBALS['bayesdbprefix'] . 'stats SET TotalMessages=TotalMessages+' . $total_messages . ',HamMessages=HamMessages+' . $ham_stats . ',SpamMessages=SpamMessages+' . $spam_stats . ', UnsureMessages=UnsureMessages+' . $unsure_stats . ($GLOBALS['bayesspam_do_timing'] ? ',TimedMessages=TimedMessages+' . $GLOBALS['bayes_parsed_messages'] . ',TotalParseTime=TotalParseTime+' . $GLOBALS['bayes_parse_time'] : '') . ' WHERE UserName=\'' . $GLOBALS['bayes_username'] . '\'');
        }
    }
}
/**
 * @param stream $imap_stream imap connection resource
 * @param object $mbx_tree
 * @since since 1.5.0
 */
function sqimap_get_status_mbx_tree($imap_stream, &$mbx_tree)
{
    global $unseen_notify, $unseen_type, $trash_folder, $move_to_trash;
    $aMbxs = $aQuery = array();
    sqimap_tree_to_ref_array($mbx_tree, $aMbxs);
    // remove the root node
    array_shift($aMbxs);
    if ($unseen_notify == 3) {
        $cnt = count($aMbxs);
        for ($i = 0; $i < $cnt; ++$i) {
            $oMbx =& $aMbxs[$i];
            if (!$oMbx->is_noselect) {
                $mbx = $oMbx->mailboxname_full;
                if ($unseen_type == 2 || $move_to_trash && $oMbx->mailboxname_full == $trash_folder) {
                    $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (MESSAGES UNSEEN RECENT)';
                } else {
                    $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (UNSEEN RECENT)';
                }
                sqimap_prepare_pipelined_query($query, $tag, $aQuery, false);
            } else {
                $oMbx->unseen = $oMbx->total = $oMbx->recent = false;
                $tag = false;
            }
            $oMbx->tag = $tag;
            $aMbxs[$i] =& $oMbx;
        }
        // execute all the queries at once
        $aResponse = sqimap_run_pipelined_command($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
        $cnt = count($aMbxs);
        for ($i = 0; $i < $cnt; ++$i) {
            $oMbx =& $aMbxs[$i];
            $tag = $oMbx->tag;
            if ($tag && $aServerResponse[$tag] == 'OK') {
                $sResponse = implode('', $aResponse[$tag]);
                if (preg_match('/UNSEEN\\s+([0-9]+)/i', $sResponse, $regs)) {
                    $oMbx->unseen = $regs[1];
                }
                if (preg_match('/MESSAGES\\s+([0-9]+)/i', $sResponse, $regs)) {
                    $oMbx->total = $regs[1];
                }
                if (preg_match('/RECENT\\s+([0-9]+)/i', $sResponse, $regs)) {
                    $oMbx->recent = $regs[1];
                }
            }
            unset($oMbx->tag);
        }
    } else {
        if ($unseen_notify == 2) {
            // INBOX only
            $cnt = count($aMbxs);
            for ($i = 0; $i < $cnt; ++$i) {
                $oMbx =& $aMbxs[$i];
                if (strtoupper($oMbx->mailboxname_full) == 'INBOX' || $move_to_trash && $oMbx->mailboxname_full == $trash_folder) {
                    if ($unseen_type == 2 || $oMbx->mailboxname_full == $trash_folder && $move_to_trash) {
                        $aStatus = sqimap_status_messages($imap_stream, $oMbx->mailboxname_full);
                        $oMbx->unseen = $aStatus['UNSEEN'];
                        $oMbx->total = $aStatus['MESSAGES'];
                        $oMbx->recent = $aStatus['RECENT'];
                    } else {
                        $oMbx->unseen = sqimap_unseen_messages($imap_stream, $oMbx->mailboxname_full);
                    }
                    $aMbxs[$i] =& $oMbx;
                    if (!$move_to_trash && $trash_folder) {
                        break;
                    } else {
                        // trash comes after INBOX
                        if ($oMbx->mailboxname_full == $trash_folder) {
                            break;
                        }
                    }
                }
            }
        }
    }
    $cnt = count($aMbxs);
    for ($i = 0; $i < $cnt; ++$i) {
        $oMbx =& $aMbxs[$i];
        unset($hook_status);
        if (!empty($oMbx->unseen)) {
            $hook_status['UNSEEN'] = $oMbx->unseen;
        }
        if (!empty($oMbx->total)) {
            $hook_status['MESSAGES'] = $oMbx->total;
        }
        if (!empty($oMbx->recent)) {
            $hook_status['RECENT'] = $oMbx->recent;
        }
        if (!empty($hook_status)) {
            $hook_status['MAILBOX'] = $oMbx->mailboxname_full;
            $hook_status['CALLER'] = 'sqimap_get_status_mbx_tree';
            // helps w/ debugging
            do_hook('folder_status', $hook_status);
        }
    }
}