Changeset ffd3e29 in github for program/include/rcube_imap.php


Ignore:
Timestamp:
Jun 4, 2010 5:16:30 AM (3 years ago)
Author:
alecpl <alec@…>
Branches:
master, HEAD, courier-fix, dev-browser-capabilities, pdo, release-0.6, release-0.7, release-0.8
Children:
309f49f
Parents:
2144f9c
Message:
  • Some fixes for BADCHARSET handling in SORT/THREAD/SEARCH response
File:
1 edited

Legend:

Unmodified
Added
Removed
  • program/include/rcube_imap.php

    rf22b543 rffd3e29  
    298298        if (is_array($str) && $msgs == null) 
    299299            list($str, $msgs, $charset, $sort_field, $threads) = $str; 
    300         if ($msgs != null && !is_array($msgs)) 
     300        if ($msgs === false) 
     301            $msgs = array(); 
     302        else if ($msgs != null && !is_array($msgs)) 
    301303            $msgs = explode(',', $msgs); 
    302304 
     
    608610                $msg_index = array(); 
    609611 
    610             if ($slice) 
     612            if ($slice && $msg_index) 
    611613                $msg_index = array_slice($msg_index, ($this->sort_order == 'DESC' ? 0 : -$slice), $slice); 
    612614 
     
    11981200            } 
    11991201 
    1200             if ($this->sort_order == 'DESC') 
     1202            if ($a_index !== false && $this->sort_order == 'DESC') 
    12011203                $a_index = array_reverse($a_index); 
    12021204 
     
    12051207        // fetch complete message index 
    12061208        else if ($this->get_capability('SORT')) { 
    1207             if ($a_index = $this->conn->sort($mailbox, 
    1208                 $this->sort_field, $this->skip_deleted ? 'UNDELETED' : '')) { 
    1209                 if ($this->sort_order == 'DESC') 
    1210                     $a_index = array_reverse($a_index); 
    1211  
    1212                 $this->cache[$key] = $a_index; 
    1213                 } 
     1209            $a_index = $this->conn->sort($mailbox, 
     1210                $this->sort_field, $this->skip_deleted ? 'UNDELETED' : ''); 
     1211 
     1212            if ($a_index !== false && $this->sort_order == 'DESC') 
     1213                $a_index = array_reverse($a_index); 
     1214 
     1215            $this->cache[$key] = $a_index; 
    12141216        } 
    12151217        else if ($a_index = $this->conn->fetchHeaderIndex( 
     
    12231225        } 
    12241226 
    1225         return $this->cache[$key]; 
     1227        return $this->cache[$key] !== false ? $this->cache[$key] : array(); 
    12261228    } 
    12271229 
     
    13801382        $results = $this->_search_index($mailbox, $str, $charset, $sort_field); 
    13811383 
    1382         // try search with US-ASCII charset (should be supported by server) 
    1383         // only if UTF-8 search is not supported 
    1384         if (empty($results) && !is_array($results) && !empty($charset) && $charset != 'US-ASCII') 
    1385         { 
    1386             // convert strings to US_ASCII 
    1387             if(preg_match_all('/\{([0-9]+)\}\r\n/', $str, $matches, PREG_OFFSET_CAPTURE)) { 
    1388                 $last = 0; $res = ''; 
    1389                 foreach($matches[1] as $m) 
    1390                 { 
    1391                     $string_offset = $m[1] + strlen($m[0]) + 4; // {}\r\n 
    1392                     $string = substr($str, $string_offset - 1, $m[0]); 
    1393                     $string = rcube_charset_convert($string, $charset, 'US-ASCII'); 
    1394                     if (!$string) 
    1395                         continue; 
    1396                     $res .= sprintf("%s{%d}\r\n%s", substr($str, $last, $m[1] - $last - 1), strlen($string), $string); 
    1397                     $last = $m[0] + $string_offset - 1; 
    1398                 } 
    1399                 if ($last < strlen($str)) 
    1400                     $res .= substr($str, $last, strlen($str)-$last); 
    1401             } 
    1402             else // strings for conversion not found 
    1403                 $res = $str; 
    1404  
    1405             $results = $this->search($mbox_name, $res, NULL, $sort_field); 
    1406         } 
    1407  
    14081384        $this->set_search_set($str, $results, $charset, $sort_field, (bool)$this->threading); 
    14091385 
     
    14271403 
    14281404        if ($this->threading) { 
    1429             list ($thread_tree, $msg_depth, $has_children) = $this->conn->thread( 
    1430                 $mailbox, $this->threading, $criteria, $charset); 
    1431  
    1432             $a_messages = array( 
    1433                 'tree'  => $thread_tree, 
    1434                     'depth'     => $msg_depth, 
    1435                     'children' => $has_children 
    1436             ); 
     1405            $a_messages = $this->conn->thread($mailbox, $this->threading, $criteria, $charset); 
     1406 
     1407            // Error, try with US-ASCII (RFC5256: SORT/THREAD must support US-ASCII and UTF-8, 
     1408            // but I've seen that Courier doesn't support UTF-8) 
     1409            if ($a_messages === false && $charset && $charset != 'US-ASCII') 
     1410                $a_messages = $this->conn->thread($mailbox, $this->threading, 
     1411                    $this->convert_criteria($criteria, $charset), 'US-ASCII'); 
     1412 
     1413            if ($a_messages !== false) { 
     1414                list ($thread_tree, $msg_depth, $has_children) = $a_messages; 
     1415                $a_messages = array( 
     1416                    'tree'      => $thread_tree, 
     1417                        'depth' => $msg_depth, 
     1418                        'children' => $has_children 
     1419                ); 
     1420            } 
    14371421        } 
    14381422        else if ($sort_field && $this->get_capability('SORT')) { 
     
    14401424            $a_messages = $this->conn->sort($mailbox, $sort_field, $criteria, false, $charset); 
    14411425 
    1442             if (!$a_messages) 
    1443                     return array(); 
     1426            // Error, try with US-ASCII (RFC5256: SORT/THREAD must support US-ASCII and UTF-8, 
     1427            // but I've seen that Courier doesn't support UTF-8) 
     1428            if ($a_messages === false && $charset && $charset != 'US-ASCII') 
     1429                $a_messages = $this->conn->sort($mailbox, $sort_field, 
     1430                    $this->convert_criteria($criteria, $charset), false, 'US-ASCII'); 
    14441431        } 
    14451432        else { 
     
    14501437            else { 
    14511438                $a_messages = $this->conn->search($mailbox, 
    1452                         ($charset ? "CHARSET $charset " : '') . $criteria); 
    1453  
    1454                 if (!$a_messages) 
    1455                     return array(); 
    1456  
    1457             // I didn't found that SEARCH always returns sorted IDs 
    1458             if (!$this->sort_field) 
    1459                 sort($a_messages); 
     1439                    ($charset ? "CHARSET $charset " : '') . $criteria); 
     1440 
     1441                // Error, try with US-ASCII (some servers may support only US-ASCII) 
     1442                if ($a_messages === false && $charset && $charset != 'US-ASCII') 
     1443                    $a_messages = $this->conn->search($mailbox, 
     1444                        'CHARSET US-ASCII ' . $this->convert_criteria($criteria, $charset)); 
     1445 
     1446                // I didn't found that SEARCH should return sorted IDs 
     1447                    if (is_array($a_messages) && !$this->sort_field) 
     1448                    sort($a_messages); 
    14601449            } 
    14611450        } 
     
    14881477 
    14891478        return $this->conn->search($mailbox, $str, $ret_uid); 
     1479    } 
     1480 
     1481 
     1482    /** 
     1483     * Converts charset of search criteria string 
     1484     * 
     1485     * @param  string  Search string 
     1486     * @param  string  Original charset 
     1487     * @param  string  Destination charset (default US-ASCII) 
     1488     * @return string  Search string 
     1489     * @access private 
     1490     */ 
     1491    private function convert_criteria($str, $charset, $dest_charset='US-ASCII') 
     1492    { 
     1493        // convert strings to US_ASCII 
     1494        if (preg_match_all('/\{([0-9]+)\}\r\n/', $str, $matches, PREG_OFFSET_CAPTURE)) { 
     1495            $last = 0; $res = ''; 
     1496            foreach ($matches[1] as $m) { 
     1497                $string_offset = $m[1] + strlen($m[0]) + 4; // {}\r\n 
     1498                $string = substr($str, $string_offset - 1, $m[0]); 
     1499                $string = rcube_charset_convert($string, $charset, $dest_charset); 
     1500                if (!$string) 
     1501                    continue; 
     1502                $res .= sprintf("%s{%d}\r\n%s", substr($str, $last, $m[1] - $last - 1), strlen($string), $string); 
     1503                $last = $m[0] + $string_offset - 1; 
     1504            } 
     1505            if ($last < strlen($str)) 
     1506                $res .= substr($str, $last, strlen($str)-$last); 
     1507        } 
     1508        else // strings for conversion not found 
     1509            $res = $str; 
     1510 
     1511        return $res; 
    14901512    } 
    14911513 
Note: See TracChangeset for help on using the changeset viewer.