| 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 Log; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | use Settings; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | class QueryBuilderFilter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public static function getAlertFilter() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         return json_encode(self::generateMacroFilter('alert.macros.rule', self::generateTableFilter())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     private static function generateMacroFilter($setting, $filter = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         foreach (Settings::get($setting, []) as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $filter[] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 'id'        => 'macros.'.$key, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 'type'      => 'integer', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 'input'     => 'radio', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 'values'    => ['1' => 'Yes', '2' => 'No'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 'colors'    => ['1' => 'success', '2' => 'danger'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 'operators' => ['equal'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         return $filter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     private static function generateTableFilter($filter = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $check_start = microtime(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $cached = Cache::get('query_builder_table_filter_migrations', []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $db = DB::table('migrations')->pluck('migration'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         if ($db != $cached) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             Cache::forget('query_builder_table_filter'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             Cache::add('query_builder_table_filter_migrations', $db, 30); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $check_end = microtime(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         Log::info('Query Builder filter check time: '.($check_end - $check_start).'s'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return Cache::remember('query_builder_table_filter', 300, function() use ($filter) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $schema = DB::getDoctrineSchemaManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             // Doctrine DBAL has issues with enums, pretend they are strings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $schema->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | //            $validTypes = ['string', 'integer', 'double', 'date', 'time', 'datetime', 'boolean']; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $ignoreTypes = ['blob', 'binary']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             $tables = $schema->listTables(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             foreach ($tables as $table) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 $columns = $schema->listTableColumns($table->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 $tableName = $table->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 // only allow tables with direct association to device_id for now | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 if (!$table->hasColumn('device_id')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 foreach ($columns as $column) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                     $item = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                     $type = $column->getType()->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                     $name = $column->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                     switch ($type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                         case 'text': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | //                            $item['input'] = 'textarea'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                         case 'string': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                             $item['type'] = 'string'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                         case 'integer': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                         case 'smallint': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                         case 'bigint': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                             $item['type'] = 'integer'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                         case 'double': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                         case 'float': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                         case 'decimal': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                             $item['type'] = 'double'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                         case 'date': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                             $item['type'] = 'date'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                         case 'time': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                             $item['type'] = 'time'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                         case 'datetime': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                             $item['type'] = 'datetime'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                         case 'boolean': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                             $item['type'] = 'boolean'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                     if (!isset($item['type'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                         if (!in_array($type, $ignoreTypes)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                             dd($type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                         continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                     // ignore device id columns, except in the devices table | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                     if ($name == 'device_id') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                         if ($tableName != 'devices') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                             continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                     $item['id'] = $tableName.'.'.$name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                     $filter[] = $item; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             return $filter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 153 |  |  |     public static function getGroupFilter() | 
            
                                                        
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 155 |  |  |         return json_encode(self::generateMacroFilter('alert.macros.group', self::generateTableFilter())); | 
            
                                                        
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 157 |  |  | } | 
            
                        
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: