Add Horde_Imap_Client_Base::validSearchCharset()
authorMichael M Slusarz <slusarz@curecanti.org>
Mon, 26 Apr 2010 21:03:53 +0000 (15:03 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Mon, 26 Apr 2010 21:33:25 +0000 (15:33 -0600)
framework/Imap_Client/lib/Horde/Imap/Client/Base.php
framework/Imap_Client/package.xml

index 3340b4c..24e988d 100644 (file)
@@ -72,7 +72,8 @@ abstract class Horde_Imap_Client_Base
      */
     protected $_init = array(
         'enabled' => array(),
-        'namespace' => array()
+        'namespace' => array(),
+        's_charset' => array()
     );
 
     /**
@@ -2714,7 +2715,7 @@ abstract class Horde_Imap_Client_Base
      */
     abstract protected function _setMetadata($mailbox, $data);
 
-    /* Utility functions. */
+    /* Public utility functions. */
 
     /**
      * Returns a unique identifier for the current mailbox status.
@@ -3011,6 +3012,58 @@ abstract class Horde_Imap_Client_Base
     }
 
     /**
+     * Determines if the given charset is valid for search-related queries.
+     *
+     * @param string $charset  The query charset.
+     *
+     * @return boolean  True if server supports this charset.
+     */
+    public function validSearchCharset($charset)
+    {
+        if (!isset($this->_init['enabled']['s_charset'][$charset])) {
+            $support = null;
+
+            switch ($charset) {
+            case 'US-ASCII';
+                /* US-ASCII is always supported (RFC 3501 [6.4.4]). */
+                $support = true;
+                break;
+
+            case 'UTF-8':
+                /* SORT (RFC 5266) & ESORT (RFC 5267) require UTF-8
+                 * support. */
+                if ($this->queryCapability('SORT') ||
+                    $this->queryCapability('ESORT')) {
+                    $support = true;
+                }
+                break;
+            }
+
+            /* Use a dummy search query and search for BADCHARSET
+             * response. */
+            if (is_null($support)) {
+                $query = new Horde_Imap_Client_Search_Query();
+                $query->charset($charset);
+                $query->text('a');
+                try {
+                    $this->search('INBOX', $query);
+                    $support = true;
+                } catch (Horde_Imap_Client_Exception $e) {
+                    /* BADCHARSET is only a MAYBE return - but there is no
+                     * other way of determining charset support. */
+                    $support = ($e->getCode() != Horde_Imap_Client_Exception::BADCHARSET);
+                }
+            }
+
+            $this->_init['enabled']['s_charset'][$charset] = $support;
+        }
+
+        return $this->_init['enabled']['s_charset'][$charset];
+    }
+
+    /* Private utility functions. */
+
+    /**
      * Returns UIDs for an ALL search, or for a sequence number -> UID lookup.
      *
      * @param mixed $ids    If null, return all UIDs for the mailbox. If an
index dbe1c20..d30f63f 100644 (file)
@@ -31,7 +31,8 @@ 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>* Add Horde_Imap_Client_Base::fetchFromSectionString().
+ <notes>* Add Horde_Imap_Client_Base::validSearchCharset().
+ * Add Horde_Imap_Client_Base::fetchFromSectionString().
  * Add support for RFC 4469 (CATENATE).
  * Correctly output 8-bit strings (RFC 3501 [4.3]).
  * Add Horde_Imap_Client_Utils::stripNonAtomChars().