/** * 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; }
/** * 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'); } }