Ejemplo n.º 1
0
 public static function get($options)
 {
     global $USER_DETAILS;
     $result = array();
     $nodeCheck = false;
     $user_type = $USER_DETAILS['type'];
     $sort_columns = array('dcheckid', 'druleid');
     // allowed columns for sorting
     $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND, API_OUTPUT_CUSTOM);
     // allowed output options for [ select_* ] params
     $sql_parts = array('select' => array('dchecks' => 'dc.dcheckid'), 'from' => array('dchecks' => 'dchecks dc'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null);
     $def_options = array('nodeids' => null, 'dcheckids' => null, 'druleids' => null, 'dhostids' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'output' => API_OUTPUT_REFER, 'selectDRules' => null, 'selectDHosts' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null);
     $options = zbx_array_merge($def_options, $options);
     if (is_array($options['output'])) {
         unset($sql_parts['select']['dchecks']);
         foreach ($options['output'] as $key => $field) {
             $sql_parts['select'][$field] = ' dc.' . $field;
         }
         $options['output'] = API_OUTPUT_CUSTOM;
     }
     // editable + PERMISSION CHECK
     if (USER_TYPE_SUPER_ADMIN == $user_type) {
     } else {
         if (is_null($options['editable']) && $USER_DETAILS['type'] == USER_TYPE_ZABBIX_ADMIN) {
         } else {
             if (!is_null($options['editable']) && $USER_DETAILS['type'] != USER_TYPE_SUPER_ADMIN) {
                 return array();
             }
         }
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // dcheckids
     if (!is_null($options['dcheckids'])) {
         zbx_value2array($options['dcheckids']);
         $sql_parts['where']['dcheckid'] = DBcondition('dc.dcheckid', $options['dcheckids']);
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('ds.dcheckid', $nodeids);
         }
     }
     // druleids
     if (!is_null($options['druleids'])) {
         zbx_value2array($options['druleids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['druleid'] = 'dc.druleid';
         }
         $sql_parts['where'][] = DBcondition('dc.druleid', $options['druleids']);
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['druleid'] = 'dc.druleid';
         }
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('dc.druleid', $nodeids);
         }
     }
     // dhostids
     if (!is_null($options['dhostids'])) {
         zbx_value2array($options['dhostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['dservices'] = 'dh.dhostid';
         }
         $sql_parts['from']['dhosts'] = 'dhosts dh';
         $sql_parts['where']['dh'] = DBcondition('dh.dhostid', $options['dhostids']);
         $sql_parts['where']['dcdh'] = 'dc.druleid=dh.druleid';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['dhostid'] = 'dh.dhostid';
         }
     }
     // dserviceids
     if (!is_null($options['dserviceids'])) {
         zbx_value2array($options['dserviceids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['dserviceid'] = 'ds.dserviceid';
         }
         $sql_parts['from']['dhosts'] = 'dhosts dh';
         $sql_parts['from']['dservices'] = 'dservices ds';
         $sql_parts['where']['ds'] = DBcondition('ds.dserviceid', $options['dserviceids']);
         $sql_parts['where']['dcdh'] = 'dc.druleid=dh.druleid';
         $sql_parts['where']['dhds'] = 'dh.hostid=ds.hostid';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['dserviceid'] = 'ds.dserviceid';
         }
     }
     // node check !!!!!
     // should last, after all ****IDS checks
     if (!$nodeCheck) {
         $nodeCheck = true;
         $sql_parts['where'][] = DBin_node('dc.dcheckid', $nodeids);
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sql_parts['select']['dchecks'] = 'dc.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sql_parts['select'] = array('count(DISTINCT dc.dcheckid) as rowscount');
         //groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sql_parts['group'] as $key => $fields) {
                 $sql_parts['select'][$key] = $fields;
             }
         }
     }
     // filter
     if (is_array($options['filter'])) {
         zbx_db_filter('dchecks dc', $options, $sql_parts);
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('dchecks dc', $options, $sql_parts);
     }
     // order
     // restrict not allowed columns for sorting
     $options['sortfield'] = str_in_array($options['sortfield'], $sort_columns) ? $options['sortfield'] : '';
     if (!zbx_empty($options['sortfield'])) {
         $sortorder = $options['sortorder'] == ZBX_SORT_DOWN ? ZBX_SORT_DOWN : ZBX_SORT_UP;
         $sql_parts['order'][$options['sortfield']] = 'dc.' . $options['sortfield'] . ' ' . $sortorder;
         if (!str_in_array('dc.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('dc.*', $sql_parts['select'])) {
             $sql_parts['select'][$options['sortfield']] = 'dc.' . $options['sortfield'];
         }
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sql_parts['limit'] = $options['limit'];
     }
     //-------
     $sql_parts['select'] = array_unique($sql_parts['select']);
     $sql_parts['from'] = array_unique($sql_parts['from']);
     $sql_parts['where'] = array_unique($sql_parts['where']);
     $sql_parts['group'] = array_unique($sql_parts['group']);
     $sql_parts['order'] = array_unique($sql_parts['order']);
     $sql_select = '';
     $sql_from = '';
     $sql_where = '';
     $sql_group = '';
     $sql_order = '';
     if (!empty($sql_parts['select'])) {
         $sql_select .= implode(',', $sql_parts['select']);
     }
     if (!empty($sql_parts['from'])) {
         $sql_from .= implode(',', $sql_parts['from']);
     }
     if (!empty($sql_parts['where'])) {
         $sql_where .= implode(' AND ', $sql_parts['where']);
     }
     if (!empty($sql_parts['group'])) {
         $sql_where .= ' GROUP BY ' . implode(',', $sql_parts['group']);
     }
     if (!empty($sql_parts['order'])) {
         $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']);
     }
     $sql_limit = $sql_parts['limit'];
     $sql = 'SELECT ' . zbx_db_distinct($sql_parts) . ' ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . $sql_where . $sql_group . $sql_order;
     //SDI($sql);
     $res = DBselect($sql, $sql_limit);
     while ($dcheck = DBfetch($res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $dcheck;
             } else {
                 $result = $dcheck['rowscount'];
             }
         } else {
             $dcheckids[$dcheck['dcheckid']] = $dcheck['dcheckid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$dcheck['dcheckid']] = array('dcheckid' => $dcheck['dcheckid']);
             } else {
                 if (!isset($result[$dcheck['dcheckid']])) {
                     $result[$dcheck['dcheckid']] = array();
                 }
                 if (!is_null($options['selectDRules']) && !isset($result[$dcheck['dcheckid']]['drules'])) {
                     $result[$dcheck['dcheckid']]['drules'] = array();
                 }
                 if (!is_null($options['selectDHosts']) && !isset($result[$dcheck['dcheckid']]['dhosts'])) {
                     $result[$dcheck['dcheckid']]['dhosts'] = array();
                 }
                 if (!is_null($options['selectHosts']) && !isset($result[$dcheck['dcheckid']]['hosts'])) {
                     $result[$dcheck['dcheckid']]['hosts'] = array();
                 }
                 // druleids
                 if (isset($dcheck['druleid']) && is_null($options['selectDRules'])) {
                     if (!isset($result[$dcheck['dcheckid']]['drules'])) {
                         $result[$dcheck['dcheckid']]['drules'] = array();
                     }
                     $result[$dcheck['dcheckid']]['drules'][] = array('druleid' => $dcheck['druleid']);
                 }
                 // dhostids
                 if (isset($dcheck['dhostid']) && is_null($options['selectDHosts'])) {
                     if (!isset($result[$dcheck['dcheckid']]['dhosts'])) {
                         $result[$dcheck['dcheckid']]['dhosts'] = array();
                     }
                     $result[$dcheck['dcheckid']]['dhosts'][] = array('dhostid' => $dcheck['dhostid']);
                 }
                 $result[$dcheck['dcheckid']] += $dcheck;
             }
         }
     }
     Copt::memoryPick();
     if (!is_null($options['countOutput'])) {
         if (is_null($options['preservekeys'])) {
             $result = zbx_cleanHashes($result);
         }
         return $result;
     }
     // Adding Objects
     // select_drules
     if (!is_null($options['selectDRules'])) {
         $obj_params = array('nodeids' => $nodeids, 'dcheckids' => $dcheckids, 'preservekeys' => 1);
         if (is_array($options['selectDRules']) || str_in_array($options['selectDRules'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['selectDRules'];
             $drules = CDRule::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($drules, 'name');
             }
             foreach ($drules as $druleid => $drule) {
                 unset($drules[$druleid]['dchecks']);
                 $count = array();
                 foreach ($drule['dchecks'] as $dnum => $dcheck) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$dcheck['dcheckid']])) {
                             $count[$dcheck['dcheckid']] = 0;
                         }
                         $count[$dcheck['dcheckid']]++;
                         if ($count[$dcheck['dcheckid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$dcheck['dcheckid']]['drules'][] =& $drules[$druleid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['selectDRules']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $drules = CDRule::get($obj_params);
                 $drules = zbx_toHash($drules, 'dcheckid');
                 foreach ($result as $dcheckid => $dcheck) {
                     if (isset($drules[$dcheckid])) {
                         $result[$dcheckid]['drules'] = $drules[$dcheckid]['rowscount'];
                     } else {
                         $result[$dcheckid]['drules'] = 0;
                     }
                 }
             }
         }
     }
     // selectDHosts
     if (!is_null($options['selectDHosts'])) {
         $obj_params = array('nodeids' => $nodeids, 'dcheckids' => $dcheckids, 'preservekeys' => 1);
         if (is_array($options['selectDHosts']) || str_in_array($options['selectDHosts'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['selectDHosts'];
             $dhosts = CDHost::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($dhosts, 'dhostid');
             }
             foreach ($dhosts as $dhostid => $dhost) {
                 unset($dhosts[$dhostid]['dchecks']);
                 foreach ($dhost['dchecks'] as $snum => $dcheck) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$dcheck['dcheckid']])) {
                             $count[$dcheck['dcheckid']] = 0;
                         }
                         $count[$dcheck['dcheckid']]++;
                         if ($count[$dcheck['dcheckid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$dcheck['dcheckid']]['dhosts'][] =& $dhosts[$dhostid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['selectDHosts']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $dhosts = CDHost::get($obj_params);
                 $dhosts = zbx_toHash($dhosts, 'dhostid');
                 foreach ($result as $dcheckid => $dcheck) {
                     if (isset($dhosts[$dcheckid])) {
                         $result[$dcheckid]['dhosts'] = $dhosts[$dcheckid]['rowscount'];
                     } else {
                         $result[$dcheckid]['dhosts'] = 0;
                     }
                 }
             }
         }
     }
     // select_hosts
     if (!is_null($options['selectHosts'])) {
         $obj_params = array('nodeids' => $nodeids, 'dcheckids' => $dcheckids, 'preservekeys' => 1, 'sortfield' => 'status');
         if (is_array($options['selectHosts']) || str_in_array($options['selectHosts'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['selectHosts'];
             $hosts = CHost::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($hosts, 'hostid');
             }
             foreach ($hosts as $hostid => $host) {
                 unset($hosts[$hostid]['dchecks']);
                 foreach ($host['dchecks'] as $dnum => $dcheck) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$dcheck['dcheckid']])) {
                             $count[$dcheck['dcheckid']] = 0;
                         }
                         $count[$dcheck['dcheckid']]++;
                         if ($count[$dcheck['dcheckid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$dcheck['dcheckid']]['hosts'][] =& $hosts[$hostid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['selectHosts']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $hosts = CHost::get($obj_params);
                 $hosts = zbx_toHash($hosts, 'hostid');
                 foreach ($result as $dcheckid => $dcheck) {
                     if (isset($hosts[$dcheckid])) {
                         $result[$dcheckid]['hosts'] = $hosts[$dcheckid]['rowscount'];
                     } else {
                         $result[$dcheckid]['hosts'] = 0;
                     }
                 }
             }
         }
     }
     Copt::memoryPick();
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Ejemplo n.º 2
0
 /**
  * Get Host data
  *
  * {@source}
  * @access public
  * @static
  * @since 1.8
  * @version 1
  *
  * @param _array $options
  * @param array $options['nodeids'] Node IDs
  * @param array $options['groupids'] HostGroup IDs
  * @param array $options['hostids'] Host IDs
  * @param boolean $options['monitored_hosts'] only monitored Hosts
  * @param boolean $options['templated_hosts'] include templates in result
  * @param boolean $options['with_items'] only with items
  * @param boolean $options['with_monitored_items'] only with monitored items
  * @param boolean $options['with_historical_items'] only with historical items
  * @param boolean $options['with_triggers'] only with triggers
  * @param boolean $options['with_monitored_triggers'] only with monitored triggers
  * @param boolean $options['with_httptests'] only with http tests
  * @param boolean $options['with_monitored_httptests'] only with monitored http tests
  * @param boolean $options['with_graphs'] only with graphs
  * @param boolean $options['editable'] only with read-write permission. Ignored for SuperAdmins
  * @param int $options['extendoutput'] return all fields for Hosts
  * @param boolean $options['select_groups'] select HostGroups
  * @param boolean $options['select_templates'] select Templates
  * @param boolean $options['select_items'] select Items
  * @param boolean $options['select_triggers'] select Triggers
  * @param boolean $options['select_graphs'] select Graphs
  * @param boolean $options['select_applications'] select Applications
  * @param boolean $options['select_macros'] select Macros
  * @param boolean $options['select_profile'] select Profile
  * @param int $options['count'] count Hosts, returned column name is rowscount
  * @param string $options['pattern'] search hosts by pattern in Host name
  * @param string $options['extendPattern'] search hosts by pattern in Host name, ip and DNS
  * @param int $options['limit'] limit selection
  * @param string $options['sortfield'] field to sort by
  * @param string $options['sortorder'] sort order
  * @return array|boolean Host data as array or false if error
  */
 public static function get($options = array())
 {
     global $USER_DETAILS;
     $result = array();
     $nodeCheck = false;
     $user_type = $USER_DETAILS['type'];
     $userid = $USER_DETAILS['userid'];
     $sort_columns = array('hostid', 'host', 'status', 'dns', 'ip');
     // allowed columns for sorting
     $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND, API_OUTPUT_CUSTOM);
     // allowed output options for [ select_* ] params
     $sql_parts = array('select' => array('hosts' => 'h.hostid'), 'from' => array('hosts' => 'hosts h'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null);
     $def_options = array('nodeids' => null, 'groupids' => null, 'hostids' => null, 'proxyids' => null, 'templateids' => null, 'itemids' => null, 'triggerids' => null, 'maintenanceids' => null, 'graphids' => null, 'dhostids' => null, 'dserviceids' => null, 'monitored_hosts' => null, 'templated_hosts' => null, 'proxy_hosts' => null, 'with_items' => null, 'with_monitored_items' => null, 'with_historical_items' => null, 'with_triggers' => null, 'with_monitored_triggers' => null, 'with_httptests' => null, 'with_monitored_httptests' => null, 'with_graphs' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'output' => API_OUTPUT_REFER, 'extendoutput' => null, 'select_groups' => null, 'selectParentTemplates' => null, 'select_items' => null, 'select_triggers' => null, 'select_graphs' => null, 'select_dhosts' => null, 'select_dservices' => null, 'select_applications' => null, 'select_macros' => null, 'select_profile' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null);
     $options = zbx_array_merge($def_options, $options);
     if (!is_null($options['extendoutput'])) {
         $options['output'] = API_OUTPUT_EXTEND;
         if (!is_null($options['select_groups'])) {
             $options['select_groups'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['selectParentTemplates'])) {
             $options['selectParentTemplates'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_items'])) {
             $options['select_items'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_triggers'])) {
             $options['select_triggers'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_graphs'])) {
             $options['select_graphs'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_applications'])) {
             $options['select_applications'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_macros'])) {
             $options['select_macros'] = API_OUTPUT_EXTEND;
         }
     }
     if (is_array($options['output'])) {
         unset($sql_parts['select']['hosts']);
         $sql_parts['select']['hostid'] = ' h.hostid';
         foreach ($options['output'] as $key => $field) {
             $sql_parts['select'][$field] = ' h.' . $field;
         }
         $options['output'] = API_OUTPUT_CUSTOM;
     }
     // editable + PERMISSION CHECK
     if (USER_TYPE_SUPER_ADMIN == $user_type || $options['nopermissions']) {
     } else {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
         $sql_parts['where'][] = 'EXISTS (' . ' SELECT hh.hostid ' . ' FROM hosts hh, hosts_groups hgg, rights r, users_groups ug ' . ' WHERE hh.hostid=h.hostid ' . ' AND hh.hostid=hgg.hostid ' . ' AND r.id=hgg.groupid ' . ' AND r.groupid=ug.usrgrpid ' . ' AND ug.userid=' . $userid . ' AND r.permission>=' . $permission . ' AND NOT EXISTS( ' . ' SELECT hggg.groupid ' . ' FROM hosts_groups hggg, rights rr, users_groups gg ' . ' WHERE hggg.hostid=hgg.hostid ' . ' AND rr.id=hggg.groupid ' . ' AND rr.groupid=gg.usrgrpid ' . ' AND gg.userid=' . $userid . ' AND rr.permission<' . $permission . ' )) ';
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         $sql_parts['where']['hostid'] = DBcondition('h.hostid', $options['hostids']);
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('h.hostid', $nodeids);
         }
     }
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['groupid'] = 'hg.groupid';
         }
         $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
         $sql_parts['where'][] = DBcondition('hg.groupid', $options['groupids']);
         $sql_parts['where']['hgh'] = 'hg.hostid=h.hostid';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['groupid'] = 'hg.groupid';
         }
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('hg.groupid', $nodeids);
         }
     }
     // proxyids
     if (!is_null($options['proxyids'])) {
         zbx_value2array($options['proxyids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['proxy_hostid'] = 'h.proxy_hostid';
         }
         $sql_parts['where'][] = DBcondition('h.proxy_hostid', $options['proxyids']);
     }
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['templateid'] = 'ht.templateid';
         }
         $sql_parts['from']['hosts_templates'] = 'hosts_templates ht';
         $sql_parts['where'][] = DBcondition('ht.templateid', $options['templateids']);
         $sql_parts['where']['hht'] = 'h.hostid=ht.hostid';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['templateid'] = 'ht.templateid';
         }
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('ht.templateid', $nodeids);
         }
     }
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['itemid'] = 'i.itemid';
         }
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['where'][] = DBcondition('i.itemid', $options['itemids']);
         $sql_parts['where']['hi'] = 'h.hostid=i.hostid';
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('i.itemid', $nodeids);
         }
     }
     // triggerids
     if (!is_null($options['triggerids'])) {
         zbx_value2array($options['triggerids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['triggerid'] = 'f.triggerid';
         }
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['where'][] = DBcondition('f.triggerid', $options['triggerids']);
         $sql_parts['where']['hi'] = 'h.hostid=i.hostid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('f.triggerid', $nodeids);
         }
     }
     // graphids
     if (!is_null($options['graphids'])) {
         zbx_value2array($options['graphids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['graphid'] = 'gi.graphid';
         }
         $sql_parts['from']['graphs_items'] = 'graphs_items gi';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['where'][] = DBcondition('gi.graphid', $options['graphids']);
         $sql_parts['where']['igi'] = 'i.itemid=gi.itemid';
         $sql_parts['where']['hi'] = 'h.hostid=i.hostid';
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('gi.graphid', $nodeids);
         }
     }
     // dhostids
     if (!is_null($options['dhostids'])) {
         zbx_value2array($options['dhostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['dhostid'] = 'ds.dhostid';
         }
         $sql_parts['from']['dservices'] = 'dservices ds';
         $sql_parts['where'][] = DBcondition('ds.dhostid', $options['dhostids']);
         $sql_parts['where']['dsh'] = 'ds.ip=h.ip';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['dhostid'] = 'ds.dhostid';
         }
     }
     // dserviceids
     if (!is_null($options['dserviceids'])) {
         zbx_value2array($options['dserviceids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['dserviceid'] = 'ds.dserviceid';
         }
         $sql_parts['from']['dservices'] = 'dservices ds';
         $sql_parts['where'][] = DBcondition('ds.dserviceid', $options['dserviceids']);
         $sql_parts['where']['dsh'] = 'ds.ip=h.ip';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['dserviceid'] = 'ds.dserviceid';
         }
     }
     // maintenanceids
     if (!is_null($options['maintenanceids'])) {
         zbx_value2array($options['maintenanceids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['maintenanceid'] = 'mh.maintenanceid';
         }
         $sql_parts['from']['maintenances_hosts'] = 'maintenances_hosts mh';
         $sql_parts['where'][] = DBcondition('mh.maintenanceid', $options['maintenanceids']);
         $sql_parts['where']['hmh'] = 'h.hostid=mh.hostid';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['maintenanceid'] = 'mh.maintenanceid';
         }
     }
     // node check !!!!!
     // should last, after all ****IDS checks
     if (!$nodeCheck) {
         $nodeCheck = true;
         $sql_parts['where'][] = DBin_node('h.hostid', $nodeids);
     }
     // monitored_hosts, templated_hosts
     if (!is_null($options['monitored_hosts'])) {
         $sql_parts['where']['status'] = 'h.status=' . HOST_STATUS_MONITORED;
     } else {
         if (!is_null($options['templated_hosts'])) {
             $sql_parts['where']['status'] = 'h.status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ',' . HOST_STATUS_TEMPLATE . ')';
         } else {
             if (!is_null($options['proxy_hosts'])) {
                 $sql_parts['where']['status'] = 'h.status IN (' . HOST_STATUS_PROXY_ACTIVE . ',' . HOST_STATUS_PROXY_PASSIVE . ')';
             } else {
                 $sql_parts['where']['status'] = 'h.status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ')';
             }
         }
     }
     // with_items, with_monitored_items, with_historical_items
     if (!is_null($options['with_items'])) {
         $sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid )';
     } else {
         if (!is_null($options['with_monitored_items'])) {
             $sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid AND i.status=' . ITEM_STATUS_ACTIVE . ')';
         } else {
             if (!is_null($options['with_historical_items'])) {
                 $sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid AND (i.status=' . ITEM_STATUS_ACTIVE . ' OR i.status=' . ITEM_STATUS_NOTSUPPORTED . ') AND i.lastvalue IS NOT NULL)';
             }
         }
     }
     // with_triggers, with_monitored_triggers
     if (!is_null($options['with_triggers'])) {
         $sql_parts['where'][] = 'EXISTS( ' . ' SELECT i.itemid ' . ' FROM items i, functions f, triggers t ' . ' WHERE i.hostid=h.hostid ' . ' AND i.itemid=f.itemid ' . ' AND f.triggerid=t.triggerid)';
     } else {
         if (!is_null($options['with_monitored_triggers'])) {
             $sql_parts['where'][] = 'EXISTS( ' . ' SELECT i.itemid ' . ' FROM items i, functions f, triggers t ' . ' WHERE i.hostid=h.hostid ' . ' AND i.status=' . ITEM_STATUS_ACTIVE . ' AND i.itemid=f.itemid ' . ' AND f.triggerid=t.triggerid ' . ' AND t.status=' . TRIGGER_STATUS_ENABLED . ')';
         }
     }
     // with_httptests, with_monitored_httptests
     if (!is_null($options['with_httptests'])) {
         $sql_parts['where'][] = 'EXISTS( ' . ' SELECT a.applicationid ' . ' FROM applications a, httptest ht ' . ' WHERE a.hostid=h.hostid ' . ' AND ht.applicationid=a.applicationid)';
     } else {
         if (!is_null($options['with_monitored_httptests'])) {
             $sql_parts['where'][] = 'EXISTS( ' . ' SELECT a.applicationid ' . ' FROM applications a, httptest ht ' . ' WHERE a.hostid=h.hostid ' . ' AND ht.applicationid=a.applicationid ' . ' AND ht.status=' . HTTPTEST_STATUS_ACTIVE . ')';
         }
     }
     // with_graphs
     if (!is_null($options['with_graphs'])) {
         $sql_parts['where'][] = 'EXISTS( ' . ' SELECT DISTINCT i.itemid ' . ' FROM items i, graphs_items gi ' . ' WHERE i.hostid=h.hostid ' . ' AND i.itemid=gi.itemid)';
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sql_parts['select']['hosts'] = 'h.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sql_parts['select'] = array('count(DISTINCT h.hostid) as rowscount');
         //groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sql_parts['group'] as $key => $fields) {
                 $sql_parts['select'][$key] = $fields;
             }
         }
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('hosts h', $options, $sql_parts);
     }
     // filter
     if (is_array($options['filter'])) {
         zbx_db_filter('hosts h', $options, $sql_parts);
     }
     // order
     // restrict not allowed columns for sorting
     $options['sortfield'] = str_in_array($options['sortfield'], $sort_columns) ? $options['sortfield'] : '';
     if (!zbx_empty($options['sortfield'])) {
         $sortorder = $options['sortorder'] == ZBX_SORT_DOWN ? ZBX_SORT_DOWN : ZBX_SORT_UP;
         $sql_parts['order'][$options['sortfield']] = 'h.' . $options['sortfield'] . ' ' . $sortorder;
         if (!str_in_array('h.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('h.*', $sql_parts['select'])) {
             $sql_parts['select'][$options['sortfield']] = 'h.' . $options['sortfield'];
         }
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sql_parts['limit'] = $options['limit'];
     }
     //-------
     $hostids = array();
     $sql_parts['select'] = array_unique($sql_parts['select']);
     $sql_parts['from'] = array_unique($sql_parts['from']);
     $sql_parts['where'] = array_unique($sql_parts['where']);
     $sql_parts['group'] = array_unique($sql_parts['group']);
     $sql_parts['order'] = array_unique($sql_parts['order']);
     $sql_select = '';
     $sql_from = '';
     $sql_where = '';
     $sql_group = '';
     $sql_order = '';
     if (!empty($sql_parts['select'])) {
         $sql_select .= implode(',', $sql_parts['select']);
     }
     if (!empty($sql_parts['from'])) {
         $sql_from .= implode(',', $sql_parts['from']);
     }
     if (!empty($sql_parts['where'])) {
         $sql_where .= implode(' AND ', $sql_parts['where']);
     }
     if (!empty($sql_parts['group'])) {
         $sql_where .= ' GROUP BY ' . implode(',', $sql_parts['group']);
     }
     if (!empty($sql_parts['order'])) {
         $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']);
     }
     $sql_limit = $sql_parts['limit'];
     $sql = 'SELECT ' . zbx_db_distinct($sql_parts) . ' ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . $sql_where . $sql_group . $sql_order;
     //SDI($sql);
     $res = DBselect($sql, $sql_limit);
     while ($host = DBfetch($res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $host;
             } else {
                 $result = $host['rowscount'];
             }
         } else {
             $hostids[$host['hostid']] = $host['hostid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$host['hostid']] = array('hostid' => $host['hostid']);
             } else {
                 if (!isset($result[$host['hostid']])) {
                     $result[$host['hostid']] = array();
                 }
                 if (!is_null($options['select_groups']) && !isset($result[$host['hostid']]['groups'])) {
                     $result[$host['hostid']]['groups'] = array();
                 }
                 if (!is_null($options['selectParentTemplates']) && !isset($result[$host['hostid']]['parentTemplates'])) {
                     $result[$host['hostid']]['parentTemplates'] = array();
                 }
                 if (!is_null($options['select_items']) && !isset($result[$host['hostid']]['items'])) {
                     $result[$host['hostid']]['items'] = array();
                 }
                 if (!is_null($options['select_profile']) && !isset($result[$host['hostid']]['profile'])) {
                     $result[$host['hostid']]['profile'] = array();
                     $result[$host['hostid']]['profile_ext'] = array();
                 }
                 if (!is_null($options['select_triggers']) && !isset($result[$host['hostid']]['triggers'])) {
                     $result[$host['hostid']]['triggers'] = array();
                 }
                 if (!is_null($options['select_graphs']) && !isset($result[$host['hostid']]['graphs'])) {
                     $result[$host['hostid']]['graphs'] = array();
                 }
                 if (!is_null($options['select_dhosts']) && !isset($result[$host['hostid']]['dhosts'])) {
                     $result[$host['hostid']]['dhosts'] = array();
                 }
                 if (!is_null($options['select_dservices']) && !isset($result[$host['hostid']]['dservices'])) {
                     $result[$host['hostid']]['dservices'] = array();
                 }
                 if (!is_null($options['select_applications']) && !isset($result[$host['hostid']]['applications'])) {
                     $result[$host['hostid']]['applications'] = array();
                 }
                 if (!is_null($options['select_macros']) && !isset($result[$host['hostid']]['macros'])) {
                     $result[$host['hostid']]['macros'] = array();
                 }
                 //					if(!is_null($options['select_maintenances']) && !isset($result[$host['hostid']]['maintenances'])){
                 //						$result[$host['hostid']]['maintenances'] = array();
                 //					}
                 // groupids
                 if (isset($host['groupid']) && is_null($options['select_groups'])) {
                     if (!isset($result[$host['hostid']]['groups'])) {
                         $result[$host['hostid']]['groups'] = array();
                     }
                     $result[$host['hostid']]['groups'][] = array('groupid' => $host['groupid']);
                     unset($host['groupid']);
                 }
                 // templateids
                 if (isset($host['templateid'])) {
                     if (!isset($result[$host['hostid']]['templates'])) {
                         $result[$host['hostid']]['templates'] = array();
                     }
                     $result[$host['hostid']]['templates'][] = array('templateid' => $host['templateid']);
                     unset($host['templateid']);
                 }
                 // triggerids
                 if (isset($host['triggerid']) && is_null($options['select_triggers'])) {
                     if (!isset($result[$host['hostid']]['triggers'])) {
                         $result[$host['hostid']]['triggers'] = array();
                     }
                     $result[$host['hostid']]['triggers'][] = array('triggerid' => $host['triggerid']);
                     unset($host['triggerid']);
                 }
                 // itemids
                 if (isset($host['itemid']) && is_null($options['select_items'])) {
                     if (!isset($result[$host['hostid']]['items'])) {
                         $result[$host['hostid']]['items'] = array();
                     }
                     $result[$host['hostid']]['items'][] = array('itemid' => $host['itemid']);
                     unset($host['itemid']);
                 }
                 // graphids
                 if (isset($host['graphid']) && is_null($options['select_graphs'])) {
                     if (!isset($result[$host['hostid']]['graphs'])) {
                         $result[$host['hostid']]['graphs'] = array();
                     }
                     $result[$host['hostid']]['graphs'][] = array('graphid' => $host['graphid']);
                     unset($host['graphid']);
                 }
                 // dhostids
                 if (isset($host['dhostid']) && is_null($options['select_dhosts'])) {
                     if (!isset($result[$host['hostid']]['dhosts'])) {
                         $result[$host['hostid']]['dhosts'] = array();
                     }
                     $result[$host['hostid']]['dhosts'][] = array('dhostid' => $host['dhostid']);
                     unset($host['dhostid']);
                 }
                 // dserviceids
                 if (isset($host['dserviceid']) && is_null($options['select_dservices'])) {
                     if (!isset($result[$host['hostid']]['dservices'])) {
                         $result[$host['hostid']]['dservices'] = array();
                     }
                     $result[$host['hostid']]['dservices'][] = array('dserviceid' => $host['dserviceid']);
                     unset($host['dserviceid']);
                 }
                 // maintenanceids
                 if (isset($host['maintenanceid'])) {
                     if (!isset($result[$host['hostid']]['maintenanceid'])) {
                         $result[$host['hostid']]['maintenances'] = array();
                     }
                     $result[$host['hostid']]['maintenances'][] = array('maintenanceid' => $host['maintenanceid']);
                     //						unset($host['maintenanceid']);
                 }
                 //---
                 $result[$host['hostid']] += $host;
             }
         }
     }
     Copt::memoryPick();
     if (!is_null($options['countOutput'])) {
         if (is_null($options['preservekeys'])) {
             $result = zbx_cleanHashes($result);
         }
         return $result;
     }
     // Adding Objects
     // Adding Groups
     if (!is_null($options['select_groups']) && str_in_array($options['select_groups'], $subselects_allowed_outputs)) {
         $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_groups'], 'hostids' => $hostids, 'preservekeys' => 1);
         $groups = CHostgroup::get($obj_params);
         foreach ($groups as $groupid => $group) {
             $ghosts = $group['hosts'];
             unset($group['hosts']);
             foreach ($ghosts as $num => $host) {
                 $result[$host['hostid']]['groups'][] = $group;
             }
         }
     }
     // Adding Profiles
     if (!is_null($options['select_profile'])) {
         $sql = 'SELECT hp.* ' . ' FROM hosts_profiles hp ' . ' WHERE ' . DBcondition('hp.hostid', $hostids);
         $db_profile = DBselect($sql);
         while ($profile = DBfetch($db_profile)) {
             $result[$profile['hostid']]['profile'] = $profile;
         }
         $sql = 'SELECT hpe.* ' . ' FROM hosts_profiles_ext hpe ' . ' WHERE ' . DBcondition('hpe.hostid', $hostids);
         $db_profile_ext = DBselect($sql);
         while ($profile_ext = DBfetch($db_profile_ext)) {
             $result[$profile_ext['hostid']]['profile_ext'] = $profile_ext;
         }
     }
     // Adding Templates
     if (!is_null($options['selectParentTemplates'])) {
         $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'preservekeys' => 1);
         if (is_array($options['selectParentTemplates']) || str_in_array($options['selectParentTemplates'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['selectParentTemplates'];
             $templates = CTemplate::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($templates, 'host');
             }
             foreach ($templates as $templateid => $template) {
                 unset($templates[$templateid]['hosts']);
                 $count = array();
                 foreach ($template['hosts'] as $hnum => $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['parentTemplates'][] =& $templates[$templateid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['selectParentTemplates']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $templates = CTemplate::get($obj_params);
                 $templates = zbx_toHash($templates, 'hostid');
                 foreach ($result as $hostid => $host) {
                     if (isset($templates[$hostid])) {
                         $result[$hostid]['templates'] = $templates[$hostid]['rowscount'];
                     } else {
                         $result[$hostid]['templates'] = 0;
                     }
                 }
             }
         }
     }
     // Adding Items
     if (!is_null($options['select_items'])) {
         $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'nopermissions' => 1, 'preservekeys' => 1);
         if (is_array($options['select_items']) || str_in_array($options['select_items'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['select_items'];
             $items = CItem::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($items, 'description');
             }
             foreach ($items as $itemid => $item) {
                 unset($items[$itemid]['hosts']);
                 foreach ($item['hosts'] as $hnum => $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['items'][] =& $items[$itemid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['select_items']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $items = CItem::get($obj_params);
                 $items = zbx_toHash($items, 'hostid');
                 foreach ($result as $hostid => $host) {
                     if (isset($items[$hostid])) {
                         $result[$hostid]['items'] = $items[$hostid]['rowscount'];
                     } else {
                         $result[$hostid]['items'] = 0;
                     }
                 }
             }
         }
     }
     // Adding triggers
     if (!is_null($options['select_triggers'])) {
         $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'nopermissions' => 1, 'preservekeys' => 1);
         if (is_array($options['select_triggers']) || str_in_array($options['select_triggers'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['select_triggers'];
             $triggers = CTrigger::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($triggers, 'description');
             }
             foreach ($triggers as $triggerid => $trigger) {
                 unset($triggers[$triggerid]['hosts']);
                 foreach ($trigger['hosts'] as $hnum => $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['triggers'][] =& $triggers[$triggerid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['select_triggers']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $triggers = CTrigger::get($obj_params);
                 $triggers = zbx_toHash($triggers, 'hostid');
                 foreach ($result as $hostid => $host) {
                     if (isset($triggers[$hostid])) {
                         $result[$hostid]['triggers'] = $triggers[$hostid]['rowscount'];
                     } else {
                         $result[$hostid]['triggers'] = 0;
                     }
                 }
             }
         }
     }
     // Adding graphs
     if (!is_null($options['select_graphs'])) {
         $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'nopermissions' => 1, 'preservekeys' => 1);
         if (is_array($options['select_graphs']) || str_in_array($options['select_graphs'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['select_graphs'];
             $graphs = CGraph::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($graphs, 'name');
             }
             foreach ($graphs as $graphid => $graph) {
                 unset($graphs[$graphid]['hosts']);
                 foreach ($graph['hosts'] as $hnum => $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['graphs'][] =& $graphs[$graphid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['select_graphs']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $graphs = CGraph::get($obj_params);
                 $graphs = zbx_toHash($graphs, 'hostid');
                 foreach ($result as $hostid => $host) {
                     if (isset($graphs[$hostid])) {
                         $result[$hostid]['graphs'] = $graphs[$hostid]['rowscount'];
                     } else {
                         $result[$hostid]['graphs'] = 0;
                     }
                 }
             }
         }
     }
     // Adding discovery hosts
     if (!is_null($options['select_dhosts'])) {
         $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'nopermissions' => 1, 'preservekeys' => 1);
         if (is_array($options['select_dhosts']) || str_in_array($options['select_dhosts'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['select_dhosts'];
             $dhosts = CDHost::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($dhosts, 'dhostid');
             }
             foreach ($dhosts as $dhostid => $dhost) {
                 unset($dhosts[$dhostid]['hosts']);
                 foreach ($dhost['hosts'] as $hnum => $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['dhosts'][] =& $dhosts[$dhostid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['select_dhosts']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $dhosts = CDHost::get($obj_params);
                 $dhosts = zbx_toHash($dhosts, 'hostid');
                 foreach ($result as $hostid => $host) {
                     if (isset($dhosts[$hostid])) {
                         $result[$hostid]['dhosts'] = $dhosts[$hostid]['rowscount'];
                     } else {
                         $result[$hostid]['dhosts'] = 0;
                     }
                 }
             }
         }
     }
     // Adding applications
     if (!is_null($options['select_applications'])) {
         $obj_params = array('nodeids' => $nodeids, 'hostids' => $hostids, 'nopermissions' => 1, 'preservekeys' => 1);
         if (is_array($options['select_applications']) || str_in_array($options['select_applications'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['select_applications'];
             $applications = CApplication::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($applications, 'name');
             }
             foreach ($applications as $applicationid => $application) {
                 unset($applications[$applicationid]['hosts']);
                 foreach ($application['hosts'] as $hnum => $host) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$host['hostid']])) {
                             $count[$host['hostid']] = 0;
                         }
                         $count[$host['hostid']]++;
                         if ($count[$host['hostid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$host['hostid']]['applications'][] =& $applications[$applicationid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['select_applications']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $applications = CApplication::get($obj_params);
                 $applications = zbx_toHash($applications, 'hostid');
                 foreach ($result as $hostid => $host) {
                     if (isset($applications[$hostid])) {
                         $result[$hostid]['applications'] = $applications[$hostid]['rowscount'];
                     } else {
                         $result[$hostid]['applications'] = 0;
                     }
                 }
             }
         }
     }
     // Adding macros
     if (!is_null($options['select_macros']) && str_in_array($options['select_macros'], $subselects_allowed_outputs)) {
         $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_macros'], 'hostids' => $hostids, 'preservekeys' => 1);
         $macros = CUserMacro::get($obj_params);
         foreach ($macros as $macroid => $macro) {
             $mhosts = $macro['hosts'];
             unset($macro['hosts']);
             foreach ($mhosts as $num => $host) {
                 $result[$host['hostid']]['macros'][] = $macro;
             }
         }
     }
     Copt::memoryPick();
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Ejemplo n.º 3
0
 /**
  * Get events data
  *
  * {@source}
  * @access public
  * @static
  * @since 1.8
  * @version 1
  *
  * @param _array $options
  * @param array $options['itemids']
  * @param array $options['hostids']
  * @param array $options['groupids']
  * @param array $options['eventids']
  * @param array $options['applicationids']
  * @param array $options['status']
  * @param array $options['editable']
  * @param array $options['extendoutput']
  * @param array $options['count']
  * @param array $options['pattern']
  * @param array $options['limit']
  * @param array $options['order']
  * @return array|int item data as array or false if error
  */
 public static function get($options = array())
 {
     global $USER_DETAILS;
     $result = array();
     $nodeCheck = array();
     $user_type = $USER_DETAILS['type'];
     $userid = $USER_DETAILS['userid'];
     $sort_columns = array('eventid', 'clock');
     // allowed columns for sorting
     $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND);
     // allowed output options for [ select_* ] params
     $sql_parts = array('select' => array('events' => array('e.eventid')), 'from' => array('events' => 'events e'), 'where' => array(), 'order' => array(), 'limit' => null);
     $def_options = array('nodeids' => null, 'groupids' => null, 'hostids' => null, 'triggerids' => null, 'eventids' => null, 'editable' => null, 'object' => null, 'source' => null, 'acknowledged' => null, 'nopermissions' => null, 'hide_unknown' => null, 'value' => null, 'time_from' => null, 'time_till' => null, 'eventid_from' => null, 'eventid_till' => null, 'output' => API_OUTPUT_REFER, 'extendoutput' => null, 'select_hosts' => null, 'select_items' => null, 'select_triggers' => null, 'countOutput' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null);
     $options = zbx_array_merge($def_options, $options);
     if (!is_null($options['extendoutput'])) {
         $options['output'] = API_OUTPUT_EXTEND;
         if (!is_null($options['select_hosts'])) {
             $options['select_hosts'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_triggers'])) {
             $options['select_triggers'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_items'])) {
             $options['select_items'] = API_OUTPUT_EXTEND;
         }
     }
     // editable + PERMISSION CHECK
     if (USER_TYPE_SUPER_ADMIN == $user_type || $options['nopermissions']) {
     } else {
         if (is_null($options['source']) && is_null($options['object'])) {
             $options['object'] = EVENT_OBJECT_TRIGGER;
         }
         if ($options['object'] == EVENT_OBJECT_TRIGGER || $options['source'] == EVENT_SOURCE_TRIGGER) {
             if (!is_null($options['triggerids'])) {
                 $triggerOptions = array('triggerids' => $options['triggerids'], 'editable' => $options['editable']);
                 $triggers = CTrigger::get($triggerOptions);
                 $options['triggerids'] = zbx_objectValues($triggers, 'triggerid');
             } else {
                 $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
                 $sql_parts['from']['functions'] = 'functions f';
                 $sql_parts['from']['items'] = 'items i';
                 $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
                 $sql_parts['from']['rights'] = 'rights r';
                 $sql_parts['from']['users_groups'] = 'users_groups ug';
                 $sql_parts['where']['e'] = 'e.object=' . EVENT_OBJECT_TRIGGER;
                 $sql_parts['where']['fe'] = 'f.triggerid=e.objectid';
                 $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
                 $sql_parts['where']['hgi'] = 'hg.hostid=i.hostid';
                 $sql_parts['where'][] = 'r.id=hg.groupid ';
                 $sql_parts['where'][] = 'r.groupid=ug.usrgrpid';
                 $sql_parts['where'][] = 'ug.userid=' . $userid;
                 $sql_parts['where'][] = 'r.permission>=' . $permission;
                 $sql_parts['where'][] = 'NOT EXISTS( ' . ' SELECT ff.triggerid ' . ' FROM functions ff, items ii ' . ' WHERE ff.triggerid=e.objectid ' . ' AND ff.itemid=ii.itemid ' . ' AND EXISTS( ' . ' SELECT hgg.groupid ' . ' FROM hosts_groups hgg, rights rr, users_groups gg ' . ' WHERE hgg.hostid=ii.hostid ' . ' AND rr.id=hgg.groupid ' . ' AND rr.groupid=gg.usrgrpid ' . ' AND gg.userid=' . $userid . ' AND rr.permission<' . $permission . '))';
             }
         }
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // eventids
     if (!is_null($options['eventids'])) {
         zbx_value2array($options['eventids']);
         $sql_parts['where'][] = DBcondition('e.eventid', $options['eventids']);
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('e.objectid', $nodeids);
         }
     }
     // triggerids
     if (!is_null($options['triggerids']) && $options['object'] == EVENT_OBJECT_TRIGGER) {
         zbx_value2array($options['triggerids']);
         $sql_parts['where'][] = DBcondition('e.objectid', $options['triggerids']);
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('e.objectid', $nodeids);
         }
     }
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['groupid'] = 'hg.groupid';
         }
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
         $sql_parts['where']['hg'] = DBcondition('hg.groupid', $options['groupids']);
         $sql_parts['where']['hgi'] = 'hg.hostid=i.hostid';
         $sql_parts['where']['fe'] = 'f.triggerid=e.objectid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['hostid'] = 'i.hostid';
         }
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['where']['i'] = DBcondition('i.hostid', $options['hostids']);
         $sql_parts['where']['ft'] = 'f.triggerid=e.objectid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
     }
     // node check !!!!!
     // should last, after all ****IDS checks
     if (!$nodeCheck) {
         $nodeCheck = true;
         $sql_parts['where'][] = DBin_node('e.eventid', $nodeids);
     }
     // object
     if (!is_null($options['object'])) {
         $sql_parts['where']['o'] = 'e.object=' . $options['object'];
     }
     // source
     if (!is_null($options['source'])) {
         $sql_parts['where'][] = 'e.source=' . $options['source'];
     }
     // acknowledged
     if (!is_null($options['acknowledged'])) {
         $sql_parts['where'][] = 'e.acknowledged=' . ($options['acknowledged'] ? 1 : 0);
     }
     // hide_unknown
     if (!is_null($options['hide_unknown'])) {
         $sql_parts['where'][] = 'e.value<>' . TRIGGER_VALUE_UNKNOWN;
     }
     // time_from
     if (!is_null($options['time_from'])) {
         $sql_parts['where'][] = 'e.clock>=' . $options['time_from'];
     }
     // time_till
     if (!is_null($options['time_till'])) {
         $sql_parts['where'][] = 'e.clock<=' . $options['time_till'];
     }
     // eventid_from
     if (!is_null($options['eventid_from'])) {
         $sql_parts['where'][] = 'e.eventid>=' . $options['eventid_from'];
     }
     // eventid_till
     if (!is_null($options['eventid_till'])) {
         $sql_parts['where'][] = 'e.eventid<=' . $options['eventid_till'];
     }
     // value
     if (!is_null($options['value'])) {
         zbx_value2array($options['value']);
         $sql_parts['where'][] = DBcondition('e.value', $options['value']);
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sql_parts['select']['events'] = array('e.*');
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sql_parts['select'] = array('events' => array('COUNT(DISTINCT e.eventid) as rowscount'));
     }
     // order
     // restrict not allowed columns for sorting
     $options['sortfield'] = str_in_array($options['sortfield'], $sort_columns) ? $options['sortfield'] : '';
     if (!zbx_empty($options['sortfield'])) {
         $sortorder = $options['sortorder'] == ZBX_SORT_DOWN ? ZBX_SORT_DOWN : ZBX_SORT_UP;
         $sql_parts['order'][] = 'e.' . $options['sortfield'] . ' ' . $sortorder;
         if (!is_null($options['triggerids']) && $options['sortfield'] == 'clock') {
             $sql_parts['where']['o'] = '(e.object-0)=' . EVENT_OBJECT_TRIGGER;
         }
         $eventFields = $sql_parts['select']['events'];
         if (!str_in_array('e.' . $options['sortfield'], $eventFields) && !str_in_array('e.*', $eventFields)) {
             $sql_parts['select']['events'][] = 'e.' . $options['sortfield'];
         }
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sql_parts['limit'] = $options['limit'];
     }
     // select_********
     if ($options['output'] != API_OUTPUT_EXTEND && (!is_null($options['select_hosts']) || !is_null($options['select_triggers']) || !is_null($options['select_items']))) {
         $sql_parts['select']['events'][] = 'e.object';
         $sql_parts['select']['events'][] = 'e.objectid';
     }
     //---------------
     $eventids = array();
     $triggerids = array();
     // Event fields
     $sql_parts['select']['events'] = implode(',', array_unique($sql_parts['select']['events']));
     $sql_parts['select'] = array_unique($sql_parts['select']);
     $sql_parts['from'] = array_unique($sql_parts['from']);
     $sql_parts['where'] = array_unique($sql_parts['where']);
     $sql_parts['order'] = array_unique($sql_parts['order']);
     $sql_select = '';
     $sql_from = '';
     $sql_where = '';
     $sql_order = '';
     if (!empty($sql_parts['select'])) {
         $sql_select .= implode(',', $sql_parts['select']);
     }
     if (!empty($sql_parts['from'])) {
         $sql_from .= implode(',', $sql_parts['from']);
     }
     if (!empty($sql_parts['where'])) {
         $sql_where .= implode(' AND ', $sql_parts['where']);
     }
     if (!empty($sql_parts['order'])) {
         $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']);
     }
     $sql_limit = $sql_parts['limit'];
     $sql = 'SELECT ' . zbx_db_distinct($sql_parts) . ' ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . $sql_where . $sql_order;
     $db_res = DBselect($sql, $sql_limit);
     //SDI($sql);
     while ($event = DBfetch($db_res)) {
         if ($options['countOutput']) {
             $result = $event['rowscount'];
         } else {
             $eventids[$event['eventid']] = $event['eventid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$event['eventid']] = array('eventid' => $event['eventid']);
             } else {
                 if (isset($event['object']) && $event['object'] == EVENT_OBJECT_TRIGGER) {
                     $triggerids[$event['objectid']] = $event['objectid'];
                 }
                 if (!isset($result[$event['eventid']])) {
                     $result[$event['eventid']] = array();
                 }
                 if (!is_null($options['select_hosts']) && !isset($result[$event['eventid']]['hosts'])) {
                     $result[$event['eventid']]['hosts'] = array();
                 }
                 if (!is_null($options['select_triggers']) && !isset($result[$event['eventid']]['triggers'])) {
                     $result[$event['eventid']]['triggers'] = array();
                 }
                 if (!is_null($options['select_items']) && !isset($result[$event['eventid']]['items'])) {
                     $result[$event['eventid']]['items'] = array();
                 }
                 // hostids
                 if (isset($event['hostid']) && is_null($options['select_hosts'])) {
                     if (!isset($result[$event['eventid']]['hosts'])) {
                         $result[$event['eventid']]['hosts'] = array();
                     }
                     $result[$event['eventid']]['hosts'][] = array('hostid' => $event['hostid']);
                     unset($event['hostid']);
                 }
                 // triggerids
                 if (isset($event['triggerid']) && is_null($options['select_triggers'])) {
                     if (!isset($result[$event['eventid']]['triggers'])) {
                         $result[$event['eventid']]['triggers'] = array();
                     }
                     $result[$event['eventid']]['triggers'][] = array('triggerid' => $event['triggerid']);
                     unset($event['triggerid']);
                 }
                 // itemids
                 if (isset($event['itemid']) && is_null($options['select_items'])) {
                     if (!isset($result[$event['eventid']]['items'])) {
                         $result[$event['eventid']]['items'] = array();
                     }
                     $result[$event['eventid']]['items'][] = array('itemid' => $event['itemid']);
                     unset($event['itemid']);
                 }
                 $result[$event['eventid']] += $event;
             }
         }
     }
     Copt::memoryPick();
     if (!is_null($options['countOutput'])) {
         if (is_null($options['preservekeys'])) {
             $result = zbx_cleanHashes($result);
         }
         return $result;
     }
     // Adding Objects
     // Adding hosts
     if (!is_null($options['select_hosts']) && str_in_array($options['select_hosts'], $subselects_allowed_outputs)) {
         $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_hosts'], 'triggerids' => $triggerids, 'nopermissions' => 1, 'preservekeys' => 1);
         $hosts = CHost::get($obj_params);
         $triggers = array();
         foreach ($hosts as $hostid => $host) {
             $htriggers = $host['triggers'];
             unset($host['triggers']);
             foreach ($htriggers as $tnum => $trigger) {
                 $triggerid = $trigger['triggerid'];
                 if (!isset($triggers[$triggerid])) {
                     $triggers[$triggerid] = array('hosts' => array());
                 }
                 $triggers[$triggerid]['hosts'][] = $host;
             }
         }
         foreach ($result as $eventid => $event) {
             if (isset($triggers[$event['objectid']])) {
                 $result[$eventid]['hosts'] = $triggers[$event['objectid']]['hosts'];
             } else {
                 $result[$eventid]['hosts'] = array();
             }
         }
     }
     // Adding triggers
     if (!is_null($options['select_triggers']) && str_in_array($options['select_triggers'], $subselects_allowed_outputs)) {
         $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_triggers'], 'triggerids' => $triggerids, 'nopermissions' => 1, 'preservekeys' => 1);
         $triggers = CTrigger::get($obj_params);
         foreach ($result as $eventid => $event) {
             if (isset($triggers[$event['objectid']])) {
                 $result[$eventid]['triggers'][] = $triggers[$event['objectid']];
             } else {
                 $result[$eventid]['triggers'] = array();
             }
         }
     }
     // Adding items
     if (!is_null($options['select_items']) && str_in_array($options['select_items'], $subselects_allowed_outputs)) {
         $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_items'], 'triggerids' => $triggerids, 'webitems' => 1, 'nopermissions' => 1, 'preservekeys' => 1);
         $db_items = CItem::get($obj_params);
         $items = array();
         foreach ($db_items as $itemid => $item) {
             $itriggers = $item['triggers'];
             unset($item['triggers']);
             foreach ($itriggers as $trigger) {
                 if (!isset($items[$trigger['triggerid']])) {
                     $items[$trigger['triggerid']] = array();
                 }
                 $items[$trigger['triggerid']][] = $item;
             }
         }
         foreach ($result as $eventid => $event) {
             if (isset($items[$event['objectid']])) {
                 $result[$eventid]['items'] = $items[$event['objectid']];
             } else {
                 $result[$eventid]['items'] = array();
             }
         }
     }
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Ejemplo n.º 4
0
    /**
     * Get Users data
     *
     * {@source}
     * @access public
     * @static
     * @since 1.8
     * @version 1
     *
     * @param _array $options
     * @param array $options['nodeids'] filter by Node IDs
     * @param array $options['usrgrpids'] filter by UserGroup IDs
     * @param array $options['userids'] filter by User IDs
     * @param boolean $options['type'] filter by User type [ USER_TYPE_ZABBIX_USER: 1, USER_TYPE_ZABBIX_ADMIN: 2, USER_TYPE_SUPER_ADMIN: 3 ]
     * @param boolean $options['select_usrgrps'] extend with UserGroups data for each User
     * @param boolean $options['get_access'] extend with access data for each User
     * @param boolean $options['extendoutput'] output only User IDs if not set.
     * @param boolean $options['count'] output only count of objects in result. ( result returned in property 'rowscount' )
     * @param string $options['pattern'] filter by Host name containing only give pattern
     * @param int $options['limit'] output will be limited to given number
     * @param string $options['sortfield'] output will be sorted by given property [ 'userid', 'alias' ]
     * @param string $options['sortorder'] output will be sorted in given order [ 'ASC', 'DESC' ]
     * @return array
     */
    public static function get($options = array())
    {
        global $USER_DETAILS;
        $result = array();
        $user_type = $USER_DETAILS['type'];
        $userid = $USER_DETAILS['userid'];
        $sort_columns = array('userid', 'alias');
        // allowed columns for sorting
        $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND);
        // allowed output options for [ select_* ] params
        $sql_parts = array('select' => array('users' => 'u.userid'), 'from' => array('users' => 'users u'), 'where' => array(), 'order' => array(), 'limit' => null);
        $def_options = array('nodeids' => null, 'usrgrpids' => null, 'userids' => null, 'mediaids' => null, 'mediatypeids' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'extendoutput' => null, 'output' => API_OUTPUT_REFER, 'editable' => null, 'select_usrgrps' => null, 'select_medias' => null, 'select_mediatypes' => null, 'get_access' => null, 'countOutput' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null);
        $options = zbx_array_merge($def_options, $options);
        if (!is_null($options['extendoutput'])) {
            $options['output'] = API_OUTPUT_EXTEND;
            if (!is_null($options['select_usrgrps'])) {
                $options['select_usrgrps'] = API_OUTPUT_EXTEND;
            }
        }
        // PERMISSION CHECK
        if (USER_TYPE_SUPER_ADMIN == $user_type) {
        } else {
            if (is_null($options['editable']) && $USER_DETAILS['type'] == USER_TYPE_ZABBIX_ADMIN) {
                $sql_parts['from']['users_groups'] = 'users_groups ug';
                $sql_parts['where']['uug'] = 'u.userid=ug.userid';
                $sql_parts['where'][] = 'ug.usrgrpid IN (' . ' SELECT uug.usrgrpid' . ' FROM users_groups uug' . ' WHERE uug.userid=' . $USER_DETAILS['userid'] . ' )';
            } else {
                if (!is_null($options['editable']) || $USER_DETAILS['type'] != USER_TYPE_SUPER_ADMIN) {
                    $options['userids'] = $USER_DETAILS['userid'];
                }
            }
        }
        // nodeids
        $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
        // userids
        if (!is_null($options['userids'])) {
            zbx_value2array($options['userids']);
            $sql_parts['where'][] = DBcondition('u.userid', $options['userids']);
        }
        // usrgrpids
        if (!is_null($options['usrgrpids'])) {
            zbx_value2array($options['usrgrpids']);
            if ($options['output'] != API_OUTPUT_SHORTEN) {
                $sql_parts['select']['usrgrpid'] = 'ug.usrgrpid';
            }
            $sql_parts['from']['users_groups'] = 'users_groups ug';
            $sql_parts['where'][] = DBcondition('ug.usrgrpid', $options['usrgrpids']);
            $sql_parts['where']['uug'] = 'u.userid=ug.userid';
        }
        // mediaids
        if (!is_null($options['mediaids'])) {
            zbx_value2array($options['mediaids']);
            if ($options['output'] != API_OUTPUT_SHORTEN) {
                $sql_parts['select']['mediaid'] = 'm.mediaid';
            }
            $sql_parts['from']['media'] = 'media m';
            $sql_parts['where'][] = DBcondition('m.mediaid', $options['mediaids']);
            $sql_parts['where']['mu'] = 'm.userid=u.userid';
        }
        // mediatypeids
        if (!is_null($options['mediatypeids'])) {
            zbx_value2array($options['mediatypeids']);
            if ($options['output'] != API_OUTPUT_SHORTEN) {
                $sql_parts['select']['mediatypeid'] = 'm.mediatypeid';
            }
            $sql_parts['from']['media'] = 'media m';
            $sql_parts['where'][] = DBcondition('m.mediatypeid', $options['mediatypeids']);
            $sql_parts['where']['mu'] = 'm.userid=u.userid';
        }
        // extendoutput
        if ($options['output'] == API_OUTPUT_EXTEND) {
            $sql_parts['select']['users'] = 'u.*';
        }
        // countOutput
        if (!is_null($options['countOutput'])) {
            $options['sortfield'] = '';
            $sql_parts['select'] = array('count(u.userid) as rowscount');
        }
        // filter
        if (is_array($options['filter'])) {
            zbx_db_filter('users u', $options, $sql_parts);
        }
        // search
        if (is_array($options['search'])) {
            unset($options['search']['passwd']);
            zbx_db_search('users u', $options, $sql_parts);
        }
        // order
        // restrict not allowed columns for sorting
        $options['sortfield'] = str_in_array($options['sortfield'], $sort_columns) ? $options['sortfield'] : '';
        if (!zbx_empty($options['sortfield'])) {
            $sortorder = $options['sortorder'] == ZBX_SORT_DOWN ? ZBX_SORT_DOWN : ZBX_SORT_UP;
            $sql_parts['order'][] = 'u.' . $options['sortfield'] . ' ' . $sortorder;
            if (!str_in_array('u.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('u.*', $sql_parts['select'])) {
                $sql_parts['select'][] = 'u.' . $options['sortfield'];
            }
        }
        // limit
        if (zbx_ctype_digit($options['limit']) && $options['limit']) {
            $sql_parts['limit'] = $options['limit'];
        }
        //-------
        $userids = array();
        $sql_parts['select'] = array_unique($sql_parts['select']);
        $sql_parts['from'] = array_unique($sql_parts['from']);
        $sql_parts['where'] = array_unique($sql_parts['where']);
        $sql_parts['order'] = array_unique($sql_parts['order']);
        $sql_select = '';
        $sql_from = '';
        $sql_where = '';
        $sql_order = '';
        if (!empty($sql_parts['select'])) {
            $sql_select .= implode(',', $sql_parts['select']);
        }
        if (!empty($sql_parts['from'])) {
            $sql_from .= implode(',', $sql_parts['from']);
        }
        if (!empty($sql_parts['where'])) {
            $sql_where .= ' AND ' . implode(' AND ', $sql_parts['where']);
        }
        if (!empty($sql_parts['order'])) {
            $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']);
        }
        $sql_limit = $sql_parts['limit'];
        $sql = 'SELECT ' . zbx_db_distinct($sql_parts) . ' ' . $sql_select . '
				FROM ' . $sql_from . '
				WHERE ' . DBin_node('u.userid', $nodeids) . $sql_where . $sql_order;
        //SDI($sql);
        $res = DBselect($sql, $sql_limit);
        while ($user = DBfetch($res)) {
            if (!is_null($options['countOutput'])) {
                $result = $user['rowscount'];
            } else {
                $userids[$user['userid']] = $user['userid'];
                if ($options['output'] == API_OUTPUT_SHORTEN) {
                    $result[$user['userid']] = array('userid' => $user['userid']);
                } else {
                    if (!isset($result[$user['userid']])) {
                        $result[$user['userid']] = array();
                    }
                    if ($options['select_usrgrps'] && !isset($result[$user['userid']]['usrgrps'])) {
                        $result[$user['userid']]['usrgrps'] = array();
                    }
                    // usrgrpids
                    if (isset($user['usrgrpid']) && is_null($options['select_usrgrps'])) {
                        if (!isset($result[$user['userid']]['usrgrps'])) {
                            $result[$user['userid']]['usrgrps'] = array();
                        }
                        $result[$user['userid']]['usrgrps'][] = array('usrgrpid' => $user['usrgrpid']);
                        unset($user['usrgrpid']);
                    }
                    // mediaids
                    if (isset($user['mediaid']) && is_null($options['select_medias'])) {
                        if (!isset($result[$user['userid']]['medias'])) {
                            $result[$user['userid']]['medias'] = array();
                        }
                        $result[$user['userid']]['medias'][] = array('mediaid' => $user['mediaid']);
                        unset($user['mediaid']);
                    }
                    // mediatypeids
                    if (isset($user['mediatypeid']) && is_null($options['select_mediatypes'])) {
                        if (!isset($result[$user['userid']]['mediatypes'])) {
                            $result[$user['userid']]['mediatypes'] = array();
                        }
                        $result[$user['userid']]['mediatypes'][] = array('mediatypeid' => $user['mediatypeid']);
                        unset($user['mediatypeid']);
                    }
                    $result[$user['userid']] += $user;
                }
            }
        }
        Copt::memoryPick();
        if (!is_null($options['countOutput'])) {
            if (is_null($options['preservekeys'])) {
                $result = zbx_cleanHashes($result);
            }
            return $result;
        }
        // Adding Objects
        if (!is_null($options['get_access'])) {
            foreach ($result as $userid => $user) {
                $result[$userid] += array('api_access' => 0, 'gui_access' => 0, 'debug_mode' => 0, 'users_status' => 0);
            }
            $sql = 'SELECT ug.userid, MAX(g.api_access) as api_access,  MAX(g.gui_access) as gui_access,
						MAX(g.debug_mode) as debug_mode, MAX(g.users_status) as users_status' . ' FROM usrgrp g, users_groups ug ' . ' WHERE ' . DBcondition('ug.userid', $userids) . ' AND g.usrgrpid=ug.usrgrpid ' . ' GROUP BY ug.userid';
            $access = DBselect($sql);
            while ($useracc = DBfetch($access)) {
                $result[$useracc['userid']] = zbx_array_merge($result[$useracc['userid']], $useracc);
            }
        }
        // Adding usergroups
        if (!is_null($options['select_usrgrps']) && str_in_array($options['select_usrgrps'], $subselects_allowed_outputs)) {
            $obj_params = array('output' => $options['select_usrgrps'], 'userids' => $userids, 'preservekeys' => 1);
            $usrgrps = CUserGroup::get($obj_params);
            foreach ($usrgrps as $usrgrpid => $usrgrp) {
                $uusers = $usrgrp['users'];
                unset($usrgrp['users']);
                foreach ($uusers as $num => $user) {
                    $result[$user['userid']]['usrgrps'][] = $usrgrp;
                }
            }
        }
        // TODO:
        // Adding medias
        if (!is_null($options['select_medias']) && str_in_array($options['select_medias'], $subselects_allowed_outputs)) {
        }
        // Adding mediatypes
        if (!is_null($options['select_mediatypes']) && str_in_array($options['select_mediatypes'], $subselects_allowed_outputs)) {
        }
        // removing keys (hash -> array)
        if (is_null($options['preservekeys'])) {
            $result = zbx_cleanHashes($result);
        }
        return $result;
    }
Ejemplo n.º 5
0
 /**
  * Get Media types data
  *
  * {@source}
  * @access public
  * @static
  * @since 1.8
  * @version 1
  *
  * @param _array $options
  * @param array $options['nodeids'] filter by Node IDs
  * @param array $options['mediatypeids'] filter by Mediatype IDs
  * @param boolean $options['type'] filter by Mediatype type [ USER_TYPE_ZABBIX_USER: 1, USER_TYPE_ZABBIX_ADMIN: 2, USER_TYPE_SUPER_ADMIN: 3 ]
  * @param boolean $options['output'] output only Mediatype IDs if not set.
  * @param boolean $options['count'] output only count of objects in result. ( result returned in property 'rowscount' )
  * @param string $options['pattern'] filter by Host name containing only give pattern
  * @param int $options['limit'] output will be limited to given number
  * @param string $options['sortfield'] output will be sorted by given property [ 'mediatypeid', 'alias' ]
  * @param string $options['sortorder'] output will be sorted in given order [ 'ASC', 'DESC' ]
  * @return array
  */
 public static function get($options = array())
 {
     global $USER_DETAILS;
     $result = array();
     $nodeCheck = false;
     $user_type = $USER_DETAILS['type'];
     $userid = $USER_DETAILS['userid'];
     $sort_columns = array('mediatypeid');
     // allowed columns for sorting
     $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND);
     // allowed output options for [ select_* ] params
     $sql_parts = array('select' => array('media_type' => 'mt.mediatypeid'), 'from' => array('media_type' => 'media_type mt'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null);
     $def_options = array('nodeids' => null, 'mediatypeids' => null, 'mediaids' => null, 'userids' => null, 'editable' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'extendoutput' => null, 'output' => API_OUTPUT_REFER, 'select_users' => null, 'select_medias' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null);
     $options = zbx_array_merge($def_options, $options);
     if (!is_null($options['extendoutput'])) {
         $options['output'] = API_OUTPUT_EXTEND;
         if (!is_null($options['select_users'])) {
             $options['select_users'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_medias'])) {
             $options['select_medias'] = API_OUTPUT_EXTEND;
         }
     }
     // PERMISSION CHECK
     if (USER_TYPE_SUPER_ADMIN == $user_type) {
     } else {
         if (is_null($options['editable']) && $USER_DETAILS['type'] == USER_TYPE_ZABBIX_ADMIN) {
         } else {
             if (!is_null($options['editable']) || $USER_DETAILS['type'] != USER_TYPE_SUPER_ADMIN) {
                 return array();
             }
         }
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // mediatypeids
     if (!is_null($options['mediatypeids'])) {
         zbx_value2array($options['mediatypeids']);
         $sql_parts['where'][] = DBcondition('mt.mediatypeid', $options['mediatypeids']);
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('mt.mediatypeid', $nodeids);
         }
     }
     // mediaids
     if (!is_null($options['mediaids'])) {
         zbx_value2array($options['mediaids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['mediaid'] = 'm.mediaid';
         }
         $sql_parts['from']['medias'] = 'medias m';
         $sql_parts['where'][] = DBcondition('m.mediaid', $options['mediaids']);
         $sql_parts['where']['mmt'] = 'm.mediatypeid=mt.mediatypeid';
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('m.mediaid', $nodeids);
         }
     }
     // userids
     if (!is_null($options['userids'])) {
         zbx_value2array($options['userids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['userid'] = 'm.userid';
         }
         $sql_parts['from']['medias'] = 'medias m';
         $sql_parts['where'][] = DBcondition('m.userid', $options['userids']);
         $sql_parts['where']['mmt'] = 'm.mediatypeid=mt.mediatypeid';
         if (!$nodeCheck) {
             $nodeCheck = true;
             $sql_parts['where'][] = DBin_node('m.userid', $nodeids);
         }
     }
     // node check !!!!!
     // should last, after all ****IDS checks
     if (!$nodeCheck) {
         $nodeCheck = true;
         $sql_parts['where'][] = DBin_node('mt.mediatypeid', $nodeids);
     }
     // filter
     if (is_array($options['filter'])) {
         zbx_db_filter('media_type mt', $options, $sql_parts);
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('media_type mt', $options, $sql_parts);
     }
     // extendoutput
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sql_parts['select']['media_type'] = 'mt.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sql_parts['select'] = array('count(DISTINCT mt.mediatypeid) as rowscount');
         //groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sql_parts['group'] as $key => $fields) {
                 $sql_parts['select'][$key] = $fields;
             }
         }
     }
     // order
     // restrict not allowed columns for sorting
     $options['sortfield'] = str_in_array($options['sortfield'], $sort_columns) ? $options['sortfield'] : '';
     if (!zbx_empty($options['sortfield'])) {
         $sortorder = $options['sortorder'] == ZBX_SORT_DOWN ? ZBX_SORT_DOWN : ZBX_SORT_UP;
         $sql_parts['order'][] = 'mt.' . $options['sortfield'] . ' ' . $sortorder;
         if (!str_in_array('mt.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('mt.*', $sql_parts['select'])) {
             $sql_parts['select'][] = 'mt.' . $options['sortfield'];
         }
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sql_parts['limit'] = $options['limit'];
     }
     //-------
     $mediatypeids = array();
     $sql_parts['select'] = array_unique($sql_parts['select']);
     $sql_parts['from'] = array_unique($sql_parts['from']);
     $sql_parts['where'] = array_unique($sql_parts['where']);
     $sql_parts['group'] = array_unique($sql_parts['group']);
     $sql_parts['order'] = array_unique($sql_parts['order']);
     $sql_select = '';
     $sql_from = '';
     $sql_where = '';
     $sql_group = '';
     $sql_order = '';
     if (!empty($sql_parts['select'])) {
         $sql_select .= implode(',', $sql_parts['select']);
     }
     if (!empty($sql_parts['from'])) {
         $sql_from .= implode(',', $sql_parts['from']);
     }
     if (!empty($sql_parts['where'])) {
         $sql_where .= implode(' AND ', $sql_parts['where']);
     }
     if (!empty($sql_parts['group'])) {
         $sql_where .= ' GROUP BY ' . implode(',', $sql_parts['group']);
     }
     if (!empty($sql_parts['order'])) {
         $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']);
     }
     $sql_limit = $sql_parts['limit'];
     $sql = 'SELECT ' . zbx_db_distinct($sql_parts) . ' ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . $sql_where . $sql_group . $sql_order;
     $res = DBselect($sql, $sql_limit);
     while ($mediatype = DBfetch($res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $mediatype;
             } else {
                 $result = $mediatype['rowscount'];
             }
         } else {
             $mediatypeids[$mediatype['mediatypeid']] = $mediatype['mediatypeid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$mediatype['mediatypeid']] = array('mediatypeid' => $mediatype['mediatypeid']);
             } else {
                 if (!isset($result[$mediatype['mediatypeid']])) {
                     $result[$mediatype['mediatypeid']] = array();
                 }
                 // mediaids
                 if (isset($mediatype['mediaid']) && is_null($options['select_medias'])) {
                     if (!isset($result[$mediatype['mediatypeid']]['medias'])) {
                         $result[$mediatype['mediatypeid']]['medias'] = array();
                     }
                     $result[$mediatype['mediatypeid']]['medias'][] = array('mediaid' => $mediatype['mediaid']);
                     unset($mediatype['mediaid']);
                 }
                 // userids
                 if (isset($mediatype['userid']) && is_null($options['select_users'])) {
                     if (!isset($result[$mediatype['mediatypeid']]['users'])) {
                         $result[$mediatype['mediatypeid']]['users'] = array();
                     }
                     $result[$mediatype['mediatypeid']]['users'][] = array('userid' => $mediatype['userid']);
                     unset($mediatype['userid']);
                 }
                 $result[$mediatype['mediatypeid']] += $mediatype;
             }
         }
     }
     Copt::memoryPick();
     if (!is_null($options['countOutput'])) {
         if (is_null($options['preservekeys'])) {
             $result = zbx_cleanHashes($result);
         }
         return $result;
     }
     // Adding Objects
     // Adding users
     if (!is_null($options['select_users']) && str_in_array($options['select_users'], $subselects_allowed_outputs)) {
         $obj_params = array('output' => $options['select_users'], 'mediatypeids' => $mediatypeids, 'preservekeys' => 1);
         $users = CUser::get($obj_params);
         foreach ($users as $userid => $user) {
             $umediatypes = $user['mediatypes'];
             unset($user['mediatypes']);
             foreach ($umediatypes as $num => $mediatype) {
                 $result[$mediatype['mediatypeid']]['users'][] = $user;
             }
         }
     }
     // TODO
     // Adding medias (need media class)
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
Ejemplo n.º 6
0
 /**
  * Get Triggers data
  *
  * @param _array $options
  * @param array $options['itemids']
  * @param array $options['hostids']
  * @param array $options['groupids']
  * @param array $options['triggerids']
  * @param array $options['applicationids']
  * @param array $options['status']
  * @param array $options['editable']
  * @param array $options['extendoutput']
  * @param array $options['count']
  * @param array $options['pattern']
  * @param array $options['limit']
  * @param array $options['order']
  * @return array|int item data as array or false if error
  */
 public static function get($options = array())
 {
     global $USER_DETAILS;
     $result = array();
     $user_type = $USER_DETAILS['type'];
     $userid = $USER_DETAILS['userid'];
     $sort_columns = array('triggerid', 'description', 'status', 'priority', 'lastchange');
     // allowed columns for sorting
     $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND);
     // allowed output options for [ select_* ] params
     $sql_parts = array('select' => array('triggers' => 't.triggerid'), 'from' => array('t' => 'triggers t'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null);
     $def_options = array('nodeids' => null, 'groupids' => null, 'templateids' => null, 'hostids' => null, 'triggerids' => null, 'itemids' => null, 'applicationids' => null, 'functions' => null, 'inherited' => null, 'templated' => null, 'monitored' => null, 'active' => null, 'maintenance' => null, 'withUnacknowledgedEvents' => null, 'withAcknowledgedEvents' => null, 'withLastEventUnacknowledged' => null, 'skipDependent' => null, 'nopermissions' => null, 'editable' => null, 'lastChangeSince' => null, 'lastChangeTill' => null, 'group' => null, 'host' => null, 'only_true' => null, 'min_severity' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'expandData' => null, 'expandDescription' => null, 'output' => API_OUTPUT_REFER, 'extendoutput' => null, 'select_groups' => null, 'select_hosts' => null, 'select_items' => null, 'select_functions' => null, 'select_dependencies' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null, 'limitSelects' => null);
     $options = zbx_array_merge($def_options, $options);
     if (!is_null($options['extendoutput'])) {
         $options['output'] = API_OUTPUT_EXTEND;
         if (!is_null($options['select_hosts'])) {
             $options['select_hosts'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_items'])) {
             $options['select_items'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_dependencies'])) {
             $options['select_dependencies'] = API_OUTPUT_EXTEND;
         }
     }
     if (is_array($options['output'])) {
         unset($sql_parts['select']['triggers']);
         foreach ($options['output'] as $key => $field) {
             $sql_parts['select'][$field] = ' t.' . $field;
         }
         $options['output'] = API_OUTPUT_CUSTOM;
     }
     // editable + PERMISSION CHECK
     if (USER_TYPE_SUPER_ADMIN == $user_type || $options['nopermissions']) {
     } else {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
         /*/
         			$sql_parts['where'][] = ' EXISTS(  '.
         						' SELECT tt.triggerid  '.
         						' FROM triggers tt,functions ff,items ii,hosts_groups hgg,rights rr,users_groups ugg '.
         						' WHERE t.triggerid=tt.triggerid  '.
         							' AND ff.triggerid=tt.triggerid  '.
         							' AND ff.itemid=ii.itemid  '.
         							' AND hgg.hostid=ii.hostid  '.
         							' AND rr.id=hgg.groupid  '.
         							' AND rr.groupid=ugg.usrgrpid  '.
         							' AND ugg.userid='.$userid.
         							' AND rr.permission>='.$permission.
         							' AND NOT EXISTS(  '.
         								' SELECT fff.triggerid  '.
         								' FROM functions fff, items iii  '.
         								' WHERE fff.triggerid=tt.triggerid '.
         									' AND fff.itemid=iii.itemid '.		'    '.
         									' AND EXISTS( '.
         										' SELECT hggg.groupid '.
         										' FROM hosts_groups hggg, rights rrr, users_groups uggg '.
         										' WHERE hggg.hostid=iii.hostid '.
         											' AND rrr.id=hggg.groupid '.
         											' AND rrr.groupid=uggg.usrgrpid '.
         											' AND uggg.userid='.$userid.
         											' AND rrr.permission<'.$permission.
         										' ) '.
         								' ) '.
         						' ) ';
         //*/
         //*/
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
         $sql_parts['from']['rights'] = 'rights r';
         $sql_parts['from']['users_groups'] = 'users_groups ug';
         $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
         $sql_parts['where']['hgi'] = 'hg.hostid=i.hostid';
         $sql_parts['where'][] = 'r.id=hg.groupid ';
         $sql_parts['where'][] = 'r.groupid=ug.usrgrpid';
         $sql_parts['where'][] = 'ug.userid=' . $userid;
         $sql_parts['where'][] = 'r.permission>=' . $permission;
         $sql_parts['where'][] = 'NOT EXISTS( ' . ' SELECT ff.triggerid ' . ' FROM functions ff, items ii ' . ' WHERE ff.triggerid=t.triggerid ' . ' AND ff.itemid=ii.itemid ' . ' AND EXISTS( ' . ' SELECT hgg.groupid ' . ' FROM hosts_groups hgg, rights rr, users_groups gg ' . ' WHERE hgg.hostid=ii.hostid ' . ' AND rr.id=hgg.groupid ' . ' AND rr.groupid=gg.usrgrpid ' . ' AND gg.userid=' . $userid . ' AND rr.permission<' . $permission . '))';
         //*/
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // groupids
     if (!is_null($options['groupids'])) {
         zbx_value2array($options['groupids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['groupid'] = 'hg.groupid';
         }
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
         $sql_parts['where']['hgi'] = 'hg.hostid=i.hostid';
         $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
         $sql_parts['where']['groupid'] = DBcondition('hg.groupid', $options['groupids']);
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['hg'] = 'hg.groupid';
         }
     }
     // templateids
     if (!is_null($options['templateids'])) {
         zbx_value2array($options['templateids']);
         if (!is_null($options['hostids'])) {
             zbx_value2array($options['hostids']);
             $options['hostids'] = array_merge($options['hostids'], $options['templateids']);
         } else {
             $options['hostids'] = $options['templateids'];
         }
     }
     // hostids
     if (!is_null($options['hostids'])) {
         zbx_value2array($options['hostids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['hostid'] = 'i.hostid';
         }
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['where']['hostid'] = DBcondition('i.hostid', $options['hostids']);
         $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
         if (!is_null($options['groupCount'])) {
             $sql_parts['group']['i'] = 'i.hostid';
         }
     }
     // triggerids
     if (!is_null($options['triggerids'])) {
         zbx_value2array($options['triggerids']);
         $sql_parts['where']['triggerid'] = DBcondition('t.triggerid', $options['triggerids']);
     }
     // itemids
     if (!is_null($options['itemids'])) {
         zbx_value2array($options['itemids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['itemid'] = 'f.itemid';
         }
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['where']['itemid'] = DBcondition('f.itemid', $options['itemids']);
         $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
     }
     // applicationids
     if (!is_null($options['applicationids'])) {
         zbx_value2array($options['applicationids']);
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['applicationid'] = 'a.applicationid';
         }
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['from']['applications'] = 'applications a';
         $sql_parts['where']['a'] = DBcondition('a.applicationid', $options['applicationids']);
         $sql_parts['where']['ia'] = 'i.hostid=a.hostid';
         $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
     }
     // functions
     if (!is_null($options['functions'])) {
         zbx_value2array($options['functions']);
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sql_parts['where'][] = DBcondition('f.function', $options['functions'], false, true);
     }
     // monitored
     if (!is_null($options['monitored'])) {
         $sql_parts['where']['monitored'] = '' . ' NOT EXISTS (' . ' SELECT ff.functionid' . ' FROM functions ff' . ' WHERE ff.triggerid=t.triggerid' . ' AND EXISTS (' . ' SELECT ii.itemid' . ' FROM items ii, hosts hh' . ' WHERE ff.itemid=ii.itemid' . ' AND hh.hostid=ii.hostid' . ' AND (' . ' ii.status<>' . ITEM_STATUS_ACTIVE . ' OR hh.status<>' . HOST_STATUS_MONITORED . ' )' . ' )' . ' )';
         $sql_parts['where']['status'] = 't.status=' . TRIGGER_STATUS_ENABLED;
     }
     // active
     if (!is_null($options['active'])) {
         $sql_parts['where']['active'] = '' . ' NOT EXISTS (' . ' SELECT ff.functionid' . ' FROM functions ff' . ' WHERE ff.triggerid=t.triggerid' . ' AND EXISTS (' . ' SELECT ii.itemid' . ' FROM items ii, hosts hh' . ' WHERE ff.itemid=ii.itemid' . ' AND hh.hostid=ii.hostid' . ' AND  hh.status<>' . HOST_STATUS_MONITORED . ' )' . ' )';
         $sql_parts['where']['status'] = 't.status=' . TRIGGER_STATUS_ENABLED;
     }
     // maintenance
     if (!is_null($options['maintenance'])) {
         $sql_parts['where'][] = ($options['maintenance'] == 0 ? ' NOT ' : '') . ' EXISTS (' . ' SELECT ff.functionid' . ' FROM functions ff' . ' WHERE ff.triggerid=t.triggerid' . ' AND EXISTS (' . ' SELECT ii.itemid' . ' FROM items ii, hosts hh' . ' WHERE ff.itemid=ii.itemid' . ' AND hh.hostid=ii.hostid' . ' AND hh.maintenance_status=1' . ' )' . ' )';
         $sql_parts['where'][] = 't.status=' . TRIGGER_STATUS_ENABLED;
     }
     // lastChangeSince
     if (!is_null($options['lastChangeSince'])) {
         $sql_parts['where']['lastchangesince'] = 't.lastchange>' . $options['lastChangeSince'];
     }
     // lastChangeTill
     if (!is_null($options['lastChangeTill'])) {
         $sql_parts['where']['lastchangetill'] = 't.lastchange<' . $options['lastChangeTill'];
     }
     // withUnacknowledgedEvents
     if (!is_null($options['withUnacknowledgedEvents'])) {
         $sql_parts['where']['unack'] = ' EXISTS(' . ' SELECT e.eventid' . ' FROM events e' . ' WHERE e.objectid=t.triggerid' . ' AND e.object=0' . ' AND e.value=' . TRIGGER_VALUE_TRUE . ' AND e.acknowledged=0)';
     }
     // withAcknowledgedEvents
     if (!is_null($options['withAcknowledgedEvents'])) {
         $sql_parts['where']['ack'] = 'NOT EXISTS(' . ' SELECT e.eventid' . ' FROM events e' . ' WHERE e.objectid=t.triggerid' . ' AND e.object=0' . ' AND e.value=' . TRIGGER_VALUE_TRUE . ' AND e.acknowledged=0)';
     }
     // templated
     if (!is_null($options['templated'])) {
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['from']['hosts'] = 'hosts h';
         $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
         $sql_parts['where']['hi'] = 'h.hostid=i.hostid';
         if ($options['templated']) {
             $sql_parts['where'][] = 'h.status=' . HOST_STATUS_TEMPLATE;
         } else {
             $sql_parts['where'][] = 'h.status<>' . HOST_STATUS_TEMPLATE;
         }
     }
     // inherited
     if (!is_null($options['inherited'])) {
         if ($options['inherited']) {
             $sql_parts['where'][] = 't.templateid<>0';
         } else {
             $sql_parts['where'][] = 't.templateid=0';
         }
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('triggers t', $options, $sql_parts);
     }
     // --- FILTER ---
     if (is_array($options['filter'])) {
         zbx_db_filter('triggers t', $options, $sql_parts);
         if (isset($options['filter']['host']) && !is_null($options['filter']['host'])) {
             zbx_value2array($options['filter']['host']);
             $sql_parts['from']['functions'] = 'functions f';
             $sql_parts['from']['items'] = 'items i';
             $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
             $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
             $sql_parts['from']['hosts'] = 'hosts h';
             $sql_parts['where']['hi'] = 'h.hostid=i.hostid';
             $sql_parts['where']['host'] = DBcondition('h.host', $options['filter']['host'], false, true);
         }
         if (isset($options['filter']['hostid']) && !is_null($options['filter']['hostid'])) {
             zbx_value2array($options['filter']['hostid']);
             $sql_parts['from']['functions'] = 'functions f';
             $sql_parts['from']['items'] = 'items i';
             $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
             $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
             $sql_parts['where']['hostid'] = DBcondition('i.hostid', $options['filter']['hostid']);
         }
     }
     // group
     if (!is_null($options['group'])) {
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['name'] = 'g.name';
         }
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
         $sql_parts['from']['groups'] = 'groups g';
         $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
         $sql_parts['where']['hgi'] = 'hg.hostid=i.hostid';
         $sql_parts['where']['ghg'] = 'g.groupid = hg.groupid';
         $sql_parts['where']['group'] = ' UPPER(g.name)=' . zbx_dbstr(zbx_strtoupper($options['group']));
     }
     // host
     if (!is_null($options['host'])) {
         if ($options['output'] != API_OUTPUT_SHORTEN) {
             $sql_parts['select']['host'] = 'h.host';
         }
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['from']['hosts'] = 'hosts h';
         $sql_parts['where']['i'] = DBcondition('i.hostid', $options['hostids']);
         $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
         $sql_parts['where']['hi'] = 'h.hostid=i.hostid';
         $sql_parts['where']['host'] = ' UPPER(h.host)=' . zbx_dbstr(zbx_strtoupper($options['host']));
     }
     // only_true
     if (!is_null($options['only_true'])) {
         $sql_parts['where']['ot'] = '((t.value=' . TRIGGER_VALUE_TRUE . ')' . ' OR ' . '((t.value=' . TRIGGER_VALUE_FALSE . ') AND (t.lastchange>' . (time() - TRIGGER_FALSE_PERIOD) . ')))';
     }
     // min_severity
     if (!is_null($options['min_severity'])) {
         $sql_parts['where'][] = 't.priority>=' . $options['min_severity'];
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sql_parts['select']['triggers'] = 't.*';
     }
     // expandData
     if (!is_null($options['expandData'])) {
         $sql_parts['select']['host'] = 'h.host';
         $sql_parts['select']['hostid'] = 'h.hostid';
         $sql_parts['from']['functions'] = 'functions f';
         $sql_parts['from']['items'] = 'items i';
         $sql_parts['from']['hosts'] = 'hosts h';
         $sql_parts['where']['ft'] = 'f.triggerid=t.triggerid';
         $sql_parts['where']['fi'] = 'f.itemid=i.itemid';
         $sql_parts['where']['hi'] = 'h.hostid=i.hostid';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sql_parts['select'] = array('COUNT(DISTINCT t.triggerid) as rowscount');
         // groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sql_parts['group'] as $key => $fields) {
                 $sql_parts['select'][$key] = $fields;
             }
         }
     }
     // order
     // restrict not allowed columns for sorting
     $options['sortfield'] = str_in_array($options['sortfield'], $sort_columns) ? $options['sortfield'] : '';
     if (!zbx_empty($options['sortfield'])) {
         $sortorder = $options['sortorder'] == ZBX_SORT_DOWN ? ZBX_SORT_DOWN : ZBX_SORT_UP;
         $sql_parts['order'][] = 't.' . $options['sortfield'] . ' ' . $sortorder;
         if (!str_in_array('t.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('t.*', $sql_parts['select'])) {
             $sql_parts['select'][] = 't.' . $options['sortfield'];
         }
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sql_parts['limit'] = $options['limit'];
     }
     //---------------
     $triggerids = array();
     $sql_parts['select'] = array_unique($sql_parts['select']);
     $sql_parts['from'] = array_unique($sql_parts['from']);
     $sql_parts['where'] = array_unique($sql_parts['where']);
     $sql_parts['group'] = array_unique($sql_parts['group']);
     $sql_parts['order'] = array_unique($sql_parts['order']);
     $sql_select = '';
     $sql_from = '';
     $sql_where = '';
     $sql_group = '';
     $sql_order = '';
     if (!empty($sql_parts['select'])) {
         $sql_select .= implode(',', $sql_parts['select']);
     }
     if (!empty($sql_parts['from'])) {
         $sql_from .= implode(',', $sql_parts['from']);
     }
     if (!empty($sql_parts['where'])) {
         $sql_where .= ' AND ' . implode(' AND ', $sql_parts['where']);
     }
     if (!empty($sql_parts['group'])) {
         $sql_where .= ' GROUP BY ' . implode(',', $sql_parts['group']);
     }
     if (!empty($sql_parts['order'])) {
         $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']);
     }
     $sql_limit = $sql_parts['limit'];
     $sql = 'SELECT ' . zbx_db_distinct($sql_parts) . ' ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . DBin_node('t.triggerid', $nodeids) . $sql_where . $sql_group . $sql_order;
     //SDI($sql);
     $db_res = DBselect($sql, $sql_limit);
     while ($trigger = DBfetch($db_res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $trigger;
             } else {
                 $result = $trigger['rowscount'];
             }
         } else {
             $triggerids[$trigger['triggerid']] = $trigger['triggerid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$trigger['triggerid']] = array('triggerid' => $trigger['triggerid']);
             } else {
                 if (!isset($result[$trigger['triggerid']])) {
                     $result[$trigger['triggerid']] = array();
                 }
                 if (!is_null($options['select_hosts']) && !isset($result[$trigger['triggerid']]['hosts'])) {
                     $result[$trigger['triggerid']]['hosts'] = array();
                 }
                 if (!is_null($options['select_items']) && !isset($result[$trigger['triggerid']]['items'])) {
                     $result[$trigger['triggerid']]['items'] = array();
                 }
                 if (!is_null($options['select_functions']) && !isset($result[$trigger['triggerid']]['functions'])) {
                     $result[$trigger['triggerid']]['functions'] = array();
                 }
                 if (!is_null($options['select_dependencies']) && !isset($result[$trigger['triggerid']]['dependencies'])) {
                     $result[$trigger['triggerid']]['dependencies'] = array();
                 }
                 // groups
                 if (isset($trigger['groupid']) && is_null($options['select_groups'])) {
                     if (!isset($result[$trigger['triggerid']]['groups'])) {
                         $result[$trigger['triggerid']]['groups'] = array();
                     }
                     $result[$trigger['triggerid']]['groups'][] = array('groupid' => $trigger['groupid']);
                     unset($trigger['groupid']);
                 }
                 // hostids
                 if (isset($trigger['hostid']) && is_null($options['select_hosts'])) {
                     if (!isset($result[$trigger['triggerid']]['hosts'])) {
                         $result[$trigger['triggerid']]['hosts'] = array();
                     }
                     $result[$trigger['triggerid']]['hosts'][] = array('hostid' => $trigger['hostid']);
                     if (is_null($options['expandData'])) {
                         unset($trigger['hostid']);
                     }
                 }
                 // itemids
                 if (isset($trigger['itemid']) && is_null($options['select_items'])) {
                     if (!isset($result[$trigger['triggerid']]['items'])) {
                         $result[$trigger['triggerid']]['items'] = array();
                     }
                     $result[$trigger['triggerid']]['items'][] = array('itemid' => $trigger['itemid']);
                     unset($trigger['itemid']);
                 }
                 $result[$trigger['triggerid']] += $trigger;
             }
         }
     }
     Copt::memoryPick();
     if (!is_null($options['countOutput'])) {
         if (is_null($options['preservekeys'])) {
             $result = zbx_cleanHashes($result);
         }
         return $result;
     }
     // skipDependent
     if (!is_null($options['skipDependent'])) {
         $tids = $triggerids;
         $map = array();
         do {
             $sql = 'SELECT d.triggerid_down, d.triggerid_up, t.value ' . ' FROM trigger_depends d, triggers t ' . ' WHERE ' . DBcondition('d.triggerid_down', $tids) . ' AND d.triggerid_up=t.triggerid';
             $db_result = DBselect($sql);
             $tids = array();
             while ($row = DBfetch($db_result)) {
                 if (TRIGGER_VALUE_TRUE == $row['value']) {
                     if (isset($map[$row['triggerid_down']])) {
                         foreach ($map[$row['triggerid_down']] as $triggerid => $state) {
                             unset($result[$triggerid]);
                             unset($triggerids[$triggerid]);
                         }
                     } else {
                         unset($result[$row['triggerid_down']]);
                         unset($triggerids[$row['triggerid_down']]);
                     }
                 } else {
                     if (isset($map[$row['triggerid_down']])) {
                         if (!isset($map[$row['triggerid_up']])) {
                             $map[$row['triggerid_up']] = array();
                         }
                         $map[$row['triggerid_up']] += $map[$row['triggerid_down']];
                     } else {
                         if (!isset($map[$row['triggerid_up']])) {
                             $map[$row['triggerid_up']] = array();
                         }
                         $map[$row['triggerid_up']][$row['triggerid_down']] = 1;
                     }
                     $tids[] = $row['triggerid_up'];
                 }
             }
         } while (!empty($tids));
     }
     // withLastEventUnacknowledged
     if (!is_null($options['withLastEventUnacknowledged'])) {
         $eventids = array();
         $sql = 'SELECT max(e.eventid) as eventid, e.objectid' . ' FROM events e ' . ' WHERE e.object=' . EVENT_OBJECT_TRIGGER . ' AND ' . DBcondition('e.objectid', $triggerids) . ' AND ' . DBcondition('e.value', array(TRIGGER_VALUE_TRUE)) . ' GROUP BY e.objectid';
         $events_db = DBselect($sql);
         while ($event = DBfetch($events_db)) {
             $eventids[] = $event['eventid'];
         }
         $correct_triggerids = array();
         $sql = 'SELECT e.objectid' . ' FROM events e ' . ' WHERE ' . DBcondition('e.eventid', $eventids) . ' AND e.acknowledged=0';
         $triggers_db = DBselect($sql);
         while ($trigger = DBfetch($triggers_db)) {
             $correct_triggerids[$trigger['objectid']] = $trigger['objectid'];
         }
         foreach ($result as $triggerid => $trigger) {
             if (!isset($correct_triggerids[$triggerid])) {
                 unset($result[$triggerid]);
                 unset($triggerids[$triggerid]);
             }
         }
     }
     // Adding Objects
     // Adding trigger dependencies
     if (!is_null($options['select_dependencies']) && str_in_array($options['select_dependencies'], $subselects_allowed_outputs)) {
         $deps = array();
         $depids = array();
         $sql = 'SELECT triggerid_up, triggerid_down ' . ' FROM trigger_depends ' . ' WHERE ' . DBcondition('triggerid_down', $triggerids);
         $db_deps = DBselect($sql);
         while ($db_dep = DBfetch($db_deps)) {
             if (!isset($deps[$db_dep['triggerid_down']])) {
                 $deps[$db_dep['triggerid_down']] = array();
             }
             $deps[$db_dep['triggerid_down']][$db_dep['triggerid_up']] = $db_dep['triggerid_up'];
             $depids[] = $db_dep['triggerid_up'];
         }
         $obj_params = array('triggerids' => $depids, 'output' => $options['select_dependencies'], 'expandData' => 1, 'preservekeys' => 1);
         $allowed = self::get($obj_params);
         //allowed triggerids
         foreach ($deps as $triggerid => $deptriggers) {
             foreach ($deptriggers as $num => $deptriggerid) {
                 if (isset($allowed[$deptriggerid])) {
                     $result[$triggerid]['dependencies'][] = $allowed[$deptriggerid];
                 }
             }
         }
     }
     // Adding groups
     if (!is_null($options['select_groups']) && str_in_array($options['select_groups'], $subselects_allowed_outputs)) {
         $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_groups'], 'triggerids' => $triggerids, 'preservekeys' => 1);
         $groups = CHostgroup::get($obj_params);
         foreach ($groups as $groupid => $group) {
             $gtriggers = $group['triggers'];
             unset($group['triggers']);
             foreach ($gtriggers as $num => $trigger) {
                 $result[$trigger['triggerid']]['groups'][] = $group;
             }
         }
     }
     // Adding hosts
     if (!is_null($options['select_hosts'])) {
         $obj_params = array('nodeids' => $nodeids, 'triggerids' => $triggerids, 'templated_hosts' => 1, 'nopermissions' => 1, 'preservekeys' => 1);
         if (is_array($options['select_hosts']) || str_in_array($options['select_hosts'], $subselects_allowed_outputs)) {
             $obj_params['output'] = $options['select_hosts'];
             $hosts = CHost::get($obj_params);
             if (!is_null($options['limitSelects'])) {
                 order_result($hosts, 'host');
             }
             foreach ($hosts as $hostid => $host) {
                 unset($hosts[$hostid]['triggers']);
                 $count = array();
                 foreach ($host['triggers'] as $tnum => $trigger) {
                     if (!is_null($options['limitSelects'])) {
                         if (!isset($count[$trigger['triggerid']])) {
                             $count[$trigger['triggerid']] = 0;
                         }
                         $count[$trigger['triggerid']]++;
                         if ($count[$trigger['triggerid']] > $options['limitSelects']) {
                             continue;
                         }
                     }
                     $result[$trigger['triggerid']]['hosts'][] =& $hosts[$hostid];
                 }
             }
         } else {
             if (API_OUTPUT_COUNT == $options['select_hosts']) {
                 $obj_params['countOutput'] = 1;
                 $obj_params['groupCount'] = 1;
                 $hosts = CHost::get($obj_params);
                 $hosts = zbx_toHash($hosts, 'hostid');
                 foreach ($result as $triggerid => $trigger) {
                     if (isset($hosts[$triggerid])) {
                         $result[$triggerid]['hosts'] = $hosts[$triggerid]['rowscount'];
                     } else {
                         $result[$triggerid]['hosts'] = 0;
                     }
                 }
             }
         }
     }
     // Adding Functions
     if (!is_null($options['select_functions']) && str_in_array($options['select_functions'], $subselects_allowed_outputs)) {
         if ($options['select_functions'] == API_OUTPUT_EXTEND) {
             $sql_select = 'f.*';
         } else {
             $sql_select = 'f.functionid, f.triggerid';
         }
         $sql = 'SELECT ' . $sql_select . ' FROM functions f ' . ' WHERE ' . DBcondition('f.triggerid', $triggerids);
         $res = DBselect($sql);
         while ($function = DBfetch($res)) {
             $triggerid = $function['triggerid'];
             unset($function['triggerid']);
             $result[$triggerid]['functions'][] = $function;
         }
     }
     // Adding Items
     if (!is_null($options['select_items']) && str_in_array($options['select_items'], $subselects_allowed_outputs)) {
         $obj_params = array('nodeids' => $nodeids, 'output' => $options['select_items'], 'triggerids' => $triggerids, 'webitems' => 1, 'nopermissions' => 1, 'preservekeys' => 1);
         $items = CItem::get($obj_params);
         foreach ($items as $itemid => $item) {
             $itriggers = $item['triggers'];
             unset($item['triggers']);
             foreach ($itriggers as $num => $trigger) {
                 $result[$trigger['triggerid']]['items'][] = $item;
             }
         }
     }
     // expandDescription
     if (!is_null($options['expandDescription'])) {
         // Function compare values {{{
         foreach ($result as $tnum => $trigger) {
             preg_match_all('/\\$([1-9])/u', $trigger['description'], $numbers);
             preg_match_all('~{[0-9]+}[+\\-\\*/<>=#]?[\\(]*(?P<val>[+\\-0-9]+)[\\)]*~u', $trigger['expression'], $matches);
             foreach ($numbers[1] as $i) {
                 $rep = isset($matches['val'][$i - 1]) ? $matches['val'][$i - 1] : '';
                 $result[$tnum]['description'] = str_replace('$' . $i, $rep, $result[$tnum]['description']);
             }
         }
         // }}}
         $functionids = array();
         $triggers_to_expand_hosts = array();
         $triggers_to_expand_items = array();
         $triggers_to_expand_items2 = array();
         foreach ($result as $tnum => $trigger) {
             preg_match_all('/{HOSTNAME([1-9]?)}/u', $trigger['description'], $hnums);
             if (!empty($hnums[1])) {
                 preg_match_all('/{([0-9]+)}/u', $trigger['expression'], $funcs);
                 $funcs = $funcs[1];
                 foreach ($hnums[1] as $fnum) {
                     $fnum = $fnum ? $fnum : 1;
                     if (isset($funcs[$fnum - 1])) {
                         $functionid = $funcs[$fnum - 1];
                         $functionids[$functionid] = $functionid;
                         $triggers_to_expand_hosts[$trigger['triggerid']][$functionid] = $fnum;
                     }
                 }
             }
             preg_match_all('/{ITEM.LASTVALUE([1-9]?)}/u', $trigger['description'], $inums);
             if (!empty($inums[1])) {
                 preg_match_all('/{([0-9]+)}/u', $trigger['expression'], $funcs);
                 $funcs = $funcs[1];
                 foreach ($inums[1] as $fnum) {
                     $fnum = $fnum ? $fnum : 1;
                     if (isset($funcs[$fnum - 1])) {
                         $functionid = $funcs[$fnum - 1];
                         $functionids[$functionid] = $functionid;
                         $triggers_to_expand_items[$trigger['triggerid']][$functionid] = $fnum;
                     }
                 }
             }
             preg_match_all('/{ITEM.VALUE([1-9]?)}/u', $trigger['description'], $inums);
             if (!empty($inums[1])) {
                 preg_match_all('/{([0-9]+)}/u', $trigger['expression'], $funcs);
                 $funcs = $funcs[1];
                 foreach ($inums[1] as $fnum) {
                     $fnum = $fnum ? $fnum : 1;
                     if (isset($funcs[$fnum - 1])) {
                         $functionid = $funcs[$fnum - 1];
                         $functionids[$functionid] = $functionid;
                         $triggers_to_expand_items2[$trigger['triggerid']][$functionid] = $fnum;
                     }
                 }
             }
         }
         if (!empty($functionids)) {
             $sql = 'SELECT DISTINCT f.triggerid, f.functionid, h.host, i.lastvalue' . ' FROM functions f,items i,hosts h' . ' WHERE f.itemid=i.itemid' . ' AND i.hostid=h.hostid' . ' AND h.status<>' . HOST_STATUS_TEMPLATE . ' AND ' . DBcondition('f.functionid', $functionids);
             $db_funcs = DBselect($sql);
             while ($func = DBfetch($db_funcs)) {
                 if (isset($triggers_to_expand_hosts[$func['triggerid']][$func['functionid']])) {
                     $fnum = $triggers_to_expand_hosts[$func['triggerid']][$func['functionid']];
                     if ($fnum == 1) {
                         $result[$func['triggerid']]['description'] = str_replace('{HOSTNAME}', $func['host'], $result[$func['triggerid']]['description']);
                     }
                     $result[$func['triggerid']]['description'] = str_replace('{HOSTNAME' . $fnum . '}', $func['host'], $result[$func['triggerid']]['description']);
                 }
                 if (isset($triggers_to_expand_items[$func['triggerid']][$func['functionid']])) {
                     $fnum = $triggers_to_expand_items[$func['triggerid']][$func['functionid']];
                     if ($fnum == 1) {
                         $result[$func['triggerid']]['description'] = str_replace('{ITEM.LASTVALUE}', $func['lastvalue'], $result[$func['triggerid']]['description']);
                     }
                     $result[$func['triggerid']]['description'] = str_replace('{ITEM.LASTVALUE' . $fnum . '}', $func['lastvalue'], $result[$func['triggerid']]['description']);
                 }
                 if (isset($triggers_to_expand_items2[$func['triggerid']][$func['functionid']])) {
                     $fnum = $triggers_to_expand_items2[$func['triggerid']][$func['functionid']];
                     if ($fnum == 1) {
                         $result[$func['triggerid']]['description'] = str_replace('{ITEM.VALUE}', $func['lastvalue'], $result[$func['triggerid']]['description']);
                     }
                     $result[$func['triggerid']]['description'] = str_replace('{ITEM.VALUE' . $fnum . '}', $func['lastvalue'], $result[$func['triggerid']]['description']);
                 }
             }
         }
         foreach ($result as $tnum => $trigger) {
             if ($res = preg_match_all('/' . ZBX_PREG_EXPRESSION_USER_MACROS . '/', $trigger['description'], $arr)) {
                 $macros = CUserMacro::getMacros($arr[1], array('triggerid' => $trigger['triggerid']));
                 $search = array_keys($macros);
                 $values = array_values($macros);
                 $result[$tnum]['description'] = str_replace($search, $values, $trigger['description']);
             }
         }
     }
     COpt::memoryPick();
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }
 /**
  * Get maintenances data
  *
  * @param array $options
  * @param array $options['itemids']
  * @param array $options['hostids']
  * @param array $options['groupids']
  * @param array $options['triggerids']
  * @param array $options['maintenanceids']
  * @param boolean $options['status']
  * @param boolean $options['editable']
  * @param boolean $options['count']
  * @param string $options['pattern']
  * @param int $options['limit']
  * @param string $options['order']
  * @return array|int item data as array or false if error
  */
 public static function get($options = array())
 {
     global $USER_DETAILS;
     $result = array();
     $user_type = $USER_DETAILS['type'];
     $userid = $USER_DETAILS['userid'];
     $sort_columns = array('maintenanceid', 'name');
     // allowed columns for sorting
     $subselects_allowed_outputs = array(API_OUTPUT_REFER, API_OUTPUT_EXTEND);
     // allowed output options for [ select_* ] params
     $sql_parts = array('select' => array('maintenance' => 'm.maintenanceid'), 'from' => array('maintenances' => 'maintenances m'), 'where' => array(), 'group' => array(), 'order' => array(), 'limit' => null);
     $def_options = array('nodeids' => null, 'groupids' => null, 'hostids' => null, 'maintenanceids' => null, 'editable' => null, 'nopermissions' => null, 'filter' => null, 'search' => null, 'startSearch' => null, 'excludeSearch' => null, 'filter' => null, 'output' => API_OUTPUT_REFER, 'extendoutput' => null, 'select_groups' => null, 'select_hosts' => null, 'countOutput' => null, 'groupCount' => null, 'preservekeys' => null, 'sortfield' => '', 'sortorder' => '', 'limit' => null);
     $options = zbx_array_merge($def_options, $options);
     if (!is_null($options['extendoutput'])) {
         $options['output'] = API_OUTPUT_EXTEND;
         if (!is_null($options['select_groups'])) {
             $options['select_groups'] = API_OUTPUT_EXTEND;
         }
         if (!is_null($options['select_hosts'])) {
             $options['select_hosts'] = API_OUTPUT_EXTEND;
         }
     }
     // editable + PERMISSION CHECK
     $maintenanceids = array();
     if (USER_TYPE_SUPER_ADMIN == $user_type || $options['nopermissions']) {
         if (!is_null($options['groupids']) || !is_null($options['hostids'])) {
             if (!is_null($options['groupids'])) {
                 zbx_value2array($options['groupids']);
                 $sql = ' SELECT mmg.maintenanceid ' . ' FROM maintenances_groups mmg ' . ' WHERE ' . DBcondition('mmg.groupid', $options['groupids']);
                 $res = DBselect($sql);
                 while ($maintenance = DBfetch($res)) {
                     $maintenanceids[] = $maintenance['maintenanceid'];
                 }
             }
             $sql = ' SELECT mmh.maintenanceid ' . ' FROM maintenances_hosts mmh, hosts_groups hg ' . ' WHERE hg.hostid=mmh.hostid ';
             if (!is_null($options['groupids'])) {
                 zbx_value2array($options['groupids']);
                 $sql .= ' AND ' . DBcondition('hg.groupid', $options['groupids']);
             }
             if (!is_null($options['hostids'])) {
                 zbx_value2array($options['hostids']);
                 $sql .= ' AND ' . DBcondition('hg.hostid', $options['hostids']);
             }
             $res = DBselect($sql);
             while ($maintenance = DBfetch($res)) {
                 $maintenanceids[] = $maintenance['maintenanceid'];
             }
             $sql_parts['where'][] = DBcondition('m.maintenanceid', $maintenanceids);
         }
     } else {
         $permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ_ONLY;
         $sql = 'SELECT DISTINCT m.maintenanceid' . ' FROM maintenances m' . ' WHERE' . ' NOT EXISTS (' . ' SELECT mh3.maintenanceid' . ' FROM maintenances_hosts mh3, rights r3, users_groups ug3, hosts_groups hg3' . ' WHERE mh3.maintenanceid = m.maintenanceid' . ' AND r3.groupid = ug3.usrgrpid' . ' AND hg3.hostid = mh3.hostid' . ' AND r3.id = hg3.groupid' . ' AND ug3.userid = ' . $userid . ' AND r3.permission < ' . $permission . ' ) ' . ' AND NOT EXISTS ( ' . ' SELECT mh4.maintenanceid ' . ' FROM maintenances_hosts mh4 ' . ' WHERE mh4.maintenanceid = m.maintenanceid ' . ' AND NOT EXISTS( ' . ' SELECT r5.id ' . ' FROM rights r5, users_groups ug5, hosts_groups hg5 ' . ' WHERE r5.groupid = ug5.usrgrpid ' . ' AND hg5.hostid = mh4.hostid ' . ' AND r5.id = hg5.groupid ' . ' AND ug5.userid = ' . $userid . ' ) ' . ' ) ' . ' AND NOT EXISTS ( ' . ' SELECT mg2.maintenanceid ' . ' FROM maintenances_groups mg2, rights r3, users_groups ug3 ' . ' WHERE mg2.maintenanceid = m.maintenanceid ' . ' AND r3.groupid = ug3.usrgrpid ' . ' AND r3.id = mg2.groupid ' . ' AND ug3.userid = ' . $userid . ' AND r3.permission < ' . $permission . ' ) ' . ' AND NOT EXISTS ( ' . ' SELECT mg3.maintenanceid ' . ' FROM maintenances_groups mg3 ' . ' WHERE mg3.maintenanceid = m.maintenanceid ' . ' AND NOT EXISTS( ' . ' SELECT r5.id ' . ' FROM rights r5, users_groups ug5, hosts_groups hg5 ' . ' WHERE r5.groupid = ug5.usrgrpid ' . ' AND r5.id = mg3.groupid ' . ' AND ug5.userid = ' . $userid . ' ) ' . ' ) ';
         if (!is_null($options['groupids'])) {
             zbx_value2array($options['groupids']);
             //filtering using groups attached to maintenence
             $sql .= 'AND ( ' . ' EXISTS (' . ' SELECT mgf.maintenanceid ' . ' FROM maintenances_groups mgf ' . ' WHERE mgf.maintenanceid = m.maintenanceid ' . ' AND ' . DBcondition('mgf.groupid', $options['groupids']) . ' ) ';
             //filtering by hostgroups of hosts attached to maintenance
             $sql .= 'OR EXISTS (' . ' SELECT mh.maintenanceid ' . ' FROM maintenances_hosts mh, hosts_groups hg ' . ' WHERE mh.maintenanceid = m.maintenanceid ' . ' AND hg.hostid = mh.hostid ' . ' AND ' . DBcondition('hg.groupid', $options['groupids']) . ' ) ' . ' ) ';
         }
         if (!is_null($options['hostids'])) {
             zbx_value2array($options['hostids']);
             $sql .= 'AND EXISTS (' . ' SELECT mh.maintenanceid ' . ' FROM maintenances_hosts mh' . ' WHERE mh.maintenanceid = m.maintenanceid ' . ' AND ' . DBcondition('mh.hostid', $options['hostids']) . ' ) ';
         }
         $res = DBselect($sql);
         while ($miantenace = DBfetch($res)) {
             $maintenanceids[] = $miantenace['maintenanceid'];
         }
         $sql_parts['where'][] = DBcondition('m.maintenanceid', $maintenanceids);
     }
     // nodeids
     $nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
     // groupids
     if (!is_null($options['groupids'])) {
         $options['select_groups'] = 1;
     }
     // hostids
     if (!is_null($options['hostids'])) {
         $options['select_hosts'] = 1;
     }
     // maintenanceids
     if (!is_null($options['maintenanceids'])) {
         zbx_value2array($options['maintenanceids']);
         $sql_parts['where'][] = DBcondition('m.maintenanceid', $options['maintenanceids']);
     }
     // output
     if ($options['output'] == API_OUTPUT_EXTEND) {
         $sql_parts['select']['maintenance'] = 'm.*';
     }
     // countOutput
     if (!is_null($options['countOutput'])) {
         $options['sortfield'] = '';
         $sql_parts['select'] = array('count(DISTINCT m.maintenanceid) as rowscount');
         //groupCount
         if (!is_null($options['groupCount'])) {
             foreach ($sql_parts['group'] as $key => $fields) {
                 $sql_parts['select'][$key] = $fields;
             }
         }
     }
     // filter
     if (is_array($options['filter'])) {
         zbx_db_filter('maintenances m', $options, $sql_parts);
     }
     // search
     if (is_array($options['search'])) {
         zbx_db_search('maintenances m', $options, $sql_parts);
     }
     // order
     // restrict not allowed columns for sorting
     $options['sortfield'] = str_in_array($options['sortfield'], $sort_columns) ? $options['sortfield'] : '';
     if (!zbx_empty($options['sortfield'])) {
         $sortorder = $options['sortorder'] == ZBX_SORT_DOWN ? ZBX_SORT_DOWN : ZBX_SORT_UP;
         $sql_parts['order'][] = 'm.' . $options['sortfield'] . ' ' . $sortorder;
         if (!str_in_array('m.' . $options['sortfield'], $sql_parts['select']) && !str_in_array('m.*', $sql_parts['select'])) {
             $sql_parts['select'][] = 'm.' . $options['sortfield'];
         }
     }
     // limit
     if (zbx_ctype_digit($options['limit']) && $options['limit']) {
         $sql_parts['limit'] = $options['limit'];
     }
     //----------
     $maintenanceids = array();
     $sql_parts['select'] = array_unique($sql_parts['select']);
     $sql_parts['from'] = array_unique($sql_parts['from']);
     $sql_parts['where'] = array_unique($sql_parts['where']);
     $sql_parts['group'] = array_unique($sql_parts['group']);
     $sql_parts['order'] = array_unique($sql_parts['order']);
     $sql_select = '';
     $sql_from = '';
     $sql_where = '';
     $sql_group = '';
     $sql_order = '';
     if (!empty($sql_parts['select'])) {
         $sql_select .= implode(',', $sql_parts['select']);
     }
     if (!empty($sql_parts['from'])) {
         $sql_from .= implode(',', $sql_parts['from']);
     }
     if (!empty($sql_parts['where'])) {
         $sql_where .= ' AND ' . implode(' AND ', $sql_parts['where']);
     }
     if (!empty($sql_parts['group'])) {
         $sql_where .= ' GROUP BY ' . implode(',', $sql_parts['group']);
     }
     if (!empty($sql_parts['order'])) {
         $sql_order .= ' ORDER BY ' . implode(',', $sql_parts['order']);
     }
     $sql_limit = $sql_parts['limit'];
     $sql = 'SELECT ' . zbx_db_distinct($sql_parts) . ' ' . $sql_select . ' FROM ' . $sql_from . ' WHERE ' . DBin_node('m.maintenanceid', $nodeids) . $sql_where . $sql_order;
     $res = DBselect($sql, $sql_limit);
     while ($maintenance = DBfetch($res)) {
         if (!is_null($options['countOutput'])) {
             if (!is_null($options['groupCount'])) {
                 $result[] = $maintenance;
             } else {
                 $result = $maintenance['rowscount'];
             }
         } else {
             $maintenanceids[$maintenance['maintenanceid']] = $maintenance['maintenanceid'];
             if ($options['output'] == API_OUTPUT_SHORTEN) {
                 $result[$maintenance['maintenanceid']] = array('maintenanceid' => $maintenance['maintenanceid']);
             } else {
                 if (!isset($result[$maintenance['maintenanceid']])) {
                     $result[$maintenance['maintenanceid']] = array();
                 }
                 if (!is_null($options['select_groups']) && !isset($result[$maintenance['maintenanceid']]['groups'])) {
                     $result[$maintenance['maintenanceid']]['groups'] = array();
                 }
                 if (!is_null($options['select_hosts']) && !isset($result[$maintenance['maintenanceid']]['hosts'])) {
                     $result[$maintenance['maintenanceid']]['hosts'] = array();
                 }
                 // groupids
                 if (isset($maintenance['groupid']) && is_null($options['select_groups'])) {
                     if (!isset($result[$maintenance['maintenanceid']]['groups'])) {
                         $result[$maintenance['maintenanceid']]['groups'] = array();
                     }
                     $result[$maintenance['maintenanceid']]['groups'][] = array('groupid' => $maintenance['groupid']);
                     unset($maintenance['groupid']);
                 }
                 // hostids
                 if (isset($maintenance['hostid']) && is_null($options['select_hosts'])) {
                     if (!isset($result[$maintenance['maintenanceid']]['hosts'])) {
                         $result[$maintenance['maintenanceid']]['hosts'] = array();
                     }
                     $result[$maintenance['maintenanceid']]['hosts'][] = array('hostid' => $maintenance['hostid']);
                     unset($maintenance['hostid']);
                 }
                 $result[$maintenance['maintenanceid']] += $maintenance;
             }
         }
     }
     Copt::memoryPick();
     if (!is_null($options['countOutput'])) {
         if (is_null($options['preservekeys'])) {
             $result = zbx_cleanHashes($result);
         }
         return $result;
     }
     // select_groups
     if (is_array($options['select_groups']) || str_in_array($options['select_groups'], $subselects_allowed_outputs)) {
         $obj_params = array('nodeids' => $nodeids, 'maintenanceids' => $maintenanceids, 'preservekeys' => 1, 'output' => $options['select_groups']);
         $groups = CHostGroup::get($obj_params);
         foreach ($groups as $groupid => $group) {
             $gmaintenances = $group['maintenances'];
             unset($group['maintenances']);
             foreach ($gmaintenances as $num => $maintenance) {
                 $result[$maintenance['maintenanceid']]['groups'][] = $group;
             }
         }
     }
     // select_hosts
     if (is_array($options['select_hosts']) || str_in_array($options['select_hosts'], $subselects_allowed_outputs)) {
         $obj_params = array('nodeids' => $nodeids, 'maintenanceids' => $maintenanceids, 'preservekeys' => 1, 'output' => $options['select_hosts']);
         $hosts = CHost::get($obj_params);
         foreach ($hosts as $hostid => $host) {
             $hmaintenances = $host['maintenances'];
             unset($host['maintenances']);
             foreach ($hmaintenances as $num => $maintenance) {
                 $result[$maintenance['maintenanceid']]['hosts'][] = $host;
             }
         }
     }
     Copt::memoryPick();
     // removing keys (hash -> array)
     if (is_null($options['preservekeys'])) {
         $result = zbx_cleanHashes($result);
     }
     return $result;
 }