| 
									
										
										
										
											2014-11-30 17:49:52 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | /* Copyright (C) 2014 Daniel Preussker <f0o@devilcode.org> | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  *  | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  *  | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/>. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Rule Suggestion-AJAX | 
					
						
							|  |  |  |  * @author Daniel Preussker <f0o@devilcode.org> | 
					
						
							|  |  |  |  * @copyright 2014 f0o, LibreNMS | 
					
						
							|  |  |  |  * @license GPL | 
					
						
							|  |  |  |  * @package LibreNMS/Alerts | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | session_start(); | 
					
						
							|  |  |  | if( !isset($_SESSION['authenticated']) ) { | 
					
						
							|  |  |  | 	die("Unauthorized."); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require_once("../includes/defaults.inc.php"); | 
					
						
							|  |  |  | require_once("../config.php"); | 
					
						
							|  |  |  | require_once("../includes/definitions.inc.php"); | 
					
						
							|  |  |  | require_once("../includes/functions.php"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Levenshtein Sort | 
					
						
							|  |  |  |  * @param string $base Comparisson basis | 
					
						
							|  |  |  |  * @param array $obj Object to sort | 
					
						
							|  |  |  |  * @return array | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function levsort($base, $obj) { | 
					
						
							|  |  |  | 	$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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $obj = array(array('name'=>'Error: No suggestions found.')); | 
					
						
							| 
									
										
										
										
											2014-12-16 20:49:55 +00:00
										 |  |  | $term = array(); | 
					
						
							| 
									
										
										
										
											2014-12-18 21:50:26 +00:00
										 |  |  | $current = false; | 
					
						
							| 
									
										
										
										
											2014-11-30 17:49:52 +00: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']); | 
					
						
							|  |  |  | 		if( $config['memcached']['enable'] ) { | 
					
						
							|  |  |  | 			$chk = $memcache->get('rule-suggest_'.$term[0]); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if( !(sizeof($chk) > 0) || $chk === false ) { | 
					
						
							| 
									
										
										
										
											2015-04-24 19:05:55 +00:00
										 |  |  | 			if( $term[0] == "macros" ) { | 
					
						
							|  |  |  | 				foreach( $config['alert']['macros']['rule'] as $macro=>$v ) { | 
					
						
							|  |  |  | 					$chk[] = "macros.".$macro; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				$tmp = dbFetchRows('SHOW COLUMNS FROM '.$term[0]); | 
					
						
							|  |  |  | 				foreach( $tmp as $tst ) { | 
					
						
							|  |  |  | 					if( isset($tst['Field']) ) { | 
					
						
							|  |  |  | 						$chk[] = $term[0].'.'.$tst['Field']; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-11-30 17:49:52 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		$current = true; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		if( $config['memcached']['enable'] ) { | 
					
						
							|  |  |  | 			$chk = $memcache->get('rule-suggest-toplvl'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if( !(sizeof($chk) > 0) || $chk === false ) { | 
					
						
							| 
									
										
										
										
											2014-12-19 17:37:15 +00:00
										 |  |  | 			$tmp = dbFetchRows("SELECT TABLE_NAME FROM information_schema.COLUMNS WHERE COLUMN_NAME = 'device_id'"); | 
					
						
							| 
									
										
										
										
											2014-11-30 17:49:52 +00:00
										 |  |  | 			foreach( $tmp as $tst ) { | 
					
						
							| 
									
										
										
										
											2014-12-19 17:37:15 +00:00
										 |  |  | 				$chk[] = $tst['TABLE_NAME'].'.'; | 
					
						
							| 
									
										
										
										
											2014-11-30 17:49:52 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-04-24 19:05:55 +00:00
										 |  |  | 			$chk[] = 'macros.'; | 
					
						
							| 
									
										
										
										
											2015-05-07 20:41:20 +00:00
										 |  |  | 			$chk[] = 'bills.'; | 
					
						
							| 
									
										
										
										
											2014-11-30 17:49:52 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if( sizeof($chk) > 0 ) { | 
					
						
							|  |  |  | 		if( $config['memcached']['enable'] ) { | 
					
						
							|  |  |  | 			$memcache->set('rule-suggest-'.$oterm,$chk,86400); //Cache for 24h
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		$obj = levsort($_GET['term'],$chk); | 
					
						
							|  |  |  | 		$obj = array_chunk($obj,20,true); | 
					
						
							|  |  |  | 		$obj = $obj[0]; | 
					
						
							| 
									
										
										
										
											2014-12-16 20:49:55 +00:00
										 |  |  |                 $flds = array(); | 
					
						
							| 
									
										
										
										
											2014-11-30 17:49:52 +00:00
										 |  |  | 		if( $current == true ) { | 
					
						
							|  |  |  | 			foreach( $obj as $fld ) { | 
					
						
							|  |  |  | 				$flds[] = $fld['name']; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			$qry = dbFetchRows("SELECT ".implode(", ",$flds)." FROM ".$term[0]." WHERE device_id = ?", array($_GET['device_id'])); | 
					
						
							| 
									
										
										
										
											2014-12-16 21:10:11 +00:00
										 |  |  |                         $ret = array(); | 
					
						
							| 
									
										
										
										
											2014-11-30 17:49:52 +00:00
										 |  |  | 			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; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | die(json_encode($obj)); | 
					
						
							|  |  |  | ?>
 |