Ticket #6913: 6193_3.patch

File 6193_3.patch, 5.7 KB (added by Wiktor Walc, 13 years ago)
  • ckeditor_php4.php

     
    531531
    532532        /**
    533533         * This little function provides a basic JSON support.
    534          * http://php.net/manual/en/function.json-encode.php
    535534         * \private
    536535         *
    537536         * @param mixed $val
     
    542541                if (is_null($val)) {
    543542                        return 'null';
    544543                }
    545                 if ($val === false) {
    546                         return 'false';
     544                if (is_bool($val)) {
     545                        return $val ? 'true' : 'false';
    547546                }
    548                 if ($val === true) {
    549                         return 'true';
     547                if (is_int($val)) {
     548                        return $val;
    550549                }
    551                 if (is_scalar($val))
    552                 {
    553                         if (is_float($val))
    554                         {
    555                                 // Always use "." for floats.
    556                                 $val = str_replace(",", ".", strval($val));
     550                if (is_float($val)) {
     551                        return str_replace(',', '.', $val);
     552                }
     553                if (is_array($val) || is_object($val)) {
     554                        if (is_array($val) && (array_keys($val) === range(0,count($val)-1))) {
     555                                return '[' . implode(',', array_map(array($this, 'jsEncode'), $val)) . ']';
    557556                        }
    558 
    559                         // Use @@ to not use quotes when outputting string value
    560                         if (strpos($val, '@@') === 0) {
    561                                 return substr($val, 2);
     557                        $temp = array();
     558                        foreach ($val as $k => $v){
     559                                $temp[] = $this->jsEncode("{$k}") . ':' . $this->jsEncode($v);
    562560                        }
    563                         else {
    564                                 // All scalars are converted to strings to avoid indeterminism.
    565                                 // PHP's "1" and 1 are equal for all PHP operators, but
    566                                 // JS's "1" and 1 are not. So if we pass "1" or 1 from the PHP backend,
    567                                 // we should get the same result in the JS frontend (string).
    568                                 // Character replacements for JSON.
    569                                 static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'),
    570                                 array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
    571 
    572                                 $val = str_replace($jsonReplaces[0], $jsonReplaces[1], $val);
    573                                 if (strtoupper(substr($val, 0, 9)) == 'CKEDITOR.') {
    574                                         return $val;
    575                                 }
    576 
    577                                 return '"' . $val . '"';
    578                         }
     561                        return '{' . implode(',', $temp) . '}';
    579562                }
    580                 $isList = true;
    581                 for ($i = 0, reset($val); $i < count($val); $i++, next($val))
    582                 {
    583                         if (key($val) !== $i)
    584                         {
    585                                 $isList = false;
    586                                 break;
    587                         }
    588                 }
    589                 $result = array();
    590                 if ($isList)
    591                 {
    592                         foreach ($val as $v) $result[] = $this->jsEncode($v);
    593                         return '[ ' . join(', ', $result) . ' ]';
    594                 }
    595                 else
    596                 {
    597                         foreach ($val as $k => $v) $result[] = $this->jsEncode($k).': '.$this->jsEncode($v);
    598                         return '{ ' . join(', ', $result) . ' }';
    599                 }
     563                // String otherwise
     564                if (strpos($val, '@@') === 0)
     565                        return substr($val, 2);
     566                if (strtoupper(substr($val, 0, 9)) == 'CKEDITOR.')
     567                        return $val;
     568
     569                return '"' . str_replace(array("\\", "/", "\n", "\t", "\r", "\x08", "\x0c", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'), $val) . '"';
    600570        }
    601571}
  • ckeditor_php5.php

     
    522522
    523523        /**
    524524         * This little function provides a basic JSON support.
    525          * http://php.net/manual/en/function.json-encode.php
    526525         *
    527526         * @param mixed $val
    528527         * @return string
     
    532531                if (is_null($val)) {
    533532                        return 'null';
    534533                }
    535                 if ($val === false) {
    536                         return 'false';
     534                if (is_bool($val)) {
     535                        return $val ? 'true' : 'false';
    537536                }
    538                 if ($val === true) {
    539                         return 'true';
     537                if (is_int($val)) {
     538                        return $val;
    540539                }
    541                 if (is_scalar($val))
    542                 {
    543                         if (is_float($val))
    544                         {
    545                                 // Always use "." for floats.
    546                                 $val = str_replace(",", ".", strval($val));
     540                if (is_float($val)) {
     541                        return str_replace(',', '.', $val);
     542                }
     543                if (is_array($val) || is_object($val)) {
     544                        if (is_array($val) && (array_keys($val) === range(0,count($val)-1))) {
     545                                return '[' . implode(',', array_map(array($this, 'jsEncode'), $val)) . ']';
    547546                        }
    548 
    549                         // Use @@ to not use quotes when outputting string value
    550                         if (strpos($val, '@@') === 0) {
    551                                 return substr($val, 2);
     547                        $temp = array();
     548                        foreach ($val as $k => $v){
     549                                $temp[] = $this->jsEncode("{$k}") . ':' . $this->jsEncode($v);
    552550                        }
    553                         else {
    554                                 // All scalars are converted to strings to avoid indeterminism.
    555                                 // PHP's "1" and 1 are equal for all PHP operators, but
    556                                 // JS's "1" and 1 are not. So if we pass "1" or 1 from the PHP backend,
    557                                 // we should get the same result in the JS frontend (string).
    558                                 // Character replacements for JSON.
    559                                 static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'),
    560                                 array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
    561 
    562                                 $val = str_replace($jsonReplaces[0], $jsonReplaces[1], $val);
    563                                 if (strtoupper(substr($val, 0, 9)) == 'CKEDITOR.') {
    564                                         return $val;
    565                                 }
    566 
    567                                 return '"' . $val . '"';
    568                         }
     551                        return '{' . implode(',', $temp) . '}';
    569552                }
    570                 $isList = true;
    571                 for ($i = 0, reset($val); $i < count($val); $i++, next($val))
    572                 {
    573                         if (key($val) !== $i)
    574                         {
    575                                 $isList = false;
    576                                 break;
    577                         }
    578                 }
    579                 $result = array();
    580                 if ($isList)
    581                 {
    582                         foreach ($val as $v) $result[] = $this->jsEncode($v);
    583                         return '[ ' . join(', ', $result) . ' ]';
    584                 }
    585                 else
    586                 {
    587                         foreach ($val as $k => $v) $result[] = $this->jsEncode($k).': '.$this->jsEncode($v);
    588                         return '{ ' . join(', ', $result) . ' }';
    589                 }
     553                // String otherwise
     554                if (strpos($val, '@@') === 0)
     555                        return substr($val, 2);
     556                if (strtoupper(substr($val, 0, 9)) == 'CKEDITOR.')
     557                        return $val;
     558
     559                return '"' . str_replace(array("\\", "/", "\n", "\t", "\r", "\x08", "\x0c", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'), $val) . '"';
    590560        }
    591561}
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy