/** * Check "mappings" field properties. * * @param array $valuemaps An array of value maps. * @param array $valuemaps[]['mappings'] An array of "mappings" data. * @param string $valuemaps[]['mappings'][]['value'] Original mapping value. * @param string $valuemaps[]['mappings'][]['newvalue'] Value to which the original value is mapped to. * * @throws APIException if the input is invalid. */ protected function checkMappings(array $valuemaps) { $required_fields = ['value', 'newvalue']; foreach ($valuemaps as $valuemap) { if (!array_key_exists('mappings', $valuemap)) { continue; } if (!is_array($valuemap['mappings'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect arguments passed to function.')); } elseif (!$valuemap['mappings']) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('At least one mapping should be given for value map "%1$s".', $valuemap['name'])); } foreach ($valuemap['mappings'] as $mapping) { if (!is_array($mapping)) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect arguments passed to function.')); } $missing_keys = checkRequiredKeys($mapping, $required_fields); if ($missing_keys) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Mapping is missing parameters: %1$s for value map "%2$s".', implode(', ', $missing_keys), $valuemap['name'])); } else { foreach (['value', 'newvalue'] as $field) { if (is_array($mapping[$field])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect arguments passed to function.')); } } } if ($mapping['newvalue'] === '' || $mapping['newvalue'] === null || $mapping['newvalue'] === false) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Empty new value in value map "%1$s".', $valuemap['name'])); } } $duplicate = CArrayHelper::findDuplicate($valuemap['mappings'], 'value'); if ($duplicate) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Duplicate mapping value "%1$s" for value map "%2$s".', $duplicate['value'], $valuemap['name'])); } } }
/** * Validate web scenario parameters for create method. * - check if web scenario with same name already exists * - check if web scenario has at least one step * * @param array $httpTests */ protected function validateCreate(array $httpTests) { foreach ($httpTests as $httpTest) { $missingKeys = checkRequiredKeys($httpTest, array('name', 'hostid', 'steps')); if (!empty($missingKeys)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Web scenario missing parameters: %1$s', implode(', ', $missingKeys))); } } $hostIds = zbx_objectValues($httpTests, 'hostid'); if (!API::Host()->isWritable($hostIds)) { self::exception(ZBX_API_ERROR_PARAMETERS, _('No permissions to referred object or it does not exist!')); } foreach ($httpTests as $httpTest) { if (zbx_empty($httpTest['name'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Web scenario name cannot be empty.')); } $this->checkSslParameters($httpTest); if (empty($httpTest['steps'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Web scenario must have at least one step.')); } $this->checkSteps($httpTest); $this->checkDuplicateSteps($httpTest); } // check input array for duplicate names $collectionValidator = new CCollectionValidator(array('uniqueField' => 'name', 'uniqueField2' => 'hostid', 'messageDuplicate' => _('Web scenario "%1$s" already exists.'))); $this->checkValidator($httpTests, $collectionValidator); // check database for duplicate names $this->checkDuplicates($httpTests); $this->checkApplicationHost($httpTests); }
/** * Validate web scenario parameters for update method. * - check permissions * - check if web scenario with same name already exists * - check that each web scenario object has httptestid defined * * @param array $httpTests */ protected function validateUpdate(array $httpTests, array $dbHttpTests) { $httpTestIds = zbx_objectValues($httpTests, 'httptestid'); if (!$this->isWritable($httpTestIds)) { self::exception(ZBX_API_ERROR_PARAMETERS, _('You do not have permission to perform this operation.')); } $this->checkNames($httpTests); foreach ($httpTests as $httpTest) { $missingKeys = checkRequiredKeys($httpTest, array('httptestid')); if (!empty($missingKeys)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Web scenario missing parameters: %1$s', implode(', ', $missingKeys))); } if (isset($httpTest['name'])) { // get hostid from db if it's not provided if (isset($httpTest['hostid'])) { $hostId = $httpTest['hostid']; } else { $hostId = DBfetch(DBselect('SELECT ht.hostid FROM httptest ht' . ' WHERE ht.httptestid=' . zbx_dbstr($httpTest['httptestid']))); $hostId = $hostId['hostid']; } $nameExists = DBfetch(DBselect('SELECT ht.name FROM httptest ht' . ' WHERE ht.name=' . zbx_dbstr($httpTest['name']) . ' AND ht.hostid=' . zbx_dbstr($hostId) . ' AND ht.httptestid<>' . zbx_dbstr($httpTest['httptestid']), 1)); if ($nameExists) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Web scenario "%1$s" already exists.', $nameExists['name'])); } } if (!check_db_fields(array('httptestid' => null), $httpTest)) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect arguments passed to function.')); } if (array_key_exists('steps', $httpTest)) { $dbHttpTest = isset($httpTest['httptestid']) ? $dbHttpTests[$httpTest['httptestid']] : null; $this->checkSteps($httpTest, $dbHttpTest); $this->checkDuplicateSteps($httpTest); } } $this->checkApplicationHost($httpTests); }
/** * Validates the input parameters for the create() method. * * @param array $mediatypes * * @throws APIException if the input is invalid. */ protected function validateCreate(array $mediatypes) { if (self::$userData['type'] != USER_TYPE_SUPER_ADMIN) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('Only Super Admins can create media types.')); } if (!$mediatypes) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty input parameter.')); } $required_fields = ['type', 'description']; foreach ($mediatypes as $mediatype) { if (!is_array($mediatype)) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect arguments passed to function.')); } // Check required parameters. $missing_keys = checkRequiredKeys($mediatype, $required_fields); if ($missing_keys) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Media type is missing parameters: %1$s', implode(', ', $missing_keys))); } else { foreach ($required_fields as $field) { if ($mediatype[$field] === '' || $mediatype[$field] === null) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Field "%1$s" is missing a value for media type "%2$s".', $field, $mediatype['description'])); } } } } // Check for duplicate names. $duplicate_name = CArrayHelper::findDuplicate($mediatypes, 'description'); if ($duplicate_name) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Duplicate "description" value "%1$s" for media type.', $duplicate_name['description'])); } foreach ($mediatypes as $mediatype) { // Check if media type already exists. $db_mediatype = API::getApiService()->select('media_type', ['output' => ['description'], 'filter' => ['description' => $mediatype['description']], 'limit' => 1]); if ($db_mediatype) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Media type "%1$s" already exists.', $mediatype['description'])); } // Check additional fields and values depeding on type. $this->checkRequiredFieldsByType($mediatype); switch ($mediatype['type']) { case MEDIA_TYPE_EZ_TEXTING: $message_text_limit_validator = new CLimitedSetValidator(['values' => [EZ_TEXTING_LIMIT_USA, EZ_TEXTING_LIMIT_CANADA]]); if (!$message_text_limit_validator->validate($mediatype['exec_path'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect value "%1$s" in field "%2$s" for media type "%3$s".', $mediatype['exec_path'], 'exec_path', $mediatype['description'])); } break; case MEDIA_TYPE_EMAIL: if (array_key_exists('smtp_authentication', $mediatype)) { $smtp_authentication_validator = new CLimitedSetValidator(['values' => [SMTP_AUTHENTICATION_NONE, SMTP_AUTHENTICATION_NORMAL]]); if (!$smtp_authentication_validator->validate($mediatype['smtp_authentication'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect value "%1$s" in field "%2$s" for media type "%3$s".', $mediatype['smtp_authentication'], 'smtp_authentication', $mediatype['description'])); } if ($mediatype['smtp_authentication'] == SMTP_AUTHENTICATION_NORMAL && (!array_key_exists('passwd', $mediatype) || $mediatype['passwd'] === '' || $mediatype['passwd'] === null)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Password required for media type "%1$s".', $mediatype['description'])); } } // Validate optional 'smtp_port' field. if (array_key_exists('smtp_port', $mediatype) && !validatePortNumber($mediatype['smtp_port'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect value "%1$s" in field "%2$s" for media type "%3$s".', $mediatype['smtp_port'], 'smtp_port', $mediatype['description'])); } // Validate optional field 'smtp_security'. if (array_key_exists('smtp_security', $mediatype)) { $smtp_security_validator = new CLimitedSetValidator(['values' => [SMTP_CONNECTION_SECURITY_NONE, SMTP_CONNECTION_SECURITY_STARTTLS, SMTP_CONNECTION_SECURITY_SSL_TLS]]); if (!$smtp_security_validator->validate($mediatype['smtp_security'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect value "%1$s" in field "%2$s" for media type "%3$s".', $mediatype['smtp_security'], 'smtp_security', $mediatype['description'])); } } // Validate optional field 'smtp_verify_peer'. if (array_key_exists('smtp_verify_peer', $mediatype)) { $smtp_verify_peer_validator = new CLimitedSetValidator(['values' => [0, 1]]); if (!$smtp_verify_peer_validator->validate($mediatype['smtp_verify_peer'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect value "%1$s" in field "%2$s" for media type "%3$s".', $mediatype['smtp_verify_peer'], 'smtp_verify_peer', $mediatype['description'])); } } // Validate optional field 'smtp_verify_host'. if (array_key_exists('smtp_verify_host', $mediatype)) { $smtp_verify_host_validator = new CLimitedSetValidator(['values' => [0, 1]]); if (!$smtp_verify_host_validator->validate($mediatype['smtp_verify_host'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect value "%1$s" in field "%2$s" for media type "%3$s".', $mediatype['smtp_verify_host'], 'smtp_verify_host', $mediatype['description'])); } } break; case MEDIA_TYPE_EXEC: if (array_key_exists('exec_params', $mediatype) && $mediatype['exec_params'] !== '') { $pos = strrpos($mediatype['exec_params'], "\n"); if ($pos === false || strlen($mediatype['exec_params']) != $pos + 1) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Script parameters "%1$s" are missing the last new line feed for media type "%2$s".', $mediatype['exec_params'], $mediatype['description'])); } } break; } // Validate optional 'status' field. if (array_key_exists('status', $mediatype)) { $status_validator = new CLimitedSetValidator(['values' => [MEDIA_TYPE_STATUS_ACTIVE, MEDIA_TYPE_STATUS_DISABLED]]); if (!$status_validator->validate($mediatype['status'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect value "%1$s" in field "%2$s" for media type "%3$s".', $mediatype['status'], 'status', $mediatype['description'])); } } } }
/** * Validate the input parameters for the update() method. * * @param array $maps maps data array * @param array $db_maps db maps data array * * @throws APIException if the input is invalid. */ protected function validateUpdate(array $maps, array $db_maps) { if (!$maps) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty input parameter.')); } $user_data = self::$userData; // Validate given IDs. $this->checkObjectIds($maps, 'sysmapid', _('No "%1$s" given for map.'), _('Empty map ID.'), _('Incorrect map ID.')); $check_names = []; foreach ($maps as $map) { // Check if this map exists and user has write permissions. if (!array_key_exists($map['sysmapid'], $db_maps)) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!')); } // Validate "name" field. if (array_key_exists('name', $map)) { if (is_array($map['name'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect arguments passed to function.')); } elseif ($map['name'] === '' || $map['name'] === null || $map['name'] === false) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Map name cannot be empty.')); } if ($db_maps[$map['sysmapid']]['name'] !== $map['name']) { $check_names[] = $map; } } } if ($check_names) { // Check for duplicate names. $duplicate = CArrayHelper::findDuplicate($check_names, 'name'); if ($duplicate) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Duplicate "name" value "%1$s" for map.', $duplicate['name'])); } $db_map_names = $this->get(['output' => ['sysmapid', 'name'], 'filter' => ['name' => zbx_objectValues($check_names, 'name')], 'nopermissions' => true]); $db_map_names = zbx_toHash($db_map_names, 'name'); // Check for existing names. foreach ($check_names as $map) { if (array_key_exists($map['name'], $db_map_names) && bccomp($db_map_names[$map['name']]['sysmapid'], $map['sysmapid']) != 0) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Map "%1$s" already exists.', $map['name'])); } } } $private_validator = new CLimitedSetValidator(['values' => [PUBLIC_SHARING, PRIVATE_SHARING]]); $permission_validator = new CLimitedSetValidator(['values' => [PERM_READ, PERM_READ_WRITE]]); foreach ($maps as $map) { // Check if owner can be set. if (array_key_exists('userid', $map)) { if ($map['userid'] === '' || $map['userid'] === null || $map['userid'] === false) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Map owner cannot be empty.')); } elseif ($map['userid'] != $user_data['userid'] && $user_data['type'] != USER_TYPE_SUPER_ADMIN && $user_data['type'] != USER_TYPE_ZABBIX_ADMIN) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Only administrators can set map owner.')); } } // Unset extra field. unset($db_maps[$map['sysmapid']]['userid']); $map = array_merge($db_maps[$map['sysmapid']], $map); // Check "width" and "height" fields. if ($map['width'] > 65535 || $map['width'] < 1) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect "width" value for map "%1$s".', $map['name'])); } if ($map['height'] > 65535 || $map['height'] < 1) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect "height" value for map "%1$s".', $map['name'])); } if (!$private_validator->validate($map['private'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect "private" value "%1$s" for map "%2$s".', $map['private'], $map['name'])); } $userids = []; // Map user shares. if (array_key_exists('users', $map)) { if (!is_array($map['users'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect arguments passed to function.')); } $required_fields = ['userid', 'permission']; foreach ($map['users'] as $share) { // Check required parameters. $missing_keys = checkRequiredKeys($share, $required_fields); if ($missing_keys) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('User sharing is missing parameters: %1$s for map "%2$s".', implode(', ', $missing_keys), $map['name'])); } else { foreach ($required_fields as $field) { if ($share[$field] === '' || $share[$field] === null) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Sharing option "%1$s" is missing a value for map "%2$s".', $field, $map['name'])); } } } if (!$permission_validator->validate($share['permission'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect "permission" value "%1$s" in users for map "%2$s".', $share['permission'], $map['name'])); } if ($map['private'] == PUBLIC_SHARING && $share['permission'] == PERM_READ) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Map "%1$s" is public and read-only sharing is disallowed.', $map['name'])); } if (array_key_exists($share['userid'], $userids)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Duplicate userid "%1$s" in users for map "%2$s".', $share['userid'], $map['name'])); } $userids[$share['userid']] = $share['userid']; } } if (array_key_exists('userid', $map) && $map['userid']) { $userids[$map['userid']] = $map['userid']; } // Users validation. if ($userids) { $db_users = API::User()->get(['userids' => $userids, 'countOutput' => true]); if (count($userids) != $db_users) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect user ID specified for map "%1$s".', $map['name'])); } } // Map user group shares. if (array_key_exists('userGroups', $map)) { if (!is_array($map['userGroups'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect arguments passed to function.')); } $shared_user_groupids = []; $required_fields = ['usrgrpid', 'permission']; foreach ($map['userGroups'] as $share) { // Check required parameters. $missing_keys = checkRequiredKeys($share, $required_fields); if ($missing_keys) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('User group sharing is missing parameters: %1$s for map "%2$s".', implode(', ', $missing_keys), $map['name'])); } else { foreach ($required_fields as $field) { if ($share[$field] === '' || $share[$field] === null) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Sharing option "%1$s" is missing a value for map "%2$s".', $field, $map['name'])); } } } if (!$permission_validator->validate($share['permission'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect "permission" value "%1$s" in user groups for map "%2$s".', $share['permission'], $map['name'])); } if ($map['private'] == PUBLIC_SHARING && $share['permission'] == PERM_READ) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Map "%1$s" is public and read-only sharing is disallowed.', $map['name'])); } if (array_key_exists($share['usrgrpid'], $shared_user_groupids)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Duplicate usrgrpid "%1$s" in user groups for map "%2$s".', $share['usrgrpid'], $map['name'])); } $shared_user_groupids[$share['usrgrpid']] = $share['usrgrpid']; } if ($shared_user_groupids) { $db_user_groups = API::UserGroup()->get(['usrgrpids' => $shared_user_groupids, 'countOutput' => true]); if (count($shared_user_groupids) != $db_user_groups) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect user group ID specified for map "%1$s".', $map['name'])); } } unset($shared_user_groupids); } // Map labels. $map_labels = ['label_type' => ['typeName' => _('icon')]]; if (array_key_exists('label_format', $map) && $map['label_format'] == SYSMAP_LABEL_ADVANCED_ON) { $map_labels['label_type_hostgroup'] = ['string' => 'label_string_hostgroup', 'typeName' => _('host group')]; $map_labels['label_type_host'] = ['string' => 'label_string_host', 'typeName' => _('host')]; $map_labels['label_type_trigger'] = ['string' => 'label_string_trigger', 'typeName' => _('trigger')]; $map_labels['label_type_map'] = ['string' => 'label_string_map', 'typeName' => _('map')]; $map_labels['label_type_image'] = ['string' => 'label_string_image', 'typeName' => _('image')]; } foreach ($map_labels as $label_name => $labelData) { if (!array_key_exists($label_name, $map)) { continue; } if (sysmapElementLabel($map[$label_name]) === false) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect %1$s label type value for map "%2$s".', $labelData['typeName'], $map['name'])); } if ($map[$label_name] == MAP_LABEL_TYPE_CUSTOM) { if (!array_key_exists('string', $labelData)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect %1$s label type value for map "%2$s".', $labelData['typeName'], $map['name'])); } if (!array_key_exists($labelData['string'], $map) || zbx_empty($map[$labelData['string']])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Custom label for map "%2$s" elements of type "%1$s" may not be empty.', $labelData['typeName'], $map['name'])); } } if ($label_name === 'label_type_image' && $map[$label_name] == MAP_LABEL_TYPE_STATUS) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect %1$s label type value for map "%2$s".', $labelData['typeName'], $map['name'])); } if ($label_name === 'label_type' || $label_name === 'label_type_host') { continue; } if ($map[$label_name] == MAP_LABEL_TYPE_IP) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect %1$s label type value for map "%2$s".', $labelData['typeName'], $map['name'])); } } // Validating grid options. $possibleGridSizes = [20, 40, 50, 75, 100]; // Grid size. if (array_key_exists('grid_size', $map) && !in_array($map['grid_size'], $possibleGridSizes)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Value "%1$s" is invalid for parameter "grid_show". Choices are: "%2$s".', $map['grid_size'], implode('", "', $possibleGridSizes))); } // Grid auto align. if (array_key_exists('grid_align', $map) && $map['grid_align'] != SYSMAP_GRID_ALIGN_ON && $map['grid_align'] != SYSMAP_GRID_ALIGN_OFF) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Value "%1$s" is invalid for parameter "grid_align". Choices are: "%2$s" and "%3$s"', $map['grid_align'], SYSMAP_GRID_ALIGN_ON, SYSMAP_GRID_ALIGN_OFF)); } // Grid show. if (array_key_exists('grid_show', $map) && $map['grid_show'] != SYSMAP_GRID_SHOW_ON && $map['grid_show'] != SYSMAP_GRID_SHOW_OFF) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Value "%1$s" is invalid for parameter "grid_show". Choices are: "%2$s" and "%3$s".', $map['grid_show'], SYSMAP_GRID_SHOW_ON, SYSMAP_GRID_SHOW_OFF)); } // Urls. if (array_key_exists('urls', $map) && !empty($map['urls'])) { $urlNames = zbx_toHash($map['urls'], 'name'); foreach ($map['urls'] as $url) { if ($url['name'] === '' || $url['url'] === '') { self::exception(ZBX_API_ERROR_PARAMETERS, _s('URL should have both "name" and "url" fields for map "%1$s".', $map['name'])); } if (!array_key_exists($url['name'], $urlNames)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('URL name should be unique for map "%1$s".', $map['name'])); } unset($urlNames[$url['name']]); } } if (array_key_exists('selements', $map) && !is_array($map['selements'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect arguments passed to function.')); } // Map selement links. if (array_key_exists('links', $map) && $map['links']) { $selementids = zbx_objectValues($map['selements'], 'selementid'); foreach ($map['links'] as $link) { if (!in_array($link['selementid1'], $selementids)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Link selementid1 field is pointing to a nonexistent map selement ID "%1$s" for map "%2$s".', $link['selementid1'], $map['name'])); } if (!in_array($link['selementid2'], $selementids)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Link selementid2 field is pointing to a nonexistent map selement ID "%1$s" for map "%2$s".', $link['selementid2'], $map['name'])); } } } } }
/** * Validates the "macro" field. * * @param array $macro * @param string $macro['macro'] * * @throws APIException if the field is not valid. */ protected function checkMacro(array $macro) { $required_fields = ['macro']; $missing_keys = checkRequiredKeys($macro, $required_fields); if ($missing_keys) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('User macro missing parameters: %1$s', implode(', ', $missing_keys))); } $user_macro_parser = new CUserMacroParser(); if ($user_macro_parser->parse($macro['macro']) != CParser::PARSE_SUCCESS) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Invalid macro "%1$s": %2$s.', $macro['macro'], $user_macro_parser->getError())); } }
/** * Validates the input parameters for the update() method. * * @param array $screens * @param array $db_screens array of existing screens with screen IDs as keys. * * @throws APIException if the input is invalid. */ protected function validateUpdate(array $screens, array $db_screens) { if (!$screens) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty input parameter.')); } $user_data = self::$userData; // Validate given IDs. $this->checkObjectIds($screens, 'screenid', _('No "%1$s" given for screen.'), _('Empty screen ID.'), _('Incorrect screen ID.')); $check_names = []; foreach ($screens as $screen) { $this->validateScreenSize($screen); if (!array_key_exists($screen['screenid'], $db_screens)) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!')); } } $screens = $this->extendFromObjects(zbx_toHash($screens, 'screenid'), $db_screens, ['name']); foreach ($screens as $screen) { // "templateid" is not allowed if (array_key_exists('templateid', $screen)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Cannot update "templateid" for screen "%1$s".', $screen['name'])); } if (array_key_exists('name', $screen)) { // Validate "name" field. if (array_key_exists('name', $screen)) { if (is_array($screen['name'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect arguments passed to function.')); } elseif ($screen['name'] === '' || $screen['name'] === null || $screen['name'] === false) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Screen name cannot be empty.')); } if ($db_screens[$screen['screenid']]['name'] !== $screen['name']) { $check_names[] = $screen; } } } } if ($check_names) { // Check for duplicate names. $duplicate = CArrayHelper::findDuplicate($check_names, 'name'); if ($duplicate) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Duplicate "name" value "%1$s" for screen.', $duplicate['name'])); } $db_screen_names = $this->get(['output' => ['screenid', 'name'], 'filter' => ['name' => zbx_objectValues($check_names, 'name')], 'nopermissions' => true]); $db_screen_names = zbx_toHash($db_screen_names, 'name'); // Check for existing names. foreach ($check_names as $screen) { if (array_key_exists($screen['name'], $db_screen_names) && bccomp($db_screen_names[$screen['name']]['screenid'], $screen['screenid']) != 0) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Screen "%1$s" already exists.', $screen['name'])); } } } $private_validator = new CLimitedSetValidator(['values' => [PUBLIC_SHARING, PRIVATE_SHARING]]); $permission_validator = new CLimitedSetValidator(['values' => [PERM_READ, PERM_READ_WRITE]]); foreach ($screens as $screen) { // Check if owner can be set. if (array_key_exists('userid', $screen)) { if ($screen['userid'] === '' || $screen['userid'] === null || $screen['userid'] === false) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Screen owner cannot be empty.')); } elseif ($screen['userid'] != $user_data['userid'] && $user_data['type'] != USER_TYPE_SUPER_ADMIN && $user_data['type'] != USER_TYPE_ZABBIX_ADMIN) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Only administrators can set screen owner.')); } } // Unset extra field. unset($db_screens[$screen['screenid']]['userid']); $screen = array_merge($db_screens[$screen['screenid']], $screen); if (!$private_validator->validate($screen['private'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect "private" value "%1$s" for screen "%2$s".', $screen['private'], $screen['name'])); } $userids = []; // Screen user shares. if (array_key_exists('users', $screen)) { if (!is_array($screen['users'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect arguments passed to function.')); } $required_fields = ['userid', 'permission']; foreach ($screen['users'] as $share) { // Check required parameters. $missing_keys = checkRequiredKeys($share, $required_fields); if ($missing_keys) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('User sharing is missing parameters: %1$s for screen "%2$s".', implode(', ', $missing_keys), $screen['name'])); } else { foreach ($required_fields as $field) { if ($share[$field] === '' || $share[$field] === null) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Sharing option "%1$s" is missing a value for screen "%2$s".', $field, $screen['name'])); } } } if (!$permission_validator->validate($share['permission'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect "permission" value "%1$s" in users for screen "%2$s".', $share['permission'], $screen['name'])); } if ($screen['private'] == PUBLIC_SHARING && $share['permission'] == PERM_READ) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Screen "%1$s" is public and read-only sharing is disallowed.', $screen['name'])); } if (array_key_exists($share['userid'], $userids)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Duplicate userid "%1$s" in users for screen "%2$s".', $share['userid'], $screen['name'])); } $userids[$share['userid']] = $share['userid']; } } if (array_key_exists('userid', $screen) && $screen['userid']) { $userids[$screen['userid']] = $screen['userid']; } // Users validation. if ($userids) { $db_users = API::User()->get(['userids' => $userids, 'countOutput' => true]); if (count($userids) != $db_users) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect user ID specified for screen "%1$s".', $screen['name'])); } } // Screen user group shares. if (array_key_exists('userGroups', $screen)) { if (!is_array($screen['userGroups'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect arguments passed to function.')); } $shared_user_groupids = []; $required_fields = ['usrgrpid', 'permission']; foreach ($screen['userGroups'] as $share) { // Check required parameters. $missing_keys = checkRequiredKeys($share, $required_fields); if ($missing_keys) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('User group sharing is missing parameters: %1$s for screen "%2$s".', implode(', ', $missing_keys), $screen['name'])); } else { foreach ($required_fields as $field) { if ($share[$field] === '' || $share[$field] === null) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Sharing option "%1$s" is missing a value for screen "%2$s".', $field, $screen['name'])); } } } if (!$permission_validator->validate($share['permission'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect "permission" value "%1$s" in user groups for screen "%2$s".', $share['permission'], $screen['name'])); } if ($screen['private'] == PUBLIC_SHARING && $share['permission'] == PERM_READ) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Screen "%1$s" is public and read-only sharing is disallowed.', $screen['name'])); } if (array_key_exists($share['usrgrpid'], $shared_user_groupids)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Duplicate usrgrpid "%1$s" in user groups for screen "%2$s".', $share['usrgrpid'], $screen['name'])); } $shared_user_groupids[$share['usrgrpid']] = $share['usrgrpid']; } if ($shared_user_groupids) { $db_user_groups = API::UserGroup()->get(['usrgrpids' => $shared_user_groupids, 'countOutput' => true]); if (count($shared_user_groupids) != $db_user_groups) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect user group ID specified for screen "%1$s".', $screen['name'])); } } unset($shared_user_groupids); } } }