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 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2019 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
class CNewValidator {
private $rules;
private $input = [];
private $output = [];
private $errors = [];
private $errorsFatal = [];
/**
* Parser for validation rules.
*
* @var CValidationRule
*/
private $validationRuleParser;
/**
* Parser for range date/time.
*
* @var CRangeTimeParser
*/
private $range_time_parser;
public function __construct(array $input, array $rules) {
$this->input = $input;
$this->rules = $rules;
$this->validationRuleParser = new CValidationRule();
$this->range_time_parser = null;
$this->validate();
}
/**
* Returns true if the given $value is valid, or set's an error and returns false otherwise.
*/
private function validate() {
foreach ($this->rules as $field => $rule) {
$result = $this->validateField($field, $rule);
if (array_key_exists($field, $this->input)) {
$this->output[$field] = $this->input[$field];
}
}
}
private function validateField($field, $rules) {
if (false === ($rules = $this->validationRuleParser->parse($rules))) {
$this->addError(true, $this->validationRuleParser->getError());
return false;
}
$fatal = array_key_exists('fatal', $rules);
$flags = array_key_exists('flags', $rules) ? $rules['flags'] : 0x00;
foreach ($rules as $rule => $params) {
switch ($rule) {
/*
* 'fatal' => true
*/
case 'fatal':
// nothing to do
break;
/*
* 'not_empty' => true
*/
case 'not_empty':
if (array_key_exists($field, $this->input) && $this->input[$field] === '') {
$this->addError($fatal,
_s('Incorrect value for field "%1$s": %2$s.', $field, _('cannot be empty'))
);
return false;
}
break;
case 'json':
if (array_key_exists($field, $this->input)) {
if (!is_string($this->input[$field]) || CJs::decodeJson($this->input[$field]) === null) {
$this->addError($fatal,
_s('Incorrect value for field "%1$s": %2$s.', $field, _('JSON string is expected'))
);
return false;
}
}
break;
/*
* 'in' => array(<values>)
*/
case 'in':
if (array_key_exists($field, $this->input)) {
if (!is_string($this->input[$field]) || !in_array($this->input[$field], $params)) {
$this->addError($fatal,
is_scalar($this->input[$field])
? _s('Incorrect value "%1$s" for "%2$s" field.', $this->input[$field], $field)
: _s('Incorrect value for "%1$s" field.', $field)
);
return false;
}
}
break;
case 'int32':
if (array_key_exists($field, $this->input)) {
if (!is_string($this->input[$field]) || !$this->is_int32($this->input[$field])) {
$this->addError($fatal,
is_scalar($this->input[$field])
? _s('Incorrect value "%1$s" for "%2$s" field.', $this->input[$field], $field)
: _s('Incorrect value for "%1$s" field.', $field)
);
return false;
}
}
break;
case 'id':
if (array_key_exists($field, $this->input)) {
if (!is_string($this->input[$field]) || !$this->is_id($this->input[$field])) {
$this->addError($fatal,
is_scalar($this->input[$field])
? _s('Incorrect value "%1$s" for "%2$s" field.', $this->input[$field], $field)
: _s('Incorrect value for "%1$s" field.', $field)
);
return false;
}
}
break;
/*
* 'array_id' => true
*/
case 'array_id':
if (array_key_exists($field, $this->input)) {
if (!is_array($this->input[$field]) || !$this->is_array_id($this->input[$field])) {
$this->addError($fatal,
is_scalar($this->input[$field])
? _s('Incorrect value "%1$s" for "%2$s" field.', $this->input[$field], $field)
: _s('Incorrect value for "%1$s" field.', $field)
);
return false;
}
}
break;
/*
* 'array' => true
*/
case 'array':
if (array_key_exists($field, $this->input) && !is_array($this->input[$field])) {
$this->addError($fatal,
_s('Incorrect value "%1$s" for "%2$s" field.', $this->input[$field], $field)
);
return false;
}
break;
/*
* 'array_db' => array(
* 'table' => <table_name>,
* 'field' => <field_name>
* )
*/
case 'array_db':
if (array_key_exists($field, $this->input)) {
if (!$this->is_array_db($this->input[$field], $params['table'], $params['field'], $flags)
|| !is_array($this->input[$field])) {
$this->addError($fatal,
is_scalar($this->input[$field])
? _s('Incorrect value "%1$s" for "%2$s" field.', $this->input[$field], $field)
: _s('Incorrect value for "%1$s" field.', $field)
);
return false;
}
}
break;
/*
* 'ge' => <value>
*/
case 'ge':
if (array_key_exists($field, $this->input)) {
if (!is_string($this->input[$field]) || !$this->is_int32($this->input[$field])
|| $this->input[$field] < $params) {
$this->addError($fatal,
_s('Incorrect value "%1$s" for "%2$s" field.', $this->input[$field], $field)
);
return false;
}
}
break;
/*
* 'le' => <value>
*/
case 'le':
if (array_key_exists($field, $this->input)) {
if (!is_string($this->input[$field]) || !$this->is_int32($this->input[$field])
|| $this->input[$field] > $params) {
$this->addError($fatal,
_s('Incorrect value "%1$s" for "%2$s" field.', $this->input[$field], $field)
);
return false;
}
}
break;
/*
* 'db' => array(
* 'table' => <table_name>,
* 'field' => <field_name>
* )
*/
case 'db':
if (array_key_exists($field, $this->input)) {
if (!$this->is_db($this->input[$field], $params['table'], $params['field'], $flags)) {
$this->addError($fatal,
is_scalar($this->input[$field])
? _s('Incorrect value "%1$s" for "%2$s" field.', $this->input[$field], $field)
: _s('Incorrect value for "%1$s" field.', $field)
);
return false;
}
}
break;
/*
* 'required' => true
*/
case 'required':
if (!array_key_exists($field, $this->input)) {
$this->addError($fatal, _s('Field "%1$s" is mandatory.', $field));
return false;
}
break;
/*
* 'range_time' => true
*/
case 'range_time':
if (array_key_exists($field, $this->input) && !$this->isRangeTime($this->input[$field])) {
$this->addError($fatal,
_s('Incorrect value for field "%1$s": %2$s.', $field, _('a time is expected'))
);
return false;
}
break;
/*
* 'string' => true
*/
case 'string':
if (array_key_exists($field, $this->input) && !is_string($this->input[$field])) {
$this->addError($fatal,
_s('Incorrect value for field "%1$s": %2$s.', $field, _('a character string is expected'))
);
return false;
}
break;
/*
* 'flags' => <value1> | <value2> | ... | <valueN>
*/
case 'flags':
break;
default:
// the message can be not translated because it is an internal error
$this->addError($fatal, 'Invalid validation rule "'.$rule.'".');
return false;
}
}
return true;
}
private function is_id($value) {
if (1 != preg_match('/^[0-9]+$/', $value)) {
return false;
}
// between 0 and _I64_MAX
return (bccomp($value, '0') >= 0 && bccomp($value, '9223372036854775807') <= 0);
}
public static function is_int32($value) {
if (1 != preg_match('/^\-?[0-9]+$/', $value)) {
return false;
}
// between INT_MIN and INT_MAX
return (bccomp($value, ZBX_MIN_INT32) >= 0 && bccomp($value, ZBX_MAX_INT32) <= 0);
}
public static function is_uint64($value) {
if (1 != preg_match('/^[0-9]+$/', $value)) {
return false;
}
// between 0 and _UI64_MAX
return (bccomp($value, '0') >= 0 && bccomp($value, '18446744073709551615') <= 0);
}
/**
* Validate value against DB schema.
*
* @param array $field_schema Array of DB schema.
* @param string $field_schema['type'] Type of DB field.
* @param string $field_schema['length'] Length of DB field.
* @param string $value [IN/OUT] IN - input value, OUT - changed value according to flags.
* @param int $flags Validation flags.
*
* @return bool
*/
private function check_db_value($field_schema, &$value, $flags) {
switch ($field_schema['type']) {
case DB::FIELD_TYPE_ID:
return $this->is_id($value);
case DB::FIELD_TYPE_INT:
return $this->is_int32($value);
case DB::FIELD_TYPE_CHAR:
if ($flags & P_CRLF) {
$value = CRLFtoLF($value);
}
return (mb_strlen($value) <= $field_schema['length']);
case DB::FIELD_TYPE_TEXT:
if ($flags & P_CRLF) {
$value = CRLFtoLF($value);
}
// TODO: check length
return true;
default:
return false;
}
}
private function is_array_id(array $values) {
foreach ($values as $value) {
if (!is_string($value) || !$this->is_id($value)) {
return false;
}
}
return true;
}
/**
* Validate array of string values against DB schema.
*
* @param array $values [IN/OUT] IN - input values, OUT - changed values according to flags.
* @param string $table DB table name.
* @param string $field DB field name.
* @param int $flags Validation flags.
*
* @return bool
*/
private function is_array_db(array &$values, $table, $field, $flags) {
$table_schema = DB::getSchema($table);
foreach ($values as &$value) {
if (!is_string($value) || !$this->check_db_value($table_schema['fields'][$field], $value, $flags)) {
return false;
}
}
unset($value);
return true;
}
/**
* Validate a string value against DB schema.
*
* @param string $value [IN/OUT] IN - input value, OUT - changed value according to flags.
* @param string $table DB table name.
* @param string $field DB field name.
* @param int $flags Validation flags.
*
* @return bool
*/
private function is_db(&$value, $table, $field, $flags) {
$table_schema = DB::getSchema($table);
return (is_string($value) && $this->check_db_value($table_schema['fields'][$field], $value, $flags));
}
private function isLeapYear($year) {
return (0 == $year % 4 && (0 != $year % 100 || 0 == $year % 400));
}
private function getDaysInMonth($year, $month) {
if (in_array($month, [4, 6, 9, 11], true)) {
return 30;
}
if ($month == 2) {
return $this->isLeapYear($year) ? 29 : 28;
}
return 31;
}
private function isRangeTime($value) {
if ($this->range_time_parser === null) {
$this->range_time_parser = new CRangeTimeParser();
}
return is_string($value) && $this->range_time_parser->parse($value) == CParser::PARSE_SUCCESS;
}
/**
* Add validation error.
*
* @return string
*/
public function addError($fatal, $error) {
if ($fatal) {
$this->errorsFatal[] = $error;
}
else {
$this->errors[] = $error;
}
}
/**
* Get valid fields.
*
* @return array of fields passed validation
*/
public function getValidInput() {
return $this->output;
}
/**
* Returns array of error messages.
*
* @return array
*/
public function getAllErrors() {
return array_merge($this->errorsFatal, $this->errors);
}
/**
* Returns true if validation failed with errors.
*
* @return bool
*/
public function isError() {
return (bool) $this->errors;
}
/**
* Returns true if validation failed with fatal errors.
*
* @return bool
*/
public function isErrorFatal() {
return (bool) $this->errorsFatal;
}
}
|