/** * Returns a column object for the given row and field. Add additional service tree related formatting. * * @param $rowId * @param $colName * * @return CCol */ protected function makeCol($rowId, $colName) { $class = null; if ($colName == 'status' && zbx_is_int($this->tree[$rowId][$colName]) && $this->tree[$rowId]['id'] > 0) { $status = $this->tree[$rowId][$colName]; // do not show the severity for information and unclassified triggers if (in_array($status, array(TRIGGER_SEVERITY_INFORMATION, TRIGGER_SEVERITY_NOT_CLASSIFIED))) { $this->tree[$rowId][$colName] = new CSpan(_('OK'), 'green'); } else { $this->tree[$rowId][$colName] = getSeverityCaption($status); $class = getSeverityStyle($status); } } $col = parent::makeCol($rowId, $colName); $col->addClass($class); return $col; }
public static function checkValueTypes($table, &$values) { global $DB; $tableSchema = self::getSchema($table); foreach ($values as $field => $value) { if (!isset($tableSchema['fields'][$field])) { unset($values[$field]); continue; } if (isset($tableSchema['fields'][$field]['ref_table'])) { if ($tableSchema['fields'][$field]['null']) { $values[$field] = $values[$field] == '0' ? NULL : $values[$field]; } } if (is_null($values[$field])) { if ($tableSchema['fields'][$field]['null']) { $values[$field] = 'NULL'; } elseif (isset($tableSchema['fields'][$field]['default'])) { $values[$field] = zbx_dbstr($tableSchema['fields'][$field]['default']); } else { self::exception(self::DBEXECUTE_ERROR, _s('Field "%1$s" cannot be set to NULL.', $field)); } } else { switch ($tableSchema['fields'][$field]['type']) { case self::FIELD_TYPE_CHAR: $length = mb_strlen($values[$field]); $values[$field] = zbx_dbstr($values[$field]); if ($length > $tableSchema['fields'][$field]['length']) { self::exception(self::SCHEMA_ERROR, _s('Value "%1$s" is too long for field "%2$s" - %3$d characters. Allowed length is %4$d characters.', $values[$field], $field, $length, $tableSchema['fields'][$field]['length'])); } break; case self::FIELD_TYPE_ID: case self::FIELD_TYPE_UINT: if (!zbx_ctype_digit($values[$field])) { self::exception(self::DBEXECUTE_ERROR, _s('Incorrect value "%1$s" for unsigned int field "%2$s".', $values[$field], $field)); } $values[$field] = zbx_dbstr($values[$field]); break; case self::FIELD_TYPE_INT: if (!zbx_is_int($values[$field])) { self::exception(self::DBEXECUTE_ERROR, _s('Incorrect value "%1$s" for int field "%2$s".', $values[$field], $field)); } $values[$field] = zbx_dbstr($values[$field]); break; case self::FIELD_TYPE_FLOAT: if (!is_numeric($values[$field])) { self::exception(self::DBEXECUTE_ERROR, _s('Incorrect value "%1$s" for float field "%2$s".', $values[$field], $field)); } $values[$field] = zbx_dbstr($values[$field]); break; case self::FIELD_TYPE_TEXT: $length = mb_strlen($values[$field]); $values[$field] = zbx_dbstr($values[$field]); if ($DB['TYPE'] == ZBX_DB_DB2 || $DB['TYPE'] == ZBX_DB_ORACLE) { if ($length > 2048) { self::exception(self::SCHEMA_ERROR, _s('Value "%1$s" is too long for field "%2$s" - %3$d characters. Allowed length is 2048 characters.', $values[$field], $field, $length)); } } break; } } } }
/** * Checks that the row and column spans are valid. * * @throws APIException if the any of the spans is not an integer or missing * * @param array $screenItem * @param array $screen */ protected function checkSpans(array $screenItem, array $screen) { if (isset($screenItem['rowspan'])) { if (!zbx_is_int($screenItem['rowspan']) || $screenItem['rowspan'] < 0) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Screen "%1$s" row span in cell X - %2$s Y - %3$s is incorrect.', $screen['name'], $screenItem['x'], $screenItem['y'])); } } if (isset($screenItem['colspan'])) { if (!zbx_is_int($screenItem['colspan']) || $screenItem['colspan'] < 0) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Screen "%1$s" column span in cell X - %2$s Y - %3$s is incorrect.', $screen['name'], $screenItem['x'], $screenItem['y'])); } } }
/** * Validate the new service time. Validation is implemented as a separate function to be available directly from the * frontend. * * @throws APIException if the given service time is invalid * * @param array $serviceTime * * @return void */ function checkServiceTime(array $serviceTime) { // type validation $serviceTypes = array(SERVICE_TIME_TYPE_DOWNTIME, SERVICE_TIME_TYPE_ONETIME_DOWNTIME, SERVICE_TIME_TYPE_UPTIME); if (!isset($serviceTime['type']) || !in_array($serviceTime['type'], $serviceTypes)) { throw new APIException(ZBX_API_ERROR_PARAMETERS, _('Incorrect service time type.')); } // one-time downtime validation if ($serviceTime['type'] == SERVICE_TIME_TYPE_ONETIME_DOWNTIME) { if (!isset($serviceTime['ts_from']) || !validateUnixTime($serviceTime['ts_from'])) { throw new APIException(ZBX_API_ERROR_PARAMETERS, _('Incorrect service start time.')); } if (!isset($serviceTime['ts_to']) || !validateUnixTime($serviceTime['ts_to'])) { throw new APIException(ZBX_API_ERROR_PARAMETERS, _('Incorrect service end time.')); } } else { if (!isset($serviceTime['ts_from']) || !zbx_is_int($serviceTime['ts_from']) || $serviceTime['ts_from'] < 0 || $serviceTime['ts_from'] > SEC_PER_WEEK) { throw new APIException(ZBX_API_ERROR_PARAMETERS, _('Incorrect service start time.')); } if (!isset($serviceTime['ts_to']) || !zbx_is_int($serviceTime['ts_to']) || $serviceTime['ts_to'] < 0 || $serviceTime['ts_to'] > SEC_PER_WEEK) { throw new APIException(ZBX_API_ERROR_PARAMETERS, _('Incorrect service end time.')); } } if ($serviceTime['ts_from'] >= $serviceTime['ts_to']) { throw new APIException(ZBX_API_ERROR_PARAMETERS, _('Service start time must be less than end time.')); } }
private static function checkValueType($value, $type) { switch ($type) { case PROFILE_TYPE_ID: return zbx_ctype_digit($value); case PROFILE_TYPE_INT: return zbx_is_int($value); case PROFILE_TYPE_STR: return zbx_strlen($value) <= self::$stringProfileMaxLength; default: return true; } }
function validateNumber($value, $min = null, $max = null) { if (!zbx_is_int($value)) { return false; } if ($min !== null && $value < $min) { return false; } if ($max !== null && $value > $max) { return false; } return true; }
/** * Validates the "status" field. Assumes the "name" field is valid. * * @throws APIException if the value is incorrect * * @param array $service * * @return void */ protected function checkStatus(array $service) { if (!empty($service['status']) && !zbx_is_int($service['status'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect status for service "%1$s".', $service['name'])); } }
/** * Check value map mappings. * 1. check if at least one is defined * 2. check if value is numeric * 3. check if mappend value is not empty string * 4. check for duplicate values * * @param array $mappings * * @throws Exception */ function checkValueMapMappings(array $mappings) { if (empty($mappings)) { throw new Exception(_('Value mapping must have at least one mapping.')); } foreach ($mappings as $mapping) { if (!zbx_is_int($mapping['value'])) { throw new Exception(_('Value maps are used to create a mapping between numeric values and string representations.')); } if (zbx_empty($mapping['newvalue'])) { throw new Exception(_('Value cannot be mapped to empty string.')); } } $valueCount = array_count_values(zbx_objectValues($mappings, 'value')); foreach ($valueCount as $value => $count) { if ($count > 1) { throw new Exception(_s('Mapping value "%1$s" is not unique.', $value)); } } }
/** * Convert graph elements. * * @param array $graphs * * @return array */ protected function convertGraphs(array $graphs) { foreach ($graphs as &$graph) { $graph['graph_items'] = $this->convertGraphItems($graph['graph_items']); if (zbx_is_int($graph['ymin_type_1'])) { if ($graph['ymin_type_1'] == GRAPH_YAXIS_TYPE_ITEM_VALUE) { $graph['ymin_item_1']['key'] = $this->itemKeyConverter->convert($graph['ymin_item_1']['key']); } } if (zbx_is_int($graph['ymax_type_1'])) { if ($graph['ymax_type_1'] == GRAPH_YAXIS_TYPE_ITEM_VALUE) { $graph['ymax_item_1']['key'] = $this->itemKeyConverter->convert($graph['ymax_item_1']['key']); } } } unset($graph); return $graphs; }
/** * Checks that the row and column spans are valid. * * @throws APIException if the any of the spans is not an integer or missing * * @param array $screenItem * * @return void */ protected function checkSpans(array $screenItem) { if (zbx_empty($screenItem['rowspan']) || !zbx_is_int($screenItem['rowspan'])) { self::exception(ZBX_API_ERROR_PERMISSIONS, _s('Incorrect row span provided for screen element located at X - %1$s and Y - %2$s.', $screenItem['x'], $screenItem['y'])); } if (zbx_empty($screenItem['colspan']) || !zbx_is_int($screenItem['colspan'])) { self::exception(ZBX_API_ERROR_PERMISSIONS, _s('Incorrect column span provided for screen element located at X - %1$s and Y - %2$s.', $screenItem['x'], $screenItem['y'])); } }
private static function checkValueType($value, $type) { switch ($type) { case PROFILE_TYPE_ID: $result = zbx_ctype_digit($value); break; case PROFILE_TYPE_INT: $result = zbx_is_int($value); break; default: $result = true; } return $result; }
function check_type(&$field, $flags, &$var, $type) { if (is_array($var) && $type != T_ZBX_IP) { $err = ZBX_VALID_OK; foreach ($var as $el) { $err |= check_type($field, $flags, $el, $type); } return $err; } if ($type == T_ZBX_IP) { if (!validate_ip($var, $arr)) { if ($flags & P_SYS) { info('Critical error. Field [' . $field . '] is not IP'); return ZBX_VALID_ERROR; } else { info('Warning. Field [' . $field . '] is not IP'); return ZBX_VALID_WARNING; } } return ZBX_VALID_OK; } if ($type == T_ZBX_IP_RANGE) { if (!validate_ip_range($var)) { if ($flags & P_SYS) { info(S_CRITICAL_ERROR . '.' . SPACE . S_FIELD . SPACE . '[' . $field . ']' . SPACE . S_IS_NOT_IP_RANGE_SMALL); return ZBX_VALID_ERROR; } else { info(S_WARNING . '.' . SPACE . S_FIELD . SPACE . '[' . $field . ']' . SPACE . S_IS_NOT_IP_RANGE_SMALL); return ZBX_VALID_WARNING; } } return ZBX_VALID_OK; } if ($type == T_ZBX_PORTS) { $err = ZBX_VALID_OK; $type = $flags & P_SYS ? ZBX_VALID_ERROR : ZBX_VALID_WARNING; foreach (explode(',', $var) as $el) { foreach (explode('-', $el) as $p) { $err |= check_type($field, $flags, $p, T_ZBX_INT); if ($p > 65535 || $p < 0) { $err |= $type; } } } if ($err == ZBX_VALID_ERROR) { info(S_CRITICAL_ERROR . '.' . SPACE . S_FIELD . SPACE . '[' . $field . ']' . SPACE . S_IS_NOT_PORT_RANGE_SMALL); } else { if ($err == ZBX_VALID_WARNING) { info(S_WARNING . '.' . SPACE . S_FIELD . SPACE . '[' . $field . ']' . SPACE . S_IS_NOT_PORT_RANGE_SMALL); } } return $err; } if ($type == T_ZBX_INT_RANGE) { if (!is_int_range($var)) { if ($flags & P_SYS) { info(S_CRITICAL_ERROR . '.' . SPACE . S_FIELD . SPACE . '[' . $field . ']' . SPACE . S_IS_NOT_INTEGER_RANGE_SMALL); return ZBX_VALID_ERROR; } else { info(S_WARNING . '.' . SPACE . S_FIELD . SPACE . '[' . $field . ']' . SPACE . S_IS_NOT_INTEGER_RANGE_SMALL); return ZBX_VALID_WARNING; } } return ZBX_VALID_OK; } if ($type == T_ZBX_INT && !zbx_is_int($var)) { if ($flags & P_SYS) { info(S_CRITICAL_ERROR . '.' . SPACE . S_FIELD . SPACE . '[' . $field . ']' . SPACE . S_IS_NOT_INTEGER_SMALL); return ZBX_VALID_ERROR; } else { info(S_WARNING . '.' . SPACE . S_FIELD . SPACE . '[' . $field . ']' . SPACE . S_IS_NOT_INTEGER_SMALL); return ZBX_VALID_WARNING; } } if ($type == T_ZBX_DBL && !is_numeric($var)) { if ($flags & P_SYS) { info(S_CRITICAL_ERROR . '.' . SPACE . S_FIELD . SPACE . '[' . $field . ']' . SPACE . S_IS_NOT_DOUBLE_SMALL); return ZBX_VALID_ERROR; } else { info(S_WARNING . '.' . SPACE . S_FIELD . SPACE . '[' . $field . ']' . SPACE . S_IS_NOT_DOUBLE_SMALL); return ZBX_VALID_WARNING; } } if ($type == T_ZBX_STR && !is_string($var)) { if ($flags & P_SYS) { info(S_CRITICAL_ERROR . '.' . SPACE . S_FIELD . SPACE . '[' . $field . ']' . SPACE . S_IS_NOT_STRING_SMALL); return ZBX_VALID_ERROR; } else { info(S_WARNING . '.' . SPACE . S_FIELD . SPACE . '[' . $field . ']' . SPACE . S_IS_NOT_STRING_SMALL); return ZBX_VALID_WARNING; } } //* if ($type == T_ZBX_STR && !defined('ZBX_ALLOW_UNICODE') && zbx_strlen($var) != zbx_strlen($var)) { if ($flags & P_SYS) { info(S_CRITICAL_ERROR . '.' . SPACE . S_FIELD . SPACE . '[' . $field . ']' . SPACE . S_CONTAINS_MULTIBYTE_CHARS_SMALL); return ZBX_VALID_ERROR; } else { info(S_WARNING . '.' . SPACE . S_FIELD . SPACE . '[' . $field . '] - ' . S_MULTIBYTE_CHARS_ARE_RESTRICTED_SMALL); return ZBX_VALID_ERROR; } } //*/ if ($type == T_ZBX_CLR && !is_hex_color($var)) { $var = 'FFFFFF'; if ($flags & P_SYS) { info(S_CRITICAL_ERROR . '.' . SPACE . S_FIELD . SPACE . '[' . $field . ']' . SPACE . S_IS_NOT_A_COLOUR_SMALL); return ZBX_VALID_ERROR; } else { info(S_WARNING . '.' . SPACE . S_FIELD . SPACE . '[' . $field . ']' . SPACE . S_IS_NOT_A_COLOUR_SMALL); return ZBX_VALID_WARNING; } } return ZBX_VALID_OK; }
/** * Validate map element. * * @param string $data import data * @param array $parent_data data's parent array * @param string $path XML path * * @throws Exception if the map element is invalid */ public function validateMapElement($data, array $parent_data = null, $path) { if (zbx_is_int($parent_data['elementtype'])) { switch ($parent_data['elementtype']) { case SYSMAP_ELEMENT_TYPE_HOST: $rules = ['type' => XML_ARRAY, 'rules' => ['host' => ['type' => XML_STRING | XML_REQUIRED]]]; break; case SYSMAP_ELEMENT_TYPE_MAP: $rules = ['type' => XML_ARRAY, 'rules' => ['name' => ['type' => XML_STRING | XML_REQUIRED]]]; break; case SYSMAP_ELEMENT_TYPE_TRIGGER: $rules = ['type' => XML_ARRAY, 'rules' => ['host' => ['type' => XML_STRING], 'description' => ['type' => XML_STRING | XML_REQUIRED], 'expression' => ['type' => XML_STRING | XML_REQUIRED]]]; break; case SYSMAP_ELEMENT_TYPE_HOST_GROUP: $rules = ['type' => XML_ARRAY, 'rules' => ['name' => ['type' => XML_STRING | XML_REQUIRED]]]; break; default: return $data; } $data = (new CXmlValidatorGeneral($rules, $this->format))->validate($data, $path); } return $data; }
/** * Validate "ymax_item_1" tag. * * @param string $data import data * @param array $parent_data data's parent array * @param string $path XML path * * @throws Exception if the element is invalid */ public function validateYMaxItem($data, array $parent_data = null, $path) { if (zbx_is_int($parent_data['ymax_type_1']) && $parent_data['ymax_type_1'] == GRAPH_YAXIS_TYPE_ITEM_VALUE) { $rules = ['type' => XML_ARRAY, 'rules' => ['host' => ['type' => XML_STRING | XML_REQUIRED], 'key' => ['type' => XML_STRING | XML_REQUIRED]]]; } else { $rules = ['type' => XML_ARRAY, 'rules' => []]; } return (new CXmlValidatorGeneral($rules, $this->format))->validate($data, $path); }
/** * Validate vsize and hsize parameters. * * @param array $screen * * @throws APIException if the input is invalid. */ protected function validateScreenSize(array $screen) { foreach (['vsize', 'hsize'] as $field_name) { if (!array_key_exists($field_name, $screen)) { continue; } if (!zbx_is_int($screen[$field_name])) { self::exception(ZBX_API_ERROR_PERMISSIONS, _s('Incorrect value for field "%1$s": %2$s.', $field_name, _('a numeric value is expected'))); } if ($screen[$field_name] < SCREEN_MIN_SIZE || $screen[$field_name] > SCREEN_MAX_SIZE) { self::exception(ZBX_API_ERROR_PERMISSIONS, _s('Incorrect value for field "%1$s": %2$s.', $field_name, _s('must be between "%1$s" and "%2$s"', SCREEN_MIN_SIZE, SCREEN_MAX_SIZE))); } } }
public static function checkValueTypes($table, &$values) { $table_schema = self::getSchema($table); foreach ($values as $field => $value) { if (!isset($table_schema['fields'][$field])) { unset($values[$field]); continue; } switch ($table_schema['fields'][$field]['type']) { case self::FIELD_TYPE_CHAR: $values[$field] = zbx_dbstr($value); break; case self::FIELD_TYPE_ID: case self::FIELD_TYPE_UINT: if (!zbx_ctype_digit($value)) { self::exception(self::DBEXECUTE_ERROR, 'Incorrect value for unsigned int field'); } break; case self::FIELD_TYPE_INT: if (!zbx_is_int($value)) { self::exception(self::DBEXECUTE_ERROR, 'Incorrect value for int field'); } break; case self::FIELD_TYPE_FLOAT: if (!is_numeric($value)) { self::exception(self::DBEXECUTE_ERROR, 'Incorrect value for float field'); } break; } } }