lib/functions.php lib/kolab_api_controller.php lib/kolab_json_output.php lib/kolab_utils.php

Aleksander Machniak machniak at kolabsys.com
Wed Oct 24 20:14:58 CEST 2012


 lib/functions.php            |    2 -
 lib/kolab_api_controller.php |    5 ++-
 lib/kolab_json_output.php    |   68 ++++++++++++++++++++++++++++++++++++++++++-
 lib/kolab_utils.php          |    2 -
 4 files changed, 73 insertions(+), 4 deletions(-)

New commits:
commit 37d055e9ac0616cd210279e8db5efe3e51a26899
Author: Aleksander Machniak <alec at alec.pl>
Date:   Wed Oct 24 20:13:57 2012 +0200

    Implement base64 encoding of binary content in JSON data (Bug #1047)

diff --git a/lib/functions.php b/lib/functions.php
index 9761358..4183454 100644
--- a/lib/functions.php
+++ b/lib/functions.php
@@ -127,7 +127,7 @@ function timer($time = null, $label = '')
 {
     $now = microtime(true);
     if ($time) {
-        //console(($label ? $label.' ' : '') . sprintf('%.4f', $now - $time));
+        console(($label ? $label.' ' : '') . sprintf('%.4f', $now - $time));
     }
     return $now;
 }
diff --git a/lib/kolab_api_controller.php b/lib/kolab_api_controller.php
index 0e45a30..a4a4b78 100644
--- a/lib/kolab_api_controller.php
+++ b/lib/kolab_api_controller.php
@@ -138,10 +138,13 @@ class kolab_api_controller
 
         $service = $this->request['service'];
         $method  = $this->request['method'];
-        $postdata = @json_decode($postdata, true);
 
         Log::debug("Calling $service.$method");
 
+        // Decode request data
+        $postdata = @json_decode($postdata, true);
+        kolab_json_output::decode($postdata);
+
         // validate user session
         if (!in_array($method, array('quit', 'authenticate'))) {
             if (!$this->session_validate($postdata)) {
diff --git a/lib/kolab_json_output.php b/lib/kolab_json_output.php
index fe0d062..473e579 100644
--- a/lib/kolab_json_output.php
+++ b/lib/kolab_json_output.php
@@ -19,6 +19,7 @@
  | along with this program. If not, see <http://www.gnu.org/licenses/>      |
  +--------------------------------------------------------------------------+
  | Author: Jeroen van Meeuwen <vanmeeuwen at kolabsys.com>                     |
+ | Author: Aleksander Machniak <machniak at kolabsys.com>                      |
  +--------------------------------------------------------------------------+
 */
 
@@ -29,7 +30,9 @@ class kolab_json_output
 {
 
     /**
+     * Send success response
      *
+     * @param mixed $data Data
      */
     public function success($data)
     {
@@ -42,7 +45,9 @@ class kolab_json_output
 
 
     /**
+     * Send error response
      *
+     * @param mixed $data Data
      */
     public function error($errdata, $code = 400)
     {
@@ -55,13 +60,74 @@ class kolab_json_output
 
 
     /**
+     * Send response
      *
+     * @param mixed $data Data
      */
-    public function send($data)
+    protected function send($data)
     {
+        // Encode response
+        self::encode($data);
+
+        // Send response
         header("Content-Type: application/json");
         echo json_encode($data);
         exit;
     }
 
+
+    /**
+     * Parse response and base64-encode non-UTF8/binary data
+     *
+     * @param mixed $data Data
+     *
+     * @return bool True if data was encoded
+     */
+    public static function encode(&$data)
+    {
+        if (is_array($data)) {
+            $encoded = array();
+            foreach (array_keys($data) as $key) {
+                if (self::encode($data[$key])) {
+                    $encoded[] = $key;
+                }
+            }
+            if (!empty($encoded)) {
+                $data['__encoded'] = $encoded;
+            }
+        }
+        else if (is_string($data) && $data !== '') {
+            $result = @json_encode($data);
+            // In case of invalid characters json_encode returns "null"
+            if (($result === 'null' && $data != 'null') || $result === false) {
+                $data = base64_encode($data);
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+
+    /**
+     * Parse response and base64-decode encoded data
+     *
+     * @param mixed $data Data
+     */
+    public static function decode(&$data)
+    {
+        if (is_array($data)) {
+            $encoded = $data['__encoded'];
+            foreach ($data as $key => $value) {
+                if (is_array($value)) {
+                    self::decode($data[$key]);
+                }
+                else if (is_string($value) && $encoded && in_array($key, $encoded)) {
+                    $data[$key] = base64_decode($value);
+                }
+            }
+            unset($data['__encoded']);
+        }
+    }
+
 }
diff --git a/lib/kolab_utils.php b/lib/kolab_utils.php
index 9c6c35e..2c4c2a0 100644
--- a/lib/kolab_utils.php
+++ b/lib/kolab_utils.php
@@ -149,7 +149,7 @@ class kolab_utils
     /**
      * Finds wether an array is associative or not.
      */
-    public static function is_assoc ($arr)
+    public static function is_assoc($arr)
     {
         return is_array($arr) && count(array_filter(array_keys($arr), 'is_string')) == count($arr);
     }





More information about the commits mailing list