Update message copy and move functions to allow for custom handling / ignoring of...
[squirrelmail.git] / functions / imap_asearch.php
index 1b129564c100dbae6dc0d4c84718763de5aa19ee..dd4177ee4451ca1611b83877257e97a47709113d 100644 (file)
@@ -3,7 +3,7 @@
 /**
 * imap_search.php
 *
-* Copyright (c) 1999-2004 The SquirrelMail Project Team
+* Copyright (c) 1999-2005 The SquirrelMail Project Team
 * Licensed under the GNU GPL. For full terms see the file COPYING.
 *
 * IMAP asearch routines
@@ -31,8 +31,9 @@ $imap_asearch_debug_dump = FALSE;
 /** Imap SEARCH keys
 * @global array $imap_asearch_opcodes
 */
+global $imap_asearch_opcodes;
 $imap_asearch_opcodes = array(
-/* <sequence-set> => 'asequence', */   // Special handling, @see sqimap_asearch_build_criteria()
+/* <sequence-set> => 'asequence', */    // Special handling, @see sqimap_asearch_build_criteria()
 /*'ALL' is binary operator */
     'ANSWERED' => '',
     'BCC' => 'astring',
@@ -43,7 +44,7 @@ $imap_asearch_opcodes = array(
     'DRAFT' => '',
     'FLAGGED' => '',
     'FROM' => 'astring',
-    'HEADER' => 'afield',      // Special syntax for this one, @see sqimap_asearch_build_criteria()
+    'HEADER' => 'afield',    // Special syntax for this one, @see sqimap_asearch_build_criteria()
     'KEYWORD' => 'akeyword',
     'LARGER' => 'anum',
     'NEW' => '',
@@ -88,17 +89,6 @@ $imap_asearch_months = array(
     '12' => 'dec'
 );
 
-/** Error message titles according to imap server returned code
-* @global array $imap_error_titles
-*/
-$imap_error_titles = array(
-    'OK' => '',
-    'NO' => _("ERROR : Could not complete request."),
-    'BAD' => _("ERROR : Bad or malformed request."),
-    'BYE' => _("ERROR : Imap server closed the connection."),
-    '' => _("ERROR : Connection dropped by imap-server.")
-);
-
 /**
 * Function to display an error related to an IMAP-query.
 * We need to do our own error management since we may receive NO responses on purpose (even BAD with SORT or THREAD)
@@ -112,7 +102,16 @@ $imap_error_titles = array(
 //@global array color sm colors array
 function sqimap_asearch_error_box($response, $query, $message, $link = '')
 {
-    global $imap_error_titles;
+    global $color;
+    // Error message titles according to imap server returned code
+    $imap_error_titles = array(
+        'OK' => '',
+        'NO' => _("ERROR : Could not complete request."),
+        'BAD' => _("ERROR : Bad or malformed request."),
+        'BYE' => _("ERROR : Imap server closed the connection."),
+        '' => _("ERROR : Connection dropped by imap-server.")
+    );
+
 
     if (!array_key_exists($response, $imap_error_titles))
         $title = _("ERROR : Unknown imap response.");
@@ -124,19 +123,19 @@ function sqimap_asearch_error_box($response, $query, $message, $link = '')
         $message_title = _("Possible reason : ");
     if (function_exists('sqimap_error_box'))
         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
+    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');
-    $string = "<font color=$color[2]><b>\n" . $title . "</b><br>\n";
+    $string = "<font color=\"$color[2]\"><b>\n" . $title . "</b><br />\n";
     if ($query != '')
-        $string .= _("Query:") . ' ' . htmlspecialchars($query) . '<br>';
+        $string .= _("Query:") . ' ' . htmlspecialchars($query) . '<br />';
     if ($message_title != '')
         $string .= $message_title;
     if ($message != '')
         $string .= htmlspecialchars($message);
     if ($link != '')
         $string .= $link;
-    $string .= "</font><br>\n";
+    $string .= "</font><br />\n";
     error_box($string,$color);
     }
 }
@@ -162,7 +161,7 @@ function asearch_nz(&$var, $def = '')
 */
 function asearch_unhtmlentities($string) {
     $trans_tbl = array_flip(get_html_translation_table(HTML_ENTITIES));
-    for ($i=127; $i<255; $i++) /* Add &#<dec>; entities */
+    for ($i=127; $i<255; $i++)    /* Add &#<dec>; entities */
         $trans_tbl['&#' . $i . ';'] = chr($i);
     return strtr($string, $trans_tbl);
 /* I think the one above is quicker, though it should be benchmarked
@@ -181,8 +180,8 @@ function s_debug_dump($var_name, $var_var)
 {
     global $imap_asearch_debug_dump;
     if ($imap_asearch_debug_dump) {
-        if (function_exists('sm_print_r'))     //Only exists since 1.4.2
-            sm_print_r($var_name, $var_var);   //Better be the 'varargs' version ;)
+        if (function_exists('sm_print_r'))      //Only exists since 1.4.2
+            sm_print_r($var_name, $var_var);    //Better be the 'varargs' version ;)
         else {
             echo '<pre>';
             echo htmlentities($var_name);
@@ -195,21 +194,21 @@ function s_debug_dump($var_name, $var_var)
 /** Encode a string to quoted or literal as defined in rfc 3501
 *
 * -  4.3 String:
-*      A quoted string is a sequence of zero or more 7-bit characters,
-*       excluding CR and LF, with double quote (<">) characters at each end.
+*        A quoted string is a sequence of zero or more 7-bit characters,
+*         excluding CR and LF, with double quote (<">) characters at each end.
 * -  9. Formal Syntax:
-*      quoted-specials = DQUOTE / "\"
+*        quoted-specials = DQUOTE / "\"
 * @param string $what string to encode
 * @param string $charset search charset used
 * @return string encoded string
 */
 function sqimap_asearch_encode_string($what, $charset)
 {
-    if (strtoupper($charset) == 'ISO-2022-JP') // This should be now handled in imap_utf7_local?
+    if (strtoupper($charset) == 'ISO-2022-JP')    // This should be now handled in imap_utf7_local?
         $what = mb_convert_encoding($what, 'JIS', 'auto');
     if (preg_match('/["\\\\\r\n\x80-\xff]/', $what))
-        return '{' . strlen($what) . "}\r\n" . $what;  // 4.3 literal form
-    return '"' . $what . '"';  // 4.3 quoted string form
+        return '{' . strlen($what) . "}\r\n" . $what;    // 4.3 literal form
+    return '"' . $what . '"';    // 4.3 quoted string form
 }
 
 /**
@@ -233,7 +232,7 @@ function sqimap_asearch_parse_date($what)
         preg_match('/^([0-9]+)-+([^\-]+)-+([0-9]+)$/', $what, $what_parts);
         if (count($what_parts) == 4) {
             $what_month = strtolower(asearch_unhtmlentities($what_parts[2]));
-/*             if (!in_array($what_month, $imap_asearch_months)) {*/
+/*                if (!in_array($what_month, $imap_asearch_months)) {*/
                 foreach ($imap_asearch_months as $month_number => $month_code) {
                     if (($what_month == $month_number)
                     || ($what_month == $month_code)
@@ -245,7 +244,7 @@ function sqimap_asearch_parse_date($what)
                         break;
                     }
                 }
-/*             }*/
+/*                }*/
         }
     }
     else
@@ -286,10 +285,10 @@ function sqimap_asearch_build_criteria($opcode, $what, $charset)
                 $criteria = $opcode . ' ' . $what . ' ';
             }
         break;
-        case '':       //aflag
+        case '':    //aflag
             $criteria = $opcode . ' ';
         break;
-        case 'afield': /* HEADER field-name: field-body */
+        case 'afield':    /* HEADER field-name: field-body */
             preg_match('/^([^:]+):(.*)$/', $what, $what_parts);
             if (count($what_parts) == 3)
                 $criteria = $opcode . ' ' .
@@ -365,7 +364,7 @@ function sqimap_run_search($imapConnection, $search_string, $search_charset)
     }
     $messagelist = parseUidList($readin,'SEARCH');
 
-    if (empty($messagelist))   //Empty search response, ie '* SEARCH'
+    if (empty($messagelist))    //Empty search response, ie '* SEARCH'
         return array();
 
     $cnt = count($messagelist);
@@ -407,8 +406,8 @@ function sqimap_asearch_get_sort_criteria($mailbox, $sort_by)
     $sort_opcodes = array ('DATE', 'FROM', 'SUBJECT', 'SIZE');
     if ($internal_date_sort == true)
         $sort_opcodes[0] = 'ARRIVAL';
-//     if (handleAsSent($mailbox))
-//     if (isSentFolder($mailbox))
+//        if (handleAsSent($mailbox))
+//        if (isSentFolder($mailbox))
     if ($mailbox == $sent_folder)
         $sort_opcodes[1] = 'TO';
     return (($sort_by % 2) ? '' : 'REVERSE ') . $sort_opcodes[($sort_by >> 1) & 3];
@@ -447,19 +446,18 @@ function sqimap_asearch($imapConnection, &$mailbox_array, &$biop_array, &$unop_a
 {
 
     $search_charset = sqimap_asearch_get_charset();
-    $mbox_msgs = array();
     $mbox_search = array();
     $search_string = '';
     $cur_mailbox = $mailbox_array[0];
-    $cur_biop = '';    /* Start with ALL */
+    $cur_biop = '';    /* Start with ALL */
     /* We loop one more time than the real array count, so the last search gets fired */
     for ($cur_crit=0,$iCnt=count($where_array); $cur_crit <= $iCnt; ++$cur_crit) {
         if (empty($exclude_array[$cur_crit])) {
             $next_mailbox = (isset($mailbox_array[$cur_crit])) ? $mailbox_array[$cur_crit] : false;
             if ($next_mailbox != $cur_mailbox) {
-                $search_string = trim($search_string); /* Trim out last space */
+                $search_string = trim($search_string);    /* Trim out last space */
                 if ($cur_mailbox == 'All Folders')
-                        $search_mboxes = $mboxes_array;
+                    $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
@@ -474,7 +472,6 @@ function sqimap_asearch($imapConnection, &$mailbox_array, &$biop_array, &$unop_a
                 }
                 $cur_mailbox = $next_mailbox;
                 $search_string = '';
-
             }
             if (isset($where_array[$cur_crit]) && empty($exclude_array[$cur_crit])) {
                 for ($crit = $cur_crit; $crit < count($where_array); $crit++) {
@@ -499,7 +496,7 @@ function sqimap_asearch($imapConnection, &$mailbox_array, &$biop_array, &$unop_a
                         $aSearch[] = 'OR '.$aCriteria[$i][1];
                     } else if ($cur_biop != 'OR') {
                         $aSearch[] = 'ALL '.$aCriteria[$i][1];
-                    } else { // or only supports 2 search keys so we need to create a parenthized list
+                    } else { // OR only supports 2 search keys so we need to create a parenthesized list
                         $prev_biop = (isset($aCriteria[$i-1][0])) ? $aCriteria[$i-1][0] : false;
                         if ($prev_biop == $cur_biop) {
                             $last = $aSearch[$i-1];
@@ -509,8 +506,8 @@ function sqimap_asearch($imapConnection, &$mailbox_array, &$biop_array, &$unop_a
                             } else {
                                 $sEnd = '';
                                 while ($last && substr($last,-1) == ')') {
-                                $last = substr($last,0,-1);
-                                $sEnd .= ')';
+                                    $last = substr($last,0,-1);
+                                    $sEnd .= ')';
                                 }
                                 $aSearch[$i-1] = "(OR $last";
                                 $aSearch[] = $aCriteria[$i][1].$sEnd.')';
@@ -519,14 +516,12 @@ function sqimap_asearch($imapConnection, &$mailbox_array, &$biop_array, &$unop_a
                             $aSearch[] = $aCriteria[$i][1];
                         }
                     }
-
                 }
                 $search_string .= implode(' ',$aSearch);
             }
-
         }
     }
     return ($mbox_search);
 }
 
-?>
+?>
\ No newline at end of file