Passed
Push — master ( 4d5cad...037a61 )
by Patrick
02:02
created
library/Trapdirector/TrapsProcess/Database.php 1 patch
Spacing   +60 added lines, -60 removed lines patch added patch discarded remove patch
@@ -35,16 +35,16 @@  discard block
 block discarded – undo
35 35
      * @param Logging $logClass : where to log
36 36
      * @param array $dbParam : array of named params  type,host,dbname,username,[port],[password]
37 37
      */
38
-    function __construct($logClass,$dbParam,$dbPrefix)
38
+    function __construct($logClass, $dbParam, $dbPrefix)
39 39
     {
40 40
         $this->logging=$logClass;
41 41
         $this->dbPrefix=$dbPrefix;
42 42
         
43 43
         $this->trapDSN=$this->setupDSN($dbParam);
44
-        $this->trapUsername = $dbParam['username'];
45
-        $this->trapPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:'';
44
+        $this->trapUsername=$dbParam['username'];
45
+        $this->trapPass=(array_key_exists('password', $dbParam)) ? $dbParam['password'] : '';
46 46
         $this->trapDBType=$dbParam['db'];
47
-        $this->logging->log('DSN : '.$this->trapDSN. ';user '.$this->trapUsername.' / prefix : '. $this->dbPrefix,INFO);
47
+        $this->logging->log('DSN : '.$this->trapDSN.';user '.$this->trapUsername.' / prefix : '.$this->dbPrefix, INFO);
48 48
         $this->db_connect_trap();
49 49
         
50 50
     }
@@ -56,9 +56,9 @@  discard block
 block discarded – undo
56 56
     public function setupIDO($dbParam)
57 57
     {
58 58
         $this->idoDSN=$this->setupDSN($dbParam);
59
-        $this->idoUsername = $dbParam['username'];
60
-        $this->idoPass = (array_key_exists('password', $dbParam)) ? $dbParam['password']:'';
61
-        $this->logging->log('DSN : '.$this->idoDSN. ';user '.$this->idoUsername,INFO);
59
+        $this->idoUsername=$dbParam['username'];
60
+        $this->idoPass=(array_key_exists('password', $dbParam)) ? $dbParam['password'] : '';
61
+        $this->logging->log('DSN : '.$this->idoDSN.';user '.$this->idoUsername, INFO);
62 62
         $this->idoDBType=$dbParam['db'];
63 63
         $this->db_connect_ido();
64 64
     }
@@ -76,13 +76,13 @@  discard block
 block discarded – undo
76 76
                 return $this->idoDB;
77 77
             } catch (Exception $e) {
78 78
                 // select 1 failed, try to reconnect.
79
-                $this->logging->log('Database IDO connection lost, reconnecting',WARN);
79
+                $this->logging->log('Database IDO connection lost, reconnecting', WARN);
80 80
             }
81 81
         }
82 82
         try {
83
-            $this->idoDB = new PDO($this->idoDSN,$this->idoUsername,$this->idoPass);
83
+            $this->idoDB=new PDO($this->idoDSN, $this->idoUsername, $this->idoPass);
84 84
         } catch (PDOException $e) {
85
-            $this->logging->log('Connection failed to IDO : ' . $e->getMessage(),ERROR,'');
85
+            $this->logging->log('Connection failed to IDO : '.$e->getMessage(), ERROR, '');
86 86
         }
87 87
         return $this->idoDB;
88 88
     }
@@ -101,13 +101,13 @@  discard block
 block discarded – undo
101 101
                 return $this->trapDB;
102 102
             } catch (Exception $e) {
103 103
                 // select 1 failed, try to reconnect.
104
-                $this->logging->log('Database connection lost, reconnecting',WARN);
104
+                $this->logging->log('Database connection lost, reconnecting', WARN);
105 105
             }           
106 106
         }       
107 107
         try {
108
-            $this->trapDB = new PDO($this->trapDSN,$this->trapUsername,$this->trapPass);
108
+            $this->trapDB=new PDO($this->trapDSN, $this->trapUsername, $this->trapPass);
109 109
         } catch (PDOException $e) {
110
-            $this->logging->log('Connection failed : ' . $e->getMessage(),ERROR,'');
110
+            $this->logging->log('Connection failed : '.$e->getMessage(), ERROR, '');
111 111
         }
112 112
         return $this->trapDB;
113 113
     }
@@ -119,21 +119,21 @@  discard block
 block discarded – undo
119 119
      */
120 120
     protected function setupDSN($configElmt)  
121 121
     {
122
-        if (!array_key_exists('db',$configElmt) ||
123
-            !array_key_exists('host',$configElmt) ||
124
-            !array_key_exists('dbname',$configElmt) ||
125
-            !array_key_exists('username',$configElmt))
122
+        if (!array_key_exists('db', $configElmt) ||
123
+            !array_key_exists('host', $configElmt) ||
124
+            !array_key_exists('dbname', $configElmt) ||
125
+            !array_key_exists('username', $configElmt))
126 126
         {
127
-            $this->logging->log('Missing DB params',ERROR);
127
+            $this->logging->log('Missing DB params', ERROR);
128 128
             return ''; 
129 129
         }
130 130
         
131 131
         //	$dsn = 'mysql:dbname=traps;host=127.0.0.1';
132
-        $dsn= $configElmt['db'].':dbname='.$configElmt['dbname'].';host='.$configElmt['host'];
132
+        $dsn=$configElmt['db'].':dbname='.$configElmt['dbname'].';host='.$configElmt['host'];
133 133
         
134 134
         if (array_key_exists('port', $configElmt))
135 135
         {
136
-            $dsn .= ';port='.$configElmt['port'];
136
+            $dsn.=';port='.$configElmt['port'];
137 137
         }
138 138
         return $dsn;
139 139
     }
@@ -143,14 +143,14 @@  discard block
 block discarded – undo
143 143
      *	@param $schema_file	string File to read schema from
144 144
      *	@param $table_prefix string to replace #PREFIX# in schema file by this
145 145
      */
146
-    public function create_schema($schema_file,$table_prefix)
146
+    public function create_schema($schema_file, $table_prefix)
147 147
     {
148 148
         //Read data from snmptrapd from stdin
149 149
         $input_stream=fopen($schema_file, 'r');
150 150
         
151
-        if ($input_stream=== false)
151
+        if ($input_stream === false)
152 152
         {
153
-            $this->logging->log("Error reading schema !",ERROR,'');
153
+            $this->logging->log("Error reading schema !", ERROR, '');
154 154
             return;
155 155
         }
156 156
         $newline='';
@@ -160,15 +160,15 @@  discard block
 block discarded – undo
160 160
         
161 161
         while (($line=fgets($input_stream)) !== false)
162 162
         {
163
-            $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line));
163
+            $newline.=chop(preg_replace('/#PREFIX#/', $table_prefix, $line));
164 164
             if (preg_match('/; *$/', $newline))
165 165
             {
166
-                $sql= $newline;
166
+                $sql=$newline;
167 167
                 if ($db_conn->query($sql) === false) {
168
-                    $this->logging->log('Error create schema : '.$sql,ERROR,'');
168
+                    $this->logging->log('Error create schema : '.$sql, ERROR, '');
169 169
                     return;
170 170
                 }
171
-                if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array))
171
+                if (preg_match('/^ *CREATE TABLE ([^ ]+)/', $newline, $cur_table_array))
172 172
                 {
173 173
                     $cur_table='table '.$cur_table_array[1];
174 174
                 }
@@ -176,20 +176,20 @@  discard block
 block discarded – undo
176 176
                 {
177 177
                     $cur_table='secret SQL stuff :-)';
178 178
                 }
179
-                $this->logging->log('Creating : ' . $cur_table,INFO );
179
+                $this->logging->log('Creating : '.$cur_table, INFO);
180 180
                 $newline='';
181 181
             }
182 182
         }
183 183
         
184
-        $sql= $newline;
185
-        if ($sql != '' )
184
+        $sql=$newline;
185
+        if ($sql != '')
186 186
         {
187 187
             if ($db_conn->query($sql) === false) {
188
-                $this->logging->log('Error create schema : '.$sql,ERROR,'');
188
+                $this->logging->log('Error create schema : '.$sql, ERROR, '');
189 189
                 return;
190 190
             }
191 191
         }
192
-        $this->logging->log('Schema created',INFO);
192
+        $this->logging->log('Schema created', INFO);
193 193
     }
194 194
     
195 195
     /**
@@ -200,14 +200,14 @@  discard block
 block discarded – undo
200 200
      *     @param bool $getmsg : only get messages from version upgrades
201 201
      *     @return string : if $getmsg=true, return messages or 'ERROR' on error.
202 202
      */
203
-    public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false)
203
+    public function update_schema($prefix, $target_version, $table_prefix, $getmsg=false)
204 204
     {
205 205
         // Get current db number
206 206
         $db_conn=$this->db_connect_trap();
207 207
         $sql='SELECT value from '.$this->dbPrefix.'db_config WHERE name=\'db_version\' ';
208
-        $this->logging->log('SQL query : '.$sql,DEBUG );
208
+        $this->logging->log('SQL query : '.$sql, DEBUG);
209 209
         if (($ret_code=$db_conn->query($sql)) === false) {
210
-            $this->logging->log('Cannot get db version. Query : ' . $sql,2,'');
210
+            $this->logging->log('Cannot get db version. Query : '.$sql, 2, '');
211 211
             return 'ERROR';
212 212
         }
213 213
         $version=$ret_code->fetchAll();
@@ -215,11 +215,11 @@  discard block
 block discarded – undo
215 215
         
216 216
         if ($this->trapDBType == 'pgsql')
217 217
         {
218
-            $prefix .= 'update_pgsql/schema_';
218
+            $prefix.='update_pgsql/schema_';
219 219
         }
220 220
         else
221 221
         {
222
-            $prefix .= 'update_sql/schema_';
222
+            $prefix.='update_sql/schema_';
223 223
         }
224 224
         //echo "version all :\n";print_r($version);echo " \n $cur_ver \n";
225 225
         if ($getmsg === true)
@@ -243,17 +243,17 @@  discard block
 block discarded – undo
243 243
      *     @param string $table_prefix   to replace #PREFIX# in schema file by this
244 244
      *     @return bool : true on error
245 245
      */
246
-    public function update_schema_do($prefix,$cur_version,$target_version,$table_prefix)
246
+    public function update_schema_do($prefix, $cur_version, $target_version, $table_prefix)
247 247
     {
248
-        while($cur_version<$target_version)
248
+        while ($cur_version < $target_version)
249 249
         { // TODO : execute pre & post scripts
250 250
             $cur_version++;
251
-            $this->logging->log('Updating to version : ' .$cur_version ,INFO );
252
-            $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql';
251
+            $this->logging->log('Updating to version : '.$cur_version, INFO);
252
+            $updateFile=$prefix.'v'.($cur_version - 1).'_v'.$cur_version.'.sql';
253 253
             $input_stream=fopen($updateFile, 'r');
254
-            if ($input_stream=== false)
254
+            if ($input_stream === false)
255 255
             {
256
-                $this->logging->log("Error reading update file ". $updateFile,ERROR);
256
+                $this->logging->log("Error reading update file ".$updateFile, ERROR);
257 257
                 return true;
258 258
             }
259 259
             $newline='';
@@ -262,25 +262,25 @@  discard block
 block discarded – undo
262 262
             while (($line=fgets($input_stream)) !== false)
263 263
             {
264 264
                 if (preg_match('/^#/', $line)) continue; // ignore comment lines
265
-                $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line));
265
+                $newline.=chop(preg_replace('/#PREFIX#/', $table_prefix, $line));
266 266
                 if (preg_match('/; *$/', $newline))
267 267
                 {
268 268
                     $sql_req=$db_conn->prepare($newline);
269 269
                     if ($sql_req->execute() === false) {
270
-                        $this->logging->log('Error create schema : '.$newline,ERROR);
270
+                        $this->logging->log('Error create schema : '.$newline, ERROR);
271 271
                         return true;
272 272
                     }
273 273
                     $cur_table_array=array();
274
-                    if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array))
274
+                    if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/', $newline, $cur_table_array))
275 275
                     {
276
-                        $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2];
276
+                        $cur_table=$cur_table_array[1].' SQL table '.$cur_table_array[2];
277 277
                     }
278 278
                     else
279 279
                     {
280 280
                         $cur_table='secret SQL stuff :-)';
281 281
                         //$cur_table=$newline;
282 282
                     }
283
-                    $this->logging->log('Doing : ' . $cur_table,INFO );
283
+                    $this->logging->log('Doing : '.$cur_table, INFO);
284 284
                     
285 285
                     $newline='';
286 286
                 }
@@ -288,13 +288,13 @@  discard block
 block discarded – undo
288 288
             fclose($input_stream);
289 289
             
290 290
             $sql='UPDATE '.$this->dbPrefix.'db_config SET value='.$cur_version.' WHERE ( name=\'db_version\' )';
291
-            $this->logging->log('SQL query : '.$sql,DEBUG );
291
+            $this->logging->log('SQL query : '.$sql, DEBUG);
292 292
             if ($db_conn->query($sql) === false) {
293
-                $this->logging->log('Cannot update db version. Query : ' . $sql,WARN);
293
+                $this->logging->log('Cannot update db version. Query : '.$sql, WARN);
294 294
                 return true;
295 295
             }
296 296
             
297
-            $this->logging->log('Schema updated to version : '.$cur_version ,INFO);
297
+            $this->logging->log('Schema updated to version : '.$cur_version, INFO);
298 298
         }
299 299
         return false;
300 300
     }
@@ -306,33 +306,33 @@  discard block
 block discarded – undo
306 306
      *     @param int $target_version  target db version number
307 307
      *     @return string : return messages or 'ERROR'.
308 308
      */
309
-    private function update_schema_message($prefix,$cur_version,$target_version)
309
+    private function update_schema_message($prefix, $cur_version, $target_version)
310 310
     {
311 311
  
312 312
         $message='';
313
-        $this->logging->log('getting message for upgrade',DEBUG );
314
-        while($cur_version<$target_version)
313
+        $this->logging->log('getting message for upgrade', DEBUG);
314
+        while ($cur_version < $target_version)
315 315
         {
316 316
             $cur_version++;
317
-            $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql';
317
+            $updateFile=$prefix.'v'.($cur_version - 1).'_v'.$cur_version.'.sql';
318 318
             $input_stream=fopen($updateFile, 'r');
319
-            if ($input_stream=== false)
319
+            if ($input_stream === false)
320 320
             {
321
-                $this->logging->log("Error reading update file ". $updateFile,2,'');
321
+                $this->logging->log("Error reading update file ".$updateFile, 2, '');
322 322
                 return 'ERROR';
323 323
             }
324 324
             do 
325 325
             { 
326 326
                 $line=fgets($input_stream); 
327 327
             }
328
-            while ($line !== false && !preg_match('/#MESSAGE/',$line));
328
+            while ($line !== false && !preg_match('/#MESSAGE/', $line));
329 329
             fclose($input_stream);
330 330
             if ($line === false)
331 331
             {
332
-                $this->logging->log("No message in file ". $updateFile,2,'');
332
+                $this->logging->log("No message in file ".$updateFile, 2, '');
333 333
                 return '';
334 334
             }
335
-            $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n";
335
+            $message.=($cur_version - 1).'->'.$cur_version.' : '.preg_replace('/#MESSAGE : /', '', $line)."\n";
336 336
         }
337 337
         return $message;
338 338
     }
Please login to merge, or discard this patch.
bin/trap_class.php 1 patch
Spacing   +265 added lines, -265 removed lines patch added patch discarded remove patch
@@ -34,8 +34,8 @@  discard block
 block discarded – undo
34 34
 	protected $api_password='';
35 35
 
36 36
 	// Logs 
37
-	protected $logging;    //< Logging class.
38
-	protected $logSetup;   //< bool true if log was setup in constructor
37
+	protected $logging; //< Logging class.
38
+	protected $logSetup; //< bool true if log was setup in constructor
39 39
 	
40 40
 	// Databases
41 41
 	public $trapsDB; //< Database class
@@ -55,7 +55,7 @@  discard block
 block discarded – undo
55 55
 	private $objectsAll; //< output lines of snmptranslate list    //DUP
56 56
 	private $trapObjectsIndex; //< array of traps objects (as OID)  //DUP
57 57
 	
58
-	function __construct($etc_dir='/etc/icingaweb2',$baseLogLevel=null,$baseLogMode='syslog',$baseLogFile='')
58
+	function __construct($etc_dir='/etc/icingaweb2', $baseLogLevel=null, $baseLogMode='syslog', $baseLogFile='')
59 59
 	{
60 60
 	    // Paths of ini files
61 61
 		$this->icingaweb2_etc=$etc_dir;
@@ -63,10 +63,10 @@  discard block
 block discarded – undo
63 63
 		$this->icingaweb2_ressources=$this->icingaweb2_etc."/resources.ini";
64 64
 
65 65
 		// Setup logging
66
-		$this->logging = new Logging();
66
+		$this->logging=new Logging();
67 67
 		if ($baseLogLevel != null)
68 68
 		{
69
-		    $this->logging->setLogging($baseLogLevel, $baseLogMode,$baseLogFile);
69
+		    $this->logging->setLogging($baseLogLevel, $baseLogMode, $baseLogFile);
70 70
 		    $this->logSetup=true;
71 71
 		}
72 72
 		else
@@ -74,10 +74,10 @@  discard block
 block discarded – undo
74 74
 		$this->logging->log('Loggin started', INFO);
75 75
 
76 76
 		// Get options from ini files
77
-		$trapConfig=parse_ini_file($this->trap_module_config,true);
77
+		$trapConfig=parse_ini_file($this->trap_module_config, true);
78 78
 		if ($trapConfig == false)
79 79
 		{
80
-		    $this->logging->log("Error reading ini file : ".$this->trap_module_config,ERROR,'syslog');
80
+		    $this->logging->log("Error reading ini file : ".$this->trap_module_config, ERROR, 'syslog');
81 81
 		}
82 82
 		$this->getMainOptions($trapConfig); // Get main options from ini file
83 83
 		$this->setupDatabase($trapConfig); // Setup database class
@@ -85,7 +85,7 @@  discard block
 block discarded – undo
85 85
 		$this->getDatabaseOptions(); // Get options in database
86 86
 		if ($this->api_use === true) $this->getAPI(); // Setup API
87 87
 		
88
-		$this->mibClass = new Mib($this->logging,$this->trapsDB,$this->snmptranslate,$this->snmptranslate_dirs); // Create Mib class
88
+		$this->mibClass=new Mib($this->logging, $this->trapsDB, $this->snmptranslate, $this->snmptranslate_dirs); // Create Mib class
89 89
 		
90 90
 		$this->trap_data=array(
91 91
 			'source_ip'	=> 'unknown',
@@ -107,15 +107,15 @@  discard block
 block discarded – undo
107 107
 	 * @param string $message warning message if not found
108 108
 	 * @return boolean true if found, or false
109 109
 	 */
110
-	protected function getOptionIfSet($option_array,$option_category,$option_name, &$option_var, $log_level = 2, $message = null)
110
+	protected function getOptionIfSet($option_array, $option_category, $option_name, &$option_var, $log_level=2, $message=null)
111 111
 	{
112 112
 	    if (!isset($option_array[$option_category][$option_name]))
113 113
 	    {
114 114
 	        if ($message === null)
115 115
 	        {
116
-	            $message='No ' . $option_name . ' in config file: '. $this->trap_module_config;
116
+	            $message='No '.$option_name.' in config file: '.$this->trap_module_config;
117 117
 	        }
118
-	        $this->logging->log($message,$log_level,'syslog');
118
+	        $this->logging->log($message, $log_level, 'syslog');
119 119
 	        return false;
120 120
 	    }
121 121
 	    else
@@ -133,24 +133,24 @@  discard block
 block discarded – undo
133 133
 	{
134 134
 
135 135
 		// Snmptranslate binary path
136
-		$this->getOptionIfSet($trapConfig,'config','snmptranslate', $this->snmptranslate);
136
+		$this->getOptionIfSet($trapConfig, 'config', 'snmptranslate', $this->snmptranslate);
137 137
 
138 138
 		// mibs path
139
-		$this->getOptionIfSet($trapConfig,'config','snmptranslate_dirs', $this->snmptranslate_dirs);
139
+		$this->getOptionIfSet($trapConfig, 'config', 'snmptranslate_dirs', $this->snmptranslate_dirs);
140 140
 
141 141
 		// icinga2cmd path
142
-		$this->getOptionIfSet($trapConfig,'config','icingacmd', $this->icinga2cmd);
142
+		$this->getOptionIfSet($trapConfig, 'config', 'icingacmd', $this->icinga2cmd);
143 143
 		
144 144
 		// table prefix
145
-		$this->getOptionIfSet($trapConfig,'config','database_prefix', $this->db_prefix);
145
+		$this->getOptionIfSet($trapConfig, 'config', 'database_prefix', $this->db_prefix);
146 146
 
147 147
 		// API options
148
-		if ($this->getOptionIfSet($trapConfig,'config','icingaAPI_host', $this->api_hostname))
148
+		if ($this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_host', $this->api_hostname))
149 149
 		{
150 150
 		    $this->api_use=true;
151
-		    $this->getOptionIfSet($trapConfig,'config','icingaAPI_port', $this->api_port);
152
-		    $this->getOptionIfSet($trapConfig,'config','icingaAPI_user', $this->api_username);
153
-		    $this->getOptionIfSet($trapConfig,'config','icingaAPI_password', $this->api_password);
151
+		    $this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_port', $this->api_port);
152
+		    $this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_user', $this->api_username);
153
+		    $this->getOptionIfSet($trapConfig, 'config', 'icingaAPI_password', $this->api_password);
154 154
 		}
155 155
 	}
156 156
 	
@@ -161,40 +161,40 @@  discard block
 block discarded – undo
161 161
 	protected function setupDatabase($trapConfig)
162 162
 	{
163 163
 	    // Trap database
164
-	    if (!array_key_exists('database',$trapConfig['config']))
164
+	    if (!array_key_exists('database', $trapConfig['config']))
165 165
         {
166
-            $this->logging->log("No database in config file: ".$this->trap_module_config,ERROR,'');
166
+            $this->logging->log("No database in config file: ".$this->trap_module_config, ERROR, '');
167 167
             return;
168 168
         }
169 169
         $dbTrapName=$trapConfig['config']['database'];
170
-        $this->logging->log("Found database in config file: ".$dbTrapName,INFO );
170
+        $this->logging->log("Found database in config file: ".$dbTrapName, INFO);
171 171
 	    
172
-	   if ( ($dbConfig=parse_ini_file($this->icingaweb2_ressources,true)) === false)
172
+	   if (($dbConfig=parse_ini_file($this->icingaweb2_ressources, true)) === false)
173 173
 	    {
174
-	        $this->logging->log("Error reading ini file : ".$this->icingaweb2_ressources,ERROR,'');
174
+	        $this->logging->log("Error reading ini file : ".$this->icingaweb2_ressources, ERROR, '');
175 175
 	        return;
176 176
 	    }
177
-	    if (!array_key_exists($dbTrapName,$dbConfig))
177
+	    if (!array_key_exists($dbTrapName, $dbConfig))
178 178
 	    {
179
-	        $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2_ressources,ERROR,'');
179
+	        $this->logging->log("No database '.$dbTrapName.' in config file: ".$this->icingaweb2_ressources, ERROR, '');
180 180
 	        return;
181 181
 	    }
182 182
 	    
183
-	    $this->trapsDB = new Database($this->logging,$dbConfig[$dbTrapName],$this->db_prefix);
183
+	    $this->trapsDB=new Database($this->logging, $dbConfig[$dbTrapName], $this->db_prefix);
184 184
 	    
185 185
 	    if ($this->api_use === true) return; // In case of API use, no IDO is necessary
186 186
         
187 187
 	    // IDO Database
188
-	    if (!array_key_exists('IDOdatabase',$trapConfig['config']))
188
+	    if (!array_key_exists('IDOdatabase', $trapConfig['config']))
189 189
 	    {
190
-	        $this->logging->log("No IDOdatabase in config file: ".$this->trap_module_config,ERROR,'');
190
+	        $this->logging->log("No IDOdatabase in config file: ".$this->trap_module_config, ERROR, '');
191 191
 	    }
192 192
 	    $dbIdoName=$trapConfig['config']['IDOdatabase'];		
193 193
 
194
-	    $this->logging->log("Found IDO database in config file: ".$dbIdoName,INFO );
195
-        if (!array_key_exists($dbIdoName,$dbConfig))
194
+	    $this->logging->log("Found IDO database in config file: ".$dbIdoName, INFO);
195
+        if (!array_key_exists($dbIdoName, $dbConfig))
196 196
 	    {
197
-	        $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2_ressources,ERROR,'');
197
+	        $this->logging->log("No database '.$dbIdoName.' in config file: ".$this->icingaweb2_ressources, ERROR, '');
198 198
 	        return;
199 199
 	    }
200 200
 	    
@@ -209,13 +209,13 @@  discard block
 block discarded – undo
209 209
 		// Database options
210 210
 		if ($this->logSetup === false) // Only if logging was no setup in constructor
211 211
 		{
212
-    		$this->getDBConfigIfSet('log_level',$this->logging->debugLevel);
213
-    		$this->getDBConfigIfSet('log_destination',$this->logging->outputMode);
214
-    		$this->getDBConfigIfSet('log_file',$this->logging->outputFile);
212
+    		$this->getDBConfigIfSet('log_level', $this->logging->debugLevel);
213
+    		$this->getDBConfigIfSet('log_destination', $this->logging->outputMode);
214
+    		$this->getDBConfigIfSet('log_file', $this->logging->outputFile);
215 215
 		}
216 216
 	}
217 217
 
218
-	protected function getDBConfigIfSet($element,&$variable)
218
+	protected function getDBConfigIfSet($element, &$variable)
219 219
 	{
220 220
 		$value=$this->getDBConfig($element);
221 221
 		if ($value != 'null') $variable=$value;
@@ -231,7 +231,7 @@  discard block
 block discarded – undo
231 231
 		$db_conn=$this->trapsDB->db_connect_trap();
232 232
 		$sql='SELECT value from '.$this->db_prefix.'db_config WHERE ( name=\''.$element.'\' )';
233 233
 		if (($ret_code=$db_conn->query($sql)) === false) {
234
-			$this->logging->log('No result in query : ' . $sql,WARN,'');
234
+			$this->logging->log('No result in query : '.$sql, WARN, '');
235 235
 			return null;
236 236
 		}
237 237
 		$value=$ret_code->fetch();
@@ -248,22 +248,22 @@  discard block
 block discarded – undo
248 248
 	*	@param  string $destination file/syslog/display
249 249
 	*	@return void
250 250
 	**/	
251
-	public function trapLog( $message, $level, $destination ='') // OBSOLETE
251
+	public function trapLog($message, $level, $destination='') // OBSOLETE
252 252
 	{	
253 253
 		// TODO : replace ref with $this->logging->log 
254 254
 	    $this->logging->log($message, $level, $destination);
255 255
 	}
256 256
 	
257
-	public function setLogging($debugLvl,$outputType,$outputOption=null)  // OBSOLETE
257
+	public function setLogging($debugLvl, $outputType, $outputOption=null)  // OBSOLETE
258 258
 	{
259
-		$this->logging->setLogging($debugLvl, $outputType,$outputOption);
259
+		$this->logging->setLogging($debugLvl, $outputType, $outputOption);
260 260
 	}
261 261
 	
262 262
 	protected function getAPI()
263 263
 	{
264 264
 	    if ($this->icinga2api == null)
265 265
 	    {
266
-	        $this->icinga2api = new Icinga2API($this->api_hostname,$this->api_port);
266
+	        $this->icinga2api=new Icinga2API($this->api_hostname, $this->api_port);
267 267
 	    }
268 268
 	    return $this->icinga2api;
269 269
 	}
@@ -282,7 +282,7 @@  discard block
 block discarded – undo
282 282
 		if ($input_stream === false)
283 283
 		{
284 284
 		    $this->writeTrapErrorToDB("Error reading trap (code 1/Stdin)");
285
-			$this->logging->log("Error reading stdin !",ERROR,'');
285
+			$this->logging->log("Error reading stdin !", ERROR, '');
286 286
 			return null; // note : exception thrown by logging
287 287
 		}
288 288
 
@@ -291,21 +291,21 @@  discard block
 block discarded – undo
291 291
 		if ($this->receivingHost === false)
292 292
 		{
293 293
 		    $this->writeTrapErrorToDB("Error reading trap (code 1/Line Host)");
294
-			$this->logging->log("Error reading Host !",ERROR,''); 
294
+			$this->logging->log("Error reading Host !", ERROR, ''); 
295 295
 		}
296 296
 		// line 2 IP:port=>IP:port
297 297
 		$IP=chop(fgets($input_stream));
298 298
 		if ($IP === false)
299 299
 		{
300 300
 		    $this->writeTrapErrorToDB("Error reading trap (code 1/Line IP)");
301
-			$this->logging->log("Error reading IP !",ERROR,''); 
301
+			$this->logging->log("Error reading IP !", ERROR, ''); 
302 302
 		}
303 303
 		$matches=array();
304
-		$ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/',$IP,$matches);
305
-		if ($ret_code===0 || $ret_code===false) 
304
+		$ret_code=preg_match('/.DP: \[(.*)\]:(.*)->\[(.*)\]:(.*)/', $IP, $matches);
305
+		if ($ret_code === 0 || $ret_code === false) 
306 306
 		{
307 307
 		    $this->writeTrapErrorToDB("Error parsing trap (code 2/IP)");
308
-			$this->logging->log('Error parsing IP : '.$IP,ERROR,'');
308
+			$this->logging->log('Error parsing IP : '.$IP, ERROR, '');
309 309
 		} 
310 310
 		else 
311 311
 		{		
@@ -315,34 +315,34 @@  discard block
 block discarded – undo
315 315
 			$this->trap_data['destination_port']=$matches[4];
316 316
 		}
317 317
 
318
-		while (($vars=fgets($input_stream)) !==false)
318
+		while (($vars=fgets($input_stream)) !== false)
319 319
 		{
320 320
 			$vars=chop($vars);
321
-			$ret_code=preg_match('/^([^ ]+) (.*)$/',$vars,$matches);
322
-			if ($ret_code===0 || $ret_code===false) 
321
+			$ret_code=preg_match('/^([^ ]+) (.*)$/', $vars, $matches);
322
+			if ($ret_code === 0 || $ret_code === false) 
323 323
 			{
324
-				$this->logging->log('No match on trap data : '.$vars,WARN,'');
324
+				$this->logging->log('No match on trap data : '.$vars, WARN, '');
325 325
 			}
326 326
 			else 
327 327
 			{
328
-			    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'))
328
+			    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'))
329 329
 				{
330 330
 					$this->trap_data['trap_oid']=$matches[2];				
331 331
 				}
332 332
 				else
333 333
 				{
334
-					$object= new stdClass;
335
-					$object->oid =$matches[1];
336
-					$object->value = $matches[2];
337
-					array_push($this->trap_data_ext,$object);
334
+					$object=new stdClass;
335
+					$object->oid=$matches[1];
336
+					$object->value=$matches[2];
337
+					array_push($this->trap_data_ext, $object);
338 338
 				}
339 339
 			}
340 340
 		}
341 341
 
342
-		if ($this->trap_data['trap_oid']=='unknown') 
342
+		if ($this->trap_data['trap_oid'] == 'unknown') 
343 343
 		{
344
-		    $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)",$this->trap_data['source_ip']);
345
-			$this->logging->log('no trap oid found',ERROR,'');
344
+		    $this->writeTrapErrorToDB("No trap oid found : check snmptrapd configuration (code 3/OID)", $this->trap_data['source_ip']);
345
+			$this->logging->log('no trap oid found', ERROR, '');
346 346
 		} 
347 347
 
348 348
 		// Translate oids.
@@ -364,7 +364,7 @@  discard block
 block discarded – undo
364 364
 		}
365 365
 		
366 366
 
367
-		$this->trap_data['status']= 'waiting';
367
+		$this->trap_data['status']='waiting';
368 368
 		
369 369
 		return $this->trap_data;
370 370
 	}
@@ -380,40 +380,40 @@  discard block
 block discarded – undo
380 380
 		$db_conn=$this->trapsDB->db_connect_trap();
381 381
 		
382 382
 		$sql='SELECT mib,name from '.$this->db_prefix.'mib_cache WHERE oid=\''.$oid.'\';';
383
-		$this->logging->log('SQL query : '.$sql,DEBUG );
383
+		$this->logging->log('SQL query : '.$sql, DEBUG);
384 384
 		if (($ret_code=$db_conn->query($sql)) === false) {
385
-			$this->logging->log('No result in query : ' . $sql,ERROR,'');
385
+			$this->logging->log('No result in query : '.$sql, ERROR, '');
386 386
 		}
387 387
 		$name=$ret_code->fetch();
388 388
 		if ($name['name'] != null)
389 389
 		{
390
-			return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']);
390
+			return array('trap_name_mib'=>$name['mib'], 'trap_name'=>$name['name']);
391 391
 		}
392 392
 		
393 393
 		// Also check if it is an instance of OID
394
-		$oid_instance=preg_replace('/\.[0-9]+$/','',$oid);
394
+		$oid_instance=preg_replace('/\.[0-9]+$/', '', $oid);
395 395
 		
396 396
 		$sql='SELECT mib,name from '.$this->db_prefix.'mib_cache WHERE oid=\''.$oid_instance.'\';';
397
-		$this->logging->log('SQL query : '.$sql,DEBUG );
397
+		$this->logging->log('SQL query : '.$sql, DEBUG);
398 398
 		if (($ret_code=$db_conn->query($sql)) === false) {
399
-			$this->logging->log('No result in query : ' . $sql,ERROR,'');
399
+			$this->logging->log('No result in query : '.$sql, ERROR, '');
400 400
 		}
401 401
 		$name=$ret_code->fetch();
402 402
 		if ($name['name'] != null)
403 403
 		{
404
-			return array('trap_name_mib'=>$name['mib'],'trap_name'=>$name['name']);
404
+			return array('trap_name_mib'=>$name['mib'], 'trap_name'=>$name['name']);
405 405
 		}
406 406
 		
407 407
 		// Try to get oid name from snmptranslate
408
-		$translate=exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslate_dirs.
408
+		$translate=exec($this->snmptranslate.' -m ALL -M +'.$this->snmptranslate_dirs.
409 409
 		    ' '.$oid);
410 410
 		$matches=array();
411
-		$ret_code=preg_match('/(.*)::(.*)/',$translate,$matches);
412
-		if ($ret_code===0 || $ret_code === false) {
411
+		$ret_code=preg_match('/(.*)::(.*)/', $translate, $matches);
412
+		if ($ret_code === 0 || $ret_code === false) {
413 413
 			return NULL;
414 414
 		} else {
415
-			$this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid,INFO);
416
-			return array('trap_name_mib'=>$matches[1],'trap_name'=>$matches[2]);
415
+			$this->logging->log('Found name with snmptrapd and not in DB for oid='.$oid, INFO);
416
+			return array('trap_name_mib'=>$matches[1], 'trap_name'=>$matches[2]);
417 417
 		}	
418 418
 	}
419 419
 	
@@ -424,90 +424,90 @@  discard block
 block discarded – undo
424 424
 	**/
425 425
 	public function eraseOldTraps($days=0)
426 426
 	{
427
-		if ($days==0)
427
+		if ($days == 0)
428 428
 		{
429 429
 			if (($days=$this->getDBConfig('db_remove_days')) == null)
430 430
 			{
431
-				$this->logging->log('No days specified & no db value : no tap erase' ,WARN,'');
431
+				$this->logging->log('No days specified & no db value : no tap erase', WARN, '');
432 432
 				return;
433 433
 			}
434 434
 		}
435 435
 		$db_conn=$this->trapsDB->db_connect_trap();
436
-		$daysago = strtotime("-".$days." day");
437
-		$sql= 'delete from '.$this->db_prefix.'received where date_received < \''.date("Y-m-d H:i:s",$daysago).'\';';
436
+		$daysago=strtotime("-".$days." day");
437
+		$sql='delete from '.$this->db_prefix.'received where date_received < \''.date("Y-m-d H:i:s", $daysago).'\';';
438 438
 		if ($db_conn->query($sql) === false) {
439
-			$this->logging->log('Error erasing traps : '.$sql,ERROR,'');
439
+			$this->logging->log('Error erasing traps : '.$sql, ERROR, '');
440 440
 		}
441
-		$this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql,INFO);
441
+		$this->logging->log('Erased traps older than '.$days.' day(s) : '.$sql, INFO);
442 442
 	}
443 443
 
444 444
 	/** Write error to received trap database
445 445
 	 */
446
-	public function writeTrapErrorToDB($message,$sourceIP=null,$trapoid=null)
446
+	public function writeTrapErrorToDB($message, $sourceIP=null, $trapoid=null)
447 447
 	{
448 448
 	    
449 449
 	    $db_conn=$this->trapsDB->db_connect_trap();
450 450
 	    
451 451
 	    // add date time
452
-	    $insert_col ='date_received,status';
453
-	    $insert_val = "'" . date("Y-m-d H:i:s")."','error'";
452
+	    $insert_col='date_received,status';
453
+	    $insert_val="'".date("Y-m-d H:i:s")."','error'";
454 454
         
455
-	    if ($sourceIP !=null)
455
+	    if ($sourceIP != null)
456 456
 	    {
457
-	        $insert_col .=',source_ip';
458
-	        $insert_val .=",'". $sourceIP ."'";
457
+	        $insert_col.=',source_ip';
458
+	        $insert_val.=",'".$sourceIP."'";
459 459
 	    }
460
-	    if ($trapoid !=null)
460
+	    if ($trapoid != null)
461 461
 	    {
462
-	        $insert_col .=',trap_oid';
463
-	        $insert_val .=",'". $trapoid ."'";
462
+	        $insert_col.=',trap_oid';
463
+	        $insert_val.=",'".$trapoid."'";
464 464
 	    }
465
-	    $insert_col .=',status_detail';
466
-	    $insert_val .=",'". $message ."'";
465
+	    $insert_col.=',status_detail';
466
+	    $insert_val.=",'".$message."'";
467 467
 	    
468
-	    $sql= 'INSERT INTO '.$this->db_prefix.'received (' . $insert_col . ') VALUES ('.$insert_val.')';
468
+	    $sql='INSERT INTO '.$this->db_prefix.'received ('.$insert_col.') VALUES ('.$insert_val.')';
469 469
 	    
470 470
 	    switch ($this->trapsDB->trapDBType)
471 471
 	    {
472 472
 	        case 'pgsql':
473
-	            $sql .= ' RETURNING id;';
474
-	            $this->logging->log('sql : '.$sql,INFO);
473
+	            $sql.=' RETURNING id;';
474
+	            $this->logging->log('sql : '.$sql, INFO);
475 475
 	            if (($ret_code=$db_conn->query($sql)) === false) {
476
-	                $this->logging->log('Error SQL insert : '.$sql,1,'');
476
+	                $this->logging->log('Error SQL insert : '.$sql, 1, '');
477 477
 	            }
478
-	            $this->logging->log('SQL insertion OK',INFO );
478
+	            $this->logging->log('SQL insertion OK', INFO);
479 479
 	            // Get last id to insert oid/values in secondary table
480 480
 	            if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) {
481 481
 	                
482
-	                $this->logging->log('Erreur recuperation id',1,'');
482
+	                $this->logging->log('Erreur recuperation id', 1, '');
483 483
 	            }
484
-	            if (! isset($inserted_id_ret['id'])) {
485
-	                $this->logging->log('Error getting id',1,'');
484
+	            if (!isset($inserted_id_ret['id'])) {
485
+	                $this->logging->log('Error getting id', 1, '');
486 486
 	            }
487 487
 	            $this->trap_id=$inserted_id_ret['id'];
488 488
 	            break;
489 489
 	        case 'mysql':
490
-	            $sql .= ';';
491
-	            $this->logging->log('sql : '.$sql,INFO );
490
+	            $sql.=';';
491
+	            $this->logging->log('sql : '.$sql, INFO);
492 492
 	            if ($db_conn->query($sql) === false) {
493
-	                $this->logging->log('Error SQL insert : '.$sql,1,'');
493
+	                $this->logging->log('Error SQL insert : '.$sql, 1, '');
494 494
 	            }
495
-	            $this->logging->log('SQL insertion OK',INFO );
495
+	            $this->logging->log('SQL insertion OK', INFO);
496 496
 	            // Get last id to insert oid/values in secondary table
497 497
 	            $sql='SELECT LAST_INSERT_ID();';
498 498
 	            if (($ret_code=$db_conn->query($sql)) === false) {
499
-	                $this->logging->log('Erreur recuperation id',1,'');
499
+	                $this->logging->log('Erreur recuperation id', 1, '');
500 500
 	            }
501 501
 	            
502 502
 	            $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()'];
503
-	            if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
503
+	            if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
504 504
 	            $this->trap_id=$inserted_id;
505 505
 	            break;
506 506
 	        default:
507
-	            $this->logging->log('Error SQL type unknown  : '.$this->trapsDB->trapDBType,1,'');
507
+	            $this->logging->log('Error SQL type unknown  : '.$this->trapsDB->trapDBType, 1, '');
508 508
 	    }
509 509
 	    
510
-	    $this->logging->log('id found: '. $this->trap_id,INFO );    
510
+	    $this->logging->log('id found: '.$this->trap_id, INFO);    
511 511
 	}
512 512
 	
513 513
 	/** Write trap data to trap database
@@ -524,86 +524,86 @@  discard block
 block discarded – undo
524 524
 		$insert_col='';
525 525
 		$insert_val='';
526 526
 		// add date time
527
-		$this->trap_data['date_received'] = date("Y-m-d H:i:s");
527
+		$this->trap_data['date_received']=date("Y-m-d H:i:s");
528 528
 
529 529
 		$firstcol=1;
530 530
 		foreach ($this->trap_data as $col => $val)
531 531
 		{
532
-			if ($firstcol==0) 
532
+			if ($firstcol == 0) 
533 533
 			{
534
-				$insert_col .=',';
535
-				$insert_val .=',';
534
+				$insert_col.=',';
535
+				$insert_val.=',';
536 536
 			}
537
-			$insert_col .= $col ;
538
-			$insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val);
537
+			$insert_col.=$col;
538
+			$insert_val.=($val == null) ? 'NULL' : $db_conn->quote($val);
539 539
 			$firstcol=0;
540 540
 		}
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
 		switch ($this->trapsDB->trapDBType)
544 544
 		{
545 545
 			case 'pgsql': 
546
-				$sql .= ' RETURNING id;';
547
-				$this->logging->log('sql : '.$sql,INFO );
546
+				$sql.=' RETURNING id;';
547
+				$this->logging->log('sql : '.$sql, INFO);
548 548
 				if (($ret_code=$db_conn->query($sql)) === false) {
549
-					$this->logging->log('Error SQL insert : '.$sql,ERROR,'');
549
+					$this->logging->log('Error SQL insert : '.$sql, ERROR, '');
550 550
 				}
551
-				$this->logging->log('SQL insertion OK',INFO );
551
+				$this->logging->log('SQL insertion OK', INFO);
552 552
 				// Get last id to insert oid/values in secondary table
553 553
 				if (($inserted_id_ret=$ret_code->fetch(PDO::FETCH_ASSOC)) === false) {
554 554
 														   
555
-					$this->logging->log('Erreur recuperation id',ERROR,'');
555
+					$this->logging->log('Erreur recuperation id', ERROR, '');
556 556
 				}
557
-				if (! isset($inserted_id_ret['id'])) {
558
-					$this->logging->log('Error getting id',ERROR,'');
557
+				if (!isset($inserted_id_ret['id'])) {
558
+					$this->logging->log('Error getting id', ERROR, '');
559 559
 				}
560 560
 				$this->trap_id=$inserted_id_ret['id'];
561 561
 			break;
562 562
 			case 'mysql': 
563
-				$sql .= ';';
564
-				$this->logging->log('sql : '.$sql,INFO );
563
+				$sql.=';';
564
+				$this->logging->log('sql : '.$sql, INFO);
565 565
 				if ($db_conn->query($sql) === false) {
566
-					$this->logging->log('Error SQL insert : '.$sql,ERROR,'');
566
+					$this->logging->log('Error SQL insert : '.$sql, ERROR, '');
567 567
 				}
568
-				$this->logging->log('SQL insertion OK',INFO );
568
+				$this->logging->log('SQL insertion OK', INFO);
569 569
 				// Get last id to insert oid/values in secondary table
570 570
 				$sql='SELECT LAST_INSERT_ID();';
571 571
 				if (($ret_code=$db_conn->query($sql)) === false) {
572
-					$this->logging->log('Erreur recuperation id',ERROR,'');
572
+					$this->logging->log('Erreur recuperation id', ERROR, '');
573 573
 				}
574 574
 
575 575
 				$inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()'];
576
-				if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
576
+				if ($inserted_id == false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue");
577 577
 				$this->trap_id=$inserted_id;
578 578
 			break;
579 579
 			default: 
580
-				$this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType,ERROR,'');
580
+				$this->logging->log('Error SQL type unknown : '.$this->trapsDB->trapDBType, ERROR, '');
581 581
 		}
582
-		$this->logging->log('id found: '.$this->trap_id,INFO );
582
+		$this->logging->log('id found: '.$this->trap_id, INFO);
583 583
 		
584 584
 		// Fill trap extended data table
585 585
 		foreach ($this->trap_data_ext as $value) {			
586 586
 			// TODO : detect if trap value is encoded and decode it to UTF-8 for database
587 587
 			$firstcol=1;
588
-			$value->trap_id = $this->trap_id;
588
+			$value->trap_id=$this->trap_id;
589 589
 			$insert_col='';
590 590
 			$insert_val='';
591 591
 			foreach ($value as $col => $val)
592 592
 			{
593
-				if ($firstcol==0) 
593
+				if ($firstcol == 0) 
594 594
 				{
595
-					$insert_col .=',';
596
-					$insert_val .=',';
595
+					$insert_col.=',';
596
+					$insert_val.=',';
597 597
 				}
598
-				$insert_col .= $col;
599
-				$insert_val .= ($val==null)? 'NULL' : $db_conn->quote($val);
598
+				$insert_col.=$col;
599
+				$insert_val.=($val == null) ? 'NULL' : $db_conn->quote($val);
600 600
 				$firstcol=0;
601 601
 			}
602 602
 
603
-			$sql= 'INSERT INTO '.$this->db_prefix.'received_data (' . $insert_col . ') VALUES ('.$insert_val.');';			
603
+			$sql='INSERT INTO '.$this->db_prefix.'received_data ('.$insert_col.') VALUES ('.$insert_val.');';			
604 604
 
605 605
 			if ($db_conn->query($sql) === false) {
606
-				$this->logging->log('Erreur insertion data : ' . $sql,WARN,'');
606
+				$this->logging->log('Erreur insertion data : '.$sql, WARN, '');
607 607
 			}	
608 608
 		}	
609 609
 	}
@@ -613,14 +613,14 @@  discard block
 block discarded – undo
613 613
 	*	@param $oid string oid in numeric
614 614
 	*	@return mixed : PDO object or false
615 615
 	*/	
616
-	protected function getRules($ip,$oid)
616
+	protected function getRules($ip, $oid)
617 617
 	{
618 618
 		$db_conn=$this->trapsDB->db_connect_trap();
619 619
 		// fetch rules based on IP in rule and OID
620 620
 		$sql='SELECT * from '.$this->db_prefix.'rules WHERE trap_oid=\''.$oid.'\' ';
621
-		$this->logging->log('SQL query : '.$sql,DEBUG );
621
+		$this->logging->log('SQL query : '.$sql, DEBUG);
622 622
 		if (($ret_code=$db_conn->query($sql)) === false) {
623
-			$this->logging->log('No result in query : ' . $sql,WARN,'');
623
+			$this->logging->log('No result in query : '.$sql, WARN, '');
624 624
 			return false;
625 625
 		}
626 626
 		$rules_all=$ret_code->fetchAll();
@@ -629,7 +629,7 @@  discard block
 block discarded – undo
629 629
 		$rule_ret_key=0;
630 630
 		foreach ($rules_all as $key => $rule)
631 631
 		{
632
-			if ($rule['ip4']==$ip || $rule['ip6']==$ip)
632
+			if ($rule['ip4'] == $ip || $rule['ip6'] == $ip)
633 633
 			{
634 634
 				$rules_ret[$rule_ret_key]=$rules_all[$key];
635 635
 				//TODO : get host name by API (and check if correct in rule).
@@ -637,7 +637,7 @@  discard block
 block discarded – undo
637 637
 				continue;
638 638
 			}
639 639
 			// TODO : get hosts IP by API
640
-			if (isset($rule['host_group_name']) && $rule['host_group_name']!=null)
640
+			if (isset($rule['host_group_name']) && $rule['host_group_name'] != null)
641 641
 			{ // get ips of group members by oid
642 642
 				$db_conn2=$this->trapsDB->db_connect_ido();
643 643
 				$sql="SELECT m.host_object_id, a.address as ip4, a.address6 as ip6, b.name1 as host_name
@@ -648,15 +648,15 @@  discard block
 block discarded – undo
648 648
 						LEFT JOIN icinga_objects as b ON b.object_id = a.host_object_id
649 649
 						WHERE o.name1='".$rule['host_group_name']."';";
650 650
 				if (($ret_code2=$db_conn2->query($sql)) === false) {
651
-					$this->logging->log('No result in query : ' . $sql,WARN,'');
651
+					$this->logging->log('No result in query : '.$sql, WARN, '');
652 652
 					continue;
653 653
 				}
654 654
 				$grouphosts=$ret_code2->fetchAll();
655 655
 				//echo "rule grp :\n";print_r($grouphosts);echo "\n";
656
-				foreach ( $grouphosts as $host)
656
+				foreach ($grouphosts as $host)
657 657
 				{
658 658
 					//echo $host['ip4']."\n";
659
-					if ($host['ip4']==$ip || $host['ip6']==$ip)
659
+					if ($host['ip4'] == $ip || $host['ip6'] == $ip)
660 660
 					{
661 661
 						//echo "Rule added \n";
662 662
 						$rules_ret[$rule_ret_key]=$rules_all[$key];
@@ -679,7 +679,7 @@  discard block
 block discarded – undo
679 679
 		$db_conn=$this->trapsDB->db_connect_trap();
680 680
 		$sql="UPDATE ".$this->db_prefix."rules SET num_match = '".$set."' WHERE (id = '".$id."');";
681 681
 		if ($db_conn->query($sql) === false) {
682
-			$this->logging->log('Error in update query : ' . $sql,WARN,'');
682
+			$this->logging->log('Error in update query : '.$sql, WARN, '');
683 683
 		}
684 684
 	}
685 685
 	
@@ -691,31 +691,31 @@  discard block
 block discarded – undo
691 691
 	 * @param string $display
692 692
 	 * @returnn bool true is service check was sent without error
693 693
 	*/
694
-	public function serviceCheckResult($host,$service,$state,$display)
694
+	public function serviceCheckResult($host, $service, $state, $display)
695 695
 	{
696 696
 	    if ($this->api_use === false)
697 697
 	    {
698
-    		$send = '[' . date('U') .'] PROCESS_SERVICE_CHECK_RESULT;' .
699
-    			$host.';' .$service .';' . $state . ';'.$display;
700
-    		$this->logging->log( $send." : to : " .$this->icinga2cmd,INFO );
698
+    		$send='['.date('U').'] PROCESS_SERVICE_CHECK_RESULT;'.
699
+    			$host.';'.$service.';'.$state.';'.$display;
700
+    		$this->logging->log($send." : to : ".$this->icinga2cmd, INFO);
701 701
     		
702 702
     		// TODO : file_put_contents & fopen (,'w' or 'a') does not work. See why. Or not as using API will be by default....
703
-    		exec('echo "'.$send.'" > ' .$this->icinga2cmd);
703
+    		exec('echo "'.$send.'" > '.$this->icinga2cmd);
704 704
     		return true;
705 705
 	    }
706 706
 	    else
707 707
 	    {
708
-	        $api = $this->getAPI();
708
+	        $api=$this->getAPI();
709 709
 	        $api->setCredentials($this->api_username, $this->api_password);
710
-	        list($retcode,$retmessage)=$api->serviceCheckResult($host,$service,$state,$display);
710
+	        list($retcode, $retmessage)=$api->serviceCheckResult($host, $service, $state, $display);
711 711
 	        if ($retcode == false)
712 712
 	        {
713
-	            $this->logging->log( "Error sending result : " .$retmessage,WARN,'');
713
+	            $this->logging->log("Error sending result : ".$retmessage, WARN, '');
714 714
 	            return false;
715 715
 	        }
716 716
 	        else 
717 717
 	        {
718
-	            $this->logging->log( "Sent result : " .$retmessage,INFO );
718
+	            $this->logging->log("Sent result : ".$retmessage, INFO);
719 719
 	            return true;
720 720
 	        }
721 721
 	    }
@@ -723,7 +723,7 @@  discard block
 block discarded – undo
723 723
 	
724 724
 	public function getHostByIP($ip)
725 725
 	{
726
-	    $api = $this->getAPI();
726
+	    $api=$this->getAPI();
727 727
 	    $api->setCredentials($this->api_username, $this->api_password);
728 728
 	    return $api->getHostByIP($ip);
729 729
 	}
@@ -736,32 +736,32 @@  discard block
 block discarded – undo
736 736
 	protected function applyDisplay($display)
737 737
 	{
738 738
 	    $matches=array();
739
-	    while (preg_match('/_OID\(([0-9\.]+)\)/',$display,$matches) == 1)
739
+	    while (preg_match('/_OID\(([0-9\.]+)\)/', $display, $matches) == 1)
740 740
 		{
741 741
 			$oid=$matches[1];
742 742
 			$found=0;
743
-			foreach($this->trap_data_ext as $val)
743
+			foreach ($this->trap_data_ext as $val)
744 744
 			{
745 745
 				if ($oid == $val->oid)
746 746
 				{
747
-					$val->value=preg_replace('/"/','',$val->value);
747
+					$val->value=preg_replace('/"/', '', $val->value);
748 748
 					$rep=0;
749
-					$display=preg_replace('/_OID\('.$oid.'\)/',$val->value,$display,-1,$rep);
750
-					if ($rep==0)
749
+					$display=preg_replace('/_OID\('.$oid.'\)/', $val->value, $display, -1, $rep);
750
+					if ($rep == 0)
751 751
 					{
752
-						$this->logging->log("Error in display",WARN,'');
752
+						$this->logging->log("Error in display", WARN, '');
753 753
 						return $display;
754 754
 					}
755 755
 					$found=1;
756 756
 					break;
757 757
 				}
758 758
 			}
759
-			if ($found==0)
759
+			if ($found == 0)
760 760
 			{
761
-				$display=preg_replace('/_OID\('.$oid.'\)/','<not in trap>',$display,-1,$rep);
762
-				if ($rep==0)
761
+				$display=preg_replace('/_OID\('.$oid.'\)/', '<not in trap>', $display, -1, $rep);
762
+				if ($rep == 0)
763 763
 				{
764
-					$this->logging->log("Error in display",WARN,'');
764
+					$this->logging->log("Error in display", WARN, '');
765 765
 					return $display;
766 766
 				}				
767 767
 			}
@@ -771,27 +771,27 @@  discard block
 block discarded – undo
771 771
 
772 772
 	
773 773
 	/***************** Eval & tokenizer functions ****************/
774
-	protected function eval_getElement($rule,&$item)
774
+	protected function eval_getElement($rule, &$item)
775 775
 	{
776
-		while ($rule[$item]==' ') $item++;
777
-		if (preg_match('/[0-9\.]/',$rule[$item]))
776
+		while ($rule[$item] == ' ') $item++;
777
+		if (preg_match('/[0-9\.]/', $rule[$item]))
778 778
 		{ // number
779 779
 	
780
-			$item2=$item+1; 
781
-			while (($item2!=strlen($rule)) && (preg_match('/[0-9\.]/',$rule[$item2]))) { $item2++ ;}
782
-			$val=substr($rule,$item,$item2-$item);
780
+			$item2=$item + 1; 
781
+			while (($item2 != strlen($rule)) && (preg_match('/[0-9\.]/', $rule[$item2]))) { $item2++; }
782
+			$val=substr($rule, $item, $item2 - $item);
783 783
 			$item=$item2;
784 784
 			//echo "number ".$val."\n";
785
-			return array(0,$val);
785
+			return array(0, $val);
786 786
 		}
787 787
 		if ($rule[$item] == '"')
788 788
 		{ // string
789 789
 			$item++;
790
-			$item2=$this->eval_getNext($rule,$item,'"');
791
-			$val=substr($rule,$item,$item2-$item-1);
790
+			$item2=$this->eval_getNext($rule, $item, '"');
791
+			$val=substr($rule, $item, $item2 - $item - 1);
792 792
 			$item=$item2;
793 793
 			//echo "string : ".$val."\n";
794
-			return array(1,$val);
794
+			return array(1, $val);
795 795
 		}
796 796
 		
797 797
 		if ($rule[$item] == '(')
@@ -800,14 +800,14 @@  discard block
 block discarded – undo
800 800
 			$start=$item;
801 801
 			$parenthesis_count=0; 
802 802
 			while (($item < strlen($rule)) // Not end of string AND
803
-			      && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded ()
803
+			      && (($rule[$item] != ')') || $parenthesis_count > 0)) // Closing ')' or embeded ()
804 804
 			{ 
805
-				if ($rule[$item] == '"' )
805
+				if ($rule[$item] == '"')
806 806
 				{ // pass through string
807 807
 					$item++;
808
-					$item=$this->eval_getNext($rule,$item,'"');
808
+					$item=$this->eval_getNext($rule, $item, '"');
809 809
 				} 
810
-				else{
810
+				else {
811 811
 				    if ($rule[$item] == '(')
812 812
 				    {
813 813
 				        $parenthesis_count++;
@@ -820,49 +820,49 @@  discard block
 block discarded – undo
820 820
 				}
821 821
 			}
822 822
 			
823
-			if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);}
824
-			$val=substr($rule,$start,$item-$start);
823
+			if ($item == strlen($rule)) {throw new Exception("no closing () in ".$rule." at ".$item); }
824
+			$val=substr($rule, $start, $item - $start);
825 825
 			$item++;
826 826
 			$start=0;
827 827
 			//echo "group : ".$val."\n";
828 828
 			// returns evaluation of group as type 2 (boolean)
829
-			return array(2,$this->evaluation($val,$start));		
829
+			return array(2, $this->evaluation($val, $start));		
830 830
 		}
831
-		throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]);
831
+		throw new Exception("number/string not found in ".$rule." at ".$item.' : '.$rule[$item]);
832 832
 		
833 833
 	}
834 834
 	
835
-	protected function eval_getNext($rule,$item,$tok)
835
+	protected function eval_getNext($rule, $item, $tok)
836 836
 	{
837
-		while (($rule[$item] != $tok ) && ($item < strlen($rule))) { $item++;}
838
-		if ($item==strlen($rule)) throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item);
839
-		return $item+1;
837
+		while (($rule[$item] != $tok) && ($item < strlen($rule))) { $item++; }
838
+		if ($item == strlen($rule)) throw new Exception("closing '".$tok."' not found in ".$rule." at ".$item);
839
+		return $item + 1;
840 840
 	}
841 841
 	
842
-	protected function eval_getOper($rule,&$item)
842
+	protected function eval_getOper($rule, &$item)
843 843
 	{
844
-		while ($rule[$item]==' ') $item++;
844
+		while ($rule[$item] == ' ') $item++;
845 845
 		switch ($rule[$item])
846 846
 		{
847 847
 			case '<':
848
-				if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");}
849
-				$item++; return array(0,"<");
848
+				if ($rule[$item + 1] == '=') { $item+=2; return array(0, "<="); }
849
+				$item++; return array(0, "<");
850 850
 			case '>':
851
-				if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");}
852
-				$item++; return array(0,">");
851
+				if ($rule[$item + 1] == '=') { $item+=2; return array(0, ">="); }
852
+				$item++; return array(0, ">");
853 853
 			case '=':
854
-				$item++; return array(0,"=");	
854
+				$item++; return array(0, "=");	
855 855
 			case '!':
856
-				if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");}
857
-				throw new Exception("Erreur in expr - incorrect operator '!'  found in ".$rule ." at " .$item);
856
+				if ($rule[$item + 1] == '=') { $item+=2; return array(0, "!="); }
857
+				throw new Exception("Erreur in expr - incorrect operator '!'  found in ".$rule." at ".$item);
858 858
 			case '~':
859
-				$item++; return array(0,"~");	
859
+				$item++; return array(0, "~");	
860 860
 			case '|':
861
-				$item++; return array(1,"|");	
861
+				$item++; return array(1, "|");	
862 862
 			case '&':
863
-				$item++; return array(1,"&");
863
+				$item++; return array(1, "&");
864 864
 			default	:
865
-				throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item);
865
+				throw new Exception("Erreur in expr - operator not found in ".$rule." at ".$item);
866 866
 		}
867 867
 	}
868 868
 	
@@ -874,10 +874,10 @@  discard block
 block discarded – undo
874 874
 	*   comparison int vs strings will return null (error)
875 875
 	*	return : bool or null on error
876 876
 	*/
877
-	public function evaluation($rule,&$item)
877
+	public function evaluation($rule, &$item)
878 878
 	{
879 879
 	    //echo "Evaluation of ".substr($rule,$item)."\n";
880
-		if ( $rule[$item] == '!') // If '!' found, negate next expression.
880
+		if ($rule[$item] == '!') // If '!' found, negate next expression.
881 881
 		{
882 882
 		    $negate=true;
883 883
 		    $item++;
@@ -887,63 +887,63 @@  discard block
 block discarded – undo
887 887
 		    $negate=false;
888 888
 		}
889 889
 		// First element : number, string or ()
890
-		list($type1,$val1) = $this->eval_getElement($rule,$item);
890
+		list($type1, $val1)=$this->eval_getElement($rule, $item);
891 891
 		//echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n";
892 892
 		
893
-		if ($item==strlen($rule)) // If only element, return value, but only boolean
893
+		if ($item == strlen($rule)) // If only element, return value, but only boolean
894 894
 		{
895 895
 		  if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule);
896
-		  if ($negate === true) $val1= ! $val1;
896
+		  if ($negate === true) $val1=!$val1;
897 897
 		  return $val1;
898 898
 		}  
899 899
 		
900 900
 		// Second element : operator
901
-		list($typec,$comp) = $this->eval_getOper($rule,$item);
901
+		list($typec, $comp)=$this->eval_getOper($rule, $item);
902 902
 		//echo "Comp : ".$comp." : ".substr($rule,$item)."\n";
903 903
         
904 904
 		// Third element : number, string or ()
905
-		if ( $rule[$item] == '!') // starts with a ! so evaluate whats next
905
+		if ($rule[$item] == '!') // starts with a ! so evaluate whats next
906 906
 		{
907 907
 		    $item++;
908 908
 		    if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule);
909
-		    $val2= ! $this->evaluation($rule,$item);
909
+		    $val2=!$this->evaluation($rule, $item);
910 910
 		    $type2=2; // result is a boolean 
911 911
 		}
912 912
 		else 
913 913
 		{
914
-		    list($type2,$val2) = $this->eval_getElement($rule,$item);
914
+		    list($type2, $val2)=$this->eval_getElement($rule, $item);
915 915
 		}
916 916
 		//echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n";
917 917
 		
918
-		if ($type1!=$type2)  // cannot compare different types
918
+		if ($type1 != $type2)  // cannot compare different types
919 919
 		{ 
920 920
 		    throw new Exception("Cannot compare string & number : ".$rule);
921 921
 		}
922
-		if ($typec==1 && $type1 !=2) // cannot use & or | with string/number
922
+		if ($typec == 1 && $type1 != 2) // cannot use & or | with string/number
923 923
 		{
924 924
 		    throw new Exception("Cannot use boolean operators with string & number : ".$rule);
925 925
 		}
926 926
 		
927
-		switch ($comp){
928
-			case '<':	$retVal= ($val1 < $val2); break;
929
-			case '<=':	$retVal= ($val1 <= $val2); break;
930
-			case '>':	$retVal= ($val1 > $val2); break;
931
-			case '>=':	$retVal= ($val1 >= $val2); break;
932
-			case '=':	$retVal= ($val1 == $val2); break;
933
-			case '!=':	$retVal= ($val1 != $val2); break;
934
-			case '~':	$retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break;
935
-			case '|':	$retVal= ($val1 || $val2); break;
936
-			case '&':	$retVal= ($val1 && $val2); break;
927
+		switch ($comp) {
928
+			case '<':	$retVal=($val1 < $val2); break;
929
+			case '<=':	$retVal=($val1 <= $val2); break;
930
+			case '>':	$retVal=($val1 > $val2); break;
931
+			case '>=':	$retVal=($val1 >= $val2); break;
932
+			case '=':	$retVal=($val1 == $val2); break;
933
+			case '!=':	$retVal=($val1 != $val2); break;
934
+			case '~':	$retVal=(preg_match('/'.preg_replace('/"/', '', $val2).'/', $val1)); break;
935
+			case '|':	$retVal=($val1 || $val2); break;
936
+			case '&':	$retVal=($val1 && $val2); break;
937 937
 			default:  throw new Exception("Error in expression - unknown comp : ".$comp);
938 938
 		}
939
-		if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression
939
+		if ($negate === true) $retVal=!$retVal; // Inverse result if negate before expression
940 940
 		
941
-		if ($item==strlen($rule)) return $retVal; // End of string : return evaluation
941
+		if ($item == strlen($rule)) return $retVal; // End of string : return evaluation
942 942
 		// check for logical operator :
943 943
 		switch ($rule[$item])
944 944
 		{
945
-			case '|':	$item++; return ($retVal || $this->evaluation($rule,$item) );
946
-			case '&':	$item++; return ($retVal && $this->evaluation($rule,$item) );
945
+			case '|':	$item++; return ($retVal || $this->evaluation($rule, $item));
946
+			case '&':	$item++; return ($retVal && $this->evaluation($rule, $item));
947 947
 			
948 948
 			default:  throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]);
949 949
 		}
@@ -955,17 +955,17 @@  discard block
 block discarded – undo
955 955
 		$rule2='';
956 956
 		while ($item < strlen($rule))
957 957
 		{
958
-			if ($rule[$item]==' ') { $item++; continue; }
959
-			if ($rule[$item]=='"')
958
+			if ($rule[$item] == ' ') { $item++; continue; }
959
+			if ($rule[$item] == '"')
960 960
 			{
961 961
 				$rule2.=$rule[$item];
962 962
 				$item++;
963
-				while (($rule[$item]!='"') && ($item < strlen($rule)))
963
+				while (($rule[$item] != '"') && ($item < strlen($rule)))
964 964
 				{
965 965
 					$rule2.=$rule[$item];
966 966
 					$item++;
967 967
 				}
968
-				if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item);
968
+				if ($item == strlen($rule)) throw new Exception("closing '\"' not found in ".$rule." at ".$item);
969 969
 				$rule2.=$rule[$item];
970 970
 				$item++;
971 971
 				continue;
@@ -985,12 +985,12 @@  discard block
 block discarded – undo
985 985
 	
986 986
 	protected function eval_rule($rule)
987 987
 	{
988
-		if ($rule==null || $rule == '') // Empty rule is always true
988
+		if ($rule == null || $rule == '') // Empty rule is always true
989 989
 		{
990 990
 			return true;
991 991
 		}
992 992
 		$matches=array();
993
-		while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1)
993
+		while (preg_match('/_OID\(([0-9\.\*]+)\)/', $rule, $matches) == 1)
994 994
 		{
995 995
 			$oid=$matches[1];
996 996
 			$found=0;
@@ -1002,49 +1002,49 @@  discard block
 block discarded – undo
1002 1002
 			// replace * with \* in oid for preg_replace
1003 1003
 			$oid=preg_replace('/\*/', '\*', $oid);
1004 1004
 			
1005
-			$this->logging->log('OID in rule : '.$oid.' / '.$oidR,DEBUG );
1005
+			$this->logging->log('OID in rule : '.$oid.' / '.$oidR, DEBUG);
1006 1006
 			
1007
-			foreach($this->trap_data_ext as $val)
1007
+			foreach ($this->trap_data_ext as $val)
1008 1008
 			{
1009
-				if (preg_match("/^$oidR$/",$val->oid) == 1)
1009
+				if (preg_match("/^$oidR$/", $val->oid) == 1)
1010 1010
 				{
1011
-					if (!preg_match('/^[0-9]*\.?[0-9]+$/',$val->value))
1011
+					if (!preg_match('/^[0-9]*\.?[0-9]+$/', $val->value))
1012 1012
 					{ // If not a number, change " to ' and put " around it
1013
-						$val->value=preg_replace('/"/',"'",$val->value);
1013
+						$val->value=preg_replace('/"/', "'", $val->value);
1014 1014
 						$val->value='"'.$val->value.'"';
1015 1015
 					}
1016 1016
 					$rep=0;
1017
-					$rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep);
1018
-					if ($rep==0)
1017
+					$rule=preg_replace('/_OID\('.$oid.'\)/', $val->value, $rule, -1, $rep);
1018
+					if ($rep == 0)
1019 1019
 					{
1020
-						$this->logging->log("Error in rule_eval",WARN,'');
1020
+						$this->logging->log("Error in rule_eval", WARN, '');
1021 1021
 						return false;
1022 1022
 					}
1023 1023
 					$found=1;
1024 1024
 					break;
1025 1025
 				}
1026 1026
 			}
1027
-			if ($found==0)
1027
+			if ($found == 0)
1028 1028
 			{	// OID not found : throw error
1029 1029
 			    throw new Exception('OID '.$oid.' not found in trap');
1030 1030
 			}
1031 1031
 		}
1032 1032
 		$item=0;
1033 1033
 		$rule=$this->eval_cleanup($rule);
1034
-		$this->logging->log('Rule after clenup: '.$rule,INFO );
1034
+		$this->logging->log('Rule after clenup: '.$rule, INFO);
1035 1035
 		
1036
-		return  $this->evaluation($rule,$item);
1036
+		return  $this->evaluation($rule, $item);
1037 1037
 	}
1038 1038
 	
1039 1039
 	/** Match rules for current trap and do action
1040 1040
 	*/
1041 1041
 	public function applyRules()
1042 1042
 	{
1043
-		$rules = $this->getRules($this->trap_data['source_ip'],$this->trap_data['trap_oid']);
1043
+		$rules=$this->getRules($this->trap_data['source_ip'], $this->trap_data['trap_oid']);
1044 1044
 		
1045
-		if ($rules===false || count($rules)==0)
1045
+		if ($rules === false || count($rules) == 0)
1046 1046
 		{
1047
-			$this->logging->log('No rules found for this trap',INFO );
1047
+			$this->logging->log('No rules found for this trap', INFO);
1048 1048
 			$this->trap_data['status']='unknown';
1049 1049
 			$this->trap_to_db=true;
1050 1050
 			return;
@@ -1059,58 +1059,58 @@  discard block
 block discarded – undo
1059 1059
 			$service_name=$rule['service_name'];
1060 1060
 			
1061 1061
 			$display=$this->applyDisplay($rule['display']);
1062
-			$this->trap_action = ($this->trap_action==null)? '' : $this->trap_action . ', ';
1062
+			$this->trap_action=($this->trap_action == null) ? '' : $this->trap_action.', ';
1063 1063
 			try
1064 1064
 			{
1065
-				$this->logging->log('Rule to eval : '.$rule['rule'],INFO );
1065
+				$this->logging->log('Rule to eval : '.$rule['rule'], INFO);
1066 1066
 				$evalr=$this->eval_rule($rule['rule']);
1067 1067
 				
1068 1068
 				if ($evalr == true)
1069 1069
 				{
1070 1070
 					//$this->logging->log('rules OOK: '.print_r($rule),INFO );
1071 1071
 					$action=$rule['action_match'];
1072
-					$this->logging->log('action OK : '.$action,INFO );
1072
+					$this->logging->log('action OK : '.$action, INFO);
1073 1073
 					if ($action >= 0)
1074 1074
 					{
1075
-						if ($this->serviceCheckResult($host_name,$service_name,$action,$display) == false)
1075
+						if ($this->serviceCheckResult($host_name, $service_name, $action, $display) == false)
1076 1076
 						{
1077 1077
 						    $this->trap_action.='Error sending status : check cmd/API';
1078 1078
 						}
1079 1079
 						else
1080 1080
 						{
1081
-						    $this->add_rule_match($rule['id'],$rule['num_match']+1);
1081
+						    $this->add_rule_match($rule['id'], $rule['num_match'] + 1);
1082 1082
 						    $this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name;
1083 1083
 						}
1084 1084
 					}
1085 1085
 					else
1086 1086
 					{
1087
-						$this->add_rule_match($rule['id'],$rule['num_match']+1);
1087
+						$this->add_rule_match($rule['id'], $rule['num_match'] + 1);
1088 1088
 					}
1089
-					$this->trap_to_db=($action==-2)?false:true;
1089
+					$this->trap_to_db=($action == -2) ?false:true;
1090 1090
 				}
1091 1091
 				else
1092 1092
 				{
1093 1093
 					//$this->logging->log('rules KOO : '.print_r($rule),INFO );
1094 1094
 					
1095 1095
 					$action=$rule['action_nomatch'];
1096
-					$this->logging->log('action NOK : '.$action,INFO );
1096
+					$this->logging->log('action NOK : '.$action, INFO);
1097 1097
 					if ($action >= 0)
1098 1098
 					{
1099
-					    if ($this->serviceCheckResult($host_name,$service_name,$action,$display)==false)
1099
+					    if ($this->serviceCheckResult($host_name, $service_name, $action, $display) == false)
1100 1100
 					    {
1101 1101
 					        $this->trap_action.='Error sending status : check cmd/API';
1102 1102
 					    }
1103 1103
 					    else
1104 1104
 					    {
1105
-    						$this->add_rule_match($rule['id'],$rule['num_match']+1);
1105
+    						$this->add_rule_match($rule['id'], $rule['num_match'] + 1);
1106 1106
     						$this->trap_action.='Status '.$action.' to '.$host_name.'/'.$service_name;
1107 1107
 					    }
1108 1108
 					}
1109 1109
 					else
1110 1110
 					{
1111
-						$this->add_rule_match($rule['id'],$rule['num_match']+1);
1111
+						$this->add_rule_match($rule['id'], $rule['num_match'] + 1);
1112 1112
 					}
1113
-					$this->trap_to_db=($action==-2)?false:true;					
1113
+					$this->trap_to_db=($action == -2) ?false:true;					
1114 1114
 				}
1115 1115
 				// Put name in source_name
1116 1116
 				if (!isset($this->trap_data['source_name']))
@@ -1119,7 +1119,7 @@  discard block
 block discarded – undo
1119 1119
 				}
1120 1120
 				else
1121 1121
 				{
1122
-					if (!preg_match('/'.$rule['host_name'].'/',$this->trap_data['source_name']))
1122
+					if (!preg_match('/'.$rule['host_name'].'/', $this->trap_data['source_name']))
1123 1123
 					{ // only add if not present
1124 1124
 						$this->trap_data['source_name'].=','.$rule['host_name'];
1125 1125
 					}
@@ -1127,13 +1127,13 @@  discard block
 block discarded – undo
1127 1127
 			}
1128 1128
 			catch (Exception $e) 
1129 1129
 			{ 
1130
-			    $this->logging->log('Error in rule eval : '.$e->getMessage(),WARN,'');
1130
+			    $this->logging->log('Error in rule eval : '.$e->getMessage(), WARN, '');
1131 1131
 			    $this->trap_action.=' ERR : '.$e->getMessage();
1132 1132
 			    $this->trap_data['status']='error';
1133 1133
 			}
1134 1134
 			
1135 1135
 		}
1136
-		if ($this->trap_data['status']=='error')
1136
+		if ($this->trap_data['status'] == 'error')
1137 1137
 		{
1138 1138
 		  $this->trap_to_db=true; // Always put errors in DB for the use can see
1139 1139
 		}
@@ -1149,13 +1149,13 @@  discard block
 block discarded – undo
1149 1149
 	public function add_rule_final($time)
1150 1150
 	{
1151 1151
 		$db_conn=$this->trapsDB->db_connect_trap();
1152
-		if ($this->trap_action==null) 
1152
+		if ($this->trap_action == null) 
1153 1153
 		{
1154 1154
 			$this->trap_action='No action';
1155 1155
 		}
1156 1156
 		$sql="UPDATE ".$this->db_prefix."received SET process_time = '".$time."' , status_detail='".$this->trap_action."'  WHERE (id = '".$this->trap_id."');";
1157 1157
 		if ($db_conn->query($sql) === false) {
1158
-			$this->logging->log('Error in update query : ' . $sql,WARN,'');
1158
+			$this->logging->log('Error in update query : '.$sql, WARN, '');
1159 1159
 		}
1160 1160
 	}
1161 1161
 	
@@ -1178,7 +1178,7 @@  discard block
 block discarded – undo
1178 1178
     WHERE s.current_state != 0;";
1179 1179
 		$db_conn=$this->trapsDB->db_connect_ido();
1180 1180
 		if (($services_db=$db_conn->query($sql_query)) === false) { // set err to 1 to throw exception.
1181
-			$this->logging->log('No result in query : ' . $sql_query,ERROR,'');
1181
+			$this->logging->log('No result in query : '.$sql_query, ERROR, '');
1182 1182
 			return 0;
1183 1183
 		}
1184 1184
 		$services=$services_db->fetchAll();
@@ -1187,7 +1187,7 @@  discard block
 block discarded – undo
1187 1187
 		$sql_query="SELECT host_name, service_name, revert_ok FROM ".$this->db_prefix."rules where revert_ok != 0;";
1188 1188
 		$db_conn2=$this->trapsDB->db_connect_trap();
1189 1189
 		if (($rules_db=$db_conn2->query($sql_query)) === false) {
1190
-			$this->logging->log('No result in query : ' . $sql_query,ERROR,'');
1190
+			$this->logging->log('No result in query : '.$sql_query, ERROR, '');
1191 1191
 			return 0;
1192 1192
 		}
1193 1193
 		$rules=$rules_db->fetchAll();
@@ -1203,13 +1203,13 @@  discard block
 block discarded – undo
1203 1203
 					$service['host_name'] == $rule['host_name'] &&
1204 1204
 					($service['last_check'] + $rule['revert_ok']) < $now)
1205 1205
 				{
1206
-					$this->serviceCheckResult($service['host_name'],$service['service_name'],0,'Reset service to OK after '.$rule['revert_ok'].' seconds');
1206
+					$this->serviceCheckResult($service['host_name'], $service['service_name'], 0, 'Reset service to OK after '.$rule['revert_ok'].' seconds');
1207 1207
 					$numreset++;
1208 1208
 				}
1209 1209
 			}
1210 1210
 		}
1211 1211
 		echo "\n";
1212
-		echo $numreset . " service(s) reset to OK\n";
1212
+		echo $numreset." service(s) reset to OK\n";
1213 1213
 		return 0;
1214 1214
 		
1215 1215
 	}
Please login to merge, or discard this patch.
application/clicommands/MibCommand.php 1 patch
Spacing   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -36,39 +36,39 @@  discard block
 block discarded – undo
36 36
 	*/
37 37
 	public function updateAction()
38 38
 	{
39
-	    $background = $this->params->get('pid', null);
40
-	    $logLevel= $this->params->has('verb') ? 4 : 2;
41
-	    if ($this->params->has('force-check')) { echo "Not implemented"; return;}
42
-	    $forceCheck=$this->params->has('force-check')?True:False;
39
+	    $background=$this->params->get('pid', null);
40
+	    $logLevel=$this->params->has('verb') ? 4 : 2;
41
+	    if ($this->params->has('force-check')) { echo "Not implemented"; return; }
42
+	    $forceCheck=$this->params->has('force-check') ?True:False;
43 43
 	    $pid=1;
44 44
 	    if ($background != null)
45 45
 	    {
46
-	        $file=@fopen($background,'w');
46
+	        $file=@fopen($background, 'w');
47 47
 	        if ($file == false)
48 48
 	        {
49 49
 	            echo 'Error : cannot open pid file '.$background;
50 50
 	            return 1;
51 51
 	        }
52
-	        $pid = pcntl_fork();
52
+	        $pid=pcntl_fork();
53 53
 	        if ($pid == -1) {
54 54
 	            echo 'Error : Cannot fork process';
55 55
 	            return 1;
56 56
 	        }
57 57
 	    }
58 58
 	    $module=Icinga::app()->getModuleManager()->getModule($this->getModuleName());
59
-		require_once($module->getBaseDir() .'/bin/trap_class.php');
59
+		require_once($module->getBaseDir().'/bin/trap_class.php');
60 60
 		$icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc');
61
-		$trap = new Trap($icingaweb2_etc);
61
+		$trap=new Trap($icingaweb2_etc);
62 62
 		if ($pid == 1)
63 63
 		{
64
-		    $trap->setLogging($logLevel,'display');
64
+		    $trap->setLogging($logLevel, 'display');
65 65
 		}
66 66
 		else
67 67
 		{  // use default display TODO : if default is 'display' son process will be killed at first output....
68 68
 		    if ($pid != 0)
69 69
 		    {
70 70
 		        // father process
71
-		        fwrite($file,$pid);
71
+		        fwrite($file, $pid);
72 72
 		        fclose($file);
73 73
 		        echo "OK : process $pid in bckground";
74 74
 		        return 0;
@@ -82,11 +82,11 @@  discard block
 block discarded – undo
82 82
                 fclose(STDERR);
83 83
                 try
84 84
                 {
85
-                    $trap->mibClass->update_mib_database(false,$forceCheck);
85
+                    $trap->mibClass->update_mib_database(false, $forceCheck);
86 86
                 }
87 87
                 catch (Exception $e)
88 88
                 {
89
-                    $trap->trapLog('Error in updating : ' . $e->getMessage(),2);
89
+                    $trap->trapLog('Error in updating : '.$e->getMessage(), 2);
90 90
                 }
91 91
                 unlink($background);
92 92
                 return 0;
@@ -98,13 +98,13 @@  discard block
 block discarded – undo
98 98
 		{
99 99
 			echo "Update main mib database : \n";
100 100
 			echo "# (trap found) C (trap already processed) . (every 2 seconds) : \n";
101
-			$trap->mibClass->update_mib_database(true,$forceCheck);
101
+			$trap->mibClass->update_mib_database(true, $forceCheck);
102 102
 			echo "Done\n";
103 103
 			
104 104
 		}
105 105
 		catch (Exception $e)
106 106
 		{
107
-			echo 'Error in updating : ' . $e->getMessage();
107
+			echo 'Error in updating : '.$e->getMessage();
108 108
 		}
109 109
 		if ($pid != 1)
110 110
 		{
@@ -128,26 +128,26 @@  discard block
 block discarded – undo
128 128
 		echo "Not implemented";
129 129
 		// TODO : implement
130 130
 		return;
131
-		$Config = new TrapModuleConfig($db_prefix);
131
+		$Config=new TrapModuleConfig($db_prefix);
132 132
 		
133 133
 		try
134 134
 		{
135 135
 			
136 136
 			$dbresource=$this->Config()->get('config', 'database');
137 137
 			echo "DB name : $dbresource\n";
138
-			$db = IcingaDbConnection::fromResourceName($dbresource)->getConnection();
138
+			$db=IcingaDbConnection::fromResourceName($dbresource)->getConnection();
139 139
 			
140
-			$query = $db->select()->from($Config->getTrapTableName(),array('COUNT(*)'));			
141
-			echo "Number of traps : " . $db->fetchOne($query) ."\n";
142
-			$query = $db->select()->from($Config->getTrapDataTableName(),array('COUNT(*)'));			
143
-			echo "Number of trap objects : " . $db->fetchOne($query) ."\n";
144
-			$query = $db->select()->from($Config->getTrapRuleName(),array('COUNT(*)'));			
145
-			echo "Number of rules : " . $db->fetchOne($query) ."\n";		
140
+			$query=$db->select()->from($Config->getTrapTableName(), array('COUNT(*)'));			
141
+			echo "Number of traps : ".$db->fetchOne($query)."\n";
142
+			$query=$db->select()->from($Config->getTrapDataTableName(), array('COUNT(*)'));			
143
+			echo "Number of trap objects : ".$db->fetchOne($query)."\n";
144
+			$query=$db->select()->from($Config->getTrapRuleName(), array('COUNT(*)'));			
145
+			echo "Number of rules : ".$db->fetchOne($query)."\n";		
146 146
 			
147 147
 		}
148 148
 		catch (Exception $e)
149 149
 		{
150
-			echo 'Error in DB : ' . $e->getMessage();
150
+			echo 'Error in DB : '.$e->getMessage();
151 151
 		}	   
152 152
 	}  	 	
153 153
 
Please login to merge, or discard this patch.