1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\validators; |
4
|
|
|
|
5
|
|
|
use EE_Error; |
6
|
|
|
|
7
|
|
|
// phpcs:disable PHPCompatibility.PHP.NewConstants.json_error_invalid_property_nameFound |
8
|
|
|
// phpcs:disable PHPCompatibility.PHP.NewConstants.json_error_utf16Found |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class JsonValidator |
12
|
|
|
* Tools for validating JSON data and handling errors |
13
|
|
|
* |
14
|
|
|
* @package EventEspresso\core\services\validators |
15
|
|
|
* @author Brent Christensen |
16
|
|
|
* @since 4.9.70.p |
17
|
|
|
*/ |
18
|
|
|
class JsonValidator |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Call this method IMMEDIATELY after json_decode() and |
23
|
|
|
* it will will return true if the decoded JSON was valid, |
24
|
|
|
* or return false after adding an error if not valid. |
25
|
|
|
* The actual JSON file does not need to be supplied, |
26
|
|
|
* but details re: code execution location are required. |
27
|
|
|
* ex: |
28
|
|
|
* JsonValidator::isValid(__FILE__, __METHOD__, __LINE__) |
29
|
|
|
* |
30
|
|
|
* @param string $file |
31
|
|
|
* @param string $func |
32
|
|
|
* @param string $line |
33
|
|
|
* @return boolean |
34
|
|
|
* @since 4.9.70.p |
35
|
|
|
*/ |
36
|
|
|
public function isValid($file, $func, $line) |
37
|
|
|
{ |
38
|
|
|
if (! defined('JSON_ERROR_RECURSION')) { |
39
|
|
|
define('JSON_ERROR_RECURSION', 6); |
40
|
|
|
} |
41
|
|
|
if (! defined('JSON_ERROR_INF_OR_NAN')) { |
42
|
|
|
define('JSON_ERROR_INF_OR_NAN', 7); |
43
|
|
|
} |
44
|
|
|
if (! defined('JSON_ERROR_UNSUPPORTED_TYPE')) { |
45
|
|
|
define('JSON_ERROR_UNSUPPORTED_TYPE', 8); |
46
|
|
|
} |
47
|
|
|
if (! defined('JSON_ERROR_INVALID_PROPERTY_NAME')) { |
48
|
|
|
define('JSON_ERROR_INVALID_PROPERTY_NAME', 9); |
49
|
|
|
} |
50
|
|
|
if (! defined('JSON_ERROR_UTF16')) { |
51
|
|
|
define('JSON_ERROR_UTF16', 10); |
52
|
|
|
} |
53
|
|
|
switch (json_last_error()) { |
54
|
|
|
case JSON_ERROR_NONE: |
55
|
|
|
return true; |
56
|
|
|
case JSON_ERROR_DEPTH: |
57
|
|
|
$error = ': Maximum stack depth exceeded'; |
58
|
|
|
break; |
59
|
|
|
case JSON_ERROR_STATE_MISMATCH: |
60
|
|
|
$error = ': Invalid or malformed JSON'; |
61
|
|
|
break; |
62
|
|
|
case JSON_ERROR_CTRL_CHAR: |
63
|
|
|
$error = ': Control character error, possible malformed JSON'; |
64
|
|
|
break; |
65
|
|
|
case JSON_ERROR_SYNTAX: |
66
|
|
|
$error = ': Syntax error, malformed JSON'; |
67
|
|
|
break; |
68
|
|
|
case JSON_ERROR_UTF8: |
69
|
|
|
$error = ': Malformed UTF-8 characters, possible malformed JSON'; |
70
|
|
|
break; |
71
|
|
|
case JSON_ERROR_RECURSION: |
72
|
|
|
$error = ': One or more recursive references in the value to be encoded'; |
73
|
|
|
break; |
74
|
|
|
case JSON_ERROR_INF_OR_NAN: |
75
|
|
|
$error = ': One or more NAN or INF values in the value to be encoded'; |
76
|
|
|
break; |
77
|
|
|
case JSON_ERROR_UNSUPPORTED_TYPE: |
78
|
|
|
$error = ': A value of a type that cannot be encoded was given'; |
79
|
|
|
break; |
80
|
|
|
case JSON_ERROR_INVALID_PROPERTY_NAME: |
81
|
|
|
$error = ': A property name that cannot be encoded was given'; |
82
|
|
|
break; |
83
|
|
|
case JSON_ERROR_UTF16: |
84
|
|
|
$error = ': Malformed UTF-16 characters, possibly incorrectly encoded'; |
85
|
|
|
break; |
86
|
|
|
default: |
87
|
|
|
$error = ': Unknown error'; |
88
|
|
|
break; |
89
|
|
|
} |
90
|
|
|
EE_Error::add_error('JSON decoding failed' . $error, $file, $func, $line); |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|