require_once dirname(__FILE__) . '/include/page_header.php'; // VAR TYPE OPTIONAL FLAGS VALIDATION EXCEPTION $fields = ['regexpids' => [T_ZBX_INT, O_OPT, P_SYS, DB_ID, null], 'regexpid' => [T_ZBX_INT, O_OPT, P_SYS, DB_ID, 'isset({form}) && {form} == "update"'], 'name' => [T_ZBX_STR, O_OPT, null, NOT_EMPTY, 'isset({add}) || isset({update})', _('Name')], 'test_string' => [T_ZBX_STR, O_OPT, P_NO_TRIM, null, 'isset({add}) || isset({update})', _('Test string')], 'expressions' => [T_ZBX_STR, O_OPT, P_NO_TRIM, null, 'isset({add}) || isset({update})'], 'action' => [T_ZBX_STR, O_OPT, P_SYS | P_ACT, IN('"regexp.massdelete"'), null], 'add' => [T_ZBX_STR, O_OPT, P_SYS | P_ACT, null, null], 'update' => [T_ZBX_STR, O_OPT, P_SYS | P_ACT, null, null], 'form' => [T_ZBX_STR, O_OPT, P_SYS, null, null], 'form_refresh' => [T_ZBX_INT, O_OPT, null, null, null], 'output' => [T_ZBX_STR, O_OPT, P_ACT, null, null], 'ajaxaction' => [T_ZBX_STR, O_OPT, P_ACT, null, null], 'ajaxdata' => [T_ZBX_STR, O_OPT, P_ACT | P_NO_TRIM, null, null]]; check_fields($fields); /* * Ajax */ if (isset($_REQUEST['output']) && $_REQUEST['output'] == 'ajax') { $ajaxResponse = new CAjaxResponse(); $ajaxData = getRequest('ajaxdata', []); if (isset($_REQUEST['ajaxaction']) && $_REQUEST['ajaxaction'] == 'test') { $result = ['expressions' => [], 'errors' => [], 'final' => true]; $validator = new CRegexValidator(['messageInvalid' => _('Regular expression must be a string'), 'messageRegex' => _('Incorrect regular expression "%1$s": "%2$s"')]); foreach ($ajaxData['expressions'] as $id => $expression) { if (!in_array($expression['expression_type'], [EXPRESSION_TYPE_FALSE, EXPRESSION_TYPE_TRUE]) || $validator->validate($expression['expression'])) { $match = CGlobalRegexp::matchExpression($expression, $ajaxData['testString']); $result['expressions'][$id] = $match; } else { $match = false; $result['errors'][$id] = $validator->getError(); } $result['final'] = $result['final'] && $match; } $ajaxResponse->success($result); } $ajaxResponse->send(); require_once dirname(__FILE__) . '/include/page_footer.php'; exit; } /* * Permissions
/** * Get icon id by mapping. * * @param array $iconMap * @param array $inventory * * @return int */ function getIconByMapping($iconMap, $inventory) { if (!empty($inventory['inventory'])) { $inventories = getHostInventories(); foreach ($iconMap['mappings'] as $mapping) { try { $expr = new CGlobalRegexp($mapping['expression']); if ($expr->match($inventory['inventory'][$inventories[$mapping['inventory_link']]['db_field']])) { return $mapping['iconid']; } } catch (Exception $e) { continue; } } } return $iconMap['default_iconid']; }
/** * Checks icon maps. * @throws APIException * @param $iconMaps * @param bool $mustExist if icon map should be checked against having at least one mapping * @return void */ protected function validateMappings($iconMaps, $mustExist = true) { $inventoryFields = getHostInventories(); $imageids = API::Image()->get(['output' => ['imageid'], 'preservekeys' => true, 'filter' => ['imagetype' => IMAGE_TYPE_ICON]]); foreach ($iconMaps as $iconMap) { if (isset($iconMap['mappings']) && empty($iconMap['mappings'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map "%s" must have at least one mapping.', $iconMap['name'])); } elseif (!isset($iconMap['mappings'])) { if ($mustExist) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map "%s" must have at least one mapping.', $iconMap['name'])); } else { continue; } } $uniqField = []; foreach ($iconMap['mappings'] as $mapping) { if (!isset($mapping['expression'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Required field "expression" is missing in icon mapping.')); } elseif (!isset($mapping['inventory_link'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Required field "inventory_link" is missing in icon mapping.')); } elseif (!isset($mapping['iconid'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Required field "iconid" is missing in icon mapping.')); } elseif (!isset($inventoryFields[$mapping['inventory_link']])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map "%1$s" has mapping with incorrect inventory link "%2$s".', $iconMap['name'], $mapping['inventory_link'])); } elseif (!isset($imageids[$mapping['iconid']])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map "%1$s" has mapping with incorrect iconid "%2$s".', $iconMap['name'], $mapping['iconid'])); } try { CGlobalRegexp::isValid($mapping['expression']); } catch (Exception $e) { switch ($e->getCode()) { case CGlobalRegexp::ERROR_REGEXP_EMPTY: self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map "%s" cannot have mapping with empty expression.', $iconMap['name'])); break; case CGlobalRegexp::ERROR_REGEXP_NOT_EXISTS: self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map "%s" cannot have mapping with global expression that does not exist.', $iconMap['name'])); break; default: self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map "%s" has incorrect expression.', $iconMap['name'])); } } if (isset($uniqField[$mapping['inventory_link'] . $mapping['expression']])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon mapping entry "%1$s" against "%2$s" already exists.', $mapping['expression'], $inventoryFields[$mapping['inventory_link']]['title'])); } $uniqField[$mapping['inventory_link'] . $mapping['expression']] = true; } } }