protected function validateDChecks(array &$dChecks)
 {
     $uniq = 0;
     foreach ($dChecks as $dcnum => $dCheck) {
         if (isset($dCheck['uniq']) && $dCheck['uniq'] == 1) {
             $uniq++;
         }
         if (isset($dCheck['ports']) && !validate_port_list($dCheck['ports'])) {
             self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect port range.'));
         }
         switch ($dCheck['type']) {
             case SVC_AGENT:
                 if (!isset($dCheck['key_'])) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect key.'));
                 }
                 $itemKey = new CItemKey($dCheck['key_']);
                 if (!$itemKey->isValid()) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect key: %s', $itemKey->getError()));
                 }
                 break;
             case SVC_SNMPv1:
             case SVC_SNMPv2c:
                 if (!isset($dCheck['snmp_community']) || zbx_empty($dCheck['snmp_community'])) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect SNMP community.'));
                 }
             case SVC_SNMPv3:
                 if (!isset($dCheck['key_']) || zbx_empty($dCheck['key_'])) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect SNMP OID.'));
                 }
                 break;
         }
         if (!isset($dCheck['snmpv3_securitylevel'])) {
             $dCheck['snmpv3_securitylevel'] = ITEM_SNMPV3_SECURITYLEVEL_NOAUTHNOPRIV;
         }
         switch ($dCheck['snmpv3_securitylevel']) {
             case ITEM_SNMPV3_SECURITYLEVEL_NOAUTHNOPRIV:
                 $dChecks[$dcnum]['snmpv3_authpassphrase'] = $dChecks[$dcnum]['snmpv3_privpassphrase'] = '';
                 break;
             case ITEM_SNMPV3_SECURITYLEVEL_AUTHNOPRIV:
                 $dChecks[$dcnum]['snmpv3_privpassphrase'] = '';
                 break;
         }
         $this->validateDuplicateChecks($dChecks);
     }
     if ($uniq > 1) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Only one check can be unique.'));
     }
 }
示例#2
0
if (isset($_REQUEST['druleid'])) {
    $dbDRule = API::DRule()->get(array('druleids' => get_request('druleid'), 'output' => API_OUTPUT_EXTEND, 'selectDChecks' => API_OUTPUT_EXTEND, 'editable' => true));
    if (empty($dbDRule)) {
        access_deny();
    }
}
$_REQUEST['go'] = get_request('go', 'none');
// ajax
if (isset($_REQUEST['output']) && $_REQUEST['output'] == 'ajax') {
    $ajaxResponse = new AjaxResponse();
    if (isset($_REQUEST['ajaxaction']) && $_REQUEST['ajaxaction'] == 'validate') {
        $ajaxData = get_request('ajaxdata', array());
        foreach ($ajaxData as $check) {
            switch ($check['field']) {
                case 'port':
                    if (!validate_port_list($check['value'])) {
                        $ajaxResponse->error(_('Incorrect port range.'));
                    }
                    break;
                case 'itemKey':
                    $itemKey = new CItemKey($check['value']);
                    if (!$itemKey->isValid()) {
                        $ajaxResponse->error(_s('Incorrect key: "%1$s".', $itemKey->getError()));
                    }
                    break;
            }
        }
    }
    $ajaxResponse->send();
    require_once dirname(__FILE__) . '/include/page_footer.php';
    exit;
示例#3
0
function validate_condition($conditiontype, $value)
{
    global $USER_DETAILS;
    switch ($conditiontype) {
        case CONDITION_TYPE_HOST_GROUP:
            $available_groups = get_accessible_groups_by_user($USER_DETAILS, PERM_READ_ONLY, null, get_current_nodeid(true));
            if (!isset($available_groups[$value])) {
                error(S_INCORRECT_GROUP);
                return false;
            }
            break;
        case CONDITION_TYPE_HOST_TEMPLATE:
            $available_hosts = get_accessible_hosts_by_user($USER_DETAILS, PERM_READ_ONLY, null, get_current_nodeid(true));
            if (!isset($available_hosts[$value])) {
                error(S_INCORRECT_HOST);
                return false;
            }
            break;
        case CONDITION_TYPE_TRIGGER:
            if (!DBfetch(DBselect('select triggerid from triggers where triggerid=' . $value)) || !check_right_on_trigger_by_triggerid(PERM_READ_ONLY, $value)) {
                error(S_INCORRECT_TRIGGER);
                return false;
            }
            break;
        case CONDITION_TYPE_HOST:
            $available_hosts = get_accessible_hosts_by_user($USER_DETAILS, PERM_READ_ONLY, null, get_current_nodeid(true));
            if (!isset($available_hosts[$value])) {
                error(S_INCORRECT_HOST);
                return false;
            }
            break;
        case CONDITION_TYPE_TIME_PERIOD:
            if (!validate_period($value)) {
                error(S_INCORRECT_PERIOD . ' [' . $value . ']');
                return false;
            }
            break;
        case CONDITION_TYPE_DHOST_IP:
            if (!validate_ip_range($value)) {
                error(S_INCORRECT_IP . ' [' . $value . ']');
                return false;
            }
            break;
        case CONDITION_TYPE_DSERVICE_TYPE:
            if (S_UNKNOWN == discovery_check_type2str($value)) {
                error(S_INCORRECT_DISCOVERY_CHECK);
                return false;
            }
            break;
        case CONDITION_TYPE_DSERVICE_PORT:
            if (!validate_port_list($value)) {
                error(S_INCORRECT_PORT . ' [' . $value . ']');
                return false;
            }
            break;
        case CONDITION_TYPE_DSTATUS:
            if (S_UNKNOWN == discovery_object_status2str($value)) {
                error(S_INCORRECT_DISCOVERY_STATUS);
                return false;
            }
            break;
        case CONDITION_TYPE_EVENT_ACKNOWLEDGED:
            if (S_UNKNOWN == condition_value2str($conditiontype, $value)) {
                error(S_INCORRECT_DISCOVERY_STATUS);
                return false;
            }
            break;
        case CONDITION_TYPE_TRIGGER_NAME:
        case CONDITION_TYPE_TRIGGER_VALUE:
        case CONDITION_TYPE_TRIGGER_SEVERITY:
        case CONDITION_TYPE_MAINTENANCE:
        case CONDITION_TYPE_DUPTIME:
        case CONDITION_TYPE_DVALUE:
        case CONDITION_TYPE_APPLICATION:
            break;
        default:
            error(S_INCORRECT_CONDITION_TYPE);
            return false;
            break;
    }
    return true;
}
示例#4
0
 protected function validateDChecks(array &$dChecks)
 {
     $uniq = 0;
     foreach ($dChecks as $dcnum => $dCheck) {
         if (isset($dCheck['uniq']) && $dCheck['uniq'] == 1) {
             if (!in_array($dCheck['type'], array(SVC_AGENT, SVC_SNMPv1, SVC_SNMPv2c, SVC_SNMPv3))) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _('Only Zabbix agent, SNMPv1, SNMPv2 and SNMPv3 checks can be made unique.'));
             }
             $uniq++;
         }
         if (isset($dCheck['ports']) && !validate_port_list($dCheck['ports'])) {
             self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect port range.'));
         }
         switch ($dCheck['type']) {
             case SVC_AGENT:
                 if (!isset($dCheck['key_'])) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect key.'));
                 }
                 $itemKey = new CItemKey($dCheck['key_']);
                 if (!$itemKey->isValid()) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _s('Invalid key "%1$s": %2$s.', $dCheck['key_'], $itemKey->getError()));
                 }
                 break;
             case SVC_SNMPv1:
             case SVC_SNMPv2c:
                 if (!isset($dCheck['snmp_community']) || zbx_empty($dCheck['snmp_community'])) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect SNMP community.'));
                 }
             case SVC_SNMPv3:
                 if (!isset($dCheck['key_']) || zbx_empty($dCheck['key_'])) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect SNMP OID.'));
                 }
                 break;
         }
         // set default values for snmpv3 fields
         if (!isset($dCheck['snmpv3_securitylevel'])) {
             $dCheck['snmpv3_securitylevel'] = ITEM_SNMPV3_SECURITYLEVEL_NOAUTHNOPRIV;
         }
         switch ($dCheck['snmpv3_securitylevel']) {
             case ITEM_SNMPV3_SECURITYLEVEL_NOAUTHNOPRIV:
                 $dChecks[$dcnum]['snmpv3_authprotocol'] = ITEM_AUTHPROTOCOL_MD5;
                 $dChecks[$dcnum]['snmpv3_privprotocol'] = ITEM_PRIVPROTOCOL_DES;
                 $dChecks[$dcnum]['snmpv3_authpassphrase'] = $dChecks[$dcnum]['snmpv3_privpassphrase'] = '';
                 break;
             case ITEM_SNMPV3_SECURITYLEVEL_AUTHNOPRIV:
                 $dChecks[$dcnum]['snmpv3_privprotocol'] = ITEM_PRIVPROTOCOL_DES;
                 $dChecks[$dcnum]['snmpv3_privpassphrase'] = '';
                 break;
         }
         // validate snmpv3 fields
         if (isset($dCheck['snmpv3_securitylevel']) && $dCheck['snmpv3_securitylevel'] != ITEM_SNMPV3_SECURITYLEVEL_NOAUTHNOPRIV) {
             // snmpv3 authprotocol
             if (str_in_array($dCheck['snmpv3_securitylevel'], array(ITEM_SNMPV3_SECURITYLEVEL_AUTHNOPRIV, ITEM_SNMPV3_SECURITYLEVEL_AUTHPRIV))) {
                 if (zbx_empty($dCheck['snmpv3_authprotocol']) || isset($dCheck['snmpv3_authprotocol']) && !str_in_array($dCheck['snmpv3_authprotocol'], array(ITEM_AUTHPROTOCOL_MD5, ITEM_AUTHPROTOCOL_SHA))) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect authentication protocol for discovery rule "%1$s".', $dCheck['name']));
                 }
             }
             // snmpv3 privprotocol
             if ($dCheck['snmpv3_securitylevel'] == ITEM_SNMPV3_SECURITYLEVEL_AUTHPRIV) {
                 if (zbx_empty($dCheck['snmpv3_privprotocol']) || isset($dCheck['snmpv3_privprotocol']) && !str_in_array($dCheck['snmpv3_privprotocol'], array(ITEM_PRIVPROTOCOL_DES, ITEM_PRIVPROTOCOL_AES))) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect privacy protocol for discovery rule "%1$s".', $dCheck['name']));
                 }
             }
         }
         $this->validateDuplicateChecks($dChecks);
     }
     if ($uniq > 1) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Only one check can be unique.'));
     }
 }
 /**
  * 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();
 }
示例#6
0
 /**
  * Validate conditions.
  *
  * @static
  *
  * @param array $conditions
  * @param int   $conditions['conditiontype']
  * @param array $conditions['value']
  *
  * @return bool
  */
 public static function validateConditions($conditions, $update = false)
 {
     $conditions = zbx_toArray($conditions);
     $hostGroupIdsAll = array();
     $templateIdsAll = array();
     $triggerIdsAll = array();
     $hostIdsAll = array();
     $discoveryRuleIdsAll = array();
     $proxyIdsAll = array();
     $proxyidsAll = array();
     // build validators
     $timePeriodValidator = new CTimePeriodValidator();
     $discoveryCheckTypeValidator = new CSetValidator(array('values' => array_keys(discovery_check_type2str())));
     $discoveryObjectStatusValidator = new CSetValidator(array('values' => array_keys(discovery_object_status2str())));
     $triggerSeverityValidator = new CSetValidator(array('values' => array_keys(getSeverityCaption())));
     $discoveryObjectValidator = new CSetValidator(array('values' => array_keys(discovery_object2str())));
     $triggerValueValidator = new CSetValidator(array('values' => array_keys(trigger_value2str())));
     $eventTypeValidator = new CSetValidator(array('values' => array_keys(eventType())));
     foreach ($conditions as $condition) {
         // on create operator is mandatory and needs validation, but on update it must be validated only if it's set
         if (!$update || $update && isset($condition['operator'])) {
             $operatorValidator = new CSetValidator(array('values' => get_operators_by_conditiontype($condition['conditiontype'])));
             if (!$operatorValidator->validate($condition['operator'])) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition operator.'));
             }
         }
         if (!$update || $update && isset($condition['value'])) {
             // validate condition values depending on condition type
             switch ($condition['conditiontype']) {
                 case CONDITION_TYPE_HOST_GROUP:
                     if (!$condition['value']) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     }
                     $hostGroupIdsAll[$condition['value']] = $condition['value'];
                     break;
                 case CONDITION_TYPE_TEMPLATE:
                     if (!$condition['value']) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     }
                     $templateIdsAll[$condition['value']] = $condition['value'];
                     break;
                 case CONDITION_TYPE_TRIGGER:
                     if (!$condition['value']) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     }
                     $triggerIdsAll[$condition['value']] = $condition['value'];
                     break;
                 case CONDITION_TYPE_HOST:
                     if (!$condition['value']) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     }
                     $hostIdsAll[$condition['value']] = $condition['value'];
                     break;
                 case CONDITION_TYPE_DRULE:
                     if (!$condition['value']) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     }
                     $discoveryRuleIdsAll[$condition['value']] = $condition['value'];
                     break;
                 case CONDITION_TYPE_DCHECK:
                     if (!$condition['value']) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     }
                     $proxyIdsAll[$condition['value']] = $condition['value'];
                     break;
                 case CONDITION_TYPE_PROXY:
                     if (!$condition['value']) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     }
                     $proxyidsAll[$condition['value']] = $condition['value'];
                     break;
                 case CONDITION_TYPE_DOBJECT:
                     if (zbx_empty($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     } elseif (!$discoveryObjectValidator->validate($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition discovery object.'));
                     }
                     break;
                 case CONDITION_TYPE_TIME_PERIOD:
                     if (!$timePeriodValidator->validate($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, $timePeriodValidator->getError());
                     }
                     break;
                 case CONDITION_TYPE_DHOST_IP:
                     if (zbx_empty($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     } else {
                         if (!validate_ip_range($condition['value'])) {
                             self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect action condition ip "%1$s".', $condition['value']));
                         }
                     }
                     break;
                 case CONDITION_TYPE_DSERVICE_TYPE:
                     if (zbx_empty($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     } elseif (!$discoveryCheckTypeValidator->validate($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition discovery check.'));
                     }
                     break;
                 case CONDITION_TYPE_DSERVICE_PORT:
                     if (zbx_empty($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     } elseif (!validate_port_list($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect action condition port "%1$s".', $condition['value']));
                     }
                     break;
                 case CONDITION_TYPE_DSTATUS:
                     if (zbx_empty($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     } elseif (!$discoveryObjectStatusValidator->validate($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition discovery status.'));
                     }
                     break;
                 case CONDITION_TYPE_MAINTENANCE:
                     if (!zbx_empty($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Maintenance action condition value must be empty.'));
                     }
                     break;
                 case CONDITION_TYPE_TRIGGER_SEVERITY:
                     if (zbx_empty($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     } elseif (!$triggerSeverityValidator->validate($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition trigger severity.'));
                     }
                     break;
                 case CONDITION_TYPE_TRIGGER_VALUE:
                     if (zbx_empty($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     } elseif (!$triggerValueValidator->validate($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition trigger value.'));
                     }
                     break;
                 case CONDITION_TYPE_EVENT_TYPE:
                     if (zbx_empty($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     } elseif (!$eventTypeValidator->validate($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition event type.'));
                     }
                     break;
                 case CONDITION_TYPE_TRIGGER_NAME:
                 case CONDITION_TYPE_NODE:
                 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($condition['value'])) {
                         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                     }
                     break;
                 default:
                     self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition type.'));
                     break;
             }
         }
     }
     if (!API::HostGroup()->isWritable($hostGroupIdsAll)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition host group. Host group does not exist or you have no access to it.'));
     }
     if (!API::Host()->isWritable($hostIdsAll)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition host. Host does not exist or you have no access to it.'));
     }
     if (!API::Template()->isWritable($templateIdsAll)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition template. Template does not exist or you have no access to it.'));
     }
     if (!API::Trigger()->isWritable($triggerIdsAll)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition trigger. Trigger does not exist or you have no access to it.'));
     }
     if (!API::DRule()->isWritable($discoveryRuleIdsAll)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition discovery rule. Discovery rule does not exist or you have no access to it.'));
     }
     if (!API::DCheck()->isWritable($proxyIdsAll)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition discovery check. Discovery check does not exist or you have no access to it.'));
     }
     if (!API::Proxy()->isWritable($proxyidsAll)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition proxy. Proxy does not exist or you have no access to it.'));
     }
     return true;
 }
示例#7
0
function validate_condition($conditiontype, $value)
{
    global $USER_DETAILS;
    switch ($conditiontype) {
        case CONDITION_TYPE_HOST_GROUP:
            $groups = CHostGroup::get(array('groupids' => $value, 'output' => API_OUTPUT_SHORTEN, 'nodeids' => get_current_nodeid(true)));
            if (empty($groups)) {
                error(S_INCORRECT_GROUP);
                return false;
            }
            break;
        case CONDITION_TYPE_HOST_TEMPLATE:
            $templates = CTemplate::get(array('templateids' => $value, 'output' => API_OUTPUT_SHORTEN, 'nodeids' => get_current_nodeid(true)));
            if (empty($templates)) {
                error(S_INCORRECT_HOST);
                return false;
            }
            break;
        case CONDITION_TYPE_TRIGGER:
            $triggers = CTrigger::get(array('triggerids' => $value, 'output' => API_OUTPUT_SHORTEN, 'nodeids' => get_current_nodeid(true)));
            if (empty($triggers)) {
                error(S_INCORRECT_TRIGGER);
                return false;
            }
            break;
        case CONDITION_TYPE_HOST:
            $hosts = CHost::get(array('hostids' => $value, 'output' => API_OUTPUT_SHORTEN, 'nodeids' => get_current_nodeid(true)));
            if (empty($hosts)) {
                error(S_INCORRECT_HOST);
                return false;
            }
            break;
        case CONDITION_TYPE_TIME_PERIOD:
            if (!validate_period($value)) {
                error(S_INCORRECT_PERIOD . ' [' . $value . ']');
                return false;
            }
            break;
        case CONDITION_TYPE_DHOST_IP:
            if (!validate_ip_range($value)) {
                error(S_INCORRECT_IP . ' [' . $value . ']');
                return false;
            }
            break;
        case CONDITION_TYPE_DSERVICE_TYPE:
            if (S_UNKNOWN == discovery_check_type2str($value)) {
                error(S_INCORRECT_DISCOVERY_CHECK);
                return false;
            }
            break;
        case CONDITION_TYPE_DSERVICE_PORT:
            if (!validate_port_list($value)) {
                error(S_INCORRECT_PORT . ' [' . $value . ']');
                return false;
            }
            break;
        case CONDITION_TYPE_DSTATUS:
            if (S_UNKNOWN == discovery_object_status2str($value)) {
                error(S_INCORRECT_DISCOVERY_STATUS);
                return false;
            }
            break;
        case CONDITION_TYPE_EVENT_ACKNOWLEDGED:
            if (S_UNKNOWN == condition_value2str($conditiontype, $value)) {
                error(S_INCORRECT_DISCOVERY_STATUS);
                return false;
            }
            break;
        case CONDITION_TYPE_TRIGGER_NAME:
        case CONDITION_TYPE_TRIGGER_VALUE:
        case CONDITION_TYPE_TRIGGER_SEVERITY:
        case CONDITION_TYPE_MAINTENANCE:
        case CONDITION_TYPE_NODE:
        case CONDITION_TYPE_DRULE:
        case CONDITION_TYPE_DCHECK:
        case CONDITION_TYPE_DOBJECT:
        case CONDITION_TYPE_PROXY:
        case CONDITION_TYPE_DUPTIME:
        case CONDITION_TYPE_DVALUE:
        case CONDITION_TYPE_APPLICATION:
        case CONDITION_TYPE_HOST_NAME:
            break;
        default:
            error(S_INCORRECT_CONDITION_TYPE);
            return false;
            break;
    }
    return true;
}
 /**
  * Validate conditions.
  *
  * @static
  * @param $conditions
  * @return bool
  */
 public static function validateConditions($conditions)
 {
     $conditions = zbx_toArray($conditions);
     $hostGroupidsAll = array();
     $templateidsAll = array();
     $triggeridsAll = array();
     $hostidsAll = array();
     $discoveryRuleidsAll = array();
     $discoveryCheckidsAll = array();
     $proxyidsAll = array();
     $discoveryCheckTypes = discovery_check_type2str();
     $discoveryObjectStatuses = discovery_object_status2str();
     $timePeriodValidator = new CTimePeriodValidator();
     foreach ($conditions as $condition) {
         switch ($condition['conditiontype']) {
             case CONDITION_TYPE_HOST_GROUP:
                 $hostGroupidsAll[$condition['value']] = $condition['value'];
                 break;
             case CONDITION_TYPE_HOST_TEMPLATE:
                 $templateidsAll[$condition['value']] = $condition['value'];
                 break;
             case CONDITION_TYPE_TRIGGER:
                 $triggeridsAll[$condition['value']] = $condition['value'];
                 break;
             case CONDITION_TYPE_HOST:
                 $hostidsAll[$condition['value']] = $condition['value'];
                 break;
             case CONDITION_TYPE_DRULE:
                 $discoveryRuleidsAll[$condition['value']] = $condition['value'];
                 break;
             case CONDITION_TYPE_DCHECK:
                 $discoveryCheckidsAll[$condition['value']] = $condition['value'];
                 break;
             case CONDITION_TYPE_PROXY:
                 $proxyidsAll[$condition['value']] = $condition['value'];
                 break;
             case CONDITION_TYPE_TIME_PERIOD:
                 if (!$timePeriodValidator->validate($condition['value'])) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect action condition period "%s".', $condition['value']));
                 }
                 break;
             case CONDITION_TYPE_DHOST_IP:
                 if (!validate_ip_range($condition['value'])) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect action condition ip "%s".', $condition['value']));
                 }
                 break;
             case CONDITION_TYPE_DSERVICE_TYPE:
                 if (!isset($discoveryCheckTypes[$condition['value']])) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition discovery check.'));
                 }
                 break;
             case CONDITION_TYPE_DSERVICE_PORT:
                 if (!validate_port_list($condition['value'])) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect action condition port "%s".', $condition['value']));
                 }
                 break;
             case CONDITION_TYPE_DSTATUS:
                 if (!isset($discoveryObjectStatuses[$condition['value']])) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition discovery status.'));
                 }
                 break;
             case CONDITION_TYPE_MAINTENANCE:
                 // maintenance condition has no value...
                 break;
             case CONDITION_TYPE_TRIGGER_NAME:
             case CONDITION_TYPE_TRIGGER_VALUE:
             case CONDITION_TYPE_TRIGGER_SEVERITY:
             case CONDITION_TYPE_NODE:
             case CONDITION_TYPE_DOBJECT:
             case CONDITION_TYPE_DUPTIME:
             case CONDITION_TYPE_DVALUE:
             case CONDITION_TYPE_APPLICATION:
             case CONDITION_TYPE_HOST_NAME:
                 if (zbx_empty($condition['value'])) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty action condition.'));
                 }
                 break;
             default:
                 self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition type.'));
                 break;
         }
     }
     if (!API::HostGroup()->isWritable($hostGroupidsAll)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition host group. Host group does not exist or you have no access to it.'));
     }
     if (!API::Host()->isWritable($hostidsAll)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition host. Host does not exist or you have no access to it.'));
     }
     if (!API::Template()->isWritable($templateidsAll)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition template. Template does not exist or you have no access to it.'));
     }
     if (!API::Trigger()->isWritable($triggeridsAll)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition trigger. Trigger does not exist or you have no access to it.'));
     }
     if (!API::DRule()->isWritable($discoveryRuleidsAll)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition discovery rule. Discovery rule does not exist or you have no access to it.'));
     }
     if (!API::DCheck()->isWritable($discoveryCheckidsAll)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition discovery check. Discovery check does not exist or you have no access to it.'));
     }
     if (!API::Proxy()->isWritable($proxyidsAll)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect action condition proxy. Proxy does not exist or you have no access to it.'));
     }
     return true;
 }