summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAleksander Machniak <alec@alec.pl>2012-10-24 20:13:57 +0200
committerAleksander Machniak <alec@alec.pl>2012-10-24 20:13:57 +0200
commit37d055e9ac0616cd210279e8db5efe3e51a26899 (patch)
tree1d793a8e8d66dfac7bd1d6fa124a5fa307edc9fd
parent1f4ffa342d4d613fc5801b0f04d00a40fe422518 (diff)
downloadwebadmin-37d055e9ac0616cd210279e8db5efe3e51a26899.tar.gz
Implement base64 encoding of binary content in JSON data (Bug #1047)
-rw-r--r--lib/functions.php2
-rw-r--r--lib/kolab_api_controller.php5
-rw-r--r--lib/kolab_json_output.php68
-rw-r--r--lib/kolab_utils.php2
4 files changed, 73 insertions, 4 deletions
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@kolabsys.com> |
+ | Author: Aleksander Machniak <machniak@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);
}