--- CTrigger.org.php 2015-04-22 10:23:53.000000000 +0300 +++ CTrigger.fix.php 2015-06-08 14:28:05.000000000 +0300 @@ -126,20 +126,6 @@ $userGroups = getUserGroupsByUserId($userid); - $sqlParts['where'][] = 'NOT EXISTS ('. - 'SELECT NULL'. - ' FROM functions f,items i,hosts_groups hgg'. - ' LEFT JOIN rights r'. - ' ON r.id=hgg.groupid'. - ' AND '.dbConditionInt('r.groupid', $userGroups). - ' WHERE t.triggerid=f.triggerid '. - ' AND f.itemid=i.itemid'. - ' AND i.hostid=hgg.hostid'. - ' GROUP BY i.hostid'. - ' HAVING MAX(permission)<'.zbx_dbstr($permission). - ' OR MIN(permission) IS NULL'. - ' OR MIN(permission)='.PERM_DENY. - ')'; } // groupids @@ -231,45 +217,39 @@ // monitored if (!is_null($options['monitored'])) { - $sqlParts['where']['monitored'] = 'NOT EXISTS ('. - 'SELECT NULL'. - ' FROM functions f,items i,hosts h'. - ' WHERE t.triggerid=f.triggerid'. - ' AND f.itemid=i.itemid'. - ' AND i.hostid=h.hostid'. - ' AND ('. - 'i.status<>'.ITEM_STATUS_ACTIVE. - ' OR h.status<>'.HOST_STATUS_MONITORED. - ')'. - ')'; + $sqlParts['from']['functions'] = 'functions f'; + $sqlParts['from']['items'] = 'items i'; + $sqlParts['from']['hosts'] = 'hosts h'; + $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid'; + $sqlParts['where']['fi'] = 'f.itemid=i.itemid'; + $sqlParts['where']['hi'] = 'h.hostid=i.hostid'; + $sqlParts['where'][] = 'i.status='.ITEM_STATUS_ACTIVE; + $sqlParts['where'][] = 'h.status='.HOST_STATUS_MONITORED; $sqlParts['where']['status'] = 't.status='.TRIGGER_STATUS_ENABLED; } // active if (!is_null($options['active'])) { - $sqlParts['where']['active'] = 'NOT EXISTS ('. - 'SELECT NULL'. - ' FROM functions f,items i,hosts h'. - ' WHERE t.triggerid=f.triggerid'. - ' AND f.itemid=i.itemid'. - ' AND i.hostid=h.hostid'. - ' AND h.status<>'.HOST_STATUS_MONITORED. - ')'; + $sqlParts['from']['functions'] = 'functions f'; + $sqlParts['from']['items'] = 'items i'; + $sqlParts['from']['hosts'] = 'hosts h'; + $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid'; + $sqlParts['where']['fi'] = 'f.itemid=i.itemid'; + $sqlParts['where']['hi'] = 'h.hostid=i.hostid'; + $sqlParts['where'][] = 'h.status='.HOST_STATUS_MONITORED; $sqlParts['where']['status'] = 't.status='.TRIGGER_STATUS_ENABLED; } // maintenance if (!is_null($options['maintenance'])) { - $sqlParts['where'][] = ($options['maintenance'] == 0 ? 'NOT ' : ''). - 'EXISTS ('. - 'SELECT NULL'. - ' FROM functions f,items i,hosts h'. - ' WHERE t.triggerid=f.triggerid'. - ' AND f.itemid=i.itemid'. - ' AND i.hostid=h.hostid'. - ' AND h.maintenance_status='.HOST_MAINTENANCE_STATUS_ON. - ')'; - $sqlParts['where'][] = 't.status='.TRIGGER_STATUS_ENABLED; + $sqlParts['from']['functions'] = 'functions f'; + $sqlParts['from']['items'] = 'items i'; + $sqlParts['from']['hosts'] = 'hosts h'; + $sqlParts['where']['ft'] = 'f.triggerid=t.triggerid'; + $sqlParts['where']['fi'] = 'f.itemid=i.itemid'; + $sqlParts['where']['hi'] = 'h.hostid=i.hostid'; + $sqlParts['where'][] = 'h.maintenance_status'.($options['maintenance'] == 0 ? '<>' : '=').HOST_MAINTENANCE_STATUS_ON; + $sqlParts['where']['status'] = 't.status='.TRIGGER_STATUS_ENABLED; } // lastChangeSince @@ -284,28 +264,23 @@ // withUnacknowledgedEvents if (!is_null($options['withUnacknowledgedEvents'])) { - $sqlParts['where']['unack'] = 'EXISTS ('. - 'SELECT NULL'. - ' FROM events e'. - ' WHERE t.triggerid=e.objectid'. - ' AND e.source='.EVENT_SOURCE_TRIGGERS. - ' AND e.object='.EVENT_OBJECT_TRIGGER. - ' AND e.value='.TRIGGER_VALUE_TRUE. - ' AND e.acknowledged='.EVENT_NOT_ACKNOWLEDGED. - ')'; + $sqlParts['from']['events'] = 'events e'; + $sqlParts['where']['te'] = 't.triggerid=e.objectid'; + $sqlParts['where']['unack'] = 'e.source='.EVENT_SOURCE_TRIGGERS. + ' AND e.object='.EVENT_OBJECT_TRIGGER. + ' AND e.value='.TRIGGER_VALUE_TRUE. + ' AND e.acknowledged='.EVENT_NOT_ACKNOWLEDGED; +// } // withAcknowledgedEvents if (!is_null($options['withAcknowledgedEvents'])) { - $sqlParts['where']['ack'] = 'NOT EXISTS ('. - 'SELECT NULL'. - ' FROM events e'. - ' WHERE e.objectid=t.triggerid'. - ' AND e.source='.EVENT_SOURCE_TRIGGERS. - ' AND e.object='.EVENT_OBJECT_TRIGGER. - ' AND e.value='.TRIGGER_VALUE_TRUE. - ' AND e.acknowledged='.EVENT_NOT_ACKNOWLEDGED. - ')'; + $sqlParts['from']['events'] = 'events e'; + $sqlParts['where']['te'] = 't.triggerid=e.objectid'; + $sqlParts['where']['ack'] = '(e.source<>'.EVENT_SOURCE_TRIGGERS. + ' OR e.object<>'.EVENT_OBJECT_TRIGGER. + ' OR e.value<>'.TRIGGER_VALUE_TRUE. + ' OR e.acknowledged<>'.EVENT_NOT_ACKNOWLEDGED.')'; } // templated @@ -415,6 +390,24 @@ $sqlParts['where'][] = 't.priority>='.zbx_dbstr($options['min_severity']); } + // editable + PERMISSION CHECK + if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) { + $sqlParts['where'][] = 'NOT EXISTS ('. + 'SELECT NULL'. + ' FROM functions f,items i,hosts_groups hgg'. + ' LEFT JOIN rights r'. + ' ON r.id=hgg.groupid'. + ' AND '.dbConditionInt('r.groupid', $userGroups). + ' WHERE t.triggerid=f.triggerid '. + ' AND f.itemid=i.itemid'. + ' AND i.hostid=hgg.hostid'. + ' GROUP BY i.hostid'. + ' HAVING MAX(permission)<'.zbx_dbstr($permission). + ' OR MIN(permission) IS NULL'. + ' OR MIN(permission)='.PERM_DENY. + ')'; + } + // limit if (!zbx_ctype_digit($options['limit']) || !$options['limit']) { $options['limit'] = null;