28if (!function_exists(
'json_encode') || defined(
'PHPUNIT_MODE')) {
40 function dol_json_encode($elements)
42 dol_syslog(
"For better performance, enable the native json in your PHP", LOG_WARNING);
45 if (is_object($elements)) {
46 foreach ($elements as $key => $value) {
50 if (is_countable($elements)) {
51 $num = count($elements);
56 if (is_numeric($elements)) {
58 } elseif (is_string($elements)) {
59 return '"'.$elements.
'"';
61 if (is_numeric(key($elements)) && key($elements) == 0) {
63 $keysofelements = array_keys($elements);
65 for ($i = 0, $last = ($num - 1); $i < $num; $i++) {
66 if (!isset($elements[$keysofelements[$i]])) {
69 if (is_array($elements[$keysofelements[$i]]) || is_object($elements[$keysofelements[$i]])) {
70 $output .= json_encode($elements[$keysofelements[$i]]);
72 $output .= _val($elements[$keysofelements[$i]]);
84 $tmpelements = array();
85 if (is_array($elements)) {
86 $tmpelements = $elements;
88 if (is_object($elements)) {
89 $tmpelements = get_object_vars($elements);
91 foreach ($tmpelements as $key => $value) {
92 $output .=
'"'.$key.
'":';
93 if (is_array($value)) {
94 $output .= json_encode($value);
96 $output .= _val($value);
118 if (is_string($val)) {
121 $strlen_var = strlen($val);
127 for ($c = 0; $c < $strlen_var; ++$c) {
128 $ord_var_c = ord($val[$c]);
131 case $ord_var_c == 0x08:
134 case $ord_var_c == 0x09:
137 case $ord_var_c == 0x0A:
140 case $ord_var_c == 0x0C:
143 case $ord_var_c == 0x0D:
147 case $ord_var_c == 0x22:
148 case $ord_var_c == 0x2F:
149 case $ord_var_c == 0x5C:
151 $ascii .=
'\\'.$val[$c];
154 case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):
159 case (($ord_var_c & 0xE0) == 0xC0):
162 $char = pack(
'C*', $ord_var_c, ord($val[$c + 1]));
164 $utf16 = utf82utf16($char);
165 $ascii .= sprintf(
'\u%04s', bin2hex($utf16));
168 case (($ord_var_c & 0xF0) == 0xE0):
171 $char = pack(
'C*', $ord_var_c, ord($val[$c + 1]), ord($val[$c + 2]));
173 $utf16 = utf82utf16($char);
174 $ascii .= sprintf(
'\u%04s', bin2hex($utf16));
177 case (($ord_var_c & 0xF8) == 0xF0):
180 $char = pack(
'C*', $ord_var_c, ord($val[$c + 1]), ord($val[$c + 2]), ord($val[$c + 3]));
182 $utf16 = utf82utf16($char);
183 $ascii .= sprintf(
'\u%04s', bin2hex($utf16));
186 case (($ord_var_c & 0xFC) == 0xF8):
189 $char = pack(
'C*', $ord_var_c, ord($val[$c + 1]), ord($val[$c + 2]), ord($val[$c + 3]), ord($val[$c + 4]));
191 $utf16 = utf82utf16($char);
192 $ascii .= sprintf(
'\u%04s', bin2hex($utf16));
195 case (($ord_var_c & 0xFE) == 0xFC):
198 $char = pack(
'C*', $ord_var_c, ord($val[$c + 1]), ord($val[$c + 2]), ord($val[$c + 3]), ord($val[$c + 4]), ord($val[$c + 5]));
200 $utf16 = utf82utf16($char);
201 $ascii .= sprintf(
'\u%04s', bin2hex($utf16));
206 return '"'.$ascii.
'"';
207 } elseif (is_int($val)) {
208 return sprintf(
'%d', $val);
209 } elseif (is_float($val)) {
210 return sprintf(
'%F', $val);
211 } elseif (is_bool($val)) {
212 return ($val ?
'true' :
'false');
229 function utf82utf16($utf8)
232 if (function_exists(
'mb_convert_encoding')) {
233 return mb_convert_encoding($utf8,
'UTF-16',
'UTF-8');
236 switch (strlen($utf8)) {
245 return chr(0x07 & (ord($utf8[0]) >> 2)).chr((0xC0 & (ord($utf8[0]) << 6)) | (0x3F & ord($utf8[1])));
250 return chr((0xF0 & (ord($utf8[0]) << 4)) | (0x0F & (ord($utf8[1]) >> 2))).chr((0xC0 & (ord($utf8[1]) << 6)) | (0x7F & ord($utf8[2])));
258if (!function_exists(
'json_encode')) {
266 function json_encode($elements)
269 return dol_json_encode($elements);
274if (!function_exists(
'json_decode') || defined(
'PHPUNIT_MODE')) {
285 function dol_json_decode($json, $assoc =
false)
287 dol_syslog(
"For better performance and security, enable the native json in your PHP", LOG_WARNING);
292 $strLength = strlen($json);
294 if (is_numeric($json)) {
298 for ($i = 0; $i < $strLength; $i++) {
300 if ($i == 0 && !in_array($json[$i], array(
'{',
'[',
'"',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9'))) {
304 if (($json[$i] ==
'{') || ($json[$i] ==
'[')) {
306 } elseif (($json[$i] ==
'}') || ($json[$i] ==
']')) {
308 } elseif ($json[$i] ==
':') {
317 if ($i >= 1 && $json[$i] ==
'"' && $json[$i - 1] !=
"\\") {
318 $comment = !$comment;
330 eval(
'$array = '.$out.
';');
338 if (!empty($array)) {
340 if (count($array) > 0) {
343 foreach ($array as $key => $value) {
364 function _unval($val)
367 while (preg_match(
'/\\\u([0-9A-F]{2})([0-9A-F]{2})/i', $val, $reg)) {
369 $utf16 = chr(hexdec($reg[1])).chr(hexdec($reg[2]));
370 $utf8 = utf162utf8($utf16);
371 $val = preg_replace(
'/\\\u'.$reg[1].$reg[2].
'/i', $utf8, $val);
386 function utf162utf8($utf16)
389 if (function_exists(
'mb_convert_encoding')) {
390 return mb_convert_encoding($utf16,
'UTF-8',
'UTF-16');
393 $bytes = (ord($utf16[0]) << 8) | ord($utf16[1]);
396 case ((0x7F & $bytes) == $bytes):
401 case (0x07FF & $bytes) == $bytes:
404 return chr(0xC0 | (($bytes >> 6) & 0x1F))
405 . chr(0x80 | ($bytes & 0x3F));
407 case (0xFFFF & $bytes) == $bytes:
410 return chr(0xE0 | (($bytes >> 12) & 0x0F))
411 . chr(0x80 | (($bytes >> 6) & 0x3F))
412 . chr(0x80 | ($bytes & 0x3F));
420if (!function_exists(
'json_decode')) {
429 function json_decode($json, $assoc =
false)
432 return dol_json_decode($json, $assoc);
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.