Fix two typos "squirrelmai" -> "squirrelmail". Sheesh.
[squirrelmail.git] / functions / imap_asearch.php
index 0cb03f025c7541719e8efd75a458a1a712e0fd30..f3dc5676533690801f0fde3ee4e1309ff2109bae 100644 (file)
@@ -7,15 +7,17 @@
  * Licensed under the GNU GPL. For full terms see the file COPYING.
  *
  * IMAP asearch routines
- * Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
+ * @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
  * See README file for infos.
+ * @package squirrelmail
  *
  */
 
+/** This functionality requires the IMAP and date functions */
 require_once(SM_PATH . 'functions/imap_general.php');
 require_once(SM_PATH . 'functions/date.php');
 
-/* Set to TRUE to dump the imap dialogue */
+/** Set to TRUE to dump the imap dialogue */
 $imap_asearch_debug_dump = FALSE;
 
 $imap_asearch_opcodes = array(
@@ -88,7 +90,22 @@ function sqimap_asearch_error_box($response, $query, $message)
                $title = _("ERROR : Unknown imap response.");
        else
                $title = $imap_error_titles[$response];
-       sqimap_error_box($title, $query, _("Reason Given: "), $message);
+       $message_title = _("Reason Given: ");
+       if (function_exists('sqimap_error_box'))
+               sqimap_error_box($title, $query, $message_title, $message);
+       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";
+    if ($query != '')
+        $string .= _("Query:") . ' ' . htmlspecialchars($query) . '<br>';
+    if ($message_title != '')
+        $string .= $message_title;
+    if ($message != '')
+        $string .= htmlspecialchars($message);
+    $string .= "</font><br>\n";
+    error_box($string,$color);
+       }
 }
 
 /* This is to avoid the E_NOTICE warnings signaled by marc AT squirrelmail.org. Thanks Marc! */
@@ -99,7 +116,7 @@ function asearch_nz(&$var)
        return '';
 }
 
-/* This should give the same results as PHP 4 >= 4.3.0's html_entity_decode() */
+/* This should give the same results as PHP 4 >= 4.3.0's html_entity_decode(), except it doesn't handle hex constructs */
 function asearch_unhtmlentities($string) {
        $trans_tbl = array_flip(get_html_translation_table(HTML_ENTITIES));
        for ($i=127; $i<255; $i++)      /* Add &#<dec>; entities */
@@ -114,8 +131,16 @@ function asearch_unhtmlentities($string) {
 function s_debug_dump($var_name, $var_var)
 {
        global $imap_asearch_debug_dump;
-       if ($imap_asearch_debug_dump)
-               sm_print_r($var_name, $var_var);
+       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 ;)
+               else {
+                       echo '<pre>';
+                       echo htmlentities($var_name);
+                       print_r($var_var);
+                       echo '</pre>';
+               }
+       }
 }
 
 /*
@@ -125,14 +150,14 @@ function s_debug_dump($var_name, $var_var)
 9. Formal Syntax:
        quoted-specials = DQUOTE / "\"
 */
-function sqimap_asearch_encode_string($what, $search_charset)
+function sqimap_asearch_encode_string($what, $charset)
 {
-       if (strtoupper($search_charset) == 'ISO-2022-JP')
+       if (strtoupper($charset) == 'ISO-2022-JP')      // This should be now handled in imap_utf7_local?
                $what = mb_convert_encoding($what, 'JIS', 'auto');
 //if (ereg("[\"\\\r\n\x80-\xff]", $what))
        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
 }
 
 /*
@@ -170,7 +195,7 @@ function sqimap_asearch_parse_date($what)
        return $what_parts;
 }
 
-function sqimap_asearch_build_criteria($opcode, $what, $search_charset)
+function sqimap_asearch_build_criteria($opcode, $what, $charset)
 {
        global $imap_asearch_opcodes;
 
@@ -190,8 +215,8 @@ function sqimap_asearch_build_criteria($opcode, $what, $search_charset)
                        preg_match('/^([^:]+):(.*)$/', $what, $what_parts);
                        if (count($what_parts) == 3)
                                $criteria = $opcode . ' ' . 
-                                       sqimap_asearch_encode_string($what_parts[1], $search_charset) . ' ' .
-                                       sqimap_asearch_encode_string($what_parts[2], $search_charset) . ' ';
+                                       sqimap_asearch_encode_string($what_parts[1], $charset) . ' ' .
+                                       sqimap_asearch_encode_string($what_parts[2], $charset) . ' ';
                break;
                case 'adate':
                        $what_parts = sqimap_asearch_parse_date($what);
@@ -200,7 +225,7 @@ function sqimap_asearch_build_criteria($opcode, $what, $search_charset)
                break;
                case 'akeyword':
                case 'astring':
-                       $criteria = $opcode . ' ' . sqimap_asearch_encode_string($what, $search_charset) . ' ';
+                       $criteria = $opcode . ' ' . sqimap_asearch_encode_string($what, $charset) . ' ';
                break;
                case 'asequence':
                        $what = ereg_replace('[^0-9:\(\)]+', '', $what);
@@ -213,23 +238,21 @@ function sqimap_asearch_build_criteria($opcode, $what, $search_charset)
 
 function sqimap_run_search($imapConnection, $search_string, $search_charset)
 {
-       global $allow_charset_search, $uid_support;
+       global $uid_support;
 
        /* 6.4.4 try OPTIONAL [CHARSET] specification first */
-       if ($allow_charset_search && (!empty($search_charset)))
-               $query = 'SEARCH CHARSET ' . strtoupper($search_charset) . ' ALL ' . $search_string;
+       if ($search_charset != '')
+               $query = 'SEARCH CHARSET "' . strtoupper($search_charset) . '" ALL ' . $search_string;
        else
                $query = 'SEARCH ALL ' . $search_string;
        s_debug_dump('C:', $query);
-
-       /* read data back from IMAP */
        $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
 
-       /* 6.4.4 try US-ASCII charset if we receive a tagged NO response */
-       if ((!empty($charset))  && (strtoupper($response) == 'NO')) {
-               $query = 'SEARCH CHARSET "US-ASCII" ALL ' . $search_string;
+       /* 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);       /* added $uid_support */
+               $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
        }
        if (strtoupper($response) != 'OK') {
                sqimap_asearch_error_box($response, $query, $message);
@@ -270,6 +293,15 @@ function sqimap_run_search($imapConnection, $search_string, $search_charset)
        return $id;
 }
 
+function sqimap_asearch_get_charset()
+{
+       global $allow_charset_search, $languages, $squirrelmail_language;
+
+       if ($allow_charset_search)
+               return $languages[$squirrelmail_language]['CHARSET'];
+       return '';
+}
+
 /* replaces $mbox_msgs[$search_mailbox] = array_values(array_unique(array_merge($mbox_msgs[$search_mailbox], sqimap_run_search($imapConnection, $search_string, $search_charset))));*/
 function sqimap_array_merge_unique($to, $from)
 {
@@ -284,13 +316,8 @@ function sqimap_array_merge_unique($to, $from)
 
 function sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $mboxes_array)
 {
-       global $languages, $squirrelmail_language;
-
-/* ??? what are those for ?? */
-/*     $pos = $search_position;*/
-
+       $search_charset = sqimap_asearch_get_charset();
        $mbox_msgs = array();
-       $search_charset = $languages[$squirrelmail_language]['CHARSET'];
        $search_string = '';
        $cur_mailbox = $mailbox_array[0];
        $cur_biop = ''; /* Start with ALL */
@@ -305,8 +332,8 @@ function sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_arra
                                else
                                        $search_mboxes = array($cur_mailbox);
                                foreach ($search_mboxes as $cur_mailbox) {
-                                       sqimap_mailbox_select($imapConnection, $cur_mailbox);
                                        s_debug_dump('C:SELECT:', $cur_mailbox);
+                                       sqimap_mailbox_select($imapConnection, $cur_mailbox);
                                        if (isset($mbox_msgs[$cur_mailbox])) {
                                                if ($cur_biop == 'OR')  /* Merge with previous results */
                                                        $mbox_msgs[$cur_mailbox] = sqimap_array_merge_unique($mbox_msgs[$cur_mailbox], sqimap_run_search($imapConnection, $search_string, $search_charset));
@@ -347,4 +374,4 @@ function sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_arra
        return $mbox_msgs;
 }
 
-?>
\ No newline at end of file
+?>