From ded2b7e166d4b0acab09c00f22f379fbabba709a Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Fri, 05 May 2006 12:53:21 -0400
Subject: [PATCH] Changed login page title regarding product name (Bug #1476413)

---
 program/include/rcube_imap.inc |  333 +++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 270 insertions(+), 63 deletions(-)

diff --git a/program/include/rcube_imap.inc b/program/include/rcube_imap.inc
index 7b71dc0..30e6eb8 100644
--- a/program/include/rcube_imap.inc
+++ b/program/include/rcube_imap.inc
@@ -55,7 +55,7 @@
   var $default_folders = array('inbox', 'drafts', 'sent', 'junk', 'trash');
   var $cache = array();
   var $cache_keys = array();  
-  var $cache_changes = array();  
+  var $cache_changes = array();
   var $uid_id_map = array();
   var $msg_headers = array();
   var $capabilities = array();
@@ -70,7 +70,7 @@
    */
   function __construct($db_conn)
     {
-    $this->db = $db_conn;    
+    $this->db = $db_conn;
     }
 
 
@@ -98,7 +98,7 @@
    */
   function connect($host, $user, $pass, $port=143, $use_ssl=FALSE)
     {
-    global $ICL_SSL, $ICL_PORT;
+    global $ICL_SSL, $ICL_PORT, $IMAP_USE_INTERNAL_DATE;
     
     // check for Open-SSL support in PHP build
     if ($use_ssl && in_array('openssl', get_loaded_extensions()))
@@ -111,6 +111,8 @@
       }
 
     $ICL_PORT = $port;
+    $IMAP_USE_INTERNAL_DATE = false;
+    
     $this->conn = iil_Connect($host, $user, $pass, array('imap' => 'check'));
     $this->host = $host;
     $this->user = $user;
@@ -376,14 +378,28 @@
     }
 
 
-  // get message count for a specific mailbox; acceptes modes are: ALL, UNSEEN, RECENT
+  /**
+   * Get message count for a specific mailbox
+   *
+   * @param   string   Mailbox/folder name
+   * @param   string   Mode for count [ALL|UNSEEN|RECENT]
+   * @param   boolean  Force reading from server and update cache
+   * @return  number   Number of messages
+   * @access  public   
+   */
   function messagecount($mbox='', $mode='ALL', $force=FALSE)
     {
     $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox;
     return $this->_messagecount($mailbox, $mode, $force);
     }
 
-  // private method for getting nr of mesages
+
+  /**
+   * Private method for getting nr of messages
+   *
+   * @access  private
+   * @see     rcube_imap::messagecount
+   */
   function _messagecount($mailbox='', $mode='ALL', $force=FALSE)
     {
     $a_mailbox_cache = FALSE;
@@ -430,7 +446,7 @@
         $count = iil_C_CountMessages($this->conn, $mailbox);
       }
 
-    if (is_array($a_mailbox_cache[$mailbox]))
+    if (!is_array($a_mailbox_cache[$mailbox]))
       $a_mailbox_cache[$mailbox] = array();
       
     $a_mailbox_cache[$mailbox][$mode] = (int)$count;
@@ -442,8 +458,17 @@
     }
 
 
-  // public method for listing headers
-  // convert mailbox name with root dir first
+  /**
+   * Public method for listing headers
+   * convert mailbox name with root dir first
+   *
+   * @param   string   Mailbox/folder name
+   * @param   number   Current page to list
+   * @param   string   Header field to sort by
+   * @param   string   Sort order [ASC|DESC]
+   * @return  array    Indexed array with message header objects
+   * @access  public   
+   */
   function list_headers($mbox='', $page=NULL, $sort_field=NULL, $sort_order=NULL)
     {
     $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox;
@@ -451,7 +476,12 @@
     }
 
 
-  // private method for listing message header
+  /**
+   * Private method for listing message headers
+   *
+   * @access  private
+   * @see     rcube_imap::list_headers
+   */
   function _list_headers($mailbox='', $page=NULL, $sort_field=NULL, $sort_order=NULL, $recursive=FALSE)
     {
     if (!strlen($mailbox))
@@ -465,12 +495,15 @@
     $max = $this->_messagecount($mailbox);
     $start_msg = ($this->list_page-1) * $this->page_size;
     
+    list($begin, $end) = $this->_get_message_range($max, $page);
+    
+    /*    
     if ($page=='all')
       {
       $begin = 0;
       $end = $max;
       }
-    else if ($this->sort_order=='DESC')
+    else if (!$this->get_capability('sort') && $this->sort_order=='DESC')
       {
       $begin = $max - $this->page_size - $start_msg;
       $end =   $max - $start_msg;
@@ -484,7 +517,8 @@
     if ($begin < 0) $begin = 0;
     if ($end < 0) $end = $max;
     if ($end > $max) $end = $max;
-
+	*/
+	
 //console("fetch headers $start_msg to ".($start_msg+$this->page_size)." (msg $begin to $end)");
 
     $headers_sorted = FALSE;
@@ -508,12 +542,7 @@
         
         $msgs = $msg_index[$begin];
         for ($i=$begin+1; $i < $end; $i++)
-          {
-          //if ($this->sort_order == 'DESC')
-          //  $msgs = $msg_index[$i].','.$msgs;
-          //else
-            $msgs = $msgs.','.$msg_index[$i];
-          }
+          $msgs = $msgs.','.$msg_index[$i];
 
         $sorted = TRUE;
         }
@@ -533,7 +562,6 @@
 
         
       // fetch reuested headers from server
-      $a_header_index = iil_C_FetchHeaders($this->conn, $mailbox, $msgs);
       $a_msg_headers = array();
       $deleted_count = $this->_fetch_headers($mailbox, $msgs, $a_msg_headers, $cache_key);
 
@@ -553,19 +581,119 @@
 
 
     // if not already sorted
-    if (!$headers_sorted)
-      $a_msg_headers = iil_SortHeaders($a_msg_headers, $this->sort_field, $this->sort_order);
+//    if (!$headers_sorted)
+//      $a_msg_headers = iil_SortHeaders($a_msg_headers, $this->sort_field, $this->sort_order);
+
+
+      if (!$headers_sorted && $this->sort_order == 'DESC')
+        $a_msg_headers = array_reverse($a_msg_headers);
+      
 
     return array_values($a_msg_headers);
     }
+    
 
+  /**
+   * Public method for listing a specific set of headers
+   * convert mailbox name with root dir first
+   *
+   * @param   string   Mailbox/folder name
+   * @param   array    List of message ids to list
+   * @param   number   Current page to list
+   * @param   string   Header field to sort by
+   * @param   string   Sort order [ASC|DESC]
+   * @return  array    Indexed array with message header objects
+   * @access  public   
+   */
+  function list_header_set($mbox='', $msgs, $page=NULL, $sort_field=NULL, $sort_order=NULL)
+    {
+    $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox;
+    return $this->_list_header_set($mailbox, $msgs, $page, $sort_field, $sort_order);    
+    }
+    
+
+  /**
+   * Private method for listing a set of message headers
+   *
+   * @access  private
+   * @see     rcube_imap::list_header_set
+   */
+  function _list_header_set($mailbox, $msgs, $page=NULL, $sort_field=NULL, $sort_order=NULL)
+    {
+    // also accept a comma-separated list of message ids
+    if (is_string($msgs))
+      $msgs = split(',', $msgs);
+      
+    if (!strlen($mailbox) || empty($msgs))
+      return array();
+
+    if ($sort_field!=NULL)
+      $this->sort_field = $sort_field;
+    if ($sort_order!=NULL)
+      $this->sort_order = strtoupper($sort_order);
+
+    $max = count($msgs);
+    $start_msg = ($this->list_page-1) * $this->page_size;
+
+    // fetch reuested headers from server
+    $a_msg_headers = array();
+    $this->_fetch_headers($mailbox, join(',', $msgs), $a_msg_headers, NULL);
+
+    // return empty array if no messages found
+	if (!is_array($a_msg_headers) || empty($a_msg_headers))
+		return array();
+
+    // if not already sorted
+    $a_msg_headers = iil_SortHeaders($a_msg_headers, $this->sort_field, $this->sort_order);
+
+	// only return the requested part of the set
+	return array_slice(array_values($a_msg_headers), $start_msg, min($max-$start_msg, $this->page_size));
+    }
+
+
+  /**
+   * Helper function to get first and last index of the requested set
+   *
+   * @param  number  message count
+   * @param  mixed   page number to show, or string 'all'
+   * @return array   array with two values: first index, last index
+   * @access private
+   */
+  function _get_message_range($max, $page)
+    {
+    $start_msg = ($this->list_page-1) * $this->page_size;
+    
+    if ($page=='all')
+      {
+      $begin = 0;
+      $end = $max;
+      }
+    else if ($this->sort_order=='DESC')
+      {
+      $begin = $max - $this->page_size - $start_msg;
+      $end =   $max - $start_msg;
+      }
+    else
+      {
+      $begin = $start_msg;
+      $end   = $start_msg + $this->page_size;
+      }
+
+    if ($begin < 0) $begin = 0;
+    if ($end < 0) $end = $max;
+    if ($end > $max) $end = $max;
+    
+    return array($begin, $end);
+    }
+    
+    
 
   /**
    * Fetches message headers
    * Used for loop
    *
    * @param  string  Mailbox name
-   * @param  string  Message indey to fetch
+   * @param  string  Message index to fetch
    * @param  array   Reference to message headers array
    * @param  array   Array with cache index
    * @return number  Number of deleted messages
@@ -575,7 +703,7 @@
     {
     // cache is incomplete
     $cache_index = $this->get_message_cache_index($cache_key);
-
+    
     // fetch reuested headers from server
     $a_header_index = iil_C_FetchHeaders($this->conn, $mailbox, $msgs);
     $deleted_count = 0;
@@ -605,8 +733,7 @@
     return $deleted_count;
     }
     
-
-
+  
   // return sorted array of message UIDs
   function message_index($mbox='', $sort_field=NULL, $sort_order=NULL)
     {
@@ -629,24 +756,20 @@
     // cache is OK
     if ($cache_status>0)
       {
-      $a_index = $this->get_message_cache_index($cache_key, FALSE, $this->sort_field);
+      $a_index = $this->get_message_cache_index($cache_key, TRUE, $this->sort_field, $this->sort_order);
       return array_values($a_index);
       }
 
 
     // fetch complete message index
     $msg_count = $this->_messagecount($mailbox);
-    if ($this->get_capability('sort') && ($a_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field)))
+    if ($this->get_capability('sort') && ($a_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field, '', TRUE)))
       {
-      $a_uids = iil_C_FetchUIDs($this->conn, $mailbox);
-
       if ($this->sort_order == 'DESC')
         $a_index = array_reverse($a_index);
 
-      $i = 0;
-      $this->cache[$key] = array();
-      foreach ($a_index as $index => $value)
-        $this->cache[$key][$i++] = $a_uids[$value];
+      $this->cache[$key] = $a_index;
+
       }
     else
       {
@@ -719,16 +842,44 @@
     }
 
 
-  function search($mbox='', $criteria='ALL')
+  /**
+   * Invoke search request to IMAP server
+   *
+   * @param  string  mailbox name to search in
+   * @param  string  search criteria (ALL, TO, FROM, SUBJECT, etc)
+   * @param  string  search string
+   * @return array   search results as list of message ids
+   * @access public
+   */
+  function search($mbox='', $criteria='ALL', $str=NULL)
     {
     $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox;
-    return $this->_search_index($mailbox, $criteria);
-    }
-    
-    
+    if ($str && $criteria)
+      {
+      $criteria .= ' CHARSET UTF-8 "'.UTF7EncodeString($str).'"';
+      return $this->_search_index($mailbox, $criteria);
+      }
+    else
+      return $this->_search_index($mailbox, $criteria);
+    }    
+
+
+  /**
+   * Private search method
+   *
+   * @return array   search results as list of message ids
+   * @access private
+   * @see rcube_imap::search()
+   */
   function _search_index($mailbox, $criteria='ALL')
     {
     $a_messages = iil_C_Search($this->conn, $mailbox, $criteria);
+    
+    // clean message list (there might be some empty entries)
+    foreach ($a_messages as $i => $val)
+      if (empty($val))
+        unset($a_messages[$i]);
+        
     return $a_messages;
     }
 
@@ -738,7 +889,7 @@
     $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox;
 
     // get cached headers
-    if ($headers = $this->get_cached_message($mailbox.'.msg', $uid))
+    if ($is_uid && ($headers = $this->get_cached_message($mailbox.'.msg', $id)))
       return $headers;
 
     $msg_id = $is_uid ? $this->_uid2id($id) : $id;
@@ -783,18 +934,21 @@
 
 
   // set message flag to one or several messages
-  // possible flgs are: SEEN, DELETED, RECENT, ANSWERED, DRAFT
+  // possible flags are: SEEN, UNDELETED, DELETED, RECENT, ANSWERED, DRAFT
   function set_flag($uids, $flag)
     {
     $flag = strtoupper($flag);
     $msg_ids = array();
     if (!is_array($uids))
-      $uids = array($uids);
+      $uids = explode(',',$uids);
       
-    foreach ($uids as $uid)
+    foreach ($uids as $uid) {
       $msg_ids[$uid] = $this->_uid2id($uid);
+    }
       
-    if ($flag=='UNSEEN')
+    if ($flag=='UNDELETED')
+      $result = iil_C_Undelete($this->conn, $this->mailbox, join(',', array_values($msg_ids)));
+    else if ($flag=='UNSEEN')
       $result = iil_C_Unseen($this->conn, $this->mailbox, join(',', array_values($msg_ids)));
     else
       $result = iil_C_Flag($this->conn, $this->mailbox, join(',', array_values($msg_ids)), $flag);
@@ -837,7 +991,7 @@
     {
     $mailbox = $this->_mod_mailbox($mbox);
 
-    // make shure mailbox exists
+    // make sure mailbox exists
     if (in_array($mailbox, $this->_list_mailboxes()))
       $saved = iil_C_Append($this->conn, $mailbox, $message);
 
@@ -857,10 +1011,15 @@
     $to_mbox = $this->_mod_mailbox($to_mbox);
     $from_mbox = $from_mbox ? $this->_mod_mailbox($from_mbox) : $this->mailbox;
 
-    // make shure mailbox exists
+    // make sure mailbox exists
     if (!in_array($to_mbox, $this->_list_mailboxes()))
-      return FALSE;
-    
+      {
+      if (in_array(strtolower($to_mbox), $this->default_folders))
+        $this->create_mailbox($to_mbox, TRUE);
+      else
+        return FALSE;
+      }
+
     // convert the list of uids to array
     $a_uids = is_string($uids) ? explode(',', $uids) : (is_array($uids) ? $uids : NULL);
     
@@ -958,8 +1117,18 @@
     
     if ($msg_count>0)
       {
-      $this->clear_message_cache($mailbox.'.msg');
-      return iil_C_ClearFolder($this->conn, $mailbox);
+      $cleared = iil_C_ClearFolder($this->conn, $mailbox);
+      
+      // make sure the message count cache is cleared as well
+      if ($cleared)
+        {
+        $this->clear_message_cache($mailbox.'.msg');      
+        $a_mailbox_cache = $this->get_cache('messagecount');
+        unset($a_mailbox_cache[$mailbox]);
+        $this->update_cache('messagecount', $a_mailbox_cache);
+        }
+        
+      return $cleared;
       }
     else
       return 0;
@@ -1016,6 +1185,23 @@
     // filter folders and sort them
     $sa_unsubscribed = $this->_sort_mailbox_list($a_folders);
     return $sa_unsubscribed;
+    }
+
+
+  /**
+   * Get quota
+   * added by Nuny
+   */
+  function get_quota()
+    {
+    if ($this->get_capability('QUOTA'))
+      {
+      $result = iil_C_GetQuota($this->conn);
+      if ($result["total"])
+        return sprintf("%.2fMB / %.2fMB (%.0f%%)", $result["used"] / 1000.0, $result["total"] / 1000.0, $result["percent"]);       
+      }
+
+    return FALSE;
     }
 
 
@@ -1296,14 +1482,14 @@
       $header = iil_C_FetchHeader($this->conn, $mailbox, "$msg_count");
       $cache_uid = array_pop($cache_index);
       
-      // uids of highes message matches -> cache seems OK
+      // uids of highest message matches -> cache seems OK
       if ($cache_uid == $header->uid)
         return 1;
 
       // cache is dirty
       return -1;
       }
-    // if cache count differs less that 10% report as dirty
+    // if cache count differs less than 10% report as dirty
     else if (abs($msg_count - $cache_count) < $msg_count/10)
       return -1;
     else
@@ -1382,9 +1568,13 @@
     }
 
    
-  function get_message_cache_index($key, $force=FALSE, $sort_col='idx')
+  function get_message_cache_index($key, $force=FALSE, $sort_col='idx', $sort_order='ASC')
     {
     static $sa_message_index = array();
+    
+    // empty key -> empty array
+    if (empty($key))
+      return array();
     
     if (!empty($sa_message_index[$key]) && !$force)
       return $sa_message_index[$key];
@@ -1395,7 +1585,7 @@
        FROM ".get_table_name('messages')."
        WHERE  user_id=?
        AND    cache_key=?
-       ORDER BY ".$sort_col." ASC",
+       ORDER BY ".$this->db->quote_identifier($sort_col)." ".$sort_order,
       $_SESSION['user_id'],
       $key);
 
@@ -1408,21 +1598,21 @@
 
   function add_message_cache($key, $index, $headers)
     {
-    if (!is_object($headers) || empty($headers->uid))
+    if (!$key || !is_object($headers) || empty($headers->uid))
       return;
 
     $this->db->query(
       "INSERT INTO ".get_table_name('messages')."
-       (user_id, del, cache_key, idx, uid, subject, ".$this->db->quoteIdentifier('from').", ".$this->db->quoteIdentifier('to').", cc, date, size, headers)
-       VALUES (?, 0, ?, ?, ?, ?, ?, ?, ?, ".$this->db->fromunixtime($headers->timestamp).", ?, ?)",
+       (user_id, del, cache_key, created, idx, uid, subject, ".$this->db->quoteIdentifier('from').", ".$this->db->quoteIdentifier('to').", cc, date, size, headers)
+       VALUES (?, 0, ?, now(), ?, ?, ?, ?, ?, ?, ".$this->db->fromunixtime($headers->timestamp).", ?, ?)",
       $_SESSION['user_id'],
       $key,
       $index,
       $headers->uid,
-      substr($this->decode_header($headers->subject, TRUE), 0, 128),
-      substr($this->decode_header($headers->from, TRUE), 0, 128),
-      substr($this->decode_header($headers->to, TRUE), 0, 128),
-      substr($this->decode_header($headers->cc, TRUE), 0, 128),
+      (string)substr($this->decode_header($headers->subject, TRUE), 0, 128),
+      (string)substr($this->decode_header($headers->from, TRUE), 0, 128),
+      (string)substr($this->decode_header($headers->to, TRUE), 0, 128),
+      (string)substr($this->decode_header($headers->cc, TRUE), 0, 128),
       (int)$headers->size,
       serialize($headers));
     }
@@ -1548,10 +1738,10 @@
         $rest = quoted_printable_decode($rest);
         }
 
-      return decode_specialchars($rest, $a[0]);
+      return rcube_charset_convert($rest, $a[0]);
       }
     else
-      return $str;    //we dont' know what to do with this  
+      return $str;    // we dont' know what to do with this  
     }
 
 
@@ -1599,7 +1789,7 @@
   function charset_decode($body, $ctype_param)
     {
     if (is_array($ctype_param) && !empty($ctype_param['charset']))
-      return decode_specialchars($body, $ctype_param['charset']);
+      return rcube_charset_convert($body, $ctype_param['charset']);
 
     return $body;
     }
@@ -1648,6 +1838,15 @@
     return array_merge($a_defaults, $a_out);
     }
 
+  function get_id($uid) 
+    {
+      return $this->_uid2id($uid);
+    }
+  
+  function get_uid($id)
+    {
+      return $this->_id2uid($id);
+    }
 
   function _uid2id($uid, $mbox=NULL)
     {
@@ -1658,6 +1857,14 @@
       $this->uid_id_map[$mbox][$uid] = iil_C_UID2ID($this->conn, $mbox, $uid);
 
     return $this->uid_id_map[$mbox][$uid];
+    }
+
+  function _id2uid($id, $mbox=NULL)
+    {
+    if (!$mbox)
+      $mbox = $this->mailbox;
+      
+    return iil_C_ID2UID($this->conn, $mbox, $id);
     }
 
 
@@ -1877,4 +2084,4 @@
   return trim($output);
   }
 
-?>
+?>
\ No newline at end of file

--
Gitblit v1.9.1