X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=functions%2Fimap_asearch.php;h=0c2c21c6960fe6c9136cde9b39197b930f9e59e2;hb=d754283878a277a733ea552fa4b37191c5c737ee;hp=045da53cbe6967acaeb12a3a957544fa1c394c5b;hpb=00b05f037e230ba00d42e52e0222591534a126ad;p=squirrelmail.git diff --git a/functions/imap_asearch.php b/functions/imap_asearch.php index 045da53c..0c2c21c6 100644 --- a/functions/imap_asearch.php +++ b/functions/imap_asearch.php @@ -3,17 +3,23 @@ /** * imap_search.php * - * Copyright (c) 1999-2003 The SquirrelMail Project Team + * Copyright (c) 1999-2004 The SquirrelMail Project Team * Licensed under the GNU GPL. For full terms see the file COPYING. * * IMAP asearch routines - * @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr + * + * Subfolder search idea from Patch #806075 by Thomas Pohl xraven at users.sourceforge.net. Thanks Thomas! + * + * @version $Id$ * @package squirrelmail + * @subpackage imap * @see search.php - * + * @link http://www.ietf.org/rfc/rfc3501.txt + * @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr */ -/** This functionality requires the IMAP and date functions */ +/** This functionality requires the IMAP and date functions + */ require_once(SM_PATH . 'functions/imap_general.php'); require_once(SM_PATH . 'functions/date.php'); @@ -22,11 +28,11 @@ require_once(SM_PATH . 'functions/date.php'); */ $imap_asearch_debug_dump = FALSE; -/** Array of imap SEARCH opcodes +/** Imap SEARCH keys * @global array $imap_asearch_opcodes */ $imap_asearch_opcodes = array( -/* => 'asequence', */ +/* => 'asequence', */ // Special handling, @see sqimap_asearch_build_criteria() /*'ALL' is binary operator */ 'ANSWERED' => '', 'BCC' => 'astring', @@ -37,7 +43,7 @@ $imap_asearch_opcodes = array( 'DRAFT' => '', 'FLAGGED' => '', 'FROM' => 'astring', - 'HEADER' => 'afield', /* Special syntax for this one, see below */ + 'HEADER' => 'afield', // Special syntax for this one, @see sqimap_asearch_build_criteria() 'KEYWORD' => 'akeyword', 'LARGER' => 'anum', 'NEW' => '', @@ -89,7 +95,8 @@ $imap_error_titles = array( 'OK' => '', 'NO' => _("ERROR : Could not complete request."), 'BAD' => _("ERROR : Bad or malformed request."), - 'BYE' => _("ERROR : Imap server closed the connection.") + 'BYE' => _("ERROR : Imap server closed the connection."), + '' => _("ERROR : Connection dropped by imap-server.") ); /** @@ -99,21 +106,24 @@ $imap_error_titles = array( * @global array imap_error_titles * @param string $response the imap server response code * @param string $query the failed query - * @param string $message the error message + * @param string $message an optional error message + * @param string $link an optional link to try again */ //@global array color sm colors array -function sqimap_asearch_error_box($response, $query, $message) +function sqimap_asearch_error_box($response, $query, $message, $link = '') { global $imap_error_titles; - //if (!array_key_exists($response, $imap_error_titles)) //php 4.0.6 compatibility - if (!in_array($response, array_keys($imap_error_titles))) + if (!array_key_exists($response, $imap_error_titles)) $title = _("ERROR : Unknown imap response."); else $title = $imap_error_titles[$response]; - $message_title = _("Reason Given: "); + if ($link == '') + $message_title = _("Reason Given: "); + else + $message_title = _("Possible reason : "); if (function_exists('sqimap_error_box')) - sqimap_error_box($title, $query, $message_title, $message); + sqimap_error_box($title, $query, $message_title, $message, $link); else { //Straight copy of 1.5 imap_general.php:sqimap_error_box(). Can be removed at a later time global $color; require_once(SM_PATH . 'functions/display_messages.php'); @@ -124,21 +134,24 @@ function sqimap_asearch_error_box($response, $query, $message) $string .= $message_title; if ($message != '') $string .= htmlspecialchars($message); + if ($link != '') + $string .= $link; $string .= "
\n"; error_box($string,$color); } } /** - * This is to avoid the E_NOTICE warnings signaled by marc AT squirrelmail.org. Thanks Marc! - * @param mixed $var any variable - * @return mixed zls ('') if $var is not defined, otherwise $var + * This is a convenient way to avoid spreading if (isset(... all over the code + * @param mixed $var any variable (reference) + * @param mixed $def default value to return if unset (default is zls (''), pass 0 or array() when appropriate) + * @return mixed $def if $var is unset, otherwise $var */ -function asearch_nz(&$var) +function asearch_nz(&$var, $def = '') { if (isset($var)) return $var; - return ''; + return $def; } /** @@ -160,7 +173,7 @@ function asearch_unhtmlentities($string) { /** * Provide an easy way to dump the imap dialogue if $imap_asearch_debug_dump is TRUE - * @global imap_asearch_debug_dump + * @global bool imap_asearch_debug_dump * @param string $var_name * @param string $var_var */ @@ -203,7 +216,7 @@ function sqimap_asearch_encode_string($what, $charset) /** * Parses a user date string into an rfc 3501 date string * Handles space, slash, backslash, dot and comma as separators (and dash of course ;=) - * @global imap_asearch_months + * @global array imap_asearch_months * @param string user date * @return array a preg_match-style array: * - [0] = fully formatted rfc 3501 date string (--<4 digit year>) @@ -257,10 +270,22 @@ function sqimap_asearch_build_criteria($opcode, $what, $charset) switch ($imap_asearch_opcodes[$opcode]) { default: case 'anum': -// $what = str_replace(' ', '', $what); - $what = ereg_replace('[^0-9]+', '', $what); - if ($what != '') + $what = str_replace(' ', '', $what); + $what = ereg_replace('[^0-9]+[^KMG]$', '', strtoupper($what)); + if ($what != '') { + switch (substr($what, -1)) { + case 'G': + $what = substr($what, 0, -1) << 30; + break; + case 'M': + $what = substr($what, 0, -1) << 20; + break; + case 'K': + $what = substr($what, 0, -1) << 10; + break; + } $criteria = $opcode . ' ' . $what . ' '; + } break; case '': //aflag $criteria = $opcode . ' '; @@ -292,11 +317,11 @@ function sqimap_asearch_build_criteria($opcode, $what, $charset) /** * Another way to do array_values(array_unique(array_merge($to, $from))); - * @param array $to to array + * @param array $to to array (reference) * @param array $from from array * @return array uniquely merged array */ -function sqimap_array_merge_unique($to, $from) +function sqimap_array_merge_unique(&$to, $from) { if (empty($to)) return $from; @@ -310,7 +335,7 @@ function sqimap_array_merge_unique($to, $from) /** * Run the imap SEARCH command as defined in rfc 3501 - * @link ftp://ftp.rfc-editor.org/in-notes/rfc3501.txt + * @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 ('') @@ -318,35 +343,34 @@ function sqimap_array_merge_unique($to, $from) */ function sqimap_run_search($imapConnection, $search_string, $search_charset) { - global $uid_support; - + //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) . '" ALL ' . $search_string; else $query = 'SEARCH ALL ' . $search_string; s_debug_dump('C:', $query); - $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support); + $readin = sqimap_run_command($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 ALL ' . $search_string; s_debug_dump('C:', $query); - $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support); + $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, TRUE); } if (strtoupper($response) != 'OK') { sqimap_asearch_error_box($response, $query, $message); return array(); } - unset($messagelist); - // Keep going till we find the * SEARCH response foreach ($readin as $readin_part) { s_debug_dump('S:', $readin_part); if (substr($readin_part, 0, 9) == '* SEARCH ') { //EIMS returns multiple SEARCH responses, and this allowed according to Mark Crispin - $messagelist = sqimap_array_merge_unique($message_list, preg_split("/ /", substr($readin_part, 9))); + $messagelist = sqimap_array_merge_unique($messagelist, preg_split("/ /", substr($readin_part, 9))); } } @@ -370,22 +394,24 @@ function sqimap_run_search($imapConnection, $search_string, $search_charset) */ function sqimap_run_sort($imapConnection, $search_string, $search_charset, $sort_criteria) { - global $uid_support; - if ($search_charset == '') $search_charset = 'US-ASCII'; - $query = 'SORT (' . $sort_criteria . ') ' . strtoupper($search_charset) . ' ALL ' . $search_string; + $query = 'SORT (' . $sort_criteria . ') "' . strtoupper($search_charset) . '" ALL ' . $search_string; s_debug_dump('C:', $query); - $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support); + $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, TRUE); + s_debug_dump('S:', $response); /* 6.4 try US-ASCII charset if we received a tagged NO response (SHOULD be [BADCHARSET]) */ if (($search_charset != 'US-ASCII') && (strtoupper($response) == 'NO')) { + s_debug_dump('S:', $readin); $query = 'SORT (' . $sort_criteria . ') US-ASCII ALL ' . $search_string; s_debug_dump('C:', $query); - $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support); + $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, TRUE); + s_debug_dump('S:', $response); } if (strtoupper($response) != 'OK') { + s_debug_dump('S:', $readin); // sqimap_asearch_error_box($response, $query, $message); // return array(); return sqimap_run_search($imapConnection, $search_string, $search_charset); // Fell back to standard search @@ -396,7 +422,7 @@ function sqimap_run_sort($imapConnection, $search_string, $search_charset, $sort s_debug_dump('S:', $readin_part); if (substr($readin_part, 0, 7) == '* SORT ') { //SORT returns untagged responses - $messagelist = sqimap_array_merge_unique($message_list, preg_split("/ /", substr($readin_part, 7))); + $messagelist = sqimap_array_merge_unique($messagelist, preg_split("/ /", substr($readin_part, 7))); } } @@ -417,8 +443,8 @@ function sqimap_run_sort($imapConnection, $search_string, $search_charset, $sort * @param string $search_charset mandatory charset * @param string $thread_algorithm the threading algorithm "ORDEREDSUBJECT" or "REFERENCES" * @return array an IDs or UIDs array of matching messages or an empty array - * @global array $thread_new will be used by thread view in mailbox_display - * @global array $server_sort_array will be used by thread view in mailbox_display + * @global array thread_new will be used by thread view in mailbox_display + * @global array server_sort_array will be used by thread view in mailbox_display */ function sqimap_run_thread($imapConnection, $search_string, $search_charset, $thread_algorithm) { @@ -434,30 +460,42 @@ function sqimap_run_thread($imapConnection, $search_string, $search_charset, $th $server_sort_array = array(); - global $uid_support; - if ($search_charset == '') $search_charset = 'US-ASCII'; - $query = 'THREAD ' . $thread_algorithm . ' ' . strtoupper($search_charset) . ' ALL ' . $search_string; + $query = 'THREAD ' . $thread_algorithm . ' "' . strtoupper($search_charset) . '" ALL ' . $search_string; s_debug_dump('C:', $query); - $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support); + $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, TRUE); + s_debug_dump('S:', $response); /* 6.4 try US-ASCII charset if we received a tagged NO response (SHOULD be [BADCHARSET]) */ if (($search_charset != 'US-ASCII') && (strtoupper($response) == 'NO')) { + s_debug_dump('S:', $readin); $query = 'THREAD ' . $thread_algorithm . ' US-ASCII ALL ' . $search_string; s_debug_dump('C:', $query); - $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support); + $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, TRUE); + s_debug_dump('S:', $response); } if (strtoupper($response) != 'OK') { + s_debug_dump('S:', $readin); + if (empty($response)) { //imap server closed connection. We can't go further. /* we should at this point: - warn the user that the THREAD call has failed - (offer him a way to) disconnect it permanently in the prefs - perform the regular search instead or provide a way to do it in one click */ -// sqimap_asearch_error_box($response, $query, $message); -// return array(); - return sqimap_run_search($imapConnection, $search_string, $search_charset); // Fell back to standard search + global $sort, $mailbox, $php_self; + $message = _("The imap server failed to handle threading."); + $unthread = _("Click here to unset thread view for this mailbox and start again."); + if (preg_match('/^(.+)\?.+$/', $php_self, $regs)) + $source_url = $regs[1]; + else + $source_url = $php_self; + $link = '' . $unthread . ''; + sqimap_asearch_error_box($response, $query, $message, $link); + return array(); + } + return sqimap_run_search($imapConnection, $search_string, $search_charset); // Fell back to standard search } /* Keep going till we find the * THREAD response */ @@ -528,30 +566,64 @@ function sqimap_asearch_get_charset() * - if the searched mailbox is the sent folder then TO is being used instead of FROM * - reverse order by using REVERSE * @param string $mailbox mailbox name to sort - * @param integer $sort_by sm sort criteria + * @param integer $sort_by sm sort criteria index + * @global bool internal_date_sort sort by arrival date instead of message date + * @global string sent_folder sent folder name * @return string imap sort criteria */ function sqimap_asearch_get_sort_criteria($mailbox, $sort_by) { global $internal_date_sort, $sent_folder; - $sort_opcodes = array ('DATE', 'FROM', 'SUBJECT'); + $sort_opcodes = array ('DATE', 'FROM', 'SUBJECT', 'SIZE'); if ($internal_date_sort == true) $sort_opcodes[0] = 'ARRIVAL'; // if (handleAsSent($mailbox)) // if (isSentFolder($mailbox)) if ($mailbox == $sent_folder) $sort_opcodes[1] = 'TO'; - return (($sort_by % 2) ? '' : 'REVERSE ') . $sort_opcodes[$sort_by >> 1]; + return (($sort_by % 2) ? '' : 'REVERSE ') . $sort_opcodes[($sort_by >> 1) & 3]; +} + +/** + * @param string $cur_mailbox unformatted mailbox name + * @param array $boxes_unformatted selectable mailbox unformatted names array (reference) + * @return array sub mailboxes unformatted names + */ +function sqimap_asearch_get_sub_mailboxes($cur_mailbox, &$mboxes_array) +{ + $sub_mboxes_array = array(); + $boxcount = count($mboxes_array); + for ($boxnum=0; $boxnum < $boxcount; $boxnum++) { + if (isBoxBelow($mboxes_array[$boxnum], $cur_mailbox)) + $sub_mboxes_array[] = $mboxes_array[$boxnum]; + } + return $sub_mboxes_array; } /** * Performs the search, given all the criteria, merging results for every mailbox + * @param resource $imapConnection + * @param array $mailbox_array (reference) + * @param array $biop_array (reference) + * @param array $unop_array (reference) + * @param array $where_array (reference) + * @param array $what_array (reference) + * @param array $exclude_array (reference) + * @param array $sub_array (reference) + * @param array $mboxes_array selectable unformatted mailboxes names (reference) + * @global bool allow_server_sort comes from config.php + * @global integer sort sm internal sort order + * @global bool allow_thread_sort comes from config.php + * @global bool thread_sort_messages does it really need to global? + * @global integer sort_by_ref thread by references + * @global string data_dir + * @global string username * @return array array(mailbox => array(UIDs)) */ -function sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $mboxes_array) +function sqimap_asearch($imapConnection, &$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array, &$mboxes_array) { - global $allow_server_sort, $sort, $allow_thread_sort, $thread_sort_messages; + global $allow_server_sort, $sort, $allow_thread_sort, $thread_sort_messages, $sort_by_ref; global $data_dir, $username; $search_charset = sqimap_asearch_get_charset(); @@ -565,8 +637,10 @@ function sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_arra $next_mailbox = $mailbox_array[$cur_crit]; if ($next_mailbox != $cur_mailbox) { $search_string = trim($search_string); /* Trim out last space */ - if (($cur_mailbox == 'All Folders') && (!empty($mboxes_array))) - $search_mboxes = $mboxes_array; + if ($cur_mailbox == 'All Folders') + $search_mboxes = $mboxes_array; + else if ((!empty($sub_array[$cur_crit - 1])) || (!in_array($cur_mailbox, $mboxes_array))) + $search_mboxes = sqimap_asearch_get_sub_mailboxes($cur_mailbox, $mboxes_array); else $search_mboxes = array($cur_mailbox); foreach ($search_mboxes as $cur_mailbox) { @@ -574,7 +648,10 @@ function sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_arra sqimap_mailbox_select($imapConnection, $cur_mailbox); $thread_sort_messages = $allow_thread_sort && getPref($data_dir, $username, 'thread_' . $cur_mailbox); if ($thread_sort_messages) { - $thread_algorithm = 'REFERENCES'; + if ($sort_by_ref == 1) + $thread_algorithm = 'REFERENCES'; + else + $thread_algorithm = 'ORDEREDSUBJECT'; $found_msgs = sqimap_run_thread($imapConnection, $search_string, $search_charset, $thread_algorithm); } else