/**
 * Retrieves an array with a sorted uid list. Sorting is done on the imap server
 * @link http://www.ietf.org/internet-drafts/draft-ietf-imapext-sort-17.txt
 * @param resource $imap_stream IMAP socket connection
 * @param string $sSortField Field to sort on
 * @param bool $reverse Reverse order search
 * @return array $id sorted uid list
 */
function sqimap_get_sort_order($imap_stream, $sSortField, $reverse, $search = 'ALL')
{
    global $default_charset, $sent_folder;
    $id = array();
    $sort_test = array();
    $sort_query = '';
    if ($sSortField) {
        if ($reverse) {
            $sSortField = 'REVERSE ' . $sSortField;
        }
        $query = "SORT ({$sSortField}) " . strtoupper($default_charset) . " {$search}";
        // FIX ME sqimap_run_command should return the parsed data accessible by $aDATA['SORT']
        $aData = sqimap_run_command($imap_stream, $query, false, $response, $message, TRUE);
        /* fallback to default charset */
        if ($response == 'NO' && strpos($message, '[BADCHARSET]') !== false) {
            $query = "SORT ({$sSortField}) US-ASCII {$search}";
            $aData = sqimap_run_command($imap_stream, $query, true, $response, $message, TRUE);
        }
    }
    if ($response == 'OK') {
        return parseUidList($aData, 'SORT');
    } else {
        return false;
    }
}
Exemplo n.º 2
0
/**
 * Retrieves an array with a sorted uid list. Sorting is done on the imap server
 * @link http://www.ietf.org/internet-drafts/draft-ietf-imapext-sort-17.txt
 * @param resource $imap_stream IMAP socket connection
 * @param string $sSortField Field to sort on
 * @param bool $reverse Reverse order search
 * @return array $id sorted uid list
 */
function sqimap_get_sort_order($imap_stream, $sSortField, $reverse, $search = 'ALL')
{
    global $default_charset;
    if ($sSortField) {
        if ($reverse) {
            $sSortField = 'REVERSE ' . $sSortField;
        }
        $query = "SORT ({$sSortField}) " . strtoupper($default_charset) . " {$search}";
        // FIXME: sqimap_run_command() should return the parsed data accessible by $aDATA['SORT']
        // use sqimap_run_command_list() in case of unsolicited responses. If we don't we could loose the SORT response.
        $aData = sqimap_run_command_list($imap_stream, $query, false, $response, $message, TRUE);
        /* fallback to default charset */
        if ($response == 'NO') {
            if (strpos($message, 'BADCHARSET') !== false || strpos($message, 'character') !== false) {
                sqm_trigger_imap_error('SQM_IMAP_BADCHARSET', $query, $response, $message);
                $query = "SORT ({$sSortField}) US-ASCII {$search}";
                $aData = sqimap_run_command_list($imap_stream, $query, true, $response, $message, TRUE);
            } else {
                sqm_trigger_imap_error('SQM_IMAP_ERROR', $query, $response, $message);
            }
        } else {
            if ($response == 'BAD') {
                sqm_trigger_imap_error('SQM_IMAP_NO_SORT', $query, $response, $message);
            }
        }
    }
    if ($response == 'OK') {
        return parseUidList($aData, 'SORT');
    } else {
        return false;
    }
}
Exemplo n.º 3
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;
}