public function checkInput(&$maps, $method) { $create = $method == 'create'; $update = $method == 'update'; $delete = $method == 'delete'; // permissions if ($update || $delete) { $mapDbFields = array('sysmapid' => null); $dbMaps = $this->get(array('sysmapids' => zbx_objectValues($maps, 'sysmapid'), 'output' => API_OUTPUT_EXTEND, 'editable' => true, 'preservekeys' => true, 'selectLinks' => API_OUTPUT_EXTEND, 'selectSelements' => API_OUTPUT_EXTEND, 'selectUrls' => API_OUTPUT_EXTEND)); } else { $mapDbFields = array('name' => null, 'width' => null, 'height' => null, 'urls' => array(), 'selements' => array(), 'links' => array()); } $mapNames = array(); foreach ($maps as &$map) { if (!check_db_fields($mapDbFields, $map)) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect fields for sysmap.')); } if ($update || $delete) { if (!isset($dbMaps[$map['sysmapid']])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('No permissions to referred object or it does not exist!')); } $dbMap = array_merge($dbMaps[$map['sysmapid']], $map); } else { $dbMap = $map; } if (isset($map['name'])) { if (isset($mapNames[$map['name']])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Duplicate map name for map "%s".', $dbMap['name'])); } else { $mapNames[$map['name']] = $update ? $map['sysmapid'] : 1; } } if (isset($map['width']) && ($map['width'] > 65535 || $map['width'] < 1)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect map width value for map "%s".', $dbMap['name'])); } if (isset($map['height']) && ($map['height'] > 65535 || $map['height'] < 1)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect map height value for map "%s".', $dbMap['name'])); } // labels $mapLabels = array('label_type' => array('typeName' => _('icon'))); if ($dbMap['label_format'] == SYSMAP_LABEL_ADVANCED_ON) { $mapLabels['label_type_hostgroup'] = array('string' => 'label_string_hostgroup', 'typeName' => _('host group')); $mapLabels['label_type_host'] = array('string' => 'label_string_host', 'typeName' => _('host')); $mapLabels['label_type_trigger'] = array('string' => 'label_string_trigger', 'typeName' => _('trigger')); $mapLabels['label_type_map'] = array('string' => 'label_string_map', 'typeName' => _('map')); $mapLabels['label_type_image'] = array('string' => 'label_string_image', 'typeName' => _('image')); } foreach ($mapLabels as $labelName => $labelData) { if (!isset($map[$labelName])) { continue; } if (sysmapElementLabel($map[$labelName]) === false) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect %1$s label type value for map "%2$s".', $labelData['typeName'], $dbMap['name'])); } if ($map[$labelName] == MAP_LABEL_TYPE_CUSTOM) { if (!isset($labelData['string'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect %1$s label type value for map "%2$s".', $labelData['typeName'], $dbMap['name'])); } if (!isset($map[$labelData['string']]) || 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'], $dbMap['name'])); } } if ($labelName == 'label_type_image' && $map[$labelName] == MAP_LABEL_TYPE_STATUS) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect %1$s label type value for map "%2$s".', $labelData['typeName'], $dbMap['name'])); } if ($labelName == 'label_type' || $labelName == 'label_type_host') { continue; } if ($map[$labelName] == MAP_LABEL_TYPE_IP) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect %1$s label type value for map "%2$s".', $labelData['typeName'], $dbMap['name'])); } } // validating grid options $possibleGridSizes = array(20, 40, 50, 75, 100); if ($update || $create) { // grid size if (isset($map['grid_size']) && !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 (isset($map['grid_align']) && $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 (isset($map['grid_show']) && $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 (isset($map['urls']) && !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 "%s".', $dbMap['name'])); } if (!isset($urlNames[$url['name']])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('URL name should be unique for map "%s".', $dbMap['name'])); } unset($urlNames[$url['name']]); } } // map selement links if (!empty($map['links'])) { $selementIds = zbx_objectValues($dbMap['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'], $dbMap['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'], $dbMap['name'])); } } } } unset($map); // exists if (($create || $update) && $mapNames) { $existDbMaps = $this->get(array('filter' => array('name' => array_keys($mapNames)), 'output' => array('sysmapid', 'name'), 'nopermissions' => true)); foreach ($existDbMaps as $dbMap) { if ($create || bccomp($mapNames[$dbMap['name']], $dbMap['sysmapid']) != 0) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Map with name "%s" already exists.', $dbMap['name'])); } } } return $update || $delete ? $dbMaps : true; }
/** * 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'])); } } } } }
$user_groupids[$user_group['usrgrpid']] = $user_group['usrgrpid']; } } $data['users'] = API::User()->get(['output' => ['userid', 'alias', 'name', 'surname'], 'userids' => $userids, 'preservekeys' => true]); $data['user_groups'] = API::UserGroup()->get(['output' => ['usrgrpid', 'name'], 'usrgrpids' => $user_groupids, 'preservekeys' => true]); if (!hasRequest('sysmapid') || hasRequest('form_refresh')) { $data['sysmap'] = ['sysmapid' => getRequest('sysmapid'), 'name' => getRequest('name', ''), 'width' => getRequest('width', 800), 'height' => getRequest('height', 600), 'backgroundid' => getRequest('backgroundid', 0), 'iconmapid' => getRequest('iconmapid', 0), 'label_format' => getRequest('label_format', 0), 'label_type_host' => getRequest('label_type_host', 2), 'label_type_hostgroup' => getRequest('label_type_hostgroup', 2), 'label_type_trigger' => getRequest('label_type_trigger', 2), 'label_type_map' => getRequest('label_type_map', 2), 'label_type_image' => getRequest('label_type_image', 2), 'label_string_host' => getRequest('label_string_host', ''), 'label_string_hostgroup' => getRequest('label_string_hostgroup', ''), 'label_string_trigger' => getRequest('label_string_trigger', ''), 'label_string_map' => getRequest('label_string_map', ''), 'label_string_image' => getRequest('label_string_image', ''), 'label_type' => getRequest('label_type', 0), 'label_location' => getRequest('label_location', 0), 'highlight' => getRequest('highlight', 0), 'markelements' => getRequest('markelements', 0), 'expandproblem' => getRequest('expandproblem', 0), 'show_unack' => getRequest('show_unack', 0), 'severity_min' => getRequest('severity_min', TRIGGER_SEVERITY_NOT_CLASSIFIED), 'urls' => getRequest('urls', []), 'userid' => getRequest('userid', hasRequest('form_refresh') ? '' : $current_userid), 'private' => getRequest('private', PRIVATE_SHARING), 'users' => getRequest('users', []), 'userGroups' => getRequest('userGroups', [])]; } else { $data['sysmap'] = $sysmap; } $data['current_user_userid'] = $current_userid; $data['form_refresh'] = getRequest('form_refresh'); // config $data['config'] = select_config(); // advanced labels $data['labelTypes'] = sysmapElementLabel(); $data['labelTypesLimited'] = $data['labelTypes']; unset($data['labelTypesLimited'][MAP_LABEL_TYPE_IP]); $data['labelTypesImage'] = $data['labelTypesLimited']; unset($data['labelTypesImage'][MAP_LABEL_TYPE_STATUS]); // images $data['images'] = API::Image()->get(['output' => ['imageid', 'name'], 'filter' => ['imagetype' => IMAGE_TYPE_BACKGROUND]]); order_result($data['images'], 'name'); // icon maps $data['iconMaps'] = API::IconMap()->get(['output' => ['iconmapid', 'name'], 'preservekeys' => true]); order_result($data['iconMaps'], 'name'); // render view $mapView = new CView('monitoring.sysmap.edit', $data); $mapView->render(); $mapView->show(); } else {
** 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. **/ include 'include/views/js/configuration.sysmap.edit.js.php'; $config = select_config(); // advanced labels $labelTypes = sysmapElementLabel(); $labelTypesLimited = $labelTypes; unset($labelTypesLimited[MAP_LABEL_TYPE_IP]); $labelTypesImage = $labelTypesLimited; unset($labelTypesImage[MAP_LABEL_TYPE_STATUS]); // create sysmap form $frmSysmap = new CForm(); $frmSysmap->setName('map.edit.php'); $frmSysmap->addVar('form', get_request('form', 1)); $frmSysmap->addVar('form_refresh', get_request('form_refresh', 0) + 1); if (isset($this->data['sysmapid'])) { $frmSysmap->addVar('sysmapid', $this->data['sysmapid']); } // create sysmap $sysmapList = new CFormList('sysmaplist'); $sysmapList->addRow(_('Name'), new CTextBox('name', $this->data['name'], ZBX_TEXTBOX_STANDARD_SIZE));