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
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2023 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.
**/
/**
* General XML validator
*/
abstract class CXmlValidatorGeneral {
public const YAML = 'yaml';
public const XML = 'xml';
public const JSON = 'json';
/**
* Format of import source.
*
* @var string
*/
protected $format;
/**
* Key validation for XML_INDEXED_ARRAY containers.
*
* @var bool
*/
protected $strict = false;
/**
* This validation is for importcompare, it should produce same output in import, as for export.
*
* @var bool
*/
protected $preview = false;
/**
* @param string $format Format of import source.
*/
public function __construct(string $format) {
$this->format = $format;
}
/**
* Get key validation for XML_INDEXED_ARRAY containers.
*
* @return bool
*/
public function getStrict(): bool {
return $this->strict;
}
/**
* Set key validation for XML_INDEXED_ARRAY containers.
*
* @param bool $strict
*
* @return self
*/
public function setStrict(bool $strict): self {
$this->strict = $strict;
return $this;
}
/**
* On import preview validation should not change content.
*
* @return bool
*/
public function isPreview(): bool {
return $this->preview;
}
/**
* On import preview validation should not change content.
*
* @param bool $preview
*
* @return self
*/
public function setPreview(bool $preview): self {
$this->preview = $preview;
return $this;
}
/**
* Validate import data.
*
* @param array|string $data Import data.
* @param string $path XML path (for error reporting).
*
* @return mixed Validator does some manipulations for the incoming data. For example, converts empty tags to an
* array, if desired. Converted data is returned.
*/
abstract public function validate(array $data, string $path);
/**
* Validate import data against the rules.
*
* @param array $rules Validation rules.
* @param array|string $data Import data.
* @param string $path XML path (for error reporting).
*
* @return mixed Validator does some manipulations for the incoming data. For example, converts empty tags to an
* array, if desired. Converted data is returned.
*/
protected function doValidate(array $rules, $data, string $path) {
$this->doValidateRecursive($rules, $data, null, $path);
return $data;
}
/**
* Validate import data recursively.
*
* @param array $rules Validation rules.
* @param array|string $data Import data.
* @param array $parent_data Data's parent array (used for "ex_validate" callback functions).
* @param string $path XML path (for error reporting).
*
* @throws Exception if $data does not correspond to validation $rules.
*/
private function doValidateRecursive(array $rules, &$data, ?array $parent_data, string $path): void {
if (array_key_exists('preprocessor', $rules)) {
$data = call_user_func($rules['preprocessor'], $data);
}
if ($rules['type'] & XML_STRING) {
$this->validateString($data, $path);
$this->validateConstant($data, $rules, $path);
}
elseif ($rules['type'] & XML_ARRAY) {
if ($data === '') {
$data = [];
}
$this->validateArray($data, $path);
// unexpected tag validation
if (!array_key_exists('check_unexpected', $rules) || $rules['check_unexpected']) {
foreach ($data as $tag => $value) {
if (!array_key_exists($tag, $rules['rules'])) {
throw new Exception(_s('Invalid tag "%1$s": %2$s.', $path,
_s('unexpected tag "%1$s"', $tag)
));
}
}
}
// validation of the values type
foreach ($rules['rules'] as $tag => $rule) {
if (array_key_exists('import', $rule) && !$this->preview) {
$data[$tag] = call_user_func($rule['import'], $data);
}
if (array_key_exists($tag, $data)) {
$subpath = ($path === '/' ? $path : $path.'/').$tag;
$this->doValidateRecursive($rule, $data[$tag], $data, $subpath);
}
elseif (($rule['type'] & XML_REQUIRED) || (array_key_exists('ex_required', $rule)
&& call_user_func($rule['ex_required'], $data))) {
throw new Exception(_s('Invalid tag "%1$s": %2$s.', $path,
_s('the tag "%1$s" is missing', $tag)
));
}
}
}
elseif ($rules['type'] & XML_INDEXED_ARRAY) {
if ($data === '') {
$data = [];
}
$this->validateArray($data, $path);
$index = 0;
$prefix = $rules['prefix'];
if (array_key_exists('extra', $rules)) {
if (!array_key_exists($rules['extra'], $data)
&& ($rules['rules'][$rules['extra']]['type'] & XML_REQUIRED)) {
throw new Exception(_s('Invalid tag "%1$s": %2$s.', $path,
_s('the tag "%1$s" is missing', $rules['extra'])
));
}
}
foreach ($data as $tag => &$value) {
if (array_key_exists('extra', $rules) && $rules['extra'] == $tag) {
$subpath = ($path === '/' ? $path : $path.'/').$tag;
$this->doValidateRecursive($rules['rules'][$tag], $value, $data, $subpath);
continue;
}
if ($this->strict) {
switch ($this->format) {
case self::XML:
$is_valid_tag = ($tag === $prefix.($index == 0 ? '' : $index) || $tag === $index);
break;
case self::YAML:
case self::JSON:
$is_valid_tag = ctype_digit(strval($tag));
break;
default:
throw new Exception(_('Internal error.'));
}
if (!$is_valid_tag) {
throw new Exception(_s('Invalid tag "%1$s": %2$s.', $path, _s('unexpected tag "%1$s"', $tag)));
}
}
$index++;
$subpath = ($path === '/' ? $path : $path.'/').$prefix.'('.$index.')';
$this->doValidateRecursive($rules['rules'][$prefix], $value, $data, $subpath);
}
unset($value);
$extra = null;
if (array_key_exists('extra', $rules)) {
if (array_key_exists($rules['extra'], $data)) {
$extra = $data[$rules['extra']];
unset($data[$rules['extra']]);
}
}
if ($extra !== null) {
$data[$rules['extra']] = $extra;
}
}
if (array_key_exists('ex_validate', $rules)) {
$data = call_user_func($rules['ex_validate'], $data, $parent_data, $path);
}
}
/**
* String validator.
*
* @param mixed $value Value for validation.
* @param string $path XML path (for error reporting).
*
* @throws Exception if this $value is not a character string.
*/
private function validateString($value, string $path): void {
if (!is_string($value)) {
throw new Exception(_s('Invalid tag "%1$s": %2$s.', $path, _('a character string is expected')));
}
}
/**
* Array validator.
*
* @param mixed $value Value for validation.
* @param string $path XML path (for error reporting).
*
* @throws Exception if this $value is not an array.
*/
private function validateArray($value, string $path): void {
if (!is_array($value)) {
throw new Exception(_s('Invalid tag "%1$s": %2$s.', $path, _('an array is expected')));
}
}
/**
* Constant validator.
*
* @param mixed $value Value for validation.
* @param array $rules XML rules.
* @param string $path XML path (for error reporting).
*
* @throws Exception if this $value is an invalid constant.
*/
private function validateConstant($value, array $rules, string $path): void {
if (array_key_exists('in', $rules) && !in_array($value, array_values($rules['in']))) {
throw new Exception(_s('Invalid tag "%1$s": %2$s.', $path, _s('unexpected constant "%1$s"', $value)));
}
}
}
|