-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrester_verify.class.php
454 lines (406 loc) · 12.1 KB
/
rester_verify.class.php
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
/**
* Class rester_verify
*/
class rester_verify
{
const TYPE = 'type';
const TYPE_REGEX = 'regexp';
const TYPE_FUNCTION = 'function';
const TYPE_FILTER = 'filter';
const TYPE_TOKEN = 'token';
const OPTIONS = 'options';
const REGEXP = 'regexp';
const DEFAULT = 'default';
const REQUIRE = 'require';
const TOKEN = 'token';
const SECRET = 'secret';
/**
* @var array
*/
protected $filter = [
self::TOKEN=>[
self::TYPE=>self::TYPE_TOKEN
],
self::SECRET=>[
self::TYPE=>self::TYPE_TOKEN
],
];
/**
* @var array
*/
protected $result;
/**
* rester_verify constructor.
*
* @param string $path
* @param bool|string $path_user_func
*
* @throws Exception
*/
public function __construct($path, $path_user_func=false)
{
$this->result = [];
// 사용자 함수 있으면 추가
if($path_user_func) include $path_user_func;
// init
if(is_file($path))
{
$cfg = parse_ini_file($path,true, INI_SCANNER_RAW);
foreach($cfg as $k=>$v)
{
foreach($v as $kk=>$vv)
{
$this->filter[$k][$kk] = $vv;
}
}
}
// filter 검증
foreach ($this->filter as $k=>$v)
{
// 필드타입에 따라 옵션으로 필수로 받는 내용이 달라진다.
switch ($v[self::TYPE])
{
case self::TYPE_REGEX:
if(!isset($v[self::TYPE_REGEX]))
throw new Exception("Required parameter.[regexp]", rester_response::code_param_filter);
break;
case self::TYPE_FILTER:
if(!isset($v[self::TYPE_FILTER]))
throw new Exception("Required parameter.[filter]", rester_response::code_param_filter);
break;
case self::TYPE_FUNCTION: break;
default:
$func = 'validate_' . $v['type'];
if (!method_exists($this, $func))
throw new Exception("Not supported type. ({$v['type']})", rester_response::code_param_filter);
}
}
}
/**
*
* @param null|string $key
* @return bool|mixed
*/
public function param($key=null)
{
if(isset($this->result[$key])) return $this->result[$key];
if($key == null) return $this->result;
return false;
}
/**
* verify request parameter
*
* @param array $data
*
* @return array
* @throws Exception
*/
public function validate($data)
{
// reset result
$this->result = [];
// check param
if(!is_array($data)) return [];
// 연관배열 검사
if(sizeof($data)>0)
{
$keys = array_keys($data);
if(!is_array($data) || (array_keys($keys) === $keys))
throw new Exception("Invalid parameter.(associative array)", rester_response::code_parameter);
}
foreach($this->filter as $k=>$v)
{
$schema = $v;
// 기본값
$result = false;
if(isset($v[self::DEFAULT])) $result = $v[self::DEFAULT];
$type = $v[self::TYPE];
if($data[$k]!==null)
{
switch ($type)
{
// Using Regular Expressions : preg_match
case self::TYPE_REGEX:
if (preg_match($schema[self::REGEXP], $data[$k], $matches))
$result = $matches[0];
break;
// php validate function
// filter_val
case self::TYPE_FILTER:
$filter = null;
$options = null;
eval("\$filter = " . $schema[self::TYPE_FILTER] . ";");
if($schema[self::OPTIONS]) eval("\$options = " . $schema[self::OPTIONS] . ";");
if(!is_integer($filter))
throw new Exception($k.'='.$data[$k]." : Invalid filter format.", rester_response::code_param_filter);
if($options !== null && !is_integer($options))
throw new Exception($k.'='.$data[$k]." : Filter option format is invalid.", rester_response::code_param_filter);
if (false !== ($clean = filter_var($data[$k], $filter, $options))) $result = $clean;
break;
// User Define Function
// 사용자 정의 함수는 호출 가능할 때만 실행
case self::TYPE_FUNCTION:
$func = $k;
if (is_callable($func))
{
$result = $func($data[$k]);
}
break;
// rester define function
// 필터 오류시 warning 으로
default:
$func = 'validate_' . $schema[self::TYPE];
if (method_exists($this, $func))
{
try
{
$result = $this->$func($data[$k]);
}
catch(Exception $e)
{
rester_response::warning($e->getMessage());
}
}
else throw new Exception($k.'='.$data[$k]." : There is no Rester definition function.", rester_response::code_param_filter);
}
}
// 필수입력 체크
$require = $v[self::REQUIRE]=='true'?true:false;
if($require && $result===false)
{
throw new Exception($k." : The required input data does not have a value or pass validation.", rester_response::code_param_data);
}
if($v[self::OPTIONS] == 'like-search') $result = '%'.$result.'%';
else if($v[self::OPTIONS] == 'like-search-left') $result = '%'.$result;
else if($v[self::OPTIONS] == 'like-search-right') $result = $result.'%';
$this->result[$k] = $result;
}
return $this->result;
}
// get verified param
/**
* @param string $data
*
* @return string
* @throws Exception
*/
protected function validate_id($data)
{
if(preg_match('/^[a-zA-Z][a-zA-Z0-9_\-:.]*$/', $data, $matches)) return $data;
throw new Exception("Invalid data(id) : {$data} (a-z, A-z, 0-9, -, _, :, .)");
}
/**
* @param string $data
*
* @return string
* @throws Exception
*/
protected function validate_ip($data)
{
$result = filter_var($data,FILTER_VALIDATE_IP);
if($result) return $result;
throw new Exception("Invalid data(ip) : {$data} ");
}
/**
* @param $data
*
* @return mixed
* @throws Exception
*/
protected function validate_bool($data)
{
if(is_bool($data) || $data==0 || $data==1) return $data;
throw new Exception("Invalid data(bool) : {$data}");
}
/**
* @param $data
*
* @return mixed
* @throws Exception
*/
protected function validate_boolean($data)
{
return $this->validate_bool($data);
}
/**
* 날짜 형식 채크
*
* @param string $data
*
* @return bool|string
* @throws Exception
*/
protected function validate_datetime($data)
{
$parsed = date_parse($data);
if($parsed['error_count']===0) return $data;
throw new Exception("Invalid data(datetime) : {$data}");
}
/**
* 날짜 형식 체크
*
* @param string $data
*
* @return string
* @throws Exception
*/
protected function validate_date($data)
{
$parsed = date_parse($data);
if(
$parsed['error_count']===0 &&
$parsed['year']!==false && $parsed['month']!==false && $parsed['day']!==false &&
$parsed['hour']===false && $parsed['minute']===false && $parsed['second']===false
)
return $data;
throw new Exception("Invalid data(date) : {$data}");
}
/**
* 시간형식 체크
*
* @param string $data
*
* @return string
* @throws Exception
*/
protected function validate_time($data)
{
$parsed = date_parse($data);
if(
$parsed['error_count']===0 &&
$parsed['year']===false && $parsed['month']===false && $parsed['day']===false &&
$parsed['hour']!==false && $parsed['minute']!==false && $parsed['second']!==false
)
return $data;
throw new Exception("Invalid data(time) : {$data}");
}
/**
* @param array $data
*
* @return array
* @throws Exception
*/
protected function validate_array($data)
{
if(is_array($data)) return $data;
throw new Exception("Invalid data(array) : {$data}");
}
/**
* 파일명 검증
* 파일명에 쓸 수 없는 9가지 문자가 있으면 안됨
* \ / : * ? " < > |
*
* @param string $data
*
* @return null|string|string[]
* @throws Exception
*/
protected function validate_filename($data)
{
if(preg_match('/[\\/:\*\?\"<>\|]/', $data, $matches)) throw new Exception("Invalid data(filename) : {$data}");
return $data;
}
/**
* @param $data
*
* @return string
* @throws Exception
*/
protected function validate_token($data)
{
if(preg_match('/^[0-9a-zA-Z.!@#$%^&()-_*=+]+$/', $data, $matches)) return $data;
throw new Exception("Invalid data(token) : {$data}");
}
/**
* @param $data
*
* @return string
* @throws Exception
*/
protected function validate_module($data)
{
if(preg_match('/^[a-zA-Z][0-9a-zA-Z_-]*$/', $data, $matches)) return $data;
throw new Exception("Invalid data(module name) : {$data}");
}
/**
* @param $data
*
* @return int
* @throws Exception
*/
protected function validate_key($data)
{
if(preg_match('/^[1-9][0-9]*$/', $data, $matches)) return intval($data);
throw new Exception("Invalid data(key) : {$data}");
}
/**
* @param $data
*
* @return int
* @throws Exception
*/
protected function validate_number($data)
{
if(preg_match('/^[0-9]+$/', $data, $matches)) return intval($data);
throw new Exception("Invalid data(number) : {$data}");
}
/**
* @param $data
*
* @return string
* @throws Exception
*/
protected function validate_mime($data)
{
if(preg_match('/^[0-9a-zA-z\/\.\-\_]+$/', $data, $matches)) return $data;
throw new Exception("Invalid data(mime) : {$data}");
}
/**
* @param $data
*
* @return string
*/
protected function validate_string($data)
{
return filter_var($data,FILTER_SANITIZE_STRING);
}
/**
* @param $data
*
* @return string
* @throws Exception
*/
protected function validate_json($data)
{
if(@json_decode(stripslashes($data),true)) return $data;
else throw new Exception("Invalid data(json) : {$data}");
}
/**
* @param $data
*
* @return string
*/
protected function validate_url($data)
{
return filter_var($data,FILTER_VALIDATE_URL);
}
/**
* @param $data
*
* @return string
*/
protected function validate_email($data)
{
return filter_var($data,FILTER_VALIDATE_EMAIL);
}
/**
* @param $data
*
* @return string
*/
protected function validate_html($data)
{
return $data;
}
}