Support SORT=DISPLAY extension
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 22 Oct 2009 20:18:07 +0000 (14:18 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 22 Oct 2009 20:18:07 +0000 (14:18 -0600)
framework/Imap_Client/lib/Horde/Imap/Client.php
framework/Imap_Client/lib/Horde/Imap/Client/Base.php
framework/Imap_Client/lib/Horde/Imap/Client/Cclient.php
framework/Imap_Client/lib/Horde/Imap/Client/Cclient/Pop3.php
framework/Imap_Client/lib/Horde/Imap/Client/Socket.php
framework/Imap_Client/lib/Horde/Imap/Client/Socket/Pop3.php
framework/Imap_Client/package.xml

index ec7301d..e2f8ba2 100644 (file)
@@ -108,6 +108,9 @@ class Horde_Imap_Client
     /* SORT_THREAD provided for completeness - it is not a valid sort criteria
      * for search() (use thread() instead). */
     const SORT_THREAD = 9;
+    /* Sort criteria defined in draft-ietf-morg-sortdisplay-02 */
+    const SORT_DISPLAYFROM = 10;
+    const SORT_DISPLAYTO = 11;
 
     const SORT_RESULTS_COUNT = 1;
     const SORT_RESULTS_MATCH = 2;
index 5c4bbcf..f314c05 100644 (file)
@@ -1254,6 +1254,10 @@ abstract class Horde_Imap_Client_Base
      * Horde_Imap_Client::SORT_SIZE
      * Horde_Imap_Client::SORT_SUBJECT
      * Horde_Imap_Client::SORT_TO
+     * [On servers that support SORT=DISPLAY, these criteria are also
+     *  available:]
+     * Horde_Imap_Client::SORT_DISPLAYFROM
+     * Horde_Imap_Client::SORT_DISPLAYTO
      * </pre>
      *          Additionally, any sort criteria can be sorted in reverse order
      *          (instead of the default ascending order) by adding a
index 5ac8432..e89ecb8 100644 (file)
@@ -772,13 +772,15 @@ class Horde_Imap_Client_Cclient extends Horde_Imap_Client_Base
     {
         // Already guaranteed to be logged in here.
 
-        /* If more than 1 sort criteria given, or if SORT_REVERSE is given
-         * as a sort criteria, or search query uses IMAP4 criteria, use the
-         * Socket client instead. */
+        /* If more than 1 sort criteria given, or if SORT_REVERSE,
+         * SORT_DISPLAYFROM, or SORT_DISPLAYTO is given as a sort criteria, or
+         * search query uses IMAP4 criteria, use the Socket client instead. */
         if ($options['_query']['imap4'] ||
             (!empty($options['sort']) &&
              ((count($options['sort']) > 1) ||
-             in_array(Horde_Imap_Client::SORT_REVERSE, $options['sort'])))) {
+              in_array(Horde_Imap_Client::SORT_REVERSE, $options['sort']) ||
+              in_array(Horde_Imap_Client::SORT_DISPLAYFROM, $options['sort']) ||
+              in_array(Horde_Imap_Client::SORT_DISPLAYTO, $options['sort'])))) {
             return $this->_getSocket()->search($this->_selected, $query, $options);
         }
 
index b03fac0..338d159 100644 (file)
@@ -237,13 +237,15 @@ class Horde_Imap_Client_Cclient_Pop3 extends Horde_Imap_Client_Cclient
         // POP 3 supports c-client search criteria only.
         $search_query = $query->build();
 
-        /* If more than 1 sort criteria given, or if SORT_REVERSE is given
-         * as a sort criteria, or search query uses IMAP4 criteria, use the
-         * Socket client instead. */
+        /* If more than 1 sort criteria given, or if SORT_REVERSE,
+         * SORT_DISPLAYFROM, or SORT_DISPLAYTO is given as a sort criteria,
+         * or search query uses IMAP4 criteria, fail. */
         if ($search_query['imap4'] ||
             (!empty($options['sort']) &&
              ((count($options['sort']) > 1) ||
-             in_array(Horde_Imap_Client::SORT_REVERSE, $options['sort'])))) {
+              in_array(Horde_Imap_Client::SORT_REVERSE, $options['sort']) ||
+              in_array(Horde_Imap_Client::SORT_DISPLAYFROM, $options['sort']) ||
+              in_array(Horde_Imap_Client::SORT_DISPLAYTO, $options['sort'])))) {
             throw new Horde_Imap_Client_Exception('Unsupported search criteria on POP3 server.', Horde_Imap_Client_Exception::POP3_NOTSUPPORTED);
         }
 
index 53bfd1b..724f779 100644 (file)
@@ -34,6 +34,8 @@
  *   RFC 5267 - ESORT
  *   RFC 5464 - METADATA
  *
+ *   draft-ietf-morg-sortdisplay-02 - SORT=DISPLAY
+ *
  *   [NO RFC] - XIMAPPROXY
  *       + Requires imapproxy v1.2.7-rc1 or later
  *       + See http://lists.andrew.cmu.edu/pipermail/imapproxy-info/2008-October/000771.html and
@@ -1506,6 +1508,8 @@ class Horde_Imap_Client_Socket extends Horde_Imap_Client_Base
             Horde_Imap_Client::SORT_ARRIVAL => 'ARRIVAL',
             Horde_Imap_Client::SORT_CC => 'CC',
             Horde_Imap_Client::SORT_DATE => 'DATE',
+            Horde_Imap_Client::SORT_DISPLAYFROM => 'DISPLAYFROM',
+            Horde_Imap_Client::SORT_DISPLAYTO => 'DISPLAYTO',
             Horde_Imap_Client::SORT_FROM => 'FROM',
             Horde_Imap_Client::SORT_REVERSE => 'REVERSE',
             Horde_Imap_Client::SORT_SIZE => 'SIZE',
@@ -1532,6 +1536,13 @@ class Horde_Imap_Client_Socket extends Horde_Imap_Client_Base
             if (count(array_intersect($options['sort'], array_keys($sort_criteria))) === 0) {
                 $options['sort'] = array(Horde_Imap_Client::SORT_ARRIVAL);
             }
+
+            /* Make sure server supports DISPLAYFROM & DISPLAYTO. */
+            if ((in_array(Horde_Imap_Client::SORT_DISPLAYFROM, $options['sort']) ||
+                 in_array(Horde_Imap_Client::SORT_DISPLAYTO, $options['sort'])) &&
+                (!is_array($server_sort) || !in_array('DISPLAY', $server_sort))) {
+                $server_sort = false;
+            }
         }
 
         if ($server_sort) {
@@ -1736,6 +1747,7 @@ class Horde_Imap_Client_Socket extends Horde_Imap_Client_Base
             $slices = array();
 
             foreach ($slices_list as $slice_start => $slice) {
+                $display_sort = false;
                 $sorted = array();
 
                 if ($reverse) {
@@ -1758,21 +1770,31 @@ class Horde_Imap_Client_Socket extends Horde_Imap_Client_Base
                     asort($sorted, SORT_NUMERIC);
                     break;
 
+                case Horde_Imap_Client::SORT_DISPLAYFROM:
+                case Horde_Imap_Client::SORT_DISPLAYTO:
+                    $display_sort = true;
+                    // Fallthrough
+
                 case Horde_Imap_Client::SORT_CC:
                 case Horde_Imap_Client::SORT_FROM:
                 case Horde_Imap_Client::SORT_TO:
                     if ($val == Horde_Imap_Client::SORT_CC) {
                         $field = 'cc';
-                    } elseif ($val = Horde_Imap_Client::SORT_FROM) {
+                    } elseif (in_array($val, array(Horde_Imap_Client::SORT_DISPLAYFROM, Horde_Imap_Client::SORT_FROM))) {
                         $field = 'from';
                     } else {
                         $field = 'to';
                     }
 
                     foreach ($slice as $num) {
-                        $sorted[$num] = empty($fetch_res[$num]['envelope'][$field])
-                            ? null
-                            : $fetch_res[$num]['envelope'][$field][0]['mailbox'];
+                        if (empty($fetch_res[$num]['envelope'][$field])) {
+                            $sorted[$num] = null;
+                        } else {
+                            $tmp = ($display_sort && !empty($fetch_res[$num]['envelope'][$field][0]['personal']))
+                                ? 'personal'
+                                : 'mailbox';
+                            $sorted[$num] = $fetch_res[$num]['envelope'][$field][0][$tmp];
+                        }
                     }
                     asort($sorted, SORT_LOCALE_STRING);
                     break;
index 0f7ccac..cefdddb 100644 (file)
@@ -648,7 +648,10 @@ class Horde_Imap_Client_Socket_Pop3 extends Horde_Imap_Client_Base
         // Only support a single query: an ALL search sorted by arrival.
         if (($options['_query']['query'] != 'ALL') ||
             (!empty($options['sort']) &&
-             ((count($options['sort']) > 1) || (reset($options['sort']) != Horde_Imap_Client::SORT_ARRIVAL)))) {
+             ((count($options['sort']) > 1) ||
+              in_array(Horde_Imap_Client::SORT_ARRIVAL, $options['sort']) ||
+              in_array(Horde_Imap_Client::SORT_DISPLAYFROM, $options['sort']) ||
+              in_array(Horde_Imap_Client::SORT_DISPLAYTO, $options['sort'])))) {
             throw new Horde_Imap_Client_Exception('Server search not supported on POP3 server.', Horde_Imap_Client_Exception::POP3_NOTSUPPORTED);
         }
 
index 6382b99..851a237 100644 (file)
@@ -31,9 +31,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
   <api>alpha</api>
  </stability>
  <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes>* Added search and thread (message list) caching.
-* Added PHP socket based POP3 driver.
-* Initial release
+ <notes>* Support SORT=DISPLAY extension.
+ * Added search and thread (message list) caching.
+ * Added PHP socket based POP3 driver.
+ * Initial release
  </notes>
  <contents>
   <dir name="/">