Passed
Push — master ( 9f9a4d...75cf8d )
by Patrick
02:29
created
bin/trap_class.php 3 patches
Indentation   +577 added lines, -577 removed lines patch added patch discarded remove patch
@@ -88,24 +88,24 @@  discard block
 block discarded – undo
88 88
 	 */
89 89
 	protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = 2, $message = null)
90 90
 	{
91
-	    if (!isset($option_array[$option_category][$option_name]))
92
-	    {
93
-	        if ($message === null)
94
-	        {
95
-	            $message='No ' . $option_name . ' in config file: '. $this->trap_module_config;
96
-	        }
97
-	        $this->trapLog($message,$log_level,'syslog');
98
-	        return false;
99
-	    }
100
-	    else
101
-	    {
102
-	        $option_var=$option_array[$option_category][$option_name];
103
-	        return true;
104
-	    }
91
+		if (!isset($option_array[$option_category][$option_name]))
92
+		{
93
+			if ($message === null)
94
+			{
95
+				$message='No ' . $option_name . ' in config file: '. $this->trap_module_config;
96
+			}
97
+			$this->trapLog($message,$log_level,'syslog');
98
+			return false;
99
+		}
100
+		else
101
+		{
102
+			$option_var=$option_array[$option_category][$option_name];
103
+			return true;
104
+		}
105 105
 	}
106 106
 	
107 107
 	/** Get options from ini file and database
108
-	*/
108
+	 */
109 109
 	protected function getOptions()
110 110
 	{
111 111
 		$trap_config=parse_ini_file($this->trap_module_config,true);
@@ -128,10 +128,10 @@  discard block
 block discarded – undo
128 128
 		// API options
129 129
 		if ($this->getOptionIfSet($trap_config,'config','icingaAPI_host', $this->api_hostname))
130 130
 		{
131
-		    $this->api_use=true;
132
-		    $this->getOptionIfSet($trap_config,'config','icingaAPI_port', $this->api_port);
133
-		    $this->getOptionIfSet($trap_config,'config','icingaAPI_user', $this->api_username);
134
-		    $this->getOptionIfSet($trap_config,'config','icingaAPI_password', $this->api_password);
131
+			$this->api_use=true;
132
+			$this->getOptionIfSet($trap_config,'config','icingaAPI_port', $this->api_port);
133
+			$this->getOptionIfSet($trap_config,'config','icingaAPI_user', $this->api_username);
134
+			$this->getOptionIfSet($trap_config,'config','icingaAPI_password', $this->api_password);
135 135
 		}
136 136
 				
137 137
 		/***** Database options :  ***/
@@ -148,9 +148,9 @@  discard block
 block discarded – undo
148 148
 	}
149 149
 	
150 150
 	/** Get data from db_config
151
-	*	@param $element string name of param
152
-	*	@return $value (or null)
153
-	*/	
151
+	 *	@param $element string name of param
152
+	 *	@return $value (or null)
153
+	 */	
154 154
 	protected function getDBConfig($element)
155 155
 	{
156 156
 		$db_conn=$this->db_connect_trap();
@@ -168,11 +168,11 @@  discard block
 block discarded – undo
168 168
 	}
169 169
 	
170 170
 	/** Send log. Throws exception on critical error
171
-	*	@param	string $message Message to log
172
-	*	@param	int $level 1=critical 2=warning 3=trace 4=debug
173
-	*	@param  int $destination file/syslog/display
174
-	*	@return void
175
-	**/	
171
+	 *	@param	string $message Message to log
172
+	 *	@param	int $level 1=critical 2=warning 3=trace 4=debug
173
+	 *	@param  int $destination file/syslog/display
174
+	 *	@return void
175
+	 **/	
176 176
 	public function trapLog( $message, $level, $destination ='')
177 177
 	{	
178 178
 		if ($this->debug_level >= $level) 
@@ -216,7 +216,7 @@  discard block
 block discarded – undo
216 216
 		switch ($output_type)
217 217
 		{
218 218
 			case 'file':
219
-			    if ($output_option == null) throw new Exception("File logging without file !");
219
+				if ($output_option == null) throw new Exception("File logging without file !");
220 220
 				$this->debug_file=$output_option;
221 221
 				$this->alert_output='file';
222 222
 				break;
@@ -233,30 +233,30 @@  discard block
 block discarded – undo
233 233
 	
234 234
 	protected function getAPI()
235 235
 	{
236
-	    if ($this->icinga2api == null)
237
-	    {
238
-	        $this->icinga2api = new Icinga2API($this->api_hostname,$this->api_port);
239
-	    }
240
-	    return $this->icinga2api;
236
+		if ($this->icinga2api == null)
237
+		{
238
+			$this->icinga2api = new Icinga2API($this->api_hostname,$this->api_port);
239
+		}
240
+		return $this->icinga2api;
241 241
 	}
242 242
 
243 243
 	/** Connects to trapdb 
244
-	*	@return PDO connection
245
-	*/
244
+	 *	@return PDO connection
245
+	 */
246 246
 	public function db_connect_trap() 
247 247
 	{
248 248
 		if ($this->trapDB != null) {
249
-		    // Check if connection is still alive
250
-		    try {
251
-		        $this->trapDB->query('select 1')->fetchColumn();
252
-		        return $this->trapDB;
253
-		    } catch (Exception $e) {
254
-		        // select 1 failed, try to reconnect.
255
-		        $this->trapDB=null;
249
+			// Check if connection is still alive
250
+			try {
251
+				$this->trapDB->query('select 1')->fetchColumn();
252
+				return $this->trapDB;
253
+			} catch (Exception $e) {
254
+				// select 1 failed, try to reconnect.
255
+				$this->trapDB=null;
256 256
 				$this->trapDB=$this->db_connect('traps');
257
-		        $this->trapLog('Database connection lost, reconnecting',WARN,'');
257
+				$this->trapLog('Database connection lost, reconnecting',WARN,'');
258 258
 				return $this->trapDB;
259
-		    }
259
+			}
260 260
 		     
261 261
 		}
262 262
 		$this->trapDB=$this->db_connect('traps');
@@ -265,8 +265,8 @@  discard block
 block discarded – undo
265 265
 	
266 266
 
267 267
 	/** Connects to idodb 
268
-	*	@return PDO connection
269
-	*/
268
+	 *	@return PDO connection
269
+	 */
270 270
 	public function db_connect_ido() 
271 271
 	{
272 272
 		if ($this->idoDB != null) { return $this->idoDB; }
@@ -275,9 +275,9 @@  discard block
 block discarded – undo
275 275
 	}	
276 276
 	
277 277
 	/** connects to database named by parameter
278
-	*	@param string : 'traps' for traps database, 'ido' for ido database
279
-	*	@return PDO connection
280
-	**/
278
+	 *	@param string : 'traps' for traps database, 'ido' for ido database
279
+	 *	@return PDO connection
280
+	 **/
281 281
 	protected function db_connect($database) {
282 282
 		$confarray=$this->get_database($database);
283 283
 		//	$dsn = 'mysql:dbname=traps;host=127.0.0.1';
@@ -294,9 +294,9 @@  discard block
 block discarded – undo
294 294
 	}
295 295
 
296 296
 	/** Get database connexion options
297
-	*	@param string database : 'traps' for traps database, 'ido' for ido database
298
-	*	@return array( DB type (mysql, pgsql.) , db_host, database name , db_user, db_pass)
299
-	**/
297
+	 *	@param string database : 'traps' for traps database, 'ido' for ido database
298
+	 *	@return array( DB type (mysql, pgsql.) , db_host, database name , db_user, db_pass)
299
+	 **/
300 300
 	protected function get_database($database) {
301 301
 
302 302
 		$trap_config=parse_ini_file($this->trap_module_config,true);
@@ -347,9 +347,9 @@  discard block
 block discarded – undo
347 347
 	}	
348 348
 	
349 349
 	/** read data from stream
350
-	*	@param $stream string input stream, defaults to "php://stdin"
351
-	*	@return array trap data
352
-	*/
350
+	 *	@param $stream string input stream, defaults to "php://stdin"
351
+	 *	@return array trap data
352
+	 */
353 353
 	public function read_trap($stream='php://stdin')
354 354
 	{
355 355
 		//Read data from snmptrapd from stdin
@@ -357,7 +357,7 @@  discard block
 block discarded – undo
357 357
 
358 358
 		if ($input_stream==FALSE)
359 359
 		{
360
-		    $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)");
360
+			$this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)");
361 361
 			$this->trapLog("Error reading stdin !",ERROR,''); 
362 362
 		}
363 363
 
@@ -365,21 +365,21 @@  discard block
 block discarded – undo
365 365
 		$this->receivingHost=chop(fgets($input_stream));
366 366
 		if ($this->receivingHost === false)
367 367
 		{
368
-		    $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)");
368
+			$this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)");
369 369
 			$this->trapLog("Error reading Host !",ERROR,''); 
370 370
 		}
371 371
 		// line 2 IP:port=>IP:port
372 372
 		$IP=chop(fgets($input_stream));
373 373
 		if ($IP === false)
374 374
 		{
375
-		    $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)");
375
+			$this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)");
376 376
 			$this->trapLog("Error reading IP !",ERROR,''); 
377 377
 		}
378 378
 		$matches=array();
379 379
 		$ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches);
380 380
 		if ($ret_code===0 || $ret_code==false) 
381 381
 		{
382
-		    $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)");
382
+			$this->writeTrapErrorToDB("Error parsing trap (code 2/IP)");
383 383
 			$this->trapLog('Error parsing IP : '.$IP,ERROR,'');
384 384
 		} 
385 385
 		else 
@@ -399,7 +399,7 @@  discard block
 block discarded – undo
399 399
 			}
400 400
 			else 
401 401
 			{
402
-			    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'))
402
+				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'))
403 403
 				{
404 404
 					$this->trap_data['trap_oid']=$matches[2];				
405 405
 				}
@@ -415,7 +415,7 @@  discard block
 block discarded – undo
415 415
 
416 416
 		if ($this->trap_data['trap_oid']=='unknown') 
417 417
 		{
418
-		    $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trap_data['source_ip']);
418
+			$this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trap_data['source_ip']);
419 419
 			$this->trapLog('no trap oid found',ERROR,'');
420 420
 		} 
421 421
 
@@ -444,9 +444,9 @@  discard block
 block discarded – undo
444 444
 	}
445 445
 
446 446
 	/** Translate oid into array(MIB,Name)
447
-	* @param $oid string oid to translate
448
-	* @return mixed : null if not found or array(MIB,Name)
449
-	*/
447
+	 * @param $oid string oid to translate
448
+	 * @return mixed : null if not found or array(MIB,Name)
449
+	 */
450 450
 	public function translateOID($oid)
451 451
 	{
452 452
 		// try from database
@@ -479,7 +479,7 @@  discard block
 block discarded – undo
479 479
 		
480 480
 		// Try to get oid name from snmptranslate
481 481
 		$translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.
482
-		    ' '.$oid);
482
+			' '.$oid);
483 483
 		$matches=array();
484 484
 		$ret_code=preg_match('/(.*)::(.*)/',$translate,$matches);
485 485
 		if ($ret_code===0 || $ret_code===FALSE) {
@@ -491,9 +491,9 @@  discard block
 block discarded – undo
491 491
 	}
492 492
 	
493 493
 	/** Erase old trap records 
494
-	*	@param $days : erase traps when more than $days old
495
-	*	@return : number of lines deleted
496
-	**/
494
+	 *	@param $days : erase traps when more than $days old
495
+	 *	@return : number of lines deleted
496
+	 **/
497 497
 	public function eraseOldTraps($days=0)
498 498
 	{
499 499
 		if ($days==0)
@@ -518,74 +518,74 @@  discard block
 block discarded – undo
518 518
 	public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null)
519 519
 	{
520 520
 	    
521
-	    $db_conn=$this->db_connect_trap();
521
+		$db_conn=$this->db_connect_trap();
522 522
 	    
523
-	    $insert_col='';
524
-	    $insert_val='';
525
-	    // add date time
526
-	    $insert_col ='date_received,status';
527
-	    $insert_val = "'" . date("Y-m-d H:i:s")."','error'";
523
+		$insert_col='';
524
+		$insert_val='';
525
+		// add date time
526
+		$insert_col ='date_received,status';
527
+		$insert_val = "'" . date("Y-m-d H:i:s")."','error'";
528 528
         
529
-	    if ($sourceIP !=null)
530
-	    {
531
-	        $insert_col .=',source_ip';
532
-	        $insert_val .=",'". $sourceIP ."'";
533
-	    }
534
-	    if ($trapoid !=null)
535
-	    {
536
-	        $insert_col .=',trap_oid';
537
-	        $insert_val .=",'". $trapoid ."'";
538
-	    }
539
-	    $insert_col .=',status_detail';
540
-	    $insert_val .=",'". $message ."'";
529
+		if ($sourceIP !=null)
530
+		{
531
+			$insert_col .=',source_ip';
532
+			$insert_val .=",'". $sourceIP ."'";
533
+		}
534
+		if ($trapoid !=null)
535
+		{
536
+			$insert_col .=',trap_oid';
537
+			$insert_val .=",'". $trapoid ."'";
538
+		}
539
+		$insert_col .=',status_detail';
540
+		$insert_val .=",'". $message ."'";
541 541
 	    
542
-	    $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')';
542
+		$sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')';
543 543
 	    
544
-	    switch ($this->trapDBType)
545
-	    {
546
-	        case 'pgsql':
547
-	            $sql .= ' RETURNING id;';
548
-	            $this->trapLog('sql : '.$sql,3,'');
549
-	            if (($ret_code=$db_conn->query($sql)) == FALSE) {
550
-	                $this->trapLog('Error SQL insert : '.$sql,1,'');
551
-	            }
552
-	            $this->trapLog('SQL insertion OK',3,'');
553
-	            // Get last id to insert oid/values in secondary table
554
-	            if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) == FALSE) {
544
+		switch ($this->trapDBType)
545
+		{
546
+			case 'pgsql':
547
+				$sql .= ' RETURNING id;';
548
+				$this->trapLog('sql : '.$sql,3,'');
549
+				if (($ret_code=$db_conn->query($sql)) == FALSE) {
550
+					$this->trapLog('Error SQL insert : '.$sql,1,'');
551
+				}
552
+				$this->trapLog('SQL insertion OK',3,'');
553
+				// Get last id to insert oid/values in secondary table
554
+				if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) == FALSE) {
555 555
 	                
556
-	                $this->trapLog('Erreur recuperation id',1,'');
557
-	            }
558
-	            if (! isset($inserted_id_ret['id'])) {
559
-	                $this->trapLog('Error getting id',1,'');
560
-	            }
561
-	            $this->trap_id=$inserted_id_ret['id'];
562
-	            break;
563
-	        case 'mysql':
564
-	            $sql .= ';';
565
-	            $this->trapLog('sql : '.$sql,3,'');
566
-	            if (($ret_code=$db_conn->query($sql)) == FALSE) {
567
-	                $this->trapLog('Error SQL insert : '.$sql,1,'');
568
-	            }
569
-	            $this->trapLog('SQL insertion OK',3,'');
570
-	            // Get last id to insert oid/values in secondary table
571
-	            $sql='SELECT LAST_INSERT_ID();';
572
-	            if (($ret_code=$db_conn->query($sql)) == FALSE) {
573
-	                $this->trapLog('Erreur recuperation id',1,'');
574
-	            }
556
+					$this->trapLog('Erreur recuperation id',1,'');
557
+				}
558
+				if (! isset($inserted_id_ret['id'])) {
559
+					$this->trapLog('Error getting id',1,'');
560
+				}
561
+				$this->trap_id=$inserted_id_ret['id'];
562
+				break;
563
+			case 'mysql':
564
+				$sql .= ';';
565
+				$this->trapLog('sql : '.$sql,3,'');
566
+				if (($ret_code=$db_conn->query($sql)) == FALSE) {
567
+					$this->trapLog('Error SQL insert : '.$sql,1,'');
568
+				}
569
+				$this->trapLog('SQL insertion OK',3,'');
570
+				// Get last id to insert oid/values in secondary table
571
+				$sql='SELECT LAST_INSERT_ID();';
572
+				if (($ret_code=$db_conn->query($sql)) == FALSE) {
573
+					$this->trapLog('Erreur recuperation id',1,'');
574
+				}
575 575
 	            
576
-	            $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()'];
577
-	            if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
578
-	            $this->trap_id=$inserted_id;
579
-	            break;
580
-	        default:
581
-	            $this->trapLog('Error SQL type  : '.$this->trapDBType,1,'');
582
-	    }
576
+				$inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()'];
577
+				if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
578
+				$this->trap_id=$inserted_id;
579
+				break;
580
+			default:
581
+				$this->trapLog('Error SQL type  : '.$this->trapDBType,1,'');
582
+		}
583 583
 	    
584
-	    $this->trapLog('id found: '. $this->trap_id,3,'');    
584
+		$this->trapLog('id found: '. $this->trap_id,3,'');    
585 585
 	}
586 586
 	
587 587
 	/** Write trap data to trap database
588
-	*/
588
+	 */
589 589
 	public function writeTrapToDB()
590 590
 	{
591 591
 		
@@ -683,10 +683,10 @@  discard block
 block discarded – undo
683 683
 	}
684 684
 
685 685
 	/** Get rules from rule database with ip and oid
686
-	*	@param $ip string ipv4 or ipv6
687
-	*	@param $oid string oid in numeric
688
-	*	@return mixed : PDO object or false
689
-	*/	
686
+	 *	@param $ip string ipv4 or ipv6
687
+	 *	@param $oid string oid in numeric
688
+	 *	@return mixed : PDO object or false
689
+	 */	
690 690
 	protected function getRules($ip,$oid)
691 691
 	{
692 692
 		$db_conn=$this->db_connect_trap();
@@ -745,9 +745,9 @@  discard block
 block discarded – undo
745 745
 	}
746 746
 
747 747
 	/** Add rule match to rule
748
-	*	@param id int : rule id
749
-	*   @param set int : value to set
750
-	*/
748
+	 *	@param id int : rule id
749
+	 *   @param set int : value to set
750
+	 */
751 751
 	protected function add_rule_match($id, $set)
752 752
 	{
753 753
 		$db_conn=$this->db_connect_trap();
@@ -764,53 +764,53 @@  discard block
 block discarded – undo
764 764
 	 * @param number $state numerical staus 
765 765
 	 * @param string $display
766 766
 	 * @returnn bool true is service check was sent without error
767
-	*/
767
+	 */
768 768
 	public function serviceCheckResult($host,$service,$state,$display)
769 769
 	{
770
-	    if ($this->api_use == false)
771
-	    {
772
-    		$send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' .
773
-    			$host.';' .$service .';' . $state . ';'.$display;
774
-    		$this->trapLog( $send." : to : " .$this->icinga2cmd,3,'');
770
+		if ($this->api_use == false)
771
+		{
772
+			$send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' .
773
+				$host.';' .$service .';' . $state . ';'.$display;
774
+			$this->trapLog( $send." : to : " .$this->icinga2cmd,3,'');
775 775
     		
776
-    		// TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default....
777
-    		exec('echo "'.$send.'" > ' .$this->icinga2cmd);
778
-    		return true;
779
-	    }
780
-	    else
781
-	    {
782
-	        $api = $this->getAPI();
783
-	        $api->setCredentials($this->api_username, $this->api_password);
784
-	        list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display);
785
-	        if ($retcode == false)
786
-	        {
787
-	            $this->trapLog( "Error sending result : " .$retmessage,WARN,'');
788
-	            return false;
789
-	        }
790
-	        else 
791
-	        {
792
-	            $this->trapLog( "Sent result : " .$retmessage,3,'');
793
-	            return true;
794
-	        }
795
-	    }
776
+			// TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default....
777
+			exec('echo "'.$send.'" > ' .$this->icinga2cmd);
778
+			return true;
779
+		}
780
+		else
781
+		{
782
+			$api = $this->getAPI();
783
+			$api->setCredentials($this->api_username, $this->api_password);
784
+			list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display);
785
+			if ($retcode == false)
786
+			{
787
+				$this->trapLog( "Error sending result : " .$retmessage,WARN,'');
788
+				return false;
789
+			}
790
+			else 
791
+			{
792
+				$this->trapLog( "Sent result : " .$retmessage,3,'');
793
+				return true;
794
+			}
795
+		}
796 796
 	}
797 797
 	
798 798
 	public function getHostByIP($ip)
799 799
 	{
800
-	    $api = $this->getAPI();
801
-	    $api->setCredentials($this->api_username, $this->api_password);
802
-	    return $api->getHostByIP($ip);
800
+		$api = $this->getAPI();
801
+		$api->setCredentials($this->api_username, $this->api_password);
802
+		return $api->getHostByIP($ip);
803 803
 	}
804 804
 	
805 805
 	/** Resolve display. 
806
-	*	Changes OID(<oid>) to value if found or text "<not in trap>"
807
-	*	@param $display string
808
-	*	@return string display
809
-	*/
806
+	 *	Changes OID(<oid>) to value if found or text "<not in trap>"
807
+	 *	@param $display string
808
+	 *	@return string display
809
+	 */
810 810
 	protected function applyDisplay($display)
811 811
 	{
812
-	    $matches=array();
813
-	    while (preg_match('/_OID\(([0-9\.]+)\)/',$display,$matches) == 1)
812
+		$matches=array();
813
+		while (preg_match('/_OID\(([0-9\.]+)\)/',$display,$matches) == 1)
814 814
 		{
815 815
 			$oid=$matches[1];
816 816
 			$found=0;
@@ -870,11 +870,11 @@  discard block
 block discarded – undo
870 870
 		
871 871
 		if ($rule[$item] == '(')
872 872
 		{ // grouping
873
-		    $item++;
873
+			$item++;
874 874
 			$start=$item;
875 875
 			$parenthesis_count=0; 
876 876
 			while (($item < strlen($rule)) // Not end of string AND
877
-			      && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded ()
877
+				  && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded ()
878 878
 			{ 
879 879
 				if ($rule[$item] == '"' )
880 880
 				{ // pass through string
@@ -882,14 +882,14 @@  discard block
 block discarded – undo
882 882
 					$item=$this->eval_getNext($rule,$item,'"');
883 883
 				} 
884 884
 				else{
885
-				    if ($rule[$item] == '(')
886
-				    {
887
-				        $parenthesis_count++;
888
-				    }
889
-				    if ($rule[$item] == ')')
890
-				    {
891
-				        $parenthesis_count--;
892
-				    }
885
+					if ($rule[$item] == '(')
886
+					{
887
+						$parenthesis_count++;
888
+					}
889
+					if ($rule[$item] == ')')
890
+					{
891
+						$parenthesis_count--;
892
+					}
893 893
 					$item++;
894 894
 				}
895 895
 			}
@@ -941,24 +941,24 @@  discard block
 block discarded – undo
941 941
 	}
942 942
 	
943 943
 	/** Evaluation : makes token and evaluate. 
944
-	*	Public function for expressions testing
945
-	*	accepts : < > = <= >= !=  (typec = 0)
946
-	*	operators : & | (typec=1)
947
-	*	with : integers/float  (type 0) or strings "" (type 1) or results (type 2)
948
-	*   comparison int vs strings will return null (error)
949
-	*	return : bool or null on error
950
-	*/
944
+	 *	Public function for expressions testing
945
+	 *	accepts : < > = <= >= !=  (typec = 0)
946
+	 *	operators : & | (typec=1)
947
+	 *	with : integers/float  (type 0) or strings "" (type 1) or results (type 2)
948
+	 *   comparison int vs strings will return null (error)
949
+	 *	return : bool or null on error
950
+	 */
951 951
 	public function evaluation($rule,&$item)
952 952
 	{
953
-	    //echo "Evaluation of ".substr($rule,$item)."\n";
953
+		//echo "Evaluation of ".substr($rule,$item)."\n";
954 954
 		if ( $rule[$item] == '!') // If '!' found, negate next expression.
955 955
 		{
956
-		    $negate=true;
957
-		    $item++;
956
+			$negate=true;
957
+			$item++;
958 958
 		}
959 959
 		else
960 960
 		{
961
-		    $negate=false;
961
+			$negate=false;
962 962
 		}
963 963
 		// First element : number, string or ()
964 964
 		list($type1,$val1) = $this->eval_getElement($rule,$item);
@@ -978,24 +978,24 @@  discard block
 block discarded – undo
978 978
 		// Third element : number, string or ()
979 979
 		if ( $rule[$item] == '!') // starts with a ! so evaluate whats next
980 980
 		{
981
-		    $item++;
982
-		    if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule);
983
-		    $val2= ! $this->evaluation($rule,$item);
984
-		    $type2=2; // result is a boolean 
981
+			$item++;
982
+			if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule);
983
+			$val2= ! $this->evaluation($rule,$item);
984
+			$type2=2; // result is a boolean 
985 985
 		}
986 986
 		else 
987 987
 		{
988
-		    list($type2,$val2) = $this->eval_getElement($rule,$item);
988
+			list($type2,$val2) = $this->eval_getElement($rule,$item);
989 989
 		}
990 990
 		//echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n";
991 991
 		
992 992
 		if ($type1!=$type2)  // cannot compare different types
993 993
 		{ 
994
-		    throw new Exception("Cannot compare string & number : ".$rule);
994
+			throw new Exception("Cannot compare string & number : ".$rule);
995 995
 		}
996 996
 		if ($typec==1 && $type1 !=2) // cannot use & or | with string/number
997 997
 		{
998
-		    throw new Exception("Cannot use boolean operators with string & number : ".$rule);
998
+			throw new Exception("Cannot use boolean operators with string & number : ".$rule);
999 999
 		}
1000 1000
 		
1001 1001
 		switch ($comp){
@@ -1053,9 +1053,9 @@  discard block
 block discarded – undo
1053 1053
 	}		
1054 1054
 	
1055 1055
 	/** Evaluation rule (uses eval_* functions recursively)
1056
-	*	@param $rule string rule ( _OID(.1.3.6.1.4.1.8072.2.3.2.1)=_OID(.1.3.6.1.2.1.1.3.0) )
1057
-	*	@return : true : rule match, false : rule don't match , throw exception on error.
1058
-	*/
1056
+	 *	@param $rule string rule ( _OID(.1.3.6.1.4.1.8072.2.3.2.1)=_OID(.1.3.6.1.2.1.1.3.0) )
1057
+	 *	@return : true : rule match, false : rule don't match , throw exception on error.
1058
+	 */
1059 1059
 	
1060 1060
 	protected function eval_rule($rule)
1061 1061
 	{
@@ -1100,7 +1100,7 @@  discard block
 block discarded – undo
1100 1100
 			}
1101 1101
 			if ($found==0)
1102 1102
 			{	// OID not found : throw error
1103
-			    throw new Exception('OID '.$oid.' not found in trap');
1103
+				throw new Exception('OID '.$oid.' not found in trap');
1104 1104
 			}
1105 1105
 		}
1106 1106
 		$item=0;
@@ -1111,7 +1111,7 @@  discard block
 block discarded – undo
1111 1111
 	}
1112 1112
 	
1113 1113
 	/** Match rules for current trap and do action
1114
-	*/
1114
+	 */
1115 1115
 	public function applyRules()
1116 1116
 	{
1117 1117
 		$rules = $this->getRules($this->trap_data['source_ip'],$this->trap_data['trap_oid']);
@@ -1148,12 +1148,12 @@  discard block
 block discarded – undo
1148 1148
 					{
1149 1149
 						if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false)
1150 1150
 						{
1151
-						    $this->trap_action.='Error sending status : check cmd/API';
1151
+							$this->trap_action.='Error sending status : check cmd/API';
1152 1152
 						}
1153 1153
 						else
1154 1154
 						{
1155
-						    $this->add_rule_match($rule['id'],$rule['num_match']+1);
1156
-						    $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name;
1155
+							$this->add_rule_match($rule['id'],$rule['num_match']+1);
1156
+							$this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name;
1157 1157
 						}
1158 1158
 					}
1159 1159
 					else
@@ -1170,15 +1170,15 @@  discard block
 block discarded – undo
1170 1170
 					$this->trapLog('action NOK : '.$action,3,'');
1171 1171
 					if ($action >= 0)
1172 1172
 					{
1173
-					    if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false)
1174
-					    {
1175
-					        $this->trap_action.='Error sending status : check cmd/API';
1176
-					    }
1177
-					    else
1178
-					    {
1179
-    						$this->add_rule_match($rule['id'],$rule['num_match']+1);
1180
-    						$this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name;
1181
-					    }
1173
+						if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false)
1174
+						{
1175
+							$this->trap_action.='Error sending status : check cmd/API';
1176
+						}
1177
+						else
1178
+						{
1179
+							$this->add_rule_match($rule['id'],$rule['num_match']+1);
1180
+							$this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name;
1181
+						}
1182 1182
 					}
1183 1183
 					else
1184 1184
 					{
@@ -1201,9 +1201,9 @@  discard block
 block discarded – undo
1201 1201
 			}
1202 1202
 			catch (Exception $e) 
1203 1203
 			{ 
1204
-			    $this->trapLog('Error in rule eval : '.$e->getMessage(),WARN,'');
1205
-			    $this->trap_action.=' ERR : '.$e->getMessage();
1206
-			    $this->trap_data['status']='error';
1204
+				$this->trapLog('Error in rule eval : '.$e->getMessage(),WARN,'');
1205
+				$this->trap_action.=' ERR : '.$e->getMessage();
1206
+				$this->trap_data['status']='error';
1207 1207
 			}
1208 1208
 			
1209 1209
 		}
@@ -1218,8 +1218,8 @@  discard block
 block discarded – undo
1218 1218
 	}
1219 1219
 
1220 1220
 	/** Add Time a action to rule
1221
-	*	@param rule id
1222
-	*/
1221
+	 *	@param rule id
1222
+	 */
1223 1223
 	public function add_rule_final($time)
1224 1224
 	{
1225 1225
 		$db_conn=$this->db_connect_trap();
@@ -1236,9 +1236,9 @@  discard block
 block discarded – undo
1236 1236
 	/*********** UTILITIES *********************/
1237 1237
 	
1238 1238
 	/** Create database schema 
1239
-	*	@param $schema_file	string File to read schema from
1240
-	*	@param $table_prefix string to replace #PREFIX# in schema file by this
1241
-	*/
1239
+	 *	@param $schema_file	string File to read schema from
1240
+	 *	@param $table_prefix string to replace #PREFIX# in schema file by this
1241
+	 */
1242 1242
 	public function create_schema($schema_file,$table_prefix)
1243 1243
 	{
1244 1244
 		//Read data from snmptrapd from stdin
@@ -1257,30 +1257,30 @@  discard block
 block discarded – undo
1257 1257
 		{
1258 1258
 			$newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line));
1259 1259
 			if (preg_match('/; *$/', $newline)) 
1260
-            {
1261
-                $sql= $newline;
1262
-                if ($db_conn->query($sql) == FALSE) {
1263
-                    $this->trapLog('Error create schema : '.$sql,ERROR,'');
1264
-                }
1265
-                if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array))
1266
-                {
1267
-                    $cur_table='table '.$cur_table_array[1];
1268
-                }
1269
-                else
1270
-                {
1271
-                    $cur_table='secret SQL stuff :-)';
1272
-                }
1273
-                $this->trapLog('Creating : ' . $cur_table, 3,'');
1274
-                $newline='';
1275
-            }
1260
+			{
1261
+				$sql= $newline;
1262
+				if ($db_conn->query($sql) == FALSE) {
1263
+					$this->trapLog('Error create schema : '.$sql,ERROR,'');
1264
+				}
1265
+				if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array))
1266
+				{
1267
+					$cur_table='table '.$cur_table_array[1];
1268
+				}
1269
+				else
1270
+				{
1271
+					$cur_table='secret SQL stuff :-)';
1272
+				}
1273
+				$this->trapLog('Creating : ' . $cur_table, 3,'');
1274
+				$newline='';
1275
+			}
1276 1276
 		}
1277 1277
 		
1278 1278
 		$sql= $newline;
1279 1279
 		if ($sql != '')
1280 1280
 		{
1281
-    		if ($db_conn->query($sql) == FALSE) {
1282
-    			$this->trapLog('Error create schema : '.$sql,ERROR,'');
1283
-    		}
1281
+			if ($db_conn->query($sql) == FALSE) {
1282
+				$this->trapLog('Error create schema : '.$sql,ERROR,'');
1283
+			}
1284 1284
 		}
1285 1285
 		$this->trapLog('Schema created',3);		
1286 1286
 	}
@@ -1295,113 +1295,113 @@  discard block
 block discarded – undo
1295 1295
 	 */
1296 1296
 	public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false)
1297 1297
 	{
1298
-	    // Get current db number
1299
-	    $db_conn=$this->db_connect_trap();
1300
-	    $sql='SELECT id,value from '.$this->db_prefix.'db_config WHERE name=\'db_version\' ';
1301
-	    $this->trapLog('SQL query : '.$sql,4,'');
1302
-	    if (($ret_code=$db_conn->query($sql)) == FALSE) {
1303
-	        $this->trapLog('Cannot get db version. Query : ' . $sql,2,'');
1304
-	        return;
1305
-	    }
1306
-	    $version=$ret_code->fetchAll();
1307
-	    $cur_version=$version[0]['value'];
1308
-	    $db_version_id=$version[0]['id'];
1298
+		// Get current db number
1299
+		$db_conn=$this->db_connect_trap();
1300
+		$sql='SELECT id,value from '.$this->db_prefix.'db_config WHERE name=\'db_version\' ';
1301
+		$this->trapLog('SQL query : '.$sql,4,'');
1302
+		if (($ret_code=$db_conn->query($sql)) == FALSE) {
1303
+			$this->trapLog('Cannot get db version. Query : ' . $sql,2,'');
1304
+			return;
1305
+		}
1306
+		$version=$ret_code->fetchAll();
1307
+		$cur_version=$version[0]['value'];
1308
+		$db_version_id=$version[0]['id'];
1309 1309
 	    
1310
-	    if ($this->trapDBType == 'pgsql')
1311
-	    {
1312
-	        $prefix .= 'update_pgsql/schema_';
1313
-	    }
1314
-	    else
1315
-	    {
1316
-	        $prefix .= 'update_sql/schema_';
1317
-	    }
1318
-	    //echo "version all :\n";print_r($version);echo " \n $cur_ver \n";
1319
-	    if ($getmsg == true)
1320
-	    {
1321
-	        $message='';
1322
-	        $this->trapLog('getting message for upgrade',4,'');
1323
-	        while($cur_version<$target_version)
1324
-	        {
1325
-	            $cur_version++;
1326
-	            $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql';
1327
-	            $input_stream=fopen($updateFile, 'r');
1328
-	            if ($input_stream==FALSE)
1329
-	            {
1330
-	                $this->trapLog("Error reading update file ". $updateFile,2,'');
1331
-	                return;
1332
-	            }
1333
-	            do { $line=fgets($input_stream); }
1334
-	            while ($line !== false && !preg_match('/#MESSAGE/',$line));
1335
-	            if ($line === false)
1336
-	            {
1337
-	                $this->trapLog("No message in file ". $updateFile,2,'');
1338
-	                return;
1339
-	            }
1340
-	            $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n";
1341
-	        }
1342
-	        return $message;
1343
-	    }
1344
-	    while($cur_version<$target_version)
1345
-	    { // tODO : execute pre & post scripts
1346
-	       $cur_version++;
1347
-	       $this->trapLog('Updating to version : ' .$cur_version ,3,'');
1348
-	       $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql';
1349
-	       $input_stream=fopen($updateFile, 'r');
1350
-	       if ($input_stream==FALSE)
1351
-	       {
1352
-	           $this->trapLog("Error reading update file ". $updateFile,2,'');
1353
-	           return;
1354
-	       }
1355
-	       $newline='';
1356
-	       $db_conn=$this->db_connect_trap();
1357
-	       $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
1358
-	       while (($line=fgets($input_stream)) != FALSE)
1359
-	       {
1360
-	           if (preg_match('/^#/', $line)) continue; // ignore comment lines
1361
-	           $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line));
1362
-	           if (preg_match('/; *$/', $newline))
1363
-	           {
1364
-	               $sql_req=$db_conn->prepare($newline);
1365
-	               if ($sql_req->execute() == FALSE) {
1366
-	                   $this->trapLog('Error create schema : '.$newline,1,'');
1367
-	               }
1368
-	               $cur_table_array=array();
1369
-	               if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array))
1370
-	               {
1371
-	                   $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2];
1372
-	               }
1373
-	               else
1374
-	               {
1375
-	                   $cur_table='secret SQL stuff :-)';
1376
-	                   $cur_table=$newline;
1377
-	               }
1378
-	               $this->trapLog('Doing : ' . $cur_table, 3,'');
1310
+		if ($this->trapDBType == 'pgsql')
1311
+		{
1312
+			$prefix .= 'update_pgsql/schema_';
1313
+		}
1314
+		else
1315
+		{
1316
+			$prefix .= 'update_sql/schema_';
1317
+		}
1318
+		//echo "version all :\n";print_r($version);echo " \n $cur_ver \n";
1319
+		if ($getmsg == true)
1320
+		{
1321
+			$message='';
1322
+			$this->trapLog('getting message for upgrade',4,'');
1323
+			while($cur_version<$target_version)
1324
+			{
1325
+				$cur_version++;
1326
+				$updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql';
1327
+				$input_stream=fopen($updateFile, 'r');
1328
+				if ($input_stream==FALSE)
1329
+				{
1330
+					$this->trapLog("Error reading update file ". $updateFile,2,'');
1331
+					return;
1332
+				}
1333
+				do { $line=fgets($input_stream); }
1334
+				while ($line !== false && !preg_match('/#MESSAGE/',$line));
1335
+				if ($line === false)
1336
+				{
1337
+					$this->trapLog("No message in file ". $updateFile,2,'');
1338
+					return;
1339
+				}
1340
+				$message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n";
1341
+			}
1342
+			return $message;
1343
+		}
1344
+		while($cur_version<$target_version)
1345
+		{ // tODO : execute pre & post scripts
1346
+		   $cur_version++;
1347
+		   $this->trapLog('Updating to version : ' .$cur_version ,3,'');
1348
+		   $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql';
1349
+		   $input_stream=fopen($updateFile, 'r');
1350
+		   if ($input_stream==FALSE)
1351
+		   {
1352
+			   $this->trapLog("Error reading update file ". $updateFile,2,'');
1353
+			   return;
1354
+		   }
1355
+		   $newline='';
1356
+		   $db_conn=$this->db_connect_trap();
1357
+		   $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
1358
+		   while (($line=fgets($input_stream)) != FALSE)
1359
+		   {
1360
+			   if (preg_match('/^#/', $line)) continue; // ignore comment lines
1361
+			   $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line));
1362
+			   if (preg_match('/; *$/', $newline))
1363
+			   {
1364
+				   $sql_req=$db_conn->prepare($newline);
1365
+				   if ($sql_req->execute() == FALSE) {
1366
+					   $this->trapLog('Error create schema : '.$newline,1,'');
1367
+				   }
1368
+				   $cur_table_array=array();
1369
+				   if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array))
1370
+				   {
1371
+					   $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2];
1372
+				   }
1373
+				   else
1374
+				   {
1375
+					   $cur_table='secret SQL stuff :-)';
1376
+					   $cur_table=$newline;
1377
+				   }
1378
+				   $this->trapLog('Doing : ' . $cur_table, 3,'');
1379 1379
 	               
1380
-	               $newline='';
1381
-	           }
1382
-	       }
1383
-	       fclose($input_stream);
1380
+				   $newline='';
1381
+			   }
1382
+		   }
1383
+		   fclose($input_stream);
1384 1384
 	       
1385
-	       //$sql= $newline;
1386
-	       //if ($db_conn->query($sql) == FALSE) {
1387
-	       //    $this->trapLog('Error updating schema : '.$sql,1,'');
1388
-	       //}
1385
+		   //$sql= $newline;
1386
+		   //if ($db_conn->query($sql) == FALSE) {
1387
+		   //    $this->trapLog('Error updating schema : '.$sql,1,'');
1388
+		   //}
1389 1389
 	       
1390
-	       $sql='UPDATE '.$this->db_prefix.'db_config SET value='.$cur_version.' WHERE ( id = '.$db_version_id.' )';
1391
-	       $this->trapLog('SQL query : '.$sql,4,'');
1392
-	       if (($ret_code=$db_conn->query($sql)) == FALSE) {
1393
-	           $this->trapLog('Cannot update db version. Query : ' . $sql,2,'');
1394
-	           return;
1395
-	       }
1390
+		   $sql='UPDATE '.$this->db_prefix.'db_config SET value='.$cur_version.' WHERE ( id = '.$db_version_id.' )';
1391
+		   $this->trapLog('SQL query : '.$sql,4,'');
1392
+		   if (($ret_code=$db_conn->query($sql)) == FALSE) {
1393
+			   $this->trapLog('Cannot update db version. Query : ' . $sql,2,'');
1394
+			   return;
1395
+		   }
1396 1396
 	       
1397
-	       $this->trapLog('Schema updated to version : '.$cur_version ,3);
1398
-	    }
1397
+		   $this->trapLog('Schema updated to version : '.$cur_version ,3);
1398
+		}
1399 1399
 	}
1400 1400
 	
1401 1401
 	/** reset service to OK after time defined in rule
1402
-	*	TODO logic is : get all service in error + all rules, see if getting all rules then select services is better 
1403
-	*	@return : like a plugin : status code (0->3) <message> | <perfdata>
1404
-	**/
1402
+	 *	TODO logic is : get all service in error + all rules, see if getting all rules then select services is better 
1403
+	 *	@return : like a plugin : status code (0->3) <message> | <perfdata>
1404
+	 **/
1405 1405
 	public function reset_services()
1406 1406
 	{
1407 1407
 		// Get all services not in 'ok' state
@@ -1471,105 +1471,105 @@  discard block
 block discarded – undo
1471 1471
 		$description=$db_conn->quote($description);
1472 1472
 		if (isset($this->dbOidIndex[$oid]))
1473 1473
 		{
1474
-		    if ($this->dbOidIndex[$oid]['key'] == -1)
1475
-		    { // newly created.
1476
-		        return 0;
1477
-		    }
1474
+			if ($this->dbOidIndex[$oid]['key'] == -1)
1475
+			{ // newly created.
1476
+				return 0;
1477
+			}
1478 1478
 			if ( $name != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['name'] ||
1479
-			    $mib != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['mib'] ||
1480
-			    $type != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type'] //||
1481
-			    //$textConv != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['textual_convention'] //||
1482
-			    //$dispHint != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['display_hint'] ||
1483
-			    //$syntax != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['syntax'] ||
1484
-			    //$type_enum != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type_enum'] ||
1485
-			    //$description != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['description']
1486
-			    )
1479
+				$mib != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['mib'] ||
1480
+				$type != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type'] //||
1481
+				//$textConv != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['textual_convention'] //||
1482
+				//$dispHint != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['display_hint'] ||
1483
+				//$syntax != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['syntax'] ||
1484
+				//$type_enum != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type_enum'] ||
1485
+				//$description != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['description']
1486
+				)
1487 1487
 			{ // Do update
1488
-			    $sql='UPDATE '.$this->db_prefix.'mib_cache SET '.
1489
- 			    'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. 
1490
- 			    ', syntax = :syntax, type_enum = :type_enum, description = :description '.
1491
- 			    ' WHERE id= :id';
1492
-			    $sqlQuery=$db_conn->prepare($sql);
1488
+				$sql='UPDATE '.$this->db_prefix.'mib_cache SET '.
1489
+ 				'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'. 
1490
+ 				', syntax = :syntax, type_enum = :type_enum, description = :description '.
1491
+ 				' WHERE id= :id';
1492
+				$sqlQuery=$db_conn->prepare($sql);
1493 1493
 			    
1494
-			    $sqlParam=array(
1495
-			        ':name' => $name,
1496
-			        ':type' => $type, 
1497
-			        ':mib' => $mib, 
1498
-			        ':tc' =>  ($textConv==null)?'null':$textConv , 
1499
-			        ':display_hint' => ($dispHint==null)?'null':$dispHint ,
1500
-			        ':syntax' => ($syntax==null)?'null':$syntax,
1501
-			        ':type_enum' => ($type_enum==null)?'null':$type_enum, 
1502
-			        ':description' => ($description==null)?'null':$description,
1503
-			        ':id' => $this->dbOidAll[$this->dbOidIndex[$oid]['id']]
1504
-			    );
1494
+				$sqlParam=array(
1495
+					':name' => $name,
1496
+					':type' => $type, 
1497
+					':mib' => $mib, 
1498
+					':tc' =>  ($textConv==null)?'null':$textConv , 
1499
+					':display_hint' => ($dispHint==null)?'null':$dispHint ,
1500
+					':syntax' => ($syntax==null)?'null':$syntax,
1501
+					':type_enum' => ($type_enum==null)?'null':$type_enum, 
1502
+					':description' => ($description==null)?'null':$description,
1503
+					':id' => $this->dbOidAll[$this->dbOidIndex[$oid]['id']]
1504
+				);
1505 1505
 			    
1506
-			    if ($sqlQuery->execute($sqlParam) == FALSE) {
1507
-			        $this->trapLog('Error in query : ' . $sql,ERROR,'');
1508
-			    }
1509
-			    $this->trapLog('Trap updated : '.$name . ' / OID : '.$oid,4,'');
1506
+				if ($sqlQuery->execute($sqlParam) == FALSE) {
1507
+					$this->trapLog('Error in query : ' . $sql,ERROR,'');
1508
+				}
1509
+				$this->trapLog('Trap updated : '.$name . ' / OID : '.$oid,4,'');
1510 1510
 				return 1;
1511 1511
 			}
1512 1512
 			else
1513 1513
 			{
1514
-			    $this->trapLog('Trap unchanged : '.$name . ' / OID : '.$oid,4,'');
1515
-			    return 0;
1514
+				$this->trapLog('Trap unchanged : '.$name . ' / OID : '.$oid,4,'');
1515
+				return 0;
1516 1516
 			}
1517 1517
 		}
1518
-        // create new OID.
1518
+		// create new OID.
1519 1519
 			
1520 1520
 		// Insert data
1521 1521
 
1522 1522
 		$sql='INSERT INTO '.$this->db_prefix.'mib_cache '.
1523
-		      '(oid, name, type , mib, textual_convention, display_hint '.
1524
-              ', syntax, type_enum , description ) ' . 
1525
-              'values (:oid, :name , :type ,:mib ,:tc , :display_hint'.
1526
-              ', :syntax, :type_enum, :description )';
1523
+			  '(oid, name, type , mib, textual_convention, display_hint '.
1524
+			  ', syntax, type_enum , description ) ' . 
1525
+			  'values (:oid, :name , :type ,:mib ,:tc , :display_hint'.
1526
+			  ', :syntax, :type_enum, :description )';
1527 1527
         
1528 1528
 		if ($this->trapDBType == 'pgsql') $sql .= 'RETURNING id';
1529 1529
 		
1530 1530
 		$sqlQuery=$db_conn->prepare($sql);
1531 1531
 		
1532 1532
 		$sqlParam=array(
1533
-		    ':oid' => $oid,
1534
-		    ':name' => $name,
1535
-		    ':type' => $type,
1536
-		    ':mib' => $mib,
1537
-		    ':tc' =>  ($textConv==null)?'null':$textConv ,
1538
-		    ':display_hint' => ($dispHint==null)?'null':$dispHint ,
1539
-		    ':syntax' => ($syntax==null)?'null':$syntax,
1540
-		    ':type_enum' => ($type_enum==null)?'null':$type_enum,
1541
-		    ':description' => ($description==null)?'null':$description
1533
+			':oid' => $oid,
1534
+			':name' => $name,
1535
+			':type' => $type,
1536
+			':mib' => $mib,
1537
+			':tc' =>  ($textConv==null)?'null':$textConv ,
1538
+			':display_hint' => ($dispHint==null)?'null':$dispHint ,
1539
+			':syntax' => ($syntax==null)?'null':$syntax,
1540
+			':type_enum' => ($type_enum==null)?'null':$type_enum,
1541
+			':description' => ($description==null)?'null':$description
1542 1542
 		);
1543 1543
 		
1544 1544
 		if ($sqlQuery->execute($sqlParam) == FALSE) {
1545
-		    $this->trapLog('Error in query : ' . $sql,1,'');
1545
+			$this->trapLog('Error in query : ' . $sql,1,'');
1546 1546
 		}
1547 1547
 		
1548 1548
 		switch ($this->trapDBType)
1549 1549
 		{
1550
-		    case 'pgsql':
1551
-		        // Get last id to insert oid/values in secondary table
1552
-		        if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) == FALSE) {		            
1553
-		            $this->trapLog('Error getting id - pgsql - ',1,'');
1554
-		        }
1555
-		        if (! isset($inserted_id_ret['id'])) {
1556
-		            $this->trapLog('Error getting id - pgsql - empty.',1,'');
1557
-		        }
1558
-		        $this->dbOidIndex[$oid]['id']=$inserted_id_ret['id'];
1559
-		        break;
1560
-		    case 'mysql':
1561
-		        // Get last id to insert oid/values in secondary table
1562
-		        $sql='SELECT LAST_INSERT_ID();';
1563
-		        if (($ret_code=$db_conn->query($sql)) == FALSE) {
1564
-		            $this->trapLog('Erreur getting id - mysql - ',1,'');
1565
-		        }
1550
+			case 'pgsql':
1551
+				// Get last id to insert oid/values in secondary table
1552
+				if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) == FALSE) {		            
1553
+					$this->trapLog('Error getting id - pgsql - ',1,'');
1554
+				}
1555
+				if (! isset($inserted_id_ret['id'])) {
1556
+					$this->trapLog('Error getting id - pgsql - empty.',1,'');
1557
+				}
1558
+				$this->dbOidIndex[$oid]['id']=$inserted_id_ret['id'];
1559
+				break;
1560
+			case 'mysql':
1561
+				// Get last id to insert oid/values in secondary table
1562
+				$sql='SELECT LAST_INSERT_ID();';
1563
+				if (($ret_code=$db_conn->query($sql)) == FALSE) {
1564
+					$this->trapLog('Erreur getting id - mysql - ',1,'');
1565
+				}
1566 1566
 		        
1567
-		        $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()'];
1568
-		        if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
1569
-		        $this->dbOidIndex[$oid]['id']=$inserted_id;
1570
-		        break;
1571
-		    default:
1572
-		        $this->trapLog('Error SQL type  : '.$this->trapDBType,1,'');
1567
+				$inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()'];
1568
+				if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
1569
+				$this->dbOidIndex[$oid]['id']=$inserted_id;
1570
+				break;
1571
+			default:
1572
+				$this->trapLog('Error SQL type  : '.$this->trapDBType,1,'');
1573 1573
 		}
1574 1574
 
1575 1575
 		// Set as newly created.
@@ -1577,148 +1577,148 @@  discard block
 block discarded – undo
1577 1577
 		return 2;
1578 1578
 	}
1579 1579
 
1580
-    /**
1581
-     * create or update (with check_existing = true) objects of trap
1582
-     * @param string $trapOID : trap oid
1583
-     * @param string $trapmib : mib of trap
1584
-     * @param array $objects : array of objects name (without MIB)
1585
-     * @param bool $check_existing : check instead of create
1586
-     */
1580
+	/**
1581
+	 * create or update (with check_existing = true) objects of trap
1582
+	 * @param string $trapOID : trap oid
1583
+	 * @param string $trapmib : mib of trap
1584
+	 * @param array $objects : array of objects name (without MIB)
1585
+	 * @param bool $check_existing : check instead of create
1586
+	 */
1587 1587
 	public function trap_objects($trapOID,$trapmib,$objects,$check_existing)
1588 1588
 	{
1589
-	    $dbObjects=null; // cache of objects for trap in db
1590
-	    $db_conn=$this->db_connect_trap();
1589
+		$dbObjects=null; // cache of objects for trap in db
1590
+		$db_conn=$this->db_connect_trap();
1591 1591
 	    
1592
-	    // Get id of trapmib.
1592
+		// Get id of trapmib.
1593 1593
 
1594
-	    $trapId = $this->dbOidIndex[$trapOID]['id'];
1595
-	    if ($check_existing == true)
1596
-	    {
1597
-	        // Get all objects
1598
-	        $sql='SELECT * FROM '.$this->db_prefix.'mib_cache_trap_object where trap_id='.$trapId.';';
1599
-	        $this->trapLog('SQL query get all traps: '.$sql,4,'');
1600
-	        if (($ret_code=$db_conn->query($sql)) == FALSE) {
1601
-	            $this->trapLog('No result in query : ' . $sql,1,'');
1602
-	        }
1603
-	        $dbObjectsRaw=$ret_code->fetchAll();
1594
+		$trapId = $this->dbOidIndex[$trapOID]['id'];
1595
+		if ($check_existing == true)
1596
+		{
1597
+			// Get all objects
1598
+			$sql='SELECT * FROM '.$this->db_prefix.'mib_cache_trap_object where trap_id='.$trapId.';';
1599
+			$this->trapLog('SQL query get all traps: '.$sql,4,'');
1600
+			if (($ret_code=$db_conn->query($sql)) == FALSE) {
1601
+				$this->trapLog('No result in query : ' . $sql,1,'');
1602
+			}
1603
+			$dbObjectsRaw=$ret_code->fetchAll();
1604 1604
 	        
1605
-	        foreach ($dbObjectsRaw as $val)
1606
-	        {
1607
-	            $dbObjects[$val['object_id']]=1;
1608
-	        }
1609
-	    }
1610
-	    foreach ($objects as $object)
1611
-	    {
1612
-	        $match=$snmptrans=array();
1613
-	        $retVal=0;
1614
-	        $objOid=$objTc=$objDispHint=$objSyntax=$objDesc=$objEnum=NULL;
1615
-	        $tmpdesc='';$indesc=false;
1605
+			foreach ($dbObjectsRaw as $val)
1606
+			{
1607
+				$dbObjects[$val['object_id']]=1;
1608
+			}
1609
+		}
1610
+		foreach ($objects as $object)
1611
+		{
1612
+			$match=$snmptrans=array();
1613
+			$retVal=0;
1614
+			$objOid=$objTc=$objDispHint=$objSyntax=$objDesc=$objEnum=NULL;
1615
+			$tmpdesc='';$indesc=false;
1616 1616
 	        
1617
-	        $objMib=$trapmib;
1618
-	        exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.
1619
-	            ' -On -Td '.$objMib.'::'.$object . ' 2>/dev/null',$snmptrans,$retVal);
1620
-	        if ($retVal!=0)
1621
-	        {
1622
-	            // Maybe not trap mib, search with IR
1623
-	            exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.
1624
-	                ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal);
1625
-	            if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match))
1626
-	            { // Not found -> continue with warning
1627
-	               $this->trapLog('Error finding trap object : '.$trapmib.'::'.$object,2,'');
1628
-	               continue;
1629
-	            }
1630
-	            $objMib=$match[1];
1617
+			$objMib=$trapmib;
1618
+			exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.
1619
+				' -On -Td '.$objMib.'::'.$object . ' 2>/dev/null',$snmptrans,$retVal);
1620
+			if ($retVal!=0)
1621
+			{
1622
+				// Maybe not trap mib, search with IR
1623
+				exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.
1624
+					' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal);
1625
+				if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match))
1626
+				{ // Not found -> continue with warning
1627
+				   $this->trapLog('Error finding trap object : '.$trapmib.'::'.$object,2,'');
1628
+				   continue;
1629
+				}
1630
+				$objMib=$match[1];
1631 1631
 	            
1632
-	            // Do the snmptranslate again.
1633
-	            exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.
1634
-	                ' -On -Td '.$objMib.'::'.$object,$snmptrans,$retVal);
1635
-	            if ($retVal!=0) {
1636
-	                $this->trapLog('Error finding trap object : '.$objMib.'::'.$object,2,'');
1637
-	            }
1632
+				// Do the snmptranslate again.
1633
+				exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.
1634
+					' -On -Td '.$objMib.'::'.$object,$snmptrans,$retVal);
1635
+				if ($retVal!=0) {
1636
+					$this->trapLog('Error finding trap object : '.$objMib.'::'.$object,2,'');
1637
+				}
1638 1638
 	            
1639
-	        }
1640
-	        foreach ($snmptrans as $line)
1641
-	        {
1642
-	            if ($indesc==true)
1643
-	            {
1644
-	                $line=preg_replace('/[\t ]+/',' ',$line);
1645
-	                if (preg_match('/(.*)"$/', $line,$match))
1646
-	                {
1647
-	                    $objDesc = $tmpdesc . $match[1];
1648
-	                    $indesc=false;
1649
-	                }
1650
-	                $tmpdesc.=$line;
1651
-	                continue;
1652
-	            }
1653
-	            if (preg_match('/^\.[0-9\.]+$/', $line))
1654
-	            {
1655
-	                $objOid=$line;
1656
-	                continue;
1657
-	            }
1658
-	            if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match))
1659
-	            {
1660
-	                $objSyntax=$match[1];
1661
-                    $objEnum=$match[2];
1662
-	                continue;
1663
-	            }
1664
-	            if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match))
1665
-	            {
1666
-	                $objSyntax=$match[1];
1667
-	                continue;
1668
-	            }
1669
-	            if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match))
1670
-	            {
1671
-	                $objDispHint=$match[1];
1672
-	                continue;
1673
-	            }
1674
-	            if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match))
1675
-	            {
1676
-	                $objDesc=$match[1];
1677
-	                continue;
1678
-	            }
1679
-	            if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match))
1680
-	            {
1681
-	                $tmpdesc=$match[1];
1682
-	                $indesc=true;
1683
-	                continue;
1684
-	            }
1685
-	            if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match))
1686
-	            {
1687
-	                $objTc=$match[1];
1688
-	                continue;
1689
-	            }
1690
-	        }
1691
-	        $this->trapLog("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc",4,'');
1692
-	        //echo "$object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc / $objDesc\n";
1693
-	        // Update 
1694
-	        $this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum,$objDesc);
1639
+			}
1640
+			foreach ($snmptrans as $line)
1641
+			{
1642
+				if ($indesc==true)
1643
+				{
1644
+					$line=preg_replace('/[\t ]+/',' ',$line);
1645
+					if (preg_match('/(.*)"$/', $line,$match))
1646
+					{
1647
+						$objDesc = $tmpdesc . $match[1];
1648
+						$indesc=false;
1649
+					}
1650
+					$tmpdesc.=$line;
1651
+					continue;
1652
+				}
1653
+				if (preg_match('/^\.[0-9\.]+$/', $line))
1654
+				{
1655
+					$objOid=$line;
1656
+					continue;
1657
+				}
1658
+				if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match))
1659
+				{
1660
+					$objSyntax=$match[1];
1661
+					$objEnum=$match[2];
1662
+					continue;
1663
+				}
1664
+				if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match))
1665
+				{
1666
+					$objSyntax=$match[1];
1667
+					continue;
1668
+				}
1669
+				if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match))
1670
+				{
1671
+					$objDispHint=$match[1];
1672
+					continue;
1673
+				}
1674
+				if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match))
1675
+				{
1676
+					$objDesc=$match[1];
1677
+					continue;
1678
+				}
1679
+				if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match))
1680
+				{
1681
+					$tmpdesc=$match[1];
1682
+					$indesc=true;
1683
+					continue;
1684
+				}
1685
+				if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match))
1686
+				{
1687
+					$objTc=$match[1];
1688
+					continue;
1689
+				}
1690
+			}
1691
+			$this->trapLog("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc",4,'');
1692
+			//echo "$object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc / $objDesc\n";
1693
+			// Update 
1694
+			$this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum,$objDesc);
1695 1695
             
1696
-	        if (isset($dbObjects[$this->dbOidIndex[$objOid]['id']]))
1697
-	        {   // if link exists, continue
1698
-	            $dbObjects[$this->dbOidIndex[$objOid]['id']]=2;
1699
-	            continue;
1700
-	        }
1701
-	        if ($check_existing == true) 
1702
-	        {
1703
-	            // TODO : check link trap - objects exists, mark them.
1704
-	        }
1705
-	        // Associate in object table
1706
-	        $sql='INSERT INTO '.$this->db_prefix.'mib_cache_trap_object (trap_id,object_id) '.
1707
-	   	        'values (:trap_id, :object_id)';	        
1708
-	        $sqlQuery=$db_conn->prepare($sql);	        
1709
-	        $sqlParam=array(
1710
-	            ':trap_id' => $trapId,
1711
-	            ':object_id' => $this->dbOidIndex[$objOid]['id'],
1712
-	        );
1696
+			if (isset($dbObjects[$this->dbOidIndex[$objOid]['id']]))
1697
+			{   // if link exists, continue
1698
+				$dbObjects[$this->dbOidIndex[$objOid]['id']]=2;
1699
+				continue;
1700
+			}
1701
+			if ($check_existing == true) 
1702
+			{
1703
+				// TODO : check link trap - objects exists, mark them.
1704
+			}
1705
+			// Associate in object table
1706
+			$sql='INSERT INTO '.$this->db_prefix.'mib_cache_trap_object (trap_id,object_id) '.
1707
+	   			'values (:trap_id, :object_id)';	        
1708
+			$sqlQuery=$db_conn->prepare($sql);	        
1709
+			$sqlParam=array(
1710
+				':trap_id' => $trapId,
1711
+				':object_id' => $this->dbOidIndex[$objOid]['id'],
1712
+			);
1713 1713
 	        
1714
-	        if ($sqlQuery->execute($sqlParam) == FALSE) {
1715
-	            $this->trapLog('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$objOid]['id'] ,1,'');
1716
-	        }
1717
-	    }
1718
-	    if ($check_existing == true)
1719
-	    {
1720
-	        // TODO : remove link trap - objects that wasn't marked.
1721
-	    }
1714
+			if ($sqlQuery->execute($sqlParam) == FALSE) {
1715
+				$this->trapLog('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$objOid]['id'] ,1,'');
1716
+			}
1717
+		}
1718
+		if ($check_existing == true)
1719
+		{
1720
+			// TODO : remove link trap - objects that wasn't marked.
1721
+		}
1722 1722
 	    
1723 1723
 	}
1724 1724
 	
@@ -1728,7 +1728,7 @@  discard block
 block discarded – undo
1728 1728
 	 * @param boolean $check_change : Force check of trap params & objects
1729 1729
 	 * @param boolean $onlyTraps : only cache traps and objects (true) or all (false)
1730 1730
 	 * @param string $startOID : only cache under startOID (NOT IMPLEMENTED)
1731
-	*/	
1731
+	 */	
1732 1732
 	public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1')
1733 1733
 	{
1734 1734
 		// Timing 
@@ -1780,7 +1780,7 @@  discard block
 block discarded – undo
1780 1780
 		
1781 1781
 		for ($curElement=0;$curElement < $numElements;$curElement++)
1782 1782
 		{
1783
-		    $time_1= microtime(true);
1783
+			$time_1= microtime(true);
1784 1784
 			if ((microtime(true)-$timeFiveSec) > 2 && $display_progress)
1785 1785
 			{ // echo a . every 2 sec
1786 1786
 				echo '.';
@@ -1798,8 +1798,8 @@  discard block
 block discarded – undo
1798 1798
 			// Get oid or pass if not found
1799 1799
 			if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement]))
1800 1800
 			{
1801
-			    $time_parse1 += microtime(true) - $time_1;
1802
-			    $time_parse1N ++;
1801
+				$time_parse1 += microtime(true) - $time_1;
1802
+				$time_parse1N ++;
1803 1803
 				continue;
1804 1804
 			}
1805 1805
 			$oid=$this->objectsAll[$curElement];
@@ -1810,7 +1810,7 @@  discard block
 block discarded – undo
1810 1810
 			if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/',
1811 1811
 						$this->objectsAll[$curElement],$match))
1812 1812
 			{
1813
-			    $time_check1 += microtime(true) - $time_1;
1813
+				$time_check1 += microtime(true) - $time_1;
1814 1814
 				$time_check1N++;
1815 1815
 				continue;
1816 1816
 			}
@@ -1823,8 +1823,8 @@  discard block
 block discarded – undo
1823 1823
 				// Check if next is suboid -> in that case is cannot be a trap
1824 1824
 				if (preg_match("/^$oid/",$this->objectsAll[$curElement+1]))
1825 1825
 				{
1826
-				    $time_check2 += microtime(true) - $time_1;
1827
-				    $time_check2N++;
1826
+					$time_check2 += microtime(true) - $time_1;
1827
+					$time_check2N++;
1828 1828
 					continue;
1829 1829
 				}		
1830 1830
 				unset($snmptrans);
@@ -1832,8 +1832,8 @@  discard block
 block discarded – undo
1832 1832
 					' -Td '.$oid . ' | grep OBJECTS ',$snmptrans,$retVal);
1833 1833
 				if ($retVal!=0)
1834 1834
 				{
1835
-				    $time_check2 += microtime(true) - $time_1;
1836
-				    $time_check2N++;
1835
+					$time_check2 += microtime(true) - $time_1;
1836
+					$time_check2N++;
1837 1837
 					continue;
1838 1838
 				}
1839 1839
 				//echo "\n v1 trap found : $oid \n";
@@ -1842,7 +1842,7 @@  discard block
 block discarded – undo
1842 1842
 			}
1843 1843
 			if ($onlyTraps==true && $type!=21) // if only traps and not a trap, continue
1844 1844
 			{
1845
-			    $time_check3 += microtime(true) - $time_1;
1845
+				$time_check3 += microtime(true) - $time_1;
1846 1846
 				$time_check3N++;
1847 1847
 				continue;
1848 1848
 			}
@@ -1863,7 +1863,7 @@  discard block
 block discarded – undo
1863 1863
 			
1864 1864
 			if (!preg_match('/^(.*)::/',$snmptrans[0],$match))
1865 1865
 			{
1866
-			    $this->trapLog('Error getting mib from trap '.$oid.' : ' . $snmptrans[0],1,'');
1866
+				$this->trapLog('Error getting mib from trap '.$oid.' : ' . $snmptrans[0],1,'');
1867 1867
 			}
1868 1868
 			$trapMib=$match[1];
1869 1869
 			
@@ -1871,17 +1871,17 @@  discard block
 block discarded – undo
1871 1871
 			while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++;
1872 1872
 			if (isset($snmptrans[$numLine]))
1873 1873
 			{
1874
-			    $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]);
1874
+				$snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]);
1875 1875
 
1876
-			    while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine]))
1877
-			    {
1878
-			        $trapDesc.=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]);
1879
-			        $numLine++;
1880
-			    }
1881
-			    if (isset($snmptrans[$numLine])) {
1882
-			        $trapDesc.=preg_replace('/".*/','',$snmptrans[$numLine]);
1883
-			        $trapDesc=preg_replace('/[\t ]+/',' ',$trapDesc);
1884
-			    }
1876
+				while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine]))
1877
+				{
1878
+					$trapDesc.=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]);
1879
+					$numLine++;
1880
+				}
1881
+				if (isset($snmptrans[$numLine])) {
1882
+					$trapDesc.=preg_replace('/".*/','',$snmptrans[$numLine]);
1883
+					$trapDesc=preg_replace('/[\t ]+/',' ',$trapDesc);
1884
+				}
1885 1885
 
1886 1886
 			}
1887 1887
 			$update=$this->update_oid($oid,$trapMib,$name,$type,NULL,NULL,NULL,NULL,$trapDesc);
@@ -1889,23 +1889,23 @@  discard block
 block discarded – undo
1889 1889
 			
1890 1890
 			if (($update==0) && ($check_change==false))
1891 1891
 			{ // Trapd didn't change & force check disabled
1892
-			    $time_objects += microtime(true) - $time_1;
1893
-			    if ($display_progress) echo "C";
1894
-			    continue;
1892
+				$time_objects += microtime(true) - $time_1;
1893
+				if ($display_progress) echo "C";
1894
+				continue;
1895 1895
 			}
1896 1896
 			
1897 1897
 			$synt=null;
1898 1898
 			foreach ($snmptrans as $line)
1899 1899
 			{	
1900
-    			if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match))
1901
-    				{
1902
-    					$synt=$match[1];
1903
-    				}
1900
+				if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match))
1901
+					{
1902
+						$synt=$match[1];
1903
+					}
1904 1904
 			}
1905 1905
 			if ($synt == null) 
1906 1906
 			{
1907 1907
 				//echo "No objects for $trapOID\n";
1908
-			    $time_objects += microtime(true) - $time_1;
1908
+				$time_objects += microtime(true) - $time_1;
1909 1909
 				continue;
1910 1910
 			}
1911 1911
 			//echo "$synt \n";
@@ -1924,18 +1924,18 @@  discard block
 block discarded – undo
1924 1924
 		
1925 1925
 		if ($display_progress)
1926 1926
 		{
1927
-    		echo "\nNumber of processed traps : $time_num_traps \n";
1928
-    		echo "\nParsing : " . number_format($time_parse1+$time_check1,1) ." sec / " . ($time_parse1N+ $time_check1N)  . " occurences\n";
1929
-    		echo "Detecting traps : " . number_format($time_check2+$time_check3,1) . " sec / " . ($time_check2N+$time_check3N) ." occurences\n";
1930
-    		echo "Trap processing ($time_updateN): ".number_format($time_update,1)." sec , ";
1931
-    		echo "Objects processing ($time_objectsN) : ".number_format($time_objects,1)." sec \n";
1927
+			echo "\nNumber of processed traps : $time_num_traps \n";
1928
+			echo "\nParsing : " . number_format($time_parse1+$time_check1,1) ." sec / " . ($time_parse1N+ $time_check1N)  . " occurences\n";
1929
+			echo "Detecting traps : " . number_format($time_check2+$time_check3,1) . " sec / " . ($time_check2N+$time_check3N) ." occurences\n";
1930
+			echo "Trap processing ($time_updateN): ".number_format($time_update,1)." sec , ";
1931
+			echo "Objects processing ($time_objectsN) : ".number_format($time_objects,1)." sec \n";
1932 1932
 		}
1933 1933
 		
1934 1934
 		// Timing ends
1935 1935
 		$timeTaken=microtime(true) - $timeTaken;
1936 1936
 		if ($display_progress)
1937 1937
 		{
1938
-		    echo "Global time : ".round($timeTaken)." seconds\n";
1938
+			echo "Global time : ".round($timeTaken)." seconds\n";
1939 1939
 		}
1940 1940
 		
1941 1941
 	}
Please login to merge, or discard this patch.
Spacing   +431 added lines, -431 removed lines patch added patch discarded remove patch
@@ -7,7 +7,7 @@  discard block
 block discarded – undo
7 7
 
8 8
 use Icinga\Module\Trapdirector\Icinga2API;
9 9
 
10
-define("ERROR", 1);define("WARN", 2);define("INFO", 3);define("DEBUG", 4);
10
+define("ERROR", 1); define("WARN", 2); define("INFO", 3); define("DEBUG", 4);
11 11
 
12 12
 class Trap
13 13
 {
@@ -31,10 +31,10 @@  discard block
 block discarded – undo
31 31
 
32 32
 	//**** Options from config database
33 33
 	// Logs 
34
-	protected $debug_level=2;  // 0=No output 1=critical 2=warning 3=trace 4=ALL
34
+	protected $debug_level=2; // 0=No output 1=critical 2=warning 3=trace 4=ALL
35 35
 	protected $alert_output='display'; // alert type : file, syslog, display
36 36
 	protected $debug_file="/tmp/trapdebug.txt";
37
-	protected $debug_text=array("","Error","Warning","Info","Debug");
37
+	protected $debug_text=array("", "Error", "Warning", "Info", "Debug");
38 38
 	
39 39
 	//**** End options from database
40 40
 	
@@ -86,15 +86,15 @@  discard block
 block discarded – undo
86 86
 	 * @param string $message warning message if not found
87 87
 	 * @return boolean true if found, or false
88 88
 	 */
89
-	protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = 2, $message = null)
89
+	protected function getOptionIfSet($option_array, $option_category, $option_name, &$option_var, $log_level=2, $message=null)
90 90
 	{
91 91
 	    if (!isset($option_array[$option_category][$option_name]))
92 92
 	    {
93 93
 	        if ($message === null)
94 94
 	        {
95
-	            $message='No ' . $option_name . ' in config file: '. $this->trap_module_config;
95
+	            $message='No '.$option_name.' in config file: '.$this->trap_module_config;
96 96
 	        }
97
-	        $this->trapLog($message,$log_level,'syslog');
97
+	        $this->trapLog($message, $log_level, 'syslog');
98 98
 	        return false;
99 99
 	    }
100 100
 	    else
@@ -108,40 +108,40 @@  discard block
 block discarded – undo
108 108
 	*/
109 109
 	protected function getOptions()
110 110
 	{
111
-		$trap_config=parse_ini_file($this->trap_module_config,true);
111
+		$trap_config=parse_ini_file($this->trap_module_config, true);
112 112
 		if ($trap_config == false) 
113 113
 		{
114
-			$this->trapLog("Error reading ini file : ".$this->trap_module_config,ERROR,'syslog'); 
114
+			$this->trapLog("Error reading ini file : ".$this->trap_module_config, ERROR, 'syslog'); 
115 115
 		}
116 116
 		// Snmptranslate binary path
117
-		$this->getOptionIfSet($trap_config,'config','snmptranslate', $this->snmptranslate);
117
+		$this->getOptionIfSet($trap_config, 'config', 'snmptranslate', $this->snmptranslate);
118 118
 
119 119
 		// mibs path
120
-		$this->getOptionIfSet($trap_config,'config','snmptranslate_dirs', $this->snmptranslate_dirs);
120
+		$this->getOptionIfSet($trap_config, 'config', 'snmptranslate_dirs', $this->snmptranslate_dirs);
121 121
 
122 122
 		// icinga2cmd path
123
-		$this->getOptionIfSet($trap_config,'config','icingacmd', $this->icinga2cmd);
123
+		$this->getOptionIfSet($trap_config, 'config', 'icingacmd', $this->icinga2cmd);
124 124
 		
125 125
 		// table prefix
126
-		$this->getOptionIfSet($trap_config,'config','database_prefix', $this->db_prefix);
126
+		$this->getOptionIfSet($trap_config, 'config', 'database_prefix', $this->db_prefix);
127 127
 
128 128
 		// API options
129
-		if ($this->getOptionIfSet($trap_config,'config','icingaAPI_host', $this->api_hostname))
129
+		if ($this->getOptionIfSet($trap_config, 'config', 'icingaAPI_host', $this->api_hostname))
130 130
 		{
131 131
 		    $this->api_use=true;
132
-		    $this->getOptionIfSet($trap_config,'config','icingaAPI_port', $this->api_port);
133
-		    $this->getOptionIfSet($trap_config,'config','icingaAPI_user', $this->api_username);
134
-		    $this->getOptionIfSet($trap_config,'config','icingaAPI_password', $this->api_password);
132
+		    $this->getOptionIfSet($trap_config, 'config', 'icingaAPI_port', $this->api_port);
133
+		    $this->getOptionIfSet($trap_config, 'config', 'icingaAPI_user', $this->api_username);
134
+		    $this->getOptionIfSet($trap_config, 'config', 'icingaAPI_password', $this->api_password);
135 135
 		}
136 136
 				
137 137
 		/***** Database options :  ***/
138
-		$this->getDBConfigIfSet('log_level',$this->debug_level);
139
-		$this->getDBConfigIfSet('log_destination',$this->alert_output);
140
-		$this->getDBConfigIfSet('log_file',$this->debug_file);
138
+		$this->getDBConfigIfSet('log_level', $this->debug_level);
139
+		$this->getDBConfigIfSet('log_destination', $this->alert_output);
140
+		$this->getDBConfigIfSet('log_file', $this->debug_file);
141 141
 		$this->getAPI();
142 142
 	}
143 143
 
144
-	protected function getDBConfigIfSet($element,&$variable)
144
+	protected function getDBConfigIfSet($element, &$variable)
145 145
 	{
146 146
 		$value=$this->getDBConfig($element);
147 147
 		if ($value != 'null') $variable=$value;
@@ -156,7 +156,7 @@  discard block
 block discarded – undo
156 156
 		$db_conn=$this->db_connect_trap();
157 157
 		$sql='SELECT value from '.$this->db_prefix.'db_config WHERE ( name=\''.$element.'\' )';
158 158
 		if (($ret_code=$db_conn->query($sql)) == FALSE) {
159
-			$this->trapLog('No result in query : ' . $sql,WARN,'');
159
+			$this->trapLog('No result in query : '.$sql, WARN, '');
160 160
 			return null;
161 161
 		}
162 162
 		$value=$ret_code->fetch();
@@ -173,29 +173,29 @@  discard block
 block discarded – undo
173 173
 	*	@param  int $destination file/syslog/display
174 174
 	*	@return void
175 175
 	**/	
176
-	public function trapLog( $message, $level, $destination ='')
176
+	public function trapLog($message, $level, $destination='')
177 177
 	{	
178 178
 		if ($this->debug_level >= $level) 
179 179
 		{
180
-			$message = '['.  date("Y/m/d H:i:s") . '] ' .
181
-				'['. basename(__FILE__) . '] ['.$this->debug_text[$level].']: ' .$message . "\n";
180
+			$message='['.date("Y/m/d H:i:s").'] '.
181
+				'['.basename(__FILE__).'] ['.$this->debug_text[$level].']: '.$message."\n";
182 182
 			
183
-			if ( $destination != '' ) $output=$destination;
183
+			if ($destination != '') $output=$destination;
184 184
 			else $output=$this->alert_output;
185 185
 			switch ($output)
186 186
 			{
187 187
 				case 'file':
188
-					file_put_contents ($this->debug_file, $message , FILE_APPEND);
188
+					file_put_contents($this->debug_file, $message, FILE_APPEND);
189 189
 					break;
190 190
 				case 'syslog':
191
-					switch($level)
191
+					switch ($level)
192 192
 					{
193
-						case 1 : $prio = LOG_ERR;break;
194
-						case 2 : $prio = LOG_WARNING;break;
195
-						case 3 : $prio = LOG_INFO;break;
196
-						case 4 : $prio = LOG_DEBUG;break;
193
+						case 1 : $prio=LOG_ERR; break;
194
+						case 2 : $prio=LOG_WARNING; break;
195
+						case 3 : $prio=LOG_INFO; break;
196
+						case 4 : $prio=LOG_DEBUG; break;
197 197
 					}
198
-					syslog($prio,$message);
198
+					syslog($prio, $message);
199 199
 					break;
200 200
 				case 'display':
201 201
 					echo $message;
@@ -210,7 +210,7 @@  discard block
 block discarded – undo
210 210
 		}
211 211
 	}
212 212
 	
213
-	public function setLogging($debug_lvl,$output_type,$output_option=null)
213
+	public function setLogging($debug_lvl, $output_type, $output_option=null)
214 214
 	{
215 215
 		$this->debug_level=$debug_lvl;
216 216
 		switch ($output_type)
@@ -227,7 +227,7 @@  discard block
 block discarded – undo
227 227
 				$this->alert_output='display';
228 228
 				break;
229 229
 			default : // syslog should always work....
230
-				$this->trapLog("Error in log output : ".$output_type,ERROR,'syslog');
230
+				$this->trapLog("Error in log output : ".$output_type, ERROR, 'syslog');
231 231
 		}
232 232
 	}
233 233
 	
@@ -235,7 +235,7 @@  discard block
 block discarded – undo
235 235
 	{
236 236
 	    if ($this->icinga2api == null)
237 237
 	    {
238
-	        $this->icinga2api = new Icinga2API($this->api_hostname,$this->api_port);
238
+	        $this->icinga2api=new Icinga2API($this->api_hostname, $this->api_port);
239 239
 	    }
240 240
 	    return $this->icinga2api;
241 241
 	}
@@ -254,7 +254,7 @@  discard block
 block discarded – undo
254 254
 		        // select 1 failed, try to reconnect.
255 255
 		        $this->trapDB=null;
256 256
 				$this->trapDB=$this->db_connect('traps');
257
-		        $this->trapLog('Database connection lost, reconnecting',WARN,'');
257
+		        $this->trapLog('Database connection lost, reconnecting', WARN, '');
258 258
 				return $this->trapDB;
259 259
 		    }
260 260
 		     
@@ -281,14 +281,14 @@  discard block
 block discarded – undo
281 281
 	protected function db_connect($database) {
282 282
 		$confarray=$this->get_database($database);
283 283
 		//	$dsn = 'mysql:dbname=traps;host=127.0.0.1';
284
-		$dsn= $confarray[0].':dbname='.$confarray[2].';host='.$confarray[1];
285
-		$user = $confarray[3];
286
-		$password = $confarray[4];
287
-		$this->trapLog('DSN : '.$dsn,3);
284
+		$dsn=$confarray[0].':dbname='.$confarray[2].';host='.$confarray[1];
285
+		$user=$confarray[3];
286
+		$password=$confarray[4];
287
+		$this->trapLog('DSN : '.$dsn, 3);
288 288
 		try {
289
-			$dbh = new PDO($dsn, $user, $password);
289
+			$dbh=new PDO($dsn, $user, $password);
290 290
 		} catch (PDOException $e) {
291
-			$this->trapLog('Connection failed : ' . $e->getMessage(),ERROR,'');
291
+			$this->trapLog('Connection failed : '.$e->getMessage(), ERROR, '');
292 292
 		}
293 293
 		return $dbh;
294 294
 	}
@@ -299,16 +299,16 @@  discard block
 block discarded – undo
299 299
 	**/
300 300
 	protected function get_database($database) {
301 301
 
302
-		$trap_config=parse_ini_file($this->trap_module_config,true);
302
+		$trap_config=parse_ini_file($this->trap_module_config, true);
303 303
 		if ($trap_config == false) 
304 304
 		{
305
-			$this->trapLog("Error reading ini file : ".$this->trap_module_config,ERROR,''); 
305
+			$this->trapLog("Error reading ini file : ".$this->trap_module_config, ERROR, ''); 
306 306
 		}
307 307
 		if ($database == 'traps')
308 308
 		{
309 309
 			if (!isset($trap_config['config']['database'])) 
310 310
 			{
311
-				$this->trapLog("No Config/database in config file: ".$this->trap_module_config,ERROR,''); 
311
+				$this->trapLog("No Config/database in config file: ".$this->trap_module_config, ERROR, ''); 
312 312
 			}
313 313
 			$db_name=$trap_config['config']['database'];
314 314
 		} 
@@ -316,34 +316,34 @@  discard block
 block discarded – undo
316 316
 		{
317 317
 			if (!isset($trap_config['config']['IDOdatabase'])) 
318 318
 			{
319
-				$this->trapLog("No Config/IDOdatabase in config file: ".$this->trap_module_config,ERROR,''); 
319
+				$this->trapLog("No Config/IDOdatabase in config file: ".$this->trap_module_config, ERROR, ''); 
320 320
 			}
321 321
 			$db_name=$trap_config['config']['IDOdatabase'];		
322 322
 		}
323 323
 		else
324 324
 		{
325
-			$this->trapLog("Unknown database type : ".$database,ERROR,''); 		
325
+			$this->trapLog("Unknown database type : ".$database, ERROR, ''); 		
326 326
 		}	
327
-		$this->trapLog("Found database in config file: ".$db_name,3,''); 
328
-		$db_config=parse_ini_file($this->icingaweb2_ressources,true);
327
+		$this->trapLog("Found database in config file: ".$db_name, 3, ''); 
328
+		$db_config=parse_ini_file($this->icingaweb2_ressources, true);
329 329
 		if ($db_config == false) 
330 330
 		{
331
-			$this->trapLog("Error reading ini file : ".$this->icingaweb2_ressources,ERROR,''); 
331
+			$this->trapLog("Error reading ini file : ".$this->icingaweb2_ressources, ERROR, ''); 
332 332
 		}
333 333
 		if (!isset($db_config[$db_name])) 
334 334
 		{
335
-			$this->trapLog("No Config/database in config file: ".$this->icingaweb2_ressources,ERROR,''); 
335
+			$this->trapLog("No Config/database in config file: ".$this->icingaweb2_ressources, ERROR, ''); 
336 336
 		}
337 337
 		$db_type=$db_config[$db_name]['db'];
338 338
 		$db_host=$db_config[$db_name]['host'];
339 339
 		$db_sql_name=$db_config[$db_name]['dbname'];
340 340
 		$db_user=$db_config[$db_name]['username'];
341 341
 		$db_pass=$db_config[$db_name]['password'];
342
-		if ($database == 'traps') $this->trapDBType = $db_type;
343
-		if ($database == 'ido') $this->idoDBType = $db_type;
342
+		if ($database == 'traps') $this->trapDBType=$db_type;
343
+		if ($database == 'ido') $this->idoDBType=$db_type;
344 344
 		
345
-		$this->trapLog( "DB selected : $db_type $db_host $db_sql_name $db_user",3,''); 
346
-		return array($db_type,$db_host,$db_sql_name,$db_user,$db_pass);
345
+		$this->trapLog("DB selected : $db_type $db_host $db_sql_name $db_user", 3, ''); 
346
+		return array($db_type, $db_host, $db_sql_name, $db_user, $db_pass);
347 347
 	}	
348 348
 	
349 349
 	/** read data from stream
@@ -355,10 +355,10 @@  discard block
 block discarded – undo
355 355
 		//Read data from snmptrapd from stdin
356 356
 		$input_stream=fopen($stream, 'r');
357 357
 
358
-		if ($input_stream==FALSE)
358
+		if ($input_stream == FALSE)
359 359
 		{
360 360
 		    $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)");
361
-			$this->trapLog("Error reading stdin !",ERROR,''); 
361
+			$this->trapLog("Error reading stdin !", ERROR, ''); 
362 362
 		}
363 363
 
364 364
 		// line 1 : host
@@ -366,21 +366,21 @@  discard block
 block discarded – undo
366 366
 		if ($this->receivingHost === false)
367 367
 		{
368 368
 		    $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)");
369
-			$this->trapLog("Error reading Host !",ERROR,''); 
369
+			$this->trapLog("Error reading Host !", ERROR, ''); 
370 370
 		}
371 371
 		// line 2 IP:port=>IP:port
372 372
 		$IP=chop(fgets($input_stream));
373 373
 		if ($IP === false)
374 374
 		{
375 375
 		    $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)");
376
-			$this->trapLog("Error reading IP !",ERROR,''); 
376
+			$this->trapLog("Error reading IP !", ERROR, ''); 
377 377
 		}
378 378
 		$matches=array();
379
-		$ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches);
380
-		if ($ret_code===0 || $ret_code==false) 
379
+		$ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/', $IP, $matches);
380
+		if ($ret_code === 0 || $ret_code == false) 
381 381
 		{
382 382
 		    $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)");
383
-			$this->trapLog('Error parsing IP : '.$IP,ERROR,'');
383
+			$this->trapLog('Error parsing IP : '.$IP, ERROR, '');
384 384
 		} 
385 385
 		else 
386 386
 		{		
@@ -390,33 +390,33 @@  discard block
 block discarded – undo
390 390
 			$this->trap_data['destination_port']=$matches[4];
391 391
 		}
392 392
 
393
-		while (($vars=chop(fgets($input_stream))) !=false)
393
+		while (($vars=chop(fgets($input_stream))) != false)
394 394
 		{
395
-			$ret_code=preg_match('/^([^ ]+) (.*)$/',$vars,$matches);
396
-			if ($ret_code===0 || $ret_code===false) 
395
+			$ret_code=preg_match('/^([^ ]+) (.*)$/', $vars, $matches);
396
+			if ($ret_code === 0 || $ret_code === false) 
397 397
 			{
398
-				$this->trapLog('No match on trap data : '.$vars,WARN,'');
398
+				$this->trapLog('No match on trap data : '.$vars, WARN, '');
399 399
 			}
400 400
 			else 
401 401
 			{
402
-			    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'))
402
+			    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'))
403 403
 				{
404 404
 					$this->trap_data['trap_oid']=$matches[2];				
405 405
 				}
406 406
 				else
407 407
 				{
408
-					$object= new stdClass;
409
-					$object->oid =$matches[1];
410
-					$object->value = $matches[2];
411
-					array_push($this->trap_data_ext,$object);
408
+					$object=new stdClass;
409
+					$object->oid=$matches[1];
410
+					$object->value=$matches[2];
411
+					array_push($this->trap_data_ext, $object);
412 412
 				}
413 413
 			}
414 414
 		}
415 415
 
416
-		if ($this->trap_data['trap_oid']=='unknown') 
416
+		if ($this->trap_data['trap_oid'] == 'unknown') 
417 417
 		{
418
-		    $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trap_data['source_ip']);
419
-			$this->trapLog('no trap oid found',ERROR,'');
418
+		    $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)", $this->trap_data['source_ip']);
419
+			$this->trapLog('no trap oid found', ERROR, '');
420 420
 		} 
421 421
 
422 422
 		// Translate oids.
@@ -438,7 +438,7 @@  discard block
 block discarded – undo
438 438
 		}
439 439
 		
440 440
 
441
-		$this->trap_data['status']= 'waiting';
441
+		$this->trap_data['status']='waiting';
442 442
 		
443 443
 		return $this->trap_data;
444 444
 	}
@@ -453,40 +453,40 @@  discard block
 block discarded – undo
453 453
 		$db_conn=$this->db_connect_trap();
454 454
 		
455 455
 		$sql='SELECT mib,name from '.$this->db_prefix.'mib_cache WHERE oid=\''.$oid.'\';';
456
-		$this->trapLog('SQL query : '.$sql,4,'');
456
+		$this->trapLog('SQL query : '.$sql, 4, '');
457 457
 		if (($ret_code=$db_conn->query($sql)) == FALSE) {
458
-			$this->trapLog('No result in query : ' . $sql,ERROR,'');
458
+			$this->trapLog('No result in query : '.$sql, ERROR, '');
459 459
 		}
460 460
 		$name=$ret_code->fetch();
461 461
 		if ($name['name'] != null)
462 462
 		{
463
-			return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']);
463
+			return array('trap_name_mib'=>$name['mib'], 'trap_name'=>$name['name']);
464 464
 		}
465 465
 		
466 466
 		// Also check if it is an instance of OID
467
-		$oid_instance=preg_replace('/\.[0-9]+$/','',$oid);
467
+		$oid_instance=preg_replace('/\.[0-9]+$/', '', $oid);
468 468
 		
469 469
 		$sql='SELECT mib,name from '.$this->db_prefix.'mib_cache WHERE oid=\''.$oid_instance.'\';';
470
-		$this->trapLog('SQL query : '.$sql,4,'');
470
+		$this->trapLog('SQL query : '.$sql, 4, '');
471 471
 		if (($ret_code=$db_conn->query($sql)) == FALSE) {
472
-			$this->trapLog('No result in query : ' . $sql,ERROR,'');
472
+			$this->trapLog('No result in query : '.$sql, ERROR, '');
473 473
 		}
474 474
 		$name=$ret_code->fetch();
475 475
 		if ($name['name'] != null)
476 476
 		{
477
-			return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']);
477
+			return array('trap_name_mib'=>$name['mib'], 'trap_name'=>$name['name']);
478 478
 		}
479 479
 		
480 480
 		// Try to get oid name from snmptranslate
481
-		$translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.
481
+		$translate=exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs.
482 482
 		    ' '.$oid);
483 483
 		$matches=array();
484
-		$ret_code=preg_match('/(.*)::(.*)/',$translate,$matches);
485
-		if ($ret_code===0 || $ret_code===FALSE) {
484
+		$ret_code=preg_match('/(.*)::(.*)/', $translate, $matches);
485
+		if ($ret_code === 0 || $ret_code === FALSE) {
486 486
 			return NULL;
487 487
 		} else {
488
-			$this->trapLog('Found name with snmptrapd and not in DB for oid='.$oid,INFO,'');
489
-			return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]);
488
+			$this->trapLog('Found name with snmptrapd and not in DB for oid='.$oid, INFO, '');
489
+			return array('trap_name_mib'=>$matches[1], 'trap_name'=>$matches[2]);
490 490
 		}	
491 491
 	}
492 492
 	
@@ -496,26 +496,26 @@  discard block
 block discarded – undo
496 496
 	**/
497 497
 	public function eraseOldTraps($days=0)
498 498
 	{
499
-		if ($days==0)
499
+		if ($days == 0)
500 500
 		{
501 501
 			if (($days=$this->getDBConfig('db_remove_days')) == null)
502 502
 			{
503
-				$this->trapLog('No days specified & no db value : no tap erase' ,WARN,'');
503
+				$this->trapLog('No days specified & no db value : no tap erase', WARN, '');
504 504
 				return;
505 505
 			}
506 506
 		}
507 507
 		$db_conn=$this->db_connect_trap();
508
-		$daysago = strtotime("-".$days." day");
509
-		$sql= 'delete from '.$this->db_prefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';';
508
+		$daysago=strtotime("-".$days." day");
509
+		$sql='delete from '.$this->db_prefix.'received where date_received < \''.date("Y-m-d H:i:s", $daysago).'\';';
510 510
 		if ($db_conn->query($sql) == FALSE) {
511
-			$this->trapLog('Error erasing traps : '.$sql,ERROR,'');
511
+			$this->trapLog('Error erasing traps : '.$sql, ERROR, '');
512 512
 		}
513
-		$this->trapLog('Erased traps older than '.$days.' day(s) : '.$sql,3);
513
+		$this->trapLog('Erased traps older than '.$days.' day(s) : '.$sql, 3);
514 514
 	}
515 515
 
516 516
 	/** Write error to received trap database
517 517
 	 */
518
-	public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null)
518
+	public function writeTrapErrorToDB($message, $sourceIP=null, $trapoid=null)
519 519
 	{
520 520
 	    
521 521
 	    $db_conn=$this->db_connect_trap();
@@ -523,65 +523,65 @@  discard block
 block discarded – undo
523 523
 	    $insert_col='';
524 524
 	    $insert_val='';
525 525
 	    // add date time
526
-	    $insert_col ='date_received,status';
527
-	    $insert_val = "'" . date("Y-m-d H:i:s")."','error'";
526
+	    $insert_col='date_received,status';
527
+	    $insert_val="'".date("Y-m-d H:i:s")."','error'";
528 528
         
529
-	    if ($sourceIP !=null)
529
+	    if ($sourceIP != null)
530 530
 	    {
531
-	        $insert_col .=',source_ip';
532
-	        $insert_val .=",'". $sourceIP ."'";
531
+	        $insert_col.=',source_ip';
532
+	        $insert_val.=",'".$sourceIP."'";
533 533
 	    }
534
-	    if ($trapoid !=null)
534
+	    if ($trapoid != null)
535 535
 	    {
536
-	        $insert_col .=',trap_oid';
537
-	        $insert_val .=",'". $trapoid ."'";
536
+	        $insert_col.=',trap_oid';
537
+	        $insert_val.=",'".$trapoid."'";
538 538
 	    }
539
-	    $insert_col .=',status_detail';
540
-	    $insert_val .=",'". $message ."'";
539
+	    $insert_col.=',status_detail';
540
+	    $insert_val.=",'".$message."'";
541 541
 	    
542
-	    $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')';
542
+	    $sql='INSERT INTO '.$this->db_prefix.'received ('.$insert_col.') VALUES ('.$insert_val.')';
543 543
 	    
544 544
 	    switch ($this->trapDBType)
545 545
 	    {
546 546
 	        case 'pgsql':
547
-	            $sql .= ' RETURNING id;';
548
-	            $this->trapLog('sql : '.$sql,3,'');
547
+	            $sql.=' RETURNING id;';
548
+	            $this->trapLog('sql : '.$sql, 3, '');
549 549
 	            if (($ret_code=$db_conn->query($sql)) == FALSE) {
550
-	                $this->trapLog('Error SQL insert : '.$sql,1,'');
550
+	                $this->trapLog('Error SQL insert : '.$sql, 1, '');
551 551
 	            }
552
-	            $this->trapLog('SQL insertion OK',3,'');
552
+	            $this->trapLog('SQL insertion OK', 3, '');
553 553
 	            // Get last id to insert oid/values in secondary table
554 554
 	            if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) == FALSE) {
555 555
 	                
556
-	                $this->trapLog('Erreur recuperation id',1,'');
556
+	                $this->trapLog('Erreur recuperation id', 1, '');
557 557
 	            }
558
-	            if (! isset($inserted_id_ret['id'])) {
559
-	                $this->trapLog('Error getting id',1,'');
558
+	            if (!isset($inserted_id_ret['id'])) {
559
+	                $this->trapLog('Error getting id', 1, '');
560 560
 	            }
561 561
 	            $this->trap_id=$inserted_id_ret['id'];
562 562
 	            break;
563 563
 	        case 'mysql':
564
-	            $sql .= ';';
565
-	            $this->trapLog('sql : '.$sql,3,'');
564
+	            $sql.=';';
565
+	            $this->trapLog('sql : '.$sql, 3, '');
566 566
 	            if (($ret_code=$db_conn->query($sql)) == FALSE) {
567
-	                $this->trapLog('Error SQL insert : '.$sql,1,'');
567
+	                $this->trapLog('Error SQL insert : '.$sql, 1, '');
568 568
 	            }
569
-	            $this->trapLog('SQL insertion OK',3,'');
569
+	            $this->trapLog('SQL insertion OK', 3, '');
570 570
 	            // Get last id to insert oid/values in secondary table
571 571
 	            $sql='SELECT LAST_INSERT_ID();';
572 572
 	            if (($ret_code=$db_conn->query($sql)) == FALSE) {
573
-	                $this->trapLog('Erreur recuperation id',1,'');
573
+	                $this->trapLog('Erreur recuperation id', 1, '');
574 574
 	            }
575 575
 	            
576 576
 	            $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()'];
577
-	            if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
577
+	            if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
578 578
 	            $this->trap_id=$inserted_id;
579 579
 	            break;
580 580
 	        default:
581
-	            $this->trapLog('Error SQL type  : '.$this->trapDBType,1,'');
581
+	            $this->trapLog('Error SQL type  : '.$this->trapDBType, 1, '');
582 582
 	    }
583 583
 	    
584
-	    $this->trapLog('id found: '. $this->trap_id,3,'');    
584
+	    $this->trapLog('id found: '.$this->trap_id, 3, '');    
585 585
 	}
586 586
 	
587 587
 	/** Write trap data to trap database
@@ -598,86 +598,86 @@  discard block
 block discarded – undo
598 598
 		$insert_col='';
599 599
 		$insert_val='';
600 600
 		// add date time
601
-		$this->trap_data['date_received'] = date("Y-m-d H:i:s");
601
+		$this->trap_data['date_received']=date("Y-m-d H:i:s");
602 602
 
603 603
 		$firstcol=1;
604 604
 		foreach ($this->trap_data as $col => $val)
605 605
 		{
606
-			if ($firstcol==0) 
606
+			if ($firstcol == 0) 
607 607
 			{
608
-				$insert_col .=',';
609
-				$insert_val .=',';
608
+				$insert_col.=',';
609
+				$insert_val.=',';
610 610
 			}
611
-			$insert_col .= $col ;
612
-			$insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val);
611
+			$insert_col.=$col;
612
+			$insert_val.=($val == null) ? 'NULL' : $db_conn->quote($val);
613 613
 			$firstcol=0;
614 614
 		}
615 615
 		
616
-		$sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')';
616
+		$sql='INSERT INTO '.$this->db_prefix.'received ('.$insert_col.') VALUES ('.$insert_val.')';
617 617
 		switch ($this->trapDBType)
618 618
 		{
619 619
 			case 'pgsql': 
620
-				$sql .= ' RETURNING id;';
621
-				$this->trapLog('sql : '.$sql,3,'');
620
+				$sql.=' RETURNING id;';
621
+				$this->trapLog('sql : '.$sql, 3, '');
622 622
 				if (($ret_code=$db_conn->query($sql)) == FALSE) {
623
-					$this->trapLog('Error SQL insert : '.$sql,ERROR,'');
623
+					$this->trapLog('Error SQL insert : '.$sql, ERROR, '');
624 624
 				}
625
-				$this->trapLog('SQL insertion OK',3,'');
625
+				$this->trapLog('SQL insertion OK', 3, '');
626 626
 				// Get last id to insert oid/values in secondary table
627 627
 				if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) == FALSE) {
628 628
 														   
629
-					$this->trapLog('Erreur recuperation id',ERROR,'');
629
+					$this->trapLog('Erreur recuperation id', ERROR, '');
630 630
 				}
631
-				if (! isset($inserted_id_ret['id'])) {
632
-					$this->trapLog('Error getting id',ERROR,'');
631
+				if (!isset($inserted_id_ret['id'])) {
632
+					$this->trapLog('Error getting id', ERROR, '');
633 633
 				}
634 634
 				$this->trap_id=$inserted_id_ret['id'];
635 635
 			break;
636 636
 			case 'mysql': 
637
-				$sql .= ';';
638
-				$this->trapLog('sql : '.$sql,3,'');
637
+				$sql.=';';
638
+				$this->trapLog('sql : '.$sql, 3, '');
639 639
 				if (($ret_code=$db_conn->query($sql)) == FALSE) {
640
-					$this->trapLog('Error SQL insert : '.$sql,ERROR,'');
640
+					$this->trapLog('Error SQL insert : '.$sql, ERROR, '');
641 641
 				}
642
-				$this->trapLog('SQL insertion OK',3,'');
642
+				$this->trapLog('SQL insertion OK', 3, '');
643 643
 				// Get last id to insert oid/values in secondary table
644 644
 				$sql='SELECT LAST_INSERT_ID();';
645 645
 				if (($ret_code=$db_conn->query($sql)) == FALSE) {
646
-					$this->trapLog('Erreur recuperation id',ERROR,'');
646
+					$this->trapLog('Erreur recuperation id', ERROR, '');
647 647
 				}
648 648
 
649 649
 				$inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()'];
650
-				if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
650
+				if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
651 651
 				$this->trap_id=$inserted_id;
652 652
 			break;
653 653
 			default: 
654
-				$this->trapLog('Error SQL type  : '.$this->trapDBType,ERROR,'');
654
+				$this->trapLog('Error SQL type  : '.$this->trapDBType, ERROR, '');
655 655
 		}
656
-		$this->trapLog('id found: '.$this->trap_id,3,'');
656
+		$this->trapLog('id found: '.$this->trap_id, 3, '');
657 657
 		
658 658
 		// Fill trap extended data table
659 659
 		foreach ($this->trap_data_ext as $value) {			
660 660
 			// TODO : detect if trap value is encoded and decode it to UTF-8 for database
661 661
 			$firstcol=1;
662
-			$value->trap_id = $this->trap_id;
662
+			$value->trap_id=$this->trap_id;
663 663
 			$insert_col='';
664 664
 			$insert_val='';
665 665
 			foreach ($value as $col => $val)
666 666
 			{
667
-				if ($firstcol==0) 
667
+				if ($firstcol == 0) 
668 668
 				{
669
-					$insert_col .=',';
670
-					$insert_val .=',';
669
+					$insert_col.=',';
670
+					$insert_val.=',';
671 671
 				}
672
-				$insert_col .= $col;
673
-				$insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val);
672
+				$insert_col.=$col;
673
+				$insert_val.=($val == null) ? 'NULL' : $db_conn->quote($val);
674 674
 				$firstcol=0;
675 675
 			}
676 676
 
677
-			$sql= 'INSERT INTO '.$this->db_prefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');';			
677
+			$sql='INSERT INTO '.$this->db_prefix.'received_data ('.$insert_col.') VALUES ('.$insert_val.');';			
678 678
 
679 679
 			if (($ret_code=$db_conn->query($sql)) == FALSE) {
680
-				$this->trapLog('Erreur insertion data : ' . $sql,WARN,'');
680
+				$this->trapLog('Erreur insertion data : '.$sql, WARN, '');
681 681
 			}	
682 682
 		}	
683 683
 	}
@@ -687,14 +687,14 @@  discard block
 block discarded – undo
687 687
 	*	@param $oid string oid in numeric
688 688
 	*	@return mixed : PDO object or false
689 689
 	*/	
690
-	protected function getRules($ip,$oid)
690
+	protected function getRules($ip, $oid)
691 691
 	{
692 692
 		$db_conn=$this->db_connect_trap();
693 693
 		// fetch rules based on IP in rule and OID
694 694
 		$sql='SELECT * from '.$this->db_prefix.'rules WHERE trap_oid=\''.$oid.'\' ';
695
-		$this->trapLog('SQL query : '.$sql,4,'');
695
+		$this->trapLog('SQL query : '.$sql, 4, '');
696 696
 		if (($ret_code=$db_conn->query($sql)) == FALSE) {
697
-			$this->trapLog('No result in query : ' . $sql,WARN,'');
697
+			$this->trapLog('No result in query : '.$sql, WARN, '');
698 698
 			return false;
699 699
 		}
700 700
 		$rules_all=$ret_code->fetchAll();
@@ -703,7 +703,7 @@  discard block
 block discarded – undo
703 703
 		$rule_ret_key=0;
704 704
 		foreach ($rules_all as $key => $rule)
705 705
 		{
706
-			if ($rule['ip4']==$ip || $rule['ip6']==$ip)
706
+			if ($rule['ip4'] == $ip || $rule['ip6'] == $ip)
707 707
 			{
708 708
 				$rules_ret[$rule_ret_key]=$rules_all[$key];
709 709
 				//TODO : get host name by API (and check if correct in rule).
@@ -711,7 +711,7 @@  discard block
 block discarded – undo
711 711
 				continue;
712 712
 			}
713 713
 			// TODO : get hosts IP by API
714
-			if (isset($rule['host_group_name']) && $rule['host_group_name']!=null)
714
+			if (isset($rule['host_group_name']) && $rule['host_group_name'] != null)
715 715
 			{ // get ips of group members by oid
716 716
 				$db_conn2=$this->db_connect_ido();
717 717
 				$sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name
@@ -722,15 +722,15 @@  discard block
 block discarded – undo
722 722
 						LEFT JOIN icinga_objects as b ON b.object_id = a.host_object_id
723 723
 						WHERE o.name1='".$rule['host_group_name']."';";
724 724
 				if (($ret_code2=$db_conn2->query($sql)) == FALSE) {
725
-					$this->trapLog('No result in query : ' . $sql,WARN,'');
725
+					$this->trapLog('No result in query : '.$sql, WARN, '');
726 726
 					continue;
727 727
 				}
728 728
 				$grouphosts=$ret_code2->fetchAll();
729 729
 				//echo "rule grp :\n";print_r($grouphosts);echo "\n";
730
-				foreach ( $grouphosts as $host)
730
+				foreach ($grouphosts as $host)
731 731
 				{
732 732
 					//echo $host['ip4']."\n";
733
-					if ($host['ip4']==$ip || $host['ip6']==$ip)
733
+					if ($host['ip4'] == $ip || $host['ip6'] == $ip)
734 734
 					{
735 735
 						//echo "Rule added \n";
736 736
 						$rules_ret[$rule_ret_key]=$rules_all[$key];
@@ -753,7 +753,7 @@  discard block
 block discarded – undo
753 753
 		$db_conn=$this->db_connect_trap();
754 754
 		$sql="UPDATE ".$this->db_prefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');";
755 755
 		if ($db_conn->query($sql) == FALSE) {
756
-			$this->trapLog('Error in update query : ' . $sql,WARN,'');
756
+			$this->trapLog('Error in update query : '.$sql, WARN, '');
757 757
 		}
758 758
 	}
759 759
 	
@@ -765,31 +765,31 @@  discard block
 block discarded – undo
765 765
 	 * @param string $display
766 766
 	 * @returnn bool true is service check was sent without error
767 767
 	*/
768
-	public function serviceCheckResult($host,$service,$state,$display)
768
+	public function serviceCheckResult($host, $service, $state, $display)
769 769
 	{
770 770
 	    if ($this->api_use == false)
771 771
 	    {
772
-    		$send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' .
773
-    			$host.';' .$service .';' . $state . ';'.$display;
774
-    		$this->trapLog( $send." : to : " .$this->icinga2cmd,3,'');
772
+    		$send='['.date('U').'] PROCESS_SERVICE_CHECK_RESULT;'.
773
+    			$host.';'.$service.';'.$state.';'.$display;
774
+    		$this->trapLog($send." : to : ".$this->icinga2cmd, 3, '');
775 775
     		
776 776
     		// TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default....
777
-    		exec('echo "'.$send.'" > ' .$this->icinga2cmd);
777
+    		exec('echo "'.$send.'" > '.$this->icinga2cmd);
778 778
     		return true;
779 779
 	    }
780 780
 	    else
781 781
 	    {
782
-	        $api = $this->getAPI();
782
+	        $api=$this->getAPI();
783 783
 	        $api->setCredentials($this->api_username, $this->api_password);
784
-	        list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display);
784
+	        list($retcode, $retmessage)=$api->serviceCheckResult($host, $service, $state, $display);
785 785
 	        if ($retcode == false)
786 786
 	        {
787
-	            $this->trapLog( "Error sending result : " .$retmessage,WARN,'');
787
+	            $this->trapLog("Error sending result : ".$retmessage, WARN, '');
788 788
 	            return false;
789 789
 	        }
790 790
 	        else 
791 791
 	        {
792
-	            $this->trapLog( "Sent result : " .$retmessage,3,'');
792
+	            $this->trapLog("Sent result : ".$retmessage, 3, '');
793 793
 	            return true;
794 794
 	        }
795 795
 	    }
@@ -797,7 +797,7 @@  discard block
 block discarded – undo
797 797
 	
798 798
 	public function getHostByIP($ip)
799 799
 	{
800
-	    $api = $this->getAPI();
800
+	    $api=$this->getAPI();
801 801
 	    $api->setCredentials($this->api_username, $this->api_password);
802 802
 	    return $api->getHostByIP($ip);
803 803
 	}
@@ -810,32 +810,32 @@  discard block
 block discarded – undo
810 810
 	protected function applyDisplay($display)
811 811
 	{
812 812
 	    $matches=array();
813
-	    while (preg_match('/_OID\(([0-9\.]+)\)/',$display,$matches) == 1)
813
+	    while (preg_match('/_OID\(([0-9\.]+)\)/', $display, $matches) == 1)
814 814
 		{
815 815
 			$oid=$matches[1];
816 816
 			$found=0;
817
-			foreach($this->trap_data_ext as $val)
817
+			foreach ($this->trap_data_ext as $val)
818 818
 			{
819 819
 				if ($oid == $val->oid)
820 820
 				{
821
-					$val->value=preg_replace('/"/','',$val->value);
821
+					$val->value=preg_replace('/"/', '', $val->value);
822 822
 					$rep=0;
823
-					$display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep);
824
-					if ($rep==0)
823
+					$display=preg_replace('/_OID\('.$oid.'\)/', $val->value, $display, -1, $rep);
824
+					if ($rep == 0)
825 825
 					{
826
-						$this->trapLog("Error in display",WARN,'');
826
+						$this->trapLog("Error in display", WARN, '');
827 827
 						return $display;
828 828
 					}
829 829
 					$found=1;
830 830
 					break;
831 831
 				}
832 832
 			}
833
-			if ($found==0)
833
+			if ($found == 0)
834 834
 			{
835
-				$display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep);
836
-				if ($rep==0)
835
+				$display=preg_replace('/_OID\('.$oid.'\)/', '<not in trap>', $display, -1, $rep);
836
+				if ($rep == 0)
837 837
 				{
838
-					$this->trapLog("Error in display",WARN,'');
838
+					$this->trapLog("Error in display", WARN, '');
839 839
 					return $display;
840 840
 				}				
841 841
 			}
@@ -845,27 +845,27 @@  discard block
 block discarded – undo
845 845
 
846 846
 	
847 847
 	/***************** Eval & tokenizer functions ****************/
848
-	protected function eval_getElement($rule,&$item)
848
+	protected function eval_getElement($rule, &$item)
849 849
 	{
850
-		while ($rule[$item]==' ') $item++;
851
-		if (preg_match('/[0-9\.]/',$rule[$item]))
850
+		while ($rule[$item] == ' ') $item++;
851
+		if (preg_match('/[0-9\.]/', $rule[$item]))
852 852
 		{ // number
853 853
 	
854
-			$item2=$item+1; 
855
-			while (($item2!=strlen($rule)) && (preg_match('/[0-9\.]/',$rule[$item2]))) { $item2++ ;}
856
-			$val=substr($rule,$item,$item2-$item);
854
+			$item2=$item + 1; 
855
+			while (($item2 != strlen($rule)) && (preg_match('/[0-9\.]/', $rule[$item2]))) { $item2++; }
856
+			$val=substr($rule, $item, $item2 - $item);
857 857
 			$item=$item2;
858 858
 			//echo "number ".$val."\n";
859
-			return array(0,$val);
859
+			return array(0, $val);
860 860
 		}
861 861
 		if ($rule[$item] == '"')
862 862
 		{ // string
863 863
 			$item++;
864
-			$item2=$this->eval_getNext($rule,$item,'"');
865
-			$val=substr($rule,$item,$item2-$item-1);
864
+			$item2=$this->eval_getNext($rule, $item, '"');
865
+			$val=substr($rule, $item, $item2 - $item - 1);
866 866
 			$item=$item2;
867 867
 			//echo "string : ".$val."\n";
868
-			return array(1,$val);
868
+			return array(1, $val);
869 869
 		}
870 870
 		
871 871
 		if ($rule[$item] == '(')
@@ -874,14 +874,14 @@  discard block
 block discarded – undo
874 874
 			$start=$item;
875 875
 			$parenthesis_count=0; 
876 876
 			while (($item < strlen($rule)) // Not end of string AND
877
-			      && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded ()
877
+			      && (($rule[$item] != ')') || $parenthesis_count > 0)) // Closing ')' or embeded ()
878 878
 			{ 
879
-				if ($rule[$item] == '"' )
879
+				if ($rule[$item] == '"')
880 880
 				{ // pass through string
881 881
 					$item++;
882
-					$item=$this->eval_getNext($rule,$item,'"');
882
+					$item=$this->eval_getNext($rule, $item, '"');
883 883
 				} 
884
-				else{
884
+				else {
885 885
 				    if ($rule[$item] == '(')
886 886
 				    {
887 887
 				        $parenthesis_count++;
@@ -894,49 +894,49 @@  discard block
 block discarded – undo
894 894
 				}
895 895
 			}
896 896
 			
897
-			if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);}
898
-			$val=substr($rule,$start,$item-$start);
897
+			if ($item == strlen($rule)) {throw new Exception("no closing () in ".$rule." at ".$item); }
898
+			$val=substr($rule, $start, $item - $start);
899 899
 			$item++;
900 900
 			$start=0;
901 901
 			//echo "group : ".$val."\n";
902 902
 			// returns evaluation of group as type 2 (boolean)
903
-			return array(2,$this->evaluation($val,$start));		
903
+			return array(2, $this->evaluation($val, $start));		
904 904
 		}
905
-		throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]);
905
+		throw new Exception("number/string not found in ".$rule." at ".$item.' : '.$rule[$item]);
906 906
 		
907 907
 	}
908 908
 	
909
-	protected function eval_getNext($rule,$item,$tok)
909
+	protected function eval_getNext($rule, $item, $tok)
910 910
 	{
911
-		while (($rule[$item] != $tok ) && ($item < strlen($rule))) { $item++;}
912
-		if ($item==strlen($rule)) throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item);
913
-		return $item+1;
911
+		while (($rule[$item] != $tok) && ($item < strlen($rule))) { $item++; }
912
+		if ($item == strlen($rule)) throw new Exception("closing '".$tok."' not found in ".$rule." at ".$item);
913
+		return $item + 1;
914 914
 	}
915 915
 	
916
-	protected function eval_getOper($rule,&$item)
916
+	protected function eval_getOper($rule, &$item)
917 917
 	{
918
-		while ($rule[$item]==' ') $item++;
918
+		while ($rule[$item] == ' ') $item++;
919 919
 		switch ($rule[$item])
920 920
 		{
921 921
 			case '<':
922
-				if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");}
923
-				$item++; return array(0,"<");
922
+				if ($rule[$item + 1] == '=') { $item+=2; return array(0, "<="); }
923
+				$item++; return array(0, "<");
924 924
 			case '>':
925
-				if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");}
926
-				$item++; return array(0,">");
925
+				if ($rule[$item + 1] == '=') { $item+=2; return array(0, ">="); }
926
+				$item++; return array(0, ">");
927 927
 			case '=':
928
-				$item++; return array(0,"=");	
928
+				$item++; return array(0, "=");	
929 929
 			case '!':
930
-				if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");}
931
-				throw new Exception("Erreur in expr - incorrect operator '!'  found in ".$rule ." at " .$item);
930
+				if ($rule[$item + 1] == '=') { $item+=2; return array(0, "!="); }
931
+				throw new Exception("Erreur in expr - incorrect operator '!'  found in ".$rule." at ".$item);
932 932
 			case '~':
933
-				$item++; return array(0,"~");	
933
+				$item++; return array(0, "~");	
934 934
 			case '|':
935
-				$item++; return array(1,"|");	
935
+				$item++; return array(1, "|");	
936 936
 			case '&':
937
-				$item++; return array(1,"&");
937
+				$item++; return array(1, "&");
938 938
 			default	:
939
-				throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item);
939
+				throw new Exception("Erreur in expr - operator not found in ".$rule." at ".$item);
940 940
 		}
941 941
 	}
942 942
 	
@@ -948,10 +948,10 @@  discard block
 block discarded – undo
948 948
 	*   comparison int vs strings will return null (error)
949 949
 	*	return : bool or null on error
950 950
 	*/
951
-	public function evaluation($rule,&$item)
951
+	public function evaluation($rule, &$item)
952 952
 	{
953 953
 	    //echo "Evaluation of ".substr($rule,$item)."\n";
954
-		if ( $rule[$item] == '!') // If '!' found, negate next expression.
954
+		if ($rule[$item] == '!') // If '!' found, negate next expression.
955 955
 		{
956 956
 		    $negate=true;
957 957
 		    $item++;
@@ -961,63 +961,63 @@  discard block
 block discarded – undo
961 961
 		    $negate=false;
962 962
 		}
963 963
 		// First element : number, string or ()
964
-		list($type1,$val1) = $this->eval_getElement($rule,$item);
964
+		list($type1, $val1)=$this->eval_getElement($rule, $item);
965 965
 		//echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n";
966 966
 		
967
-		if ($item==strlen($rule)) // If only element, return value, but only boolean
967
+		if ($item == strlen($rule)) // If only element, return value, but only boolean
968 968
 		{
969 969
 		  if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule);
970
-		  if ($negate == true) $val1= ! $val1;
970
+		  if ($negate == true) $val1=!$val1;
971 971
 		  return $val1;
972 972
 		}  
973 973
 		
974 974
 		// Second element : operator
975
-		list($typec,$comp) = $this->eval_getOper($rule,$item);
975
+		list($typec, $comp)=$this->eval_getOper($rule, $item);
976 976
 		//echo "Comp : ".$comp." : ".substr($rule,$item)."\n";
977 977
         
978 978
 		// Third element : number, string or ()
979
-		if ( $rule[$item] == '!') // starts with a ! so evaluate whats next
979
+		if ($rule[$item] == '!') // starts with a ! so evaluate whats next
980 980
 		{
981 981
 		    $item++;
982 982
 		    if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule);
983
-		    $val2= ! $this->evaluation($rule,$item);
983
+		    $val2=!$this->evaluation($rule, $item);
984 984
 		    $type2=2; // result is a boolean 
985 985
 		}
986 986
 		else 
987 987
 		{
988
-		    list($type2,$val2) = $this->eval_getElement($rule,$item);
988
+		    list($type2, $val2)=$this->eval_getElement($rule, $item);
989 989
 		}
990 990
 		//echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n";
991 991
 		
992
-		if ($type1!=$type2)  // cannot compare different types
992
+		if ($type1 != $type2)  // cannot compare different types
993 993
 		{ 
994 994
 		    throw new Exception("Cannot compare string & number : ".$rule);
995 995
 		}
996
-		if ($typec==1 && $type1 !=2) // cannot use & or | with string/number
996
+		if ($typec == 1 && $type1 != 2) // cannot use & or | with string/number
997 997
 		{
998 998
 		    throw new Exception("Cannot use boolean operators with string & number : ".$rule);
999 999
 		}
1000 1000
 		
1001
-		switch ($comp){
1002
-			case '<':	$retVal= ($val1 < $val2); break;
1003
-			case '<=':	$retVal= ($val1 <= $val2); break;
1004
-			case '>':	$retVal= ($val1 > $val2); break;
1005
-			case '>=':	$retVal= ($val1 >= $val2); break;
1006
-			case '=':	$retVal= ($val1 == $val2); break;
1007
-			case '!=':	$retVal= ($val1 != $val2); break;
1008
-			case '~':	$retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break;
1009
-			case '|':	$retVal= ($val1 || $val2); break;
1010
-			case '&':	$retVal= ($val1 && $val2); break;
1001
+		switch ($comp) {
1002
+			case '<':	$retVal=($val1 < $val2); break;
1003
+			case '<=':	$retVal=($val1 <= $val2); break;
1004
+			case '>':	$retVal=($val1 > $val2); break;
1005
+			case '>=':	$retVal=($val1 >= $val2); break;
1006
+			case '=':	$retVal=($val1 == $val2); break;
1007
+			case '!=':	$retVal=($val1 != $val2); break;
1008
+			case '~':	$retVal=(preg_match('/'.preg_replace('/"/', '', $val2).'/', $val1)); break;
1009
+			case '|':	$retVal=($val1 || $val2); break;
1010
+			case '&':	$retVal=($val1 && $val2); break;
1011 1011
 			default:  throw new Exception("Error in expression - unknown comp : ".$comp);
1012 1012
 		}
1013
-		if ($negate == true) $retVal = ! $retVal; // Inverse result if negate before expression
1013
+		if ($negate == true) $retVal=!$retVal; // Inverse result if negate before expression
1014 1014
 		
1015
-		if ($item==strlen($rule)) return $retVal; // End of string : return evaluation
1015
+		if ($item == strlen($rule)) return $retVal; // End of string : return evaluation
1016 1016
 		// check for logical operator :
1017 1017
 		switch ($rule[$item])
1018 1018
 		{
1019
-			case '|':	$item++; return ($retVal || $this->evaluation($rule,$item) ); break;
1020
-			case '&':	$item++; return ($retVal && $this->evaluation($rule,$item) ); break;
1019
+			case '|':	$item++; return ($retVal || $this->evaluation($rule, $item)); break;
1020
+			case '&':	$item++; return ($retVal && $this->evaluation($rule, $item)); break;
1021 1021
 			
1022 1022
 			default:  throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]);
1023 1023
 		}
@@ -1029,17 +1029,17 @@  discard block
 block discarded – undo
1029 1029
 		$rule2='';
1030 1030
 		while ($item < strlen($rule))
1031 1031
 		{
1032
-			if ($rule[$item]==' ') { $item++; continue; }
1033
-			if ($rule[$item]=='"')
1032
+			if ($rule[$item] == ' ') { $item++; continue; }
1033
+			if ($rule[$item] == '"')
1034 1034
 			{
1035 1035
 				$rule2.=$rule[$item];
1036 1036
 				$item++;
1037
-				while (($rule[$item]!='"') && ($item < strlen($rule)))
1037
+				while (($rule[$item] != '"') && ($item < strlen($rule)))
1038 1038
 				{
1039 1039
 					$rule2.=$rule[$item];
1040 1040
 					$item++;
1041 1041
 				}
1042
-				if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item);
1042
+				if ($item == strlen($rule)) throw new Exception("closing '\"' not found in ".$rule." at ".$item);
1043 1043
 				$rule2.=$rule[$item];
1044 1044
 				$item++;
1045 1045
 				continue;
@@ -1059,12 +1059,12 @@  discard block
 block discarded – undo
1059 1059
 	
1060 1060
 	protected function eval_rule($rule)
1061 1061
 	{
1062
-		if ($rule==null || $rule == '') // Empty rule is always true
1062
+		if ($rule == null || $rule == '') // Empty rule is always true
1063 1063
 		{
1064 1064
 			return true;
1065 1065
 		}
1066 1066
 		$matches=array();
1067
-		while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1)
1067
+		while (preg_match('/_OID\(([0-9\.\*]+)\)/', $rule, $matches) == 1)
1068 1068
 		{
1069 1069
 			$oid=$matches[1];
1070 1070
 			$found=0;
@@ -1076,49 +1076,49 @@  discard block
 block discarded – undo
1076 1076
 			// replace * with \* in oid for preg_replace
1077 1077
 			$oid=preg_replace('/\*/', '\*', $oid);
1078 1078
 			
1079
-			$this->trapLog('OID in rule : '.$oid.' / '.$oidR,4,'');
1079
+			$this->trapLog('OID in rule : '.$oid.' / '.$oidR, 4, '');
1080 1080
 			
1081
-			foreach($this->trap_data_ext as $val)
1081
+			foreach ($this->trap_data_ext as $val)
1082 1082
 			{
1083
-				if (preg_match("/^$oidR$/",$val->oid) == 1)
1083
+				if (preg_match("/^$oidR$/", $val->oid) == 1)
1084 1084
 				{
1085
-					if (!preg_match('/^[0-9]*\.?[0-9]+$/',$val->value))
1085
+					if (!preg_match('/^[0-9]*\.?[0-9]+$/', $val->value))
1086 1086
 					{ // If not a number, change " to ' and put " around it
1087
-						$val->value=preg_replace('/"/',"'",$val->value);
1087
+						$val->value=preg_replace('/"/', "'", $val->value);
1088 1088
 						$val->value='"'.$val->value.'"';
1089 1089
 					}
1090 1090
 					$rep=0;
1091
-					$rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep);
1092
-					if ($rep==0)
1091
+					$rule=preg_replace('/_OID\('.$oid.'\)/', $val->value, $rule, -1, $rep);
1092
+					if ($rep == 0)
1093 1093
 					{
1094
-						$this->trapLog("Error in rule_eval",WARN,'');
1094
+						$this->trapLog("Error in rule_eval", WARN, '');
1095 1095
 						return false;
1096 1096
 					}
1097 1097
 					$found=1;
1098 1098
 					break;
1099 1099
 				}
1100 1100
 			}
1101
-			if ($found==0)
1101
+			if ($found == 0)
1102 1102
 			{	// OID not found : throw error
1103 1103
 			    throw new Exception('OID '.$oid.' not found in trap');
1104 1104
 			}
1105 1105
 		}
1106 1106
 		$item=0;
1107 1107
 		$rule=$this->eval_cleanup($rule);
1108
-		$this->trapLog('Rule after clenup: '.$rule,3,'');
1108
+		$this->trapLog('Rule after clenup: '.$rule, 3, '');
1109 1109
 		
1110
-		return  $this->evaluation($rule,$item);
1110
+		return  $this->evaluation($rule, $item);
1111 1111
 	}
1112 1112
 	
1113 1113
 	/** Match rules for current trap and do action
1114 1114
 	*/
1115 1115
 	public function applyRules()
1116 1116
 	{
1117
-		$rules = $this->getRules($this->trap_data['source_ip'],$this->trap_data['trap_oid']);
1117
+		$rules=$this->getRules($this->trap_data['source_ip'], $this->trap_data['trap_oid']);
1118 1118
 		
1119
-		if ($rules==FALSE || count($rules)==0)
1119
+		if ($rules == FALSE || count($rules) == 0)
1120 1120
 		{
1121
-			$this->trapLog('No rules found for this trap',3,'');
1121
+			$this->trapLog('No rules found for this trap', 3, '');
1122 1122
 			$this->trap_data['status']='unknown';
1123 1123
 			$this->trap_to_db=true;
1124 1124
 			return;
@@ -1133,58 +1133,58 @@  discard block
 block discarded – undo
1133 1133
 			$service_name=$rule['service_name'];
1134 1134
 			
1135 1135
 			$display=$this->applyDisplay($rule['display']);
1136
-			$this->trap_action = ($this->trap_action==null)? '' : $this->trap_action . ', ';
1136
+			$this->trap_action=($this->trap_action == null) ? '' : $this->trap_action.', ';
1137 1137
 			try
1138 1138
 			{
1139
-				$this->trapLog('Rule to eval : '.$rule['rule'],3,'');
1139
+				$this->trapLog('Rule to eval : '.$rule['rule'], 3, '');
1140 1140
 				$evalr=$this->eval_rule($rule['rule']);
1141 1141
 				
1142 1142
 				if ($evalr == true)
1143 1143
 				{
1144 1144
 					//$this->trapLog('rules OOK: '.print_r($rule),3,'');
1145 1145
 					$action=$rule['action_match'];
1146
-					$this->trapLog('action OK : '.$action,3,'');
1146
+					$this->trapLog('action OK : '.$action, 3, '');
1147 1147
 					if ($action >= 0)
1148 1148
 					{
1149
-						if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false)
1149
+						if ($this->serviceCheckResult($host_name, $service_name, $action, $display) == false)
1150 1150
 						{
1151 1151
 						    $this->trap_action.='Error sending status : check cmd/API';
1152 1152
 						}
1153 1153
 						else
1154 1154
 						{
1155
-						    $this->add_rule_match($rule['id'],$rule['num_match']+1);
1155
+						    $this->add_rule_match($rule['id'], $rule['num_match'] + 1);
1156 1156
 						    $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name;
1157 1157
 						}
1158 1158
 					}
1159 1159
 					else
1160 1160
 					{
1161
-						$this->add_rule_match($rule['id'],$rule['num_match']+1);
1161
+						$this->add_rule_match($rule['id'], $rule['num_match'] + 1);
1162 1162
 					}
1163
-					$this->trap_to_db=($action==-2)?false:true;
1163
+					$this->trap_to_db=($action == -2) ?false:true;
1164 1164
 				}
1165 1165
 				else
1166 1166
 				{
1167 1167
 					//$this->trapLog('rules KOO : '.print_r($rule),3,'');
1168 1168
 					
1169 1169
 					$action=$rule['action_nomatch'];
1170
-					$this->trapLog('action NOK : '.$action,3,'');
1170
+					$this->trapLog('action NOK : '.$action, 3, '');
1171 1171
 					if ($action >= 0)
1172 1172
 					{
1173
-					    if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false)
1173
+					    if ($this->serviceCheckResult($host_name, $service_name, $action, $display) == false)
1174 1174
 					    {
1175 1175
 					        $this->trap_action.='Error sending status : check cmd/API';
1176 1176
 					    }
1177 1177
 					    else
1178 1178
 					    {
1179
-    						$this->add_rule_match($rule['id'],$rule['num_match']+1);
1179
+    						$this->add_rule_match($rule['id'], $rule['num_match'] + 1);
1180 1180
     						$this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name;
1181 1181
 					    }
1182 1182
 					}
1183 1183
 					else
1184 1184
 					{
1185
-						$this->add_rule_match($rule['id'],$rule['num_match']+1);
1185
+						$this->add_rule_match($rule['id'], $rule['num_match'] + 1);
1186 1186
 					}
1187
-					$this->trap_to_db=($action==-2)?false:true;					
1187
+					$this->trap_to_db=($action == -2) ?false:true;					
1188 1188
 				}
1189 1189
 				// Put name in source_name
1190 1190
 				if (!isset($this->trap_data['source_name']))
@@ -1193,7 +1193,7 @@  discard block
 block discarded – undo
1193 1193
 				}
1194 1194
 				else
1195 1195
 				{
1196
-					if (!preg_match('/'.$rule['host_name'].'/',$this->trap_data['source_name']))
1196
+					if (!preg_match('/'.$rule['host_name'].'/', $this->trap_data['source_name']))
1197 1197
 					{ // only add if not present
1198 1198
 						$this->trap_data['source_name'].=','.$rule['host_name'];
1199 1199
 					}
@@ -1201,13 +1201,13 @@  discard block
 block discarded – undo
1201 1201
 			}
1202 1202
 			catch (Exception $e) 
1203 1203
 			{ 
1204
-			    $this->trapLog('Error in rule eval : '.$e->getMessage(),WARN,'');
1204
+			    $this->trapLog('Error in rule eval : '.$e->getMessage(), WARN, '');
1205 1205
 			    $this->trap_action.=' ERR : '.$e->getMessage();
1206 1206
 			    $this->trap_data['status']='error';
1207 1207
 			}
1208 1208
 			
1209 1209
 		}
1210
-		if ($this->trap_data['status']=='error')
1210
+		if ($this->trap_data['status'] == 'error')
1211 1211
 		{
1212 1212
 		  $this->trap_to_db=true; // Always put errors in DB for the use can see
1213 1213
 		}
@@ -1223,13 +1223,13 @@  discard block
 block discarded – undo
1223 1223
 	public function add_rule_final($time)
1224 1224
 	{
1225 1225
 		$db_conn=$this->db_connect_trap();
1226
-		if ($this->trap_action==null) 
1226
+		if ($this->trap_action == null) 
1227 1227
 		{
1228 1228
 			$this->trap_action='No action';
1229 1229
 		}
1230 1230
 		$sql="UPDATE ".$this->db_prefix."received SET process_time = '".$time."' , status_detail='".$this->trap_action."'  WHERE (id = '".$this->trap_id."');";
1231 1231
 		if ($db_conn->query($sql) == FALSE) {
1232
-			$this->trapLog('Error in update query : ' . $sql,WARN,'');
1232
+			$this->trapLog('Error in update query : '.$sql, WARN, '');
1233 1233
 		}
1234 1234
 	}
1235 1235
 	
@@ -1239,14 +1239,14 @@  discard block
 block discarded – undo
1239 1239
 	*	@param $schema_file	string File to read schema from
1240 1240
 	*	@param $table_prefix string to replace #PREFIX# in schema file by this
1241 1241
 	*/
1242
-	public function create_schema($schema_file,$table_prefix)
1242
+	public function create_schema($schema_file, $table_prefix)
1243 1243
 	{
1244 1244
 		//Read data from snmptrapd from stdin
1245 1245
 		$input_stream=fopen($schema_file, 'r');
1246 1246
 
1247
-		if ($input_stream==FALSE)
1247
+		if ($input_stream == FALSE)
1248 1248
 		{
1249
-			$this->trapLog("Error reading schema !",ERROR,''); 
1249
+			$this->trapLog("Error reading schema !", ERROR, ''); 
1250 1250
 		}
1251 1251
 		$newline='';
1252 1252
 		$cur_table='';
@@ -1255,14 +1255,14 @@  discard block
 block discarded – undo
1255 1255
 		
1256 1256
 		while (($line=fgets($input_stream)) !== false)
1257 1257
 		{
1258
-			$newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line));
1258
+			$newline.=chop(preg_replace('/#PREFIX#/', $table_prefix, $line));
1259 1259
 			if (preg_match('/; *$/', $newline)) 
1260 1260
             {
1261
-                $sql= $newline;
1261
+                $sql=$newline;
1262 1262
                 if ($db_conn->query($sql) == FALSE) {
1263
-                    $this->trapLog('Error create schema : '.$sql,ERROR,'');
1263
+                    $this->trapLog('Error create schema : '.$sql, ERROR, '');
1264 1264
                 }
1265
-                if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array))
1265
+                if (preg_match('/^ *CREATE TABLE ([^ ]+)/', $newline, $cur_table_array))
1266 1266
                 {
1267 1267
                     $cur_table='table '.$cur_table_array[1];
1268 1268
                 }
@@ -1270,19 +1270,19 @@  discard block
 block discarded – undo
1270 1270
                 {
1271 1271
                     $cur_table='secret SQL stuff :-)';
1272 1272
                 }
1273
-                $this->trapLog('Creating : ' . $cur_table, 3,'');
1273
+                $this->trapLog('Creating : '.$cur_table, 3, '');
1274 1274
                 $newline='';
1275 1275
             }
1276 1276
 		}
1277 1277
 		
1278
-		$sql= $newline;
1278
+		$sql=$newline;
1279 1279
 		if ($sql != '')
1280 1280
 		{
1281 1281
     		if ($db_conn->query($sql) == FALSE) {
1282
-    			$this->trapLog('Error create schema : '.$sql,ERROR,'');
1282
+    			$this->trapLog('Error create schema : '.$sql, ERROR, '');
1283 1283
     		}
1284 1284
 		}
1285
-		$this->trapLog('Schema created',3);		
1285
+		$this->trapLog('Schema created', 3);		
1286 1286
 	}
1287 1287
 
1288 1288
 	/** 
@@ -1293,14 +1293,14 @@  discard block
 block discarded – undo
1293 1293
 	 *     @param bool $getmsg : only get messages from version upgrades
1294 1294
 	 *     @return string : if $getmsg=true, return messages.
1295 1295
 	 */
1296
-	public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false)
1296
+	public function update_schema($prefix, $target_version, $table_prefix, $getmsg=false)
1297 1297
 	{
1298 1298
 	    // Get current db number
1299 1299
 	    $db_conn=$this->db_connect_trap();
1300 1300
 	    $sql='SELECT id,value from '.$this->db_prefix.'db_config WHERE name=\'db_version\' ';
1301
-	    $this->trapLog('SQL query : '.$sql,4,'');
1301
+	    $this->trapLog('SQL query : '.$sql, 4, '');
1302 1302
 	    if (($ret_code=$db_conn->query($sql)) == FALSE) {
1303
-	        $this->trapLog('Cannot get db version. Query : ' . $sql,2,'');
1303
+	        $this->trapLog('Cannot get db version. Query : '.$sql, 2, '');
1304 1304
 	        return;
1305 1305
 	    }
1306 1306
 	    $version=$ret_code->fetchAll();
@@ -1309,47 +1309,47 @@  discard block
 block discarded – undo
1309 1309
 	    
1310 1310
 	    if ($this->trapDBType == 'pgsql')
1311 1311
 	    {
1312
-	        $prefix .= 'update_pgsql/schema_';
1312
+	        $prefix.='update_pgsql/schema_';
1313 1313
 	    }
1314 1314
 	    else
1315 1315
 	    {
1316
-	        $prefix .= 'update_sql/schema_';
1316
+	        $prefix.='update_sql/schema_';
1317 1317
 	    }
1318 1318
 	    //echo "version all :\n";print_r($version);echo " \n $cur_ver \n";
1319 1319
 	    if ($getmsg == true)
1320 1320
 	    {
1321 1321
 	        $message='';
1322
-	        $this->trapLog('getting message for upgrade',4,'');
1323
-	        while($cur_version<$target_version)
1322
+	        $this->trapLog('getting message for upgrade', 4, '');
1323
+	        while ($cur_version < $target_version)
1324 1324
 	        {
1325 1325
 	            $cur_version++;
1326
-	            $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql';
1326
+	            $updateFile=$prefix.'v'.($cur_version - 1).'_v'.$cur_version.'.sql';
1327 1327
 	            $input_stream=fopen($updateFile, 'r');
1328
-	            if ($input_stream==FALSE)
1328
+	            if ($input_stream == FALSE)
1329 1329
 	            {
1330
-	                $this->trapLog("Error reading update file ". $updateFile,2,'');
1330
+	                $this->trapLog("Error reading update file ".$updateFile, 2, '');
1331 1331
 	                return;
1332 1332
 	            }
1333 1333
 	            do { $line=fgets($input_stream); }
1334
-	            while ($line !== false && !preg_match('/#MESSAGE/',$line));
1334
+	            while ($line !== false && !preg_match('/#MESSAGE/', $line));
1335 1335
 	            if ($line === false)
1336 1336
 	            {
1337
-	                $this->trapLog("No message in file ". $updateFile,2,'');
1337
+	                $this->trapLog("No message in file ".$updateFile, 2, '');
1338 1338
 	                return;
1339 1339
 	            }
1340
-	            $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n";
1340
+	            $message.=($cur_version - 1).'->'.$cur_version.' : '.preg_replace('/#MESSAGE : /', '', $line)."\n";
1341 1341
 	        }
1342 1342
 	        return $message;
1343 1343
 	    }
1344
-	    while($cur_version<$target_version)
1344
+	    while ($cur_version < $target_version)
1345 1345
 	    { // tODO : execute pre & post scripts
1346 1346
 	       $cur_version++;
1347
-	       $this->trapLog('Updating to version : ' .$cur_version ,3,'');
1348
-	       $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql';
1347
+	       $this->trapLog('Updating to version : '.$cur_version, 3, '');
1348
+	       $updateFile=$prefix.'v'.($cur_version - 1).'_v'.$cur_version.'.sql';
1349 1349
 	       $input_stream=fopen($updateFile, 'r');
1350
-	       if ($input_stream==FALSE)
1350
+	       if ($input_stream == FALSE)
1351 1351
 	       {
1352
-	           $this->trapLog("Error reading update file ". $updateFile,2,'');
1352
+	           $this->trapLog("Error reading update file ".$updateFile, 2, '');
1353 1353
 	           return;
1354 1354
 	       }
1355 1355
 	       $newline='';
@@ -1358,24 +1358,24 @@  discard block
 block discarded – undo
1358 1358
 	       while (($line=fgets($input_stream)) != FALSE)
1359 1359
 	       {
1360 1360
 	           if (preg_match('/^#/', $line)) continue; // ignore comment lines
1361
-	           $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line));
1361
+	           $newline.=chop(preg_replace('/#PREFIX#/', $table_prefix, $line));
1362 1362
 	           if (preg_match('/; *$/', $newline))
1363 1363
 	           {
1364 1364
 	               $sql_req=$db_conn->prepare($newline);
1365 1365
 	               if ($sql_req->execute() == FALSE) {
1366
-	                   $this->trapLog('Error create schema : '.$newline,1,'');
1366
+	                   $this->trapLog('Error create schema : '.$newline, 1, '');
1367 1367
 	               }
1368 1368
 	               $cur_table_array=array();
1369
-	               if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array))
1369
+	               if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/', $newline, $cur_table_array))
1370 1370
 	               {
1371
-	                   $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2];
1371
+	                   $cur_table=$cur_table_array[1].' SQL table '.$cur_table_array[2];
1372 1372
 	               }
1373 1373
 	               else
1374 1374
 	               {
1375 1375
 	                   $cur_table='secret SQL stuff :-)';
1376 1376
 	                   $cur_table=$newline;
1377 1377
 	               }
1378
-	               $this->trapLog('Doing : ' . $cur_table, 3,'');
1378
+	               $this->trapLog('Doing : '.$cur_table, 3, '');
1379 1379
 	               
1380 1380
 	               $newline='';
1381 1381
 	           }
@@ -1388,13 +1388,13 @@  discard block
 block discarded – undo
1388 1388
 	       //}
1389 1389
 	       
1390 1390
 	       $sql='UPDATE '.$this->db_prefix.'db_config SET value='.$cur_version.' WHERE ( id = '.$db_version_id.' )';
1391
-	       $this->trapLog('SQL query : '.$sql,4,'');
1391
+	       $this->trapLog('SQL query : '.$sql, 4, '');
1392 1392
 	       if (($ret_code=$db_conn->query($sql)) == FALSE) {
1393
-	           $this->trapLog('Cannot update db version. Query : ' . $sql,2,'');
1393
+	           $this->trapLog('Cannot update db version. Query : '.$sql, 2, '');
1394 1394
 	           return;
1395 1395
 	       }
1396 1396
 	       
1397
-	       $this->trapLog('Schema updated to version : '.$cur_version ,3);
1397
+	       $this->trapLog('Schema updated to version : '.$cur_version, 3);
1398 1398
 	    }
1399 1399
 	}
1400 1400
 	
@@ -1415,7 +1415,7 @@  discard block
 block discarded – undo
1415 1415
     WHERE s.current_state != 0;";
1416 1416
 		$db_conn=$this->db_connect_ido();
1417 1417
 		if (($services_db=$db_conn->query($sql_query)) == FALSE) { // set err to 1 to throw exception.
1418
-			$this->trapLog('No result in query : ' . $sql_query,ERROR,'');
1418
+			$this->trapLog('No result in query : '.$sql_query, ERROR, '');
1419 1419
 		}
1420 1420
 		$services=$services_db->fetchAll();
1421 1421
 		
@@ -1423,7 +1423,7 @@  discard block
 block discarded – undo
1423 1423
 		$sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->db_prefix."rules where revert_ok != 0;";
1424 1424
 		$db_conn2=$this->db_connect_trap();
1425 1425
 		if (($rules_db=$db_conn2->query($sql_query)) == FALSE) {
1426
-			$this->trapLog('No result in query : ' . $sql_query,ERROR,''); 
1426
+			$this->trapLog('No result in query : '.$sql_query, ERROR, ''); 
1427 1427
 		}
1428 1428
 		$rules=$rules_db->fetchAll();
1429 1429
 		
@@ -1438,13 +1438,13 @@  discard block
 block discarded – undo
1438 1438
 					$service['host_name'] == $rule['host_name'] &&
1439 1439
 					($service['last_check'] + $rule['revert_ok']) < $now)
1440 1440
 				{
1441
-					$this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds');
1441
+					$this->serviceCheckResult($service['host_name'], $service['service_name'], 0, 'Reset service to OK after '.$rule['revert_ok'].' seconds');
1442 1442
 					$numreset++;
1443 1443
 				}
1444 1444
 			}
1445 1445
 		}
1446 1446
 		echo "\n";
1447
-		echo $numreset . " service(s) reset to OK\n";
1447
+		echo $numreset." service(s) reset to OK\n";
1448 1448
 		return 0;
1449 1449
 		
1450 1450
 	}
@@ -1465,7 +1465,7 @@  discard block
 block discarded – undo
1465 1465
 	 * @param string $description
1466 1466
 	 * @return number : 0=unchanged, 1 = changed, 2=created
1467 1467
 	 */
1468
-	public function update_oid($oid,$mib,$name,$type,$textConv,$dispHint,$syntax,$type_enum,$description=NULL)
1468
+	public function update_oid($oid, $mib, $name, $type, $textConv, $dispHint, $syntax, $type_enum, $description=NULL)
1469 1469
 	{
1470 1470
 		$db_conn=$this->db_connect_trap();
1471 1471
 		$description=$db_conn->quote($description);
@@ -1475,7 +1475,7 @@  discard block
 block discarded – undo
1475 1475
 		    { // newly created.
1476 1476
 		        return 0;
1477 1477
 		    }
1478
-			if ( $name != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['name'] ||
1478
+			if ($name != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['name'] ||
1479 1479
 			    $mib != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['mib'] ||
1480 1480
 			    $type != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['type'] //||
1481 1481
 			    //$textConv != $this->dbOidAll[$this->dbOidIndex[$oid]['key']]['textual_convention'] //||
@@ -1495,23 +1495,23 @@  discard block
 block discarded – undo
1495 1495
 			        ':name' => $name,
1496 1496
 			        ':type' => $type, 
1497 1497
 			        ':mib' => $mib, 
1498
-			        ':tc' =>  ($textConv==null)?'null':$textConv , 
1499
-			        ':display_hint' => ($dispHint==null)?'null':$dispHint ,
1500
-			        ':syntax' => ($syntax==null)?'null':$syntax,
1501
-			        ':type_enum' => ($type_enum==null)?'null':$type_enum, 
1502
-			        ':description' => ($description==null)?'null':$description,
1498
+			        ':tc' =>  ($textConv == null) ? 'null' : $textConv, 
1499
+			        ':display_hint' => ($dispHint == null) ? 'null' : $dispHint,
1500
+			        ':syntax' => ($syntax == null) ? 'null' : $syntax,
1501
+			        ':type_enum' => ($type_enum == null) ? 'null' : $type_enum, 
1502
+			        ':description' => ($description == null) ? 'null' : $description,
1503 1503
 			        ':id' => $this->dbOidAll[$this->dbOidIndex[$oid]['id']]
1504 1504
 			    );
1505 1505
 			    
1506 1506
 			    if ($sqlQuery->execute($sqlParam) == FALSE) {
1507
-			        $this->trapLog('Error in query : ' . $sql,ERROR,'');
1507
+			        $this->trapLog('Error in query : '.$sql, ERROR, '');
1508 1508
 			    }
1509
-			    $this->trapLog('Trap updated : '.$name . ' / OID : '.$oid,4,'');
1509
+			    $this->trapLog('Trap updated : '.$name.' / OID : '.$oid, 4, '');
1510 1510
 				return 1;
1511 1511
 			}
1512 1512
 			else
1513 1513
 			{
1514
-			    $this->trapLog('Trap unchanged : '.$name . ' / OID : '.$oid,4,'');
1514
+			    $this->trapLog('Trap unchanged : '.$name.' / OID : '.$oid, 4, '');
1515 1515
 			    return 0;
1516 1516
 			}
1517 1517
 		}
@@ -1521,11 +1521,11 @@  discard block
 block discarded – undo
1521 1521
 
1522 1522
 		$sql='INSERT INTO '.$this->db_prefix.'mib_cache '.
1523 1523
 		      '(oid, name, type , mib, textual_convention, display_hint '.
1524
-              ', syntax, type_enum , description ) ' . 
1524
+              ', syntax, type_enum , description ) '. 
1525 1525
               'values (:oid, :name , :type ,:mib ,:tc , :display_hint'.
1526 1526
               ', :syntax, :type_enum, :description )';
1527 1527
         
1528
-		if ($this->trapDBType == 'pgsql') $sql .= 'RETURNING id';
1528
+		if ($this->trapDBType == 'pgsql') $sql.='RETURNING id';
1529 1529
 		
1530 1530
 		$sqlQuery=$db_conn->prepare($sql);
1531 1531
 		
@@ -1534,15 +1534,15 @@  discard block
 block discarded – undo
1534 1534
 		    ':name' => $name,
1535 1535
 		    ':type' => $type,
1536 1536
 		    ':mib' => $mib,
1537
-		    ':tc' =>  ($textConv==null)?'null':$textConv ,
1538
-		    ':display_hint' => ($dispHint==null)?'null':$dispHint ,
1539
-		    ':syntax' => ($syntax==null)?'null':$syntax,
1540
-		    ':type_enum' => ($type_enum==null)?'null':$type_enum,
1541
-		    ':description' => ($description==null)?'null':$description
1537
+		    ':tc' =>  ($textConv == null) ? 'null' : $textConv,
1538
+		    ':display_hint' => ($dispHint == null) ? 'null' : $dispHint,
1539
+		    ':syntax' => ($syntax == null) ? 'null' : $syntax,
1540
+		    ':type_enum' => ($type_enum == null) ? 'null' : $type_enum,
1541
+		    ':description' => ($description == null) ? 'null' : $description
1542 1542
 		);
1543 1543
 		
1544 1544
 		if ($sqlQuery->execute($sqlParam) == FALSE) {
1545
-		    $this->trapLog('Error in query : ' . $sql,1,'');
1545
+		    $this->trapLog('Error in query : '.$sql, 1, '');
1546 1546
 		}
1547 1547
 		
1548 1548
 		switch ($this->trapDBType)
@@ -1550,10 +1550,10 @@  discard block
 block discarded – undo
1550 1550
 		    case 'pgsql':
1551 1551
 		        // Get last id to insert oid/values in secondary table
1552 1552
 		        if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) == FALSE) {		            
1553
-		            $this->trapLog('Error getting id - pgsql - ',1,'');
1553
+		            $this->trapLog('Error getting id - pgsql - ', 1, '');
1554 1554
 		        }
1555
-		        if (! isset($inserted_id_ret['id'])) {
1556
-		            $this->trapLog('Error getting id - pgsql - empty.',1,'');
1555
+		        if (!isset($inserted_id_ret['id'])) {
1556
+		            $this->trapLog('Error getting id - pgsql - empty.', 1, '');
1557 1557
 		        }
1558 1558
 		        $this->dbOidIndex[$oid]['id']=$inserted_id_ret['id'];
1559 1559
 		        break;
@@ -1561,15 +1561,15 @@  discard block
 block discarded – undo
1561 1561
 		        // Get last id to insert oid/values in secondary table
1562 1562
 		        $sql='SELECT LAST_INSERT_ID();';
1563 1563
 		        if (($ret_code=$db_conn->query($sql)) == FALSE) {
1564
-		            $this->trapLog('Erreur getting id - mysql - ',1,'');
1564
+		            $this->trapLog('Erreur getting id - mysql - ', 1, '');
1565 1565
 		        }
1566 1566
 		        
1567 1567
 		        $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()'];
1568
-		        if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
1568
+		        if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
1569 1569
 		        $this->dbOidIndex[$oid]['id']=$inserted_id;
1570 1570
 		        break;
1571 1571
 		    default:
1572
-		        $this->trapLog('Error SQL type  : '.$this->trapDBType,1,'');
1572
+		        $this->trapLog('Error SQL type  : '.$this->trapDBType, 1, '');
1573 1573
 		}
1574 1574
 
1575 1575
 		// Set as newly created.
@@ -1584,21 +1584,21 @@  discard block
 block discarded – undo
1584 1584
      * @param array $objects : array of objects name (without MIB)
1585 1585
      * @param bool $check_existing : check instead of create
1586 1586
      */
1587
-	public function trap_objects($trapOID,$trapmib,$objects,$check_existing)
1587
+	public function trap_objects($trapOID, $trapmib, $objects, $check_existing)
1588 1588
 	{
1589 1589
 	    $dbObjects=null; // cache of objects for trap in db
1590 1590
 	    $db_conn=$this->db_connect_trap();
1591 1591
 	    
1592 1592
 	    // Get id of trapmib.
1593 1593
 
1594
-	    $trapId = $this->dbOidIndex[$trapOID]['id'];
1594
+	    $trapId=$this->dbOidIndex[$trapOID]['id'];
1595 1595
 	    if ($check_existing == true)
1596 1596
 	    {
1597 1597
 	        // Get all objects
1598 1598
 	        $sql='SELECT * FROM '.$this->db_prefix.'mib_cache_trap_object where trap_id='.$trapId.';';
1599
-	        $this->trapLog('SQL query get all traps: '.$sql,4,'');
1599
+	        $this->trapLog('SQL query get all traps: '.$sql, 4, '');
1600 1600
 	        if (($ret_code=$db_conn->query($sql)) == FALSE) {
1601
-	            $this->trapLog('No result in query : ' . $sql,1,'');
1601
+	            $this->trapLog('No result in query : '.$sql, 1, '');
1602 1602
 	        }
1603 1603
 	        $dbObjectsRaw=$ret_code->fetchAll();
1604 1604
 	        
@@ -1612,39 +1612,39 @@  discard block
 block discarded – undo
1612 1612
 	        $match=$snmptrans=array();
1613 1613
 	        $retVal=0;
1614 1614
 	        $objOid=$objTc=$objDispHint=$objSyntax=$objDesc=$objEnum=NULL;
1615
-	        $tmpdesc='';$indesc=false;
1615
+	        $tmpdesc=''; $indesc=false;
1616 1616
 	        
1617 1617
 	        $objMib=$trapmib;
1618
-	        exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.
1619
-	            ' -On -Td '.$objMib.'::'.$object . ' 2>/dev/null',$snmptrans,$retVal);
1620
-	        if ($retVal!=0)
1618
+	        exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs.
1619
+	            ' -On -Td '.$objMib.'::'.$object.' 2>/dev/null', $snmptrans, $retVal);
1620
+	        if ($retVal != 0)
1621 1621
 	        {
1622 1622
 	            // Maybe not trap mib, search with IR
1623
-	            exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.
1624
-	                ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal);
1625
-	            if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match))
1623
+	            exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs.
1624
+	                ' -IR '.$object.' 2>/dev/null', $snmptrans, $retVal);
1625
+	            if ($retVal != 0 || !preg_match('/(.*)::(.*)/', $snmptrans[0], $match))
1626 1626
 	            { // Not found -> continue with warning
1627
-	               $this->trapLog('Error finding trap object : '.$trapmib.'::'.$object,2,'');
1627
+	               $this->trapLog('Error finding trap object : '.$trapmib.'::'.$object, 2, '');
1628 1628
 	               continue;
1629 1629
 	            }
1630 1630
 	            $objMib=$match[1];
1631 1631
 	            
1632 1632
 	            // Do the snmptranslate again.
1633
-	            exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.
1634
-	                ' -On -Td '.$objMib.'::'.$object,$snmptrans,$retVal);
1635
-	            if ($retVal!=0) {
1636
-	                $this->trapLog('Error finding trap object : '.$objMib.'::'.$object,2,'');
1633
+	            exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs.
1634
+	                ' -On -Td '.$objMib.'::'.$object, $snmptrans, $retVal);
1635
+	            if ($retVal != 0) {
1636
+	                $this->trapLog('Error finding trap object : '.$objMib.'::'.$object, 2, '');
1637 1637
 	            }
1638 1638
 	            
1639 1639
 	        }
1640 1640
 	        foreach ($snmptrans as $line)
1641 1641
 	        {
1642
-	            if ($indesc==true)
1642
+	            if ($indesc == true)
1643 1643
 	            {
1644
-	                $line=preg_replace('/[\t ]+/',' ',$line);
1645
-	                if (preg_match('/(.*)"$/', $line,$match))
1644
+	                $line=preg_replace('/[\t ]+/', ' ', $line);
1645
+	                if (preg_match('/(.*)"$/', $line, $match))
1646 1646
 	                {
1647
-	                    $objDesc = $tmpdesc . $match[1];
1647
+	                    $objDesc=$tmpdesc.$match[1];
1648 1648
 	                    $indesc=false;
1649 1649
 	                }
1650 1650
 	                $tmpdesc.=$line;
@@ -1655,43 +1655,43 @@  discard block
 block discarded – undo
1655 1655
 	                $objOid=$line;
1656 1656
 	                continue;
1657 1657
 	            }
1658
-	            if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/',$line,$match))
1658
+	            if (preg_match('/^[\t ]+SYNTAX[\t ]+([^{]*) \{(.*)\}/', $line, $match))
1659 1659
 	            {
1660 1660
 	                $objSyntax=$match[1];
1661 1661
                     $objEnum=$match[2];
1662 1662
 	                continue;
1663 1663
 	            }
1664
-	            if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/',$line,$match))
1664
+	            if (preg_match('/^[\t ]+SYNTAX[\t ]+(.*)/', $line, $match))
1665 1665
 	            {
1666 1666
 	                $objSyntax=$match[1];
1667 1667
 	                continue;
1668 1668
 	            }
1669
-	            if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/',$line,$match))
1669
+	            if (preg_match('/^[\t ]+DISPLAY-HINT[\t ]+"(.*)"/', $line, $match))
1670 1670
 	            {
1671 1671
 	                $objDispHint=$match[1];
1672 1672
 	                continue;
1673 1673
 	            }
1674
-	            if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/',$line,$match))
1674
+	            if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)"/', $line, $match))
1675 1675
 	            {
1676 1676
 	                $objDesc=$match[1];
1677 1677
 	                continue;
1678 1678
 	            }
1679
-	            if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$line,$match))
1679
+	            if (preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/', $line, $match))
1680 1680
 	            {
1681 1681
 	                $tmpdesc=$match[1];
1682 1682
 	                $indesc=true;
1683 1683
 	                continue;
1684 1684
 	            }
1685
-	            if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/',$line,$match))
1685
+	            if (preg_match('/^[\t ]+-- TEXTUAL CONVENTION[\t ]+(.*)/', $line, $match))
1686 1686
 	            {
1687 1687
 	                $objTc=$match[1];
1688 1688
 	                continue;
1689 1689
 	            }
1690 1690
 	        }
1691
-	        $this->trapLog("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc",4,'');
1691
+	        $this->trapLog("Adding trap $object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc", 4, '');
1692 1692
 	        //echo "$object : $objOid / $objSyntax / $objEnum / $objDispHint / $objTc / $objDesc\n";
1693 1693
 	        // Update 
1694
-	        $this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum,$objDesc);
1694
+	        $this->update_oid($objOid, $objMib, $object, '3', $objTc, $objDispHint, $objSyntax, $objEnum, $objDesc);
1695 1695
             
1696 1696
 	        if (isset($dbObjects[$this->dbOidIndex[$objOid]['id']]))
1697 1697
 	        {   // if link exists, continue
@@ -1712,7 +1712,7 @@  discard block
 block discarded – undo
1712 1712
 	        );
1713 1713
 	        
1714 1714
 	        if ($sqlQuery->execute($sqlParam) == FALSE) {
1715
-	            $this->trapLog('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$objOid]['id'] ,1,'');
1715
+	            $this->trapLog('Error adding trap object : '.$sql.' / '.$trapId.'/'.$this->dbOidIndex[$objOid]['id'], 1, '');
1716 1716
 	        }
1717 1717
 	    }
1718 1718
 	    if ($check_existing == true)
@@ -1729,19 +1729,19 @@  discard block
 block discarded – undo
1729 1729
 	 * @param boolean $onlyTraps : only cache traps and objects (true) or all (false)
1730 1730
 	 * @param string $startOID : only cache under startOID (NOT IMPLEMENTED)
1731 1731
 	*/	
1732
-	public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1')
1732
+	public function update_mib_database($display_progress=false, $check_change=false, $onlyTraps=true, $startOID='.1')
1733 1733
 	{
1734 1734
 		// Timing 
1735
-		$timeTaken = microtime(true);
1735
+		$timeTaken=microtime(true);
1736 1736
 		$retVal=0;
1737 1737
 		// Get all mib objects from all mibs
1738
-		$snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.' -On -Tto 2>/dev/null';
1739
-		$this->trapLog('Getting all traps : '.$snmpCommand,4,'');
1738
+		$snmpCommand=$this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs.' -On -Tto 2>/dev/null';
1739
+		$this->trapLog('Getting all traps : '.$snmpCommand, 4, '');
1740 1740
 		unset($this->objectsAll);
1741
-		exec($snmpCommand,$this->objectsAll,$retVal);		
1742
-		if ($retVal!=0)
1741
+		exec($snmpCommand, $this->objectsAll, $retVal);		
1742
+		if ($retVal != 0)
1743 1743
 		{
1744
-			$this->trapLog('error executing snmptranslate',ERROR,'');
1744
+			$this->trapLog('error executing snmptranslate', ERROR, '');
1745 1745
 		}
1746 1746
 		
1747 1747
 		// Get all mibs from databse to have a memory index
@@ -1749,14 +1749,14 @@  discard block
 block discarded – undo
1749 1749
 		$db_conn=$this->db_connect_trap();
1750 1750
 		
1751 1751
 		$sql='SELECT * from '.$this->db_prefix.'mib_cache;';
1752
-		$this->trapLog('SQL query : '.$sql,4,'');
1752
+		$this->trapLog('SQL query : '.$sql, 4, '');
1753 1753
 		if (($ret_code=$db_conn->query($sql)) == FALSE) {
1754
-			$this->trapLog('No result in query : ' . $sql,ERROR,'');
1754
+			$this->trapLog('No result in query : '.$sql, ERROR, '');
1755 1755
 		}
1756 1756
 		$this->dbOidAll=$ret_code->fetchAll();
1757 1757
 		$this->dbOidIndex=array();
1758 1758
 		// Create the index for db;
1759
-		foreach($this->dbOidAll as $key=>$val)
1759
+		foreach ($this->dbOidAll as $key=>$val)
1760 1760
 		{
1761 1761
 			$this->dbOidIndex[$val['oid']]['key']=$key;
1762 1762
 			$this->dbOidIndex[$val['oid']]['id']=$val['id'];
@@ -1764,11 +1764,11 @@  discard block
 block discarded – undo
1764 1764
 		
1765 1765
 		// Count elements to show progress
1766 1766
 		$numElements=count($this->objectsAll);
1767
-		$this->trapLog('Total snmp objects returned by snmptranslate : '.$numElements,3,'');
1767
+		$this->trapLog('Total snmp objects returned by snmptranslate : '.$numElements, 3, '');
1768 1768
 		
1769
-		$step=$basestep=$numElements/10; // output display of % done
1769
+		$step=$basestep=$numElements / 10; // output display of % done
1770 1770
 		$num_step=0;
1771
-		$timeFiveSec = microtime(true); // Used for display a '.' every <n> seconds
1771
+		$timeFiveSec=microtime(true); // Used for display a '.' every <n> seconds
1772 1772
 		
1773 1773
 		// Create index for trap objects
1774 1774
 		$this->trapObjectsIndex=array();
@@ -1778,28 +1778,28 @@  discard block
 block discarded – undo
1778 1778
 		$time_parse1N=$time_check1N=$time_check2N=$time_check3N=$time_updateN=$time_objectsN=0;
1779 1779
 		$time_num_traps=0;
1780 1780
 		
1781
-		for ($curElement=0;$curElement < $numElements;$curElement++)
1781
+		for ($curElement=0; $curElement < $numElements; $curElement++)
1782 1782
 		{
1783
-		    $time_1= microtime(true);
1784
-			if ((microtime(true)-$timeFiveSec) > 2 && $display_progress)
1783
+		    $time_1=microtime(true);
1784
+			if ((microtime(true) - $timeFiveSec) > 2 && $display_progress)
1785 1785
 			{ // echo a . every 2 sec
1786 1786
 				echo '.';
1787
-				$timeFiveSec = microtime(true);
1787
+				$timeFiveSec=microtime(true);
1788 1788
 			}
1789
-			if ($curElement>$step) 
1789
+			if ($curElement > $step) 
1790 1790
 			{ // display progress
1791 1791
 				$num_step++;
1792 1792
 				$step+=$basestep;
1793 1793
 				if ($display_progress)
1794 1794
 				{				
1795
-					echo "\n" . ($num_step*10). '% : ';
1795
+					echo "\n".($num_step * 10).'% : ';
1796 1796
 				}
1797 1797
 			}
1798 1798
 			// Get oid or pass if not found
1799
-			if (!preg_match('/^\.[0-9\.]+$/',$this->objectsAll[$curElement]))
1799
+			if (!preg_match('/^\.[0-9\.]+$/', $this->objectsAll[$curElement]))
1800 1800
 			{
1801
-			    $time_parse1 += microtime(true) - $time_1;
1802
-			    $time_parse1N ++;
1801
+			    $time_parse1+=microtime(true) - $time_1;
1802
+			    $time_parse1N++;
1803 1803
 				continue;
1804 1804
 			}
1805 1805
 			$oid=$this->objectsAll[$curElement];
@@ -1808,9 +1808,9 @@  discard block
 block discarded – undo
1808 1808
 			$curElement++;
1809 1809
 			$match=$snmptrans=array();
1810 1810
 			if (!preg_match('/ +([^\(]+)\(.+\) type=([0-9]+)( tc=([0-9]+))?( hint=(.+))?/',
1811
-						$this->objectsAll[$curElement],$match))
1811
+						$this->objectsAll[$curElement], $match))
1812 1812
 			{
1813
-			    $time_check1 += microtime(true) - $time_1;
1813
+			    $time_check1+=microtime(true) - $time_1;
1814 1814
 				$time_check1N++;
1815 1815
 				continue;
1816 1816
 			}
@@ -1818,21 +1818,21 @@  discard block
 block discarded – undo
1818 1818
 			$name=$match[1]; // Name 
1819 1819
 			$type=$match[2]; // type (21=trap, 0: may be trap, else : not trap
1820 1820
 			
1821
-			if ($type==0) // object type=0 : check if v1 trap
1821
+			if ($type == 0) // object type=0 : check if v1 trap
1822 1822
 			{
1823 1823
 				// Check if next is suboid -> in that case is cannot be a trap
1824
-				if (preg_match("/^$oid/",$this->objectsAll[$curElement+1]))
1824
+				if (preg_match("/^$oid/", $this->objectsAll[$curElement + 1]))
1825 1825
 				{
1826
-				    $time_check2 += microtime(true) - $time_1;
1826
+				    $time_check2+=microtime(true) - $time_1;
1827 1827
 				    $time_check2N++;
1828 1828
 					continue;
1829 1829
 				}		
1830 1830
 				unset($snmptrans);
1831
-				exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.
1832
-					' -Td '.$oid . ' | grep OBJECTS ',$snmptrans,$retVal);
1833
-				if ($retVal!=0)
1831
+				exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs.
1832
+					' -Td '.$oid.' | grep OBJECTS ', $snmptrans, $retVal);
1833
+				if ($retVal != 0)
1834 1834
 				{
1835
-				    $time_check2 += microtime(true) - $time_1;
1835
+				    $time_check2+=microtime(true) - $time_1;
1836 1836
 				    $time_check2N++;
1837 1837
 					continue;
1838 1838
 				}
@@ -1840,56 +1840,56 @@  discard block
 block discarded – undo
1840 1840
 				// Force as trap.
1841 1841
 				$type=21;
1842 1842
 			}
1843
-			if ($onlyTraps==true && $type!=21) // if only traps and not a trap, continue
1843
+			if ($onlyTraps == true && $type != 21) // if only traps and not a trap, continue
1844 1844
 			{
1845
-			    $time_check3 += microtime(true) - $time_1;
1845
+			    $time_check3+=microtime(true) - $time_1;
1846 1846
 				$time_check3N++;
1847 1847
 				continue;
1848 1848
 			}
1849 1849
 			
1850 1850
 			$time_num_traps++;
1851 1851
 			
1852
-			$this->trapLog('Found trap : '.$match[1] . ' / OID : '.$oid,3,'');
1852
+			$this->trapLog('Found trap : '.$match[1].' / OID : '.$oid, 3, '');
1853 1853
 			if ($display_progress) echo '#'; // echo a # when trap found
1854 1854
 				
1855 1855
 			// get trap objects & source MIB
1856 1856
 			unset($snmptrans);
1857
-			exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.
1858
-					' -Td '.$oid,$snmptrans,$retVal);
1859
-			if ($retVal!=0)
1857
+			exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs.
1858
+					' -Td '.$oid, $snmptrans, $retVal);
1859
+			if ($retVal != 0)
1860 1860
 			{
1861
-				$this->trapLog('error executing snmptranslate',ERROR,'');
1861
+				$this->trapLog('error executing snmptranslate', ERROR, '');
1862 1862
 			}
1863 1863
 			
1864
-			if (!preg_match('/^(.*)::/',$snmptrans[0],$match))
1864
+			if (!preg_match('/^(.*)::/', $snmptrans[0], $match))
1865 1865
 			{
1866
-			    $this->trapLog('Error getting mib from trap '.$oid.' : ' . $snmptrans[0],1,'');
1866
+			    $this->trapLog('Error getting mib from trap '.$oid.' : '.$snmptrans[0], 1, '');
1867 1867
 			}
1868 1868
 			$trapMib=$match[1];
1869 1869
 			
1870
-			$numLine=1;$trapDesc='';
1871
-			while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++;
1870
+			$numLine=1; $trapDesc='';
1871
+			while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/', $snmptrans[$numLine], $match)) $numLine++;
1872 1872
 			if (isset($snmptrans[$numLine]))
1873 1873
 			{
1874
-			    $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]);
1874
+			    $snmptrans[$numLine]=preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/', '', $snmptrans[$numLine]);
1875 1875
 
1876
-			    while (isset($snmptrans[$numLine]) && !preg_match('/"/',$snmptrans[$numLine]))
1876
+			    while (isset($snmptrans[$numLine]) && !preg_match('/"/', $snmptrans[$numLine]))
1877 1877
 			    {
1878
-			        $trapDesc.=preg_replace('/[\t ]+/',' ',$snmptrans[$numLine]);
1878
+			        $trapDesc.=preg_replace('/[\t ]+/', ' ', $snmptrans[$numLine]);
1879 1879
 			        $numLine++;
1880 1880
 			    }
1881 1881
 			    if (isset($snmptrans[$numLine])) {
1882
-			        $trapDesc.=preg_replace('/".*/','',$snmptrans[$numLine]);
1883
-			        $trapDesc=preg_replace('/[\t ]+/',' ',$trapDesc);
1882
+			        $trapDesc.=preg_replace('/".*/', '', $snmptrans[$numLine]);
1883
+			        $trapDesc=preg_replace('/[\t ]+/', ' ', $trapDesc);
1884 1884
 			    }
1885 1885
 
1886 1886
 			}
1887
-			$update=$this->update_oid($oid,$trapMib,$name,$type,NULL,NULL,NULL,NULL,$trapDesc);
1888
-			$time_update += microtime(true) - $time_1; $time_1= microtime(true);
1887
+			$update=$this->update_oid($oid, $trapMib, $name, $type, NULL, NULL, NULL, NULL, $trapDesc);
1888
+			$time_update+=microtime(true) - $time_1; $time_1=microtime(true);
1889 1889
 			
1890
-			if (($update==0) && ($check_change==false))
1890
+			if (($update == 0) && ($check_change == false))
1891 1891
 			{ // Trapd didn't change & force check disabled
1892
-			    $time_objects += microtime(true) - $time_1;
1892
+			    $time_objects+=microtime(true) - $time_1;
1893 1893
 			    if ($display_progress) echo "C";
1894 1894
 			    continue;
1895 1895
 			}
@@ -1897,7 +1897,7 @@  discard block
 block discarded – undo
1897 1897
 			$synt=null;
1898 1898
 			foreach ($snmptrans as $line)
1899 1899
 			{	
1900
-    			if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match))
1900
+    			if (preg_match('/OBJECTS.*\{([^\}]+)\}/', $line, $match))
1901 1901
     				{
1902 1902
     					$synt=$match[1];
1903 1903
     				}
@@ -1905,30 +1905,30 @@  discard block
 block discarded – undo
1905 1905
 			if ($synt == null) 
1906 1906
 			{
1907 1907
 				//echo "No objects for $trapOID\n";
1908
-			    $time_objects += microtime(true) - $time_1;
1908
+			    $time_objects+=microtime(true) - $time_1;
1909 1909
 				continue;
1910 1910
 			}
1911 1911
 			//echo "$synt \n";
1912 1912
 			$trapObjects=array();
1913
-			while (preg_match('/ *([^ ,]+) *,* */',$synt,$match))
1913
+			while (preg_match('/ *([^ ,]+) *,* */', $synt, $match))
1914 1914
 			{
1915
-				array_push($trapObjects,$match[1]);
1916
-				$synt=preg_replace('/'.$match[0].'/','',$synt);
1915
+				array_push($trapObjects, $match[1]);
1916
+				$synt=preg_replace('/'.$match[0].'/', '', $synt);
1917 1917
 			}
1918 1918
 			
1919 1919
 			$this->trap_objects($oid, $trapMib, $trapObjects, false);
1920 1920
 			
1921
-			$time_objects += microtime(true) - $time_1;
1921
+			$time_objects+=microtime(true) - $time_1;
1922 1922
 			$time_objectsN++;
1923 1923
 		}
1924 1924
 		
1925 1925
 		if ($display_progress)
1926 1926
 		{
1927 1927
     		echo "\nNumber of processed traps : $time_num_traps \n";
1928
-    		echo "\nParsing : " . number_format($time_parse1+$time_check1,1) ." sec / " . ($time_parse1N+ $time_check1N)  . " occurences\n";
1929
-    		echo "Detecting traps : " . number_format($time_check2+$time_check3,1) . " sec / " . ($time_check2N+$time_check3N) ." occurences\n";
1930
-    		echo "Trap processing ($time_updateN): ".number_format($time_update,1)." sec , ";
1931
-    		echo "Objects processing ($time_objectsN) : ".number_format($time_objects,1)." sec \n";
1928
+    		echo "\nParsing : ".number_format($time_parse1 + $time_check1, 1)." sec / ".($time_parse1N + $time_check1N)." occurences\n";
1929
+    		echo "Detecting traps : ".number_format($time_check2 + $time_check3, 1)." sec / ".($time_check2N + $time_check3N)." occurences\n";
1930
+    		echo "Trap processing ($time_updateN): ".number_format($time_update, 1)." sec , ";
1931
+    		echo "Objects processing ($time_objectsN) : ".number_format($time_objects, 1)." sec \n";
1932 1932
 		}
1933 1933
 		
1934 1934
 		// Timing ends
Please login to merge, or discard this patch.
Braces   +122 added lines, -78 removed lines patch added patch discarded remove patch
@@ -96,8 +96,7 @@  discard block
 block discarded – undo
96 96
 	        }
97 97
 	        $this->trapLog($message,$log_level,'syslog');
98 98
 	        return false;
99
-	    }
100
-	    else
99
+	    } else
101 100
 	    {
102 101
 	        $option_var=$option_array[$option_category][$option_name];
103 102
 	        return true;
@@ -144,7 +143,9 @@  discard block
 block discarded – undo
144 143
 	protected function getDBConfigIfSet($element,&$variable)
145 144
 	{
146 145
 		$value=$this->getDBConfig($element);
147
-		if ($value != 'null') $variable=$value;
146
+		if ($value != 'null') {
147
+			$variable=$value;
148
+		}
148 149
 	}
149 150
 	
150 151
 	/** Get data from db_config
@@ -180,8 +181,11 @@  discard block
 block discarded – undo
180 181
 			$message = '['.  date("Y/m/d H:i:s") . '] ' .
181 182
 				'['. basename(__FILE__) . '] ['.$this->debug_text[$level].']: ' .$message . "\n";
182 183
 			
183
-			if ( $destination != '' ) $output=$destination;
184
-			else $output=$this->alert_output;
184
+			if ( $destination != '' ) {
185
+				$output=$destination;
186
+			} else {
187
+				$output=$this->alert_output;
188
+			}
185 189
 			switch ($output)
186 190
 			{
187 191
 				case 'file':
@@ -216,7 +220,9 @@  discard block
 block discarded – undo
216 220
 		switch ($output_type)
217 221
 		{
218 222
 			case 'file':
219
-			    if ($output_option == null) throw new Exception("File logging without file !");
223
+			    if ($output_option == null) {
224
+			    	throw new Exception("File logging without file !");
225
+			    }
220 226
 				$this->debug_file=$output_option;
221 227
 				$this->alert_output='file';
222 228
 				break;
@@ -311,16 +317,14 @@  discard block
 block discarded – undo
311 317
 				$this->trapLog("No Config/database in config file: ".$this->trap_module_config,ERROR,''); 
312 318
 			}
313 319
 			$db_name=$trap_config['config']['database'];
314
-		} 
315
-		else if ($database == 'ido')
320
+		} else if ($database == 'ido')
316 321
 		{
317 322
 			if (!isset($trap_config['config']['IDOdatabase'])) 
318 323
 			{
319 324
 				$this->trapLog("No Config/IDOdatabase in config file: ".$this->trap_module_config,ERROR,''); 
320 325
 			}
321 326
 			$db_name=$trap_config['config']['IDOdatabase'];		
322
-		}
323
-		else
327
+		} else
324 328
 		{
325 329
 			$this->trapLog("Unknown database type : ".$database,ERROR,''); 		
326 330
 		}	
@@ -339,8 +343,12 @@  discard block
 block discarded – undo
339 343
 		$db_sql_name=$db_config[$db_name]['dbname'];
340 344
 		$db_user=$db_config[$db_name]['username'];
341 345
 		$db_pass=$db_config[$db_name]['password'];
342
-		if ($database == 'traps') $this->trapDBType = $db_type;
343
-		if ($database == 'ido') $this->idoDBType = $db_type;
346
+		if ($database == 'traps') {
347
+			$this->trapDBType = $db_type;
348
+		}
349
+		if ($database == 'ido') {
350
+			$this->idoDBType = $db_type;
351
+		}
344 352
 		
345 353
 		$this->trapLog( "DB selected : $db_type $db_host $db_sql_name $db_user",3,''); 
346 354
 		return array($db_type,$db_host,$db_sql_name,$db_user,$db_pass);
@@ -381,8 +389,7 @@  discard block
 block discarded – undo
381 389
 		{
382 390
 		    $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)");
383 391
 			$this->trapLog('Error parsing IP : '.$IP,ERROR,'');
384
-		} 
385
-		else 
392
+		} else 
386 393
 		{		
387 394
 			$this->trap_data['source_ip']=$matches[1];
388 395
 			$this->trap_data['destination_ip']=$matches[3];
@@ -396,14 +403,12 @@  discard block
 block discarded – undo
396 403
 			if ($ret_code===0 || $ret_code===false) 
397 404
 			{
398 405
 				$this->trapLog('No match on trap data : '.$vars,WARN,'');
399
-			}
400
-			else 
406
+			} else 
401 407
 			{
402 408
 			    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'))
403 409
 				{
404 410
 					$this->trap_data['trap_oid']=$matches[2];				
405
-				}
406
-				else
411
+				} else
407 412
 				{
408 413
 					$object= new stdClass;
409 414
 					$object->oid =$matches[1];
@@ -574,7 +579,9 @@  discard block
 block discarded – undo
574 579
 	            }
575 580
 	            
576 581
 	            $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()'];
577
-	            if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
582
+	            if ($inserted_id==false) {
583
+	            	throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
584
+	            }
578 585
 	            $this->trap_id=$inserted_id;
579 586
 	            break;
580 587
 	        default:
@@ -590,7 +597,9 @@  discard block
 block discarded – undo
590 597
 	{
591 598
 		
592 599
 		// If action is ignore -> don't send t DB
593
-		if ($this->trap_to_db == false) return;
600
+		if ($this->trap_to_db == false) {
601
+			return;
602
+		}
594 603
 		
595 604
 		
596 605
 		$db_conn=$this->db_connect_trap();
@@ -647,7 +656,9 @@  discard block
 block discarded – undo
647 656
 				}
648 657
 
649 658
 				$inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()'];
650
-				if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
659
+				if ($inserted_id==false) {
660
+					throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
661
+				}
651 662
 				$this->trap_id=$inserted_id;
652 663
 			break;
653 664
 			default: 
@@ -776,8 +787,7 @@  discard block
 block discarded – undo
776 787
     		// TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default....
777 788
     		exec('echo "'.$send.'" > ' .$this->icinga2cmd);
778 789
     		return true;
779
-	    }
780
-	    else
790
+	    } else
781 791
 	    {
782 792
 	        $api = $this->getAPI();
783 793
 	        $api->setCredentials($this->api_username, $this->api_password);
@@ -786,8 +796,7 @@  discard block
 block discarded – undo
786 796
 	        {
787 797
 	            $this->trapLog( "Error sending result : " .$retmessage,WARN,'');
788 798
 	            return false;
789
-	        }
790
-	        else 
799
+	        } else 
791 800
 	        {
792 801
 	            $this->trapLog( "Sent result : " .$retmessage,3,'');
793 802
 	            return true;
@@ -847,7 +856,9 @@  discard block
 block discarded – undo
847 856
 	/***************** Eval & tokenizer functions ****************/
848 857
 	protected function eval_getElement($rule,&$item)
849 858
 	{
850
-		while ($rule[$item]==' ') $item++;
859
+		while ($rule[$item]==' ') {
860
+			$item++;
861
+		}
851 862
 		if (preg_match('/[0-9\.]/',$rule[$item]))
852 863
 		{ // number
853 864
 	
@@ -874,14 +885,15 @@  discard block
 block discarded – undo
874 885
 			$start=$item;
875 886
 			$parenthesis_count=0; 
876 887
 			while (($item < strlen($rule)) // Not end of string AND
877
-			      && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded ()
888
+			      && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) {
889
+				// Closing ')' or embeded ()
878 890
 			{ 
879 891
 				if ($rule[$item] == '"' )
880 892
 				{ // pass through string
881 893
 					$item++;
894
+			}
882 895
 					$item=$this->eval_getNext($rule,$item,'"');
883
-				} 
884
-				else{
896
+				} else{
885 897
 				    if ($rule[$item] == '(')
886 898
 				    {
887 899
 				        $parenthesis_count++;
@@ -909,13 +921,17 @@  discard block
 block discarded – undo
909 921
 	protected function eval_getNext($rule,$item,$tok)
910 922
 	{
911 923
 		while (($rule[$item] != $tok ) && ($item < strlen($rule))) { $item++;}
912
-		if ($item==strlen($rule)) throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item);
924
+		if ($item==strlen($rule)) {
925
+			throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item);
926
+		}
913 927
 		return $item+1;
914 928
 	}
915 929
 	
916 930
 	protected function eval_getOper($rule,&$item)
917 931
 	{
918
-		while ($rule[$item]==' ') $item++;
932
+		while ($rule[$item]==' ') {
933
+			$item++;
934
+		}
919 935
 		switch ($rule[$item])
920 936
 		{
921 937
 			case '<':
@@ -951,12 +967,13 @@  discard block
 block discarded – undo
951 967
 	public function evaluation($rule,&$item)
952 968
 	{
953 969
 	    //echo "Evaluation of ".substr($rule,$item)."\n";
954
-		if ( $rule[$item] == '!') // If '!' found, negate next expression.
970
+		if ( $rule[$item] == '!') {
971
+			// If '!' found, negate next expression.
955 972
 		{
956 973
 		    $negate=true;
957
-		    $item++;
958 974
 		}
959
-		else
975
+		    $item++;
976
+		} else
960 977
 		{
961 978
 		    $negate=false;
962 979
 		}
@@ -964,10 +981,14 @@  discard block
 block discarded – undo
964 981
 		list($type1,$val1) = $this->eval_getElement($rule,$item);
965 982
 		//echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n";
966 983
 		
967
-		if ($item==strlen($rule)) // If only element, return value, but only boolean
984
+		if ($item==strlen($rule)) {
985
+			// If only element, return value, but only boolean
968 986
 		{
969 987
 		  if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule);
970
-		  if ($negate == true) $val1= ! $val1;
988
+		}
989
+		  if ($negate == true) {
990
+		  	$val1= ! $val1;
991
+		  }
971 992
 		  return $val1;
972 993
 		}  
973 994
 		
@@ -976,27 +997,34 @@  discard block
 block discarded – undo
976 997
 		//echo "Comp : ".$comp." : ".substr($rule,$item)."\n";
977 998
         
978 999
 		// Third element : number, string or ()
979
-		if ( $rule[$item] == '!') // starts with a ! so evaluate whats next
1000
+		if ( $rule[$item] == '!') {
1001
+			// starts with a ! so evaluate whats next
980 1002
 		{
981 1003
 		    $item++;
982
-		    if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule);
1004
+		}
1005
+		    if ($typec != 1) {
1006
+		    	throw new Exception("Mixing boolean and comparison : ".$rule);
1007
+		    }
983 1008
 		    $val2= ! $this->evaluation($rule,$item);
984 1009
 		    $type2=2; // result is a boolean 
985
-		}
986
-		else 
1010
+		} else 
987 1011
 		{
988 1012
 		    list($type2,$val2) = $this->eval_getElement($rule,$item);
989 1013
 		}
990 1014
 		//echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n";
991 1015
 		
992
-		if ($type1!=$type2)  // cannot compare different types
1016
+		if ($type1!=$type2) {
1017
+			// cannot compare different types
993 1018
 		{ 
994 1019
 		    throw new Exception("Cannot compare string & number : ".$rule);
995 1020
 		}
996
-		if ($typec==1 && $type1 !=2) // cannot use & or | with string/number
1021
+		}
1022
+		if ($typec==1 && $type1 !=2) {
1023
+			// cannot use & or | with string/number
997 1024
 		{
998 1025
 		    throw new Exception("Cannot use boolean operators with string & number : ".$rule);
999 1026
 		}
1027
+		}
1000 1028
 		
1001 1029
 		switch ($comp){
1002 1030
 			case '<':	$retVal= ($val1 < $val2); break;
@@ -1010,9 +1038,15 @@  discard block
 block discarded – undo
1010 1038
 			case '&':	$retVal= ($val1 && $val2); break;
1011 1039
 			default:  throw new Exception("Error in expression - unknown comp : ".$comp);
1012 1040
 		}
1013
-		if ($negate == true) $retVal = ! $retVal; // Inverse result if negate before expression
1041
+		if ($negate == true) {
1042
+			$retVal = ! $retVal;
1043
+		}
1044
+		// Inverse result if negate before expression
1014 1045
 		
1015
-		if ($item==strlen($rule)) return $retVal; // End of string : return evaluation
1046
+		if ($item==strlen($rule)) {
1047
+			return $retVal;
1048
+		}
1049
+		// End of string : return evaluation
1016 1050
 		// check for logical operator :
1017 1051
 		switch ($rule[$item])
1018 1052
 		{
@@ -1039,7 +1073,9 @@  discard block
 block discarded – undo
1039 1073
 					$rule2.=$rule[$item];
1040 1074
 					$item++;
1041 1075
 				}
1042
-				if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item);
1076
+				if ($item == strlen ($rule)) {
1077
+					throw new Exception("closing '\"' not found in ".$rule ." at " .$item);
1078
+				}
1043 1079
 				$rule2.=$rule[$item];
1044 1080
 				$item++;
1045 1081
 				continue;
@@ -1059,10 +1095,12 @@  discard block
 block discarded – undo
1059 1095
 	
1060 1096
 	protected function eval_rule($rule)
1061 1097
 	{
1062
-		if ($rule==null || $rule == '') // Empty rule is always true
1098
+		if ($rule==null || $rule == '') {
1099
+			// Empty rule is always true
1063 1100
 		{
1064 1101
 			return true;
1065 1102
 		}
1103
+		}
1066 1104
 		$matches=array();
1067 1105
 		while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1)
1068 1106
 		{
@@ -1149,20 +1187,17 @@  discard block
 block discarded – undo
1149 1187
 						if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false)
1150 1188
 						{
1151 1189
 						    $this->trap_action.='Error sending status : check cmd/API';
1152
-						}
1153
-						else
1190
+						} else
1154 1191
 						{
1155 1192
 						    $this->add_rule_match($rule['id'],$rule['num_match']+1);
1156 1193
 						    $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name;
1157 1194
 						}
1158
-					}
1159
-					else
1195
+					} else
1160 1196
 					{
1161 1197
 						$this->add_rule_match($rule['id'],$rule['num_match']+1);
1162 1198
 					}
1163 1199
 					$this->trap_to_db=($action==-2)?false:true;
1164
-				}
1165
-				else
1200
+				} else
1166 1201
 				{
1167 1202
 					//$this->trapLog('rules KOO : '.print_r($rule),3,'');
1168 1203
 					
@@ -1173,14 +1208,12 @@  discard block
 block discarded – undo
1173 1208
 					    if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false)
1174 1209
 					    {
1175 1210
 					        $this->trap_action.='Error sending status : check cmd/API';
1176
-					    }
1177
-					    else
1211
+					    } else
1178 1212
 					    {
1179 1213
     						$this->add_rule_match($rule['id'],$rule['num_match']+1);
1180 1214
     						$this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name;
1181 1215
 					    }
1182
-					}
1183
-					else
1216
+					} else
1184 1217
 					{
1185 1218
 						$this->add_rule_match($rule['id'],$rule['num_match']+1);
1186 1219
 					}
@@ -1190,16 +1223,14 @@  discard block
 block discarded – undo
1190 1223
 				if (!isset($this->trap_data['source_name']))
1191 1224
 				{
1192 1225
 					$this->trap_data['source_name']=$rule['host_name'];
1193
-				}
1194
-				else
1226
+				} else
1195 1227
 				{
1196 1228
 					if (!preg_match('/'.$rule['host_name'].'/',$this->trap_data['source_name']))
1197 1229
 					{ // only add if not present
1198 1230
 						$this->trap_data['source_name'].=','.$rule['host_name'];
1199 1231
 					}
1200 1232
 				}
1201
-			}
1202
-			catch (Exception $e) 
1233
+			} catch (Exception $e) 
1203 1234
 			{ 
1204 1235
 			    $this->trapLog('Error in rule eval : '.$e->getMessage(),WARN,'');
1205 1236
 			    $this->trap_action.=' ERR : '.$e->getMessage();
@@ -1210,8 +1241,7 @@  discard block
 block discarded – undo
1210 1241
 		if ($this->trap_data['status']=='error')
1211 1242
 		{
1212 1243
 		  $this->trap_to_db=true; // Always put errors in DB for the use can see
1213
-		}
1214
-		else
1244
+		} else
1215 1245
 		{
1216 1246
 		  $this->trap_data['status']='done';
1217 1247
 		}
@@ -1265,8 +1295,7 @@  discard block
 block discarded – undo
1265 1295
                 if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array))
1266 1296
                 {
1267 1297
                     $cur_table='table '.$cur_table_array[1];
1268
-                }
1269
-                else
1298
+                } else
1270 1299
                 {
1271 1300
                     $cur_table='secret SQL stuff :-)';
1272 1301
                 }
@@ -1310,8 +1339,7 @@  discard block
 block discarded – undo
1310 1339
 	    if ($this->trapDBType == 'pgsql')
1311 1340
 	    {
1312 1341
 	        $prefix .= 'update_pgsql/schema_';
1313
-	    }
1314
-	    else
1342
+	    } else
1315 1343
 	    {
1316 1344
 	        $prefix .= 'update_sql/schema_';
1317 1345
 	    }
@@ -1357,7 +1385,10 @@  discard block
 block discarded – undo
1357 1385
 	       $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
1358 1386
 	       while (($line=fgets($input_stream)) != FALSE)
1359 1387
 	       {
1360
-	           if (preg_match('/^#/', $line)) continue; // ignore comment lines
1388
+	           if (preg_match('/^#/', $line)) {
1389
+	           	continue;
1390
+	           }
1391
+	           // ignore comment lines
1361 1392
 	           $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line));
1362 1393
 	           if (preg_match('/; *$/', $newline))
1363 1394
 	           {
@@ -1369,8 +1400,7 @@  discard block
 block discarded – undo
1369 1400
 	               if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array))
1370 1401
 	               {
1371 1402
 	                   $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2];
1372
-	               }
1373
-	               else
1403
+	               } else
1374 1404
 	               {
1375 1405
 	                   $cur_table='secret SQL stuff :-)';
1376 1406
 	                   $cur_table=$newline;
@@ -1508,8 +1538,7 @@  discard block
 block discarded – undo
1508 1538
 			    }
1509 1539
 			    $this->trapLog('Trap updated : '.$name . ' / OID : '.$oid,4,'');
1510 1540
 				return 1;
1511
-			}
1512
-			else
1541
+			} else
1513 1542
 			{
1514 1543
 			    $this->trapLog('Trap unchanged : '.$name . ' / OID : '.$oid,4,'');
1515 1544
 			    return 0;
@@ -1525,7 +1554,9 @@  discard block
 block discarded – undo
1525 1554
               'values (:oid, :name , :type ,:mib ,:tc , :display_hint'.
1526 1555
               ', :syntax, :type_enum, :description )';
1527 1556
         
1528
-		if ($this->trapDBType == 'pgsql') $sql .= 'RETURNING id';
1557
+		if ($this->trapDBType == 'pgsql') {
1558
+			$sql .= 'RETURNING id';
1559
+		}
1529 1560
 		
1530 1561
 		$sqlQuery=$db_conn->prepare($sql);
1531 1562
 		
@@ -1565,7 +1596,9 @@  discard block
 block discarded – undo
1565 1596
 		        }
1566 1597
 		        
1567 1598
 		        $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()'];
1568
-		        if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
1599
+		        if ($inserted_id==false) {
1600
+		        	throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
1601
+		        }
1569 1602
 		        $this->dbOidIndex[$oid]['id']=$inserted_id;
1570 1603
 		        break;
1571 1604
 		    default:
@@ -1818,12 +1851,14 @@  discard block
 block discarded – undo
1818 1851
 			$name=$match[1]; // Name 
1819 1852
 			$type=$match[2]; // type (21=trap, 0: may be trap, else : not trap
1820 1853
 			
1821
-			if ($type==0) // object type=0 : check if v1 trap
1854
+			if ($type==0) {
1855
+				// object type=0 : check if v1 trap
1822 1856
 			{
1823 1857
 				// Check if next is suboid -> in that case is cannot be a trap
1824 1858
 				if (preg_match("/^$oid/",$this->objectsAll[$curElement+1]))
1825 1859
 				{
1826 1860
 				    $time_check2 += microtime(true) - $time_1;
1861
+			}
1827 1862
 				    $time_check2N++;
1828 1863
 					continue;
1829 1864
 				}		
@@ -1840,9 +1875,11 @@  discard block
 block discarded – undo
1840 1875
 				// Force as trap.
1841 1876
 				$type=21;
1842 1877
 			}
1843
-			if ($onlyTraps==true && $type!=21) // if only traps and not a trap, continue
1878
+			if ($onlyTraps==true && $type!=21) {
1879
+				// if only traps and not a trap, continue
1844 1880
 			{
1845 1881
 			    $time_check3 += microtime(true) - $time_1;
1882
+			}
1846 1883
 				$time_check3N++;
1847 1884
 				continue;
1848 1885
 			}
@@ -1850,7 +1887,10 @@  discard block
 block discarded – undo
1850 1887
 			$time_num_traps++;
1851 1888
 			
1852 1889
 			$this->trapLog('Found trap : '.$match[1] . ' / OID : '.$oid,3,'');
1853
-			if ($display_progress) echo '#'; // echo a # when trap found
1890
+			if ($display_progress) {
1891
+				echo '#';
1892
+			}
1893
+			// echo a # when trap found
1854 1894
 				
1855 1895
 			// get trap objects & source MIB
1856 1896
 			unset($snmptrans);
@@ -1868,7 +1908,9 @@  discard block
 block discarded – undo
1868 1908
 			$trapMib=$match[1];
1869 1909
 			
1870 1910
 			$numLine=1;$trapDesc='';
1871
-			while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) $numLine++;
1911
+			while (isset($snmptrans[$numLine]) && !preg_match('/^[\t ]+DESCRIPTION[\t ]+"(.*)/',$snmptrans[$numLine],$match)) {
1912
+				$numLine++;
1913
+			}
1872 1914
 			if (isset($snmptrans[$numLine]))
1873 1915
 			{
1874 1916
 			    $snmptrans[$numLine] = preg_replace('/^[\t ]+DESCRIPTION[\t ]+"/','',$snmptrans[$numLine]);
@@ -1890,7 +1932,9 @@  discard block
 block discarded – undo
1890 1932
 			if (($update==0) && ($check_change==false))
1891 1933
 			{ // Trapd didn't change & force check disabled
1892 1934
 			    $time_objects += microtime(true) - $time_1;
1893
-			    if ($display_progress) echo "C";
1935
+			    if ($display_progress) {
1936
+			    	echo "C";
1937
+			    }
1894 1938
 			    continue;
1895 1939
 			}
1896 1940
 			
Please login to merge, or discard this patch.