function sqimap_get_small_header_list($imap_stream, $msg_list)
{
    global $squirrelmail_language, $color, $data_dir, $username, $imap_server_type;
    global $uid_support, $allow_server_sort;
    /* Get the small headers for each message in $msg_list */
    $sid = sqimap_session_id($uid_support);
    $maxmsg = sizeof($msg_list);
    $msgs_str = sqimap_message_list_squisher($msg_list);
    $messages = array();
    $read_list = array();
    /*
     * We need to return the data in the same order as the caller supplied
     * in $msg_list, but IMAP servers are free to return responses in
     * whatever order they wish... So we need to re-sort manually
     */
    for ($i = 0; $i < sizeof($msg_list); $i++) {
        $id2index[$msg_list[$i]] = $i;
    }
    $internaldate = getPref($data_dir, $username, 'internal_date_sort');
    if ($internaldate) {
        $query = "{$sid} FETCH {$msgs_str} (FLAGS UID RFC822.SIZE INTERNALDATE BODY.PEEK[HEADER.FIELDS (Date To Cc From Subject X-Priority Content-Type)])\r\n";
    } else {
        $query = "{$sid} FETCH {$msgs_str} (FLAGS UID RFC822.SIZE BODY.PEEK[HEADER.FIELDS (Date To Cc From Subject X-Priority Content-Type)])\r\n";
    }
    fputs($imap_stream, $query);
    $readin_list = sqimap_read_data_list($imap_stream, $sid, false, $response, $message);
    $i = 0;
    foreach ($readin_list as $r) {
        if (!$uid_support) {
            if (!preg_match("/^\\*\\s+([0-9]+)\\s+FETCH/iAU", $r[0], $regs)) {
                set_up_language($squirrelmail_language);
                echo '<br><b><font color=$color[2]>' . _("ERROR : Could not complete request.") . '</b><br>' . _("Unknown response from IMAP server: ") . ' 1.' . htmlspecialchars($r[0]) . "</font><br>\n";
            } else {
                if (!isset($id2index[$regs[1]]) || !count($id2index[$regs[1]])) {
                    set_up_language($squirrelmail_language);
                    echo '<br><b><font color=$color[2]>' . _("ERROR : Could not complete request.") . '</b><br>' . _("Unknown message number in reply from server: ") . htmlspecialchars($regs[1]) . "</font><br>\n";
                } else {
                    $read_list[$id2index[$regs[1]]] = $r;
                }
            }
        } else {
            if (!preg_match("/^\\*\\s+([0-9]+)\\s+FETCH.*UID\\s+([0-9]+)\\s+/iAU", $r[0], $regs)) {
                set_up_language($squirrelmail_language);
                echo '<br><b><font color=$color[2]>' . _("ERROR : Could not complete request.") . '</b><br>' . _("Unknown response from IMAP server: ") . ' 1.' . htmlspecialchars($r[0]) . "</font><br>\n";
            } else {
                if (!isset($id2index[$regs[2]]) || !count($id2index[$regs[2]])) {
                    set_up_language($squirrelmail_language);
                    echo '<br><b><font color=$color[2]>' . _("ERROR : Could not complete request.") . '</b><br>' . _("Unknown message number in reply from server: ") . htmlspecialchars($regs[2]) . "</font><br>\n";
                } else {
                    $read_list[$id2index[$regs[2]]] = $r;
                    $unique_id = $regs[2];
                }
            }
        }
    }
    arsort($read_list);
    $patterns = array("/^To:(.*)\$/AUi", "/^From:(.*)\$/AUi", "/^X-Priority:(.*)\$/AUi", "/^Cc:(.*)\$/AUi", "/^Date:(.*)\$/AUi", "/^Subject:(.*)\$/AUi", "/^Content-Type:(.*)\$/AUi");
    $regpattern = '';
    for ($msgi = 0; $msgi < $maxmsg; $msgi++) {
        $subject = _("(no subject)");
        $from = _("Unknown Sender");
        $priority = 0;
        $messageid = '<>';
        $cc = $to = $date = $type[0] = $type[1] = $inrepto = '';
        $flag_seen = $flag_answered = $flag_deleted = $flag_flagged = false;
        $read = $read_list[$msgi];
        $prevline = false;
        foreach ($read as $read_part) {
            //unfold multi-line headers
            if ($prevline && $prevline[strlen($prevline) - 1] == "\n" && ($read_part[0] == ' ' || $read_part[0] == "\t")) {
                $read_part = substr($prevline, 0, -2) . preg_replace('/(\\t\\s+)/', ' ', $read_part);
            }
            $prevline = $read_part;
            if ($read_part[0] == '*') {
                if ($internaldate) {
                    if (preg_match("/^.+INTERNALDATE\\s+\"(.+)\".+/iUA", $read_part, $reg)) {
                        $tmpdate = trim($reg[1]);
                        $tmpdate = str_replace('  ', ' ', $tmpdate);
                        $tmpdate = explode(' ', $tmpdate);
                        $date = str_replace('-', ' ', $tmpdate[0]) . " " . $tmpdate[1] . ' ' . $tmpdate[2];
                    }
                }
                if (preg_match("/^.+RFC822.SIZE\\s+(\\d+).+/iA", $read_part, $reg)) {
                    $size = $reg[1];
                }
                if (preg_match("/^.+FLAGS\\s+\\((.*)\\).+/iUA", $read_part, $regs)) {
                    $flags = explode(' ', trim($regs[1]));
                    foreach ($flags as $flag) {
                        $flag = strtolower($flag);
                        if ($flag == '\\seen') {
                            $flag_seen = true;
                        } else {
                            if ($flag == '\\answered') {
                                $flag_answered = true;
                            } else {
                                if ($flag == '\\deleted') {
                                    $flag_deleted = true;
                                } else {
                                    if ($flag == '\\flagged') {
                                        $flag_flagged = true;
                                    }
                                }
                            }
                        }
                    }
                }
                if (preg_match("/^.+UID\\s+(\\d+).+/iA", $read_part, $reg)) {
                    $unique_id = $reg[1];
                }
            } else {
                $firstchar = strtoupper($read_part[0]);
                if ($firstchar == 'T') {
                    $regpattern = $patterns[0];
                    $id = 1;
                } else {
                    if ($firstchar == 'F') {
                        $regpattern = $patterns[1];
                        $id = 2;
                    } else {
                        if ($firstchar == 'X') {
                            $regpattern = $patterns[2];
                            $id = 3;
                        } else {
                            if ($firstchar == 'C') {
                                if (strtolower($read_part[1]) == 'c') {
                                    $regpattern = $patterns[3];
                                    $id = 4;
                                } else {
                                    if (strtolower($read_part[1]) == 'o') {
                                        $regpattern = $patterns[6];
                                        $id = 7;
                                    }
                                }
                            } else {
                                if ($firstchar == 'D' && !$internaldate) {
                                    $regpattern = $patterns[4];
                                    $id = 5;
                                } else {
                                    if ($firstchar == 'S') {
                                        $regpattern = $patterns[5];
                                        $id = 6;
                                    } else {
                                        $regpattern = '';
                                    }
                                }
                            }
                        }
                    }
                }
                if ($regpattern) {
                    if (preg_match($regpattern, $read_part, $regs)) {
                        switch ($id) {
                            case 1:
                                $to = trim($regs[1]);
                                break;
                            case 2:
                                $from = trim($regs[1]);
                                break;
                            case 3:
                                $priority = $regs[1];
                                break;
                            case 4:
                                $cc = trim($regs[1]);
                                break;
                            case 5:
                                $date = $regs[1];
                                break;
                            case 6:
                                $subject = trim($regs[1]);
                                if ($subject == "") {
                                    $subject = _("(no subject)");
                                }
                                break;
                            case 7:
                                $type = strtolower(trim($regs[1]));
                                if ($pos = strpos($type, ";")) {
                                    $type = substr($type, 0, $pos);
                                }
                                $type = explode("/", $type);
                                if (!is_array($type)) {
                                    $type[0] = 'text';
                                }
                                if (!isset($type[1])) {
                                    $type[1] = '';
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        }
        if (isset($date)) {
            $date = str_replace('  ', ' ', $date);
            $tmpdate = explode(' ', trim($date));
        } else {
            $tmpdate = $date = array('', '', '', '', '', '');
        }
        if ($uid_support) {
            $messages[$msgi]['ID'] = $unique_id;
        } else {
            $messages[$msgi]['ID'] = $msg_list[$msgi];
        }
        $messages[$msgi]['TIME_STAMP'] = getTimeStamp($tmpdate);
        $messages[$msgi]['DATE_STRING'] = getDateString($messages[$msgi]['TIME_STAMP']);
        $messages[$msgi]['FROM'] = parseAddress($from);
        $messages[$msgi]['SUBJECT'] = $subject;
        //        if (handleAsSent($mailbox)) {
        $messages[$msgi]['TO'] = parseAddress($to);
        //        }
        $messages[$msgi]['PRIORITY'] = $priority;
        $messages[$msgi]['CC'] = parseAddress($cc);
        $messages[$msgi]['SIZE'] = $size;
        $messages[$msgi]['TYPE0'] = $type[0];
        $messages[$msgi]['FLAG_DELETED'] = $flag_deleted;
        $messages[$msgi]['FLAG_ANSWERED'] = $flag_answered;
        $messages[$msgi]['FLAG_SEEN'] = $flag_seen;
        $messages[$msgi]['FLAG_FLAGGED'] = $flag_flagged;
        /* non server sort stuff */
        if (!$allow_server_sort) {
            $from = parseAddress($from);
            if ($from[0][1]) {
                $from = decodeHeader($from[0][1]);
            } else {
                $from = $from[0][0];
            }
            $messages[$msgi]['FROM-SORT'] = $from;
            $subject_sort = strtolower(decodeHeader($subject));
            if (preg_match("/^(vedr|sv|re|aw):\\s*(.*)\$/si", $subject_sort, $matches)) {
                $messages[$msgi]['SUBJECT-SORT'] = $matches[2];
            } else {
                $messages[$msgi]['SUBJECT-SORT'] = $subject_sort;
            }
        }
    }
    return $messages;
}
Exemplo n.º 2
0
/**
 * FIXME: Undocumented function
 * @access private
 */
function spam_filters($imap_stream)
{
    global $data_dir, $username, $uid_support;
    global $SpamFilters_YourHop;
    global $SpamFilters_DNScache;
    global $SpamFilters_SharedCache;
    global $SpamFilters_BulkQuery;
    $filters_spam_scan = getPref($data_dir, $username, 'filters_spam_scan');
    $filters_spam_folder = getPref($data_dir, $username, 'filters_spam_folder');
    $filters = load_spam_filters();
    if ($SpamFilters_SharedCache) {
        filters_LoadCache();
    }
    $run = false;
    foreach ($filters as $Key => $Value) {
        if ($Value['enabled']) {
            $run = true;
            break;
        }
    }
    // short-circuit
    if (!$run) {
        return;
    }
    sqimap_mailbox_select($imap_stream, 'INBOX');
    $search_array = array();
    if ($filters_spam_scan == 'new') {
        $read = sqimap_run_command($imap_stream, 'SEARCH UNSEEN', true, $response, $message, $uid_support);
        if (isset($read[0])) {
            for ($i = 0, $iCnt = count($read); $i < $iCnt; ++$i) {
                if (preg_match("/^\\* SEARCH (.+)\$/", $read[$i], $regs)) {
                    $search_array = explode(' ', trim($regs[1]));
                    break;
                }
            }
        }
    }
    if ($filters_spam_scan == 'new' && count($search_array)) {
        $msg_str = sqimap_message_list_squisher($search_array);
        $imap_query = 'FETCH ' . $msg_str . ' (FLAGS BODY.PEEK[HEADER.FIELDS (RECEIVED)])';
    } else {
        if ($filters_spam_scan != 'new') {
            $imap_query = 'FETCH 1:* (FLAGS BODY.PEEK[HEADER.FIELDS (RECEIVED)])';
        } else {
            return;
        }
    }
    $read = sqimap_run_command_list($imap_stream, $imap_query, true, $response, $message, $uid_support);
    if (isset($response) && $response != 'OK') {
        return;
    }
    $messages = parseFetch($read);
    $bulkquery = strlen($SpamFilters_BulkQuery) > 0 ? true : false;
    $aSpamIds = array();
    foreach ($messages as $id => $message) {
        if (isset($message['UID'])) {
            $MsgNum = $message['UID'];
        } else {
            $MsgNum = $id;
        }
        if (isset($message['received'])) {
            foreach ($message['received'] as $received) {
                if (is_int(strpos($received, $SpamFilters_YourHop))) {
                    if (preg_match('/([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})/', $received, $matches)) {
                        $IsSpam = false;
                        if (filters_spam_check_site($matches[1], $matches[2], $matches[3], $matches[4], $filters)) {
                            $aSpamIds[] = $MsgNum;
                            $IsSpam = true;
                        }
                        if ($bulkquery) {
                            array_shift($matches);
                            $IP = explode('.', $matches);
                            foreach ($filters as $key => $value) {
                                if ($filters[$key]['enabled'] && $filters[$key]['dns']) {
                                    if (strlen($SpamFilters_DNScache[$IP . '.' . $filters[$key]['dns']]) == 0) {
                                        $IPs[$IP] = true;
                                        break;
                                    }
                                }
                            }
                        }
                        if ($SpamFilters_YourHop == ' ' || $IsSpam) {
                            break;
                        }
                    }
                }
            }
        }
    }
    if (count($aSpamIds) && sqimap_mailbox_exists($imap_stream, $filters_spam_folder)) {
        sqimap_msgs_list_move($imap_stream, $aSpamIds, $filters_spam_folder);
        sqimap_mailbox_expunge($imap_stream, 'INBOX', true, $aSpamIds);
    }
    if ($bulkquery && count($IPs)) {
        filters_bulkquery($filters, $IPs);
    }
    if ($SpamFilters_SharedCache) {
        filters_SaveCache();
    } else {
        sqsession_register($SpamFilters_DNScache, 'SpamFilters_DNScache');
    }
}
Exemplo n.º 3
0
/**
 * Retrieves a list with headers, flags, size or internaldate from the imap server
 *
 * WARNING: function is not portable between SquirrelMail 1.2.x, 1.4.x and 1.5.x.
 * Output format, third argument and $msg_list array format requirements differ.
 * @param stream $imap_stream imap connection
 * @param array  $msg_list array with id's to create a msgs set from
 * @param array  $aHeaderFields (since 1.5.0) requested header fields
 * @param array  $aFetchItems (since 1.5.0) requested other fetch items like FLAGS, RFC822.SIZE
 * @return array $aMessages associative array with messages. Key is the UID, value is an associative array
 * @since 1.1.3
 */
function sqimap_get_small_header_list($imap_stream, $msg_list, $aHeaderFields = array('Date', 'To', 'Cc', 'From', 'Subject', 'X-Priority', 'Content-Type'), $aFetchItems = array('FLAGS', 'RFC822.SIZE', 'INTERNALDATE'))
{
    $aMessageList = array();
    /**
     * Catch other priority headers as well
     */
    if (in_array('X-Priority', $aHeaderFields, true)) {
        $aHeaderFields[] = 'Importance';
        $aHeaderFields[] = 'Priority';
    }
    $bUidFetch = !in_array('UID', $aFetchItems, true);
    /* Get the small headers for each message in $msg_list */
    if ($msg_list !== NULL) {
        $msgs_str = sqimap_message_list_squisher($msg_list);
        /*
         * We need to return the data in the same order as the caller supplied
         * in $msg_list, but IMAP servers are free to return responses in
         * whatever order they wish... So we need to re-sort manually
         */
        if ($bUidFetch) {
            for ($i = 0; $i < sizeof($msg_list); $i++) {
                $aMessageList["{$msg_list[$i]}"] = array();
            }
        }
    } else {
        $msgs_str = '1:*';
    }
    /*
     * Create the query
     */
    $sFetchItems = '';
    $query = "FETCH {$msgs_str} (";
    if (count($aFetchItems)) {
        $sFetchItems = implode(' ', $aFetchItems);
    }
    if (count($aHeaderFields)) {
        $sHeaderFields = implode(' ', $aHeaderFields);
        $sFetchItems .= ' BODY.PEEK[HEADER.FIELDS (' . $sHeaderFields . ')]';
    }
    $query .= trim($sFetchItems) . ')';
    $aResponse = sqimap_run_command_list($imap_stream, $query, true, $response, $message, $bUidFetch);
    $aMessages = parseFetch($aResponse, $aMessageList);
    array_reverse($aMessages);
    return $aMessages;
}
Exemplo n.º 4
0
function sqimap_toggle_flag($imap_stream, $id, $flag, $set, $handle_errors)
{
    global $uid_support;
    $msgs_id = sqimap_message_list_squisher($id);
    $set_string = $set ? '+' : '-';
    $read = sqimap_run_command($imap_stream, "STORE {$msgs_id} " . $set_string . "FLAGS ({$flag})", $handle_errors, $response, $message, $uid_support);
}
Exemplo n.º 5
0
/**
 * Expunges a mailbox, ie. delete all contents.
 */
function sqimap_mailbox_expunge($imap_stream, $mailbox, $handle_errors = true, $id = '')
{
    global $uid_support;
    if ($id) {
        if (is_array($id)) {
            $id = sqimap_message_list_squisher($id);
        }
        $id = ' ' . $id;
        $uid = $uid_support;
    } else {
        $uid = false;
    }
    $read = sqimap_run_command($imap_stream, 'EXPUNGE' . $id, $handle_errors, $response, $message, $uid);
    $cnt = 0;
    if (is_array($read)) {
        foreach ($read as $r) {
            if (preg_match('/^\\*\\s[0-9]+\\sEXPUNGE/AUi', $r, $regs)) {
                $cnt++;
            }
        }
    }
    return $cnt;
}
function bayesspam_prune($args)
{
    if ($args[0] != 'left_main_before' || !isset($_SESSION['just_logged_in']) || $_SESSION['just_logged_in'] != true) {
        return;
    }
    if ($GLOBALS['bayesdbhandle'] == null) {
        return;
    }
    if ($GLOBALS['bayesspam_prune_threshold'] == 0) {
        return;
    }
    $secsago = 86400 * ($GLOBALS['bayesspam_prune_threshold'] - 1);
    /* Convert days to seconds */
    $cutoffdate = date('d-M-Y', time() - $secsago);
    if (!sqimap_mailbox_exists($GLOBALS['imapConnection'], $GLOBALS['bayesspam_folder'])) {
        return;
    }
    $notrash = false;
    if (!sqimap_mailbox_exists($GLOBALS['imapConnection'], $GLOBALS['trash_folder'])) {
        $notrash = true;
    } else {
        $trashbox = sqimap_mailbox_select($GLOBALS['imapConnection'], $GLOBALS['trash_folder']);
        if (strtolower($trashbox['RIGHTS']) != "read-write") {
            $notrash = true;
        }
    }
    $mbx = sqimap_mailbox_select($GLOBALS['imapConnection'], $GLOBALS['bayesspam_folder']);
    if (strtolower($mbx['RIGHTS']) != "read-write") {
        return;
    }
    $query = "SEARCH SENTBEFORE {$cutoffdate}";
    $read = sqimap_run_command($GLOBALS['imapConnection'], $query, TRUE, $response, $message, $GLOBALS['uid_support']);
    $results = str_replace(' ', ',', substr($read[0], 9));
    $msglist = trim($results);
    /* get rid of that nasty whitespace */
    if (strlen($msglist) < 1) {
        return;
    }
    $msglist = sqimap_message_list_squisher(explode(' ', $msglist));
    if (!$notrash && $GLOBALS['default_move_to_trash']) {
        sqimap_msgs_list_move($GLOBALS['imapConnection'], $msglist, $GLOBALS['trash_folder']);
    } else {
        sqimap_msgs_list_delete($GLOBALS['imapConnection'], $GLOBALS['bayesspam_folder'], $msglist);
    }
    if ($GLOBALS['auto_expunge']) {
        sqimap_mailbox_expunge($GLOBALS['imapConnection'], $GLOBALS['bayesspam_folder']);
    }
}
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'] . '\'');
        }
    }
}
Exemplo n.º 8
0
/**
 * FIXME: Undocumented function
 * @access private
 */
function spam_filters($imap_stream)
{
    global $data_dir, $username, $uid_support;
    global $SpamFilters_YourHop;
    global $SpamFilters_DNScache;
    global $SpamFilters_SharedCache;
    global $SpamFilters_BulkQuery;
    $filters_spam_scan = getPref($data_dir, $username, 'filters_spam_scan');
    $filters_spam_folder = getPref($data_dir, $username, 'filters_spam_folder');
    $filters = load_spam_filters();
    if ($SpamFilters_SharedCache) {
        filters_LoadCache();
    }
    $run = 0;
    foreach ($filters as $Key => $Value) {
        if ($Value['enabled']) {
            $run++;
        }
    }
    // short-circuit
    if ($run == 0) {
        return;
    }
    sqimap_mailbox_select($imap_stream, 'INBOX');
    // Ask for a big list of all "Received" headers in the inbox with
    // flags for each message.  Kinda big.
    if ($filters_spam_scan != 'new') {
        $read = sqimap_run_command($imap_stream, 'FETCH 1:* (FLAGS BODY.PEEK[HEADER.FIELDS ' . '(RECEIVED)])', true, $response, $message, $uid_support);
    } else {
        $read = sqimap_run_command($imap_stream, 'SEARCH UNSEEN', true, $response, $message, $uid_support);
        if ($response != 'OK' || trim($read[0]) == '* SEARCH') {
            $read = sqimap_run_command($imap_stream, 'FETCH 1:* (FLAGS BODY.PEEK[HEADER.FIELDS ' . '(RECEIVED)])', true, $response, $message, $uid_support);
        } else {
            if (isset($read[0])) {
                if (preg_match("/^\\* SEARCH (.+)\$/", $read[0], $regs)) {
                    $search_array = preg_split("/ /", trim($regs[1]));
                }
            }
            $msgs_str = sqimap_message_list_squisher($search_array);
            $imap_query = 'FETCH ' . $msgs_str;
            $imap_query .= ' (FLAGS BODY.PEEK[HEADER.FIELDS ';
            $imap_query .= '(RECEIVED)])';
            $read = sqimap_run_command($imap_stream, $imap_query, true, $response, $message, $uid_support);
        }
    }
    if (isset($response) && $response != 'OK') {
        return;
    }
    if (strlen($SpamFilters_BulkQuery) > 0) {
        filters_bulkquery($filters_spam_scan, $filters, $read);
    }
    $i = 0;
    while ($i < count($read)) {
        // EIMS will give funky results
        $Chunks = explode(' ', $read[$i]);
        if ($Chunks[0] != '*') {
            $i++;
            continue;
        }
        $MsgNum = $Chunks[1];
        $IPs = array();
        $i++;
        $IsSpam = 0;
        // Look through all of the Received headers for IP addresses
        // Stop when I get ")" on a line
        // Stop if I get "*" on a line (don't advance)
        // and above all, stop if $i is bigger than the total # of lines
        while ($i < count($read) && ($read[$i][0] != ')' && $read[$i][0] != '*' && $read[$i][0] != "\n") && !$IsSpam) {
            // Check to see if this line is the right "Received from" line
            // to check
            if (is_int(strpos($read[$i], $SpamFilters_YourHop))) {
                // short-circuit and skip work if we don't scan this one
                $read[$i] = ereg_replace('[^0-9\\.]', ' ', $read[$i]);
                $elements = explode(' ', $read[$i]);
                foreach ($elements as $value) {
                    if ($value != '' && ereg('[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}', $value, $regs)) {
                        $Chunks = explode('.', $value);
                        if (filters_spam_check_site($Chunks[0], $Chunks[1], $Chunks[2], $Chunks[3], $filters)) {
                            $IsSpam++;
                            break;
                            // no sense in checking more IPs
                        }
                        // If we've checked one IP and YourHop is
                        // just a space
                        if ($SpamFilters_YourHop == ' ') {
                            break;
                            // don't check any more
                        }
                    }
                }
            }
            $i++;
        }
        // Lookie!  It's spam!  Yum!
        if ($IsSpam) {
            if (sqimap_mailbox_exists($imap_stream, $filters_spam_folder)) {
                // check if message copy was successful
                if (sqimap_messages_copy($imap_stream, $MsgNum, $MsgNum, $filters_spam_folder, false)) {
                    sqimap_messages_flag($imap_stream, $MsgNum, $MsgNum, 'Deleted', false);
                }
            }
        } else {
        }
    }
    sqimap_mailbox_expunge($imap_stream, 'INBOX');
    if ($SpamFilters_SharedCache) {
        filters_SaveCache();
    } else {
        sqsession_register($SpamFilters_DNScache, 'SpamFilters_DNScache');
    }
}