Example #1
0
/**
 * Run an IMAP test and return the results
 *
 * @param mixed $imap_stream
 * @param string $string imap command
 * @return array Response from the IMAP server
 * @access private
 */
function imap_test($imap_stream, $string)
{
    print "<tr><td>" . sm_encode_html_special_chars($string) . "</td></tr>";
    $response = sqimap_run_command_list($imap_stream, trim($string), false, $responses, $message, false);
    array_push($response, $responses . ' ' . $message);
    return $response;
}
Example #2
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;
}
Example #3
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 #4
0
/**
 * Obsolete?
 */
function sqimap_get_headerfield($imap_stream, $field)
{
    global $uid_support, $squirrelmail_language;
    $sid = sqimap_session_id(false);
    $results = array();
    $read_list = array();
    $query = "FETCH 1:* (UID BODY.PEEK[HEADER.FIELDS ({$field})])";
    $readin_list = sqimap_run_command_list($imap_stream, $query, true, $response, $message, $uid_support);
    $i = 0;
    foreach ($readin_list as $r) {
        $r = implode('', $r);
        /* first we unfold the header */
        $r = str_replace(array("\r\n", "\n\t", "\n\\s"), array("\n", '', ''), $r);
        /*
         * now we can make a new header array with each element representing
         * a headerline
         */
        $r = explode("\n", $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.' . $r[0] . "</font><br />\n";
            } else {
                $id = $regs[1];
            }
        } 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.' . $r[0] . "</font><br />\n";
            } else {
                $id = $regs[2];
            }
        }
        $field = $r[1];
        $field = substr($field, strlen($field) + 2);
        $result[] = array($id, $field);
    }
    return $result;
}
Example #5
0
/**
 * Run the IMAP SEARCH command as defined in rfc 3501
 * @link http://www.ietf.org/rfc/rfc3501.txt
 * @param resource $imapConnection the current imap stream
 * @param string $search_string the full search expression eg "ALL RECENT"
 * @param string $search_charset charset to use or zls ('')
 * @return array an IDs or UIDs array of matching messages or an empty array
 * @since 1.5.0
 */
function sqimap_run_search($imapConnection, $search_string, $search_charset)
{
    //For some reason, this seems to happen and forbids searching servers not allowing OPTIONAL [CHARSET]
    if (strtoupper($search_charset) == 'US-ASCII') {
        $search_charset = '';
    }
    /* 6.4.4 try OPTIONAL [CHARSET] specification first */
    if ($search_charset != '') {
        $query = 'SEARCH CHARSET "' . strtoupper($search_charset) . '" ' . $search_string;
    } else {
        $query = 'SEARCH ' . $search_string;
    }
    $readin = sqimap_run_command_list($imapConnection, $query, false, $response, $message, TRUE);
    /* 6.4.4 try US-ASCII charset if we tried an OPTIONAL [CHARSET] and received a tagged NO response (SHOULD be [BADCHARSET]) */
    if ($search_charset != '' && strtoupper($response) == 'NO') {
        $query = 'SEARCH CHARSET US-ASCII ' . $search_string;
        $readin = sqimap_run_command_list($imapConnection, $query, false, $response, $message, TRUE);
    }
    if (strtoupper($response) != 'OK') {
        sqimap_asearch_error_box($response, $query, $message);
        return array();
    }
    $messagelist = parseUidList($readin, 'SEARCH');
    if (empty($messagelist)) {
        //Empty search response, ie '* SEARCH'
        return array();
    }
    $cnt = count($messagelist);
    for ($q = 0; $q < $cnt; $q++) {
        $id[$q] = trim($messagelist[$q]);
    }
    return $id;
}
Example #6
0
function showMessagesForMailbox($imapConnection, $in_box, $ok_box, $er_box, $domain, $skannauskansio)
{
    // Valitaan inboxi
    $mbxresponse = sqimap_mailbox_select($imapConnection, $in_box);
    // haetaan inboxin kaikki viestit
    $query = "SEARCH UID 1:*";
    $read_list = sqimap_run_command_list($imapConnection, $query, true, $response, $message, '');
    // Otetaan messageid:t
    if (preg_match("/\\* SEARCH ([0-9 ]*)/", $read_list[0][0], $matches)) {
        $messaget = explode(" ", trim($matches[1]));
    } else {
        $messaget = array();
    }
    $messu_seqid_corr = 0;
    foreach ($messaget as $messu_seqid) {
        // Oliks tää ok maili
        $is_ok = FALSE;
        $messu_seqid = $messu_seqid - $messu_seqid_corr;
        // Haetaan viestin UID
        $query = "FETCH {$messu_seqid} (UID)";
        $fetch_uid = sqimap_run_command_list($imapConnection, $query, true, $response, $message, '');
        if (isset($fetch_uid[0][0]) and preg_match("/\\(UID ([0-9]*)\\)/", $fetch_uid[0][0], $matches)) {
            $uid = $matches[1];
        } else {
            continue;
        }
        // Haetaan viestin From
        $query = "UID FETCH {$uid} (BODY[HEADER.FIELDS (From)])";
        $fetch_from = sqimap_run_command_list($imapConnection, $query, true, $response, $message, '');
        // Onko lähettäjän domaini ok?
        $okdomain = FALSE;
        if (empty($domain)) {
            $okdomain = TRUE;
        } elseif (is_array($domain) and count($domain) > 0) {
            foreach ($domain as $d_d) {
                if (preg_match("/[a-z\\.]*?@{$d_d}/i", $fetch_from[0][1])) {
                    $okdomain = TRUE;
                    break;
                }
            }
            if (!$okdomain) {
                echo "Laskuja hyväksytään vain ";
                foreach ($domain as $d_d) {
                    echo $d_d . " ";
                }
                echo "domainista, " . $fetch_from[0][1] . "\n";
            }
        } elseif (!is_array($domain) and $domain != '') {
            if (preg_match("/[a-z\\.]*?@{$domain}/i", $fetch_from[0][1])) {
                $okdomain = TRUE;
            } else {
                echo "Laskuja hyväksytään vain {$domain} domainista, " . $fetch_from[0][1] . "\n";
            }
        }
        if ($okdomain) {
            // Haetaan viestin BODYSTRUCTURE
            $query = "UID FETCH {$uid} (BODYSTRUCTURE)";
            $fetch_message = sqimap_run_command_list($imapConnection, $query, true, $response, $message, '');
            preg_match("/BODYSTRUCTURE \\((.*)\\)/", $fetch_message[0][0], $matches);
            $bodyt = explode(")(", $matches[1]);
            for ($bodyind = 1; $bodyind <= count($bodyt); $bodyind++) {
                // Haetaan viestin BODY, tai siis osa siitä
                $query = "UID FETCH {$uid} (BODY[{$bodyind}])";
                $fetch_body_part = sqimap_run_command_list($imapConnection, $query, true, $response, $message, '');
                if (preg_match("/\\(BODY\\[{$bodyind}\\] NIL\\)/", $fetch_body_part[0][0])) {
                    break;
                }
                // Fetchataan filename
                if (preg_match("/\\(\"(FILE)?NAME\" \"(.*?)\"\\)/", $bodyt[$bodyind - 1], $matches)) {
                    // Ok maili
                    $is_ok = TRUE;
                    $path_parts = pathinfo($matches[2]);
                    $name = strtoupper($path_parts['filename']);
                    $ext = strtoupper($path_parts['extension']);
                    // Hyväksytyt filet
                    if (strtoupper($ext) != "JPG" and strtoupper($ext) != "JPEG" and strtoupper($ext) != "PNG" and strtoupper($ext) != "GIF" and strtoupper($ext) != "PDF") {
                        echo "Ainoastaan .jpg .gif .png .pdf tiedostot sallittuja!\n";
                        continue;
                    }
                    // Kirjoitetaan liitetiedosto levylle
                    $attachmentbody = "";
                    for ($line = 1; $line < count($fetch_body_part[0]) - 1; $line++) {
                        $attachmentbody .= trim($fetch_body_part[0][$line]);
                    }
                    $attachmentbody = base64_decode($attachmentbody);
                    // Katotaan, ettei samalla nimellä oo jo laskua jonossa
                    if (file_exists($skannauskansio . "/" . $matches[2])) {
                        $kala = 1;
                        $filename = $matches[2];
                        while (file_exists($skannauskansio . "/" . $filename)) {
                            $filename = $kala . "_" . $matches[2];
                            $kala++;
                        }
                        $matches[2] = $filename;
                    }
                    file_put_contents($skannauskansio . "/" . $matches[2], $attachmentbody);
                }
            }
        }
        $movebox = $is_ok ? $ok_box : $er_box;
        // Siiretään maili sopivaan kansioon
        $query = "UID COPY {$uid} {$movebox}";
        $response = sqimap_run_command_list($imapConnection, $query, true, $response, $message, '');
        $query = "UID STORE {$uid} +flags (\\Deleted)";
        $response = sqimap_run_command_list($imapConnection, $query, true, $response, $message, '');
        // Sekvenssinumero penenee automnaattisesti sitä mukaa kun siirretään maileja veke kansiosta
        $messu_seqid_corr++;
    }
}