1<?php 2/* 3** Zabbix 4** Copyright (C) 2001-2021 Zabbix SIA 5** 6** This program is free software; you can redistribute it and/or modify 7** it under the terms of the GNU General Public License as published by 8** the Free Software Foundation; either version 2 of the License, or 9** (at your option) any later version. 10** 11** This program is distributed in the hope that it will be useful, 12** but WITHOUT ANY WARRANTY; without even the implied warranty of 13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14** GNU General Public License for more details. 15** 16** You should have received a copy of the GNU General Public License 17** along with this program; if not, write to the Free Software 18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19**/ 20 21 22class CIdValidator extends CValidator { 23 24 /** 25 * Value to determine whether to allow empty values 26 * 27 * @var bool 28 */ 29 public $empty = false; 30 31 /** 32 * Error message if the id has wrong type or id is out of range or invalid 33 * 34 * @var string 35 */ 36 public $messageInvalid; 37 38 /** 39 * Error message if the id is empty 40 * 41 * @var string 42 */ 43 public $messageEmpty; 44 45 /** 46 * Validates ID value 47 * 48 * @param string $value 49 * 50 * @return bool 51 */ 52 public function validate($value) { 53 if (!is_string($value) && !is_int($value)) { 54 $this->error($this->messageInvalid, $this->stringify($value)); 55 56 return false; 57 } 58 59 if (!$this->empty && (string) $value === '0') { 60 $this->error($this->messageEmpty); 61 62 return false; 63 } 64 65 $regex = $this->empty ? '/^(0|(?!0)[0-9]+)$/' : '/^(?!0)\d+$/'; 66 67 if (!preg_match($regex, $value) || 68 bccomp($value, 0) == -1 || 69 bccomp($value, ZBX_DB_MAX_ID) == 1 70 ) { 71 $this->error($this->messageInvalid, $value); 72 73 return false; 74 } 75 76 return true; 77 } 78} 79