From e5d60d69d4108a0e62270548117773244ca1905e Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Wed, 09 Dec 2009 03:18:47 -0500
Subject: [PATCH] - Use built-in json_encode() for proper JSON format in AJAX replies (and compat. with jQuery 1.4)

---
 CHANGELOG                             |    1 
 program/include/rcube_shared.inc      |   91 ---------------------------------------------
 program/steps/mail/compose.inc        |    2 
 program/include/rcube_json_output.php |    4 +-
 program/steps/mail/func.inc           |    2 
 program/include/rcube_template.php    |    4 +-
 6 files changed, 7 insertions(+), 97 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index d621e10..17dd365 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 CHANGELOG RoundCube Webmail
 ===========================
 
+- Use built-in json_encode() for proper JSON format in AJAX replies
 - Allow setting only selected params in 'message_compose' hook (#1486312)
 - Plugin API: added 'message_compose_body' hook (#1486285)
 - Fix counters of all folders are checked in 'getunread' action  with check_all_folders disabled (#1486128)
diff --git a/program/include/rcube_json_output.php b/program/include/rcube_json_output.php
index ea23dc2..cb8a7db 100644
--- a/program/include/rcube_json_output.php
+++ b/program/include/rcube_json_output.php
@@ -239,7 +239,7 @@
         if (!empty($this->callbacks))
           $response['callbacks'] = $this->callbacks;
 
-        echo json_serialize($response);
+        echo json_encode($response);
     }
     
     
@@ -255,7 +255,7 @@
         foreach ($this->commands as $i => $args) {
             $method = array_shift($args);
             foreach ($args as $i => $arg) {
-                $args[$i] = json_serialize($arg);
+                $args[$i] = json_encode($arg);
             }
 
             $out .= sprintf(
diff --git a/program/include/rcube_shared.inc b/program/include/rcube_shared.inc
index 2cf59af..f4c6f71 100644
--- a/program/include/rcube_shared.inc
+++ b/program/include/rcube_shared.inc
@@ -109,97 +109,6 @@
 
 
 /**
- * Returns whether an $str is a reserved word for any of the version of Javascript or ECMAScript
- * @param str String to check
- * @return boolean True if $str is a reserver word, False if not
- */
-function is_js_reserved_word($str)
-{
-  return in_array($str, array(
-    // ECMASript ver 4 reserved words
-    'as','break','case','catch','class','const','continue',
-    'default','delete','do','else','export','extends','false','finally','for','function',
-    'if','import','in','instanceof','is','namespace','new','null','package','private',
-    'public','return','super','switch','this','throw','true','try','typeof','use','var',
-    'void','while','with',
-    // ECMAScript ver 4 future reserved words
-    'abstract','debugger','enum','goto','implements','interface','native','protected',
-    'synchronized','throws','transient','volatile',
-    // special meaning in some contexts
-    'get','set',
-    // were reserved in ECMAScript ver 3
-    'boolean','byte','char','double','final','float','int','long','short','static'
-  ));
-}
-
-
-/**
- * Convert a variable into a javascript object notation
- *
- * @param mixed Input value
- * @return string Serialized JSON string
- */
-function json_serialize($var)
-{
-  if (is_object($var))
-    $var = get_object_vars($var);
-
-  if (is_array($var))
-  {
-    // empty array
-    if (!sizeof($var))
-      return '[]';
-    else
-    {
-      $keys_arr = array_keys($var);
-      $is_assoc = $have_numeric = 0;
-
-      for ($i=0; $i<sizeof($keys_arr); ++$i)
-      {
-        if (is_numeric($keys_arr[$i]))
-          $have_numeric = 1;
-        if (!is_numeric($keys_arr[$i]) || $keys_arr[$i] != $i)
-          $is_assoc = 1;
-        if ($is_assoc && $have_numeric)
-          break;
-      }
-      
-      $brackets = $is_assoc ? '{}' : '[]';
-      $pairs = array();
-
-      foreach ($var as $key => $value)
-      {
-        // enclose key with quotes if it is not variable-name conform
-        if (!preg_match('/^[_a-zA-Z]{1}[_a-zA-Z0-9]*$/', $key) || is_js_reserved_word($key))
-          $key = "'$key'";
-
-        $pairs[] = sprintf("%s%s", $is_assoc ? "$key:" : '', json_serialize($value));
-      }
-
-      return $brackets{0} . implode(',', $pairs) . $brackets{1};
-    }
-  }
-  else if (!is_string($var) && strval(intval($var)) === strval($var))
-    return $var;
-  else if (is_bool($var))
-    return $var ? '1' : '0';
-  else
-    return "'".JQ($var)."'";
-}
-
-
-/**
- * Function to convert an array to a javascript array
- * Actually an alias function for json_serialize()
- * @deprecated
- */
-function array2js($arr, $type='')
-{
-  return json_serialize($arr);
-}
-
-
-/**
  * Similar function as in_array() but case-insensitive
  *
  * @param mixed Needle value
diff --git a/program/include/rcube_template.php b/program/include/rcube_template.php
index 5226f8d..19f2d43 100755
--- a/program/include/rcube_template.php
+++ b/program/include/rcube_template.php
@@ -414,12 +414,12 @@
     {
         $out = '';
         if (!$this->framed && !empty($this->js_env)) {
-            $out .= JS_OBJECT_NAME . '.set_env('.json_serialize($this->js_env).");\n";
+            $out .= JS_OBJECT_NAME . '.set_env('.json_encode($this->js_env).");\n";
         }
         foreach ($this->js_commands as $i => $args) {
             $method = array_shift($args);
             foreach ($args as $i => $arg) {
-                $args[$i] = json_serialize($arg);
+                $args[$i] = json_encode($arg);
             }
             $parent = $this->framed || preg_match('/^parent\./', $method);
             $out .= sprintf(
diff --git a/program/steps/mail/compose.inc b/program/steps/mail/compose.inc
index a441e00..999242c 100644
--- a/program/steps/mail/compose.inc
+++ b/program/steps/mail/compose.inc
@@ -506,7 +506,7 @@
       JQ(Q(rcube_label('close'))),
       JQ(Q(rcube_label('revertto'))),
       JQ(Q(rcube_label('nospellerrors'))),
-      json_serialize($spellcheck_langs),
+      json_encode($spellcheck_langs),
       $lang,
       $attrib['id'],
       JS_OBJECT_NAME), 'foot');
diff --git a/program/steps/mail/func.inc b/program/steps/mail/func.inc
index b3016ee..6b2ce09 100644
--- a/program/steps/mail/func.inc
+++ b/program/steps/mail/func.inc
@@ -522,7 +522,7 @@
   
   if (is_array($quota)) {
     $OUTPUT->add_script('$(document).ready(function(){
-	rcmail.set_quota('.json_serialize($quota).')});', 'foot');
+	rcmail.set_quota('.json_encode($quota).')});', 'foot');
     $quota = '';
     }
   

--
Gitblit v1.9.1