Passed
Push — master ( b8f383...dcdb45 )
by Patrick
02:09
created
bin/trap_class.php 1 patch
Braces   +119 added lines, -74 removed lines patch added patch discarded remove patch
@@ -65,9 +65,9 @@  discard block
 block discarded – undo
65 65
 		{
66 66
 		    $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile);
67 67
 		    $this->logSetup=true;
68
+		} else {
69
+				    $this->logSetup=false;
68 70
 		}
69
-		else 
70
-		    $this->logSetup=false;
71 71
 		$this->logging->log('Loggin started', INFO);
72 72
 
73 73
 		// Get options from ini files
@@ -80,7 +80,10 @@  discard block
 block discarded – undo
80 80
 		$this->setupDatabase($trapConfig); // Setup database class
81 81
 		
82 82
 		$this->getDatabaseOptions(); // Get options in database
83
-		if ($this->api_use === true) $this->getAPI(); // Setup API
83
+		if ($this->api_use === true) {
84
+			$this->getAPI();
85
+		}
86
+		// Setup API
84 87
 		
85 88
 		$this->trap_data=array(
86 89
 			'source_ip'	=> 'unknown',
@@ -112,8 +115,7 @@  discard block
 block discarded – undo
112 115
 	        }
113 116
 	        $this->logging->log($message,$log_level,'syslog');
114 117
 	        return false;
115
-	    }
116
-	    else
118
+	    } else
117 119
 	    {
118 120
 	        $option_var=$option_array[$option_category][$option_name];
119 121
 	        return true;
@@ -177,7 +179,10 @@  discard block
 block discarded – undo
177 179
 	    
178 180
         $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName]);
179 181
 	    
180
-	    if ($this->api_use === true) return; // In case of API use, no IDO is necessary
182
+	    if ($this->api_use === true) {
183
+	    	return;
184
+	    }
185
+	    // In case of API use, no IDO is necessary
181 186
         
182 187
 	    // IDO Database
183 188
 	    if (!array_key_exists('IDOdatabase',$trapConfig['config']))
@@ -202,9 +207,11 @@  discard block
 block discarded – undo
202 207
 	protected function getDatabaseOptions()
203 208
 	{
204 209
 		// Database options
205
-		if ($this->logSetup === false) // Only if logging was no setup in constructor
210
+		if ($this->logSetup === false) {
211
+			// Only if logging was no setup in constructor
206 212
 		{
207 213
     		$this->getDBConfigIfSet('log_level',$this->logging->debugLevel);
214
+		}
208 215
     		$this->getDBConfigIfSet('log_destination',$this->logging->outputMode);
209 216
     		$this->getDBConfigIfSet('log_file',$this->logging->outputFile);
210 217
 		}
@@ -213,7 +220,9 @@  discard block
 block discarded – undo
213 220
 	protected function getDBConfigIfSet($element,&$variable)
214 221
 	{
215 222
 		$value=$this->getDBConfig($element);
216
-		if ($value != 'null') $variable=$value;
223
+		if ($value != 'null') {
224
+			$variable=$value;
225
+		}
217 226
 	}
218 227
 	
219 228
 	/** 
@@ -301,8 +310,7 @@  discard block
 block discarded – undo
301 310
 		{
302 311
 		    $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)");
303 312
 			$this->logging->log('Error parsing IP : '.$IP,ERROR,'');
304
-		} 
305
-		else 
313
+		} else 
306 314
 		{		
307 315
 			$this->trap_data['source_ip']=$matches[1];
308 316
 			$this->trap_data['destination_ip']=$matches[3];
@@ -317,14 +325,12 @@  discard block
 block discarded – undo
317 325
 			if ($ret_code===0 || $ret_code===false) 
318 326
 			{
319 327
 				$this->logging->log('No match on trap data : '.$vars,WARN,'');
320
-			}
321
-			else 
328
+			} else 
322 329
 			{
323 330
 			    if (($matches[1]=='.1.3.6.1.6.3.1.1.4.1.0') || ($matches[1]=='.1.3.6.1.6.3.1.1.4.1'))
324 331
 				{
325 332
 					$this->trap_data['trap_oid']=$matches[2];				
326
-				}
327
-				else
333
+				} else
328 334
 				{
329 335
 					$object= new stdClass;
330 336
 					$object->oid =$matches[1];
@@ -495,7 +501,9 @@  discard block
 block discarded – undo
495 501
 	            }
496 502
 	            
497 503
 	            $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()'];
498
-	            if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
504
+	            if ($inserted_id==false) {
505
+	            	throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
506
+	            }
499 507
 	            $this->trap_id=$inserted_id;
500 508
 	            break;
501 509
 	        default:
@@ -511,7 +519,9 @@  discard block
 block discarded – undo
511 519
 	{
512 520
 		
513 521
 		// If action is ignore -> don't send t DB
514
-		if ($this->trap_to_db === false) return;
522
+		if ($this->trap_to_db === false) {
523
+			return;
524
+		}
515 525
 		
516 526
 		
517 527
 		$db_conn=$this->trapsDB->db_connect_trap();
@@ -568,7 +578,9 @@  discard block
 block discarded – undo
568 578
 				}
569 579
 
570 580
 				$inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()'];
571
-				if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
581
+				if ($inserted_id==false) {
582
+					throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
583
+				}
572 584
 				$this->trap_id=$inserted_id;
573 585
 			break;
574 586
 			default: 
@@ -697,8 +709,7 @@  discard block
 block discarded – undo
697 709
     		// TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default....
698 710
     		exec('echo "'.$send.'" > ' .$this->icinga2cmd);
699 711
     		return true;
700
-	    }
701
-	    else
712
+	    } else
702 713
 	    {
703 714
 	        $api = $this->getAPI();
704 715
 	        $api->setCredentials($this->api_username, $this->api_password);
@@ -707,8 +718,7 @@  discard block
 block discarded – undo
707 718
 	        {
708 719
 	            $this->logging->log( "Error sending result : " .$retmessage,WARN,'');
709 720
 	            return false;
710
-	        }
711
-	        else 
721
+	        } else 
712 722
 	        {
713 723
 	            $this->logging->log( "Sent result : " .$retmessage,INFO );
714 724
 	            return true;
@@ -768,7 +778,9 @@  discard block
 block discarded – undo
768 778
 	/***************** Eval & tokenizer functions ****************/
769 779
 	protected function eval_getElement($rule,&$item)
770 780
 	{
771
-		while ($rule[$item]==' ') $item++;
781
+		while ($rule[$item]==' ') {
782
+			$item++;
783
+		}
772 784
 		if (preg_match('/[0-9\.]/',$rule[$item]))
773 785
 		{ // number
774 786
 	
@@ -795,14 +807,15 @@  discard block
 block discarded – undo
795 807
 			$start=$item;
796 808
 			$parenthesis_count=0; 
797 809
 			while (($item < strlen($rule)) // Not end of string AND
798
-			      && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded ()
810
+			      && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) {
811
+				// Closing ')' or embeded ()
799 812
 			{ 
800 813
 				if ($rule[$item] == '"' )
801 814
 				{ // pass through string
802 815
 					$item++;
816
+			}
803 817
 					$item=$this->eval_getNext($rule,$item,'"');
804
-				} 
805
-				else{
818
+				} else{
806 819
 				    if ($rule[$item] == '(')
807 820
 				    {
808 821
 				        $parenthesis_count++;
@@ -830,13 +843,17 @@  discard block
 block discarded – undo
830 843
 	protected function eval_getNext($rule,$item,$tok)
831 844
 	{
832 845
 		while (($rule[$item] != $tok ) && ($item < strlen($rule))) { $item++;}
833
-		if ($item==strlen($rule)) throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item);
846
+		if ($item==strlen($rule)) {
847
+			throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item);
848
+		}
834 849
 		return $item+1;
835 850
 	}
836 851
 	
837 852
 	protected function eval_getOper($rule,&$item)
838 853
 	{
839
-		while ($rule[$item]==' ') $item++;
854
+		while ($rule[$item]==' ') {
855
+			$item++;
856
+		}
840 857
 		switch ($rule[$item])
841 858
 		{
842 859
 			case '<':
@@ -872,12 +889,13 @@  discard block
 block discarded – undo
872 889
 	public function evaluation($rule,&$item)
873 890
 	{
874 891
 	    //echo "Evaluation of ".substr($rule,$item)."\n";
875
-		if ( $rule[$item] == '!') // If '!' found, negate next expression.
892
+		if ( $rule[$item] == '!') {
893
+			// If '!' found, negate next expression.
876 894
 		{
877 895
 		    $negate=true;
878
-		    $item++;
879 896
 		}
880
-		else
897
+		    $item++;
898
+		} else
881 899
 		{
882 900
 		    $negate=false;
883 901
 		}
@@ -885,10 +903,14 @@  discard block
 block discarded – undo
885 903
 		list($type1,$val1) = $this->eval_getElement($rule,$item);
886 904
 		//echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n";
887 905
 		
888
-		if ($item==strlen($rule)) // If only element, return value, but only boolean
906
+		if ($item==strlen($rule)) {
907
+			// If only element, return value, but only boolean
889 908
 		{
890 909
 		  if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule);
891
-		  if ($negate === true) $val1= ! $val1;
910
+		}
911
+		  if ($negate === true) {
912
+		  	$val1= ! $val1;
913
+		  }
892 914
 		  return $val1;
893 915
 		}  
894 916
 		
@@ -897,27 +919,34 @@  discard block
 block discarded – undo
897 919
 		//echo "Comp : ".$comp." : ".substr($rule,$item)."\n";
898 920
         
899 921
 		// Third element : number, string or ()
900
-		if ( $rule[$item] == '!') // starts with a ! so evaluate whats next
922
+		if ( $rule[$item] == '!') {
923
+			// starts with a ! so evaluate whats next
901 924
 		{
902 925
 		    $item++;
903
-		    if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule);
926
+		}
927
+		    if ($typec != 1) {
928
+		    	throw new Exception("Mixing boolean and comparison : ".$rule);
929
+		    }
904 930
 		    $val2= ! $this->evaluation($rule,$item);
905 931
 		    $type2=2; // result is a boolean 
906
-		}
907
-		else 
932
+		} else 
908 933
 		{
909 934
 		    list($type2,$val2) = $this->eval_getElement($rule,$item);
910 935
 		}
911 936
 		//echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n";
912 937
 		
913
-		if ($type1!=$type2)  // cannot compare different types
938
+		if ($type1!=$type2) {
939
+			// cannot compare different types
914 940
 		{ 
915 941
 		    throw new Exception("Cannot compare string & number : ".$rule);
916 942
 		}
917
-		if ($typec==1 && $type1 !=2) // cannot use & or | with string/number
943
+		}
944
+		if ($typec==1 && $type1 !=2) {
945
+			// cannot use & or | with string/number
918 946
 		{
919 947
 		    throw new Exception("Cannot use boolean operators with string & number : ".$rule);
920 948
 		}
949
+		}
921 950
 		
922 951
 		switch ($comp){
923 952
 			case '<':	$retVal= ($val1 < $val2); break;
@@ -931,9 +960,15 @@  discard block
 block discarded – undo
931 960
 			case '&':	$retVal= ($val1 && $val2); break;
932 961
 			default:  throw new Exception("Error in expression - unknown comp : ".$comp);
933 962
 		}
934
-		if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression
963
+		if ($negate === true) {
964
+			$retVal = ! $retVal;
965
+		}
966
+		// Inverse result if negate before expression
935 967
 		
936
-		if ($item==strlen($rule)) return $retVal; // End of string : return evaluation
968
+		if ($item==strlen($rule)) {
969
+			return $retVal;
970
+		}
971
+		// End of string : return evaluation
937 972
 		// check for logical operator :
938 973
 		switch ($rule[$item])
939 974
 		{
@@ -960,7 +995,9 @@  discard block
 block discarded – undo
960 995
 					$rule2.=$rule[$item];
961 996
 					$item++;
962 997
 				}
963
-				if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item);
998
+				if ($item == strlen ($rule)) {
999
+					throw new Exception("closing '\"' not found in ".$rule ." at " .$item);
1000
+				}
964 1001
 				$rule2.=$rule[$item];
965 1002
 				$item++;
966 1003
 				continue;
@@ -980,10 +1017,12 @@  discard block
 block discarded – undo
980 1017
 	
981 1018
 	protected function eval_rule($rule)
982 1019
 	{
983
-		if ($rule==null || $rule == '') // Empty rule is always true
1020
+		if ($rule==null || $rule == '') {
1021
+			// Empty rule is always true
984 1022
 		{
985 1023
 			return true;
986 1024
 		}
1025
+		}
987 1026
 		$matches=array();
988 1027
 		while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1)
989 1028
 		{
@@ -1070,20 +1109,17 @@  discard block
 block discarded – undo
1070 1109
 						if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false)
1071 1110
 						{
1072 1111
 						    $this->trap_action.='Error sending status : check cmd/API';
1073
-						}
1074
-						else
1112
+						} else
1075 1113
 						{
1076 1114
 						    $this->add_rule_match($rule['id'],$rule['num_match']+1);
1077 1115
 						    $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name;
1078 1116
 						}
1079
-					}
1080
-					else
1117
+					} else
1081 1118
 					{
1082 1119
 						$this->add_rule_match($rule['id'],$rule['num_match']+1);
1083 1120
 					}
1084 1121
 					$this->trap_to_db=($action==-2)?false:true;
1085
-				}
1086
-				else
1122
+				} else
1087 1123
 				{
1088 1124
 					//$this->logging->log('rules KOO : '.print_r($rule),INFO );
1089 1125
 					
@@ -1094,14 +1130,12 @@  discard block
 block discarded – undo
1094 1130
 					    if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false)
1095 1131
 					    {
1096 1132
 					        $this->trap_action.='Error sending status : check cmd/API';
1097
-					    }
1098
-					    else
1133
+					    } else
1099 1134
 					    {
1100 1135
     						$this->add_rule_match($rule['id'],$rule['num_match']+1);
1101 1136
     						$this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name;
1102 1137
 					    }
1103
-					}
1104
-					else
1138
+					} else
1105 1139
 					{
1106 1140
 						$this->add_rule_match($rule['id'],$rule['num_match']+1);
1107 1141
 					}
@@ -1111,16 +1145,14 @@  discard block
 block discarded – undo
1111 1145
 				if (!isset($this->trap_data['source_name']))
1112 1146
 				{
1113 1147
 					$this->trap_data['source_name']=$rule['host_name'];
1114
-				}
1115
-				else
1148
+				} else
1116 1149
 				{
1117 1150
 					if (!preg_match('/'.$rule['host_name'].'/',$this->trap_data['source_name']))
1118 1151
 					{ // only add if not present
1119 1152
 						$this->trap_data['source_name'].=','.$rule['host_name'];
1120 1153
 					}
1121 1154
 				}
1122
-			}
1123
-			catch (Exception $e) 
1155
+			} catch (Exception $e) 
1124 1156
 			{ 
1125 1157
 			    $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,'');
1126 1158
 			    $this->trap_action.=' ERR : '.$e->getMessage();
@@ -1131,8 +1163,7 @@  discard block
 block discarded – undo
1131 1163
 		if ($this->trap_data['status']=='error')
1132 1164
 		{
1133 1165
 		  $this->trap_to_db=true; // Always put errors in DB for the use can see
1134
-		}
1135
-		else
1166
+		} else
1136 1167
 		{
1137 1168
 		  $this->trap_data['status']='done';
1138 1169
 		}
@@ -1187,8 +1218,7 @@  discard block
 block discarded – undo
1187 1218
                 if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array))
1188 1219
                 {
1189 1220
                     $cur_table='table '.$cur_table_array[1];
1190
-                }
1191
-                else
1221
+                } else
1192 1222
                 {
1193 1223
                     $cur_table='secret SQL stuff :-)';
1194 1224
                 }
@@ -1232,8 +1262,7 @@  discard block
 block discarded – undo
1232 1262
 	    if ($this->trapsDB->trapDBType == 'pgsql')
1233 1263
 	    {
1234 1264
 	        $prefix .= 'update_pgsql/schema_';
1235
-	    }
1236
-	    else
1265
+	    } else
1237 1266
 	    {
1238 1267
 	        $prefix .= 'update_sql/schema_';
1239 1268
 	    }
@@ -1279,7 +1308,10 @@  discard block
 block discarded – undo
1279 1308
 	       $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
1280 1309
 	       while (($line=fgets($input_stream)) !== false)
1281 1310
 	       {
1282
-	           if (preg_match('/^#/', $line)) continue; // ignore comment lines
1311
+	           if (preg_match('/^#/', $line)) {
1312
+	           	continue;
1313
+	           }
1314
+	           // ignore comment lines
1283 1315
 	           $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line));
1284 1316
 	           if (preg_match('/; *$/', $newline))
1285 1317
 	           {
@@ -1291,8 +1323,7 @@  discard block
 block discarded – undo
1291 1323
 	               if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array))
1292 1324
 	               {
1293 1325
 	                   $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2];
1294
-	               }
1295
-	               else
1326
+	               } else
1296 1327
 	               {
1297 1328
 	                   $cur_table='secret SQL stuff :-)';
1298 1329
 	                   //$cur_table=$newline;
@@ -1432,8 +1463,7 @@  discard block
 block discarded – undo
1432 1463
 			    }
1433 1464
 			    $this->logging->log('Trap updated : '.$name . ' / OID : '.$oid,DEBUG );
1434 1465
 				return 1;
1435
-			}
1436
-			else
1466
+			} else
1437 1467
 			{
1438 1468
 			    $this->logging->log('Trap unchanged : '.$name . ' / OID : '.$oid,DEBUG );
1439 1469
 			    return 0;
@@ -1449,7 +1479,9 @@  discard block
 block discarded – undo
1449 1479
               'values (:oid, :name , :type ,:mib ,:tc , :display_hint'.
1450 1480
               ', :syntax, :type_enum, :description )';
1451 1481
         
1452
-		if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id';
1482
+		if ($this->trapsDB->trapDBType == 'pgsql') {
1483
+			$sql .= 'RETURNING id';
1484
+		}
1453 1485
 		
1454 1486
 		$sqlQuery=$db_conn->prepare($sql);
1455 1487
 		
@@ -1489,7 +1521,9 @@  discard block
 block discarded – undo
1489 1521
 		        }
1490 1522
 		        
1491 1523
 		        $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()'];
1492
-		        if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
1524
+		        if ($inserted_id==false) {
1525
+		        	throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
1526
+		        }
1493 1527
 		        $this->dbOidIndex[$oid]['id']=$inserted_id;
1494 1528
 		        break;
1495 1529
 		    default:
@@ -1742,12 +1776,14 @@  discard block
 block discarded – undo
1742 1776
 			$name=$match[1]; // Name 
1743 1777
 			$type=$match[2]; // type (21=trap, 0: may be trap, else : not trap
1744 1778
 			
1745
-			if ($type==0) // object type=0 : check if v1 trap
1779
+			if ($type==0) {
1780
+				// object type=0 : check if v1 trap
1746 1781
 			{
1747 1782
 				// Check if next is suboid -> in that case is cannot be a trap
1748 1783
 				if (preg_match("/^$oid/",$this->objectsAll[$curElement+1]))
1749 1784
 				{
1750 1785
 				    $time_check2 += microtime(true) - $time_1;
1786
+			}
1751 1787
 				    $time_check2N++;
1752 1788
 					continue;
1753 1789
 				}		
@@ -1764,9 +1800,11 @@  discard block
 block discarded – undo
1764 1800
 				// Force as trap.
1765 1801
 				$type=21;
1766 1802
 			}
1767
-			if ($onlyTraps===true && $type!=21) // if only traps and not a trap, continue
1803
+			if ($onlyTraps===true && $type!=21) {
1804
+				// if only traps and not a trap, continue
1768 1805
 			{
1769 1806
 			    $time_check3 += microtime(true) - $time_1;
1807
+			}
1770 1808
 				$time_check3N++;
1771 1809
 				continue;
1772 1810
 			}
@@ -1774,7 +1812,10 @@  discard block
 block discarded – undo
1774 1812
 			$time_num_traps++;
1775 1813
 			
1776 1814
 			$this->logging->log('Found trap : '.$match[1] . ' / OID : '.$oid,INFO );
1777
-			if ($display_progress) echo '#'; // echo a # when trap found
1815
+			if ($display_progress) {
1816
+				echo '#';
1817
+			}
1818
+			// echo a # when trap found
1778 1819
 				
1779 1820
 			// get trap objects & source MIB
1780 1821
 			unset($snmptrans);
@@ -1792,7 +1833,9 @@  discard block
 block discarded – undo
1792 1833
 			$trapMib=$match[1];
1793 1834
 			
1794 1835
 			$numLine=1;$trapDesc='';
1795
-			while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++;
1836
+			while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) {
1837
+				$numLine++;
1838
+			}
1796 1839
 			if (isset($snmptrans[$numLine]))
1797 1840
 			{
1798 1841
 			    $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]);
@@ -1814,7 +1857,9 @@  discard block
 block discarded – undo
1814 1857
 			if (($update==0) && ($check_change===false))
1815 1858
 			{ // Trapd didn't change & force check disabled
1816 1859
 			    $time_objects += microtime(true) - $time_1;
1817
-			    if ($display_progress) echo "C";
1860
+			    if ($display_progress) {
1861
+			    	echo "C";
1862
+			    }
1818 1863
 			    continue;
1819 1864
 			}
1820 1865
 			
Please login to merge, or discard this patch.
library/Trapdirector/TrapsController.php 1 patch
Braces   +62 added lines, -30 removed lines patch added patch discarded remove patch
@@ -87,10 +87,11 @@  discard block
 block discarded – undo
87 87
 		try 
88 88
 		{
89 89
 			$dbconn = IcingaDbConnection::fromResourceName($DBname);
90
-		} 
91
-		catch (Exception $e)
90
+		} catch (Exception $e)
92 91
 		{
93
-			if ($test) return array(2,$DBname);
92
+			if ($test) {
93
+				return array(2,$DBname);
94
+			}
94 95
 			$this->redirectNow('trapdirector/settings?dberror=2');
95 96
 			return null;
96 97
 		}
@@ -98,10 +99,11 @@  discard block
 block discarded – undo
98 99
 			try 
99 100
 			{
100 101
 				$db=$dbconn->getConnection();
101
-			}
102
-			catch (Exception $e) 
102
+			} catch (Exception $e) 
103 103
 			{
104
-				if ($test) return array(3,$DBname,$e->getMessage());
104
+				if ($test) {
105
+					return array(3,$DBname,$e->getMessage());
106
+				}
105 107
 				$this->redirectNow('trapdirector/settings?dberror=3');
106 108
 				return null;
107 109
 			}
@@ -113,25 +115,32 @@  discard block
 block discarded – undo
113 115
 				$version=$db->fetchRow($query);
114 116
 				if ( ($version == null) || ! property_exists($version,'value') )
115 117
 				{
116
-					if ($test) return array(4,$DBname);
118
+					if ($test) {
119
+						return array(4,$DBname);
120
+					}
117 121
 					$this->redirectNow('trapdirector/settings?dberror=4');
118 122
 					return null;
119 123
 				}
120 124
 				if ($version->value < $this->getModuleConfig()->getDbMinVersion()) 
121 125
 				{
122
-					if ($test) return array(5,$version->value,$this->getModuleConfig()->getDbMinVersion());
126
+					if ($test) {
127
+						return array(5,$version->value,$this->getModuleConfig()->getDbMinVersion());
128
+					}
123 129
 					$this->redirectNow('trapdirector/settings?dberror=5');
124 130
 					return null;
125 131
 				}
126
-			}
127
-			catch (Exception $e) 
132
+			} catch (Exception $e) 
128 133
 			{
129
-				if ($test) return array(3,$DBname,$e->getMessage());
134
+				if ($test) {
135
+					return array(3,$DBname,$e->getMessage());
136
+				}
130 137
 				$this->redirectNow('trapdirector/settings?dberror=4');
131 138
 				return null;
132 139
 			}
133 140
 		}
134
-		if ($test) return array(0,'');
141
+		if ($test) {
142
+			return array(0,'');
143
+		}
135 144
 		return $dbconn;
136 145
 	}
137 146
 
@@ -142,31 +151,41 @@  discard block
 block discarded – undo
142 151
 	 */
143 152
 	public function getDb($test=false)
144 153
 	{
145
-		if ($this->trapDB != null && $test = false) return $this->trapDB;
154
+		if ($this->trapDB != null && $test = false) {
155
+			return $this->trapDB;
156
+		}
146 157
 		
147 158
 		$dbresource=$this->Config()->get('config', 'database');
148 159
 		
149 160
 		if ( ! $dbresource )
150 161
 		{	
151
-			if ($test) return array(1,'');
162
+			if ($test) {
163
+				return array(1,'');
164
+			}
152 165
 			$this->redirectNow('trapdirector/settings?dberror=1');
153 166
 			return null;
154 167
 		}
155 168
 		$retDB=$this->getDbByName($dbresource,$test,true);
156
-		if ($test == true) return $retDB;
169
+		if ($test == true) {
170
+			return $retDB;
171
+		}
157 172
 		$this->trapDB=$retDB;
158 173
 		return $this->trapDB;
159 174
 	}
160 175
 	
161 176
 	public function getIdoDb($test=false)
162 177
 	{
163
-		if ($this->icingaDB != null && $test = false) return $this->icingaDB;
178
+		if ($this->icingaDB != null && $test = false) {
179
+			return $this->icingaDB;
180
+		}
164 181
 		// TODO : get ido database directly from icingaweb2 config -> (or not if using only API)
165 182
 		$dbresource=$this->Config()->get('config', 'IDOdatabase');;
166 183
 
167 184
 		if ( ! $dbresource )
168 185
 		{
169
-		    if ($test) return array(1,'No database in config.ini');
186
+		    if ($test) {
187
+		    	return array(1,'No database in config.ini');
188
+		    }
170 189
 		    $this->redirectNow('trapdirector/settings?idodberror=1');
171 190
 		    return null;
172 191
 		}
@@ -174,10 +193,11 @@  discard block
 block discarded – undo
174 193
 		try
175 194
 		{
176 195
 		    $dbconn = IcingaDbConnection::fromResourceName($dbresource);
177
-		}
178
-		catch (Exception $e)
196
+		} catch (Exception $e)
179 197
 		{
180
-		    if ($test) return array(2,"Database $dbresource does not exists in IcingaWeb2");
198
+		    if ($test) {
199
+		    	return array(2,"Database $dbresource does not exists in IcingaWeb2");
200
+		    }
181 201
 		    $this->redirectNow('trapdirector/settings?idodberror=2');
182 202
 		    return null;
183 203
 		}
@@ -197,8 +217,7 @@  discard block
 block discarded – undo
197 217
 		    {
198 218
 		        return array(4,"$dbresource does not look like an IDO database");
199 219
 		    }
200
-		}
201
-		catch (Exception $e)
220
+		} catch (Exception $e)
202 221
 		{
203 222
 			return array(3,"Error connecting to $dbresource : " . $e->getMessage());
204 223
 		}
@@ -446,8 +465,7 @@  discard block
 block discarded – undo
446 465
 				if (isset($common_services[$service->name2]['num']))
447 466
 				{
448 467
 					$common_services[$service->name2]['num'] +=1;
449
-				}
450
-				else
468
+				} else
451 469
 				{
452 470
 					$common_services[$service->name2]['num']=1;
453 471
 					$common_services[$service->name2]['name']=$service->name;
@@ -592,7 +610,9 @@  discard block
 block discarded – undo
592 610
 			$condition=($condition===null)?'':$condition.' AND ';
593 611
 			$condition.="trap_oid='$oid'";
594 612
 		}
595
-		if($condition === null) return null;
613
+		if($condition === null) {
614
+			return null;
615
+		}
596 616
 		$query=$db->delete(
597 617
 			$this->getModuleConfig()->getTrapTableName(),
598 618
 			$condition
@@ -620,7 +640,9 @@  discard block
 block discarded – undo
620 640
 			$condition=($condition===null)?'':$condition.' AND ';
621 641
 			$condition.="trap_oid='$oid'";
622 642
 		}
623
-		if($condition === null) return 0;
643
+		if($condition === null) {
644
+			return 0;
645
+		}
624 646
 		$query=$db->select()
625 647
 			->from(
626 648
 				$this->getModuleConfig()->getTrapTableName(),
@@ -644,18 +666,28 @@  discard block
 block discarded – undo
644 666
 				array('value'=>'value'))
645 667
 			->where('name=?',$element);
646 668
 		$return_row=$db->fetchRow($query);
647
-		if ($return_row==null)  // value does not exists
669
+		if ($return_row==null) {
670
+			// value does not exists
648 671
 		{
649 672
 			$default=$this->getModuleConfig()->getDBConfigDefaults();
650
-			if ( ! isset($default[$element])) return null; // no default and not value
673
+		}
674
+			if ( ! isset($default[$element])) {
675
+				return null;
676
+			}
677
+			// no default and not value
651 678
 			
652 679
 			$this->addDBConfigValue($element,$default[$element]);
653 680
 			return $default[$element];
654 681
 		}
655
-		if ($return_row->value == null) // value id empty
682
+		if ($return_row->value == null) {
683
+			// value id empty
656 684
 		{
657 685
 			$default=$this->getModuleConfig()->getDBConfigDefaults();
658
-			if ( ! isset($default[$element])) return null; // no default and not value
686
+		}
687
+			if ( ! isset($default[$element])) {
688
+				return null;
689
+			}
690
+			// no default and not value
659 691
 			$this->setDBConfigValue($element,$default[$element]);
660 692
 			return $default[$element];			
661 693
 		}
Please login to merge, or discard this patch.