本文整理汇总了PHP中check_db_fields函数的典型用法代码示例。如果您正苦于以下问题:PHP check_db_fields函数的具体用法?PHP check_db_fields怎么用?PHP check_db_fields使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了check_db_fields函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: add_expression
function add_expression($regexpid, $expression = array())
{
$db_fields = array('expression' => null, 'expression_type' => null, 'case_sensitive' => 0, 'exp_delimiter' => ',');
if (!check_db_fields($db_fields, $expression)) {
error('Incorrect arguments pasted to function [add_expression]');
return false;
}
$expressionid = get_dbid('expressions', 'expressionid');
$result = DBexecute('INSERT INTO expressions (expressionid,regexpid,expression,expression_type,case_sensitive,exp_delimiter) ' . ' VALUES (' . $expressionid . ',' . $regexpid . ',' . zbx_dbstr($expression['expression']) . ',' . $expression['expression_type'] . ',' . $expression['case_sensitive'] . ',' . zbx_dbstr($expression['exp_delimiter']) . ')');
return $result ? $expressionid : false;
}
示例2: add_expression
function add_expression($regexpid, $expression = array())
{
$db_fields = array('expression' => null, 'expression_type' => null, 'case_sensitive' => 0, 'exp_delimiter' => ',');
if (!check_db_fields($db_fields, $expression)) {
error(S_INCORRECT_ARGUMENTS_PASSED_TO_FUNCTION . ' [add_expression]');
return false;
}
$expressionid = get_dbid('expressions', 'expressionid');
$result = DBexecute('INSERT INTO expressions (expressionid,regexpid,expression,expression_type,case_sensitive,exp_delimiter) ' . ' VALUES (' . $expressionid . ',' . $regexpid . ',' . zbx_dbstr($expression['expression']) . ',' . $expression['expression_type'] . ',' . $expression['case_sensitive'] . ',' . zbx_dbstr($expression['exp_delimiter']) . ')');
return $result ? $expressionid : false;
}
示例3: validateCreate
/**
* Validate trigger prototypes to be created.
*
* @param array $triggerPrototypes
*
* @throws APIException if validation failed.
*/
protected function validateCreate(array $triggerPrototypes)
{
$triggerDbFields = ['description' => null, 'expression' => null, 'error' => _('Trigger just added. No status update so far.')];
foreach ($triggerPrototypes as $triggerPrototype) {
if (!check_db_fields($triggerDbFields, $triggerPrototype)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Wrong fields for trigger.'));
}
if (array_key_exists('templateid', $triggerPrototype)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Cannot set "templateid" for trigger prototype "%1$s".', $triggerPrototype['description']));
}
$this->checkExpression($triggerPrototype);
$this->checkIfExistsOnHost($triggerPrototype);
}
}
示例4: validateCreate
/**
* Validates the input parameters for the create() method.
*
* @throws APIException if the input is invalid
*
* @param array $scripts
*/
protected function validateCreate(array $scripts)
{
if (self::$userData['type'] != USER_TYPE_SUPER_ADMIN) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('You do not have permission to perform this operation.'));
}
$dbFields = array('command' => null, 'name' => null);
$names = array();
foreach ($scripts as $script) {
if (!check_db_fields($dbFields, $script)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Wrong fields for script.'));
}
if (zbx_empty($script['name'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty name for script.'));
}
if (isset($names[$script['name']])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Duplicate script name "%1$s".', $script['name']));
}
$names[$script['name']] = $script['name'];
}
$dbScripts = $this->get(array('output' => array('name'), 'filter' => array('name' => $names), 'nopermissions' => true, 'limit' => 1));
if ($dbScript = reset($dbScripts)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Script "%1$s" already exists.', $dbScript['name']));
}
}
示例5: update_user
function update_user($userid, $user)
{
$result = true;
$sql = 'SELECT DISTINCT * ' . ' FROM users ' . ' WHERE ( alias=' . zbx_dbstr($user['alias']) . ' OR userid=' . $userid . ' ) ' . ' AND ' . DBin_node('userid', id2nodeid($userid));
$db_users = DBselect($sql);
while ($db_user = DBfetch($db_users)) {
if ($db_user['userid'] != $userid) {
error('User ' . $user['alias'] . ' already exists');
return false;
}
$user_db_fields = $db_user;
}
if (!isset($user_db_fields)) {
return false;
}
if (isset($user['passwd'])) {
$user['passwd'] = md5($user['passwd']);
}
if (!check_db_fields($user_db_fields, $user)) {
error('Incorrect arguments pasted to function [update_user]');
return false;
}
$sql = 'UPDATE users SET ' . ' name=' . zbx_dbstr($user['name']) . ' ,surname=' . zbx_dbstr($user['surname']) . ' ,alias=' . zbx_dbstr($user['alias']) . ' ,passwd=' . zbx_dbstr($user['passwd']) . ' ,url=' . zbx_dbstr($user['url']) . ' ,autologin=' . $user['autologin'] . ' ,autologout=' . $user['autologout'] . ' ,lang=' . zbx_dbstr($user['lang']) . ' ,theme=' . zbx_dbstr($user['theme']) . ' ,refresh=' . $user['refresh'] . ' ,rows_per_page=' . $user['rows_per_page'] . ' ,type=' . $user['type'] . ' WHERE userid=' . $userid;
$result = DBexecute($sql);
if ($result && !is_null($user['user_groups'])) {
$result = DBexecute('DELETE FROM users_groups WHERE userid=' . $userid);
foreach ($user['user_groups'] as $groupid => $group_name) {
if (!$result) {
break;
}
$users_groups_id = get_dbid('users_groups', 'id');
$result = DBexecute('INSERT INTO users_groups (id, usrgrpid, userid)' . 'values(' . $users_groups_id . ',' . $groupid . ',' . $userid . ')');
}
}
if ($result && !is_null($user['user_medias'])) {
$result = DBexecute('DELETE FROM media WHERE userid=' . $userid);
foreach ($user['user_medias'] as $mediaid => $media_data) {
if (!$result) {
break;
}
$mediaid = get_dbid('media', 'mediaid');
$result = DBexecute('INSERT INTO media (mediaid, userid, mediatypeid, sendto, active, severity, period)' . ' values (' . $mediaid . ',' . $userid . ',' . $media_data['mediatypeid'] . ',' . zbx_dbstr($media_data['sendto']) . ',' . $media_data['active'] . ',' . $media_data['severity'] . ',' . zbx_dbstr($media_data['period']) . ')');
}
}
return $result;
}
示例6: addRegexpExpressions
function addRegexpExpressions($regexpId, array $expressions)
{
$dbFields = array('expression' => null, 'expression_type' => null);
foreach ($expressions as &$expression) {
if (!check_db_fields($dbFields, $expression)) {
throw new Exception(_('Incorrect arguments passed to function') . ' [add_expression]');
}
$expression['regexpid'] = $regexpId;
}
unset($expression);
DB::insert('expressions', $expressions);
}
示例7: update
/**
* Update maintenances.
*
* @param array $maintenances
*
* @return boolean
*/
public function update(array $maintenances)
{
$maintenances = zbx_toArray($maintenances);
$maintenanceids = zbx_objectValues($maintenances, 'maintenanceid');
// validate maintenance permissions
if (self::$userData['type'] == USER_TYPE_ZABBIX_USER) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!'));
}
$updMaintenances = $this->get(['maintenanceids' => zbx_objectValues($maintenances, 'maintenanceid'), 'editable' => true, 'output' => API_OUTPUT_EXTEND, 'selectGroups' => ['groupid'], 'selectHosts' => ['hostid'], 'selectTimeperiods' => API_OUTPUT_EXTEND, 'preservekeys' => true]);
$maintenanceNamesChanged = [];
foreach ($maintenances as $maintenance) {
if (!isset($updMaintenances[$maintenance['maintenanceid']])) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!'));
}
if (isset($maintenance['name']) && !zbx_empty($maintenance['name']) && $updMaintenances[$maintenance['maintenanceid']]['name'] !== $maintenance['name']) {
if (isset($maintenanceNamesChanged[$maintenance['name']])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Maintenance "%1$s" already exists.', $maintenance['name']));
} else {
$maintenanceNamesChanged[$maintenance['name']] = $maintenance['name'];
}
}
}
// check if maintenance already exists
if ($maintenanceNamesChanged) {
$dbMaintenances = $this->get(['output' => ['name'], 'filter' => ['name' => $maintenanceNamesChanged], 'nopermissions' => true, 'limit' => 1]);
if ($dbMaintenances) {
$dbMaintenance = reset($dbMaintenances);
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Maintenance "%1$s" already exists.', $dbMaintenance['name']));
}
}
$hostids = [];
$groupids = [];
foreach ($maintenances as $maintenance) {
// validate maintenance active since
if (!validateUnixTime($maintenance['active_since'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('"%s" must be between 1970.01.01 and 2038.01.18.', _('Active since')));
}
// validate maintenance active till
if (!validateUnixTime($maintenance['active_till'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('"%s" must be between 1970.01.01 and 2038.01.18.', _('Active till')));
}
// validate maintenance active interval
if ($maintenance['active_since'] > $maintenance['active_till']) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Maintenance "Active since" value cannot be bigger than "Active till".'));
}
// validate timeperiods
if (!array_key_exists('timeperiods', $maintenance) || !is_array($maintenance['timeperiods']) || !$maintenance['timeperiods']) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('At least one maintenance period must be created.'));
}
foreach ($maintenance['timeperiods'] as $timeperiod) {
if (!is_array($timeperiod)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('At least one maintenance period must be created.'));
}
}
$hostids = array_merge($hostids, $maintenance['hostids']);
$groupids = array_merge($groupids, $maintenance['groupids']);
}
// validate hosts & groups
if (empty($hostids) && empty($groupids)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('At least one host or group should be selected.'));
}
// validate hosts permissions
$options = ['hostids' => $hostids, 'editable' => true, 'output' => ['hostid'], 'preservekeys' => true];
$updHosts = API::Host()->get($options);
foreach ($hostids as $hostid) {
if (!isset($updHosts[$hostid])) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('You do not have permission to perform this operation.'));
}
}
// validate groups permissions
$options = ['groupids' => $groupids, 'editable' => true, 'output' => ['groupid'], 'preservekeys' => true];
$updGroups = API::HostGroup()->get($options);
foreach ($groupids as $groupid) {
if (!isset($updGroups[$groupid])) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!'));
}
}
$this->removeSecondsFromTimes($maintenances);
$update = [];
foreach ($maintenances as $mnum => $maintenance) {
$dbFields = ['maintenanceid' => null];
// validate fields
if (!check_db_fields($dbFields, $maintenance)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect parameters for maintenance.'));
}
$update[$mnum] = ['values' => $maintenance, 'where' => ['maintenanceid' => $maintenance['maintenanceid']]];
// update time periods
$this->replaceTimePeriods($updMaintenances[$maintenance['maintenanceid']], $maintenance);
}
DB::update('maintenances', $update);
// some of the hosts and groups bound to maintenance must be deleted, other inserted and others left alone
$insertHosts = [];
$insertGroups = [];
//.........这里部分代码省略.........
示例8: create
/**
* Create web scenario.
*
* @param $httpTests
*
* @return array
*/
public function create($httpTests)
{
$httpTests = zbx_toArray($httpTests);
if (!$httpTests) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty input parameters.'));
}
// find hostid by applicationid
foreach ($httpTests as &$httpTest) {
unset($httpTest['templateid']);
if (empty($httpTest['hostid']) && !empty($httpTest['applicationid'])) {
$dbHostId = DBfetch(DBselect('SELECT a.hostid' . ' FROM applications a' . ' WHERE a.applicationid=' . zbx_dbstr($httpTest['applicationid'])));
$httpTest['hostid'] = $dbHostId['hostid'];
}
}
unset($httpTest);
foreach ($httpTests as &$httpTest) {
$defaultValues = array('verify_peer' => HTTPTEST_VERIFY_PEER_OFF, 'verify_host' => HTTPTEST_VERIFY_HOST_OFF);
check_db_fields($defaultValues, $httpTest);
}
unset($httpTest);
$this->validateCreate($httpTests);
$httpTests = Manager::HttpTest()->persist($httpTests);
return array('httptestids' => zbx_objectValues($httpTests, 'httptestid'));
}
示例9: update
/**
* Update IconMap.
* @param array $iconMaps
* @return array
*/
public function update(array $iconMaps)
{
if (USER_TYPE_SUPER_ADMIN != self::$userData['type']) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('Only Super Admins can update icon maps.'));
}
$iconMaps = zbx_toArray($iconMaps);
$iconMapids = zbx_objectValues($iconMaps, 'iconmapid');
$updates = array();
$duplicates = array();
foreach ($iconMaps as $iconMap) {
if (!check_db_fields(array('iconmapid' => null), $iconMap)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect parameters for icon map update method "%s".', $iconMap['name']));
}
if (isset($iconMap['name'])) {
if (zbx_empty($iconMap['name'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map name cannot be empty.'));
} elseif (isset($duplicates[$iconMap['name']])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Cannot create icon maps with identical name "%s".', $iconMap['name']));
} else {
$duplicates[$iconMap['name']] = $iconMap['name'];
}
}
}
$this->validateMappings($iconMaps, false);
$iconMapsUpd = API::IconMap()->get(array('iconmapids' => $iconMapids, 'output' => API_OUTPUT_EXTEND, 'preservekeys' => true, 'selectMappings' => API_OUTPUT_EXTEND));
$mappingsCreate = $mappingsUpdate = $mappingidsDelete = array();
foreach ($iconMaps as $iconMap) {
if (!isset($iconMapsUpd[$iconMap['iconmapid']])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map with iconmapid "%s" does not exist.', $iconMap['iconmapid']));
}
// Existence
if (isset($iconMap['name'])) {
$iconMapExists = $this->get(array('filter' => array('name' => $iconMap['name']), 'output' => API_OUTPUT_SHORTEN, 'editable' => true, 'nopermissions' => true, 'preservekeys' => true));
if (($iconMapExists = reset($iconMapExists)) && bccomp($iconMapExists['iconmapid'], $iconMap['iconmapid']) != 0) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map "%s" already exists.', $iconMap['name']));
}
}
if (isset($iconMap['mappings'])) {
$mappingsDb = $iconMapsUpd[$iconMap['iconmapid']]['mappings'];
foreach ($iconMap['mappings'] as $mapping) {
$mapping['iconmapid'] = $iconMap['iconmapid'];
if (isset($mapping['iconmappingid']) && isset($mappingsDb[$mapping['iconmappingid']])) {
$iconmappingid = $mapping['iconmappingid'];
unset($mapping['iconmappingid']);
$mappingsUpdate[] = array('values' => $mapping, 'where' => array('iconmappingid' => $iconmappingid));
unset($mappingsDb[$iconmappingid]);
} else {
$mappingsCreate[] = $mapping;
}
}
$mappingidsDelete = array_merge($mappingidsDelete, array_keys($mappingsDb));
}
$iconMapid = $iconMap['iconmapid'];
unset($iconMap['iconmapid']);
if (!empty($iconMap)) {
$updates[] = array('values' => $iconMap, 'where' => array('iconmapid' => $iconMapid));
}
}
DB::update('icon_map', $updates);
DB::insert('icon_mapping', $mappingsCreate);
DB::update('icon_mapping', $mappingsUpdate);
if (!empty($mappingidsDelete)) {
DB::delete('icon_mapping', array('iconmappingid' => $mappingidsDelete));
}
return array('iconmapids' => $iconMapids);
}
示例10: checkInput
protected function checkInput(&$hosts, $method)
{
$create = $method == 'create';
$update = $method == 'update';
$delete = $method == 'delete';
// permissions
$groupids = array();
foreach ($hosts as $host) {
if (!isset($host['groups'])) {
continue;
}
$groupids = array_merge($groupids, zbx_objectValues($host['groups'], 'groupid'));
}
if ($update || $delete) {
$hostDBfields = array('hostid' => null);
$dbHosts = $this->get(array('output' => array('hostid', 'host'), 'hostids' => zbx_objectValues($hosts, 'hostid'), 'editable' => true, 'preservekeys' => true));
} else {
$hostDBfields = array('host' => null);
}
if (!empty($groupids)) {
$dbGroups = API::HostGroup()->get(array('output' => API_OUTPUT_EXTEND, 'groupids' => $groupids, 'editable' => true, 'preservekeys' => true));
}
$inventoryFields = getHostInventories();
$inventoryFields = zbx_objectValues($inventoryFields, 'db_field');
$hostNames = array();
foreach ($hosts as &$host) {
if (!check_db_fields($hostDBfields, $host)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Wrong fields for host "%s".', isset($host['host']) ? $host['host'] : ''));
}
if (isset($host['inventory']) && !empty($host['inventory'])) {
$fields = array_keys($host['inventory']);
foreach ($fields as $field) {
if (!in_array($field, $inventoryFields)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect inventory field "%s".', $field));
}
}
}
if ($update || $delete) {
if (!isset($dbHosts[$host['hostid']])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('No permissions to referred object or it does not exist!'));
}
if ($delete) {
$host['host'] = $dbHosts[$host['hostid']]['host'];
}
} else {
// if visible name is not given or empty it should be set to host name
if (!isset($host['name']) || zbx_empty(trim($host['name']))) {
$host['name'] = $host['host'];
}
if (!isset($host['groups'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('No groups for host "%s".', $host['host']));
}
if (!isset($host['interfaces'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('No interfaces for host "%s".', $host['host']));
}
}
if ($delete) {
continue;
}
if (isset($host['groups'])) {
if (!is_array($host['groups']) || empty($host['groups'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('No groups for host "%s".', $host['host']));
}
foreach ($host['groups'] as $group) {
if (!isset($dbGroups[$group['groupid']])) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!'));
}
}
}
if (isset($host['interfaces'])) {
if (!is_array($host['interfaces']) || empty($host['interfaces'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('No interfaces for host "%s".', $host['host']));
}
}
if (isset($host['host'])) {
// Check if host name isn't longer than 64 chars
if (zbx_strlen($host['host']) > 64) {
self::exception(ZBX_API_ERROR_PARAMETERS, _n('Maximum host name length is %2$d characters, "%3$s" is %1$d character.', 'Maximum host name length is %2$d characters, "%3$s" is %1$d characters.', zbx_strlen($host['host']), 64, $host['host']));
}
if (!preg_match('/^' . ZBX_PREG_HOST_FORMAT . '$/', $host['host'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect characters used for host name "%s".', $host['host']));
}
if (isset($hostNames['host'][$host['host']])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Duplicate host. Host with the same host name "%s" already exists in data.', $host['host']));
}
$hostNames['host'][$host['host']] = $update ? $host['hostid'] : 1;
}
if (isset($host['name'])) {
if ($update) {
// if visible name is empty replace it with host name
if (zbx_empty(trim($host['name']))) {
if (!isset($host['host'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Visible name cannot be empty if host name is missing.'));
}
$host['name'] = $host['host'];
}
}
// Check if visible name isn't longer than 64 chars
if (zbx_strlen($host['name']) > 64) {
self::exception(ZBX_API_ERROR_PARAMETERS, _n('Maximum visible host name length is %2$d characters, "%3$s" is %1$d character.', 'Maximum visible host name length is %2$d characters, "%3$s" is %1$d characters.', zbx_strlen($host['name']), 64, $host['name']));
//.........这里部分代码省略.........
示例11: checkInput
/**
* Check items data.
*
* Any system field passed to the function will be unset.
*
* @throw APIException
*
* @param array $items passed by reference
* @param bool $update
*
* @return void
*/
protected function checkInput(array &$items, $update = false)
{
if ($update) {
$itemDbFields = ['itemid' => null];
$dbItemsFields = ['itemid', 'templateid'];
foreach ($this->fieldRules as $field => $rule) {
if (!isset($rule['system'])) {
$dbItemsFields[] = $field;
}
}
$dbItems = $this->get(['output' => $dbItemsFields, 'itemids' => zbx_objectValues($items, 'itemid'), 'editable' => true, 'preservekeys' => true]);
$dbHosts = API::Host()->get(['output' => ['hostid', 'status', 'name'], 'hostids' => zbx_objectValues($dbItems, 'hostid'), 'templated_hosts' => true, 'editable' => true, 'selectApplications' => ['applicationid', 'flags'], 'preservekeys' => true]);
} else {
$itemDbFields = ['name' => null, 'key_' => null, 'hostid' => null, 'type' => null, 'value_type' => null, 'delay' => '0', 'delay_flex' => ''];
$dbHosts = API::Host()->get(['output' => ['hostid', 'status', 'name'], 'hostids' => zbx_objectValues($items, 'hostid'), 'templated_hosts' => true, 'editable' => true, 'selectApplications' => ['applicationid', 'flags'], 'preservekeys' => true]);
}
// interfaces
$interfaces = API::HostInterface()->get(['output' => ['interfaceid', 'hostid', 'type'], 'hostids' => zbx_objectValues($dbHosts, 'hostid'), 'nopermissions' => true, 'preservekeys' => true]);
if ($update) {
$updateDiscoveredValidator = new CUpdateDiscoveredValidator(['allowed' => ['itemid', 'status'], 'messageAllowedField' => _('Cannot update "%2$s" for a discovered item "%1$s".')]);
foreach ($items as $item) {
// check permissions
if (!isset($dbItems[$item['itemid']])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('No permissions to referred object or it does not exist!'));
}
$dbItem = $dbItems[$item['itemid']];
$itemName = isset($item['name']) ? $item['name'] : $dbItem['name'];
// discovered fields, except status, cannot be updated
$updateDiscoveredValidator->setObjectName($itemName);
$this->checkPartialValidator($item, $updateDiscoveredValidator, $dbItem);
}
$items = $this->extendObjects($this->tableName(), $items, ['name', 'flags']);
}
$item_key_parser = new CItemKey();
foreach ($items as $inum => &$item) {
$item = $this->clearValues($item);
$fullItem = $items[$inum];
if (!check_db_fields($itemDbFields, $item)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect arguments passed to function.'));
}
if ($update) {
check_db_fields($dbItems[$item['itemid']], $fullItem);
$this->checkNoParameters($item, ['templateid', 'state'], _('Cannot update "%1$s" for item "%2$s".'), $item['name']);
// apply rules
foreach ($this->fieldRules as $field => $rules) {
if (0 != $fullItem['templateid'] && isset($rules['template']) || isset($rules['system'])) {
unset($item[$field]);
// For templated item and fields that should not be modified, use the value from DB.
if (array_key_exists($field, $dbItems[$item['itemid']]) && array_key_exists($field, $fullItem)) {
$fullItem[$field] = $dbItems[$item['itemid']][$field];
}
}
}
if (!isset($item['key_'])) {
$item['key_'] = $fullItem['key_'];
}
if (!isset($item['hostid'])) {
$item['hostid'] = $fullItem['hostid'];
}
// if a templated item is being assigned to an interface with a different type, ignore it
$itemInterfaceType = itemTypeInterface($dbItems[$item['itemid']]['type']);
if ($fullItem['templateid'] && isset($item['interfaceid']) && isset($interfaces[$item['interfaceid']]) && $itemInterfaceType !== INTERFACE_TYPE_ANY && $interfaces[$item['interfaceid']]['type'] != $itemInterfaceType) {
unset($item['interfaceid']);
}
} else {
if (!isset($dbHosts[$item['hostid']])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('No permissions to referred object or it does not exist!'));
}
check_db_fields($itemDbFields, $fullItem);
$this->checkNoParameters($item, ['templateid', 'state'], _('Cannot set "%1$s" for item "%2$s".'), $item['name']);
}
$host = $dbHosts[$fullItem['hostid']];
if ($fullItem['type'] == ITEM_TYPE_ZABBIX_ACTIVE) {
$item['delay_flex'] = '';
}
if ($fullItem['value_type'] == ITEM_VALUE_TYPE_STR) {
$item['delta'] = 0;
}
if ($fullItem['value_type'] != ITEM_VALUE_TYPE_UINT64) {
$item['data_type'] = 0;
}
// For non-numeric types, whichever value was entered in trends field, is overwritten to zero.
if ($fullItem['value_type'] == ITEM_VALUE_TYPE_STR || $fullItem['value_type'] == ITEM_VALUE_TYPE_LOG || $fullItem['value_type'] == ITEM_VALUE_TYPE_TEXT) {
$item['trends'] = 0;
}
// check if the item requires an interface
$itemInterfaceType = itemTypeInterface($fullItem['type']);
if ($itemInterfaceType !== false && $host['status'] != HOST_STATUS_TEMPLATE) {
//.........这里部分代码省略.........
示例12: validateUpdate
/**
* Validate web scenario parameters for update method.
* - check permissions
* - check if web scenario with same name already exists
* - check that each web scenario object has httptestid defined
*
* @param array $httpTests
*/
protected function validateUpdate(array $httpTests, array $dbHttpTests)
{
$httpTestIds = zbx_objectValues($httpTests, 'httptestid');
if (!$this->isWritable($httpTestIds)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('You do not have permission to perform this operation.'));
}
$this->checkNames($httpTests);
foreach ($httpTests as $httpTest) {
$missingKeys = checkRequiredKeys($httpTest, array('httptestid'));
if (!empty($missingKeys)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Web scenario missing parameters: %1$s', implode(', ', $missingKeys)));
}
if (isset($httpTest['name'])) {
// get hostid from db if it's not provided
if (isset($httpTest['hostid'])) {
$hostId = $httpTest['hostid'];
} else {
$hostId = DBfetch(DBselect('SELECT ht.hostid FROM httptest ht' . ' WHERE ht.httptestid=' . zbx_dbstr($httpTest['httptestid'])));
$hostId = $hostId['hostid'];
}
$nameExists = DBfetch(DBselect('SELECT ht.name FROM httptest ht' . ' WHERE ht.name=' . zbx_dbstr($httpTest['name']) . ' AND ht.hostid=' . zbx_dbstr($hostId) . ' AND ht.httptestid<>' . zbx_dbstr($httpTest['httptestid']), 1));
if ($nameExists) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Web scenario "%1$s" already exists.', $nameExists['name']));
}
}
if (!check_db_fields(array('httptestid' => null), $httpTest)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect arguments passed to function.'));
}
if (array_key_exists('steps', $httpTest)) {
$dbHttpTest = isset($httpTest['httptestid']) ? $dbHttpTests[$httpTest['httptestid']] : null;
$this->checkSteps($httpTest, $dbHttpTest);
$this->checkDuplicateSteps($httpTest);
}
}
$this->checkApplicationHost($httpTests);
}
示例13: addItems
/**
* Add items to graph
*
* <code>
* $items = array(
* *string 'graphid' => null,
* array 'items' => (
* 'item1' => array(
* *int 'itemid' => null,
* int 'color' => '000000',
* int 'drawtype' => 0,
* int 'sortorder' => 0,
* int 'yaxisside' => 1,
* int 'calc_fnc' => 2,
* int 'type' => 0,
* int 'periods_cnt' => 5,
* ), ... )
* );
* </code>
*
* @static
* @param array $items multidimensional array with items data
* @return boolean
*/
public static function addItems($items)
{
$error = 'Unknown ZABBIX internal error';
$result_ids = array();
$result = false;
$tpl_graph = false;
$graphid = $items['graphid'];
$items_tmp = $items['items'];
$items = array();
$itemids = array();
foreach ($items_tmp as $item) {
$graph_db_fields = array('itemid' => null, 'color' => '000000', 'drawtype' => 0, 'sortorder' => 0, 'yaxisside' => 1, 'calc_fnc' => 2, 'type' => 0, 'periods_cnt' => 5);
if (!check_db_fields($graph_db_fields, $item)) {
self::$error = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => 'Wrong fields for item [ ' . $item['itemid'] . ' ]');
return false;
}
$items[$item['itemid']] = $item;
$itemids[$item['itemid']] = $item['itemid'];
}
// check if graph is templated graph, then items cannot be added
$graph = CGraph::getById(array('graphid' => $graphid));
if ($graph['templateid'] != 0) {
self::$error = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => 'Cannot edit templated graph : ' . $graph['name']);
return false;
}
// check if graph belongs to template, if so, only items from same template can be added
$tmp_hosts = get_hosts_by_graphid($graphid);
$host = DBfetch($tmp_hosts);
// if graph belongs to template, only one host is possible
if ($host["status"] == HOST_STATUS_TEMPLATE) {
$sql = 'SELECT DISTINCT count(i.hostid) as count
FROM items i
WHERE i.hostid<>' . $host['hostid'] . ' AND ' . DBcondition('i.itemid', $itemids);
$host_count = DBfetch(DBselect($sql));
if ($host_count['count']) {
self::$error = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => 'You must use items only from host : ' . $host['host'] . ' for template graph : ' . $graph['name']);
return false;
}
$tpl_graph = true;
}
DBstart(false);
$result = self::addItems_rec($graphid, $items, $tpl_graph);
$result = DBend($result);
if ($result) {
return $result;
} else {
self::$error = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => $error);
//'Internal zabbix error');
return false;
}
}
示例14: update
/**
* Update UserGroups.
* Checks permissions - only super admins can update usergroups.
* Formats data to be used in massUpdate() method.
*
* @param array $usrgrps
*
* @return int[] array['usrgrpids'] returns passed group ids
*/
public function update($usrgrps)
{
if (USER_TYPE_SUPER_ADMIN != self::$userData['type']) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('Only Super Admins can update user groups.'));
}
$usrgrps = zbx_toArray($usrgrps);
foreach ($usrgrps as $usrgrp) {
// checks if usergroup id is present
$groupDbFields = array('usrgrpid' => null);
if (!check_db_fields($groupDbFields, $usrgrp)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect parameters for user group.'));
}
$usrgrp['usrgrpids'] = $usrgrp['usrgrpid'];
unset($usrgrp['usrgrpid']);
if (!$this->massUpdate($usrgrp)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot update group.'));
}
}
return array('usrgrpids' => zbx_objectValues($usrgrps, 'usrgrpid'));
}
示例15: create
/**
* Add Template
*
* @param array $templates multidimensional array with templates data
* @param string $templates['host']
* @return boolean
*/
public function create($templates)
{
$templates = zbx_toArray($templates);
$templateids = array();
// CHECK IF HOSTS HAVE AT LEAST 1 GROUP {{{
foreach ($templates as $tnum => $template) {
if (empty($template['groups'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('No groups for template [ %s ]', $template['host']));
}
$templates[$tnum]['groups'] = zbx_toArray($templates[$tnum]['groups']);
foreach ($templates[$tnum]['groups'] as $gnum => $group) {
$groupids[$group['groupid']] = $group['groupid'];
}
}
// }}} CHECK IF HOSTS HAVE AT LEAST 1 GROUP
// PERMISSIONS {{{
$options = array('groupids' => $groupids, 'editable' => 1, 'preservekeys' => 1);
$updGroups = API::HostGroup()->get($options);
foreach ($groupids as $gnum => $groupid) {
if (!isset($updGroups[$groupid])) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('You do not have permission to perform this operation.'));
}
}
// }}} PERMISSIONS
foreach ($templates as $tnum => $template) {
// If visible name is not given or empty it should be set to host name
if (!isset($template['name']) || isset($template['name']) && zbx_empty(trim($template['name']))) {
if (isset($template['host'])) {
$template['name'] = $template['host'];
}
}
$templateDbFields = array('host' => null);
if (!check_db_fields($templateDbFields, $template)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Field "host" is mandatory'));
}
if (!preg_match('/^' . ZBX_PREG_HOST_FORMAT . '$/', $template['host'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect characters used for Template name [ %1$s ]', $template['host']));
}
if (isset($template['host'])) {
if ($this->exists(array('host' => $template['host']))) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Template "%s" already exists.', $template['host']));
}
if (API::Host()->exists(array('host' => $template['host']))) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Host "%s" already exists.', $template['host']));
}
}
if (isset($template['name'])) {
if ($this->exists(array('name' => $template['name']))) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Template with the same visible name "%s" already exists.', $template['name']));
}
if (API::Host()->exists(array('name' => $template['name']))) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Host with the same visible name "%s" already exists.', $template['name']));
}
}
$templateid = DB::insert('hosts', array(array('host' => $template['host'], 'name' => $template['name'], 'status' => HOST_STATUS_TEMPLATE)));
$templateids[] = $templateid = reset($templateid);
foreach ($template['groups'] as $group) {
$hostgroupid = get_dbid('hosts_groups', 'hostgroupid');
$result = DBexecute('INSERT INTO hosts_groups (hostgroupid,hostid,groupid) VALUES (' . zbx_dbstr($hostgroupid) . ',' . zbx_dbstr($templateid) . ',' . zbx_dbstr($group['groupid']) . ')');
if (!$result) {
self::exception(ZBX_API_ERROR_PARAMETERS, 'DBerror');
}
}
$template['templateid'] = $templateid;
$options = array();
$options['templates'] = $template;
if (isset($template['templates']) && !is_null($template['templates'])) {
$options['templates_link'] = $template['templates'];
}
if (isset($template['macros']) && !is_null($template['macros'])) {
$options['macros'] = $template['macros'];
}
if (isset($template['hosts']) && !is_null($template['hosts'])) {
$options['hosts'] = $template['hosts'];
}
$result = $this->massAdd($options);
if (!$result) {
self::exception(ZBX_API_ERROR_PARAMETERS);
}
}
return array('templateids' => $templateids);
}