Example #1
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');
    }
}
Example #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');
    }
}
Example #3
0
/**
 * Loops through all the Received Headers to find IP Addresses
 * @param stream imap_stream the stream to read from
 * @access private
 */
function spam_filters($imap_stream)
{
    global $data_dir, $username;
    global $SpamFilters_YourHop;
    global $SpamFilters_DNScache;
    global $SpamFilters_SharedCache;
    global $SpamFilters_BulkQuery;
    global $SpamFilters_CacheTTL;
    $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 $Value) {
        if ($Value['enabled']) {
            $run = true;
            break;
        }
    }
    // short-circuit
    if (!$run) {
        return;
    }
    // Ask for a big list of all "Received" headers in the inbox with
    // flags for each message.  Kinda big.
    if ($filters_spam_scan == 'new') {
        $search_array = array();
        $read = sqimap_run_command($imap_stream, 'SEARCH UNSEEN', true, $response, $message, TRUE);
        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)) {
        $headers = sqimap_get_small_header_list($imap_stream, $search_array, array('Received'), array());
    } else {
        if ($filters_spam_scan != 'new') {
            $headers = sqimap_get_small_header_list($imap_stream, null, array('Received'), array());
        } else {
            return;
        }
    }
    if (!count($headers)) {
        return;
    }
    $bulkquery = strlen($SpamFilters_BulkQuery) > 0 ? true : false;
    $IPs = array();
    $aSpamIds = array();
    foreach ($headers as $id => $aValue) {
        if (isset($aValue['UID'])) {
            $MsgNum = $aValue['UID'];
        } else {
            $MsgNum = $id;
        }
        // Look through all of the Received headers for IP addresses
        if (isset($aValue['RECEIVED'])) {
            foreach ($aValue['RECEIVED'] as $received) {
                // Check to see if this line is the right "Received from" line
                // to check
                // $aValue['Received'] is an array with all the received lines.
                // We should check them from bottom to top and only check the first 2.
                // Currently we check only the header where $SpamFilters_YourHop in occures
                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, $aMatch)) {
                        $isspam = false;
                        if (filters_spam_check_site($aMatch[1], $aMatch[2], $aMatch[3], $aMatch[4], $filters)) {
                            $aSpamIds[] = $MsgNum;
                            $isspam = true;
                        }
                        if ($bulkquery) {
                            array_shift($aMatch);
                            $IP = explode('.', $aMatch);
                            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 we've checked one IP and YourHop is
                        // just a space
                        if ($SpamFilters_YourHop == ' ' || $isspam) {
                            break;
                            // don't check any more
                        }
                    }
                }
            }
        }
    }
    // Lookie!  It's spam!  Yum!
    if (count($aSpamIds) && sqimap_mailbox_exists($imap_stream, $filters_spam_folder)) {
        if (!sqimap_msgs_list_move($imap_stream, $aSpamIds, $filters_spam_folder)) {
            // if errors occurred, don't try to filter again during this session
            sqsession_register(TRUE, 'filters_error');
            global $color;
            error_box(_("A problem occurred filtering messages. Check filter settings and account quota if applicable. Filtering is disabled for the remainder of this login session."), $color);
        }
        // expunge even in the case of errors, in case some
        // messages were filtered before the error happened
        sqimap_mailbox_expunge($imap_stream, 'INBOX');
    }
    if ($bulkquery && count($IPs)) {
        filters_bulkquery($filters, $IPs);
    }
    if ($SpamFilters_SharedCache) {
        filters_SaveCache();
    } else {
        sqsession_register($SpamFilters_DNScache, 'SpamFilters_DNScache');
    }
}