/**
  * Checks if an objects contains any of the given parameters.
  *
  * Example:
  * checkNoParameters($item, array('templateid', 'state'), _('Cannot set "%1$s" for item "%2$s".'), $item['name']);
  * If any of the parameters 'templateid' or 'state' are present in the object, it will be placed in "%1$s"
  * and $item['name'] will be placed in "%2$s".
  *
  * @throws APIException			if any of the parameters are present in the object
  *
  * @param array  $object
  * @param array  $params		array of parameters to check
  * @param string $error
  * @param string $objectName
  */
 protected function checkNoParameters(array $object, array $params, $error, $objectName)
 {
     foreach ($params as $param) {
         if (array_key_exists($param, $object)) {
             $error = _params($error, [$param, $objectName]);
             self::exception(ZBX_API_ERROR_PARAMETERS, $error);
         }
     }
 }
Exemple #2
0
 /**
  * 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;
 }
Exemple #3
0
function vkPost($url, $message = 'message', $title = 'title', $descr = 'descr')
{
    global $url_vk, $url_site;
    $o = '../cache/cookie.txt';
    $h = _params($o, $url_vk, true);
    if ($h['my_id'] == 0) {
        _auth($o, $d, true);
        $h = _params($o, $url_vk, true);
    }
    if ($h['my_id'] != 0) {
        $r = _status($o, $h['post_hash'], $url, $message, $title, $descr, $h['user_id']);
        $c = preg_match_all('/page_wall_count_all/smi', $r, $f);
        if ($c == 0) {
            return false;
        } else {
            return true;
        }
    }
}
 /**
  * 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;
 }
/**
 * Translates the string with respect to the given context and plural forms, also replaces placeholders with supplied arguments.
 * If no translation is found, the original string will be used. Unlimited number of parameters supplied.
 * Parameter placeholders must be defined as %1$s, %2$s etc.
 *
 * Example: _xn('%1$s message for arg1 "%2$s"', '%1$s messages for arg1 "%2$s"', 3, 'context', 'arg1Value');
 * returns: '3 messages for arg1 "arg1Value"'
 *
 * @param string $message			string to translate
 * @param string $messagePlural		string to translate for plural form
 * @param int    $num				number to determine usage of plural form, also is used as first replace argument
 * @param string $context			context of the string
 * @param string $param				parameter to be replace the first placeholder
 * @param string $param,...			unlimited number of optional parameters
 *
 * @return string
 */
function _xn($message, $messagePlural, $num, $context)
{
    $arguments = array_slice(func_get_args(), 4);
    array_unshift($arguments, $num);
    return $context == '' ? _params(ngettext($message, $messagePlural, $num), $arguments) : _params(npgettext($context, $message, $messagePlural, $num), $arguments);
}