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 (is_null($values[$field])) { if ($tableSchema['fields'][$field]['null']) { $values[$field] = 'NULL'; } elseif (isset($tableSchema['fields'][$field]['default'])) { $values[$field] = $tableSchema['fields'][$field]['default']; } else { self::exception(self::DBEXECUTE_ERROR, _s('Mandatory field "%1$s" is missing in table "%2$s".', $field, $table)); } } if (isset($tableSchema['fields'][$field]['ref_table'])) { if ($tableSchema['fields'][$field]['null']) { $values[$field] = zero2null($values[$field]); } } if ($values[$field] === 'NULL') { if (!$tableSchema['fields'][$field]['null']) { self::exception(self::DBEXECUTE_ERROR, _s('Incorrect value "NULL" for NOT NULL field "%1$s".', $field)); } } else { switch ($tableSchema['fields'][$field]['type']) { case self::FIELD_TYPE_CHAR: $length = zbx_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 = zbx_strlen($values[$field]); $values[$field] = zbx_dbstr($values[$field]); if ($DB['TYPE'] == ZBX_DB_DB2) { 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; } } } }
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' => API_OUTPUT_SHORTEN, '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'); foreach ($colors as $color) { if (isset($configs[$color]) && !is_null($configs[$color])) { if (!preg_match('/[0-9a-f]{6}/i', $configs[$color])) { error(_('Colour is not correct: expecting hexadecimal colour code (6 symbols).')); 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 . '=' . zero2null($value); } else { $update[] = $key . '=' . zbx_dbstr($value); } } } if (count($update) == 0) { error(_('Nothing to do.')); return null; } return DBexecute('UPDATE config SET ' . implode(',', $update) . ' WHERE ' . DBin_node('configid', false)); }