/** * Check that application belongs to http test host. * * @param array $httpTests */ protected function checkApplicationHost(array $httpTests) { $appIds = zbx_objectValues($httpTests, 'applicationid'); $appIds = zbx_toHash($appIds); unset($appIds['0']); if (!empty($appIds)) { $appHostIds = array(); $dbCursor = DBselect('SELECT a.hostid,a.applicationid' . ' FROM applications a' . ' WHERE ' . dbConditionInt('a.applicationid', $appIds)); while ($dbApp = DBfetch($dbCursor)) { $appHostIds[$dbApp['applicationid']] = $dbApp['hostid']; } foreach ($httpTests as $httpTest) { if (isset($httpTest['applicationid'])) { if (!idcmp($appHostIds[$httpTest['applicationid']], $httpTest['hostid'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('The web scenario application belongs to a different host than the web scenario host.')); } } } } }
/** * Prepares and returns an array of child items, inherited from items $itemsToInherit on the given hosts. * * @param array $itemsToInherit * @param array|null $hostIds * * @return array an array of unsaved child items */ protected function prepareInheritedItems(array $itemsToInherit, array $hostIds = null) { // fetch all child hosts $chdHosts = API::Host()->get(array('output' => array('hostid', 'host', 'status'), 'selectParentTemplates' => array('templateid'), 'selectInterfaces' => API_OUTPUT_EXTEND, 'templateids' => zbx_objectValues($itemsToInherit, 'hostid'), 'hostids' => $hostIds, 'preservekeys' => true, 'nopermissions' => true, 'templated_hosts' => true)); if (empty($chdHosts)) { return array(); } $newItems = array(); foreach ($chdHosts as $hostId => $host) { $templateids = zbx_toHash($host['parentTemplates'], 'templateid'); // skip items not from parent templates of current host $parentItems = array(); foreach ($itemsToInherit as $inum => $parentItem) { if (isset($templateids[$parentItem['hostid']])) { $parentItems[$inum] = $parentItem; } } // check existing items to decide insert or update $exItems = API::Item()->get(array('output' => array('itemid', 'type', 'key_', 'flags', 'templateid'), 'hostids' => $hostId, 'preservekeys' => true, 'nopermissions' => true, 'filter' => array('flags' => null))); $exItemsKeys = zbx_toHash($exItems, 'key_'); $exItemsTpl = zbx_toHash($exItems, 'templateid'); foreach ($parentItems as $parentItem) { $exItem = null; // check if an item of a different type with the same key exists if (isset($exItemsKeys[$parentItem['key_']])) { $exItem = $exItemsKeys[$parentItem['key_']]; if ($exItem['flags'] != $parentItem['flags']) { $this->errorInheritFlags($exItem['flags'], $exItem['key_'], $host['host']); } } // update by templateid if (isset($exItemsTpl[$parentItem['itemid']])) { $exItem = $exItemsTpl[$parentItem['itemid']]; if (isset($exItemsKeys[$parentItem['key_']]) && !idcmp($exItemsKeys[$parentItem['key_']]['templateid'], $parentItem['itemid'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _params($this->getErrorMsg(self::ERROR_EXISTS), array($parentItem['key_'], $host['host']))); } } // update by key if (isset($exItemsKeys[$parentItem['key_']])) { $exItem = $exItemsKeys[$parentItem['key_']]; if ($exItem['templateid'] > 0 && !idcmp($exItem['templateid'], $parentItem['itemid'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _params($this->getErrorMsg(self::ERROR_EXISTS_TEMPLATE), array($parentItem['key_'], $host['host']))); } } if ($host['status'] == HOST_STATUS_TEMPLATE || !isset($parentItem['type'])) { unset($parentItem['interfaceid']); } elseif (isset($parentItem['type']) && isset($exItem) && $parentItem['type'] != $exItem['type'] || !isset($exItem)) { $interface = self::findInterfaceForItem($parentItem, $host['interfaces']); if (!empty($interface)) { $parentItem['interfaceid'] = $interface['interfaceid']; } elseif ($interface !== false) { self::exception(ZBX_API_ERROR_PARAMETERS, _params($this->getErrorMsg(self::ERROR_NO_INTERFACE), array($host['host'], $parentItem['key_']))); } } else { unset($parentItem['interfaceid']); } // copying item $newItem = $parentItem; $newItem['hostid'] = $host['hostid']; $newItem['templateid'] = $parentItem['itemid']; // setting item application if (isset($parentItem['applications'])) { $newItem['applications'] = get_same_applications_for_host($parentItem['applications'], $host['hostid']); } if ($exItem) { $newItem['itemid'] = $exItem['itemid']; } else { unset($newItem['itemid']); } $newItems[] = $newItem; } } return $newItems; }
/** * Checks that no trigger with the same description and expression as $trigger exist on the given host. * Assumes the given trigger is valid. * * @throws APIException if at least one trigger exists * * @param array $trigger a trigger with an exploded expression * @param null $hostid * * @return void */ protected function checkIfExistsOnHost(array $trigger, $hostId = null) { // skip the check if the description and expression haven't been changed if (!isset($trigger['description']) && !isset($trigger['expression'])) { return; } // make sure we have all the required data if (!isset($trigger['description']) || !isset($trigger['expression'])) { $explodeExpression = !isset($trigger['expression']); $trigger = $this->extendObject($this->tableName(), $trigger, array('description', 'expression')); if ($explodeExpression) { $trigger['expression'] = explode_exp($trigger['expression']); } } $filter = array('description' => $trigger['description']); if ($hostId) { $filter['hostid'] = $hostId; } else { $expressionData = new CTriggerExpression($trigger['expression']); $expressionData->parse($trigger['expression']); $expressionHosts = $expressionData->getHosts(); $filter['host'] = reset($expressionHosts); } $triggers = $this->get(array('filter' => $filter, 'output' => array('expression', 'triggerid'), 'nopermissions' => true)); foreach ($triggers as $dbTrigger) { $tmpExp = explode_exp($dbTrigger['expression']); // check if the expressions are also equal and that this is a different trigger $differentTrigger = !isset($trigger['triggerid']) || !idcmp($trigger['triggerid'], $dbTrigger['triggerid']); if (strcmp($tmpExp, $trigger['expression']) == 0 && $differentTrigger) { $options = array('output' => array('name'), 'templated_hosts' => true, 'nopermissions' => true, 'limit' => 1); if (isset($filter['host'])) { $options['filter'] = array('host' => $filter['host']); } else { $options['hostids'] = $hostId; } $host = API::Host()->get($options); $host = reset($host); self::exception(ZBX_API_ERROR_PARAMETERS, _s('Trigger "%1$s" already exists on "%2$s".', $trigger['description'], $host['name'])); } } }
/** * Prepares and returns an array of child items, inherited from items $itemsToInherit on the given hosts. * * @param array $itemsToInherit * @param array|null $hostIds * * @return array an array of unsaved child items */ protected function prepareInheritedItems(array $itemsToInherit, array $hostIds = null) { // fetch all child hosts $chdHosts = API::Host()->get(['output' => ['hostid', 'host', 'status'], 'selectParentTemplates' => ['templateid'], 'selectInterfaces' => API_OUTPUT_EXTEND, 'templateids' => zbx_objectValues($itemsToInherit, 'hostid'), 'hostids' => $hostIds, 'preservekeys' => true, 'nopermissions' => true, 'templated_hosts' => true]); if (empty($chdHosts)) { return []; } $newItems = []; foreach ($chdHosts as $hostId => $host) { $templateids = zbx_toHash($host['parentTemplates'], 'templateid'); // skip items not from parent templates of current host $parentItems = []; foreach ($itemsToInherit as $inum => $parentItem) { if (isset($templateids[$parentItem['hostid']])) { $parentItems[$inum] = $parentItem; } } // check existing items to decide insert or update $exItems = API::Item()->get(['output' => ['itemid', 'type', 'key_', 'flags', 'templateid'], 'hostids' => $hostId, 'preservekeys' => true, 'nopermissions' => true, 'filter' => ['flags' => null]]); $exItemsKeys = zbx_toHash($exItems, 'key_'); $exItemsTpl = zbx_toHash($exItems, 'templateid'); $itemids_with_application_prototypes = []; foreach ($parentItems as $parentItem) { if (isset($parentItem['applicationPrototypes']) && is_array($parentItem['applicationPrototypes']) && !array_key_exists('ruleid', $parentItem)) { $itemids_with_application_prototypes[$parentItem['itemid']] = true; } } if ($itemids_with_application_prototypes) { $discovery_rules = DBfetchArray(DBselect('SELECT id.itemid,id.parent_itemid' . ' FROM item_discovery id' . ' WHERE ' . dbConditionInt('id.itemid', array_keys($itemids_with_application_prototypes)))); $discovery_rules = zbx_toHash($discovery_rules, 'itemid'); } foreach ($parentItems as $parentItem) { $exItem = null; // check if an item of a different type with the same key exists if (isset($exItemsKeys[$parentItem['key_']])) { $exItem = $exItemsKeys[$parentItem['key_']]; if ($exItem['flags'] != $parentItem['flags']) { $this->errorInheritFlags($exItem['flags'], $exItem['key_'], $host['host']); } } // update by templateid if (isset($exItemsTpl[$parentItem['itemid']])) { $exItem = $exItemsTpl[$parentItem['itemid']]; if (isset($exItemsKeys[$parentItem['key_']]) && !idcmp($exItemsKeys[$parentItem['key_']]['templateid'], $parentItem['itemid'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _params($this->getErrorMsg(self::ERROR_EXISTS), [$parentItem['key_'], $host['host']])); } } // update by key if (isset($exItemsKeys[$parentItem['key_']])) { $exItem = $exItemsKeys[$parentItem['key_']]; if ($exItem['templateid'] > 0 && !idcmp($exItem['templateid'], $parentItem['itemid'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _params($this->getErrorMsg(self::ERROR_EXISTS_TEMPLATE), [$parentItem['key_'], $host['host']])); } } if ($host['status'] == HOST_STATUS_TEMPLATE || !isset($parentItem['type'])) { unset($parentItem['interfaceid']); } elseif (isset($parentItem['type']) && isset($exItem) && $parentItem['type'] != $exItem['type'] || !isset($exItem)) { $interface = self::findInterfaceForItem($parentItem, $host['interfaces']); if (!empty($interface)) { $parentItem['interfaceid'] = $interface['interfaceid']; } elseif ($interface !== false) { self::exception(ZBX_API_ERROR_PARAMETERS, _params($this->getErrorMsg(self::ERROR_NO_INTERFACE), [$host['host'], $parentItem['key_']])); } } else { unset($parentItem['interfaceid']); } // copying item $newItem = $parentItem; $newItem['hostid'] = $host['hostid']; $newItem['templateid'] = $parentItem['itemid']; // setting item application if (isset($parentItem['applications'])) { $newItem['applications'] = get_same_applications_for_host($parentItem['applications'], $host['hostid']); } if ($parentItem['flags'] == ZBX_FLAG_DISCOVERY_PROTOTYPE && array_key_exists('applicationPrototypes', $parentItem)) { // Get discovery rule ID for current item prototype, if it is not yet set. if (array_key_exists('ruleid', $parentItem)) { $discovery_ruleid = $parentItem['ruleid']; } else { $discovery_ruleid = $discovery_rules[$parentItem['itemid']]['parent_itemid']; } $newItem['applicationPrototypes'] = []; $db_application_prototypes = DBfetchArray(DBselect('SELECT ap.application_prototypeid,ap.name' . ' FROM application_prototype ap' . ' WHERE ap.itemid=' . zbx_dbstr($discovery_ruleid) . ' AND ' . dbConditionString('ap.name', zbx_objectValues($parentItem['applicationPrototypes'], 'name')))); $db_application_prototypes = zbx_toHash($db_application_prototypes, 'name'); foreach ($parentItem['applicationPrototypes'] as $application_prototype) { $db_application_prototype = $db_application_prototypes[$application_prototype['name']]; $newItem['applicationPrototypes'][] = ['name' => $application_prototype['name'], 'templateid' => $db_application_prototype['application_prototypeid']]; } } if ($exItem) { $newItem['itemid'] = $exItem['itemid']; } else { unset($newItem['itemid']); } $newItems[] = $newItem; } } return $newItems; }
/** * For existing hosts we need to set an interfaceid for existing interfaces or they will be added. * * @param array $hosts * * @return array */ protected function addInterfaceIds(array $hosts) { $dbInterfaces = API::HostInterface()->get(array('hostids' => zbx_objectValues($hosts, 'hostid'), 'output' => API_OUTPUT_EXTEND, 'preservekeys' => true)); foreach ($dbInterfaces as $dbInterface) { foreach ($hosts as $hnum => $host) { if (!empty($host['interfaces']) && idcmp($host['hostid'], $dbInterface['hostid'])) { foreach ($host['interfaces'] as $inum => $interface) { if ($dbInterface['ip'] == $interface['ip'] && $dbInterface['dns'] == $interface['dns'] && $dbInterface['useip'] == $interface['useip'] && $dbInterface['port'] == $interface['port'] && $dbInterface['type'] == $interface['type'] && $dbInterface['main'] == $interface['main']) { $hosts[$hnum]['interfaces'][$inum]['interfaceid'] = $dbInterface['interfaceid']; break; } } } if (empty($hosts[$hnum]['interfaces'])) { unset($hosts[$hnum]['interfaces']); } } } return $hosts; }
/** * Check that application belongs to http test host. * * @param array $httpTests */ protected function checkApplicationHost(array $httpTests) { // applications containing 0 in ID, will be removed from web scenario foreach ($httpTests as $httpTestId => $httpTest) { if (array_key_exists('applicationid', $httpTest) && $httpTest['applicationid'] == 0) { unset($httpTests[$httpTestId]); } } $applicationids = zbx_objectValues($httpTests, 'applicationid'); if ($applicationids) { $applications = API::getApiService()->select('applications', ['output' => ['applicationid', 'hostid', 'name', 'flags'], 'applicationids' => $applicationids, 'preservekeys' => true]); // check if applications exist and are normal applications foreach ($applicationids as $applicationid) { if (!array_key_exists($applicationid, $applications)) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!')); } elseif ($applications[$applicationid]['flags'] == ZBX_FLAG_DISCOVERY_CREATED) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Cannot add a discovered application "%1$s" to a web scenario.', $applications[$applicationid]['name'])); } } foreach ($httpTests as $httpTest) { if (!idcmp($applications[$httpTest['applicationid']]['hostid'], $httpTest['hostid'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('The web scenario application belongs to a different host than the web scenario host.')); } } } }
/** * Checks whether favorite value exists. * * @param string $idx identifier of favorite value group * @param int $favid value id * @param string $favobj source object * * @return boolean */ public static function exists($idx, $favid, $favobj = null) { $favorites = self::get($idx); foreach ($favorites as $favorite) { if (idcmp($favid, $favorite['value']) && $favorite['source'] == $favobj) { return true; } } return false; }
/** * Prepares and returns an array of child host prototypes, inherited from host prototypes $hostPrototypes * on the given hosts. * * Each host prototype must have the "ruleid" parameter set. * * @param array $hostPrototypes * @param array $hostIds * * @return array an array of unsaved child host prototypes */ protected function prepareInheritedObjects(array $hostPrototypes, array $hostIds = null) { // fetch the related discovery rules with their hosts $discoveryRules = API::DiscoveryRule()->get(['output' => ['itemid', 'hostid'], 'selectHosts' => ['hostid'], 'itemids' => zbx_objectValues($hostPrototypes, 'ruleid'), 'templated' => true, 'nopermissions' => true, 'preservekeys' => true]); // fetch all child hosts to inherit to // do not inherit host prototypes on discovered hosts $chdHosts = API::Host()->get(['output' => ['hostid', 'host', 'status'], 'selectParentTemplates' => ['templateid'], 'templateids' => zbx_objectValues($discoveryRules, 'hostid'), 'hostids' => $hostIds, 'nopermissions' => true, 'templated_hosts' => true, 'filter' => ['flags' => ZBX_FLAG_DISCOVERY_NORMAL]]); if (empty($chdHosts)) { return []; } // fetch the child discovery rules $childDiscoveryRules = API::DiscoveryRule()->get(['output' => ['itemid', 'templateid', 'hostid'], 'preservekeys' => true, 'filter' => ['templateid' => array_keys($discoveryRules)]]); // fetch child host prototypes and group them by discovery rule $childHostPrototypes = API::HostPrototype()->get(['output' => ['hostid', 'host', 'templateid'], 'selectGroupLinks' => API_OUTPUT_EXTEND, 'selectGroupPrototypes' => API_OUTPUT_EXTEND, 'selectDiscoveryRule' => ['itemid'], 'discoveryids' => zbx_objectValues($childDiscoveryRules, 'itemid')]); foreach ($childDiscoveryRules as &$childDiscoveryRule) { $childDiscoveryRule['hostPrototypes'] = []; } unset($childDiscoveryRule); foreach ($childHostPrototypes as $childHostPrototype) { $discoveryRuleId = $childHostPrototype['discoveryRule']['itemid']; unset($childHostPrototype['discoveryRule']); $childDiscoveryRules[$discoveryRuleId]['hostPrototypes'][] = $childHostPrototype; } // match each discovery that the parent host prototypes belong to to the child discovery rule for each host $discoveryRuleChildren = []; foreach ($childDiscoveryRules as $childRule) { $discoveryRuleChildren[$childRule['templateid']][$childRule['hostid']] = $childRule['itemid']; } $newHostPrototypes = []; foreach ($chdHosts as $host) { $hostId = $host['hostid']; // skip items not from parent templates of current host $templateIds = zbx_toHash($host['parentTemplates'], 'templateid'); $parentHostPrototypes = []; foreach ($hostPrototypes as $inum => $parentHostPrototype) { $parentTemplateId = $discoveryRules[$parentHostPrototype['ruleid']]['hostid']; if (isset($templateIds[$parentTemplateId])) { $parentHostPrototypes[$inum] = $parentHostPrototype; } } foreach ($parentHostPrototypes as $parentHostPrototype) { $childDiscoveryRuleId = $discoveryRuleChildren[$parentHostPrototype['ruleid']][$hostId]; $exHostPrototype = null; // check if the child discovery rule already has host prototypes $exHostPrototypes = $childDiscoveryRules[$childDiscoveryRuleId]['hostPrototypes']; if ($exHostPrototypes) { $exHostPrototypesHosts = zbx_toHash($exHostPrototypes, 'host'); $exHostPrototypesTemplateIds = zbx_toHash($exHostPrototypes, 'templateid'); // look for an already created inherited host prototype // if one exists - update it if (isset($exHostPrototypesTemplateIds[$parentHostPrototype['hostid']])) { $exHostPrototype = $exHostPrototypesTemplateIds[$parentHostPrototype['hostid']]; // check if there's a host prototype on the target host with the same host name but from a different template // or no template if (isset($exHostPrototypesHosts[$parentHostPrototype['host']]) && !idcmp($exHostPrototypesHosts[$parentHostPrototype['host']]['templateid'], $parentHostPrototype['hostid'])) { $discoveryRule = DBfetch(DBselect('SELECT i.name FROM items i WHERE i.itemid=' . zbx_dbstr($exHostPrototype['discoveryRule']['itemid']))); self::exception(ZBX_API_ERROR_PARAMETERS, _s('Host prototype "%1$s" already exists on "%2$s".', $parentHostPrototype['host'], $discoveryRule['name'])); } } // look for a host prototype with the same host name // if one exists - convert it to an inherited host prototype if (isset($exHostPrototypesHosts[$parentHostPrototype['host']])) { $exHostPrototype = $exHostPrototypesHosts[$parentHostPrototype['host']]; // check that this host prototype is not inherited from a different template if ($exHostPrototype['templateid'] > 0 && !idcmp($exHostPrototype['templateid'], $parentHostPrototype['hostid'])) { $discoveryRule = DBfetch(DBselect('SELECT i.name FROM items i WHERE i.itemid=' . zbx_dbstr($exHostPrototype['discoveryRule']['itemid']))); self::exception(ZBX_API_ERROR_PARAMETERS, _s('Host prototype "%1$s" already exists on "%2$s", inherited from another template.', $parentHostPrototype['host'], $discoveryRule['name'])); } } } // copy host prototype $newHostPrototype = $parentHostPrototype; $newHostPrototype['ruleid'] = $discoveryRuleChildren[$parentHostPrototype['ruleid']][$hostId]; $newHostPrototype['templateid'] = $parentHostPrototype['hostid']; // update an existing inherited host prototype if ($exHostPrototype) { // look for existing group prototypes to update $exGroupPrototypesByTemplateId = zbx_toHash($exHostPrototype['groupPrototypes'], 'templateid'); $exGroupPrototypesByName = zbx_toHash($exHostPrototype['groupPrototypes'], 'name'); $exGroupPrototypesByGroupId = zbx_toHash($exHostPrototype['groupLinks'], 'groupid'); // look for a group prototype that can be updated foreach ($newHostPrototype['groupPrototypes'] as &$groupPrototype) { // updated an inherited item prototype by templateid if (isset($exGroupPrototypesByTemplateId[$groupPrototype['group_prototypeid']])) { $groupPrototype['group_prototypeid'] = $exGroupPrototypesByTemplateId[$groupPrototype['group_prototypeid']]['group_prototypeid']; } elseif (isset($groupPrototype['name']) && !zbx_empty($groupPrototype['name']) && isset($exGroupPrototypesByName[$groupPrototype['name']])) { $groupPrototype['templateid'] = $groupPrototype['group_prototypeid']; $groupPrototype['group_prototypeid'] = $exGroupPrototypesByName[$groupPrototype['name']]['group_prototypeid']; } elseif (isset($groupPrototype['groupid']) && $groupPrototype['groupid'] && isset($exGroupPrototypesByGroupId[$groupPrototype['groupid']])) { $groupPrototype['templateid'] = $groupPrototype['group_prototypeid']; $groupPrototype['group_prototypeid'] = $exGroupPrototypesByGroupId[$groupPrototype['groupid']]['group_prototypeid']; } else { $groupPrototype['templateid'] = $groupPrototype['group_prototypeid']; unset($groupPrototype['group_prototypeid']); } unset($groupPrototype['hostid']); } unset($groupPrototype); $newHostPrototype['hostid'] = $exHostPrototype['hostid']; } else { foreach ($newHostPrototype['groupPrototypes'] as &$groupPrototype) { $groupPrototype['templateid'] = $groupPrototype['group_prototypeid']; unset($groupPrototype['group_prototypeid'], $groupPrototype['hostid']); } unset($groupPrototype); unset($newHostPrototype['hostid']); } $newHostPrototypes[] = $newHostPrototype; } } return $newHostPrototypes; }
function view() { // global $CJob; $CJob->requireLogin(); global $params, $MRecruiter, $MCompany, $MJob; // Validations $this->startValidations(); $this->validate(isset($_GET['id']) and ($entry = $MRecruiter->getByID($id = $_GET['id'])) != NULL, $err, 'unknown recruiter'); // Code if ($this->isValid()) { $data = $this->data($entry); $this->validate(($company = $MCompany->get($data['company'])) != NULL, $err, 'recruiter has not set up company profile'); if ($this->isValid()) { $data['company'] = $company['name']; $jobs = $MJob->getByRecruiter($id); $data['jobtitles'] = array(); $data['joblocations'] = array(); foreach ($jobs as $job) { array_push($data['jobtitles'], $job['title']); array_push($data['joblocations'], $job['location']); } $data['isme'] = isset($_SESSION['_id']) ? idcmp($id, $_SESSION['_id']) : false; $data['recruiterid'] = $id; if ($data['photo'] == 'assets/gfx/defaultpic.png') { $data['photo'] = $GLOBALS['dirpre'] . $data['photo']; } $this->render('recruiter', $data); return; } } $this->error($err); $this->render('notice'); }
/** * Checks that the given dependency is valid. * * @throws APIException if the dependency is invalid * * @param array $dependency * * @return void */ protected function checkDependency(array $dependency) { if (idcmp($dependency['serviceid'], $dependency['dependsOnServiceid'])) { $service = API::getApi()->select($this->tableName(), array('output' => array('name'), 'serviceids' => $dependency['serviceid'])); $service = reset($service); self::exception(ZBX_API_ERROR_PARAMETERS, _s('Service "%1$s" cannot be dependent on itself.', $service['name'])); } // check 'soft' field value if (!isset($dependency['soft']) || !in_array((int) $dependency['soft'], array(0, 1), true)) { $service = API::getApi()->select($this->tableName(), array('output' => array('name'), 'serviceids' => $dependency['serviceid'])); $service = reset($service); self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect "soft" field value for dependency for service "%1$s".', $service['name'])); } }
/** * Generate http tests data for inheritance. * Using passed parameters decide if new http tests must be created on host or existing ones must be updated. * * @param array $httpTests which we need to inherit * @param array $hostsTemaplatesMap * * @throws Exception * @return array with http tests, existing apps have 'httptestid' key. */ protected function prepareInheritedHttpTests(array $httpTests, array $hostsTemaplatesMap) { $hostHttpTests = $this->getHttpTestsMapsByHostIds(array_keys($hostsTemaplatesMap)); $result = array(); foreach ($httpTests as $httpTest) { $httpTestId = $httpTest['httptestid']; foreach ($hostHttpTests as $hostId => $hostHttpTest) { // if http test template is not linked to host we skip it if ($hostsTemaplatesMap[$hostId] != $httpTest['hostid']) { continue; } $exHttpTest = null; // update by templateid if (isset($hostHttpTest['byTemplateId'][$httpTestId])) { $exHttpTest = $hostHttpTest['byTemplateId'][$httpTestId]; // need to check templateid here too in case we update linked http test to name that already exists on linked host if (isset($httpTest['name']) && isset($hostHttpTest['byName'][$httpTest['name']]) && !idcmp($exHttpTest['templateid'], $hostHttpTest['byName'][$httpTest['name']]['templateid'])) { $host = DBfetch(DBselect('SELECT h.name FROM hosts h WHERE h.hostid=' . zbx_dbstr($hostId))); throw new Exception(_s('Web scenario "%1$s" already exists on host "%2$s".', $exHttpTest['name'], $host['name'])); } } else { if (isset($hostHttpTest['byName'][$httpTest['name']])) { $exHttpTest = $hostHttpTest['byName'][$httpTest['name']]; if ($exHttpTest['templateid'] > 0 || !$this->compareHttpSteps($httpTest, $exHttpTest)) { $host = DBfetch(DBselect('SELECT h.name FROM hosts h WHERE h.hostid=' . zbx_dbstr($hostId))); throw new Exception(_s('Web scenario "%1$s" already exists on host "%2$s".', $exHttpTest['name'], $host['name'])); } $this->createLinkageBetweenHttpTests($httpTestId, $exHttpTest['httptestid']); continue; } } $newHttpTest = $httpTest; $newHttpTest['hostid'] = $hostId; $newHttpTest['templateid'] = $httpTestId; if ($exHttpTest) { $newHttpTest['httptestid'] = $exHttpTest['httptestid']; $this->setHttpTestParent($exHttpTest['httptestid'], $httpTestId); if (isset($newHttpTest['steps'])) { $newHttpTest['steps'] = $this->prepareHttpSteps($httpTest['steps'], $exHttpTest['httptestid']); } } else { unset($newHttpTest['httptestid']); } if (!empty($newHttpTest['applicationid'])) { $newHttpTest['applicationid'] = $this->findChildApplication($newHttpTest['applicationid'], $hostId); } $result[] = $newHttpTest; } } return $result; }
/** * Update host groups. * * @param array $groups * @param array $groups[0]['name'], ... * @param array $groups[0]['groupid'], ... * * @return boolean */ public function update(array $groups) { $groups = zbx_toArray($groups); $groupids = zbx_objectValues($groups, 'groupid'); if (empty($groups)) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty input parameter.')); } // permissions $updGroups = $this->get(array('groupids' => $groupids, 'editable' => true, 'output' => API_OUTPUT_SHORTEN, 'preservekeys' => true)); foreach ($groups as $group) { if (!isset($updGroups[$group['groupid']])) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('You do not have permission to perform this operation.')); } } // name duplicate check $groupsNames = $this->get(array('filter' => array('name' => zbx_objectValues($groups, 'name')), 'output' => array('groupid', 'name'), 'editable' => true, 'nopermissions' => true)); $groupsNames = zbx_toHash($groupsNames, 'name'); $update = array(); foreach ($groups as $group) { if (isset($group['name']) && isset($groupsNames[$group['name']]) && !idcmp($groupsNames[$group['name']]['groupid'], $group['groupid'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Host group "%1$s" already exists.', $group['name'])); } // prevents updating several groups with same name $groupsNames[$group['name']] = array('groupid' => $group['groupid']); $update[] = array('values' => array('name' => $group['name']), 'where' => array('groupid' => $group['groupid'])); } DB::update('groups', $update); return array('groupids' => $groupids); }
/** * Update host groups. * * @param array $groups * @param array $groups[0]['name'], ... * @param array $groups[0]['groupid'], ... * * @return boolean */ public function update(array $groups) { $groups = zbx_toArray($groups); $groupids = zbx_objectValues($groups, 'groupid'); if (empty($groups)) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty input parameter.')); } // permissions $updGroups = $this->get(array('output' => array('groupid', 'flags', 'name'), 'groupids' => $groupids, 'editable' => true, 'preservekeys' => true)); foreach ($groups as $group) { if (!isset($updGroups[$group['groupid']])) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!')); } $this->checkNoParameters($group, array('internal'), _('Cannot update "%1$s" for host group "%2$s".'), isset($group['name']) ? $group['name'] : $updGroups[$group['groupid']]['name']); } // name duplicate check $groupsNames = $this->get(array('filter' => array('name' => zbx_objectValues($groups, 'name')), 'output' => array('groupid', 'name'), 'editable' => true, 'nopermissions' => true)); $groupsNames = zbx_toHash($groupsNames, 'name'); $updateDiscoveredValidator = new CUpdateDiscoveredValidator(array('messageAllowed' => _('Cannot update a discovered host group.'))); $update = array(); foreach ($groups as $group) { if (isset($group['name'])) { if (zbx_empty($group['name'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Host group name cannot be empty.')); } // cannot update discovered host groups $this->checkPartialValidator($group, $updateDiscoveredValidator, $updGroups[$group['groupid']]); if (isset($groupsNames[$group['name']]) && !idcmp($groupsNames[$group['name']]['groupid'], $group['groupid'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Host group "%1$s" already exists.', $group['name'])); } $update[] = array('values' => array('name' => $group['name']), 'where' => array('groupid' => $group['groupid'])); } // prevents updating several groups with same name $groupsNames[$group['name']] = array('groupid' => $group['groupid']); } DB::update('groups', $update); return array('groupids' => $groupids); }
/** * Validates the input parameters for the update() method. * * @param array $mediatypes * * @throws APIException if the input is invalid. */ protected function validateUpdate(array $mediatypes) { if (self::$userData['type'] != USER_TYPE_SUPER_ADMIN) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('Only Super Admins can edit media types.')); } if (!$mediatypes) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty input parameter.')); } // Validate given IDs. $this->checkObjectIds($mediatypes, 'mediatypeid', _('No "%1$s" given for media type.'), _('Empty media type ID.'), _('Incorrect media type ID.')); $mediatypeids = zbx_objectValues($mediatypes, 'mediatypeid'); // Check value map names. $db_mediatypes = API::getApiService()->select('media_type', ['output' => ['mediatypeid', 'type', 'description', 'exec_path', 'status', 'smtp_port', 'smtp_verify_peer', 'smtp_verify_host', 'smtp_authentication'], 'mediatypeids' => $mediatypeids, 'preservekeys' => true]); $check_names = []; foreach ($mediatypes as $mediatype) { // Check if this media type exists. if (!array_key_exists($mediatype['mediatypeid'], $db_mediatypes)) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!')); } // Validate "description" field. if (array_key_exists('description', $mediatype)) { if (is_array($mediatype['description'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Incorrect arguments passed to function.')); } elseif ($mediatype['description'] === '' || $mediatype['description'] === null || $mediatype['description'] === false) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect value for field "%1$s": %2$s.', 'description', _('cannot be empty'))); } $check_names[$mediatype['description']] = true; } } if ($check_names) { $db_mediatype_names = API::getApiService()->select('media_type', ['output' => ['mediatypeid', 'description'], 'filter' => ['name' => array_keys($check_names)]]); $db_mediatype_names = zbx_toHash($db_mediatype_names, 'description'); foreach ($mediatypes as $mediatype) { if (array_key_exists('description', $mediatype) && array_key_exists($mediatype['description'], $db_mediatype_names) && !idcmp($db_mediatype_names[$mediatype['description']]['mediatypeid'], $mediatype['mediatypeid'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Media type "%1$s" already exists.', $mediatype['description'])); } } } // Populate "description" field, if not set. Type field should not be populated at this point. $mediatypes = $this->extendFromObjects(zbx_toHash($mediatypes, 'mediatypeid'), $db_mediatypes, ['description']); $duplicate_name = CArrayHelper::findDuplicate($mediatypes, 'description'); if ($duplicate_name) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Duplicate "description" value "%1$s" for media type.', $duplicate_name['description'])); } foreach ($mediatypes as $mediatype) { $db_mediatype = $db_mediatypes[$mediatype['mediatypeid']]; // Recheck mandatory fields if type changed. if (array_key_exists('type', $mediatype) && $db_mediatype['type'] != $mediatype['type']) { $this->checkRequiredFieldsByType($mediatype); } else { $optional_fields_by_type = [MEDIA_TYPE_EMAIL => ['smtp_server', 'smtp_helo', 'smtp_email'], MEDIA_TYPE_EXEC => ['exec_path'], MEDIA_TYPE_SMS => ['gsm_modem'], MEDIA_TYPE_JABBER => ['username'], MEDIA_TYPE_EZ_TEXTING => ['exec_path', 'username']]; foreach ($optional_fields_by_type[$db_mediatype['type']] as $field) { if (array_key_exists($field, $mediatype) && ($mediatype[$field] === '' || $mediatype[$field] === null)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Field "%1$s" is missing a value for media type "%2$s".', $field, $mediatype['description'])); } } // Populate "type" field from DB, since it is not set and is required for further validation. $mediatype['type'] = $db_mediatype['type']; } switch ($mediatype['type']) { case MEDIA_TYPE_EZ_TEXTING: if (array_key_exists('exec_path', $mediatype)) { $message_text_limit_validator = new CLimitedSetValidator(['values' => [EZ_TEXTING_LIMIT_USA, EZ_TEXTING_LIMIT_CANADA]]); if ($db_mediatype['exec_path'] !== $mediatype['exec_path'] && !$message_text_limit_validator->validate($mediatype['exec_path'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect value "%1$s" in field "%2$s" for media type "%3$s".', $mediatype['exec_path'], 'exec_path', $mediatype['description'])); } } break; case MEDIA_TYPE_EMAIL: if (array_key_exists('smtp_authentication', $mediatype)) { $smtp_authentication_validator = new CLimitedSetValidator(['values' => [SMTP_AUTHENTICATION_NONE, SMTP_AUTHENTICATION_NORMAL]]); if (!$smtp_authentication_validator->validate($mediatype['smtp_authentication'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect value "%1$s" in field "%2$s" for media type "%3$s".', $mediatype['smtp_authentication'], 'smtp_authentication', $mediatype['description'])); } if ($mediatype['smtp_authentication'] == SMTP_AUTHENTICATION_NORMAL) { // Check 'passwd' field when auth is set to 'normal' manually. if ($db_mediatype['smtp_authentication'] == $mediatype['smtp_authentication'] && array_key_exists('passwd', $mediatype) && ($mediatype['passwd'] === '' || $mediatype['passwd'] === null)) { /* * When auth is set to 'normal', check if password field is set manually. * Otherwise the password is not changed. */ self::exception(ZBX_API_ERROR_PARAMETERS, _s('Password required for media type "%1$s".', $mediatype['description'])); } elseif ($db_mediatype['smtp_authentication'] != $mediatype['smtp_authentication'] && (!array_key_exists('passwd', $mediatype) || $mediatype['passwd'] === '' || $mediatype['passwd'] === null)) { /* * First check if 'passwd' field exists when authentication is changed from * 'none' to 'normal' and then validate it. */ self::exception(ZBX_API_ERROR_PARAMETERS, _s('Password required for media type "%1$s".', $mediatype['description'])); } } } elseif ($db_mediatype['smtp_authentication'] == SMTP_AUTHENTICATION_NORMAL && array_key_exists('passwd', $mediatype) && ($mediatype['passwd'] === '' || $mediatype['passwd'] === null)) { // Check 'passwd' field depeding on authentication set from DB and when it is set to 'normal'. self::exception(ZBX_API_ERROR_PARAMETERS, _s('Password required for media type "%1$s".', $mediatype['description'])); } // Validate optional 'smtp_port' field. if (array_key_exists('smtp_port', $mediatype) && $db_mediatype['smtp_port'] != $mediatype['smtp_port'] && !validatePortNumber($mediatype['smtp_port'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect value "%1$s" in field "%2$s" for media type "%3$s".', $mediatype['smtp_port'], 'smtp_port', $mediatype['description'])); } // Validate optional field 'smtp_security'. if (array_key_exists('smtp_security', $mediatype)) { $smtp_security_validator = new CLimitedSetValidator(['values' => [SMTP_CONNECTION_SECURITY_NONE, SMTP_CONNECTION_SECURITY_STARTTLS, SMTP_CONNECTION_SECURITY_SSL_TLS]]); if (!$smtp_security_validator->validate($mediatype['smtp_security'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect value "%1$s" in field "%2$s" for media type "%3$s".', $mediatype['smtp_security'], 'smtp_security', $mediatype['description'])); } } // Validate optional field 'smtp_verify_peer'. if (array_key_exists('smtp_verify_peer', $mediatype) && $db_mediatype['smtp_verify_peer'] != $mediatype['smtp_verify_peer']) { $smtp_verify_peer_validator = new CLimitedSetValidator(['values' => [0, 1]]); if (!$smtp_verify_peer_validator->validate($mediatype['smtp_verify_peer'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect value "%1$s" in field "%2$s" for media type "%3$s".', $mediatype['smtp_verify_peer'], 'smtp_verify_peer', $mediatype['description'])); } } // Validate optional field 'smtp_verify_host'. if (array_key_exists('smtp_verify_host', $mediatype) && $db_mediatype['smtp_verify_host'] != $mediatype['smtp_verify_host']) { $smtp_verify_host_validator = new CLimitedSetValidator(['values' => [0, 1]]); if (!$smtp_verify_host_validator->validate($mediatype['smtp_verify_host'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect value "%1$s" in field "%2$s" for media type "%3$s".', $mediatype['smtp_verify_host'], 'smtp_verify_host', $mediatype['description'])); } } break; case MEDIA_TYPE_EXEC: if (array_key_exists('exec_params', $mediatype) && $mediatype['exec_params'] !== '') { $pos = strrpos($mediatype['exec_params'], "\n"); if ($pos === false || strlen($mediatype['exec_params']) != $pos + 1) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Script parameters "%1$s" are missing the last new line feed for media type "%2$s".', $mediatype['exec_params'], $mediatype['description'])); } } break; } // Validate optional 'status' field and only when status is changed. if (array_key_exists('status', $mediatype) && $db_mediatype['status'] != $mediatype['status']) { $status_validator = new CLimitedSetValidator(['values' => [MEDIA_TYPE_STATUS_ACTIVE, MEDIA_TYPE_STATUS_DISABLED]]); if (!$status_validator->validate($mediatype['status'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect value "%1$s" in field "%2$s" for media type "%3$s".', $mediatype['status'], 'status', $mediatype['description'])); } } } }
protected function inherit($applications, $hostids = null) { if (empty($applications)) { return $applications; } $applications = zbx_toHash($applications, 'applicationid'); $chdHosts = API::Host()->get(array('output' => array('hostid', 'host'), 'templateids' => zbx_objectValues($applications, 'hostid'), 'hostids' => $hostids, 'preservekeys' => 1, 'nopermissions' => 1, 'templated_hosts' => 1)); if (empty($chdHosts)) { return true; } $insertApplications = array(); $updateApplications = array(); foreach ($chdHosts as $hostid => $host) { $templateids = zbx_toHash($host['templates'], 'templateid'); // skip applications not from parent templates of current host $parentApplications = array(); foreach ($applications as $parentApplicationId => $parentApplication) { if (isset($templateids[$parentApplication['hostid']])) { $parentApplications[$parentApplicationId] = $parentApplication; } } // check existing items to decide insert or update $exApplications = $this->get(array('output' => API_OUTPUT_EXTEND, 'hostids' => $hostid, 'preservekeys' => true, 'nopermissions' => true)); $exApplicationsNames = zbx_toHash($exApplications, 'name'); $exApplicationsTpl = zbx_toHash($exApplications, 'templateid'); foreach ($parentApplications as $parentApplicationId => $parentApplication) { $exApplication = null; // update by templateid if (isset($exApplicationsTpl[$parentApplicationId])) { $exApplication = $exApplicationsTpl[$parentApplicationId]; } // update by name if (isset($parentApplication['name']) && isset($exApplicationsNames[$parentApplication['name']])) { $exApplication = $exApplicationsNames[$parentApplication['name']]; if ($exApplication['templateid'] > 0 && !idcmp($exApplication['templateid'], $parentApplication['applicationid'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Application "%1$s" already exists for host "%2$s".', $exApplication['name'], $host['name'])); } } $newApplication = $parentApplication; $newApplication['hostid'] = $host['hostid']; $newApplication['templateid'] = $parentApplication['applicationid']; if ($exApplication) { $newApplication['applicationid'] = $exApplication['applicationid']; $updateApplications[] = $newApplication; } else { $insertApplications[] = $newApplication; } } } $this->createReal($insertApplications); $this->updateReal($updateApplications); $inheritedApplications = array_merge($insertApplications, $updateApplications); $this->inherit($inheritedApplications); return true; }
show_messages(); /* * Display */ if (isset($_REQUEST['form'])) { $data = array('hostid' => getRequest('hostid', 0), 'httptestid' => getRequest('httptestid'), 'form' => getRequest('form'), 'form_refresh' => getRequest('form_refresh'), 'templates' => array()); $host = API::Host()->get(array('output' => array('status'), 'hostids' => $data['hostid'], 'templated_hosts' => true)); $data['host'] = reset($host); if (isset($data['httptestid'])) { // get templates $httpTestId = $data['httptestid']; while ($httpTestId) { $dbTest = DBfetch(DBselect('SELECT h.hostid,h.name,h.status,ht.httptestid,ht.templateid' . ' FROM hosts h,httptest ht' . ' WHERE ht.hostid=h.hostid' . ' AND ht.httptestid=' . zbx_dbstr($httpTestId))); $httpTestId = null; if (!empty($dbTest)) { if (!idcmp($data['httptestid'], $dbTest['httptestid'])) { $data['templates'][] = new CLink($dbTest['name'], 'httpconf.php?form=update&httptestid=' . $dbTest['httptestid'] . '&hostid=' . $dbTest['hostid'], 'highlight underline weight_normal'); $data['templates'][] = SPACE . '⇒' . SPACE; } $httpTestId = $dbTest['templateid']; } } $data['templates'] = array_reverse($data['templates']); array_shift($data['templates']); } if (isset($_REQUEST['httptestid']) && !isset($_REQUEST['form_refresh'])) { $dbHttpTest = DBfetch(DBselect('SELECT ht.*' . ' FROM httptest ht' . ' WHERE ht.httptestid=' . zbx_dbstr($_REQUEST['httptestid']))); $data['name'] = $dbHttpTest['name']; $data['applicationid'] = $dbHttpTest['applicationid']; $data['new_application'] = ''; $data['delay'] = $dbHttpTest['delay'];
} $interfaceTable = new CTable(null, 'formElementTable'); $interfaceTable->addRow(array(_('IP address'), _('DNS name'), _('Connect to'), _('Port'))); $connectByComboBox = new CRadioButtonList('interface[useip]', $this->data['interface']['useip']); $connectByComboBox->addValue(_('IP'), 1); $connectByComboBox->addValue(_('DNS'), 0); $connectByComboBox->useJQueryStyle(); $interfaceTable->addRow(array(new CTextBox('interface[ip]', $this->data['interface']['ip'], ZBX_TEXTBOX_SMALL_SIZE, 'no', 64), new CTextBox('interface[dns]', $this->data['interface']['dns'], ZBX_TEXTBOX_SMALL_SIZE, 'no', 64), $connectByComboBox, new CTextBox('interface[port]', $this->data['interface']['port'], 18, 'no', 64))); $proxyFormList->addRow(_('Interface'), new CDiv($interfaceTable, 'objectgroup inlineblock border_dotted ui-corner-all')); } // append hosts to form list $hostsTweenBox = new CTweenBox($proxyForm, 'hosts', $this->data['hosts']); foreach ($this->data['dbHosts'] as $host) { // show only normal hosts, and discovered hosts monitored by the current proxy // for new proxies display only normal hosts if ($this->data['proxyid'] && idcmp($this->data['proxyid'], $host['proxy_hostid']) || $host['flags'] == ZBX_FLAG_DISCOVERY_NORMAL) { $hostsTweenBox->addItem($host['hostid'], $host['name'], null, empty($host['proxy_hostid']) || !empty($this->data['proxyid']) && bccomp($host['proxy_hostid'], $this->data['proxyid']) == 0 && $host['flags'] == ZBX_FLAG_DISCOVERY_NORMAL); } } $proxyFormList->addRow(_('Hosts'), $hostsTweenBox->get(_('Proxy hosts'), _('Other hosts'))); // append tabs to form $proxyTab = new CTabView(); $proxyTab->addTab('proxyTab', _('Proxy'), $proxyFormList); $proxyForm->addItem($proxyTab); // append buttons to form if (!empty($this->data['proxyid'])) { $proxyForm->addItem(makeFormFooter(new CSubmit('save', _('Save')), array(new CSubmit('clone', _('Clone')), new CButtonDelete(_('Delete proxy?'), url_param('form') . url_param('proxyid')), new CButtonCancel()))); } else { $proxyForm->addItem(makeFormFooter(new CSubmit('save', _('Save')), new CButtonCancel())); } // append form to widget
function view() { // global $CJob; $CJob->requireLogin(); global $MCompany; global $MRecruiter; // Validations $this->startValidations(); $this->validate(isset($_GET['id']) and ($entry = $MCompany->get($id = $_GET['id'])) != NULL, $err, 'unknown company'); // Code if ($this->isValid()) { $data = $entry; $me = $MRecruiter->me(); $data['isme'] = !is_null($me) ? idcmp($id, $me['company']) : false; self::displayMetatags('companyprofile'); self::render('companies/viewcompany', $data); return; } self::error($err); self::render('notice'); }