1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ntb\RestAPI; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Some generic validators for incoming data. |
7
|
|
|
* @author Christian Blank <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
class RestValidatorHelper { |
10
|
|
|
const DefaultMaxLength = 1600; |
11
|
|
|
const PHP_INT_MIN = -2147483648; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param array $data the data from the request |
15
|
|
|
* @param string $field the field, that should be checked |
16
|
|
|
* @param array $options |
17
|
|
|
* @return string |
|
|
|
|
18
|
|
|
* @throws \ValidationException |
19
|
|
|
*/ |
20
|
|
|
public static function validate_string($data, $field, $options=[]) { |
21
|
|
|
$options = array_merge([ |
22
|
|
|
'required' => true, |
23
|
|
|
'min' => 0, |
24
|
|
|
'max' => self::DefaultMaxLength |
25
|
|
|
], $options); |
26
|
|
|
$required = $options['required']; |
27
|
|
|
$minLength = $options['min']; |
28
|
|
|
$maxLength = $options['max']; |
29
|
|
|
if(isset($data[$field]) && is_string($data[$field])) { |
30
|
|
|
$string = $data[$field]; |
31
|
|
|
// TODO: maybe the converting should not be made in validator |
32
|
|
|
$string = \Convert::raw2sql(trim($string)); |
33
|
|
|
$length = strlen($string); |
34
|
|
|
if($length > $maxLength) { |
35
|
|
|
throw new \ValidationException("Given $field is to long"); |
36
|
|
|
} else if($length < $minLength) { |
37
|
|
|
throw new \ValidationException("Given $field is to short"); |
38
|
|
|
} |
39
|
|
|
return $string; |
40
|
|
|
} else if($required) { |
41
|
|
|
throw new \ValidationException("No $field given, but $field is required"); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param mixed $data |
47
|
|
|
* @param string $field |
48
|
|
|
* @param array $options |
49
|
|
|
* @return int |
|
|
|
|
50
|
|
|
* @throws \ValidationException |
51
|
|
|
*/ |
52
|
|
|
public static function validate_int($data, $field, $options=[]) { |
53
|
|
|
$options = array_merge([ |
54
|
|
|
'required' => true, |
55
|
|
|
'min' => self::PHP_INT_MIN, |
56
|
|
|
'max' => PHP_INT_MAX |
57
|
|
|
], $options); |
58
|
|
|
$required = $options['required']; |
59
|
|
|
$min = $options['min']; |
60
|
|
|
$max = $options['max']; |
61
|
|
|
if(isset($data[$field]) && is_numeric($data[$field])) { |
62
|
|
|
$int = (int) $data[$field]; |
63
|
|
|
if($int >= $min && $int <= $max) { |
64
|
|
|
return $int; |
65
|
|
|
} else { |
66
|
|
|
throw new \ValidationException("Given integer '$int' are not in range"); |
67
|
|
|
} |
68
|
|
|
} else if($required) { |
69
|
|
|
throw new \ValidationException("No $field given, but $field is required"); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param mixed $data |
75
|
|
|
* @param string $field |
76
|
|
|
* @param array $options |
77
|
|
|
* @return string |
|
|
|
|
78
|
|
|
* @throws \ValidationException |
79
|
|
|
*/ |
80
|
|
|
public static function validate_datetime($data, $field, $options=[]) { |
81
|
|
|
$options = array_merge([ |
82
|
|
|
'required' => true |
83
|
|
|
], $options); |
84
|
|
|
$required = $options['required']; |
85
|
|
|
if(isset($data[$field]) && is_string($data[$field])) { |
86
|
|
|
$date = $data[$field]; |
87
|
|
|
$dateTime = new \SS_Datetime(); |
88
|
|
|
$dateTime->setValue($date); |
89
|
|
|
if(!$dateTime->getValue()) { |
90
|
|
|
throw new \ValidationException("No valid datetime given."); |
91
|
|
|
} |
92
|
|
|
return $dateTime->Format('Y-m-d H:i:s'); |
93
|
|
|
} else if($required) { |
94
|
|
|
throw new \ValidationException("No $field given, but $field is required"); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param mixed $data |
100
|
|
|
* @param string $field |
101
|
|
|
* @param array $options |
102
|
|
|
* @return string |
103
|
|
|
* @throws \ValidationException |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
public static function validate_url($data, $field, $options=[]) { |
|
|
|
|
106
|
|
|
$options = array_merge([ |
107
|
|
|
'required' => true |
108
|
|
|
], $options); |
109
|
|
|
$required = $options['required']; |
110
|
|
|
if(isset($data[$field]) && is_string($data[$field])) { |
111
|
|
|
$url = $data[$field]; |
112
|
|
|
if(!self::is_url($url)) { |
113
|
|
|
throw new \ValidationException("No valid url given"); |
114
|
|
|
} |
115
|
|
|
return $url; |
116
|
|
|
} else if($required) { |
117
|
|
|
throw new \ValidationException("No $field given, but $field is required"); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Validates an URL (defined in RFC 3986). |
123
|
|
|
* |
124
|
|
|
* @param string $url the url, that should be validated |
125
|
|
|
* @return boolean |
126
|
|
|
*/ |
127
|
|
|
public static function is_url($url) { |
128
|
|
|
/** |
129
|
|
|
* @author https://gist.github.com/dperini/729294 |
130
|
|
|
*/ |
131
|
|
|
$regex = '_^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]-*)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]-*)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?$_iuS'; |
132
|
|
|
return preg_match($regex, $url) === 1; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param mixed $data |
137
|
|
|
* @param string $field |
138
|
|
|
* @param array $options |
139
|
|
|
* @return string |
140
|
|
|
* @throws \ValidationException |
141
|
|
|
*/ |
142
|
|
|
public static function validate_country_code($data, $field, $options=[]) { |
143
|
|
|
$options = array_merge([ |
144
|
|
|
'required' => true |
145
|
|
|
], $options); |
146
|
|
|
$required = $options['required']; |
147
|
|
|
if(isset($data[$field]) && is_string($data[$field])) { |
148
|
|
|
$code = $data[$field]; |
149
|
|
|
$countries = \Zend_Locale::getTranslationList('territory', \i18n::get_locale(), 2); |
150
|
|
|
if(!array_key_exists(strtoupper($code), $countries)) { |
151
|
|
|
throw new \ValidationException("No valid country code given"); |
152
|
|
|
} |
153
|
|
|
return $code; |
154
|
|
|
} else if($required) { |
155
|
|
|
throw new \ValidationException("No $field given, but $field is required"); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param mixed $data |
161
|
|
|
* @param string $field |
162
|
|
|
* @param array $options |
163
|
|
|
* @return string |
164
|
|
|
* @throws \ValidationException |
165
|
|
|
*/ |
166
|
|
View Code Duplication |
public static function validate_email($data, $field, $options=[]) { |
|
|
|
|
167
|
|
|
$options = array_merge([ |
168
|
|
|
'required' => true |
169
|
|
|
], $options); |
170
|
|
|
$required = $options['required']; |
171
|
|
|
if(isset($data[$field]) && is_string($data[$field])) { |
172
|
|
|
$email = $data[$field]; |
173
|
|
|
if(\Email::is_valid_address($email) === 0) { |
174
|
|
|
throw new \ValidationException("No valid email given"); |
175
|
|
|
} |
176
|
|
|
return $email; |
177
|
|
|
} else if($required) { |
178
|
|
|
throw new \ValidationException("No $field given, but $field is required"); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.