/** * Validate input given to action.update API call. * * @param array $actions * @param array $actionsDb * * @internal param array $actionDb */ public function validateUpdate($actions, $actionsDb) { foreach ($actions as $action) { if (isset($action['actionid']) && !isset($actionsDb[$action['actionid']])) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!')); } } $actions = zbx_toHash($actions, 'actionid'); // check fields $duplicates = array(); foreach ($actions as $action) { $actionName = isset($action['name']) ? $action['name'] : $actionsDb[$action['actionid']]['name']; if (!check_db_fields(array('actionid' => null), $action)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect parameters for action update method "%1$s".', $actionName)); } // check if user changed esc_period for trigger eventsource if (isset($action['esc_period']) && $action['esc_period'] < SEC_PER_MIN && $actionsDb[$action['actionid']]['eventsource'] == EVENT_SOURCE_TRIGGERS) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Action "%1$s" has incorrect value for "esc_period" (minimum %2$s seconds).', $actionName, SEC_PER_MIN)); } $this->checkNoParameters($action, array('eventsource'), _('Cannot update "%1$s" for action "%2$s".'), $actionName); if (!isset($action['name'])) { continue; } if (isset($duplicates[$action['name']])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Action "%1$s" already exists.', $action['name'])); } else { $duplicates[$action['name']] = $action['name']; } } // Unset accidentally passed in "evaltype" and "formula" fields. foreach ($actions as &$action) { unset($action['evaltype'], $action['formula']); } unset($action); $filterValidator = new CSchemaValidator($this->getFilterSchema()); $filterConditionValidator = new CSchemaValidator($this->getFilterConditionSchema()); $operationsToValidate = array(); $conditionsToValidate = array(); foreach ($actions as $actionId => $action) { $actionDb = $actionsDb[$actionId]; if (isset($action['name'])) { $actionName = $action['name']; $actionExists = $this->get(array('filter' => array('name' => $actionName), 'output' => array('actionid'), 'editable' => true, 'nopermissions' => true, 'preservekeys' => true)); if (($actionExists = reset($actionExists)) && bccomp($actionExists['actionid'], $actionId) != 0) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Action "%1$s" already exists.', $actionName)); } } else { $actionName = $actionDb['name']; } if (isset($action['filter'])) { $actionFilter = $action['filter']; $filterValidator->setObjectName($actionName); $filterConditionValidator->setObjectName($actionName); $this->checkValidator($actionFilter, $filterValidator); foreach ($actionFilter['conditions'] as $condition) { $this->checkValidator($condition, $filterConditionValidator); $conditionsToValidate[] = $condition; } } if (isset($action['operations']) && empty($action['operations'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Action "%1$s" no operations defined.', $actionName)); } elseif (isset($action['operations'])) { $operationsDb = $actionsDb[$action['actionid']]['operations']; $operationsDb = zbx_toHash($operationsDb, 'operationid'); foreach ($action['operations'] as $operation) { if (!isset($operation['operationid'])) { $operationsToValidate[] = $operation; } elseif (isset($operationsDb[$operation['operationid']])) { $operationsToValidate[] = $operation; } else { self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action operationid.')); } } } } if ($conditionsToValidate) { $this->validateConditionsPermissions($conditionsToValidate); } if ($operationsToValidate) { $this->validateOperationsIntegrity($operationsToValidate); } }
/** * Validates the input parameters for the create() method. * * @throws APIException if the input is invalid * * @param array $hostPrototypes * * @return void */ protected function validateCreate(array $hostPrototypes) { // host prototype validator $hostPrototypeValidator = new CSchemaValidator($this->getHostPrototypeSchema()); $hostPrototypeValidator->setValidator('ruleid', new CIdValidator(['messageEmpty' => _('No discovery rule ID given for host prototype "%1$s".'), 'messageInvalid' => _('Incorrect discovery rule ID for host prototype "%1$s".')])); // group validators $groupLinkValidator = new CSchemaValidator($this->getGroupLinkSchema()); $groupPrototypeValidator = new CSchemaValidator($this->getGroupPrototypeSchema()); $groupPrototypeGroupIds = []; foreach ($hostPrototypes as $hostPrototype) { // host prototype $hostPrototypeValidator->setObjectName(isset($hostPrototype['host']) ? $hostPrototype['host'] : ''); $this->checkValidator($hostPrototype, $hostPrototypeValidator); // groups foreach ($hostPrototype['groupLinks'] as $groupPrototype) { $this->checkValidator($groupPrototype, $groupLinkValidator); $groupPrototypeGroupIds[$groupPrototype['groupid']] = $groupPrototype['groupid']; } // group prototypes if (isset($hostPrototype['groupPrototypes'])) { foreach ($hostPrototype['groupPrototypes'] as $groupPrototype) { $groupPrototypeValidator->setObjectName(isset($groupPrototype['name']) ? $groupPrototype['name'] : ''); $this->checkValidator($groupPrototype, $groupPrototypeValidator); } } } $this->checkDiscoveryRulePermissions(zbx_objectValues($hostPrototypes, 'ruleid')); $this->checkHostGroupsPermissions($groupPrototypeGroupIds); // check if the host is discovered $discoveryRules = API::getApiService()->select('items', ['output' => ['hostid'], 'itemids' => zbx_objectValues($hostPrototypes, 'ruleid')]); $this->checkValidator(zbx_objectValues($discoveryRules, 'hostid'), new CHostNormalValidator(['message' => _('Cannot create a host prototype on a discovered host "%1$s".')])); // check if group prototypes use discovered host groups $this->checkValidator(array_unique($groupPrototypeGroupIds), new CHostGroupNormalValidator(['message' => _('Group prototype cannot be based on a discovered host group "%1$s".')])); $this->checkDuplicates($hostPrototypes); }
/** * Check item data and set missing default values. * * @param array $items passed by reference * @param bool $update * @param array $dbItems */ protected function checkInput(array &$items, $update = false, array $dbItems = array()) { // add the values that cannot be changed, but are required for further processing foreach ($items as &$item) { $item['flags'] = ZBX_FLAG_DISCOVERY_RULE; $item['value_type'] = ITEM_VALUE_TYPE_TEXT; // unset fields that are updated using the 'filter' parameter unset($item['evaltype']); unset($item['formula']); } unset($item); parent::checkInput($items, $update); $validateItems = $items; if ($update) { $validateItems = $this->extendFromObjects(zbx_toHash($validateItems, 'itemid'), $dbItems, array('name')); } // filter validator $filterValidator = new CSchemaValidator($this->getFilterSchema()); // condition validation $conditionValidator = new CSchemaValidator($this->getFilterConditionSchema()); foreach ($validateItems as $item) { // validate custom formula and conditions if (isset($item['filter'])) { $filterValidator->setObjectName($item['name']); $this->checkValidator($item['filter'], $filterValidator); foreach ($item['filter']['conditions'] as $condition) { $conditionValidator->setObjectName($item['name']); $this->checkValidator($condition, $conditionValidator); } } } }