Changeset cff8861 in github


Ignore:
Timestamp:
Mar 24, 2010 7:27:44 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:
02e2b43
Parents:
b488c1d
Message:
  • small backend cleanup + support for operations on ALL messages in a folder
Location:
program
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • program/include/rcube_imap.php

    r18ace62 rcff8861  
    21052105   * Set message flag to one or several messages 
    21062106   * 
    2107    * @param mixed  Message UIDs as array or as comma-separated string 
    2108    * @param string Flag to set: SEEN, UNDELETED, DELETED, RECENT, ANSWERED, DRAFT, MDNSENT 
    2109    * @param string Folder name 
     2107   * @param mixed   Message UIDs as array or comma-separated string, or '*' 
     2108   * @param string  Flag to set: SEEN, UNDELETED, DELETED, RECENT, ANSWERED, DRAFT, MDNSENT 
     2109   * @param string  Folder name 
    21102110   * @param boolean True to skip message cache clean up 
    2111    * @return boolean True on success, False on failure 
     2111   * @return int    Number of flagged messages, -1 on failure 
    21122112   */ 
    21132113  function set_flag($uids, $flag, $mbox_name=NULL, $skip_cache=false) 
     
    21162116 
    21172117    $flag = strtoupper($flag); 
    2118     if (!is_array($uids)) 
    2119       $uids = explode(',',$uids); 
    2120        
     2118    list($uids, $all_mode) = $this->_parse_uids($uids); 
     2119 
    21212120    if (strpos($flag, 'UN') === 0) 
    2122       $result = iil_C_UnFlag($this->conn, $mailbox, join(',', $uids), substr($flag, 2)); 
     2121      $result = iil_C_UnFlag($this->conn, $mailbox, $uids, substr($flag, 2)); 
    21232122    else 
    2124       $result = iil_C_Flag($this->conn, $mailbox, join(',', $uids), $flag); 
    2125  
    2126     // reload message headers if cached 
    2127     if ($this->caching_enabled && !$skip_cache) { 
    2128       $cache_key = $mailbox.'.msg'; 
    2129       $this->remove_message_cache($cache_key, $uids); 
    2130       } 
    2131  
    2132     // set nr of messages that were flaged 
    2133     $count = count($uids); 
    2134  
    2135     // clear message count cache 
    2136     if ($result && $flag=='SEEN') 
    2137       $this->_set_messagecount($mailbox, 'UNSEEN', $count*(-1)); 
    2138     else if ($result && $flag=='UNSEEN') 
    2139       $this->_set_messagecount($mailbox, 'UNSEEN', $count); 
    2140     else if ($result && $flag=='DELETED') 
    2141       $this->_set_messagecount($mailbox, 'ALL', $count*(-1)); 
     2123      $result = iil_C_Flag($this->conn, $mailbox, $uids, $flag); 
     2124 
     2125    if ($result >= 0) { 
     2126      // reload message headers if cached 
     2127      if ($this->caching_enabled && !$skip_cache) { 
     2128        $cache_key = $mailbox.'.msg'; 
     2129        if ($all_mode) 
     2130          $this->clear_message_cache($cache_key); 
     2131        else 
     2132          $this->remove_message_cache($cache_key, explode(',', $uids)); 
     2133        } 
     2134      // update counters 
     2135      if ($flag=='SEEN') 
     2136        $this->_set_messagecount($mailbox, 'UNSEEN', $result*(-1)); 
     2137      else if ($flag=='UNSEEN') 
     2138        $this->_set_messagecount($mailbox, 'UNSEEN', $result); 
     2139      else if ($flag=='DELETED') 
     2140        $this->_set_messagecount($mailbox, 'ALL', $result*(-1)); 
     2141      } 
    21422142 
    21432143    return $result; 
     
    21482148   * Remove message flag for one or several messages 
    21492149   * 
    2150    * @param mixed  Message UIDs as array or as comma-separated string 
     2150   * @param mixed  Message UIDs as array or comma-separated string, or '*' 
    21512151   * @param string Flag to unset: SEEN, DELETED, RECENT, ANSWERED, DRAFT, MDNSENT 
    21522152   * @param string Folder name 
    2153    * @return boolean True on success, False on failure 
     2153   * @return int   Number of flagged messages, -1 on failure 
    21542154   * @see set_flag 
    21552155   */ 
     
    21982198   * Move a message from one mailbox to another 
    21992199   * 
    2200    * @param string List of UIDs to move, separated by comma 
     2200   * @param mixed  Message UIDs as array or comma-separated string, or '*' 
    22012201   * @param string Target mailbox 
    22022202   * @param string Source mailbox 
     
    22102210    $from_mbox = $from_mbox ? $this->mod_mailbox($from_mbox) : $this->mailbox; 
    22112211 
    2212     // convert the list of uids to array 
    2213     $a_uids = is_string($uids) ? explode(',', $uids) : (is_array($uids) ? $uids : NULL); 
     2212    list($uids, $all_mode) = $this->_parse_uids($uids); 
    22142213 
    22152214    // exit if no message uids are specified 
    2216     if (!is_array($a_uids) || empty($a_uids)) 
     2215    if (empty($uids)) 
    22172216      return false; 
    22182217 
     
    22342233 
    22352234    // move messages 
    2236     $iil_move = iil_C_Move($this->conn, join(',', $a_uids), $from_mbox, $to_mbox); 
     2235    $iil_move = iil_C_Move($this->conn, $uids, $from_mbox, $to_mbox); 
    22372236    $moved = !($iil_move === false || $iil_move < 0); 
    22382237 
     
    22402239    // really deleted from the source mailbox 
    22412240    if ($moved) { 
    2242       $this->_expunge($from_mbox, false, $a_uids); 
     2241      $this->_expunge($from_mbox, false, $uids); 
    22432242      $this->_clear_messagecount($from_mbox); 
    22442243      $this->_clear_messagecount($to_mbox); 
     
    22462245    // moving failed 
    22472246    else if ($config->get('delete_always', false) && $tbox == $config->get('trash_mbox')) { 
    2248       $moved = $this->delete_message($a_uids, $fbox); 
     2247      $moved = $this->delete_message($uids, $fbox); 
    22492248    } 
    22502249 
     
    22562255      if ($this->search_set && $from_mbox == $this->mailbox) { 
    22572256        // threads are too complicated to just remove messages from set 
    2258         if ($this->search_threads) 
     2257        if ($this->search_threads || $all_mode) 
    22592258          $this->refresh_search(); 
    22602259        else { 
    2261           foreach ($a_uids as $uid) 
     2260          $uids = explode(',', $uids); 
     2261          foreach ($uids as $uid) 
    22622262            $a_mids[] = $this->_uid2id($uid, $from_mbox); 
    22632263          $this->search_set = array_diff($this->search_set, $a_mids); 
     
    22672267      // update cached message headers 
    22682268      $cache_key = $from_mbox.'.msg'; 
    2269       if ($start_index = $this->get_message_cache_index_min($cache_key, $a_uids)) { 
     2269      if ($all_mode || ($start_index = $this->get_message_cache_index_min($cache_key, $uids))) { 
    22702270        // clear cache from the lowest index on 
    2271         $this->clear_message_cache($cache_key, $start_index); 
     2271        $this->clear_message_cache($cache_key, $all_mode ? 1 : $start_index); 
    22722272       } 
    22732273    } 
     
    22802280   * Copy a message from one mailbox to another 
    22812281   * 
    2282    * @param string List of UIDs to copy, separated by comma 
     2282   * @param mixed  Message UIDs as array or comma-separated string, or '*' 
    22832283   * @param string Target mailbox 
    22842284   * @param string Source mailbox 
     
    22922292    $from_mbox = $from_mbox ? $this->mod_mailbox($from_mbox) : $this->mailbox; 
    22932293 
    2294     // convert the list of uids to array 
    2295     $a_uids = is_string($uids) ? explode(',', $uids) : (is_array($uids) ? $uids : NULL); 
     2294    list($uids, $all_mode) = $this->_parse_uids($uids); 
    22962295 
    22972296    // exit if no message uids are specified 
    2298     if (!is_array($a_uids) || empty($a_uids)) 
     2297    if (empty($uids)) 
    22992298      return false; 
    23002299 
     
    23092308 
    23102309    // copy messages 
    2311     $iil_copy = iil_C_Copy($this->conn, join(',', $a_uids), $from_mbox, $to_mbox); 
     2310    $iil_copy = iil_C_Copy($this->conn, $uids, $from_mbox, $to_mbox); 
    23122311    $copied = !($iil_copy === false || $iil_copy < 0); 
    23132312 
     
    23232322   * Mark messages as deleted and expunge mailbox 
    23242323   * 
    2325    * @param string List of UIDs to move, separated by comma 
     2324   * @param mixed  Message UIDs as array or comma-separated string, or '*' 
    23262325   * @param string Source mailbox 
    23272326   * @return boolean True on success, False on error 
     
    23312330    $mailbox = $mbox_name ? $this->mod_mailbox($mbox_name) : $this->mailbox; 
    23322331 
    2333     // convert the list of uids to array 
    2334     $a_uids = is_string($uids) ? explode(',', $uids) : (is_array($uids) ? $uids : NULL); 
    2335      
     2332    list($uids, $all_mode) = $this->_parse_uids($uids); 
     2333 
    23362334    // exit if no message uids are specified 
    2337     if (!is_array($a_uids) || empty($a_uids)) 
     2335    if (empty($uids)) 
    23382336      return false; 
    23392337 
    2340     $deleted = iil_C_Delete($this->conn, $mailbox, join(',', $a_uids)); 
     2338    $deleted = iil_C_Delete($this->conn, $mailbox, $uids); 
    23412339 
    23422340    if ($deleted) { 
    23432341      // send expunge command in order to have the deleted message 
    23442342      // really deleted from the mailbox 
    2345       $this->_expunge($mailbox, false, $a_uids); 
     2343      $this->_expunge($mailbox, false, $uids); 
    23462344      $this->_clear_messagecount($mailbox); 
    23472345      unset($this->uid_id_map[$mailbox]); 
     
    23532351      if ($this->search_set && $mailbox == $this->mailbox) { 
    23542352        // threads are too complicated to just remove messages from set 
    2355         if ($this->search_threads) 
     2353        if ($this->search_threads || $all_mode) 
    23562354          $this->refresh_search(); 
    23572355        else { 
    2358           foreach ($a_uids as $uid) 
     2356          $uids = explode(',', $uids); 
     2357          foreach ($uids as $uid) 
    23592358            $a_mids[] = $this->_uid2id($uid, $mailbox); 
    23602359          $this->search_set = array_diff($this->search_set, $a_mids); 
     
    23642363      // remove deleted messages from cache 
    23652364      $cache_key = $mailbox.'.msg'; 
    2366       if ($start_index = $this->get_message_cache_index_min($cache_key, $a_uids)) { 
     2365      if ($all_mode || ($start_index = $this->get_message_cache_index_min($cache_key, $uids))) { 
    23672366        // clear cache from the lowest index on 
    2368         $this->clear_message_cache($cache_key, $start_index); 
     2367        $this->clear_message_cache($cache_key, $all_mode ? 1 : $start_index); 
    23692368      } 
    23702369    } 
     
    24222421   * Send IMAP expunge command and clear cache 
    24232422   * 
    2424    * @see rcube_imap::expunge() 
    2425    * @param string      Mailbox name 
    2426    * @param boolean     False if cache should not be cleared 
    2427    * @param string      List of UIDs to remove, separated by comma 
     2423   * @param string       Mailbox name 
     2424   * @param boolean  False if cache should not be cleared 
     2425   * @param mixed    Message UIDs as array or comma-separated string, or '*' 
    24282426   * @return boolean True on success 
    24292427   * @access private 
     2428   * @see rcube_imap::expunge() 
    24302429   */ 
    24312430  private function _expunge($mailbox, $clear_cache=true, $uids=NULL) 
     
    24482447 
    24492448 
     2449  /** 
     2450   * Parse message UIDs input 
     2451   * 
     2452   * @param mixed  UIDs array or comma-separated list or '*' or '1:*' 
     2453   * @return array Two elements array with UIDs converted to list and ALL flag  
     2454   * @access private 
     2455   */ 
     2456  private function _parse_uids($uids) 
     2457    { 
     2458    if ($uids === '*' || $uids === '1:*') { 
     2459      $uids = '1:*'; 
     2460      $all = true; 
     2461      } 
     2462    else { 
     2463      if (is_array($uids)) 
     2464        $uids = join(',', $uids); 
     2465 
     2466      if (preg_match('/[^0-9,]/', $uids)) 
     2467        $uids = ''; 
     2468      } 
     2469 
     2470    return array($uids, (bool) $all); 
     2471    } 
     2472     
     2473 
    24502474  /* -------------------------------- 
    24512475   *        folder managment 
    24522476   * --------------------------------*/ 
    2453  
    24542477 
    24552478  /** 
     
    31493172      return; 
    31503173     
     3174    if (!empty($uids) && !is_array($uids)) { 
     3175      if ($uids == '*' || $uids == '1:*') 
     3176        $uids = NULL; 
     3177      else 
     3178        $uids = explode(',', $uids); 
     3179      } 
     3180 
    31513181    $sql_result = $this->db->query( 
    31523182      "SELECT MIN(idx) AS minidx 
  • program/lib/imap.inc

    r15e00bd rcff8861  
    390390} 
    391391 
    392 function iil_C_ClearCapability(&$conn) 
     392function iil_ClearCapability() 
    393393{ 
    394394        $conn->capability = array(); 
     
    661661                         
    662662                        // Now we're authenticated, capabilities need to be reread 
    663                         iil_C_ClearCapability($conn); 
     663                        iil_ClearCapability(); 
    664664                } 
    665665        } 
     
    14261426                iil_PutLine($conn->fp, "exp1 $command"); 
    14271427                do { 
    1428                         $line=chop(iil_ReadLine($conn->fp, 100)); 
     1428                        $line = iil_ReadLine($conn->fp, 100); 
    14291429                        if ($line[0] == '*') { 
    1430                                 $c++; 
    1431                         } 
     1430                $c++; 
     1431                } 
    14321432                } while (!iil_StartsWith($line, 'exp1', true)); 
    14331433                 
     
    14481448        } 
    14491449     
    1450         $fp    = $conn->fp; 
    14511450        $flags = $GLOBALS['IMAP_FLAGS']; 
    1452          
    1453         $flag = strtoupper($flag); 
    1454         $flag = $flags[$flag]; 
    1455      
    1456         if (iil_C_Select($conn, $mailbox)) { 
    1457                 $c = 0; 
    1458                 iil_PutLine($fp, "flg UID STORE $messages " . $mod . "FLAGS (" . $flag . ")"); 
    1459                 do { 
    1460                         $line=chop(iil_ReadLine($fp, 100)); 
    1461                         if ($line[0] == '*') { 
    1462                             $c++; 
    1463                         } 
    1464                 } while (!iil_StartsWith($line, 'flg', true)); 
    1465  
    1466                 if (iil_ParseResult($line) == 0) { 
    1467                         return $c; 
    1468                 } 
    1469                 $conn->error = $line; 
    1470                 return -1; 
    1471         } 
    1472         $conn->error = 'Select failed'; 
     1451        $flag = $flags[strtoupper($flag)]; 
     1452     
     1453        if (!iil_C_Select($conn, $mailbox)) { 
     1454            return -1; 
     1455        } 
     1456     
     1457    $c = 0; 
     1458        iil_PutLine($conn->fp, "flg UID STORE $messages " . $mod . "FLAGS (" . $flag . ")"); 
     1459        do { 
     1460                $line = iil_ReadLine($conn->fp, 1000); 
     1461                if ($line[0] == '*') { 
     1462                    $c++; 
     1463        } 
     1464        } while (!iil_StartsWith($line, 'flg', true)); 
     1465 
     1466        if (iil_ParseResult($line) == 0) { 
     1467                return $c; 
     1468        } 
     1469 
     1470        $conn->error = $line; 
    14731471        return -1; 
    14741472} 
     
    14871485 
    14881486function iil_C_Copy(&$conn, $messages, $from, $to) { 
    1489         $fp = $conn->fp; 
    14901487 
    14911488        if (empty($from) || empty($to)) { 
     
    14931490        } 
    14941491     
    1495         if (iil_C_Select($conn, $from)) { 
    1496                 $c=0; 
    1497                  
    1498                 iil_PutLine($fp, "cpy1 UID COPY $messages \"".iil_Escape($to)."\""); 
    1499                 $line = iil_ReadReply($fp); 
    1500                 return iil_ParseResult($line); 
    1501         } else { 
    1502                 return -1; 
    1503         } 
     1492        if (!iil_C_Select($conn, $from)) { 
     1493        return -1; 
     1494        } 
     1495         
     1496    iil_PutLine($conn->fp, "cpy1 UID COPY $messages \"".iil_Escape($to)."\""); 
     1497        $line = iil_ReadReply($conn->fp); 
     1498        return iil_ParseResult($line); 
    15041499} 
    15051500 
    15061501function iil_C_CountUnseen(&$conn, $folder) { 
    1507         $index = iil_C_Search($conn, $folder, 'ALL UNSEEN'); 
    1508         if (is_array($index)) 
    1509                 return count($index); 
    1510         return false; 
     1502    $index = iil_C_Search($conn, $folder, 'ALL UNSEEN'); 
     1503    if (is_array($index)) 
     1504        return count($index); 
     1505    return false; 
    15111506} 
    15121507 
Note: See TracChangeset for help on using the changeset viewer.