コード例 #1
0
ファイル: CHost.php プロジェクト: jbfavre/debian-zabbix
 /**
  * Validates the input parameters for the update() method.
  *
  * @param array $hosts			hosts data array
  * @param array $db_hosts		db hosts data array
  *
  * @throws APIException if the input is invalid.
  */
 protected function validateUpdate(array $hosts, array $db_hosts)
 {
     $host_db_fields = ['hostid' => null];
     $hosts_full = [];
     foreach ($hosts as $host) {
         // Validate mandatory fields.
         if (!check_db_fields($host_db_fields, $host)) {
             self::exception(ZBX_API_ERROR_PARAMETERS, _s('Wrong fields for host "%1$s".', array_key_exists('host', $host) ? $host['host'] : ''));
         }
         // Validate host permissions.
         if (!array_key_exists($host['hostid'], $db_hosts)) {
             self::exception(ZBX_API_ERROR_PARAMETERS, _('No permissions to referred object or it does not exist!'));
         }
         // Validate "groups" field.
         if (array_key_exists('groups', $host) && (!is_array($host['groups']) || !$host['groups'])) {
             self::exception(ZBX_API_ERROR_PARAMETERS, _s('No groups for host "%1$s".', $db_hosts[$host['hostid']]['host']));
         }
         // Permissions to host groups is validated in massUpdate().
     }
     $inventory_fields = zbx_objectValues(getHostInventories(), 'db_field');
     $status_validator = new CLimitedSetValidator(['values' => [HOST_STATUS_MONITORED, HOST_STATUS_NOT_MONITORED], 'messageInvalid' => _('Incorrect status for host "%1$s".')]);
     $update_discovered_validator = new CUpdateDiscoveredValidator(['allowed' => ['hostid', 'status', 'inventory', 'description'], 'messageAllowedField' => _('Cannot update "%2$s" for a discovered host "%1$s".')]);
     $host_names = [];
     foreach ($hosts as &$host) {
         $db_host = $db_hosts[$host['hostid']];
         $host_name = array_key_exists('host', $host) ? $host['host'] : $db_host['host'];
         if (array_key_exists('status', $host)) {
             $status_validator->setObjectName($host_name);
             $this->checkValidator($host['status'], $status_validator);
         }
         if (array_key_exists('inventory', $host) && $host['inventory']) {
             if (array_key_exists('inventory_mode', $host) && $host['inventory_mode'] == HOST_INVENTORY_DISABLED) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot set inventory fields for disabled inventory.'));
             }
             $fields = array_keys($host['inventory']);
             foreach ($fields as $field) {
                 if (!in_array($field, $inventory_fields)) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect inventory field "%s".', $field));
                 }
             }
         }
         // cannot update certain fields for discovered hosts
         $update_discovered_validator->setObjectName($host_name);
         $this->checkPartialValidator($host, $update_discovered_validator, $db_host);
         if (array_key_exists('interfaces', $host)) {
             if (!is_array($host['interfaces']) || !$host['interfaces']) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _s('No interfaces for host "%s".', $host['host']));
             }
         }
         if (array_key_exists('host', $host)) {
             if (!preg_match('/^' . ZBX_PREG_HOST_FORMAT . '$/', $host['host'])) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect characters used for host name "%s".', $host['host']));
             }
             if (array_key_exists('host', $host_names) && array_key_exists($host['host'], $host_names['host'])) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _s('Duplicate host. Host with the same host name "%s" already exists in data.', $host['host']));
             }
             $host_names['host'][$host['host']] = $host['hostid'];
         }
         if (array_key_exists('name', $host)) {
             // if visible name is empty replace it with host name
             if (zbx_empty(trim($host['name']))) {
                 if (!array_key_exists('host', $host)) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _s('Visible name cannot be empty if host name is missing.'));
                 }
                 $host['name'] = $host['host'];
             }
             if (array_key_exists('name', $host_names) && array_key_exists($host['name'], $host_names['name'])) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _s('Duplicate host. Host with the same visible name "%s" already exists in data.', $host['name']));
             }
             $host_names['name'][$host['name']] = $host['hostid'];
         }
         $hosts_full[] = zbx_array_merge($db_host, $host);
     }
     unset($host);
     if (array_key_exists('host', $host_names) || array_key_exists('name', $host_names)) {
         $filter = [];
         if (array_key_exists('host', $host_names)) {
             $filter['host'] = array_keys($host_names['host']);
         }
         if (array_key_exists('name', $host_names)) {
             $filter['name'] = array_keys($host_names['name']);
         }
         $hosts_exists = $this->get(['output' => ['hostid', 'host', 'name'], 'filter' => $filter, 'searchByAny' => true, 'nopermissions' => true, 'preservekeys' => true]);
         foreach ($hosts_exists as $host_exists) {
             if (array_key_exists('host', $host_names) && array_key_exists($host_exists['host'], $host_names['host']) && bccomp($host_exists['hostid'], $host_names['host'][$host_exists['host']]) != 0) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _s('Host with the same name "%s" already exists.', $host_exists['host']));
             }
             if (array_key_exists('name', $host_names) && array_key_exists($host_exists['name'], $host_names['name']) && bccomp($host_exists['hostid'], $host_names['name'][$host_exists['name']]) != 0) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _s('Host with the same visible name "%s" already exists.', $host_exists['name']));
             }
         }
         $templates_exists = API::Template()->get(['output' => ['hostid', 'host', 'name'], 'filter' => $filter, 'searchByAny' => true, 'nopermissions' => true, 'preservekeys' => true]);
         foreach ($templates_exists as $template_exists) {
             if (array_key_exists('host', $host_names) && array_key_exists($template_exists['host'], $host_names['host']) && bccomp($template_exists['templateid'], $host_names['host'][$template_exists['host']]) != 0) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _s('Template with the same name "%s" already exists.', $template_exists['host']));
             }
             if (array_key_exists('name', $host_names) && array_key_exists($template_exists['name'], $host_names['name']) && bccomp($template_exists['templateid'], $host_names['name'][$template_exists['name']]) != 0) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _s('Template with the same visible name "%s" already exists.', $template_exists['name']));
             }
         }
     }
     $this->validateEncryption($hosts_full);
 }
コード例 #2
0
ファイル: CHttpTest.php プロジェクト: omidmt/zabbix-greenplum
 /**
  * @param $httpTest
  *
  * @throws APIException if bad value for "verify_peer" parameter.
  * @throws APIException if bad value for "verify_host" parameter.
  * @throws APIException if SSL cert is present but SSL key is not.
  */
 protected function checkSslParameters($httpTest)
 {
     $verifyPeerValidator = new CLimitedSetValidator(array('values' => array(HTTPTEST_VERIFY_PEER_ON, HTTPTEST_VERIFY_PEER_OFF), 'messageInvalid' => _('Incorrect SSL verify peer value for web scenario "%1$s".')));
     $verifyPeerValidator->setObjectName($httpTest['name']);
     $this->checkValidator($httpTest['verify_peer'], $verifyPeerValidator);
     $verifyHostValidator = new CLimitedSetValidator(array('values' => array(HTTPTEST_VERIFY_HOST_ON, HTTPTEST_VERIFY_HOST_OFF), 'messageInvalid' => _('Incorrect SSL verify host value for web scenario "%1$s".')));
     $verifyHostValidator->setObjectName($httpTest['name']);
     $this->checkValidator($httpTest['verify_host'], $verifyHostValidator);
     if ($httpTest['ssl_key_password'] != '' && $httpTest['ssl_key_file'] == '') {
         self::exception(ZBX_API_ERROR_PARAMETERS, _s('Empty SSL key file for web scenario "%1$s".', $httpTest['name']));
     }
     if ($httpTest['ssl_key_file'] != '' && $httpTest['ssl_cert_file'] == '') {
         self::exception(ZBX_API_ERROR_PARAMETERS, _s('Empty SSL certificate file for web scenario "%1$s".', $httpTest['name']));
     }
 }
コード例 #3
0
 /**
  * Returns true if the given $value is valid, or set's an error and returns false otherwise.
  *
  *
  * @param $condition
  *
  * @return bool
  */
 public function validate($condition)
 {
     // build validators
     $timePeriodValidator = new CTimePeriodValidator();
     $discoveryCheckTypeValidator = new CLimitedSetValidator(array('values' => array_keys(discovery_check_type2str())));
     $discoveryObjectStatusValidator = new CLimitedSetValidator(array('values' => array_keys(discovery_object_status2str())));
     $triggerSeverityValidator = new CLimitedSetValidator(array('values' => array(TRIGGER_SEVERITY_NOT_CLASSIFIED, TRIGGER_SEVERITY_INFORMATION, TRIGGER_SEVERITY_WARNING, TRIGGER_SEVERITY_AVERAGE, TRIGGER_SEVERITY_HIGH, TRIGGER_SEVERITY_DISASTER)));
     $discoveryObjectValidator = new CLimitedSetValidator(array('values' => array_keys(discovery_object2str())));
     $triggerValueValidator = new CLimitedSetValidator(array('values' => array_keys(trigger_value2str())));
     $eventTypeValidator = new CLimitedSetValidator(array('values' => array_keys(eventType())));
     $conditionValue = $condition['value'];
     // validate condition values depending on condition type
     switch ($condition['conditiontype']) {
         case CONDITION_TYPE_HOST_GROUP:
             if (!$conditionValue) {
                 $this->setError(_('Empty action condition.'));
             }
             break;
         case CONDITION_TYPE_TEMPLATE:
             if (!$conditionValue) {
                 $this->setError(_('Empty action condition.'));
             }
             break;
         case CONDITION_TYPE_TRIGGER:
             if (!$conditionValue) {
                 $this->setError(_('Empty action condition.'));
             }
             break;
         case CONDITION_TYPE_HOST:
             if (!$conditionValue) {
                 $this->setError(_('Empty action condition.'));
             }
             break;
         case CONDITION_TYPE_DRULE:
             if (!$conditionValue) {
                 $this->setError(_('Empty action condition.'));
             }
             break;
         case CONDITION_TYPE_DCHECK:
             if (!$conditionValue) {
                 $this->setError(_('Empty action condition.'));
             }
             break;
         case CONDITION_TYPE_PROXY:
             if (!$conditionValue) {
                 $this->setError(_('Empty action condition.'));
             }
             break;
         case CONDITION_TYPE_DOBJECT:
             if (zbx_empty($conditionValue)) {
                 $this->setError(_('Empty action condition.'));
             } elseif (!$discoveryObjectValidator->validate($conditionValue)) {
                 $this->setError(_('Incorrect action condition discovery object.'));
             }
             break;
         case CONDITION_TYPE_TIME_PERIOD:
             if (!$timePeriodValidator->validate($conditionValue)) {
                 $this->setError($timePeriodValidator->getError());
             }
             break;
         case CONDITION_TYPE_DHOST_IP:
             $ipRangeValidator = new CIPRangeValidator();
             if (zbx_empty($conditionValue)) {
                 $this->setError(_('Empty action condition.'));
             } elseif (!$ipRangeValidator->validate($conditionValue)) {
                 $this->setError($ipRangeValidator->getError());
             }
             break;
         case CONDITION_TYPE_DSERVICE_TYPE:
             if (zbx_empty($conditionValue)) {
                 $this->setError(_('Empty action condition.'));
             } elseif (!$discoveryCheckTypeValidator->validate($conditionValue)) {
                 $this->setError(_('Incorrect action condition discovery check.'));
             }
             break;
         case CONDITION_TYPE_DSERVICE_PORT:
             if (zbx_empty($conditionValue)) {
                 $this->setError(_('Empty action condition.'));
             } elseif (!validate_port_list($conditionValue)) {
                 $this->setError(_s('Incorrect action condition port "%1$s".', $conditionValue));
             }
             break;
         case CONDITION_TYPE_DSTATUS:
             if (zbx_empty($conditionValue)) {
                 $this->setError(_('Empty action condition.'));
             } elseif (!$discoveryObjectStatusValidator->validate($conditionValue)) {
                 $this->setError(_('Incorrect action condition discovery status.'));
             }
             break;
         case CONDITION_TYPE_MAINTENANCE:
             if (!zbx_empty($conditionValue)) {
                 $this->setError(_('Maintenance action condition value must be empty.'));
             }
             break;
         case CONDITION_TYPE_TRIGGER_SEVERITY:
             if (zbx_empty($conditionValue)) {
                 $this->setError(_('Empty action condition.'));
             } elseif (!$triggerSeverityValidator->validate($conditionValue)) {
                 $this->setError(_('Incorrect action condition trigger severity.'));
             }
             break;
         case CONDITION_TYPE_TRIGGER_VALUE:
             if (zbx_empty($conditionValue)) {
                 $this->setError(_('Empty action condition.'));
             } elseif (!$triggerValueValidator->validate($conditionValue)) {
                 $this->setError(_('Incorrect action condition trigger value.'));
             }
             break;
         case CONDITION_TYPE_EVENT_TYPE:
             if (zbx_empty($conditionValue)) {
                 $this->setError(_('Empty action condition.'));
             } elseif (!$eventTypeValidator->validate($conditionValue)) {
                 $this->setError(_('Incorrect action condition event type.'));
             }
             break;
         case CONDITION_TYPE_TRIGGER_NAME:
         case CONDITION_TYPE_DUPTIME:
         case CONDITION_TYPE_DVALUE:
         case CONDITION_TYPE_APPLICATION:
         case CONDITION_TYPE_HOST_NAME:
         case CONDITION_TYPE_HOST_METADATA:
             if (zbx_empty($conditionValue)) {
                 $this->setError(_('Empty action condition.'));
             }
             break;
         default:
             $this->setError(_('Incorrect action condition type.'));
     }
     // If no error is not set, return true.
     return !(bool) $this->getError();
 }
コード例 #4
0
ファイル: CEvent.php プロジェクト: TonywalkerCN/Zabbix
 /**
  * Validates the input parameters for the get() method.
  *
  * @throws APIException     if the input is invalid
  *
  * @param array     $options
  *
  * @return void
  */
 protected function validateGet(array $options)
 {
     $sourceValidator = new CLimitedSetValidator(array('values' => array_keys(eventSource())));
     if (!$sourceValidator->validate($options['source'])) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect source value.'));
     }
     $objectValidator = new CLimitedSetValidator(array('values' => array_keys(eventObject())));
     if (!$objectValidator->validate($options['object'])) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect object value.'));
     }
     $sourceObjectValidator = new CEventSourceObjectValidator();
     if (!$sourceObjectValidator->validate(array('source' => $options['source'], 'object' => $options['object']))) {
         self::exception(ZBX_API_ERROR_PARAMETERS, $sourceObjectValidator->getError());
     }
 }
コード例 #5
0
ファイル: CMediatype.php プロジェクト: jbfavre/debian-zabbix
 /**
  * Check required fields by type. Values for fields must not be empty.
  *
  * @param array		$mediatype							An array of media type data.
  * @param string	$mediatype['description']			Name of the media type.
  * @param string	$mediatype['type']					E-mail, Script, SMS, Jabber and Ez Texting.
  *
  * @throws APIException if the input is invalid.
  */
 protected function checkRequiredFieldsByType(array $mediatype)
 {
     $type_validator = new CLimitedSetValidator(['values' => array_keys(media_type2str())]);
     if (!$type_validator->validate($mediatype['type'])) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect value "%1$s" in field "%2$s" for media type "%3$s".', $mediatype['type'], 'type', $mediatype['description']));
     }
     $required_fields_by_type = [MEDIA_TYPE_EMAIL => ['smtp_server', 'smtp_helo', 'smtp_email'], MEDIA_TYPE_EXEC => ['exec_path'], MEDIA_TYPE_SMS => ['gsm_modem'], MEDIA_TYPE_JABBER => ['username'], MEDIA_TYPE_EZ_TEXTING => ['exec_path', 'username']];
     foreach ($required_fields_by_type[$mediatype['type']] as $field) {
         // Check if fields set on Create method. For update method they are checked when type is changed.
         if (!array_key_exists($field, $mediatype)) {
             self::exception(ZBX_API_ERROR_PARAMETERS, _s('Field "%1$s" is required for media type "%2$s".', $field, $mediatype['description']));
         } elseif (array_key_exists($field, $mediatype) && ($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']));
         }
     }
 }
コード例 #6
0
ファイル: CHost.php プロジェクト: omidmt/zabbix-greenplum
 protected function checkInput(&$hosts, $method)
 {
     $create = $method == 'create';
     $update = $method == 'update';
     $hostDBfields = $update ? array('hostid' => null) : array('host' => null);
     if ($update) {
         $dbHosts = $this->get(array('output' => array('hostid', 'host', 'flags'), 'hostids' => zbx_objectValues($hosts, 'hostid'), 'editable' => true, 'preservekeys' => true));
         foreach ($hosts as $host) {
             if (!isset($dbHosts[$host['hostid']])) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _('No permissions to referred object or it does not exist!'));
             }
         }
     } else {
         $groupids = array();
         foreach ($hosts as $host) {
             if (!isset($host['groups'])) {
                 continue;
             }
             $groupids = array_merge($groupids, zbx_objectValues($host['groups'], 'groupid'));
         }
         if (!empty($groupids)) {
             $dbGroups = API::HostGroup()->get(array('output' => API_OUTPUT_EXTEND, 'groupids' => $groupids, 'editable' => true, 'preservekeys' => true));
         }
     }
     foreach ($hosts as $host) {
         // validate mandatory fields
         if (!check_db_fields($hostDBfields, $host)) {
             self::exception(ZBX_API_ERROR_PARAMETERS, _s('Wrong fields for host "%s".', isset($host['host']) ? $host['host'] : ''));
         }
         if ($update) {
             $hostId = $host['hostid'];
             // validate host permissions
             if (!isset($dbHosts[$hostId])) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _('No permissions to referred object or it does not exist!'));
             }
             if (isset($host['groups']) && (!is_array($host['groups']) || !$host['groups'])) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _s('No groups for host "%s".', $dbHosts[$hostId]['host']));
             }
         } else {
             if (!isset($host['groups']) || !is_array($host['groups']) || !$host['groups']) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _s('No groups for host "%s".', $host['host']));
             }
             foreach ($host['groups'] as $group) {
                 if (!isset($dbGroups[$group['groupid']])) {
                     self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!'));
                 }
             }
         }
     }
     $inventoryFields = getHostInventories();
     $inventoryFields = zbx_objectValues($inventoryFields, 'db_field');
     $statusValidator = new CLimitedSetValidator(array('values' => array(HOST_STATUS_MONITORED, HOST_STATUS_NOT_MONITORED), 'messageInvalid' => _('Incorrect status for host "%1$s".')));
     $hostNames = array();
     foreach ($hosts as &$host) {
         if (isset($host['status'])) {
             $hostName = isset($host['host']) ? $host['host'] : $dbHosts[$host['hostid']]['host'];
             $statusValidator->setObjectName($hostName);
             $this->checkValidator($host['status'], $statusValidator);
         }
         if (isset($host['inventory']) && !empty($host['inventory'])) {
             if (isset($host['inventory_mode']) && $host['inventory_mode'] == HOST_INVENTORY_DISABLED) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot set inventory fields for disabled inventory.'));
             }
             $fields = array_keys($host['inventory']);
             foreach ($fields as $field) {
                 if (!in_array($field, $inventoryFields)) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect inventory field "%s".', $field));
                 }
             }
         }
         $updateDiscoveredValidator = new CUpdateDiscoveredValidator(array('allowed' => array('hostid', 'status', 'inventory', 'description'), 'messageAllowedField' => _('Cannot update "%1$s" for a discovered host.')));
         if ($update) {
             // cannot update certain fields for discovered hosts
             $this->checkPartialValidator($host, $updateDiscoveredValidator, $dbHosts[$host['hostid']]);
         } else {
             // if visible name is not given or empty it should be set to host name
             if (!isset($host['name']) || zbx_empty(trim($host['name']))) {
                 $host['name'] = $host['host'];
             }
             if (!isset($host['interfaces'])) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _s('No interfaces for host "%s".', $host['host']));
             }
         }
         if (isset($host['interfaces'])) {
             if (!is_array($host['interfaces']) || empty($host['interfaces'])) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _s('No interfaces for host "%s".', $host['host']));
             }
         }
         if (isset($host['host'])) {
             if (!preg_match('/^' . ZBX_PREG_HOST_FORMAT . '$/', $host['host'])) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect characters used for host name "%s".', $host['host']));
             }
             if (isset($hostNames['host'][$host['host']])) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _s('Duplicate host. Host with the same host name "%s" already exists in data.', $host['host']));
             }
             $hostNames['host'][$host['host']] = $update ? $host['hostid'] : 1;
         }
         if (isset($host['name'])) {
             if ($update) {
                 // if visible name is empty replace it with host name
                 if (zbx_empty(trim($host['name']))) {
                     if (!isset($host['host'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _s('Visible name cannot be empty if host name is missing.'));
                     }
                     $host['name'] = $host['host'];
                 }
             }
             if (isset($hostNames['name'][$host['name']])) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _s('Duplicate host. Host with the same visible name "%s" already exists in data.', $host['name']));
             }
             $hostNames['name'][$host['name']] = $update ? $host['hostid'] : 1;
         }
     }
     unset($host);
     if ($update || $create) {
         if (isset($hostNames['host']) || isset($hostNames['name'])) {
             $filter = array();
             if (isset($hostNames['host'])) {
                 $filter['host'] = array_keys($hostNames['host']);
             }
             if (isset($hostNames['name'])) {
                 $filter['name'] = array_keys($hostNames['name']);
             }
             $options = array('output' => array('hostid', 'host', 'name'), 'filter' => $filter, 'searchByAny' => true, 'nopermissions' => true, 'preservekeys' => true);
             $hostsExists = $this->get($options);
             foreach ($hostsExists as $hostExists) {
                 if (isset($hostNames['host'][$hostExists['host']])) {
                     if (!$update || bccomp($hostExists['hostid'], $hostNames['host'][$hostExists['host']]) != 0) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _s('Host with the same name "%s" already exists.', $hostExists['host']));
                     }
                 }
                 if (isset($hostNames['name'][$hostExists['name']])) {
                     if (!$update || bccomp($hostExists['hostid'], $hostNames['name'][$hostExists['name']]) != 0) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _s('Host with the same visible name "%s" already exists.', $hostExists['name']));
                     }
                 }
             }
             $templatesExists = API::Template()->get($options);
             foreach ($templatesExists as $templateExists) {
                 if (isset($hostNames['host'][$templateExists['host']])) {
                     if (!$update || bccomp($templateExists['templateid'], $hostNames['host'][$templateExists['host']]) != 0) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _s('Template with the same name "%s" already exists.', $templateExists['host']));
                     }
                 }
                 if (isset($hostNames['name'][$templateExists['name']])) {
                     if (!$update || bccomp($templateExists['templateid'], $hostNames['name'][$templateExists['name']]) != 0) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _s('Template with the same visible name "%s" already exists.', $templateExists['name']));
                     }
                 }
             }
         }
     }
     return $update ? $dbHosts : $hosts;
 }
コード例 #7
0
ファイル: CMap.php プロジェクト: jbfavre/debian-zabbix
 /**
  * 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']));
                 }
             }
         }
     }
 }
コード例 #8
0
/**
 * Validate items array for bar reports - IDs, color, permissions, etc.
 * Color validation for graph items is only for "Distribution of values for multiple periods" section ($config == 1).
 * Automatically set caption like item name if none is set. If no axis side is set, set LEFT side as default.
 *
 * @param array $items
 *
 * @return mixed	valid items array on success or false on failure
 */
function validateBarReportItems($items = array())
{
    $config = getRequest('config', BR_DISTRIBUTION_MULTIPLE_PERIODS);
    if (!isset($items) || !$items) {
        return false;
    }
    $fields = array('itemid', 'calc_fnc');
    if ($config == BR_DISTRIBUTION_MULTIPLE_PERIODS) {
        array_push($fields, 'color');
    }
    foreach ($items as $item) {
        foreach ($fields as $field) {
            if (!isset($item[$field])) {
                show_error_message(_s('Missing "%1$s" field for item.', $field));
                return false;
            }
        }
        $itemIds[$item['itemid']] = $item['itemid'];
    }
    $validItems = API::Item()->get(array('output' => array('itemid', 'hostid', 'name', 'key_', 'value_type'), 'selectHosts' => array('name'), 'webitems' => true, 'itemids' => $itemIds, 'preservekeys' => true));
    $items = zbx_toHash($items, 'itemid');
    $allowedValueTypes = array(ITEM_VALUE_TYPE_FLOAT, ITEM_VALUE_TYPE_UINT64);
    foreach ($validItems as &$item) {
        if (!in_array($item['value_type'], $allowedValueTypes)) {
            show_error_message(_s('Cannot add a non-numeric item "%1$s".', $item['name']));
            return false;
        }
        // add host name and set caption like item name for valid items
        $item['host'] = reset($item['hosts']);
        unset($item['hosts']);
        // if no caption set, set default caption like item name
        if (!isset($items[$item['itemid']]['caption']) || zbx_empty($items[$item['itemid']]['caption'])) {
            $item['caption'] = $item['name'];
        } else {
            $validItems[$item['itemid']]['caption'] = $items[$item['itemid']]['caption'];
        }
        if (!isset($items[$item['itemid']]['axisside']) || zbx_empty($items[$item['itemid']]['axisside'])) {
            $items[$item['itemid']]['axisside'] = GRAPH_YAXIS_SIDE_LEFT;
        }
    }
    unset($item);
    // check axis value. 0 = count
    $calcFncValidator = new CLimitedSetValidator(array('values' => array(0, CALC_FNC_MIN, CALC_FNC_AVG, CALC_FNC_MAX)));
    $axisValidator = new CLimitedSetValidator(array('values' => array(GRAPH_YAXIS_SIDE_LEFT, GRAPH_YAXIS_SIDE_RIGHT)));
    if ($config == BR_DISTRIBUTION_MULTIPLE_PERIODS) {
        $colorValidator = new CColorValidator();
    }
    foreach ($items as $item) {
        if (!$calcFncValidator->validate($item['calc_fnc'])) {
            show_error_message(_s('Incorrect value for field "%1$s".', 'calc_fnc'));
            return false;
        }
        if (!$axisValidator->validate($item['axisside'])) {
            show_error_message(_s('Incorrect value for field "%1$s".', 'axisside'));
            return false;
        }
        if ($config == BR_DISTRIBUTION_MULTIPLE_PERIODS) {
            if (!$colorValidator->validate($item['color'])) {
                show_error_message($colorValidator->getError());
                return false;
            }
            $validItems[$item['itemid']]['color'] = $item['color'];
        }
        $validItems[$item['itemid']]['calc_fnc'] = $item['calc_fnc'];
        $validItems[$item['itemid']]['axisside'] = $item['axisside'];
    }
    return $validItems;
}
コード例 #9
0
ファイル: CScreen.php プロジェクト: jbfavre/debian-zabbix
 /**
  * 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);
         }
     }
 }