Passed
Push — master ( b8f383...dcdb45 )
by Patrick
02:09
created
bin/trap_in.php 1 patch
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -3,7 +3,7 @@  discard block
 block discarded – undo
3 3
 use Trapdirector\Logging;
4 4
 
5 5
 // start
6
-$time1 = microtime(true);
6
+$time1=microtime(true);
7 7
 
8 8
 require_once ('trap_class.php');
9 9
 
@@ -15,7 +15,7 @@  discard block
 block discarded – undo
15 15
 
16 16
 try
17 17
 {
18
-    $Trap = new Trap($icingaweb2_etc);
18
+    $Trap=new Trap($icingaweb2_etc);
19 19
     //$Trap = new Trap($icingaweb2_etc,4,'display'); // For debug
20 20
     //$Trap = new Trap($icingaweb2_etc,4,'syslog'); // For debug
21 21
     //$Trap->setLogging(4,'syslog'); 
@@ -36,12 +36,12 @@  discard block
 block discarded – undo
36 36
 {
37 37
     if ($Trap == null)
38 38
     {  // Exception in trap creation : log in display & syslog
39
-        $logging = new Logging();
40
-        $logging->log("Caught exception creating Trap class",2);
39
+        $logging=new Logging();
40
+        $logging->log("Caught exception creating Trap class", 2);
41 41
     }
42 42
     else
43 43
     {
44
-	   $Trap->trapLog("Exception : ". $e->getMessage(),2,0);
44
+	   $Trap->trapLog("Exception : ".$e->getMessage(), 2, 0);
45 45
     }
46 46
 }
47 47
 
Please login to merge, or discard this patch.
bin/trap_class.php 1 patch
Spacing   +415 added lines, -415 removed lines patch added patch discarded remove patch
@@ -32,8 +32,8 @@  discard block
 block discarded – undo
32 32
 	protected $api_password='';
33 33
 
34 34
 	// Logs 
35
-	protected $logging;    //< Logging class.
36
-	protected $logSetup;   //< bool true if log was setup in constructor
35
+	protected $logging; //< Logging class.
36
+	protected $logSetup; //< bool true if log was setup in constructor
37 37
 	
38 38
 	// Databases
39 39
 	protected $trapsDB; //< Database class
@@ -52,7 +52,7 @@  discard block
 block discarded – undo
52 52
 	private $objectsAll; //< output lines of snmptranslate list
53 53
 	private $trapObjectsIndex; //< array of traps objects (as OID)
54 54
 	
55
-	function __construct($etc_dir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='')
55
+	function __construct($etc_dir='/etc/icingaweb2', $baseLogLevel=null, $baseLogMode='syslog', $baseLogFile='')
56 56
 	{
57 57
 	    // Paths of ini files
58 58
 		$this->icingaweb2_etc=$etc_dir;
@@ -60,10 +60,10 @@  discard block
 block discarded – undo
60 60
 		$this->icingaweb2_ressources=$this->icingaweb2_etc."/resources.ini";
61 61
 
62 62
 		// Setup logging
63
-		$this->logging = new Logging();
63
+		$this->logging=new Logging();
64 64
 		if ($baseLogLevel != null)
65 65
 		{
66
-		    $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile);
66
+		    $this->logging->setLogging($baseLogLevel, $baseLogMode, $baseLogFile);
67 67
 		    $this->logSetup=true;
68 68
 		}
69 69
 		else 
@@ -71,10 +71,10 @@  discard block
 block discarded – undo
71 71
 		$this->logging->log('Loggin started', INFO);
72 72
 
73 73
 		// Get options from ini files
74
-		$trapConfig=parse_ini_file($this->trap_module_config,true);
74
+		$trapConfig=parse_ini_file($this->trap_module_config, true);
75 75
 		if ($trapConfig == false)
76 76
 		{
77
-		    $this->logging->log("Error reading ini file : ".$this->trap_module_config,ERROR,'syslog');
77
+		    $this->logging->log("Error reading ini file : ".$this->trap_module_config, ERROR, 'syslog');
78 78
 		}
79 79
 		$this->getMainOptions($trapConfig); // Get main options from ini file
80 80
 		$this->setupDatabase($trapConfig); // Setup database class
@@ -102,15 +102,15 @@  discard block
 block discarded – undo
102 102
 	 * @param string $message warning message if not found
103 103
 	 * @return boolean true if found, or false
104 104
 	 */
105
-	protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = 2, $message = null)
105
+	protected function getOptionIfSet($option_array, $option_category, $option_name, &$option_var, $log_level=2, $message=null)
106 106
 	{
107 107
 	    if (!isset($option_array[$option_category][$option_name]))
108 108
 	    {
109 109
 	        if ($message === null)
110 110
 	        {
111
-	            $message='No ' . $option_name . ' in config file: '. $this->trap_module_config;
111
+	            $message='No '.$option_name.' in config file: '.$this->trap_module_config;
112 112
 	        }
113
-	        $this->logging->log($message,$log_level,'syslog');
113
+	        $this->logging->log($message, $log_level, 'syslog');
114 114
 	        return false;
115 115
 	    }
116 116
 	    else
@@ -128,24 +128,24 @@  discard block
 block discarded – undo
128 128
 	{
129 129
 
130 130
 		// Snmptranslate binary path
131
-		$this->getOptionIfSet($trapConfig,'config','snmptranslate', $this->snmptranslate);
131
+		$this->getOptionIfSet($trapConfig, 'config', 'snmptranslate', $this->snmptranslate);
132 132
 
133 133
 		// mibs path
134
-		$this->getOptionIfSet($trapConfig,'config','snmptranslate_dirs', $this->snmptranslate_dirs);
134
+		$this->getOptionIfSet($trapConfig, 'config', 'snmptranslate_dirs', $this->snmptranslate_dirs);
135 135
 
136 136
 		// icinga2cmd path
137
-		$this->getOptionIfSet($trapConfig,'config','icingacmd', $this->icinga2cmd);
137
+		$this->getOptionIfSet($trapConfig, 'config', 'icingacmd', $this->icinga2cmd);
138 138
 		
139 139
 		// table prefix
140
-		$this->getOptionIfSet($trapConfig,'config','database_prefix', $this->db_prefix);
140
+		$this->getOptionIfSet($trapConfig, 'config', 'database_prefix', $this->db_prefix);
141 141
 
142 142
 		// API options
143
-		if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->api_hostname))
143
+		if ($this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_host', $this->api_hostname))
144 144
 		{
145 145
 		    $this->api_use=true;
146
-		    $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->api_port);
147
-		    $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->api_username);
148
-		    $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->api_password);
146
+		    $this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_port', $this->api_port);
147
+		    $this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_user', $this->api_username);
148
+		    $this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_password', $this->api_password);
149 149
 		}
150 150
 	}
151 151
 	
@@ -156,40 +156,40 @@  discard block
 block discarded – undo
156 156
 	protected function setupDatabase($trapConfig)
157 157
 	{
158 158
 	    // Trap database
159
-	    if (!array_key_exists('database',$trapConfig['config']))
159
+	    if (!array_key_exists('database', $trapConfig['config']))
160 160
         {
161
-            $this->logging->log("No database in config file: ".$this->trap_module_config,ERROR,'');
161
+            $this->logging->log("No database in config file: ".$this->trap_module_config, ERROR, '');
162 162
             return;
163 163
         }
164 164
         $dbTrapName=$trapConfig['config']['database'];
165
-        $this->logging->log("Found database in config file: ".$dbTrapName,INFO );
165
+        $this->logging->log("Found database in config file: ".$dbTrapName, INFO);
166 166
 	    
167
-	   if ( ($dbConfig=parse_ini_file($this->icingaweb2_ressources,true)) === false)
167
+	   if (($dbConfig=parse_ini_file($this->icingaweb2_ressources, true)) === false)
168 168
 	    {
169
-	        $this->logging->log("Error reading ini file : ".$this->icingaweb2_ressources,ERROR,'');
169
+	        $this->logging->log("Error reading ini file : ".$this->icingaweb2_ressources, ERROR, '');
170 170
 	        return;
171 171
 	    }
172
-	    if (!array_key_exists($dbTrapName,$dbConfig))
172
+	    if (!array_key_exists($dbTrapName, $dbConfig))
173 173
 	    {
174
-	        $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2_ressources,ERROR,'');
174
+	        $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2_ressources, ERROR, '');
175 175
 	        return;
176 176
 	    }
177 177
 	    
178
-        $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName]);
178
+        $this->trapsDB=new Database($this->logging, $dbConfig[$dbTrapName]);
179 179
 	    
180 180
 	    if ($this->api_use === true) return; // In case of API use, no IDO is necessary
181 181
         
182 182
 	    // IDO Database
183
-	    if (!array_key_exists('IDOdatabase',$trapConfig['config']))
183
+	    if (!array_key_exists('IDOdatabase', $trapConfig['config']))
184 184
 	    {
185
-	        $this->logging->log("No IDOdatabase in config file: ".$this->trap_module_config,ERROR,'');
185
+	        $this->logging->log("No IDOdatabase in config file: ".$this->trap_module_config, ERROR, '');
186 186
 	    }
187 187
 	    $dbIdoName=$trapConfig['config']['IDOdatabase'];		
188 188
 
189
-	    $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO );
190
-        if (!array_key_exists($dbIdoName,$dbConfig))
189
+	    $this->logging->log("Found IDO database in config file: ".$dbIdoName, INFO);
190
+        if (!array_key_exists($dbIdoName, $dbConfig))
191 191
 	    {
192
-	        $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2_ressources,ERROR,'');
192
+	        $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2_ressources, ERROR, '');
193 193
 	        return;
194 194
 	    }
195 195
 	    
@@ -204,13 +204,13 @@  discard block
 block discarded – undo
204 204
 		// Database options
205 205
 		if ($this->logSetup === false) // Only if logging was no setup in constructor
206 206
 		{
207
-    		$this->getDBConfigIfSet('log_level',$this->logging->debugLevel);
208
-    		$this->getDBConfigIfSet('log_destination',$this->logging->outputMode);
209
-    		$this->getDBConfigIfSet('log_file',$this->logging->outputFile);
207
+    		$this->getDBConfigIfSet('log_level', $this->logging->debugLevel);
208
+    		$this->getDBConfigIfSet('log_destination', $this->logging->outputMode);
209
+    		$this->getDBConfigIfSet('log_file', $this->logging->outputFile);
210 210
 		}
211 211
 	}
212 212
 
213
-	protected function getDBConfigIfSet($element,&$variable)
213
+	protected function getDBConfigIfSet($element, &$variable)
214 214
 	{
215 215
 		$value=$this->getDBConfig($element);
216 216
 		if ($value != 'null') $variable=$value;
@@ -226,7 +226,7 @@  discard block
 block discarded – undo
226 226
 		$db_conn=$this->trapsDB->db_connect_trap();
227 227
 		$sql='SELECT value from '.$this->db_prefix.'db_config WHERE ( name=\''.$element.'\' )';
228 228
 		if (($ret_code=$db_conn->query($sql)) === false) {
229
-			$this->logging->log('No result in query : ' . $sql,WARN,'');
229
+			$this->logging->log('No result in query : '.$sql, WARN, '');
230 230
 			return null;
231 231
 		}
232 232
 		$value=$ret_code->fetch();
@@ -243,22 +243,22 @@  discard block
 block discarded – undo
243 243
 	*	@param  string $destination file/syslog/display
244 244
 	*	@return void
245 245
 	**/	
246
-	public function trapLog( $message, $level, $destination ='') // OBSOLETE
246
+	public function trapLog($message, $level, $destination='') // OBSOLETE
247 247
 	{	
248 248
 		// TODO : replace ref with $this->logging->log 
249 249
 	    $this->logging->log($message, $level, $destination);
250 250
 	}
251 251
 	
252
-	public function setLogging($debugLvl,$outputType,$outputOption=null)  // OBSOLETE
252
+	public function setLogging($debugLvl, $outputType, $outputOption=null)  // OBSOLETE
253 253
 	{
254
-		$this->logging->setLogging($debugLvl, $outputType,$outputOption);
254
+		$this->logging->setLogging($debugLvl, $outputType, $outputOption);
255 255
 	}
256 256
 	
257 257
 	protected function getAPI()
258 258
 	{
259 259
 	    if ($this->icinga2api == null)
260 260
 	    {
261
-	        $this->icinga2api = new Icinga2API($this->api_hostname,$this->api_port);
261
+	        $this->icinga2api=new Icinga2API($this->api_hostname, $this->api_port);
262 262
 	    }
263 263
 	    return $this->icinga2api;
264 264
 	}
@@ -277,7 +277,7 @@  discard block
 block discarded – undo
277 277
 		if ($input_stream === false)
278 278
 		{
279 279
 		    $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)");
280
-			$this->logging->log("Error reading stdin !",ERROR,'');
280
+			$this->logging->log("Error reading stdin !", ERROR, '');
281 281
 			return null; // note : exception thrown by logging
282 282
 		}
283 283
 
@@ -286,21 +286,21 @@  discard block
 block discarded – undo
286 286
 		if ($this->receivingHost === false)
287 287
 		{
288 288
 		    $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)");
289
-			$this->logging->log("Error reading Host !",ERROR,''); 
289
+			$this->logging->log("Error reading Host !", ERROR, ''); 
290 290
 		}
291 291
 		// line 2 IP:port=>IP:port
292 292
 		$IP=chop(fgets($input_stream));
293 293
 		if ($IP === false)
294 294
 		{
295 295
 		    $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)");
296
-			$this->logging->log("Error reading IP !",ERROR,''); 
296
+			$this->logging->log("Error reading IP !", ERROR, ''); 
297 297
 		}
298 298
 		$matches=array();
299
-		$ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches);
300
-		if ($ret_code===0 || $ret_code===false) 
299
+		$ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/', $IP, $matches);
300
+		if ($ret_code === 0 || $ret_code === false) 
301 301
 		{
302 302
 		    $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)");
303
-			$this->logging->log('Error parsing IP : '.$IP,ERROR,'');
303
+			$this->logging->log('Error parsing IP : '.$IP, ERROR, '');
304 304
 		} 
305 305
 		else 
306 306
 		{		
@@ -310,34 +310,34 @@  discard block
 block discarded – undo
310 310
 			$this->trap_data['destination_port']=$matches[4];
311 311
 		}
312 312
 
313
-		while (($vars=fgets($input_stream)) !==false)
313
+		while (($vars=fgets($input_stream)) !== false)
314 314
 		{
315 315
 			$vars=chop($vars);
316
-			$ret_code=preg_match('/^([^ ]+) (.*)$/',$vars,$matches);
317
-			if ($ret_code===0 || $ret_code===false) 
316
+			$ret_code=preg_match('/^([^ ]+) (.*)$/', $vars, $matches);
317
+			if ($ret_code === 0 || $ret_code === false) 
318 318
 			{
319
-				$this->logging->log('No match on trap data : '.$vars,WARN,'');
319
+				$this->logging->log('No match on trap data : '.$vars, WARN, '');
320 320
 			}
321 321
 			else 
322 322
 			{
323
-			    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'))
323
+			    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 324
 				{
325 325
 					$this->trap_data['trap_oid']=$matches[2];				
326 326
 				}
327 327
 				else
328 328
 				{
329
-					$object= new stdClass;
330
-					$object->oid =$matches[1];
331
-					$object->value = $matches[2];
332
-					array_push($this->trap_data_ext,$object);
329
+					$object=new stdClass;
330
+					$object->oid=$matches[1];
331
+					$object->value=$matches[2];
332
+					array_push($this->trap_data_ext, $object);
333 333
 				}
334 334
 			}
335 335
 		}
336 336
 
337
-		if ($this->trap_data['trap_oid']=='unknown') 
337
+		if ($this->trap_data['trap_oid'] == 'unknown') 
338 338
 		{
339
-		    $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trap_data['source_ip']);
340
-			$this->logging->log('no trap oid found',ERROR,'');
339
+		    $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)", $this->trap_data['source_ip']);
340
+			$this->logging->log('no trap oid found', ERROR, '');
341 341
 		} 
342 342
 
343 343
 		// Translate oids.
@@ -359,7 +359,7 @@  discard block
 block discarded – undo
359 359
 		}
360 360
 		
361 361
 
362
-		$this->trap_data['status']= 'waiting';
362
+		$this->trap_data['status']='waiting';
363 363
 		
364 364
 		return $this->trap_data;
365 365
 	}
@@ -375,40 +375,40 @@  discard block
 block discarded – undo
375 375
 		$db_conn=$this->trapsDB->db_connect_trap();
376 376
 		
377 377
 		$sql='SELECT mib,name from '.$this->db_prefix.'mib_cache WHERE oid=\''.$oid.'\';';
378
-		$this->logging->log('SQL query : '.$sql,DEBUG );
378
+		$this->logging->log('SQL query : '.$sql, DEBUG);
379 379
 		if (($ret_code=$db_conn->query($sql)) === false) {
380
-			$this->logging->log('No result in query : ' . $sql,ERROR,'');
380
+			$this->logging->log('No result in query : '.$sql, ERROR, '');
381 381
 		}
382 382
 		$name=$ret_code->fetch();
383 383
 		if ($name['name'] != null)
384 384
 		{
385
-			return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']);
385
+			return array('trap_name_mib'=>$name['mib'], 'trap_name'=>$name['name']);
386 386
 		}
387 387
 		
388 388
 		// Also check if it is an instance of OID
389
-		$oid_instance=preg_replace('/\.[0-9]+$/','',$oid);
389
+		$oid_instance=preg_replace('/\.[0-9]+$/', '', $oid);
390 390
 		
391 391
 		$sql='SELECT mib,name from '.$this->db_prefix.'mib_cache WHERE oid=\''.$oid_instance.'\';';
392
-		$this->logging->log('SQL query : '.$sql,DEBUG );
392
+		$this->logging->log('SQL query : '.$sql, DEBUG);
393 393
 		if (($ret_code=$db_conn->query($sql)) === false) {
394
-			$this->logging->log('No result in query : ' . $sql,ERROR,'');
394
+			$this->logging->log('No result in query : '.$sql, ERROR, '');
395 395
 		}
396 396
 		$name=$ret_code->fetch();
397 397
 		if ($name['name'] != null)
398 398
 		{
399
-			return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']);
399
+			return array('trap_name_mib'=>$name['mib'], 'trap_name'=>$name['name']);
400 400
 		}
401 401
 		
402 402
 		// Try to get oid name from snmptranslate
403
-		$translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.
403
+		$translate=exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs.
404 404
 		    ' '.$oid);
405 405
 		$matches=array();
406
-		$ret_code=preg_match('/(.*)::(.*)/',$translate,$matches);
407
-		if ($ret_code===0 || $ret_code === false) {
406
+		$ret_code=preg_match('/(.*)::(.*)/', $translate, $matches);
407
+		if ($ret_code === 0 || $ret_code === false) {
408 408
 			return NULL;
409 409
 		} else {
410
-			$this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid,INFO);
411
-			return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]);
410
+			$this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid, INFO);
411
+			return array('trap_name_mib'=>$matches[1], 'trap_name'=>$matches[2]);
412 412
 		}	
413 413
 	}
414 414
 	
@@ -419,90 +419,90 @@  discard block
 block discarded – undo
419 419
 	**/
420 420
 	public function eraseOldTraps($days=0)
421 421
 	{
422
-		if ($days==0)
422
+		if ($days == 0)
423 423
 		{
424 424
 			if (($days=$this->getDBConfig('db_remove_days')) == null)
425 425
 			{
426
-				$this->logging->log('No days specified & no db value : no tap erase' ,WARN,'');
426
+				$this->logging->log('No days specified & no db value : no tap erase', WARN, '');
427 427
 				return;
428 428
 			}
429 429
 		}
430 430
 		$db_conn=$this->trapsDB->db_connect_trap();
431
-		$daysago = strtotime("-".$days." day");
432
-		$sql= 'delete from '.$this->db_prefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';';
431
+		$daysago=strtotime("-".$days." day");
432
+		$sql='delete from '.$this->db_prefix.'received where date_received < \''.date("Y-m-d H:i:s", $daysago).'\';';
433 433
 		if ($db_conn->query($sql) === false) {
434
-			$this->logging->log('Error erasing traps : '.$sql,ERROR,'');
434
+			$this->logging->log('Error erasing traps : '.$sql, ERROR, '');
435 435
 		}
436
-		$this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql,INFO);
436
+		$this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql, INFO);
437 437
 	}
438 438
 
439 439
 	/** Write error to received trap database
440 440
 	 */
441
-	public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null)
441
+	public function writeTrapErrorToDB($message, $sourceIP=null, $trapoid=null)
442 442
 	{
443 443
 	    
444 444
 	    $db_conn=$this->trapsDB->db_connect_trap();
445 445
 	    
446 446
 	    // add date time
447
-	    $insert_col ='date_received,status';
448
-	    $insert_val = "'" . date("Y-m-d H:i:s")."','error'";
447
+	    $insert_col='date_received,status';
448
+	    $insert_val="'".date("Y-m-d H:i:s")."','error'";
449 449
         
450
-	    if ($sourceIP !=null)
450
+	    if ($sourceIP != null)
451 451
 	    {
452
-	        $insert_col .=',source_ip';
453
-	        $insert_val .=",'". $sourceIP ."'";
452
+	        $insert_col.=',source_ip';
453
+	        $insert_val.=",'".$sourceIP."'";
454 454
 	    }
455
-	    if ($trapoid !=null)
455
+	    if ($trapoid != null)
456 456
 	    {
457
-	        $insert_col .=',trap_oid';
458
-	        $insert_val .=",'". $trapoid ."'";
457
+	        $insert_col.=',trap_oid';
458
+	        $insert_val.=",'".$trapoid."'";
459 459
 	    }
460
-	    $insert_col .=',status_detail';
461
-	    $insert_val .=",'". $message ."'";
460
+	    $insert_col.=',status_detail';
461
+	    $insert_val.=",'".$message."'";
462 462
 	    
463
-	    $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')';
463
+	    $sql='INSERT INTO '.$this->db_prefix.'received ('.$insert_col.') VALUES ('.$insert_val.')';
464 464
 	    
465 465
 	    switch ($this->trapsDB->trapDBType)
466 466
 	    {
467 467
 	        case 'pgsql':
468
-	            $sql .= ' RETURNING id;';
469
-	            $this->logging->log('sql : '.$sql,INFO);
468
+	            $sql.=' RETURNING id;';
469
+	            $this->logging->log('sql : '.$sql, INFO);
470 470
 	            if (($ret_code=$db_conn->query($sql)) === false) {
471
-	                $this->logging->log('Error SQL insert : '.$sql,1,'');
471
+	                $this->logging->log('Error SQL insert : '.$sql, 1, '');
472 472
 	            }
473
-	            $this->logging->log('SQL insertion OK',INFO );
473
+	            $this->logging->log('SQL insertion OK', INFO);
474 474
 	            // Get last id to insert oid/values in secondary table
475 475
 	            if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) {
476 476
 	                
477
-	                $this->logging->log('Erreur recuperation id',1,'');
477
+	                $this->logging->log('Erreur recuperation id', 1, '');
478 478
 	            }
479
-	            if (! isset($inserted_id_ret['id'])) {
480
-	                $this->logging->log('Error getting id',1,'');
479
+	            if (!isset($inserted_id_ret['id'])) {
480
+	                $this->logging->log('Error getting id', 1, '');
481 481
 	            }
482 482
 	            $this->trap_id=$inserted_id_ret['id'];
483 483
 	            break;
484 484
 	        case 'mysql':
485
-	            $sql .= ';';
486
-	            $this->logging->log('sql : '.$sql,INFO );
485
+	            $sql.=';';
486
+	            $this->logging->log('sql : '.$sql, INFO);
487 487
 	            if ($db_conn->query($sql) === false) {
488
-	                $this->logging->log('Error SQL insert : '.$sql,1,'');
488
+	                $this->logging->log('Error SQL insert : '.$sql, 1, '');
489 489
 	            }
490
-	            $this->logging->log('SQL insertion OK',INFO );
490
+	            $this->logging->log('SQL insertion OK', INFO);
491 491
 	            // Get last id to insert oid/values in secondary table
492 492
 	            $sql='SELECT LAST_INSERT_ID();';
493 493
 	            if (($ret_code=$db_conn->query($sql)) === false) {
494
-	                $this->logging->log('Erreur recuperation id',1,'');
494
+	                $this->logging->log('Erreur recuperation id', 1, '');
495 495
 	            }
496 496
 	            
497 497
 	            $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");
498
+	            if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
499 499
 	            $this->trap_id=$inserted_id;
500 500
 	            break;
501 501
 	        default:
502
-	            $this->logging->log('Error SQL type unknown  : '.$this->trapsDB->trapDBType,1,'');
502
+	            $this->logging->log('Error SQL type unknown  : '.$this->trapsDB->trapDBType, 1, '');
503 503
 	    }
504 504
 	    
505
-	    $this->logging->log('id found: '. $this->trap_id,INFO );    
505
+	    $this->logging->log('id found: '.$this->trap_id, INFO);    
506 506
 	}
507 507
 	
508 508
 	/** Write trap data to trap database
@@ -519,86 +519,86 @@  discard block
 block discarded – undo
519 519
 		$insert_col='';
520 520
 		$insert_val='';
521 521
 		// add date time
522
-		$this->trap_data['date_received'] = date("Y-m-d H:i:s");
522
+		$this->trap_data['date_received']=date("Y-m-d H:i:s");
523 523
 
524 524
 		$firstcol=1;
525 525
 		foreach ($this->trap_data as $col => $val)
526 526
 		{
527
-			if ($firstcol==0) 
527
+			if ($firstcol == 0) 
528 528
 			{
529
-				$insert_col .=',';
530
-				$insert_val .=',';
529
+				$insert_col.=',';
530
+				$insert_val.=',';
531 531
 			}
532
-			$insert_col .= $col ;
533
-			$insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val);
532
+			$insert_col.=$col;
533
+			$insert_val.=($val == null) ? 'NULL' : $db_conn->quote($val);
534 534
 			$firstcol=0;
535 535
 		}
536 536
 		
537
-		$sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')';
537
+		$sql='INSERT INTO '.$this->db_prefix.'received ('.$insert_col.') VALUES ('.$insert_val.')';
538 538
 		switch ($this->trapsDB->trapDBType)
539 539
 		{
540 540
 			case 'pgsql': 
541
-				$sql .= ' RETURNING id;';
542
-				$this->logging->log('sql : '.$sql,INFO );
541
+				$sql.=' RETURNING id;';
542
+				$this->logging->log('sql : '.$sql, INFO);
543 543
 				if (($ret_code=$db_conn->query($sql)) === false) {
544
-					$this->logging->log('Error SQL insert : '.$sql,ERROR,'');
544
+					$this->logging->log('Error SQL insert : '.$sql, ERROR, '');
545 545
 				}
546
-				$this->logging->log('SQL insertion OK',INFO );
546
+				$this->logging->log('SQL insertion OK', INFO);
547 547
 				// Get last id to insert oid/values in secondary table
548 548
 				if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) {
549 549
 														   
550
-					$this->logging->log('Erreur recuperation id',ERROR,'');
550
+					$this->logging->log('Erreur recuperation id', ERROR, '');
551 551
 				}
552
-				if (! isset($inserted_id_ret['id'])) {
553
-					$this->logging->log('Error getting id',ERROR,'');
552
+				if (!isset($inserted_id_ret['id'])) {
553
+					$this->logging->log('Error getting id', ERROR, '');
554 554
 				}
555 555
 				$this->trap_id=$inserted_id_ret['id'];
556 556
 			break;
557 557
 			case 'mysql': 
558
-				$sql .= ';';
559
-				$this->logging->log('sql : '.$sql,INFO );
558
+				$sql.=';';
559
+				$this->logging->log('sql : '.$sql, INFO);
560 560
 				if ($db_conn->query($sql) === false) {
561
-					$this->logging->log('Error SQL insert : '.$sql,ERROR,'');
561
+					$this->logging->log('Error SQL insert : '.$sql, ERROR, '');
562 562
 				}
563
-				$this->logging->log('SQL insertion OK',INFO );
563
+				$this->logging->log('SQL insertion OK', INFO);
564 564
 				// Get last id to insert oid/values in secondary table
565 565
 				$sql='SELECT LAST_INSERT_ID();';
566 566
 				if (($ret_code=$db_conn->query($sql)) === false) {
567
-					$this->logging->log('Erreur recuperation id',ERROR,'');
567
+					$this->logging->log('Erreur recuperation id', ERROR, '');
568 568
 				}
569 569
 
570 570
 				$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");
571
+				if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
572 572
 				$this->trap_id=$inserted_id;
573 573
 			break;
574 574
 			default: 
575
-				$this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,ERROR,'');
575
+				$this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType, ERROR, '');
576 576
 		}
577
-		$this->logging->log('id found: '.$this->trap_id,INFO );
577
+		$this->logging->log('id found: '.$this->trap_id, INFO);
578 578
 		
579 579
 		// Fill trap extended data table
580 580
 		foreach ($this->trap_data_ext as $value) {			
581 581
 			// TODO : detect if trap value is encoded and decode it to UTF-8 for database
582 582
 			$firstcol=1;
583
-			$value->trap_id = $this->trap_id;
583
+			$value->trap_id=$this->trap_id;
584 584
 			$insert_col='';
585 585
 			$insert_val='';
586 586
 			foreach ($value as $col => $val)
587 587
 			{
588
-				if ($firstcol==0) 
588
+				if ($firstcol == 0) 
589 589
 				{
590
-					$insert_col .=',';
591
-					$insert_val .=',';
590
+					$insert_col.=',';
591
+					$insert_val.=',';
592 592
 				}
593
-				$insert_col .= $col;
594
-				$insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val);
593
+				$insert_col.=$col;
594
+				$insert_val.=($val == null) ? 'NULL' : $db_conn->quote($val);
595 595
 				$firstcol=0;
596 596
 			}
597 597
 
598
-			$sql= 'INSERT INTO '.$this->db_prefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');';			
598
+			$sql='INSERT INTO '.$this->db_prefix.'received_data ('.$insert_col.') VALUES ('.$insert_val.');';			
599 599
 
600 600
 			if ($db_conn->query($sql) === false) {
601
-				$this->logging->log('Erreur insertion data : ' . $sql,WARN,'');
601
+				$this->logging->log('Erreur insertion data : '.$sql, WARN, '');
602 602
 			}	
603 603
 		}	
604 604
 	}
@@ -608,14 +608,14 @@  discard block
 block discarded – undo
608 608
 	*	@param $oid string oid in numeric
609 609
 	*	@return mixed : PDO object or false
610 610
 	*/	
611
-	protected function getRules($ip,$oid)
611
+	protected function getRules($ip, $oid)
612 612
 	{
613 613
 		$db_conn=$this->trapsDB->db_connect_trap();
614 614
 		// fetch rules based on IP in rule and OID
615 615
 		$sql='SELECT * from '.$this->db_prefix.'rules WHERE trap_oid=\''.$oid.'\' ';
616
-		$this->logging->log('SQL query : '.$sql,DEBUG );
616
+		$this->logging->log('SQL query : '.$sql, DEBUG);
617 617
 		if (($ret_code=$db_conn->query($sql)) === false) {
618
-			$this->logging->log('No result in query : ' . $sql,WARN,'');
618
+			$this->logging->log('No result in query : '.$sql, WARN, '');
619 619
 			return false;
620 620
 		}
621 621
 		$rules_all=$ret_code->fetchAll();
@@ -624,7 +624,7 @@  discard block
 block discarded – undo
624 624
 		$rule_ret_key=0;
625 625
 		foreach ($rules_all as $key => $rule)
626 626
 		{
627
-			if ($rule['ip4']==$ip || $rule['ip6']==$ip)
627
+			if ($rule['ip4'] == $ip || $rule['ip6'] == $ip)
628 628
 			{
629 629
 				$rules_ret[$rule_ret_key]=$rules_all[$key];
630 630
 				//TODO : get host name by API (and check if correct in rule).
@@ -632,7 +632,7 @@  discard block
 block discarded – undo
632 632
 				continue;
633 633
 			}
634 634
 			// TODO : get hosts IP by API
635
-			if (isset($rule['host_group_name']) && $rule['host_group_name']!=null)
635
+			if (isset($rule['host_group_name']) && $rule['host_group_name'] != null)
636 636
 			{ // get ips of group members by oid
637 637
 				$db_conn2=$this->trapsDB->db_connect_ido();
638 638
 				$sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name
@@ -643,15 +643,15 @@  discard block
 block discarded – undo
643 643
 						LEFT JOIN icinga_objects as b ON b.object_id = a.host_object_id
644 644
 						WHERE o.name1='".$rule['host_group_name']."';";
645 645
 				if (($ret_code2=$db_conn2->query($sql)) === false) {
646
-					$this->logging->log('No result in query : ' . $sql,WARN,'');
646
+					$this->logging->log('No result in query : '.$sql, WARN, '');
647 647
 					continue;
648 648
 				}
649 649
 				$grouphosts=$ret_code2->fetchAll();
650 650
 				//echo "rule grp :\n";print_r($grouphosts);echo "\n";
651
-				foreach ( $grouphosts as $host)
651
+				foreach ($grouphosts as $host)
652 652
 				{
653 653
 					//echo $host['ip4']."\n";
654
-					if ($host['ip4']==$ip || $host['ip6']==$ip)
654
+					if ($host['ip4'] == $ip || $host['ip6'] == $ip)
655 655
 					{
656 656
 						//echo "Rule added \n";
657 657
 						$rules_ret[$rule_ret_key]=$rules_all[$key];
@@ -674,7 +674,7 @@  discard block
 block discarded – undo
674 674
 		$db_conn=$this->trapsDB->db_connect_trap();
675 675
 		$sql="UPDATE ".$this->db_prefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');";
676 676
 		if ($db_conn->query($sql) === false) {
677
-			$this->logging->log('Error in update query : ' . $sql,WARN,'');
677
+			$this->logging->log('Error in update query : '.$sql, WARN, '');
678 678
 		}
679 679
 	}
680 680
 	
@@ -686,31 +686,31 @@  discard block
 block discarded – undo
686 686
 	 * @param string $display
687 687
 	 * @returnn bool true is service check was sent without error
688 688
 	*/
689
-	public function serviceCheckResult($host,$service,$state,$display)
689
+	public function serviceCheckResult($host, $service, $state, $display)
690 690
 	{
691 691
 	    if ($this->api_use === false)
692 692
 	    {
693
-    		$send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' .
694
-    			$host.';' .$service .';' . $state . ';'.$display;
695
-    		$this->logging->log( $send." : to : " .$this->icinga2cmd,INFO );
693
+    		$send='['.date('U').'] PROCESS_SERVICE_CHECK_RESULT;'.
694
+    			$host.';'.$service.';'.$state.';'.$display;
695
+    		$this->logging->log($send." : to : ".$this->icinga2cmd, INFO);
696 696
     		
697 697
     		// TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default....
698
-    		exec('echo "'.$send.'" > ' .$this->icinga2cmd);
698
+    		exec('echo "'.$send.'" > '.$this->icinga2cmd);
699 699
     		return true;
700 700
 	    }
701 701
 	    else
702 702
 	    {
703
-	        $api = $this->getAPI();
703
+	        $api=$this->getAPI();
704 704
 	        $api->setCredentials($this->api_username, $this->api_password);
705
-	        list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display);
705
+	        list($retcode, $retmessage)=$api->serviceCheckResult($host, $service, $state, $display);
706 706
 	        if ($retcode == false)
707 707
 	        {
708
-	            $this->logging->log( "Error sending result : " .$retmessage,WARN,'');
708
+	            $this->logging->log("Error sending result : ".$retmessage, WARN, '');
709 709
 	            return false;
710 710
 	        }
711 711
 	        else 
712 712
 	        {
713
-	            $this->logging->log( "Sent result : " .$retmessage,INFO );
713
+	            $this->logging->log("Sent result : ".$retmessage, INFO);
714 714
 	            return true;
715 715
 	        }
716 716
 	    }
@@ -718,7 +718,7 @@  discard block
 block discarded – undo
718 718
 	
719 719
 	public function getHostByIP($ip)
720 720
 	{
721
-	    $api = $this->getAPI();
721
+	    $api=$this->getAPI();
722 722
 	    $api->setCredentials($this->api_username, $this->api_password);
723 723
 	    return $api->getHostByIP($ip);
724 724
 	}
@@ -731,32 +731,32 @@  discard block
 block discarded – undo
731 731
 	protected function applyDisplay($display)
732 732
 	{
733 733
 	    $matches=array();
734
-	    while (preg_match('/_OID\(([0-9\.]+)\)/',$display,$matches) == 1)
734
+	    while (preg_match('/_OID\(([0-9\.]+)\)/', $display, $matches) == 1)
735 735
 		{
736 736
 			$oid=$matches[1];
737 737
 			$found=0;
738
-			foreach($this->trap_data_ext as $val)
738
+			foreach ($this->trap_data_ext as $val)
739 739
 			{
740 740
 				if ($oid == $val->oid)
741 741
 				{
742
-					$val->value=preg_replace('/"/','',$val->value);
742
+					$val->value=preg_replace('/"/', '', $val->value);
743 743
 					$rep=0;
744
-					$display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep);
745
-					if ($rep==0)
744
+					$display=preg_replace('/_OID\('.$oid.'\)/', $val->value, $display, -1, $rep);
745
+					if ($rep == 0)
746 746
 					{
747
-						$this->logging->log("Error in display",WARN,'');
747
+						$this->logging->log("Error in display", WARN, '');
748 748
 						return $display;
749 749
 					}
750 750
 					$found=1;
751 751
 					break;
752 752
 				}
753 753
 			}
754
-			if ($found==0)
754
+			if ($found == 0)
755 755
 			{
756
-				$display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep);
757
-				if ($rep==0)
756
+				$display=preg_replace('/_OID\('.$oid.'\)/', '<not in trap>', $display, -1, $rep);
757
+				if ($rep == 0)
758 758
 				{
759
-					$this->logging->log("Error in display",WARN,'');
759
+					$this->logging->log("Error in display", WARN, '');
760 760
 					return $display;
761 761
 				}				
762 762
 			}
@@ -766,27 +766,27 @@  discard block
 block discarded – undo
766 766
 
767 767
 	
768 768
 	/***************** Eval & tokenizer functions ****************/
769
-	protected function eval_getElement($rule,&$item)
769
+	protected function eval_getElement($rule, &$item)
770 770
 	{
771
-		while ($rule[$item]==' ') $item++;
772
-		if (preg_match('/[0-9\.]/',$rule[$item]))
771
+		while ($rule[$item] == ' ') $item++;
772
+		if (preg_match('/[0-9\.]/', $rule[$item]))
773 773
 		{ // number
774 774
 	
775
-			$item2=$item+1; 
776
-			while (($item2!=strlen($rule)) && (preg_match('/[0-9\.]/',$rule[$item2]))) { $item2++ ;}
777
-			$val=substr($rule,$item,$item2-$item);
775
+			$item2=$item + 1; 
776
+			while (($item2 != strlen($rule)) && (preg_match('/[0-9\.]/', $rule[$item2]))) { $item2++; }
777
+			$val=substr($rule, $item, $item2 - $item);
778 778
 			$item=$item2;
779 779
 			//echo "number ".$val."\n";
780
-			return array(0,$val);
780
+			return array(0, $val);
781 781
 		}
782 782
 		if ($rule[$item] == '"')
783 783
 		{ // string
784 784
 			$item++;
785
-			$item2=$this->eval_getNext($rule,$item,'"');
786
-			$val=substr($rule,$item,$item2-$item-1);
785
+			$item2=$this->eval_getNext($rule, $item, '"');
786
+			$val=substr($rule, $item, $item2 - $item - 1);
787 787
 			$item=$item2;
788 788
 			//echo "string : ".$val."\n";
789
-			return array(1,$val);
789
+			return array(1, $val);
790 790
 		}
791 791
 		
792 792
 		if ($rule[$item] == '(')
@@ -795,14 +795,14 @@  discard block
 block discarded – undo
795 795
 			$start=$item;
796 796
 			$parenthesis_count=0; 
797 797
 			while (($item < strlen($rule)) // Not end of string AND
798
-			      && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded ()
798
+			      && (($rule[$item] != ')') || $parenthesis_count > 0)) // Closing ')' or embeded ()
799 799
 			{ 
800
-				if ($rule[$item] == '"' )
800
+				if ($rule[$item] == '"')
801 801
 				{ // pass through string
802 802
 					$item++;
803
-					$item=$this->eval_getNext($rule,$item,'"');
803
+					$item=$this->eval_getNext($rule, $item, '"');
804 804
 				} 
805
-				else{
805
+				else {
806 806
 				    if ($rule[$item] == '(')
807 807
 				    {
808 808
 				        $parenthesis_count++;
@@ -815,49 +815,49 @@  discard block
 block discarded – undo
815 815
 				}
816 816
 			}
817 817
 			
818
-			if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);}
819
-			$val=substr($rule,$start,$item-$start);
818
+			if ($item == strlen($rule)) {throw new Exception("no closing () in ".$rule." at ".$item); }
819
+			$val=substr($rule, $start, $item - $start);
820 820
 			$item++;
821 821
 			$start=0;
822 822
 			//echo "group : ".$val."\n";
823 823
 			// returns evaluation of group as type 2 (boolean)
824
-			return array(2,$this->evaluation($val,$start));		
824
+			return array(2, $this->evaluation($val, $start));		
825 825
 		}
826
-		throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]);
826
+		throw new Exception("number/string not found in ".$rule." at ".$item.' : '.$rule[$item]);
827 827
 		
828 828
 	}
829 829
 	
830
-	protected function eval_getNext($rule,$item,$tok)
830
+	protected function eval_getNext($rule, $item, $tok)
831 831
 	{
832
-		while (($rule[$item] != $tok ) && ($item < strlen($rule))) { $item++;}
833
-		if ($item==strlen($rule)) throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item);
834
-		return $item+1;
832
+		while (($rule[$item] != $tok) && ($item < strlen($rule))) { $item++; }
833
+		if ($item == strlen($rule)) throw new Exception("closing '".$tok."' not found in ".$rule." at ".$item);
834
+		return $item + 1;
835 835
 	}
836 836
 	
837
-	protected function eval_getOper($rule,&$item)
837
+	protected function eval_getOper($rule, &$item)
838 838
 	{
839
-		while ($rule[$item]==' ') $item++;
839
+		while ($rule[$item] == ' ') $item++;
840 840
 		switch ($rule[$item])
841 841
 		{
842 842
 			case '<':
843
-				if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");}
844
-				$item++; return array(0,"<");
843
+				if ($rule[$item + 1] == '=') { $item+=2; return array(0, "<="); }
844
+				$item++; return array(0, "<");
845 845
 			case '>':
846
-				if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");}
847
-				$item++; return array(0,">");
846
+				if ($rule[$item + 1] == '=') { $item+=2; return array(0, ">="); }
847
+				$item++; return array(0, ">");
848 848
 			case '=':
849
-				$item++; return array(0,"=");	
849
+				$item++; return array(0, "=");	
850 850
 			case '!':
851
-				if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");}
852
-				throw new Exception("Erreur in expr - incorrect operator '!'  found in ".$rule ." at " .$item);
851
+				if ($rule[$item + 1] == '=') { $item+=2; return array(0, "!="); }
852
+				throw new Exception("Erreur in expr - incorrect operator '!'  found in ".$rule." at ".$item);
853 853
 			case '~':
854
-				$item++; return array(0,"~");	
854
+				$item++; return array(0, "~");	
855 855
 			case '|':
856
-				$item++; return array(1,"|");	
856
+				$item++; return array(1, "|");	
857 857
 			case '&':
858
-				$item++; return array(1,"&");
858
+				$item++; return array(1, "&");
859 859
 			default	:
860
-				throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item);
860
+				throw new Exception("Erreur in expr - operator not found in ".$rule." at ".$item);
861 861
 		}
862 862
 	}
863 863
 	
@@ -869,10 +869,10 @@  discard block
 block discarded – undo
869 869
 	*   comparison int vs strings will return null (error)
870 870
 	*	return : bool or null on error
871 871
 	*/
872
-	public function evaluation($rule,&$item)
872
+	public function evaluation($rule, &$item)
873 873
 	{
874 874
 	    //echo "Evaluation of ".substr($rule,$item)."\n";
875
-		if ( $rule[$item] == '!') // If '!' found, negate next expression.
875
+		if ($rule[$item] == '!') // If '!' found, negate next expression.
876 876
 		{
877 877
 		    $negate=true;
878 878
 		    $item++;
@@ -882,63 +882,63 @@  discard block
 block discarded – undo
882 882
 		    $negate=false;
883 883
 		}
884 884
 		// First element : number, string or ()
885
-		list($type1,$val1) = $this->eval_getElement($rule,$item);
885
+		list($type1, $val1)=$this->eval_getElement($rule, $item);
886 886
 		//echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n";
887 887
 		
888
-		if ($item==strlen($rule)) // If only element, return value, but only boolean
888
+		if ($item == strlen($rule)) // If only element, return value, but only boolean
889 889
 		{
890 890
 		  if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule);
891
-		  if ($negate === true) $val1= ! $val1;
891
+		  if ($negate === true) $val1=!$val1;
892 892
 		  return $val1;
893 893
 		}  
894 894
 		
895 895
 		// Second element : operator
896
-		list($typec,$comp) = $this->eval_getOper($rule,$item);
896
+		list($typec, $comp)=$this->eval_getOper($rule, $item);
897 897
 		//echo "Comp : ".$comp." : ".substr($rule,$item)."\n";
898 898
         
899 899
 		// Third element : number, string or ()
900
-		if ( $rule[$item] == '!') // starts with a ! so evaluate whats next
900
+		if ($rule[$item] == '!') // starts with a ! so evaluate whats next
901 901
 		{
902 902
 		    $item++;
903 903
 		    if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule);
904
-		    $val2= ! $this->evaluation($rule,$item);
904
+		    $val2=!$this->evaluation($rule, $item);
905 905
 		    $type2=2; // result is a boolean 
906 906
 		}
907 907
 		else 
908 908
 		{
909
-		    list($type2,$val2) = $this->eval_getElement($rule,$item);
909
+		    list($type2, $val2)=$this->eval_getElement($rule, $item);
910 910
 		}
911 911
 		//echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n";
912 912
 		
913
-		if ($type1!=$type2)  // cannot compare different types
913
+		if ($type1 != $type2)  // cannot compare different types
914 914
 		{ 
915 915
 		    throw new Exception("Cannot compare string & number : ".$rule);
916 916
 		}
917
-		if ($typec==1 && $type1 !=2) // cannot use & or | with string/number
917
+		if ($typec == 1 && $type1 != 2) // cannot use & or | with string/number
918 918
 		{
919 919
 		    throw new Exception("Cannot use boolean operators with string & number : ".$rule);
920 920
 		}
921 921
 		
922
-		switch ($comp){
923
-			case '<':	$retVal= ($val1 < $val2); break;
924
-			case '<=':	$retVal= ($val1 <= $val2); break;
925
-			case '>':	$retVal= ($val1 > $val2); break;
926
-			case '>=':	$retVal= ($val1 >= $val2); break;
927
-			case '=':	$retVal= ($val1 == $val2); break;
928
-			case '!=':	$retVal= ($val1 != $val2); break;
929
-			case '~':	$retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break;
930
-			case '|':	$retVal= ($val1 || $val2); break;
931
-			case '&':	$retVal= ($val1 && $val2); break;
922
+		switch ($comp) {
923
+			case '<':	$retVal=($val1 < $val2); break;
924
+			case '<=':	$retVal=($val1 <= $val2); break;
925
+			case '>':	$retVal=($val1 > $val2); break;
926
+			case '>=':	$retVal=($val1 >= $val2); break;
927
+			case '=':	$retVal=($val1 == $val2); break;
928
+			case '!=':	$retVal=($val1 != $val2); break;
929
+			case '~':	$retVal=(preg_match('/'.preg_replace('/"/', '', $val2).'/', $val1)); break;
930
+			case '|':	$retVal=($val1 || $val2); break;
931
+			case '&':	$retVal=($val1 && $val2); break;
932 932
 			default:  throw new Exception("Error in expression - unknown comp : ".$comp);
933 933
 		}
934
-		if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression
934
+		if ($negate === true) $retVal=!$retVal; // Inverse result if negate before expression
935 935
 		
936
-		if ($item==strlen($rule)) return $retVal; // End of string : return evaluation
936
+		if ($item == strlen($rule)) return $retVal; // End of string : return evaluation
937 937
 		// check for logical operator :
938 938
 		switch ($rule[$item])
939 939
 		{
940
-			case '|':	$item++; return ($retVal || $this->evaluation($rule,$item) );
941
-			case '&':	$item++; return ($retVal && $this->evaluation($rule,$item) );
940
+			case '|':	$item++; return ($retVal || $this->evaluation($rule, $item));
941
+			case '&':	$item++; return ($retVal && $this->evaluation($rule, $item));
942 942
 			
943 943
 			default:  throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]);
944 944
 		}
@@ -950,17 +950,17 @@  discard block
 block discarded – undo
950 950
 		$rule2='';
951 951
 		while ($item < strlen($rule))
952 952
 		{
953
-			if ($rule[$item]==' ') { $item++; continue; }
954
-			if ($rule[$item]=='"')
953
+			if ($rule[$item] == ' ') { $item++; continue; }
954
+			if ($rule[$item] == '"')
955 955
 			{
956 956
 				$rule2.=$rule[$item];
957 957
 				$item++;
958
-				while (($rule[$item]!='"') && ($item < strlen($rule)))
958
+				while (($rule[$item] != '"') && ($item < strlen($rule)))
959 959
 				{
960 960
 					$rule2.=$rule[$item];
961 961
 					$item++;
962 962
 				}
963
-				if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item);
963
+				if ($item == strlen($rule)) throw new Exception("closing '\"' not found in ".$rule." at ".$item);
964 964
 				$rule2.=$rule[$item];
965 965
 				$item++;
966 966
 				continue;
@@ -980,12 +980,12 @@  discard block
 block discarded – undo
980 980
 	
981 981
 	protected function eval_rule($rule)
982 982
 	{
983
-		if ($rule==null || $rule == '') // Empty rule is always true
983
+		if ($rule == null || $rule == '') // Empty rule is always true
984 984
 		{
985 985
 			return true;
986 986
 		}
987 987
 		$matches=array();
988
-		while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1)
988
+		while (preg_match('/_OID\(([0-9\.\*]+)\)/', $rule, $matches) == 1)
989 989
 		{
990 990
 			$oid=$matches[1];
991 991
 			$found=0;
@@ -997,49 +997,49 @@  discard block
 block discarded – undo
997 997
 			// replace * with \* in oid for preg_replace
998 998
 			$oid=preg_replace('/\*/', '\*', $oid);
999 999
 			
1000
-			$this->logging->log('OID in rule : '.$oid.' / '.$oidR,DEBUG );
1000
+			$this->logging->log('OID in rule : '.$oid.' / '.$oidR, DEBUG);
1001 1001
 			
1002
-			foreach($this->trap_data_ext as $val)
1002
+			foreach ($this->trap_data_ext as $val)
1003 1003
 			{
1004
-				if (preg_match("/^$oidR$/",$val->oid) == 1)
1004
+				if (preg_match("/^$oidR$/", $val->oid) == 1)
1005 1005
 				{
1006
-					if (!preg_match('/^[0-9]*\.?[0-9]+$/',$val->value))
1006
+					if (!preg_match('/^[0-9]*\.?[0-9]+$/', $val->value))
1007 1007
 					{ // If not a number, change " to ' and put " around it
1008
-						$val->value=preg_replace('/"/',"'",$val->value);
1008
+						$val->value=preg_replace('/"/', "'", $val->value);
1009 1009
 						$val->value='"'.$val->value.'"';
1010 1010
 					}
1011 1011
 					$rep=0;
1012
-					$rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep);
1013
-					if ($rep==0)
1012
+					$rule=preg_replace('/_OID\('.$oid.'\)/', $val->value, $rule, -1, $rep);
1013
+					if ($rep == 0)
1014 1014
 					{
1015
-						$this->logging->log("Error in rule_eval",WARN,'');
1015
+						$this->logging->log("Error in rule_eval", WARN, '');
1016 1016
 						return false;
1017 1017
 					}
1018 1018
 					$found=1;
1019 1019
 					break;
1020 1020
 				}
1021 1021
 			}
1022
-			if ($found==0)
1022
+			if ($found == 0)
1023 1023
 			{	// OID not found : throw error
1024 1024
 			    throw new Exception('OID '.$oid.' not found in trap');
1025 1025
 			}
1026 1026
 		}
1027 1027
 		$item=0;
1028 1028
 		$rule=$this->eval_cleanup($rule);
1029
-		$this->logging->log('Rule after clenup: '.$rule,INFO );
1029
+		$this->logging->log('Rule after clenup: '.$rule, INFO);
1030 1030
 		
1031
-		return  $this->evaluation($rule,$item);
1031
+		return  $this->evaluation($rule, $item);
1032 1032
 	}
1033 1033
 	
1034 1034
 	/** Match rules for current trap and do action
1035 1035
 	*/
1036 1036
 	public function applyRules()
1037 1037
 	{
1038
-		$rules = $this->getRules($this->trap_data['source_ip'],$this->trap_data['trap_oid']);
1038
+		$rules=$this->getRules($this->trap_data['source_ip'], $this->trap_data['trap_oid']);
1039 1039
 		
1040
-		if ($rules===false || count($rules)==0)
1040
+		if ($rules === false || count($rules) == 0)
1041 1041
 		{
1042
-			$this->logging->log('No rules found for this trap',INFO );
1042
+			$this->logging->log('No rules found for this trap', INFO);
1043 1043
 			$this->trap_data['status']='unknown';
1044 1044
 			$this->trap_to_db=true;
1045 1045
 			return;
@@ -1054,58 +1054,58 @@  discard block
 block discarded – undo
1054 1054
 			$service_name=$rule['service_name'];
1055 1055
 			
1056 1056
 			$display=$this->applyDisplay($rule['display']);
1057
-			$this->trap_action = ($this->trap_action==null)? '' : $this->trap_action . ', ';
1057
+			$this->trap_action=($this->trap_action == null) ? '' : $this->trap_action.', ';
1058 1058
 			try
1059 1059
 			{
1060
-				$this->logging->log('Rule to eval : '.$rule['rule'],INFO );
1060
+				$this->logging->log('Rule to eval : '.$rule['rule'], INFO);
1061 1061
 				$evalr=$this->eval_rule($rule['rule']);
1062 1062
 				
1063 1063
 				if ($evalr == true)
1064 1064
 				{
1065 1065
 					//$this->logging->log('rules OOK: '.print_r($rule),INFO );
1066 1066
 					$action=$rule['action_match'];
1067
-					$this->logging->log('action OK : '.$action,INFO );
1067
+					$this->logging->log('action OK : '.$action, INFO);
1068 1068
 					if ($action >= 0)
1069 1069
 					{
1070
-						if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false)
1070
+						if ($this->serviceCheckResult($host_name, $service_name, $action, $display) == false)
1071 1071
 						{
1072 1072
 						    $this->trap_action.='Error sending status : check cmd/API';
1073 1073
 						}
1074 1074
 						else
1075 1075
 						{
1076
-						    $this->add_rule_match($rule['id'],$rule['num_match']+1);
1076
+						    $this->add_rule_match($rule['id'], $rule['num_match'] + 1);
1077 1077
 						    $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name;
1078 1078
 						}
1079 1079
 					}
1080 1080
 					else
1081 1081
 					{
1082
-						$this->add_rule_match($rule['id'],$rule['num_match']+1);
1082
+						$this->add_rule_match($rule['id'], $rule['num_match'] + 1);
1083 1083
 					}
1084
-					$this->trap_to_db=($action==-2)?false:true;
1084
+					$this->trap_to_db=($action == -2) ?false:true;
1085 1085
 				}
1086 1086
 				else
1087 1087
 				{
1088 1088
 					//$this->logging->log('rules KOO : '.print_r($rule),INFO );
1089 1089
 					
1090 1090
 					$action=$rule['action_nomatch'];
1091
-					$this->logging->log('action NOK : '.$action,INFO );
1091
+					$this->logging->log('action NOK : '.$action, INFO);
1092 1092
 					if ($action >= 0)
1093 1093
 					{
1094
-					    if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false)
1094
+					    if ($this->serviceCheckResult($host_name, $service_name, $action, $display) == false)
1095 1095
 					    {
1096 1096
 					        $this->trap_action.='Error sending status : check cmd/API';
1097 1097
 					    }
1098 1098
 					    else
1099 1099
 					    {
1100
-    						$this->add_rule_match($rule['id'],$rule['num_match']+1);
1100
+    						$this->add_rule_match($rule['id'], $rule['num_match'] + 1);
1101 1101
     						$this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name;
1102 1102
 					    }
1103 1103
 					}
1104 1104
 					else
1105 1105
 					{
1106
-						$this->add_rule_match($rule['id'],$rule['num_match']+1);
1106
+						$this->add_rule_match($rule['id'], $rule['num_match'] + 1);
1107 1107
 					}
1108
-					$this->trap_to_db=($action==-2)?false:true;					
1108
+					$this->trap_to_db=($action == -2) ?false:true;					
1109 1109
 				}
1110 1110
 				// Put name in source_name
1111 1111
 				if (!isset($this->trap_data['source_name']))
@@ -1114,7 +1114,7 @@  discard block
 block discarded – undo
1114 1114
 				}
1115 1115
 				else
1116 1116
 				{
1117
-					if (!preg_match('/'.$rule['host_name'].'/',$this->trap_data['source_name']))
1117
+					if (!preg_match('/'.$rule['host_name'].'/', $this->trap_data['source_name']))
1118 1118
 					{ // only add if not present
1119 1119
 						$this->trap_data['source_name'].=','.$rule['host_name'];
1120 1120
 					}
@@ -1122,13 +1122,13 @@  discard block
 block discarded – undo
1122 1122
 			}
1123 1123
 			catch (Exception $e) 
1124 1124
 			{ 
1125
-			    $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,'');
1125
+			    $this->logging->log('Error in rule eval : '.$e->getMessage(), WARN, '');
1126 1126
 			    $this->trap_action.=' ERR : '.$e->getMessage();
1127 1127
 			    $this->trap_data['status']='error';
1128 1128
 			}
1129 1129
 			
1130 1130
 		}
1131
-		if ($this->trap_data['status']=='error')
1131
+		if ($this->trap_data['status'] == 'error')
1132 1132
 		{
1133 1133
 		  $this->trap_to_db=true; // Always put errors in DB for the use can see
1134 1134
 		}
@@ -1144,13 +1144,13 @@  discard block
 block discarded – undo
1144 1144
 	public function add_rule_final($time)
1145 1145
 	{
1146 1146
 		$db_conn=$this->trapsDB->db_connect_trap();
1147
-		if ($this->trap_action==null) 
1147
+		if ($this->trap_action == null) 
1148 1148
 		{
1149 1149
 			$this->trap_action='No action';
1150 1150
 		}
1151 1151
 		$sql="UPDATE ".$this->db_prefix."received SET process_time = '".$time."' , status_detail='".$this->trap_action."'  WHERE (id = '".$this->trap_id."');";
1152 1152
 		if ($db_conn->query($sql) === false) {
1153
-			$this->logging->log('Error in update query : ' . $sql,WARN,'');
1153
+			$this->logging->log('Error in update query : '.$sql, WARN, '');
1154 1154
 		}
1155 1155
 	}
1156 1156
 	
@@ -1160,14 +1160,14 @@  discard block
 block discarded – undo
1160 1160
 	*	@param $schema_file	string File to read schema from
1161 1161
 	*	@param $table_prefix string to replace #PREFIX# in schema file by this
1162 1162
 	*/
1163
-	public function create_schema($schema_file,$table_prefix)
1163
+	public function create_schema($schema_file, $table_prefix)
1164 1164
 	{
1165 1165
 		//Read data from snmptrapd from stdin
1166 1166
 		$input_stream=fopen($schema_file, 'r');
1167 1167
 
1168
-		if ($input_stream=== false)
1168
+		if ($input_stream === false)
1169 1169
 		{
1170
-			$this->logging->log("Error reading schema !",ERROR,''); 
1170
+			$this->logging->log("Error reading schema !", ERROR, ''); 
1171 1171
 			return;
1172 1172
 		}
1173 1173
 		$newline='';
@@ -1177,14 +1177,14 @@  discard block
 block discarded – undo
1177 1177
 		
1178 1178
 		while (($line=fgets($input_stream)) !== false)
1179 1179
 		{
1180
-			$newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line));
1180
+			$newline.=chop(preg_replace('/#PREFIX#/', $table_prefix, $line));
1181 1181
 			if (preg_match('/; *$/', $newline)) 
1182 1182
             {
1183
-                $sql= $newline;
1183
+                $sql=$newline;
1184 1184
                 if ($db_conn->query($sql) === false) {
1185
-                    $this->logging->log('Error create schema : '.$sql,ERROR,'');
1185
+                    $this->logging->log('Error create schema : '.$sql, ERROR, '');
1186 1186
                 }
1187
-                if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array))
1187
+                if (preg_match('/^ *CREATE TABLE ([^ ]+)/', $newline, $cur_table_array))
1188 1188
                 {
1189 1189
                     $cur_table='table '.$cur_table_array[1];
1190 1190
                 }
@@ -1192,19 +1192,19 @@  discard block
 block discarded – undo
1192 1192
                 {
1193 1193
                     $cur_table='secret SQL stuff :-)';
1194 1194
                 }
1195
-                $this->logging->log('Creating : ' . $cur_table,INFO );
1195
+                $this->logging->log('Creating : '.$cur_table, INFO);
1196 1196
                 $newline='';
1197 1197
             }
1198 1198
 		}
1199 1199
 		
1200
-		$sql= $newline;
1200
+		$sql=$newline;
1201 1201
 		if ($sql != '')
1202 1202
 		{
1203 1203
     		if ($db_conn->query($sql) === false) {
1204
-    			$this->logging->log('Error create schema : '.$sql,ERROR,'');
1204
+    			$this->logging->log('Error create schema : '.$sql, ERROR, '');
1205 1205
     		}
1206 1206
 		}
1207
-		$this->logging->log('Schema created',INFO);		
1207
+		$this->logging->log('Schema created', INFO);		
1208 1208
 	}
1209 1209
 
1210 1210
 	/** 
@@ -1215,14 +1215,14 @@  discard block
 block discarded – undo
1215 1215
 	 *     @param bool $getmsg : only get messages from version upgrades
1216 1216
 	 *     @return string : if $getmsg=true, return messages.
1217 1217
 	 */
1218
-	public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false)
1218
+	public function update_schema($prefix, $target_version, $table_prefix, $getmsg=false)
1219 1219
 	{
1220 1220
 	    // Get current db number
1221 1221
 	    $db_conn=$this->trapsDB->db_connect_trap();
1222 1222
 	    $sql='SELECT id,value from '.$this->db_prefix.'db_config WHERE name=\'db_version\' ';
1223
-	    $this->logging->log('SQL query : '.$sql,DEBUG );
1223
+	    $this->logging->log('SQL query : '.$sql, DEBUG);
1224 1224
 	    if (($ret_code=$db_conn->query($sql)) === false) {
1225
-	        $this->logging->log('Cannot get db version. Query : ' . $sql,2,'');
1225
+	        $this->logging->log('Cannot get db version. Query : '.$sql, 2, '');
1226 1226
 	        return;
1227 1227
 	    }
1228 1228
 	    $version=$ret_code->fetchAll();
@@ -1231,47 +1231,47 @@  discard block
 block discarded – undo
1231 1231
 	    
1232 1232
 	    if ($this->trapsDB->trapDBType == 'pgsql')
1233 1233
 	    {
1234
-	        $prefix .= 'update_pgsql/schema_';
1234
+	        $prefix.='update_pgsql/schema_';
1235 1235
 	    }
1236 1236
 	    else
1237 1237
 	    {
1238
-	        $prefix .= 'update_sql/schema_';
1238
+	        $prefix.='update_sql/schema_';
1239 1239
 	    }
1240 1240
 	    //echo "version all :\n";print_r($version);echo " \n $cur_ver \n";
1241 1241
 	    if ($getmsg === true)
1242 1242
 	    {
1243 1243
 	        $message='';
1244
-	        $this->logging->log('getting message for upgrade',DEBUG );
1245
-	        while($cur_version<$target_version)
1244
+	        $this->logging->log('getting message for upgrade', DEBUG);
1245
+	        while ($cur_version < $target_version)
1246 1246
 	        {
1247 1247
 	            $cur_version++;
1248
-	            $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql';
1248
+	            $updateFile=$prefix.'v'.($cur_version - 1).'_v'.$cur_version.'.sql';
1249 1249
 	            $input_stream=fopen($updateFile, 'r');
1250
-	            if ($input_stream=== false)
1250
+	            if ($input_stream === false)
1251 1251
 	            {
1252
-	                $this->logging->log("Error reading update file ". $updateFile,2,'');
1252
+	                $this->logging->log("Error reading update file ".$updateFile, 2, '');
1253 1253
 	                return;
1254 1254
 	            }
1255 1255
 	            do { $line=fgets($input_stream); }
1256
-	            while ($line !== false && !preg_match('/#MESSAGE/',$line));
1256
+	            while ($line !== false && !preg_match('/#MESSAGE/', $line));
1257 1257
 	            if ($line === false)
1258 1258
 	            {
1259
-	                $this->logging->log("No message in file ". $updateFile,2,'');
1259
+	                $this->logging->log("No message in file ".$updateFile, 2, '');
1260 1260
 	                return;
1261 1261
 	            }
1262
-	            $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n";
1262
+	            $message.=($cur_version - 1).'->'.$cur_version.' : '.preg_replace('/#MESSAGE : /', '', $line)."\n";
1263 1263
 	        }
1264 1264
 	        return $message;
1265 1265
 	    }
1266
-	    while($cur_version<$target_version)
1266
+	    while ($cur_version < $target_version)
1267 1267
 	    { // tODO : execute pre & post scripts
1268 1268
 	       $cur_version++;
1269
-	       $this->logging->log('Updating to version : ' .$cur_version ,INFO );
1270
-	       $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql';
1269
+	       $this->logging->log('Updating to version : '.$cur_version, INFO);
1270
+	       $updateFile=$prefix.'v'.($cur_version - 1).'_v'.$cur_version.'.sql';
1271 1271
 	       $input_stream=fopen($updateFile, 'r');
1272
-	       if ($input_stream=== false)
1272
+	       if ($input_stream === false)
1273 1273
 	       {
1274
-	           $this->logging->log("Error reading update file ". $updateFile,2,'');
1274
+	           $this->logging->log("Error reading update file ".$updateFile, 2, '');
1275 1275
 	           return;
1276 1276
 	       }
1277 1277
 	       $newline='';
@@ -1280,24 +1280,24 @@  discard block
 block discarded – undo
1280 1280
 	       while (($line=fgets($input_stream)) !== false)
1281 1281
 	       {
1282 1282
 	           if (preg_match('/^#/', $line)) continue; // ignore comment lines
1283
-	           $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line));
1283
+	           $newline.=chop(preg_replace('/#PREFIX#/', $table_prefix, $line));
1284 1284
 	           if (preg_match('/; *$/', $newline))
1285 1285
 	           {
1286 1286
 	               $sql_req=$db_conn->prepare($newline);
1287 1287
 	               if ($sql_req->execute() === false) {
1288
-	                   $this->logging->log('Error create schema : '.$newline,1,'');
1288
+	                   $this->logging->log('Error create schema : '.$newline, 1, '');
1289 1289
 	               }
1290 1290
 	               $cur_table_array=array();
1291
-	               if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array))
1291
+	               if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/', $newline, $cur_table_array))
1292 1292
 	               {
1293
-	                   $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2];
1293
+	                   $cur_table=$cur_table_array[1].' SQL table '.$cur_table_array[2];
1294 1294
 	               }
1295 1295
 	               else
1296 1296
 	               {
1297 1297
 	                   $cur_table='secret SQL stuff :-)';
1298 1298
 	                   //$cur_table=$newline;
1299 1299
 	               }
1300
-	               $this->logging->log('Doing : ' . $cur_table,INFO );
1300
+	               $this->logging->log('Doing : '.$cur_table, INFO);
1301 1301
 	               
1302 1302
 	               $newline='';
1303 1303
 	           }
@@ -1310,13 +1310,13 @@  discard block
 block discarded – undo
1310 1310
 	       //}
1311 1311
 	       
1312 1312
 	       $sql='UPDATE '.$this->db_prefix.'db_config SET value='.$cur_version.' WHERE ( id = '.$db_version_id.' )';
1313
-	       $this->logging->log('SQL query : '.$sql,DEBUG );
1313
+	       $this->logging->log('SQL query : '.$sql, DEBUG);
1314 1314
 	       if ($db_conn->query($sql) === false) {
1315
-	           $this->logging->log('Cannot update db version. Query : ' . $sql,2);
1315
+	           $this->logging->log('Cannot update db version. Query : '.$sql, 2);
1316 1316
 	           return;
1317 1317
 	       }
1318 1318
 	       
1319
-	       $this->logging->log('Schema updated to version : '.$cur_version ,INFO);
1319
+	       $this->logging->log('Schema updated to version : '.$cur_version, INFO);
1320 1320
 	    }
1321 1321
 	}
1322 1322
 	
@@ -1337,7 +1337,7 @@  discard block
 block discarded – undo
1337 1337
     WHERE s.current_state != 0;";
1338 1338
 		$db_conn=$this->trapsDB->db_connect_ido();
1339 1339
 		if (($services_db=$db_conn->query($sql_query)) === false) { // set err to 1 to throw exception.
1340
-			$this->logging->log('No result in query : ' . $sql_query,ERROR,'');
1340
+			$this->logging->log('No result in query : '.$sql_query, ERROR, '');
1341 1341
 			return 0;
1342 1342
 		}
1343 1343
 		$services=$services_db->fetchAll();
@@ -1346,7 +1346,7 @@  discard block
 block discarded – undo
1346 1346
 		$sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->db_prefix."rules where revert_ok != 0;";
1347 1347
 		$db_conn2=$this->trapsDB->db_connect_trap();
1348 1348
 		if (($rules_db=$db_conn2->query($sql_query)) === false) {
1349
-			$this->logging->log('No result in query : ' . $sql_query,ERROR,'');
1349
+			$this->logging->log('No result in query : '.$sql_query, ERROR, '');
1350 1350
 			return 0;
1351 1351
 		}
1352 1352
 		$rules=$rules_db->fetchAll();
@@ -1362,13 +1362,13 @@  discard block
 block discarded – undo
1362 1362
 					$service['host_name'] == $rule['host_name'] &&
1363 1363
 					($service['last_check'] + $rule['revert_ok']) < $now)
1364 1364
 				{
1365
-					$this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds');
1365
+					$this->serviceCheckResult($service['host_name'], $service['service_name'], 0, 'Reset service to OK after '.$rule['revert_ok'].' seconds');
1366 1366
 					$numreset++;
1367 1367
 				}
1368 1368
 			}
1369 1369
 		}
1370 1370
 		echo "\n";
1371
-		echo $numreset . " service(s) reset to OK\n";
1371
+		echo $numreset." service(s) reset to OK\n";
1372 1372
 		return 0;
1373 1373
 		
1374 1374
 	}
@@ -1389,7 +1389,7 @@  discard block
 block discarded – undo
1389 1389
 	 * @param string $description
1390 1390
 	 * @return number : 0=unchanged, 1 = changed, 2=created
1391 1391
 	 */
1392
-	public function update_oid($oid,$mib,$name,$type,$textConv,$dispHint,$syntax,$type_enum,$description=NULL)
1392
+	public function update_oid($oid, $mib, $name, $type, $textConv, $dispHint, $syntax, $type_enum, $description=NULL)
1393 1393
 	{
1394 1394
 		$db_conn=$this->trapsDB->db_connect_trap();
1395 1395
 		$description=$db_conn->quote($description);
@@ -1399,7 +1399,7 @@  discard block
 block discarded – undo
1399 1399
 		    { // newly created.
1400 1400
 		        return 0;
1401 1401
 		    }
1402
-			if ( $name != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['name'] ||
1402
+			if ($name != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['name'] ||
1403 1403
 			    $mib != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['mib'] ||
1404 1404
 			    $type != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type'] //||
1405 1405
 			    //$textConv != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['textual_convention'] //||
@@ -1419,23 +1419,23 @@  discard block
 block discarded – undo
1419 1419
 			        ':name' => $name,
1420 1420
 			        ':type' => $type, 
1421 1421
 			        ':mib' => $mib, 
1422
-			        ':tc' =>  ($textConv==null)?'null':$textConv , 
1423
-			        ':display_hint' => ($dispHint==null)?'null':$dispHint ,
1424
-			        ':syntax' => ($syntax==null)?'null':$syntax,
1425
-			        ':type_enum' => ($type_enum==null)?'null':$type_enum, 
1426
-			        ':description' => ($description==null)?'null':$description,
1422
+			        ':tc' =>  ($textConv == null) ? 'null' : $textConv, 
1423
+			        ':display_hint' => ($dispHint == null) ? 'null' : $dispHint,
1424
+			        ':syntax' => ($syntax == null) ? 'null' : $syntax,
1425
+			        ':type_enum' => ($type_enum == null) ? 'null' : $type_enum, 
1426
+			        ':description' => ($description == null) ? 'null' : $description,
1427 1427
 			        ':id' => $this->dbOidAll[$this->dbOidIndex[$oid]['id']]
1428 1428
 			    );
1429 1429
 			    
1430 1430
 			    if ($sqlQuery->execute($sqlParam) === false) {
1431
-			        $this->logging->log('Error in query : ' . $sql,ERROR,'');
1431
+			        $this->logging->log('Error in query : '.$sql, ERROR, '');
1432 1432
 			    }
1433
-			    $this->logging->log('Trap updated : '.$name . ' / OID : '.$oid,DEBUG );
1433
+			    $this->logging->log('Trap updated : '.$name.' / OID : '.$oid, DEBUG);
1434 1434
 				return 1;
1435 1435
 			}
1436 1436
 			else
1437 1437
 			{
1438
-			    $this->logging->log('Trap unchanged : '.$name . ' / OID : '.$oid,DEBUG );
1438
+			    $this->logging->log('Trap unchanged : '.$name.' / OID : '.$oid, DEBUG);
1439 1439
 			    return 0;
1440 1440
 			}
1441 1441
 		}
@@ -1445,11 +1445,11 @@  discard block
 block discarded – undo
1445 1445
 
1446 1446
 		$sql='INSERT INTO '.$this->db_prefix.'mib_cache '.
1447 1447
 		      '(oid, name, type , mib, textual_convention, display_hint '.
1448
-              ', syntax, type_enum , description ) ' . 
1448
+              ', syntax, type_enum , description ) '. 
1449 1449
               'values (:oid, :name , :type ,:mib ,:tc , :display_hint'.
1450 1450
               ', :syntax, :type_enum, :description )';
1451 1451
         
1452
-		if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id';
1452
+		if ($this->trapsDB->trapDBType == 'pgsql') $sql.='RETURNING id';
1453 1453
 		
1454 1454
 		$sqlQuery=$db_conn->prepare($sql);
1455 1455
 		
@@ -1458,15 +1458,15 @@  discard block
 block discarded – undo
1458 1458
 		    ':name' => $name,
1459 1459
 		    ':type' => $type,
1460 1460
 		    ':mib' => $mib,
1461
-		    ':tc' =>  ($textConv==null)?'null':$textConv ,
1462
-		    ':display_hint' => ($dispHint==null)?'null':$dispHint ,
1463
-		    ':syntax' => ($syntax==null)?'null':$syntax,
1464
-		    ':type_enum' => ($type_enum==null)?'null':$type_enum,
1465
-		    ':description' => ($description==null)?'null':$description
1461
+		    ':tc' =>  ($textConv == null) ? 'null' : $textConv,
1462
+		    ':display_hint' => ($dispHint == null) ? 'null' : $dispHint,
1463
+		    ':syntax' => ($syntax == null) ? 'null' : $syntax,
1464
+		    ':type_enum' => ($type_enum == null) ? 'null' : $type_enum,
1465
+		    ':description' => ($description == null) ? 'null' : $description
1466 1466
 		);
1467 1467
 		
1468 1468
 		if ($sqlQuery->execute($sqlParam) === false) {
1469
-		    $this->logging->log('Error in query : ' . $sql,1,'');
1469
+		    $this->logging->log('Error in query : '.$sql, 1, '');
1470 1470
 		}
1471 1471
 		
1472 1472
 		switch ($this->trapsDB->trapDBType)
@@ -1474,10 +1474,10 @@  discard block
 block discarded – undo
1474 1474
 		    case 'pgsql':
1475 1475
 		        // Get last id to insert oid/values in secondary table
1476 1476
 		        if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) {		            
1477
-		            $this->logging->log('Error getting id - pgsql - ',1,'');
1477
+		            $this->logging->log('Error getting id - pgsql - ', 1, '');
1478 1478
 		        }
1479
-		        if (! isset($inserted_id_ret['id'])) {
1480
-		            $this->logging->log('Error getting id - pgsql - empty.',1,'');
1479
+		        if (!isset($inserted_id_ret['id'])) {
1480
+		            $this->logging->log('Error getting id - pgsql - empty.', 1, '');
1481 1481
 		        }
1482 1482
 		        $this->dbOidIndex[$oid]['id']=$inserted_id_ret['id'];
1483 1483
 		        break;
@@ -1485,15 +1485,15 @@  discard block
 block discarded – undo
1485 1485
 		        // Get last id to insert oid/values in secondary table
1486 1486
 		        $sql='SELECT LAST_INSERT_ID();';
1487 1487
 		        if (($ret_code=$db_conn->query($sql)) === false) {
1488
-		            $this->logging->log('Erreur getting id - mysql - ',1,'');
1488
+		            $this->logging->log('Erreur getting id - mysql - ', 1, '');
1489 1489
 		        }
1490 1490
 		        
1491 1491
 		        $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");
1492
+		        if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
1493 1493
 		        $this->dbOidIndex[$oid]['id']=$inserted_id;
1494 1494
 		        break;
1495 1495
 		    default:
1496
-		        $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,1,'');
1496
+		        $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType, 1, '');
1497 1497
 		}
1498 1498
 
1499 1499
 		// Set as newly created.
@@ -1508,21 +1508,21 @@  discard block
 block discarded – undo
1508 1508
      * @param array $objects : array of objects name (without MIB)
1509 1509
      * @param bool $check_existing : check instead of create
1510 1510
      */
1511
-	public function trap_objects($trapOID,$trapmib,$objects,$check_existing)
1511
+	public function trap_objects($trapOID, $trapmib, $objects, $check_existing)
1512 1512
 	{
1513 1513
 	    $dbObjects=null; // cache of objects for trap in db
1514 1514
 	    $db_conn=$this->trapsDB->db_connect_trap();
1515 1515
 	    
1516 1516
 	    // Get id of trapmib.
1517 1517
 
1518
-	    $trapId = $this->dbOidIndex[$trapOID]['id'];
1518
+	    $trapId=$this->dbOidIndex[$trapOID]['id'];
1519 1519
 	    if ($check_existing === true)
1520 1520
 	    {
1521 1521
 	        // Get all objects
1522 1522
 	        $sql='SELECT * FROM '.$this->db_prefix.'mib_cache_trap_object where trap_id='.$trapId.';';
1523
-	        $this->logging->log('SQL query get all traps: '.$sql,DEBUG );
1523
+	        $this->logging->log('SQL query get all traps: '.$sql, DEBUG);
1524 1524
 	        if (($ret_code=$db_conn->query($sql)) === false) {
1525
-	            $this->logging->log('No result in query : ' . $sql,1,'');
1525
+	            $this->logging->log('No result in query : '.$sql, 1, '');
1526 1526
 	        }
1527 1527
 	        $dbObjectsRaw=$ret_code->fetchAll();
1528 1528
 	        
@@ -1536,39 +1536,39 @@  discard block
 block discarded – undo
1536 1536
 	        $match=$snmptrans=array();
1537 1537
 	        $retVal=0;
1538 1538
 	        $objOid=$objTc=$objDispHint=$objSyntax=$objDesc=$objEnum=NULL;
1539
-	        $tmpdesc='';$indesc=false;
1539
+	        $tmpdesc=''; $indesc=false;
1540 1540
 	        
1541 1541
 	        $objMib=$trapmib;
1542
-	        exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.
1543
-	            ' -On -Td '.$objMib.'::'.$object . ' 2>/dev/null',$snmptrans,$retVal);
1544
-	        if ($retVal!=0)
1542
+	        exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs.
1543
+	            ' -On -Td '.$objMib.'::'.$object.' 2>/dev/null', $snmptrans, $retVal);
1544
+	        if ($retVal != 0)
1545 1545
 	        {
1546 1546
 	            // Maybe not trap mib, search with IR
1547
-	            exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.
1548
-	                ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal);
1549
-	            if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match))
1547
+	            exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs.
1548
+	                ' -IR '.$object.' 2>/dev/null', $snmptrans, $retVal);
1549
+	            if ($retVal != 0 || !preg_match('/(.*)::(.*)/', $snmptrans[0], $match))
1550 1550
 	            { // Not found -> continue with warning
1551
-	               $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,'');
1551
+	               $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object, 2, '');
1552 1552
 	               continue;
1553 1553
 	            }
1554 1554
 	            $objMib=$match[1];
1555 1555
 	            
1556 1556
 	            // Do the snmptranslate again.
1557
-	            exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.
1558
-	                ' -On -Td '.$objMib.'::'.$object,$snmptrans,$retVal);
1559
-	            if ($retVal!=0) {
1560
-	                $this->logging->log('Error finding trap object : '.$objMib.'::'.$object,2,'');
1557
+	            exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs.
1558
+	                ' -On -Td '.$objMib.'::'.$object, $snmptrans, $retVal);
1559
+	            if ($retVal != 0) {
1560
+	                $this->logging->log('Error finding trap object : '.$objMib.'::'.$object, 2, '');
1561 1561
 	            }
1562 1562
 	            
1563 1563
 	        }
1564 1564
 	        foreach ($snmptrans as $line)
1565 1565
 	        {
1566
-	            if ($indesc===true)
1566
+	            if ($indesc === true)
1567 1567
 	            {
1568
-	                $line=preg_replace('/[\t ]+/',' ',$line);
1569
-	                if (preg_match('/(.*)"$/', $line,$match))
1568
+	                $line=preg_replace('/[\t ]+/', ' ', $line);
1569
+	                if (preg_match('/(.*)"$/', $line, $match))
1570 1570
 	                {
1571
-	                    $objDesc = $tmpdesc . $match[1];
1571
+	                    $objDesc=$tmpdesc.$match[1];
1572 1572
 	                    $indesc=false;
1573 1573
 	                }
1574 1574
 	                $tmpdesc.=$line;
@@ -1579,43 +1579,43 @@  discard block
 block discarded – undo
1579 1579
 	                $objOid=$line;
1580 1580
 	                continue;
1581 1581
 	            }
1582
-	            if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match))
1582
+	            if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/', $line, $match))
1583 1583
 	            {
1584 1584
 	                $objSyntax=$match[1];
1585 1585
                     $objEnum=$match[2];
1586 1586
 	                continue;
1587 1587
 	            }
1588
-	            if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match))
1588
+	            if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/', $line, $match))
1589 1589
 	            {
1590 1590
 	                $objSyntax=$match[1];
1591 1591
 	                continue;
1592 1592
 	            }
1593
-	            if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match))
1593
+	            if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/', $line, $match))
1594 1594
 	            {
1595 1595
 	                $objDispHint=$match[1];
1596 1596
 	                continue;
1597 1597
 	            }
1598
-	            if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match))
1598
+	            if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/', $line, $match))
1599 1599
 	            {
1600 1600
 	                $objDesc=$match[1];
1601 1601
 	                continue;
1602 1602
 	            }
1603
-	            if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match))
1603
+	            if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/', $line, $match))
1604 1604
 	            {
1605 1605
 	                $tmpdesc=$match[1];
1606 1606
 	                $indesc=true;
1607 1607
 	                continue;
1608 1608
 	            }
1609
-	            if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match))
1609
+	            if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/', $line, $match))
1610 1610
 	            {
1611 1611
 	                $objTc=$match[1];
1612 1612
 	                continue;
1613 1613
 	            }
1614 1614
 	        }
1615
-	        $this->logging->log("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc",DEBUG );
1615
+	        $this->logging->log("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc", DEBUG);
1616 1616
 	        //echo "$object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc / $objDesc\n";
1617 1617
 	        // Update 
1618
-	        $this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum,$objDesc);
1618
+	        $this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum, $objDesc);
1619 1619
             
1620 1620
 	        if (isset($dbObjects[$this->dbOidIndex[$objOid]['id']]))
1621 1621
 	        {   // if link exists, continue
@@ -1636,7 +1636,7 @@  discard block
 block discarded – undo
1636 1636
 	        );
1637 1637
 	        
1638 1638
 	        if ($sqlQuery->execute($sqlParam) === false) {
1639
-	            $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$objOid]['id'] ,1,'');
1639
+	            $this->logging->log('Error adding trap object : '.$sql.' / '.$trapId.'/'.$this->dbOidIndex[$objOid]['id'], 1, '');
1640 1640
 	        }
1641 1641
 	    }
1642 1642
 	    if ($check_existing === true)
@@ -1653,19 +1653,19 @@  discard block
 block discarded – undo
1653 1653
 	 * @param boolean $onlyTraps : only cache traps and objects (true) or all (false)
1654 1654
 	 * @param string $startOID : only cache under startOID (NOT IMPLEMENTED)
1655 1655
 	*/	
1656
-	public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1')
1656
+	public function update_mib_database($display_progress=false, $check_change=false, $onlyTraps=true, $startOID='.1')
1657 1657
 	{
1658 1658
 		// Timing 
1659
-		$timeTaken = microtime(true);
1659
+		$timeTaken=microtime(true);
1660 1660
 		$retVal=0;
1661 1661
 		// Get all mib objects from all mibs
1662
-		$snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.' -On -Tto 2>/dev/null';
1663
-		$this->logging->log('Getting all traps : '.$snmpCommand,DEBUG );
1662
+		$snmpCommand=$this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs.' -On -Tto 2>/dev/null';
1663
+		$this->logging->log('Getting all traps : '.$snmpCommand, DEBUG);
1664 1664
 		unset($this->objectsAll);
1665
-		exec($snmpCommand,$this->objectsAll,$retVal);		
1666
-		if ($retVal!=0)
1665
+		exec($snmpCommand, $this->objectsAll, $retVal);		
1666
+		if ($retVal != 0)
1667 1667
 		{
1668
-			$this->logging->log('error executing snmptranslate',ERROR,'');
1668
+			$this->logging->log('error executing snmptranslate', ERROR, '');
1669 1669
 		}
1670 1670
 		
1671 1671
 		// Get all mibs from databse to have a memory index
@@ -1673,14 +1673,14 @@  discard block
 block discarded – undo
1673 1673
 		$db_conn=$this->trapsDB->db_connect_trap();
1674 1674
 		
1675 1675
 		$sql='SELECT * from '.$this->db_prefix.'mib_cache;';
1676
-		$this->logging->log('SQL query : '.$sql,DEBUG );
1676
+		$this->logging->log('SQL query : '.$sql, DEBUG);
1677 1677
 		if (($ret_code=$db_conn->query($sql)) === false) {
1678
-			$this->logging->log('No result in query : ' . $sql,ERROR,'');
1678
+			$this->logging->log('No result in query : '.$sql, ERROR, '');
1679 1679
 		}
1680 1680
 		$this->dbOidAll=$ret_code->fetchAll();
1681 1681
 		$this->dbOidIndex=array();
1682 1682
 		// Create the index for db;
1683
-		foreach($this->dbOidAll as $key=>$val)
1683
+		foreach ($this->dbOidAll as $key=>$val)
1684 1684
 		{
1685 1685
 			$this->dbOidIndex[$val['oid']]['key']=$key;
1686 1686
 			$this->dbOidIndex[$val['oid']]['id']=$val['id'];
@@ -1688,11 +1688,11 @@  discard block
 block discarded – undo
1688 1688
 		
1689 1689
 		// Count elements to show progress
1690 1690
 		$numElements=count($this->objectsAll);
1691
-		$this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO );
1691
+		$this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements, INFO);
1692 1692
 		
1693
-		$step=$basestep=$numElements/10; // output display of % done
1693
+		$step=$basestep=$numElements / 10; // output display of % done
1694 1694
 		$num_step=0;
1695
-		$timeFiveSec = microtime(true); // Used for display a '.' every <n> seconds
1695
+		$timeFiveSec=microtime(true); // Used for display a '.' every <n> seconds
1696 1696
 		
1697 1697
 		// Create index for trap objects
1698 1698
 		$this->trapObjectsIndex=array();
@@ -1702,28 +1702,28 @@  discard block
 block discarded – undo
1702 1702
 		$time_parse1N=$time_check1N=$time_check2N=$time_check3N=$time_updateN=$time_objectsN=0;
1703 1703
 		$time_num_traps=0;
1704 1704
 		
1705
-		for ($curElement=0;$curElement < $numElements;$curElement++)
1705
+		for ($curElement=0; $curElement < $numElements; $curElement++)
1706 1706
 		{
1707
-		    $time_1= microtime(true);
1708
-			if ((microtime(true)-$timeFiveSec) > 2 && $display_progress)
1707
+		    $time_1=microtime(true);
1708
+			if ((microtime(true) - $timeFiveSec) > 2 && $display_progress)
1709 1709
 			{ // echo a . every 2 sec
1710 1710
 				echo '.';
1711
-				$timeFiveSec = microtime(true);
1711
+				$timeFiveSec=microtime(true);
1712 1712
 			}
1713
-			if ($curElement>$step) 
1713
+			if ($curElement > $step) 
1714 1714
 			{ // display progress
1715 1715
 				$num_step++;
1716 1716
 				$step+=$basestep;
1717 1717
 				if ($display_progress)
1718 1718
 				{				
1719
-					echo "\n" . ($num_step*10). '% : ';
1719
+					echo "\n".($num_step * 10).'% : ';
1720 1720
 				}
1721 1721
 			}
1722 1722
 			// Get oid or pass if not found
1723
-			if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement]))
1723
+			if (!preg_match('/^\.[0-9\.]+$/', $this->objectsAll[$curElement]))
1724 1724
 			{
1725
-			    $time_parse1 += microtime(true) - $time_1;
1726
-			    $time_parse1N ++;
1725
+			    $time_parse1+=microtime(true) - $time_1;
1726
+			    $time_parse1N++;
1727 1727
 				continue;
1728 1728
 			}
1729 1729
 			$oid=$this->objectsAll[$curElement];
@@ -1732,9 +1732,9 @@  discard block
 block discarded – undo
1732 1732
 			$curElement++;
1733 1733
 			$match=$snmptrans=array();
1734 1734
 			if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/',
1735
-						$this->objectsAll[$curElement],$match))
1735
+						$this->objectsAll[$curElement], $match))
1736 1736
 			{
1737
-			    $time_check1 += microtime(true) - $time_1;
1737
+			    $time_check1+=microtime(true) - $time_1;
1738 1738
 				$time_check1N++;
1739 1739
 				continue;
1740 1740
 			}
@@ -1742,21 +1742,21 @@  discard block
 block discarded – undo
1742 1742
 			$name=$match[1]; // Name 
1743 1743
 			$type=$match[2]; // type (21=trap, 0: may be trap, else : not trap
1744 1744
 			
1745
-			if ($type==0) // object type=0 : check if v1 trap
1745
+			if ($type == 0) // object type=0 : check if v1 trap
1746 1746
 			{
1747 1747
 				// Check if next is suboid -> in that case is cannot be a trap
1748
-				if (preg_match("/^$oid/",$this->objectsAll[$curElement+1]))
1748
+				if (preg_match("/^$oid/", $this->objectsAll[$curElement + 1]))
1749 1749
 				{
1750
-				    $time_check2 += microtime(true) - $time_1;
1750
+				    $time_check2+=microtime(true) - $time_1;
1751 1751
 				    $time_check2N++;
1752 1752
 					continue;
1753 1753
 				}		
1754 1754
 				unset($snmptrans);
1755
-				exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.
1756
-					' -Td '.$oid . ' | grep OBJECTS ',$snmptrans,$retVal);
1757
-				if ($retVal!=0)
1755
+				exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs.
1756
+					' -Td '.$oid.' | grep OBJECTS ', $snmptrans, $retVal);
1757
+				if ($retVal != 0)
1758 1758
 				{
1759
-				    $time_check2 += microtime(true) - $time_1;
1759
+				    $time_check2+=microtime(true) - $time_1;
1760 1760
 				    $time_check2N++;
1761 1761
 					continue;
1762 1762
 				}
@@ -1764,56 +1764,56 @@  discard block
 block discarded – undo
1764 1764
 				// Force as trap.
1765 1765
 				$type=21;
1766 1766
 			}
1767
-			if ($onlyTraps===true && $type!=21) // if only traps and not a trap, continue
1767
+			if ($onlyTraps === true && $type != 21) // if only traps and not a trap, continue
1768 1768
 			{
1769
-			    $time_check3 += microtime(true) - $time_1;
1769
+			    $time_check3+=microtime(true) - $time_1;
1770 1770
 				$time_check3N++;
1771 1771
 				continue;
1772 1772
 			}
1773 1773
 			
1774 1774
 			$time_num_traps++;
1775 1775
 			
1776
-			$this->logging->log('Found trap : '.$match[1] . ' / OID : '.$oid,INFO );
1776
+			$this->logging->log('Found trap : '.$match[1].' / OID : '.$oid, INFO);
1777 1777
 			if ($display_progress) echo '#'; // echo a # when trap found
1778 1778
 				
1779 1779
 			// get trap objects & source MIB
1780 1780
 			unset($snmptrans);
1781
-			exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.
1782
-					' -Td '.$oid,$snmptrans,$retVal);
1783
-			if ($retVal!=0)
1781
+			exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs.
1782
+					' -Td '.$oid, $snmptrans, $retVal);
1783
+			if ($retVal != 0)
1784 1784
 			{
1785
-				$this->logging->log('error executing snmptranslate',ERROR,'');
1785
+				$this->logging->log('error executing snmptranslate', ERROR, '');
1786 1786
 			}
1787 1787
 			
1788
-			if (!preg_match('/^(.*)::/',$snmptrans[0],$match))
1788
+			if (!preg_match('/^(.*)::/', $snmptrans[0], $match))
1789 1789
 			{
1790
-			    $this->logging->log('Error getting mib from trap '.$oid.' : ' . $snmptrans[0],1,'');
1790
+			    $this->logging->log('Error getting mib from trap '.$oid.' : '.$snmptrans[0], 1, '');
1791 1791
 			}
1792 1792
 			$trapMib=$match[1];
1793 1793
 			
1794
-			$numLine=1;$trapDesc='';
1795
-			while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++;
1794
+			$numLine=1; $trapDesc='';
1795
+			while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/', $snmptrans[$numLine], $match)) $numLine++;
1796 1796
 			if (isset($snmptrans[$numLine]))
1797 1797
 			{
1798
-			    $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]);
1798
+			    $snmptrans[$numLine]=preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/', '', $snmptrans[$numLine]);
1799 1799
 
1800
-			    while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine]))
1800
+			    while (isset($snmptrans[$numLine]) && !preg_match('/"/', $snmptrans[$numLine]))
1801 1801
 			    {
1802
-			        $trapDesc.=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]);
1802
+			        $trapDesc.=preg_replace('/[\t ]+/', ' ', $snmptrans[$numLine]);
1803 1803
 			        $numLine++;
1804 1804
 			    }
1805 1805
 			    if (isset($snmptrans[$numLine])) {
1806
-			        $trapDesc.=preg_replace('/".*/','',$snmptrans[$numLine]);
1807
-			        $trapDesc=preg_replace('/[\t ]+/',' ',$trapDesc);
1806
+			        $trapDesc.=preg_replace('/".*/', '', $snmptrans[$numLine]);
1807
+			        $trapDesc=preg_replace('/[\t ]+/', ' ', $trapDesc);
1808 1808
 			    }
1809 1809
 
1810 1810
 			}
1811
-			$update=$this->update_oid($oid,$trapMib,$name,$type,NULL,NULL,NULL,NULL,$trapDesc);
1812
-			$time_update += microtime(true) - $time_1; $time_1= microtime(true);
1811
+			$update=$this->update_oid($oid, $trapMib, $name, $type, NULL, NULL, NULL, NULL, $trapDesc);
1812
+			$time_update+=microtime(true) - $time_1; $time_1=microtime(true);
1813 1813
 			
1814
-			if (($update==0) && ($check_change===false))
1814
+			if (($update == 0) && ($check_change === false))
1815 1815
 			{ // Trapd didn't change & force check disabled
1816
-			    $time_objects += microtime(true) - $time_1;
1816
+			    $time_objects+=microtime(true) - $time_1;
1817 1817
 			    if ($display_progress) echo "C";
1818 1818
 			    continue;
1819 1819
 			}
@@ -1821,7 +1821,7 @@  discard block
 block discarded – undo
1821 1821
 			$synt=null;
1822 1822
 			foreach ($snmptrans as $line)
1823 1823
 			{	
1824
-    			if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match))
1824
+    			if (preg_match('/OBJECTS.*\{([^\}]+)\}/', $line, $match))
1825 1825
     				{
1826 1826
     					$synt=$match[1];
1827 1827
     				}
@@ -1829,30 +1829,30 @@  discard block
 block discarded – undo
1829 1829
 			if ($synt == null) 
1830 1830
 			{
1831 1831
 				//echo "No objects for $trapOID\n";
1832
-			    $time_objects += microtime(true) - $time_1;
1832
+			    $time_objects+=microtime(true) - $time_1;
1833 1833
 				continue;
1834 1834
 			}
1835 1835
 			//echo "$synt \n";
1836 1836
 			$trapObjects=array();
1837
-			while (preg_match('/ *([^ ,]+) *,* */',$synt,$match))
1837
+			while (preg_match('/ *([^ ,]+) *,* */', $synt, $match))
1838 1838
 			{
1839
-				array_push($trapObjects,$match[1]);
1840
-				$synt=preg_replace('/'.$match[0].'/','',$synt);
1839
+				array_push($trapObjects, $match[1]);
1840
+				$synt=preg_replace('/'.$match[0].'/', '', $synt);
1841 1841
 			}
1842 1842
 			
1843 1843
 			$this->trap_objects($oid, $trapMib, $trapObjects, false);
1844 1844
 			
1845
-			$time_objects += microtime(true) - $time_1;
1845
+			$time_objects+=microtime(true) - $time_1;
1846 1846
 			$time_objectsN++;
1847 1847
 		}
1848 1848
 		
1849 1849
 		if ($display_progress)
1850 1850
 		{
1851 1851
     		echo "\nNumber of processed traps : $time_num_traps \n";
1852
-    		echo "\nParsing : " . number_format($time_parse1+$time_check1,1) ." sec / " . ($time_parse1N+ $time_check1N)  . " occurences\n";
1853
-    		echo "Detecting traps : " . number_format($time_check2+$time_check3,1) . " sec / " . ($time_check2N+$time_check3N) ." occurences\n";
1854
-    		echo "Trap processing ($time_updateN): ".number_format($time_update,1)." sec , ";
1855
-    		echo "Objects processing ($time_objectsN) : ".number_format($time_objects,1)." sec \n";
1852
+    		echo "\nParsing : ".number_format($time_parse1 + $time_check1, 1)." sec / ".($time_parse1N + $time_check1N)." occurences\n";
1853
+    		echo "Detecting traps : ".number_format($time_check2 + $time_check3, 1)." sec / ".($time_check2N + $time_check3N)." occurences\n";
1854
+    		echo "Trap processing ($time_updateN): ".number_format($time_update, 1)." sec , ";
1855
+    		echo "Objects processing ($time_objectsN) : ".number_format($time_objects, 1)." sec \n";
1856 1856
 		}
1857 1857
 		
1858 1858
 		// Timing ends
Please login to merge, or discard this patch.
library/Trapdirector/TrapsController.php 1 patch
Spacing   +112 added lines, -112 removed lines patch added patch discarded remove patch
@@ -22,14 +22,14 @@  discard block
 block discarded – undo
22 22
 
23 23
 class TrapsController extends Controller
24 24
 {
25
-	protected $moduleConfig;  	//< TrapModuleConfig instance
26
-	protected $trapTableList; 	//< TrapTableList (by date)
27
-	protected $trapTableHostList; 	//< TrapTableList (by hosts)
28
-	protected $handlerTableList; 	//< HandlerTableList instance
29
-	protected $trapDB;			//< Trap database
30
-	protected $icingaDB;		//< Icinga IDO database;
31
-	protected $MIBData; 		//< MIBLoader class
32
-	protected $trapClass;		//< Trap class for bin/trap_class.php
25
+	protected $moduleConfig; //< TrapModuleConfig instance
26
+	protected $trapTableList; //< TrapTableList (by date)
27
+	protected $trapTableHostList; //< TrapTableList (by hosts)
28
+	protected $handlerTableList; //< HandlerTableList instance
29
+	protected $trapDB; //< Trap database
30
+	protected $icingaDB; //< Icinga IDO database;
31
+	protected $MIBData; //< MIBLoader class
32
+	protected $trapClass; //< Trap class for bin/trap_class.php
33 33
 		
34 34
 	/** Get instance of TrapModuleConfig class
35 35
 	*	@return TrapModuleConfig
@@ -43,14 +43,14 @@  discard block
 block discarded – undo
43 43
 			{
44 44
 				$this->redirectNow('trapdirector/settings?message=No database prefix');
45 45
 			}
46
-			$this->moduleConfig = new TrapModuleConfig($db_prefix);
46
+			$this->moduleConfig=new TrapModuleConfig($db_prefix);
47 47
 		}
48 48
 		return $this->moduleConfig;
49 49
 	}
50 50
 	
51 51
 	public function getTrapListTable() {
52 52
 		if ($this->trapTableList == Null) {
53
-			$this->trapTableList = new TrapTableList();
53
+			$this->trapTableList=new TrapTableList();
54 54
 			$this->trapTableList->setConfig($this->getModuleConfig());
55 55
 		}
56 56
 		return $this->trapTableList;
@@ -60,7 +60,7 @@  discard block
 block discarded – undo
60 60
 	{
61 61
 	    if ($this->trapTableHostList == Null) 
62 62
 		{
63
-	        $this->trapTableHostList = new TrapTableHostList();
63
+	        $this->trapTableHostList=new TrapTableHostList();
64 64
 	        $this->trapTableHostList->setConfig($this->getModuleConfig());
65 65
 	    }
66 66
 	    return $this->trapTableHostList;
@@ -70,7 +70,7 @@  discard block
 block discarded – undo
70 70
 	{
71 71
 		if ($this->handlerTableList == Null) 
72 72
 		{
73
-			$this->handlerTableList = new HandlerTableList();
73
+			$this->handlerTableList=new HandlerTableList();
74 74
 			$this->handlerTableList->setConfig($this->getModuleConfig());
75 75
 		}
76 76
 		return $this->handlerTableList;
@@ -82,15 +82,15 @@  discard block
 block discarded – undo
82 82
 	*	@param $test_version bool if set to flase, does not test database version of trapDB
83 83
 	*	@return array<integer,mixed>|mixed : if test=false, returns DB connexion, else array(error_num,message) or null on error.
84 84
 	*/
85
-	public function getDbByName($DBname,$test=false,$test_version=true)
85
+	public function getDbByName($DBname, $test=false, $test_version=true)
86 86
 	{
87 87
 		try 
88 88
 		{
89
-			$dbconn = IcingaDbConnection::fromResourceName($DBname);
89
+			$dbconn=IcingaDbConnection::fromResourceName($DBname);
90 90
 		} 
91 91
 		catch (Exception $e)
92 92
 		{
93
-			if ($test) return array(2,$DBname);
93
+			if ($test) return array(2, $DBname);
94 94
 			$this->redirectNow('trapdirector/settings?dberror=2');
95 95
 			return null;
96 96
 		}
@@ -101,37 +101,37 @@  discard block
 block discarded – undo
101 101
 			}
102 102
 			catch (Exception $e) 
103 103
 			{
104
-				if ($test) return array(3,$DBname,$e->getMessage());
104
+				if ($test) return array(3, $DBname, $e->getMessage());
105 105
 				$this->redirectNow('trapdirector/settings?dberror=3');
106 106
 				return null;
107 107
 			}
108 108
 			try
109 109
 			{
110
-				$query = $db->select()
111
-					->from($this->getModuleConfig()->getDbConfigTableName(),'value')
110
+				$query=$db->select()
111
+					->from($this->getModuleConfig()->getDbConfigTableName(), 'value')
112 112
 					->where('name=\'db_version\'');
113 113
 				$version=$db->fetchRow($query);
114
-				if ( ($version == null) || ! property_exists($version,'value') )
114
+				if (($version == null) || !property_exists($version, 'value'))
115 115
 				{
116
-					if ($test) return array(4,$DBname);
116
+					if ($test) return array(4, $DBname);
117 117
 					$this->redirectNow('trapdirector/settings?dberror=4');
118 118
 					return null;
119 119
 				}
120 120
 				if ($version->value < $this->getModuleConfig()->getDbMinVersion()) 
121 121
 				{
122
-					if ($test) return array(5,$version->value,$this->getModuleConfig()->getDbMinVersion());
122
+					if ($test) return array(5, $version->value, $this->getModuleConfig()->getDbMinVersion());
123 123
 					$this->redirectNow('trapdirector/settings?dberror=5');
124 124
 					return null;
125 125
 				}
126 126
 			}
127 127
 			catch (Exception $e) 
128 128
 			{
129
-				if ($test) return array(3,$DBname,$e->getMessage());
129
+				if ($test) return array(3, $DBname, $e->getMessage());
130 130
 				$this->redirectNow('trapdirector/settings?dberror=4');
131 131
 				return null;
132 132
 			}
133 133
 		}
134
-		if ($test) return array(0,'');
134
+		if ($test) return array(0, '');
135 135
 		return $dbconn;
136 136
 	}
137 137
 
@@ -142,17 +142,17 @@  discard block
 block discarded – undo
142 142
 	 */
143 143
 	public function getDb($test=false)
144 144
 	{
145
-		if ($this->trapDB != null && $test = false) return $this->trapDB;
145
+		if ($this->trapDB != null && $test=false) return $this->trapDB;
146 146
 		
147 147
 		$dbresource=$this->Config()->get('config', 'database');
148 148
 		
149
-		if ( ! $dbresource )
149
+		if (!$dbresource)
150 150
 		{	
151
-			if ($test) return array(1,'');
151
+			if ($test) return array(1, '');
152 152
 			$this->redirectNow('trapdirector/settings?dberror=1');
153 153
 			return null;
154 154
 		}
155
-		$retDB=$this->getDbByName($dbresource,$test,true);
155
+		$retDB=$this->getDbByName($dbresource, $test, true);
156 156
 		if ($test == true) return $retDB;
157 157
 		$this->trapDB=$retDB;
158 158
 		return $this->trapDB;
@@ -160,78 +160,78 @@  discard block
 block discarded – undo
160 160
 	
161 161
 	public function getIdoDb($test=false)
162 162
 	{
163
-		if ($this->icingaDB != null && $test = false) return $this->icingaDB;
163
+		if ($this->icingaDB != null && $test=false) return $this->icingaDB;
164 164
 		// TODO : get ido database directly from icingaweb2 config -> (or not if using only API)
165
-		$dbresource=$this->Config()->get('config', 'IDOdatabase');;
165
+		$dbresource=$this->Config()->get('config', 'IDOdatabase'); ;
166 166
 
167
-		if ( ! $dbresource )
167
+		if (!$dbresource)
168 168
 		{
169
-		    if ($test) return array(1,'No database in config.ini');
169
+		    if ($test) return array(1, 'No database in config.ini');
170 170
 		    $this->redirectNow('trapdirector/settings?idodberror=1');
171 171
 		    return null;
172 172
 		}
173 173
 		
174 174
 		try
175 175
 		{
176
-		    $dbconn = IcingaDbConnection::fromResourceName($dbresource);
176
+		    $dbconn=IcingaDbConnection::fromResourceName($dbresource);
177 177
 		}
178 178
 		catch (Exception $e)
179 179
 		{
180
-		    if ($test) return array(2,"Database $dbresource does not exists in IcingaWeb2");
180
+		    if ($test) return array(2, "Database $dbresource does not exists in IcingaWeb2");
181 181
 		    $this->redirectNow('trapdirector/settings?idodberror=2');
182 182
 		    return null;
183 183
 		}
184 184
 		
185 185
 		if ($test == false) 
186 186
 		{ 
187
-			$this->icingaDB = $dbconn; 
187
+			$this->icingaDB=$dbconn; 
188 188
 			return $this->icingaDB;
189 189
 		}
190 190
 		
191 191
 		try
192 192
 		{
193
-		    $query = $dbconn->select()
194
-		    ->from('icinga_dbversion',array('version'));
193
+		    $query=$dbconn->select()
194
+		    ->from('icinga_dbversion', array('version'));
195 195
 		    $version=$dbconn->fetchRow($query);
196
-		    if ( ($version == null) || ! property_exists($version,'version') )
196
+		    if (($version == null) || !property_exists($version, 'version'))
197 197
 		    {
198
-		        return array(4,"$dbresource does not look like an IDO database");
198
+		        return array(4, "$dbresource does not look like an IDO database");
199 199
 		    }
200 200
 		}
201 201
 		catch (Exception $e)
202 202
 		{
203
-			return array(3,"Error connecting to $dbresource : " . $e->getMessage());
203
+			return array(3, "Error connecting to $dbresource : ".$e->getMessage());
204 204
 		}
205 205
 		
206
-		return array(0,'');
206
+		return array(0, '');
207 207
 	}
208 208
 	
209
-    protected function applyPaginationLimits(Paginatable $paginatable, $limit = 25, $offset = null)
209
+    protected function applyPaginationLimits(Paginatable $paginatable, $limit=25, $offset=null)
210 210
     {
211
-        $limit = $this->params->get('limit', $limit);
212
-        $page = $this->params->get('page', $offset);
211
+        $limit=$this->params->get('limit', $limit);
212
+        $page=$this->params->get('page', $offset);
213 213
 
214 214
         $paginatable->limit($limit, $page > 0 ? ($page - 1) * $limit : 0);
215 215
 
216 216
         return $paginatable;
217 217
     }	
218 218
 	
219
-	public function displayExitError($source,$message)
219
+	public function displayExitError($source, $message)
220 220
 	{	// TODO : check better ways to transmit data (with POST ?)
221 221
 		$this->redirectNow('trapdirector/error?source='.$source.'&message='.$message);
222 222
 	}
223 223
 	
224 224
 	protected function checkReadPermission()
225 225
 	{
226
-        if (! $this->Auth()->hasPermission('trapdirector/view')) {
227
-            $this->displayExitError('Permissions','No permission fo view content');
226
+        if (!$this->Auth()->hasPermission('trapdirector/view')) {
227
+            $this->displayExitError('Permissions', 'No permission fo view content');
228 228
         }		
229 229
 	}
230 230
 
231 231
 	protected function checkConfigPermission()
232 232
 	{
233
-        if (! $this->Auth()->hasPermission('trapdirector/config')) {
234
-            $this->displayExitError('Permissions','No permission fo configure');
233
+        if (!$this->Auth()->hasPermission('trapdirector/config')) {
234
+            $this->displayExitError('Permissions', 'No permission fo configure');
235 235
         }		
236 236
 	}
237 237
 	
@@ -242,10 +242,10 @@  discard block
 block discarded – undo
242 242
      */
243 243
 	protected function checkModuleConfigPermission($check=0)
244 244
 	{
245
-        if (! $this->Auth()->hasPermission('trapdirector/module_config')) {
245
+        if (!$this->Auth()->hasPermission('trapdirector/module_config')) {
246 246
             if ($check == 0)
247 247
             {
248
-                $this->displayExitError('Permissions','No permission fo configure module');
248
+                $this->displayExitError('Permissions', 'No permission fo configure module');
249 249
             }
250 250
             return false;
251 251
         }
@@ -257,10 +257,10 @@  discard block
 block discarded – undo
257 257
 	{ // TODO : try/catch here ? or within caller
258 258
 		if ($this->trapClass == null)
259 259
 		{
260
-			require_once($this->Module()->getBaseDir() .'/bin/trap_class.php');
260
+			require_once($this->Module()->getBaseDir().'/bin/trap_class.php');
261 261
 			$icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc');
262 262
 			//$debug_level=4;
263
-			$this->trapClass = new Trap($icingaweb2_etc);
263
+			$this->trapClass=new Trap($icingaweb2_etc);
264 264
 			//$Trap->setLogging($debug_level,'syslog');
265 265
 		}
266 266
 		return $this->trapClass;
@@ -294,12 +294,12 @@  discard block
 block discarded – undo
294 294
 	protected function getHostByIP($ip) 
295 295
 	{
296 296
 		// select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id
297
-		$db = $this->getIdoDb()->getConnection();
297
+		$db=$this->getIdoDb()->getConnection();
298 298
 		// TODO : check for SQL injections
299 299
 		$query=$db->select()
300 300
 				->from(
301 301
 					array('a' => 'icinga_objects'),
302
-					array('name' => 'a.name1','id' => 'object_id'))
302
+					array('name' => 'a.name1', 'id' => 'object_id'))
303 303
 				->join(
304 304
 					array('b' => 'icinga_hosts'),
305 305
 					'b.host_object_id=a.object_id',
@@ -315,12 +315,12 @@  discard block
 block discarded – undo
315 315
 	protected function getHostByName($name) 
316 316
 	{
317 317
 		// select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id
318
-		$db = $this->getIdoDb()->getConnection();
318
+		$db=$this->getIdoDb()->getConnection();
319 319
 		// TODO : check for SQL injections
320 320
 		$query=$db->select()
321 321
 				->from(
322 322
 					array('a' => 'icinga_objects'),
323
-					array('name' => 'a.name1','id' => 'object_id'))
323
+					array('name' => 'a.name1', 'id' => 'object_id'))
324 324
 				->join(
325 325
 					array('b' => 'icinga_hosts'),
326 326
 					'b.host_object_id=a.object_id',
@@ -336,12 +336,12 @@  discard block
 block discarded – undo
336 336
 	protected function getHostGroupByName($ip) 
337 337
 	{
338 338
 		// select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id
339
-		$db = $this->getIdoDb()->getConnection();
339
+		$db=$this->getIdoDb()->getConnection();
340 340
 		// TODO : check for SQL injections
341 341
 		$query=$db->select()
342 342
 				->from(
343 343
 					array('a' => 'icinga_objects'),
344
-					array('name' => 'a.name1','id' => 'object_id'))
344
+					array('name' => 'a.name1', 'id' => 'object_id'))
345 345
 				->join(
346 346
 					array('b' => 'icinga_hostgroups'),
347 347
 					'b.hostgroup_object_id=a.object_id',
@@ -357,8 +357,8 @@  discard block
 block discarded – undo
357 357
 	*/
358 358
 	protected function getHostInfoByID($id) 
359 359
 	{
360
-		if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id');  }
361
-		$db = $this->getIdoDb()->getConnection();
360
+		if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); }
361
+		$db=$this->getIdoDb()->getConnection();
362 362
 		$query=$db->select()
363 363
 				->from(
364 364
 					array('a' => 'icinga_objects'),
@@ -378,17 +378,17 @@  discard block
 block discarded – undo
378 378
 	*/
379 379
 	protected function getHostByObjectID($id) // TODO : duplicate of getHostInfoByID above
380 380
 	{
381
-		if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id');  }
382
-		$db = $this->getIdoDb()->getConnection();
381
+		if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); }
382
+		$db=$this->getIdoDb()->getConnection();
383 383
 		$query=$db->select()
384 384
 				->from(
385 385
 					array('a' => 'icinga_objects'),
386
-					array('name' => 'a.name1','id' => 'a.object_id'))
386
+					array('name' => 'a.name1', 'id' => 'a.object_id'))
387 387
 				->join(
388 388
 					array('b' => 'icinga_hosts'),
389 389
 					'b.host_object_id=a.object_id',
390
-					array('display_name' => 'b.display_name' , 'ip' => 'b.address', 'ip6' => 'b.address6'))
391
-				->where('a.object_id = ?',$id);
390
+					array('display_name' => 'b.display_name', 'ip' => 'b.address', 'ip6' => 'b.address6'))
391
+				->where('a.object_id = ?', $id);
392 392
 		return $db->fetchRow($query);
393 393
 	}	
394 394
 	
@@ -400,16 +400,16 @@  discard block
 block discarded – undo
400 400
 	protected function getServicesByHostid($id) 
401 401
 	{
402 402
 		// select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id
403
-		if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id');  }
404
-		$db = $this->getIdoDb()->getConnection();
403
+		if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); }
404
+		$db=$this->getIdoDb()->getConnection();
405 405
 		$query=$db->select()
406 406
 				->from(
407 407
 					array('s' => 'icinga_services'),
408
-					array('name' => 's.display_name','id' => 's.service_object_id'))
408
+					array('name' => 's.display_name', 'id' => 's.service_object_id'))
409 409
 				->join(
410 410
 					array('a' => 'icinga_objects'),
411 411
 					's.service_object_id=a.object_id',
412
-					array('is_active'=>'a.is_active','name2'=>'a.name2'))
412
+					array('is_active'=>'a.is_active', 'name2'=>'a.name2'))
413 413
 				->where('s.host_object_id='.$id.' AND a.is_active = 1');
414 414
 
415 415
 		return $db->fetchAll($query);
@@ -423,8 +423,8 @@  discard block
 block discarded – undo
423 423
 	*/
424 424
 	protected function getServicesByHostGroupid($id) 
425 425
 	{		
426
-		if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id');  }
427
-		$db = $this->getIdoDb()->getConnection();
426
+		if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); }
427
+		$db=$this->getIdoDb()->getConnection();
428 428
 		$query=$db->select()
429 429
 				->from(
430 430
 					array('s' => 'icinga_hostgroup_members'),
@@ -441,11 +441,11 @@  discard block
 block discarded – undo
441 441
 		foreach ($hosts as $key => $host)
442 442
 		{ // For each host, get all services and add in common_services if not found or add counter
443 443
 			$host_services=$this->getServicesByHostid($host->host_object_id);
444
-			foreach($host_services as $service)
444
+			foreach ($host_services as $service)
445 445
 			{
446 446
 				if (isset($common_services[$service->name2]['num']))
447 447
 				{
448
-					$common_services[$service->name2]['num'] +=1;
448
+					$common_services[$service->name2]['num']+=1;
449 449
 				}
450 450
 				else
451 451
 				{
@@ -461,7 +461,7 @@  discard block
 block discarded – undo
461 461
 		{
462 462
 		    if ($common_services[$key]['num'] == $num_hosts)
463 463
 			{
464
-				array_push($result,array($key,$common_services[$key]['name']));
464
+				array_push($result, array($key, $common_services[$key]['name']));
465 465
 			}
466 466
 		}
467 467
 		
@@ -474,9 +474,9 @@  discard block
 block discarded – undo
474 474
 	*	@param $name string service name
475 475
 	*	@return array  service id
476 476
 	*/
477
-	protected function getServiceIDByName($hostname,$name) 
477
+	protected function getServiceIDByName($hostname, $name) 
478 478
 	{
479
-		$db = $this->getIdoDb()->getConnection();
479
+		$db=$this->getIdoDb()->getConnection();
480 480
 		if ($name == null)
481 481
 		{
482 482
 			return array();
@@ -485,7 +485,7 @@  discard block
 block discarded – undo
485 485
 		$query=$db->select()
486 486
 				->from(
487 487
 					array('s' => 'icinga_services'),
488
-					array('name' => 's.display_name','id' => 's.service_object_id'))
488
+					array('name' => 's.display_name', 'id' => 's.service_object_id'))
489 489
 				->join(
490 490
 					array('a' => 'icinga_objects'),
491 491
 					's.service_object_id=a.object_id',
@@ -503,12 +503,12 @@  discard block
 block discarded – undo
503 503
 	protected function getObjectNameByid($id) 
504 504
 	{
505 505
 		// select a.name1, b.display_name from icinga.icinga_objects AS a , icinga.icinga_hosts AS b WHERE (b.address = '192.168.56.101' OR b.address6= '123456') and b.host_object_id=a.object_id
506
-		if (!preg_match('/^[0-9]+$/',$id)) { throw new Exception('Invalid id');  }
507
-		$db = $this->getIdoDb()->getConnection();
506
+		if (!preg_match('/^[0-9]+$/', $id)) { throw new Exception('Invalid id'); }
507
+		$db=$this->getIdoDb()->getConnection();
508 508
 		$query=$db->select()
509 509
 				->from(
510 510
 					array('a' => 'icinga_objects'),
511
-					array('name1' => 'a.name1','name2' => 'a.name2'))
511
+					array('name1' => 'a.name1', 'name2' => 'a.name2'))
512 512
 				->where('a.object_id='.$id.' AND a.is_active = 1');
513 513
 
514 514
 		return $db->fetchRow($query);
@@ -521,17 +521,17 @@  discard block
 block discarded – undo
521 521
 	protected function addHandlerRule($params)
522 522
 	{
523 523
 		// TODO Check for rule consistency
524
-		$db = $this->getDb()->getConnection();
524
+		$db=$this->getDb()->getConnection();
525 525
 		// Add last modified date = creation date and username
526
-		$params['created'] = new Zend_Db_Expr('NOW()');
527
-		$params['modified'] = new 	Zend_Db_Expr('NOW()');
528
-		$params['modifier'] = $this->Auth()->getUser()->getUsername();
526
+		$params['created']=new Zend_Db_Expr('NOW()');
527
+		$params['modified']=new 	Zend_Db_Expr('NOW()');
528
+		$params['modifier']=$this->Auth()->getUser()->getUsername();
529 529
 		
530 530
 		$query=$db->insert(
531 531
 			$this->getModuleConfig()->getTrapRuleName(),
532 532
 			$params
533 533
 		);
534
-		if($query==false)
534
+		if ($query == false)
535 535
 		{
536 536
 		  return null;
537 537
 		}
@@ -543,13 +543,13 @@  discard block
 block discarded – undo
543 543
 	*   @param integer $ruleID : rule id in db
544 544
 	*	@return array affected rows
545 545
 	*/
546
-	protected function updateHandlerRule($params,$ruleID)
546
+	protected function updateHandlerRule($params, $ruleID)
547 547
 	{
548 548
 		// TODO Check for rule consistency
549
-		$db = $this->getDb()->getConnection();
549
+		$db=$this->getDb()->getConnection();
550 550
 		// Add last modified date = creation date and username
551
-		$params['modified'] = new 	Zend_Db_Expr('NOW()');
552
-		$params['modifier'] = $this->Auth()->getUser()->getUsername();
551
+		$params['modified']=new 	Zend_Db_Expr('NOW()');
552
+		$params['modifier']=$this->Auth()->getUser()->getUsername();
553 553
 		
554 554
 		$numRows=$db->update(
555 555
 			$this->getModuleConfig()->getTrapRuleName(),
@@ -564,8 +564,8 @@  discard block
 block discarded – undo
564 564
 	*/
565 565
 	protected function deleteRule($ruleID)
566 566
 	{
567
-		if (!preg_match('/^[0-9]+$/',$ruleID)) { throw new Exception('Invalid id');  }
568
-		$db = $this->getDb()->getConnection();
567
+		if (!preg_match('/^[0-9]+$/', $ruleID)) { throw new Exception('Invalid id'); }
568
+		$db=$this->getDb()->getConnection();
569 569
 		
570 570
 		$query=$db->delete(
571 571
 			$this->getModuleConfig()->getTrapRuleName(),
@@ -578,10 +578,10 @@  discard block
 block discarded – undo
578 578
 	*	@param $ip string source IP (v4 or v6)
579 579
 	*	@param $oid string oid
580 580
 	*/
581
-	protected function deleteTrap($ip,$oid)
581
+	protected function deleteTrap($ip, $oid)
582 582
 	{
583 583
 		
584
-		$db = $this->getDb()->getConnection();
584
+		$db=$this->getDb()->getConnection();
585 585
 		$condition=null;
586 586
 		if ($ip != null)
587 587
 		{
@@ -589,10 +589,10 @@  discard block
 block discarded – undo
589 589
 		}
590 590
 		if ($oid != null)
591 591
 		{
592
-			$condition=($condition===null)?'':$condition.' AND ';
592
+			$condition=($condition === null) ? '' : $condition.' AND ';
593 593
 			$condition.="trap_oid='$oid'";
594 594
 		}
595
-		if($condition === null) return null;
595
+		if ($condition === null) return null;
596 596
 		$query=$db->delete(
597 597
 			$this->getModuleConfig()->getTrapTableName(),
598 598
 			$condition
@@ -606,10 +606,10 @@  discard block
 block discarded – undo
606 606
 	*	@param $ip string source IP (v4 or v6)
607 607
 	*	@param $oid string oid
608 608
 	*/
609
-	protected function countTrap($ip,$oid)
609
+	protected function countTrap($ip, $oid)
610 610
 	{
611 611
 		
612
-		$db = $this->getDb()->getConnection();
612
+		$db=$this->getDb()->getConnection();
613 613
 		$condition=null;
614 614
 		if ($ip != null)
615 615
 		{
@@ -617,10 +617,10 @@  discard block
 block discarded – undo
617 617
 		}
618 618
 		if ($oid != null)
619 619
 		{
620
-			$condition=($condition===null)?'':$condition.' AND ';
620
+			$condition=($condition === null) ? '' : $condition.' AND ';
621 621
 			$condition.="trap_oid='$oid'";
622 622
 		}
623
-		if($condition === null) return 0;
623
+		if ($condition === null) return 0;
624 624
 		$query=$db->select()
625 625
 			->from(
626 626
 				$this->getModuleConfig()->getTrapTableName(),
@@ -636,27 +636,27 @@  discard block
 block discarded – undo
636 636
 	protected function getDBConfigValue($element)
637 637
 	{
638 638
 	
639
-		$db = $this->getDb()->getConnection();
639
+		$db=$this->getDb()->getConnection();
640 640
 		
641 641
 		$query=$db->select()
642 642
 			->from(
643 643
 				$this->getModuleConfig()->getDbConfigTableName(),
644 644
 				array('value'=>'value'))
645
-			->where('name=?',$element);
645
+			->where('name=?', $element);
646 646
 		$return_row=$db->fetchRow($query);
647
-		if ($return_row==null)  // value does not exists
647
+		if ($return_row == null)  // value does not exists
648 648
 		{
649 649
 			$default=$this->getModuleConfig()->getDBConfigDefaults();
650
-			if ( ! isset($default[$element])) return null; // no default and not value
650
+			if (!isset($default[$element])) return null; // no default and not value
651 651
 			
652
-			$this->addDBConfigValue($element,$default[$element]);
652
+			$this->addDBConfigValue($element, $default[$element]);
653 653
 			return $default[$element];
654 654
 		}
655 655
 		if ($return_row->value == null) // value id empty
656 656
 		{
657 657
 			$default=$this->getModuleConfig()->getDBConfigDefaults();
658
-			if ( ! isset($default[$element])) return null; // no default and not value
659
-			$this->setDBConfigValue($element,$default[$element]);
658
+			if (!isset($default[$element])) return null; // no default and not value
659
+			$this->setDBConfigValue($element, $default[$element]);
660 660
 			return $default[$element];			
661 661
 		}
662 662
 		return $return_row->value;		
@@ -667,10 +667,10 @@  discard block
 block discarded – undo
667 667
 	*   @param string $value : value
668 668
 	*/
669 669
 		
670
-	protected function addDBConfigValue($element,$value)
670
+	protected function addDBConfigValue($element, $value)
671 671
 	{
672 672
 	
673
-		$db = $this->getDb()->getConnection();
673
+		$db=$this->getDb()->getConnection();
674 674
 		
675 675
 		$query=$db->insert(
676 676
 				$this->getModuleConfig()->getDbConfigTableName(),
@@ -686,10 +686,10 @@  discard block
 block discarded – undo
686 686
 	*	@param string $element : name of config element
687 687
 	*   @param string $value : value
688 688
 	*/
689
-	protected function setDBConfigValue($element,$value)
689
+	protected function setDBConfigValue($element, $value)
690 690
 	{
691 691
 	
692
-		$db = $this->getDb()->getConnection();
692
+		$db=$this->getDb()->getConnection();
693 693
 		$query=$db->update(
694 694
 				$this->getModuleConfig()->getDbConfigTableName(),
695 695
 				array('value'=>$value),
@@ -704,10 +704,10 @@  discard block
 block discarded – undo
704 704
 	protected function isDirectorInstalled()
705 705
 	{
706 706
 	    $output=array();
707
-	    exec('icingacli module list',$output);
707
+	    exec('icingacli module list', $output);
708 708
 	    foreach ($output as $line)
709 709
 		{
710
-			if (preg_match('/^director .*enabled/',$line))
710
+			if (preg_match('/^director .*enabled/', $line))
711 711
 			{
712 712
 				return true;
713 713
 			}
Please login to merge, or discard this patch.