Honor the truncation size from the client
authorMichael J. Rubinsky <mrubinsk@horde.org>
Thu, 3 Jun 2010 16:50:45 +0000 (12:50 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Thu, 3 Jun 2010 17:29:03 +0000 (13:29 -0400)
framework/ActiveSync/lib/Horde/ActiveSync/Driver/Base.php
framework/ActiveSync/lib/Horde/ActiveSync/Driver/Horde.php
framework/ActiveSync/lib/Horde/ActiveSync/Message/Base.php

index 46fff28..cf68559 100644 (file)
@@ -235,11 +235,12 @@ abstract class Horde_ActiveSync_Driver_Base
     abstract public function statMessage($folderId, $id);
 
     /**
+     * Obtain an ActiveSync message from the backend.
      *
-     * @param $folderid
-     * @param $id
-     * @param $truncsize
-     * @param $mimesupport
+     * @param string $folderid      The server's folder id this message is from
+     * @param string $id            The server's message id
+     * @param integer $truncsize    A TRUNCATION_* constant
+     * @param integer $mimesupport  Mime support for this message
      *
      * @return Horde_ActiveSync_Message_Base The message data
      */
@@ -469,7 +470,7 @@ abstract class Horde_ActiveSync_Driver_Base
      * @param $flags
      * @return unknown_type
      */
-    function setReadFlag($folderid, $id, $flags)
+    public function setReadFlag($folderid, $id, $flags)
     {
         return false;
     }
@@ -600,4 +601,29 @@ abstract class Horde_ActiveSync_Driver_Base
         . '</wap-provisioningdoc>';
     }
 
+    /**
+     * Truncate an UTF-8 encoded sting correctly
+     *
+     * If it's not possible to truncate properly, an empty string is returned
+     *
+     * @param string $string  The string to truncate
+     * @param string $length  The length of the returned string
+     *
+     * @return string  The truncated string
+     */
+    static public function truncate($string, $length)
+    {
+        if (strlen($string) <= $length) {
+            return $string;
+        }
+        while($length >= 0) {
+            if ((ord($string[$length]) < 0x80) || (ord($string[$length]) >= 0xC0)) {
+                return substr($string, 0, $length);
+            }
+            $length--;
+        }
+
+        return "";
+    }
+
 }
\ No newline at end of file
index 9f7cce0..bdf8102 100644 (file)
@@ -376,7 +376,7 @@ class Horde_ActiveSync_Driver_Horde extends Horde_ActiveSync_Driver_Base
     /**
      * Get a message from the backend
      *
-     * @see framework/ActiveSync/lib/Horde/ActiveSync/Driver/Horde_ActiveSync_Driver_Base#getMessage($folderid, $id, $truncsize, $mimesupport)
+     * @see framework/ActiveSync/lib/Horde/ActiveSync/Driver/Horde_ActiveSync_Driver_Base#getMessage
      */
     public function getMessage($folderid, $id, $truncsize, $mimesupport = 0)
     {
@@ -386,7 +386,7 @@ class Horde_ActiveSync_Driver_Horde extends Horde_ActiveSync_Driver_Base
         switch ($folderid) {
         case self::APPOINTMENTS_FOLDER:
             try {
-                return $this->_connector->calendar_export($id);
+                $message = $this->_connector->calendar_export($id);
             } catch (Horde_Exception $e) {
                 $this->_logger->err($e->getMessage());
                 return false;
@@ -395,17 +395,16 @@ class Horde_ActiveSync_Driver_Horde extends Horde_ActiveSync_Driver_Base
 
         case self::CONTACTS_FOLDER:
             try {
-                return $this->_connector->contacts_export($id);
+                $message = $this->_connector->contacts_export($id);
             } catch (Horde_Exception $e) {
                 $this->_logger->err($e->getMessage());
                 return false;
             }
-
             break;
 
         case self::TASKS_FOLDER:
             try {
-                return $this->_connector->tasks_export($id);
+                $message = $this->_connector->tasks_export($id);
             } catch (Horde_Exception $e) {
                 $this->_logger->err($e->getMessage());
                 return false;
@@ -414,6 +413,15 @@ class Horde_ActiveSync_Driver_Horde extends Horde_ActiveSync_Driver_Base
         default:
             return false;
         }
+        if (strlen($message->body) > $truncsize) {
+            $message->body = self::truncate($message->body, $truncsize);
+            $message->bodytruncated = 1;
+        } else {
+            // Be certain this is set.
+            $message->bodytruncated = 0;
+        }
+
+        return $message;
     }
 
     /**
index 10f4c55..072d0db 100644 (file)
@@ -285,26 +285,6 @@ class Horde_ActiveSync_Message_Base
     }
 
     /**
-     * Decodes a wbxml string into this object's properties.
-     *
-     * @param string $wbxml
-     */
-    public function decode($wbxml)
-    {
-        throw new Horde_ActiveSync_Exception('Not implemented.');
-    }
-
-    /**
-     * Encodes this message object into a wbxml string.
-     *
-     * @return string wbxml string
-     */
-    public function encode()
-    {
-        throw new Horde_ActiveSync_Exception('Not Implemented.');
-    }
-
-    /**
      * Encodes this object (and any sub-objects) as wbxml to the output stream.
      * Output is ordered according to $_mapping
      *