mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Update code in includes to be PSR-2 compliant (#4220)
refactor: Update code in /includes to be psr2 compliant #4220
This commit is contained in:
committed by
Neil Lathwood
parent
3c0fcdd46b
commit
9284bc60ff
@@ -76,10 +76,11 @@ function EditDeviceGroup($group_id, $name = null, $desc = null, $pattern = null)
|
||||
* @param string $search What to searchid for
|
||||
* @return string
|
||||
*/
|
||||
function GenGroupSQL($pattern, $search='',$extra=0) {
|
||||
function GenGroupSQL($pattern, $search = '', $extra = 0)
|
||||
{
|
||||
$pattern = RunGroupMacros($pattern);
|
||||
if ($pattern === false) {
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
$tmp = explode(' ', $pattern);
|
||||
$tables = array();
|
||||
@@ -96,33 +97,32 @@ function GenGroupSQL($pattern, $search='',$extra=0) {
|
||||
$pattern = rtrim($pattern, '||');
|
||||
|
||||
$tables = array_keys(array_flip($tables));
|
||||
if( dbFetchCell('SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_NAME = ? && COLUMN_NAME = ?',array($tables[0],'device_id')) != 1 ) {
|
||||
if (dbFetchCell('SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_NAME = ? && COLUMN_NAME = ?', array($tables[0],'device_id')) != 1) {
|
||||
//Our first table has no valid glue, append the 'devices' table to it!
|
||||
array_unshift($tables, 'devices');
|
||||
}
|
||||
$x = sizeof($tables)-1;
|
||||
$i = 0;
|
||||
$join = "";
|
||||
while( $i < $x ) {
|
||||
if( isset($tables[$i+1]) ) {
|
||||
$gtmp = ResolveGlues(array($tables[$i+1]),'device_id');
|
||||
if( $gtmp === false ) {
|
||||
while ($i < $x) {
|
||||
if (isset($tables[$i+1])) {
|
||||
$gtmp = ResolveGlues(array($tables[$i+1]), 'device_id');
|
||||
if ($gtmp === false) {
|
||||
//Cannot resolve glue-chain. Rule is invalid.
|
||||
return false;
|
||||
}
|
||||
$last = "";
|
||||
$qry = "";
|
||||
foreach( $gtmp as $glue ) {
|
||||
if( empty($last) ) {
|
||||
list($tmp,$last) = explode('.',$glue);
|
||||
foreach ($gtmp as $glue) {
|
||||
if (empty($last)) {
|
||||
list($tmp,$last) = explode('.', $glue);
|
||||
$qry .= $glue.' = ';
|
||||
}
|
||||
else {
|
||||
list($tmp,$new) = explode('.',$glue);
|
||||
} else {
|
||||
list($tmp,$new) = explode('.', $glue);
|
||||
$qry .= $tmp.'.'.$last.' && '.$tmp.'.'.$new.' = ';
|
||||
$last = $new;
|
||||
}
|
||||
if( !in_array($tmp, $tables) ) {
|
||||
if (!in_array($tmp, $tables)) {
|
||||
$tables[] = $tmp;
|
||||
}
|
||||
}
|
||||
@@ -134,14 +134,13 @@ function GenGroupSQL($pattern, $search='',$extra=0) {
|
||||
$sql_extra = ",`devices`.*";
|
||||
}
|
||||
if (!empty($search)) {
|
||||
$search = str_replace("(","",$tables[0]).'.'.$search. ' AND';
|
||||
$search = str_replace("(", "", $tables[0]).'.'.$search. ' AND';
|
||||
}
|
||||
if (!empty($join)) {
|
||||
$join = '('.rtrim($join, ' && ').') &&';
|
||||
}
|
||||
$sql = 'SELECT DISTINCT('.str_replace('(', '', $tables[0]).'.device_id)'.$sql_extra.' FROM '.implode(',', $tables).' WHERE '.$join.' '.$search.' ('.str_replace(array('%', '@', '!~', '~'), array('', '.*', 'NOT REGEXP', 'REGEXP'), $pattern).')';
|
||||
return $sql;
|
||||
|
||||
}//end GenGroupSQL()
|
||||
|
||||
|
||||
@@ -160,7 +159,6 @@ function QueryDevicesFromGroup($group_id)
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}//end QueryDevicesFromGroup()
|
||||
|
||||
/**
|
||||
@@ -183,9 +181,9 @@ function GetDevicesFromGroup($group_id, $nested = false)
|
||||
* Get all Device-Groups
|
||||
* @return array
|
||||
*/
|
||||
function GetDeviceGroups() {
|
||||
function GetDeviceGroups()
|
||||
{
|
||||
return dbFetchRows('SELECT * FROM device_groups ORDER BY name');
|
||||
|
||||
}//end GetDeviceGroups()
|
||||
|
||||
/**
|
||||
@@ -194,21 +192,20 @@ function GetDeviceGroups() {
|
||||
* @param int $extra Return extra info about the groups (name, desc, pattern)
|
||||
* @return array
|
||||
*/
|
||||
function QueryGroupsFromDevice($device_id,$extra=0) {
|
||||
function QueryGroupsFromDevice($device_id, $extra = 0)
|
||||
{
|
||||
$ret = array();
|
||||
foreach (GetDeviceGroups() as $group) {
|
||||
if (dbFetchCell(GenGroupSQL($group['pattern'], 'device_id=?',$extra).' LIMIT 1', array($device_id)) == $device_id) {
|
||||
if (dbFetchCell(GenGroupSQL($group['pattern'], 'device_id=?', $extra).' LIMIT 1', array($device_id)) == $device_id) {
|
||||
if ($extra === 0) {
|
||||
$ret[] = $group['id'];
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
$ret[] = $group;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $ret;
|
||||
|
||||
}//end QueryGroupsFromDevice()
|
||||
|
||||
/**
|
||||
@@ -234,17 +231,18 @@ function GetGroupsFromDevice($device_id, $extra = 0)
|
||||
* @param int $x Recursion-Anchor
|
||||
* @return string|boolean
|
||||
*/
|
||||
function RunGroupMacros($rule,$x=1) {
|
||||
function RunGroupMacros($rule, $x = 1)
|
||||
{
|
||||
global $config;
|
||||
krsort($config['alert']['macros']['group']);
|
||||
foreach( $config['alert']['macros']['group'] as $macro=>$value ) {
|
||||
if( !strstr($macro," ") ) {
|
||||
$rule = str_replace('%macros.'.$macro,'('.$value.')',$rule);
|
||||
foreach ($config['alert']['macros']['group'] as $macro => $value) {
|
||||
if (!strstr($macro, " ")) {
|
||||
$rule = str_replace('%macros.'.$macro, '('.$value.')', $rule);
|
||||
}
|
||||
}
|
||||
if( strstr($rule,"%macros") ) {
|
||||
if( ++$x < 30 ) {
|
||||
$rule = RunGroupMacros($rule,$x);
|
||||
if (strstr($rule, "%macros")) {
|
||||
if (++$x < 30) {
|
||||
$rule = RunGroupMacros($rule, $x);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
@@ -279,7 +277,6 @@ function UpdateGroupsForDevice($device_id)
|
||||
if (!empty($removed_groups)) {
|
||||
dbDelete('device_group_device', '`device_id`=? AND `device_group_id` IN (?)', array($device_id, array(implode(',', $removed_groups))));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user