|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* QueryBuilderFilter.php |
|
4
|
|
|
* |
|
5
|
|
|
* -Description- |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU General Public License as published by |
|
9
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
10
|
|
|
* (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
|
15
|
|
|
* GNU General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU General Public License |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19
|
|
|
* |
|
20
|
|
|
* @package LibreNMS |
|
21
|
|
|
* @link http://librenms.org |
|
22
|
|
|
* @copyright 2016 Tony Murray |
|
23
|
|
|
* @author Tony Murray <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace app; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
use Cache; |
|
30
|
|
|
use DB; |
|
31
|
|
|
use Settings; |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
class QueryBuilderFilter |
|
34
|
|
|
{ |
|
35
|
|
|
public static function getAlertFilter() |
|
36
|
|
|
{ |
|
37
|
|
|
return json_encode(self::generateMacroFilter('alert.macros.rule', self::generateTableFilter())); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
private static function generateMacroFilter($setting, $filter = []) |
|
41
|
|
|
{ |
|
42
|
|
|
foreach (Settings::get($setting, []) as $key => $value) { |
|
43
|
|
|
$filter[] = [ |
|
44
|
|
|
'id' => 'macros.'.$key, |
|
45
|
|
|
'type' => 'integer', |
|
46
|
|
|
'input' => 'radio', |
|
47
|
|
|
'values' => ['1' => 'Yes', '0' => 'No'], |
|
48
|
|
|
'operators' => ['equal'], |
|
49
|
|
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
return $filter; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private static function generateTableFilter($filter = []) |
|
55
|
|
|
{ |
|
56
|
|
|
// check if the database schema has changed |
|
57
|
|
|
$db = DB::table('migrations')->pluck('migration'); |
|
58
|
|
|
$cached = Cache::get('migrations_list', []); |
|
59
|
|
|
|
|
60
|
|
|
if ($db != $cached) { |
|
61
|
|
|
Cache::forget('query_builder_table_filter'); |
|
62
|
|
|
Cache::forever('migrations_list', $db); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
// return the table filter merged with $filter, fetch from cache if available |
|
66
|
|
|
return array_merge($filter, Cache::rememberForever('query_builder_table_filter', function() { |
|
67
|
|
|
$tableFilter = []; |
|
68
|
|
|
$schema = DB::getDoctrineSchemaManager(); |
|
69
|
|
|
|
|
70
|
|
|
// Doctrine DBAL has issues with enums, pretend they are strings |
|
71
|
|
|
$schema->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); |
|
72
|
|
|
|
|
73
|
|
|
$tables = $schema->listTables(); |
|
74
|
|
|
foreach ($tables as $table) { |
|
75
|
|
|
$columns = $schema->listTableColumns($table->getName()); |
|
76
|
|
|
$tableName = $table->getName(); |
|
77
|
|
|
|
|
78
|
|
|
// only allow tables with a direct association to device_id |
|
79
|
|
|
if (!$table->hasColumn('device_id')) { |
|
80
|
|
|
continue; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
foreach ($columns as $column) { |
|
84
|
|
|
$columnName = $column->getName(); |
|
85
|
|
|
|
|
86
|
|
|
// ignore device id columns, except in the devices table |
|
87
|
|
|
if ($columnName == 'device_id' && $tableName != 'devices') { |
|
88
|
|
|
continue; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$columnType = self::getColumnType($column->getType()->getName()); |
|
92
|
|
|
|
|
93
|
|
|
// ignore unsupported types (such as binary and blob) |
|
94
|
|
|
if (is_null($columnType)) { |
|
95
|
|
|
continue; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$tableFilter[] = [ |
|
99
|
|
|
'id' => $tableName.'.'.$columnName, |
|
100
|
|
|
'type' => $columnType, |
|
101
|
|
|
]; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
return $tableFilter; |
|
105
|
|
|
})); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
private static function getColumnType($type) |
|
109
|
|
|
{ |
|
110
|
|
|
switch ($type) { |
|
111
|
|
|
case 'text': |
|
112
|
|
|
// return 'textarea'; |
|
113
|
|
|
case 'string': |
|
114
|
|
|
return 'string'; |
|
115
|
|
|
|
|
116
|
|
|
case 'integer': |
|
117
|
|
|
case 'smallint': |
|
118
|
|
|
case 'bigint': |
|
119
|
|
|
return 'integer'; |
|
120
|
|
|
|
|
121
|
|
|
case 'double': |
|
122
|
|
|
case 'float': |
|
123
|
|
|
case 'decimal': |
|
124
|
|
|
return 'double'; |
|
125
|
|
|
|
|
126
|
|
|
case 'date': |
|
127
|
|
|
return 'date'; |
|
128
|
|
|
|
|
129
|
|
|
case 'time': |
|
130
|
|
|
return 'time'; |
|
131
|
|
|
|
|
132
|
|
|
case 'datetime': |
|
133
|
|
|
return 'datetime'; |
|
134
|
|
|
|
|
135
|
|
|
case 'boolean': |
|
136
|
|
|
return 'boolean'; |
|
137
|
|
|
} |
|
138
|
|
|
// binary, blob |
|
139
|
|
|
return null; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public static function getGroupFilter() |
|
143
|
|
|
{ |
|
144
|
|
|
return json_encode(self::generateMacroFilter('alert.macros.group', self::generateTableFilter())); |
|
145
|
|
|
} |
|
146
|
|
|
} |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: