Changeset 3170 in subversion


Ignore:
Timestamp:
Dec 9, 2009 3:18:47 AM (3 years ago)
Author:
alec
Message:
  • Use built-in json_encode() for proper JSON format in AJAX replies (and compat. with jQuery 1.4)
Location:
trunk/roundcubemail
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/roundcubemail/CHANGELOG

    r3166 r3170  
    22=========================== 
    33 
     4- Use built-in json_encode() for proper JSON format in AJAX replies 
    45- Allow setting only selected params in 'message_compose' hook (#1486312) 
    56- Plugin API: added 'message_compose_body' hook (#1486285) 
  • trunk/roundcubemail/program/include/rcube_json_output.php

    r2905 r3170  
    240240          $response['callbacks'] = $this->callbacks; 
    241241 
    242         echo json_serialize($response); 
     242        echo json_encode($response); 
    243243    } 
    244244     
     
    256256            $method = array_shift($args); 
    257257            foreach ($args as $i => $arg) { 
    258                 $args[$i] = json_serialize($arg); 
     258                $args[$i] = json_encode($arg); 
    259259            } 
    260260 
  • trunk/roundcubemail/program/include/rcube_shared.inc

    r3132 r3170  
    106106    exit; 
    107107    } 
    108 } 
    109  
    110  
    111 /** 
    112  * Returns whether an $str is a reserved word for any of the version of Javascript or ECMAScript 
    113  * @param str String to check 
    114  * @return boolean True if $str is a reserver word, False if not 
    115  */ 
    116 function is_js_reserved_word($str) 
    117 { 
    118   return in_array($str, array( 
    119     // ECMASript ver 4 reserved words 
    120     'as','break','case','catch','class','const','continue', 
    121     'default','delete','do','else','export','extends','false','finally','for','function', 
    122     'if','import','in','instanceof','is','namespace','new','null','package','private', 
    123     'public','return','super','switch','this','throw','true','try','typeof','use','var', 
    124     'void','while','with', 
    125     // ECMAScript ver 4 future reserved words 
    126     'abstract','debugger','enum','goto','implements','interface','native','protected', 
    127     'synchronized','throws','transient','volatile', 
    128     // special meaning in some contexts 
    129     'get','set', 
    130     // were reserved in ECMAScript ver 3 
    131     'boolean','byte','char','double','final','float','int','long','short','static' 
    132   )); 
    133 } 
    134  
    135  
    136 /** 
    137  * Convert a variable into a javascript object notation 
    138  * 
    139  * @param mixed Input value 
    140  * @return string Serialized JSON string 
    141  */ 
    142 function json_serialize($var) 
    143 { 
    144   if (is_object($var)) 
    145     $var = get_object_vars($var); 
    146  
    147   if (is_array($var)) 
    148   { 
    149     // empty array 
    150     if (!sizeof($var)) 
    151       return '[]'; 
    152     else 
    153     { 
    154       $keys_arr = array_keys($var); 
    155       $is_assoc = $have_numeric = 0; 
    156  
    157       for ($i=0; $i<sizeof($keys_arr); ++$i) 
    158       { 
    159         if (is_numeric($keys_arr[$i])) 
    160           $have_numeric = 1; 
    161         if (!is_numeric($keys_arr[$i]) || $keys_arr[$i] != $i) 
    162           $is_assoc = 1; 
    163         if ($is_assoc && $have_numeric) 
    164           break; 
    165       } 
    166        
    167       $brackets = $is_assoc ? '{}' : '[]'; 
    168       $pairs = array(); 
    169  
    170       foreach ($var as $key => $value) 
    171       { 
    172         // enclose key with quotes if it is not variable-name conform 
    173         if (!preg_match('/^[_a-zA-Z]{1}[_a-zA-Z0-9]*$/', $key) || is_js_reserved_word($key)) 
    174           $key = "'$key'"; 
    175  
    176         $pairs[] = sprintf("%s%s", $is_assoc ? "$key:" : '', json_serialize($value)); 
    177       } 
    178  
    179       return $brackets{0} . implode(',', $pairs) . $brackets{1}; 
    180     } 
    181   } 
    182   else if (!is_string($var) && strval(intval($var)) === strval($var)) 
    183     return $var; 
    184   else if (is_bool($var)) 
    185     return $var ? '1' : '0'; 
    186   else 
    187     return "'".JQ($var)."'"; 
    188 } 
    189  
    190  
    191 /** 
    192  * Function to convert an array to a javascript array 
    193  * Actually an alias function for json_serialize() 
    194  * @deprecated 
    195  */ 
    196 function array2js($arr, $type='') 
    197 { 
    198   return json_serialize($arr); 
    199108} 
    200109 
  • trunk/roundcubemail/program/include/rcube_template.php

    r2980 r3170  
    415415        $out = ''; 
    416416        if (!$this->framed && !empty($this->js_env)) { 
    417             $out .= JS_OBJECT_NAME . '.set_env('.json_serialize($this->js_env).");\n"; 
     417            $out .= JS_OBJECT_NAME . '.set_env('.json_encode($this->js_env).");\n"; 
    418418        } 
    419419        foreach ($this->js_commands as $i => $args) { 
    420420            $method = array_shift($args); 
    421421            foreach ($args as $i => $arg) { 
    422                 $args[$i] = json_serialize($arg); 
     422                $args[$i] = json_encode($arg); 
    423423            } 
    424424            $parent = $this->framed || preg_match('/^parent\./', $method); 
  • trunk/roundcubemail/program/steps/mail/compose.inc

    r3166 r3170  
    507507      JQ(Q(rcube_label('revertto'))), 
    508508      JQ(Q(rcube_label('nospellerrors'))), 
    509       json_serialize($spellcheck_langs), 
     509      json_encode($spellcheck_langs), 
    510510      $lang, 
    511511      $attrib['id'], 
  • trunk/roundcubemail/program/steps/mail/func.inc

    r3099 r3170  
    523523  if (is_array($quota)) { 
    524524    $OUTPUT->add_script('$(document).ready(function(){ 
    525         rcmail.set_quota('.json_serialize($quota).')});', 'foot'); 
     525        rcmail.set_quota('.json_encode($quota).')});', 'foot'); 
    526526    $quota = ''; 
    527527    } 
Note: See TracChangeset for help on using the changeset viewer.