opencart_serbian /system/helper/json.php

Language PHP Lines 135
MD5 Hash fa1e4fee2619c32eea0d802504174782 Estimated Cost $2,231 (why?)
Repository https://bitbucket.org/jjasko/opencart_serbian View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
if (!function_exists('json_encode')) {
	function json_encode($data) {
		switch (gettype($data)) {
			case 'boolean':
				return $data ? 'true' : 'false';
			case 'integer':
			case 'double':
				return $data;
			case 'resource':
			case 'string':
				# Escape non-printable or Non-ASCII characters. 
				# I also put the \\ character first, as suggested in comments on the 'addclashes' page. 
				$json = ''; 
				
				$string = '"' . addcslashes($data, "\\\"\n\r\t/" . chr(8) . chr(12)) . '"'; 
				
				# Convert UTF-8 to Hexadecimal Codepoints. 
				for ($i = 0; $i < strlen($string); $i++) { 
					$char = $string[$i]; 
					$c1 = ord($char); 
					
					# Single byte; 
					if ($c1 < 128) { 
						$json .= ($c1 > 31) ? $char : sprintf("\\u%04x", $c1); 
						
						continue; 
					} 
					
					# Double byte 
					$c2 = ord($string[++$i]); 
					
					if (($c1 & 32) === 0) { 
						$json .= sprintf("\\u%04x", ($c1 - 192) * 64 + $c2 - 128); 
						
						continue;
					} 
					
					# Triple 
					$c3 = ord($string[++$i]); 
					
					if (($c1 & 16) === 0) { 
						$json .= sprintf("\\u%04x", (($c1 - 224) <<12) + (($c2 - 128) << 6) + ($c3 - 128)); 
						
						continue; 
					} 
						
					# Quadruple 
					$c4 = ord($string[++$i]); 
					
					if (($c1 & 8 ) === 0) { 
						$u = (($c1 & 15) << 2) + (($c2>>4) & 3) - 1; 
					
						$w1 = (54<<10) + ($u<<6) + (($c2 & 15) << 2) + (($c3>>4) & 3); 
						$w2 = (55<<10) + (($c3 & 15)<<6) + ($c4-128); 
						$json .= sprintf("\\u%04x\\u%04x", $w1, $w2); 
					} 
				} 				
			
				return $json;
			case 'array':
				if (empty($data) || array_keys($data) === range(0, sizeof($data) - 1)) {
					$output = array();
					
					foreach ($data as $value) {
						$output[] = json_encode($value);
					}
					
					return '[' . implode(',', $output) . ']';
				}
			case 'object':
				$output = array();
				
				foreach ($data as $key => $value) {
					$output[] = json_encode(strval($key)) . ':' . json_encode($value);
				}
				
				return '{' . implode(',', $output) . '}';
			default:
				return 'null';
		}
	}
}

if (!function_exists('json_decode')) {	
	function json_decode($json, $assoc = false) {
		$match = '/".*?(?<!\\\\)"/';

		$string = preg_replace($match, '', $json);
		$string = preg_replace('/[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/', '', $string);

		if ($string != '') {
			return null;
		}

		$s2m = array();
		$m2s = array();

		preg_match_all($match, $json, $m);
		
		foreach ($m[0] as $s) {
			$hash = '"' . md5($s) . '"';
			$s2m[$s] = $hash;
			$m2s[$hash] = str_replace('$', '\$', $s);
		}

		$json = strtr($json, $s2m);

		$a = ($assoc) ? '' : '(object) ';
		
		$data = array(
			':' => '=>', 
			'[' => 'array(', 
			'{' => "{$a}array(", 
			']' => ')', 
			'}' => ')'
		);
		
		$json = strtr($json, $data);

		$json = preg_replace('~([\s\(,>])(-?)0~', '$1$2', $json);

		$json = strtr($json, $m2s);

		$function = @create_function('', "return {$json};");
		$return = ($function) ? $function() : null;

		unset($s2m); 
		unset($m2s); 
		unset($function);

		return $return;
	}
}
?>
Back to Top