| 
									
										
										
										
											2014-11-30 17:49:52 +00:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | /* | 
					
						
							|  |  |  |  * Copyright (C) 2014 Daniel Preussker <f0o@devilcode.org> | 
					
						
							| 
									
										
										
										
											2014-11-30 17:49:52 +00:00
										 |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |  * (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2014-11-30 17:49:52 +00:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2014-11-30 17:49:52 +00:00
										 |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-11-30 17:49:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2014-11-30 17:49:52 +00:00
										 |  |  |  * Rule Suggestion-AJAX | 
					
						
							|  |  |  |  * @author Daniel Preussker <f0o@devilcode.org> | 
					
						
							|  |  |  |  * @copyright 2014 f0o, LibreNMS | 
					
						
							|  |  |  |  * @license GPL | 
					
						
							|  |  |  |  * @package LibreNMS/Alerts | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | session_start(); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | if (!isset($_SESSION['authenticated'])) { | 
					
						
							|  |  |  |     die('Unauthorized.'); | 
					
						
							| 
									
										
										
										
											2014-11-30 17:49:52 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | require_once '../includes/defaults.inc.php'; | 
					
						
							| 
									
										
										
										
											2015-07-23 17:48:22 +01:00
										 |  |  | set_debug($_REQUEST['debug']); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | require_once '../config.php'; | 
					
						
							|  |  |  | require_once '../includes/definitions.inc.php'; | 
					
						
							|  |  |  | require_once '../includes/functions.php'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 17:49:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Levenshtein Sort | 
					
						
							|  |  |  |  * @param string $base Comparisson basis | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |  * @param array  $obj  Object to sort | 
					
						
							| 
									
										
										
										
											2014-11-30 17:49:52 +00:00
										 |  |  |  * @return array | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function levsort($base, $obj) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     $ret = array(); | 
					
						
							|  |  |  |     foreach ($obj as $elem) { | 
					
						
							|  |  |  |         $lev = levenshtein($base, $elem, 1, 10, 10); | 
					
						
							|  |  |  |         if ($lev == 0) { | 
					
						
							|  |  |  |             return array(array('name' => $elem)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             while (isset($ret["$lev"])) { | 
					
						
							|  |  |  |                 $lev += 0.1; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $ret["$lev"] = array('name' => $elem); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ksort($ret); | 
					
						
							|  |  |  |     return $ret; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 17:49:52 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-04 18:18:40 -08:00
										 |  |  | header('Content-type: application/json'); | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | $obj     = array(array('name' => 'Error: No suggestions found.')); | 
					
						
							|  |  |  | $term    = array(); | 
					
						
							| 
									
										
										
										
											2014-12-18 21:50:26 +00:00
										 |  |  | $current = false; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | if (isset($_GET['term'],$_GET['device_id'])) { | 
					
						
							|  |  |  |     $chk               = array(); | 
					
						
							|  |  |  |     $_GET['term']      = mres($_GET['term']); | 
					
						
							|  |  |  |     $_GET['device_id'] = mres($_GET['device_id']); | 
					
						
							|  |  |  |     if (strstr($_GET['term'], '.')) { | 
					
						
							|  |  |  |         $term = explode('.', $_GET['term']); | 
					
						
							| 
									
										
										
										
											2015-09-27 19:29:34 +00:00
										 |  |  |         if ($term[0] == 'macros') { | 
					
						
							|  |  |  |             foreach ($config['alert']['macros']['rule'] as $macro => $v) { | 
					
						
							|  |  |  |                 $chk[] = 'macros.'.$macro; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-09-27 19:29:34 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             $tmp = dbFetchRows('SHOW COLUMNS FROM '.$term[0]); | 
					
						
							|  |  |  |             foreach ($tmp as $tst) { | 
					
						
							|  |  |  |                 if (isset($tst['Field'])) { | 
					
						
							|  |  |  |                     $chk[] = $term[0].'.'.$tst['Field']; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $current = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2015-09-27 19:29:34 +00:00
										 |  |  |         $tmp = dbFetchRows("SELECT TABLE_NAME FROM information_schema.COLUMNS WHERE COLUMN_NAME = 'device_id'"); | 
					
						
							|  |  |  |         foreach ($tmp as $tst) { | 
					
						
							|  |  |  |             $chk[] = $tst['TABLE_NAME'].'.'; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-27 19:29:34 +00:00
										 |  |  |         $chk[] = 'macros.'; | 
					
						
							|  |  |  |         $chk[] = 'bills.'; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (sizeof($chk) > 0) { | 
					
						
							| 
									
										
										
										
											2015-09-27 19:29:34 +00:00
										 |  |  |         $obj  = levsort($_GET['term'], $chk); | 
					
						
							|  |  |  |         $obj  = array_chunk($obj, 20, true); | 
					
						
							|  |  |  |         $obj  = $obj[0]; | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |         $flds = array(); | 
					
						
							| 
									
										
										
										
											2015-08-04 18:34:24 +02:00
										 |  |  |         if ($current === true) { | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  |             foreach ($obj as $fld) { | 
					
						
							|  |  |  |                 $flds[] = $fld['name']; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $qry             = dbFetchRows('SELECT '.implode(', ', $flds).' FROM '.$term[0].' WHERE device_id = ?', array($_GET['device_id'])); | 
					
						
							|  |  |  |             $ret = array(); | 
					
						
							|  |  |  |             foreach ($obj as $lev => $fld) { | 
					
						
							|  |  |  |                 list($tbl, $chk) = explode('.', $fld['name']); | 
					
						
							|  |  |  |                 $val             = array(); | 
					
						
							|  |  |  |                 foreach ($qry as $row) { | 
					
						
							|  |  |  |                     $val[] = $row[$chk]; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 $ret[$lev] = array( | 
					
						
							|  |  |  |                     'name'    => $fld['name'], | 
					
						
							|  |  |  |                     'current' => $val, | 
					
						
							|  |  |  |                 ); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $obj = $ret; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2014-11-30 17:49:52 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-07-13 20:10:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 17:49:52 +00:00
										 |  |  | die(json_encode($obj)); |