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 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
|
<?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 containing methods for operations with icon maps.
*
* Icon maps work only with map elements that represent hosts, and used for automatic changing
* icons based on host inventory data.
* Icon maps has read access for everyone and write access only for superamins
*/
class CIconMap extends CApiService {
protected $tableName = 'icon_map';
protected $tableAlias = 'im';
protected $sortColumns = ['iconmapid', 'name'];
/**
* Get IconMap data.
* @param array $options
* @param array $options['iconmapids']
* @param array $options['sysmapids']
* @param bool $options['editable']
* @param array $options['count']
* @param array $options['limit']
* @param array $options['order']
* @return array
*/
public function get(array $options = []) {
$result = [];
$sqlParts = [
'select' => ['icon_map' => 'im.iconmapid'],
'from' => ['icon_map' => 'icon_map im'],
'where' => [],
'order' => [],
'limit' => null
];
$defOptions = [
'iconmapids' => null,
'sysmapids' => null,
'nopermissions' => null,
'editable' => false,
// filter
'filter' => null,
'search' => null,
'searchByAny' => null,
'startSearch' => false,
'excludeSearch' => false,
'searchWildcardsEnabled' => null,
// output
'output' => API_OUTPUT_EXTEND,
'selectMappings' => null,
'countOutput' => false,
'preservekeys' => false,
'sortfield' => '',
'sortorder' => '',
'limit' => null
];
$options = zbx_array_merge($defOptions, $options);
// editable + PERMISSION CHECK
if ($options['editable'] && self::$userData['type'] != USER_TYPE_SUPER_ADMIN) {
return [];
}
// iconmapids
if (!is_null($options['iconmapids'])) {
zbx_value2array($options['iconmapids']);
$sqlParts['where'][] = dbConditionInt('im.iconmapid', $options['iconmapids']);
}
// sysmapids
if (!is_null($options['sysmapids'])) {
zbx_value2array($options['sysmapids']);
$sqlParts['from']['sysmaps'] = 'sysmaps s';
$sqlParts['where'][] = dbConditionInt('s.sysmapid', $options['sysmapids']);
$sqlParts['where']['ims'] = 'im.iconmapid=s.iconmapid';
}
// filter
if (is_array($options['filter'])) {
$this->dbFilter('icon_map im', $options, $sqlParts);
}
// search
if (is_array($options['search'])) {
zbx_db_search('icon_map im', $options, $sqlParts);
}
// limit
if (zbx_ctype_digit($options['limit']) && $options['limit']) {
$sqlParts['limit'] = $options['limit'];
}
$sqlParts = $this->applyQueryOutputOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
$sqlParts = $this->applyQuerySortOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
$dbRes = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']);
while ($iconMap = DBfetch($dbRes)) {
if ($options['countOutput']) {
$result = $iconMap['rowscount'];
}
else {
$result[$iconMap['iconmapid']] = $iconMap;
}
}
if ($options['countOutput']) {
return $result;
}
if ($result) {
$result = $this->addRelatedObjects($options, $result);
}
// removing keys (hash -> array)
if (!$options['preservekeys']) {
$result = zbx_cleanHashes($result);
}
return $result;
}
/**
* @param array $iconmaps
*
* @return array
*/
public function create(array $iconmaps) {
$this->validateCreate($iconmaps);
$ins_iconmaps = [];
foreach ($iconmaps as $iconmap) {
unset($iconmap['mappings']);
$ins_iconmaps[] = $iconmap;
}
$iconmapids = DB::insertBatch('icon_map', $ins_iconmaps);
foreach ($iconmaps as $index => &$iconmap) {
$iconmap['iconmapid'] = $iconmapids[$index];
}
unset($iconmap);
$this->updateMappings($iconmaps, __FUNCTION__);
$this->addAuditBulk(AUDIT_ACTION_ADD, AUDIT_RESOURCE_ICON_MAP, $iconmaps);
return ['iconmapids' => $iconmapids];
}
/**
* @param array $iconmaps
*
* @throws APIException if the input is invalid.
*/
private function validateCreate(array &$iconmaps) {
if (self::$userData['type'] != USER_TYPE_SUPER_ADMIN) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('You do not have permission to perform this operation.'));
}
$api_input_rules = ['type' => API_OBJECTS, 'flags' => API_NOT_EMPTY | API_NORMALIZE, 'uniq' => [['name']], 'fields' => [
'name' => ['type' => API_STRING_UTF8, 'flags' => API_NOT_EMPTY | API_REQUIRED, 'length' => DB::getFieldLength('icon_map', 'name')],
'default_iconid' => ['type' => API_ID, 'flags' => API_REQUIRED],
'mappings' => ['type' => API_OBJECTS, 'flags' => API_NOT_EMPTY | API_REQUIRED, 'uniq' => [['inventory_link', 'expression']], 'fields' => [
'iconid' => ['type' => API_ID, 'flags' => API_REQUIRED],
'expression' => ['type' => API_REGEX, 'flags' => API_NOT_EMPTY | API_REQUIRED, 'length' => DB::getFieldLength('icon_mapping', 'expression')],
'inventory_link' => ['type' => API_INT32, 'flags' => API_REQUIRED, 'in' => '1:70'],
'sortorder' => ['type' => API_INT32, 'flags' => API_DEPRECATED]
]]
]];
if (!CApiInputValidator::validate($api_input_rules, $iconmaps, '/', $error)) {
self::exception(ZBX_API_ERROR_PARAMETERS, $error);
}
$this->checkDuplicates(zbx_objectValues($iconmaps, 'name'));
$this->checkMappings($iconmaps);
$this->checkIcons($iconmaps);
}
/**
* @param array $iconmaps
*
* @return array
*/
public function update(array $iconmaps) {
$this->validateUpdate($iconmaps, $db_iconmaps);
$upd_iconmaps = [];
foreach ($iconmaps as $iconmap) {
$db_iconmap = $db_iconmaps[$iconmap['iconmapid']];
$upd_iconmap = [];
if (array_key_exists('name', $iconmap) && $iconmap['name'] !== $db_iconmap['name']) {
$upd_iconmap['name'] = $iconmap['name'];
}
if (array_key_exists('default_iconid', $iconmap)
&& bccomp($iconmap['default_iconid'], $db_iconmap['default_iconid']) != 0) {
$upd_iconmap['default_iconid'] = $iconmap['default_iconid'];
}
if ($upd_iconmap) {
$upd_iconmaps[] = [
'values' => $upd_iconmap,
'where' => ['iconmapid' => $iconmap['iconmapid']]
];
}
}
if ($upd_iconmaps) {
DB::update('icon_map', $upd_iconmaps);
}
$this->updateMappings($iconmaps, __FUNCTION__);
$this->addAuditBulk(AUDIT_ACTION_UPDATE, AUDIT_RESOURCE_ICON_MAP, $iconmaps, $db_iconmaps);
return ['iconmapids' => zbx_objectValues($iconmaps, 'iconmapid')];
}
/**
* @param array $iconmaps
*
* @throws APIException if the input is invalid.
*/
private function validateUpdate(array &$iconmaps, array &$db_iconmaps = null) {
if (self::$userData['type'] != USER_TYPE_SUPER_ADMIN) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('You do not have permission to perform this operation.'));
}
$api_input_rules = ['type' => API_OBJECTS, 'flags' => API_NOT_EMPTY | API_NORMALIZE, 'uniq' => [['iconmapid'], ['name']], 'fields' => [
'iconmapid' => ['type' => API_ID, 'flags' => API_REQUIRED],
'name' => ['type' => API_STRING_UTF8, 'flags' => API_NOT_EMPTY, 'length' => DB::getFieldLength('icon_map', 'name')],
'default_iconid' => ['type' => API_ID],
'mappings' => ['type' => API_OBJECTS, 'flags' => API_NOT_EMPTY, 'uniq' => [['inventory_link', 'expression']], 'fields' => [
'iconid' => ['type' => API_ID, 'flags' => API_REQUIRED],
'expression' => ['type' => API_REGEX, 'flags' => API_NOT_EMPTY | API_REQUIRED, 'length' => DB::getFieldLength('icon_mapping', 'expression')],
'inventory_link' => ['type' => API_INT32, 'flags' => API_REQUIRED, 'in' => '1:70'],
'sortorder' => ['type' => API_INT32, 'flags' => API_DEPRECATED]
]]
]];
if (!CApiInputValidator::validate($api_input_rules, $iconmaps, '/', $error)) {
self::exception(ZBX_API_ERROR_PARAMETERS, $error);
}
$db_iconmaps = DB::select('icon_map', [
'output' => ['iconmapid', 'name', 'default_iconid'],
'iconmapids' => zbx_objectValues($iconmaps, 'iconmapid'),
'preservekeys' => true
]);
$names = [];
foreach ($iconmaps as $iconmap) {
if (!array_key_exists($iconmap['iconmapid'], $db_iconmaps)) {
self::exception(ZBX_API_ERROR_PERMISSIONS,
_('No permissions to referred object or it does not exist!')
);
}
$db_iconmap = $db_iconmaps[$iconmap['iconmapid']];
if (array_key_exists('name', $iconmap) && $iconmap['name'] !== $db_iconmap['name']) {
$names[] = $iconmap['name'];
}
}
if ($names) {
$this->checkDuplicates($names);
}
$this->checkMappings($iconmaps);
$this->checkIcons($iconmaps);
}
/**
* Check for duplicated icon maps.
*
* @param array $names
*
* @throws APIException if user already exists.
*/
private function checkDuplicates(array $names) {
$db_iconmaps = DB::select('icon_map', [
'output' => ['name'],
'filter' => ['name' => $names],
'limit' => 1
]);
if ($db_iconmaps) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map "%s" already exists.', $db_iconmaps[0]['name']));
}
}
/**
* Check icon mappings.
*
* @param $iconmaps
*
* @throws APIException
*/
private function checkMappings($iconmaps) {
$names = [];
foreach ($iconmaps as $iconmap) {
if (array_key_exists('mappings', $iconmap)) {
foreach ($iconmap['mappings'] as $mapping) {
if ($mapping['expression'][0] === '@') {
/*
* substr() performs differently on PHP < 7. Prior to PHP 7 version, FALSE was returned if
* string is equal to start characters long. As of PHP 7, an empty string will be returned.
*/
$names[isset($mapping['expression'][1]) ? substr($mapping['expression'], 1) : ''] = true;
}
}
}
}
if ($names) {
$names = array_keys($names);
$db_regexps = DB::select('regexps', [
'output' => ['name'],
'filter' => ['name' => $names]
]);
$db_regexps = zbx_toHash($db_regexps, 'name');
foreach ($names as $name) {
if (!array_key_exists($name, $db_regexps)) {
self::exception(ZBX_API_ERROR_PARAMETERS,
_s('Global regular expression "%1$s" does not exist.', $name)
);
}
}
}
}
/**
* Check icons.
*
* @param $iconmaps
*
* @throws APIException
*/
private function checkIcons($iconmaps) {
$iconids = [];
foreach ($iconmaps as $iconmap) {
if (array_key_exists('default_iconid', $iconmap)) {
$iconids[$iconmap['default_iconid']] = true;
}
if (array_key_exists('mappings', $iconmap)) {
foreach ($iconmap['mappings'] as $mapping) {
$iconids[$mapping['iconid']] = true;
}
}
}
if ($iconids) {
$iconids = array_keys($iconids);
$db_icons = API::Image()->get([
'output' => [],
'imageids' => $iconids,
'filter' => ['imagetype' => IMAGE_TYPE_ICON],
'preservekeys' => true
]);
foreach ($iconids as $iconid) {
if (!array_key_exists($iconid, $db_icons)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon with ID "%1$s" is not available.', $iconid));
}
}
}
}
/**
* Update table "icon_mapping".
*
* @param array $iconmaps
* @param string $method
*/
private function updateMappings(array $iconmaps, $method) {
$mappings = [];
foreach ($iconmaps as $iconmap) {
if (array_key_exists('mappings', $iconmap)) {
foreach ($iconmap['mappings'] as $index => $mapping) {
$mapping['iconmapid'] = $iconmap['iconmapid'];
$mapping['sortorder'] = $index;
$mappings[$iconmap['iconmapid']][] = $mapping;
}
}
}
if (!$mappings) {
return;
}
$db_mappings = ($method === 'update')
? DB::select('icon_mapping', [
'output' => ['iconmappingid', 'iconmapid', 'iconid', 'expression', 'inventory_link', 'sortorder'],
'filter' => ['iconmapid' => array_keys($mappings)],
'sortfield' => ['iconmapid', 'sortorder']
])
: [];
$ins_mappings = [];
$upd_mappings = [];
$del_mappingids = [];
foreach ($db_mappings as $db_mapping) {
if ($mappings[$db_mapping['iconmapid']]) {
$mapping = array_shift($mappings[$db_mapping['iconmapid']]);
$upd_mapping = [];
if (bccomp($mapping['iconid'], $db_mapping['iconid']) != 0) {
$upd_mapping['iconid'] = $mapping['iconid'];
}
if ($mapping['expression'] !== $db_mapping['expression']) {
$upd_mapping['expression'] = $mapping['expression'];
}
if ($mapping['inventory_link'] != $db_mapping['inventory_link']) {
$upd_mapping['inventory_link'] = $mapping['inventory_link'];
}
if ($mapping['sortorder'] != $db_mapping['sortorder']) {
$upd_mapping['sortorder'] = $mapping['sortorder'];
}
if ($upd_mapping) {
$upd_mappings[] = [
'values' => $upd_mapping,
'where' => ['iconmappingid' => $db_mapping['iconmappingid']]
];
}
}
else {
$del_mappingids[] = $db_mapping['iconmappingid'];
}
}
foreach ($iconmaps as $iconmap) {
$ins_mappings = array_merge($ins_mappings, $mappings[$iconmap['iconmapid']]);
}
if ($ins_mappings) {
DB::insertBatch('icon_mapping', $ins_mappings);
}
if ($upd_mappings) {
DB::update('icon_mapping', $upd_mappings);
}
if ($del_mappingids) {
DB::delete('icon_mapping', ['iconmappingid' => $del_mappingids]);
}
}
/**
* @param array $iconmapids
*
* @return array
*/
public function delete(array $iconmapids) {
$this->validateDelete($iconmapids, $db_iconmaps);
DB::delete('icon_mapping', ['iconmapid' => $iconmapids]);
DB::delete('icon_map', ['iconmapid' => $iconmapids]);
$this->addAuditBulk(AUDIT_ACTION_DELETE, AUDIT_RESOURCE_ICON_MAP, $db_iconmaps);
return ['iconmapids' => $iconmapids];
}
/**
* @param array $iconmapids
* @param array $db_iconmaps
*
* @throws APIException if the input is invalid.
*/
private function validateDelete(array &$iconmapids, array &$db_iconmaps = null) {
$api_input_rules = ['type' => API_IDS, 'flags' => API_NOT_EMPTY, 'uniq' => true];
if (!CApiInputValidator::validate($api_input_rules, $iconmapids, '/', $error)) {
self::exception(ZBX_API_ERROR_PARAMETERS, $error);
}
$db_iconmaps = $this->get([
'output' => ['iconmapid', 'name'],
'iconmapids' => $iconmapids,
'editable' => true,
'preservekeys' => true
]);
foreach ($iconmapids as $iconmapid) {
if (!array_key_exists($iconmapid, $db_iconmaps)) {
self::exception(ZBX_API_ERROR_PERMISSIONS,
_('No permissions to referred object or it does not exist!')
);
}
}
$db_sysmaps = DB::select('sysmaps', [
'output' => ['name', 'iconmapid'],
'filter' => ['iconmapid' => $iconmapids],
'limit' => 1
]);
if ($db_sysmaps) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map "%1$s" cannot be deleted. Used in map "%2$s".',
$db_iconmaps[$db_sysmaps[0]['iconmapid']]['name'], $db_sysmaps[0]['name']
));
}
}
protected function addRelatedObjects(array $options, array $result) {
$result = parent::addRelatedObjects($options, $result);
$iconMapIds = array_keys($result);
if ($options['selectMappings'] !== null && $options['selectMappings'] != API_OUTPUT_COUNT) {
$mappings = API::getApiService()->select('icon_mapping', [
'output' => $this->outputExtend($options['selectMappings'], ['iconmapid', 'iconmappingid']),
'filter' => ['iconmapid' => $iconMapIds],
'preservekeys' => true
]);
$relationMap = $this->createRelationMap($mappings, 'iconmapid', 'iconmappingid');
$mappings = $this->unsetExtraFields($mappings, ['iconmapid', 'iconmappingid'], $options['selectMappings']);
$result = $relationMap->mapMany($result, $mappings, 'mappings');
}
return $result;
}
}
|