function update_config($configs) { $update = array(); if (isset($configs['work_period'])) { $timePeriodValidator = new CTimePeriodValidator(); if (!$timePeriodValidator->validate($configs['work_period'])) { error(_('Incorrect working time.')); return false; } } if (isset($configs['alert_usrgrpid'])) { if ($configs['alert_usrgrpid'] != 0 && !DBfetch(DBselect('SELECT u.usrgrpid FROM usrgrp u WHERE u.usrgrpid=' . zbx_dbstr($configs['alert_usrgrpid'])))) { error(_('Incorrect user group.')); return false; } } if (isset($configs['discovery_groupid'])) { $groupid = API::HostGroup()->get(array('groupids' => $configs['discovery_groupid'], 'output' => array('groupid'), 'preservekeys' => true)); if (empty($groupid)) { error(_('Incorrect host group.')); return false; } } // checking color values to be correct hexadecimal numbers $colors = array('severity_color_0', 'severity_color_1', 'severity_color_2', 'severity_color_3', 'severity_color_4', 'severity_color_5', 'problem_unack_color', 'problem_ack_color', 'ok_unack_color', 'ok_ack_color'); $colorvalidator = new CColorValidator(); foreach ($colors as $color) { if (isset($configs[$color]) && !is_null($configs[$color])) { if (!$colorvalidator->validate($configs[$color])) { error($colorvalidator->getError()); return false; } } } if (isset($configs['ok_period']) && !is_null($configs['ok_period']) && !ctype_digit($configs['ok_period'])) { error(_('"Display OK triggers" needs to be "0" or a positive integer.')); return false; } if (isset($configs['blink_period']) && !is_null($configs['blink_period']) && !ctype_digit($configs['blink_period'])) { error(_('"Triggers blink on status change" needs to be "0" or a positive integer.')); return false; } $currentConfig = select_config(); // check duplicate severity names and if name is empty. $names = array(); for ($i = 0; $i < TRIGGER_SEVERITY_COUNT; $i++) { $varName = 'severity_name_' . $i; if (!isset($configs[$varName]) || is_null($configs[$varName])) { $configs[$varName] = $currentConfig[$varName]; } if ($configs[$varName] == '') { error(_('Severity name cannot be empty.')); return false; } if (isset($names[$configs[$varName]])) { error(_s('Duplicate severity name "%s".', $configs[$varName])); return false; } else { $names[$configs[$varName]] = 1; } } foreach ($configs as $key => $value) { if (!is_null($value)) { if ($key == 'alert_usrgrpid') { $update[] = $key . '=' . ($value == '0' ? 'NULL' : $value); } else { $update[] = $key . '=' . zbx_dbstr($value); } } } if (count($update) == 0) { error(_('Nothing to do.')); return null; } return DBexecute('UPDATE config' . ' SET ' . implode(',', $update) . whereDbNode('configid', false)); }
/** * Validate periods array for bar reports - time since, time till and color. * Automatically set caption if none is set. * * @param array $periods * * @return mixed valid periods array on success or false on failure */ function validateBarReportPeriods($periods = array()) { if (!isset($periods) || !$periods) { return false; } $fields = array('report_timesince', 'report_timetill', 'color'); $colorValidator = new CColorValidator(); foreach ($periods as &$period) { foreach ($fields as $field) { if (!isset($period[$field]) || !$period[$field]) { show_error_message(_s('Missing "%1$s" field for period.', $field)); return false; } } if (!$colorValidator->validate($period['color'])) { show_error_message($colorValidator->getError()); return false; } if (!validateUnixTime($period['report_timesince'])) { show_error_message(_s('Invalid period for field "%1$s".', 'report_timesince')); return false; } if (!validateUnixTime($period['report_timetill'])) { show_error_message(_s('Invalid period for field "%1$s".', 'report_timetill')); return false; } if (!isset($period['caption']) || zbx_empty($period['caption'])) { $period['caption'] = zbx_date2str(REPORTS_BAR_REPORT_DATE_FORMAT, $period['report_timesince']) . ' - ' . zbx_date2str(REPORTS_BAR_REPORT_DATE_FORMAT, $period['report_timetill']); } } unset($period); return $periods; }
protected function validateUpdateLinkTriggers(array $linkTriggers) { $linkTriggerDbFields = array('linktriggerid' => null); $colorValidator = new CColorValidator(); foreach ($linkTriggers as $linkTrigger) { if (!check_db_fields($linkTriggerDbFields, $linkTrigger)) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Wrong fields for linktrigger update.')); } if (isset($linkTrigger['color']) && !$colorValidator->validate($linkTrigger['color'])) { self::exception(ZBX_API_ERROR_PARAMETERS, $colorValidator->getError()); } } }
function check_type(&$field, $flags, &$var, $type, $caption = null) { if ($caption === null) { $caption = $field; } if (is_array($var) && $type != T_ZBX_IP) { $err = ZBX_VALID_OK; foreach ($var as $v) { $err |= check_type($field, $flags, $v, $type); } return $err; } $error = false; $message = ''; if ($type == T_ZBX_IP) { if (!validate_ip($var, $arr)) { $error = true; $message = _s('Field "%1$s" is not IP.', $caption); } } elseif ($type == T_ZBX_IP_RANGE) { if (!validate_ip_range($var)) { $error = true; $message = _s('Field "%1$s" is not IP range.', $caption); } } elseif ($type == T_ZBX_INT_RANGE) { if (!is_int_range($var)) { $error = true; $message = _s('Field "%1$s" is not integer list or range.', $caption); } } elseif ($type == T_ZBX_INT) { if (!zbx_is_int($var)) { $error = true; $message = _s('Field "%1$s" is not integer.', $caption); } } elseif ($type == T_ZBX_DBL) { $decimalValidator = new CDecimalValidator(array('maxPrecision' => 16, 'maxScale' => 4, 'messageInvalid' => _('Value "%2$s" of "%1$s" has incorrect decimal format.'), 'messagePrecision' => _('Value "%2$s" of "%1$s" is too long: it cannot have more than %3$s digits before the decimal point ' . 'and more than %4$s digits after the decimal point.'), 'messageNatural' => _('Value "%2$s" of "%1$s" has too many digits before the decimal point: ' . 'it cannot have more than %3$s digits.'), 'messageScale' => _('Value "%2$s" of "%1$s" has too many digits after the decimal point: ' . 'it cannot have more than %3$s digits.'))); $decimalValidator->setObjectName($caption); if (!$decimalValidator->validate($var)) { $error = true; $message = $decimalValidator->getError(); } } elseif ($type == T_ZBX_DBL_BIG) { $decimalValidator = new CDecimalValidator(array('maxScale' => 4, 'messageInvalid' => _('Value "%2$s" of "%1$s" has incorrect decimal format.'), 'messageScale' => _('Value "%2$s" of "%1$s" has too many digits after the decimal point: ' . 'it cannot have more than %3$s digits.'))); $decimalValidator->setObjectName($caption); if (!$decimalValidator->validate($var)) { $error = true; $message = $decimalValidator->getError(); } } elseif ($type == T_ZBX_DBL_STR) { $decimalStringValidator = new CDecimalStringValidator(array('messageInvalid' => _('Value "%2$s" of "%1$s" has incorrect decimal format.'))); $decimalStringValidator->setObjectName($caption); if (!$decimalStringValidator->validate($var)) { $error = true; $message = $decimalStringValidator->getError(); } } elseif ($type == T_ZBX_STR) { if (!is_string($var)) { $error = true; $message = _s('Field "%1$s" is not string.', $caption); } } elseif ($type == T_ZBX_CLR) { $colorValidator = new CColorValidator(); if (!$colorValidator->validate($var)) { $var = 'FFFFFF'; $error = true; $message = _s('Colour "%1$s" is not correct: expecting hexadecimal colour code (6 symbols).', $caption); } } if ($error) { if ($flags & P_SYS) { error($message); return ZBX_VALID_ERROR; } else { info($message); return ZBX_VALID_WARNING; } } return ZBX_VALID_OK; }
/** * Validate graph general data on Update method. * When updating graph check to what host graph belongs to and trow an error if new items added from other hosts. * Includes Y axis validation and if graph already exists somewhere in DB. * * @param array $graphs * @param array $dbGraphs */ protected function validateUpdate(array $graphs, array $dbGraphs) { $colorValidator = new CColorValidator(); foreach ($graphs as $graph) { // check for "templateid", because it is not allowed if (array_key_exists('templateid', $graph)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s($this->getErrorMsg(self::ERROR_TEMPLATED_ID), $graph['name'])); } $templatedGraph = false; if (isset($graph['gitems'])) { // first item determines to which host graph belongs to $gitem = array_shift($dbGraphs[$graph['graphid']]['gitems']); $graphHosts = API::Host()->get(array('itemids' => $gitem['itemid'], 'output' => array('hostid', 'status'), 'editable' => true, 'templated_hosts' => true)); $host = array_shift($graphHosts); // if the current graph is templated and new items to be added if (HOST_STATUS_TEMPLATE == $host['status']) { $templatedGraph = $host['hostid']; $itemIds = array(); foreach ($graph['gitems'] as $gitem) { if (!isset($gitem['gitemid']) && isset($gitem['itemid'])) { $itemIds[] = $gitem['itemid']; } } if ($itemIds) { $itemHosts = API::Host()->get(array('itemids' => $itemIds, 'output' => array('hostid'), 'editable' => true, 'templated_hosts' => true)); // only one host is allowed and it has to be the current. other templated hosts are allowed $itemHosts = array_unique(zbx_objectValues($itemHosts, 'hostid')); if (count($itemHosts) > 1 || !in_array($host['hostid'], $itemHosts)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s($this->getErrorMsg(self::ERROR_TEMPLATE_HOST_MIX), $graph['name'])); } } } // items fields foreach ($graph['gitems'] as $gitem) { // check color if (isset($gitem['color']) && !$colorValidator->validate($gitem['color'])) { self::exception(ZBX_API_ERROR_PARAMETERS, $colorValidator->getError()); } } } // check ymin, ymax items $this->checkAxisItems($graph, $templatedGraph); } $this->validateHostsAndTemplates($graphs); }
function check_type(&$field, $flags, &$var, $type, $caption = null) { if (is_null($caption)) { $caption = $field; } if (is_array($var) && $type != T_ZBX_IP) { $err = ZBX_VALID_OK; foreach ($var as $v) { $err |= check_type($field, $flags, $v, $type); } return $err; } if ($type == T_ZBX_IP) { if (!validate_ip($var, $arr)) { info(_s('Field "%1$s" is not IP.', $caption)); return $flags & P_SYS ? ZBX_VALID_ERROR : ZBX_VALID_WARNING; } return ZBX_VALID_OK; } if ($type == T_ZBX_IP_RANGE) { if (!validate_ip_range($var)) { info(_s('Field "%1$s" is not IP range.', $caption)); return $flags & P_SYS ? ZBX_VALID_ERROR : ZBX_VALID_WARNING; } return ZBX_VALID_OK; } if ($type == T_ZBX_INT_RANGE) { if (!is_int_range($var)) { info(_s('Field "%1$s" is not integer list or range.', $caption)); return $flags & P_SYS ? ZBX_VALID_ERROR : ZBX_VALID_WARNING; } return ZBX_VALID_OK; } if ($type == T_ZBX_INT && !zbx_is_int($var)) { info(_s('Field "%1$s" is not integer.', $caption)); return $flags & P_SYS ? ZBX_VALID_ERROR : ZBX_VALID_WARNING; } if ($type == T_ZBX_DBL && !is_numeric($var)) { info(_s('Field "%1$s" is not decimal number.', $caption)); return $flags & P_SYS ? ZBX_VALID_ERROR : ZBX_VALID_WARNING; } if ($type == T_ZBX_STR && !is_string($var)) { info(_s('Field "%1$s" is not string.', $caption)); return $flags & P_SYS ? ZBX_VALID_ERROR : ZBX_VALID_WARNING; } if ($type == T_ZBX_CLR) { $colorValidator = new CColorValidator(); if (!$colorValidator->validate($var)) { $var = 'FFFFFF'; info(_s('Colour "%1$s" is not correct: expecting hexadecimal colour code (6 symbols).', $caption)); return $flags & P_SYS ? ZBX_VALID_ERROR : ZBX_VALID_WARNING; } } return ZBX_VALID_OK; }