Sfoglia il codice sorgente

Added new safe json encode

causefx 7 anni fa
parent
commit
3ce6cff515
2 ha cambiato i file con 23 aggiunte e 2 eliminazioni
  1. 21 0
      api/functions/normal-functions.php
  2. 2 2
      api/index.php

+ 21 - 0
api/functions/normal-functions.php

@@ -538,4 +538,25 @@ function file_get_contents_curl($url)
 function getExtension($string)
 {
 	return preg_replace("#(.+)?\.(\w+)(\?.+)?#", "$2", $string);
+}
+
+function safe_json_encode($value, $options = 0, $depth = 512)
+{
+	$encoded = json_encode($value, $options, $depth);
+	if ($encoded === false && $value && json_last_error() == JSON_ERROR_UTF8) {
+		$encoded = json_encode(utf8ize($value), $options, $depth);
+	}
+	return $encoded;
+}
+
+function utf8ize($mixed)
+{
+	if (is_array($mixed)) {
+		foreach ($mixed as $key => $value) {
+			$mixed[$key] = utf8ize($value);
+		}
+	} elseif (is_string($mixed)) {
+		return mb_convert_encoding($mixed, "UTF-8", "UTF-8");
+	}
+	return $mixed;
 }

+ 2 - 2
api/index.php

@@ -1290,8 +1290,8 @@ $generationTime += microtime(true);
 $result['generationTime'] = (sprintf('%f', $generationTime) * 1000) . 'ms';
 //return JSON array
 if ($pretty) {
-	echo '<pre>' . json_encode($result, JSON_PRETTY_PRINT) . '</pre>';
+	echo '<pre>' . safe_json_encode($result, JSON_PRETTY_PRINT) . '</pre>';
 } else {
-	exit(json_encode($result, JSON_HEX_QUOT | JSON_HEX_TAG));
+	exit(safe_json_encode($result, JSON_HEX_QUOT | JSON_HEX_TAG));
 }