Passed
Push — master ( ce1111...6b0d98 )
by Patrick
02:53
created
library/Trapdirector/TrapsProcess/Rule.php 1 patch
Spacing   +95 added lines, -95 removed lines patch added patch discarded remove patch
@@ -23,30 +23,30 @@  discard block
 block discarded – undo
23 23
  * Get full number 
24 24
  * @return array<number,string>
25 25
  */
26
-    private function get_number($rule,&$item)
26
+    private function get_number($rule, &$item)
27 27
     {
28
-        $item2=$item+1;
28
+        $item2=$item + 1;
29 29
         while (
30
-            ($item2!=strlen($rule)) 
31
-            && (preg_match('/[\-0-9\.]/',$rule[$item2]))) 
30
+            ($item2 != strlen($rule)) 
31
+            && (preg_match('/[\-0-9\.]/', $rule[$item2]))) 
32 32
         { 
33
-            $item2++ ;
33
+            $item2++;
34 34
         }
35
-        $val=substr($rule,$item,$item2-$item);
35
+        $val=substr($rule, $item, $item2 - $item);
36 36
         $item=$item2;
37 37
         //echo "number ".$val."\n";
38 38
         
39
-        return array(0,$val);
39
+        return array(0, $val);
40 40
     }
41 41
 
42
-    private function get_string($rule,&$item)
42
+    private function get_string($rule, &$item)
43 43
     {
44 44
         $item++;
45
-        $item2=$this->eval_getNext($rule,$item,'"');
46
-        $val=substr($rule,$item,$item2-$item-1);
45
+        $item2=$this->eval_getNext($rule, $item, '"');
46
+        $val=substr($rule, $item, $item2 - $item - 1);
47 47
         $item=$item2;
48 48
         //echo "string : ".$val."\n";
49
-        return array(1,$val);
49
+        return array(1, $val);
50 50
         
51 51
     }
52 52
     
@@ -58,20 +58,20 @@  discard block
 block discarded – undo
58 58
      * @throws Exception
59 59
      * @return string : everything inside parenthesis
60 60
      */
61
-    private function parse_parenthesis(string $rule,int &$item) : string
61
+    private function parse_parenthesis(string $rule, int &$item) : string
62 62
     {
63 63
         $item++;
64 64
         $start=$item;
65 65
         $parenthesis_count=0;
66 66
         while (($item < strlen($rule)) // Not end of string AND
67
-            && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded ()
67
+            && (($rule[$item] != ')') || $parenthesis_count > 0)) // Closing ')' or embeded ()
68 68
         {
69
-            if ($rule[$item] == '"' )
69
+            if ($rule[$item] == '"')
70 70
             { // pass through string
71 71
                 $item++;
72
-                $item=$this->eval_getNext($rule,$item,'"');
72
+                $item=$this->eval_getNext($rule, $item, '"');
73 73
             }
74
-            else{
74
+            else {
75 75
                 if ($rule[$item] == '(')
76 76
                 {
77 77
                     $parenthesis_count++;
@@ -84,8 +84,8 @@  discard block
 block discarded – undo
84 84
             }
85 85
         }
86 86
         
87
-        if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);}
88
-        $val=substr($rule,$start,$item-$start);
87
+        if ($item == strlen($rule)) {throw new Exception("no closing () in ".$rule." at ".$item); }
88
+        $val=substr($rule, $start, $item - $start);
89 89
         $item++;
90 90
         return $val;
91 91
     }
@@ -97,45 +97,45 @@  discard block
 block discarded – undo
97 97
      * @param int $item
98 98
      * @return array
99 99
      */
100
-    private function get_group(string $rule,int &$item) : array
100
+    private function get_group(string $rule, int &$item) : array
101 101
     {
102 102
         // gets eveything inside parenthesis
103 103
         $val=$this->parse_parenthesis($rule, $item);
104 104
         // Returns boolean with evaluation of all inside parenthesis
105 105
         $start=0;
106
-        return array(2,$this->evaluation($val,$start));
106
+        return array(2, $this->evaluation($val, $start));
107 107
     }
108 108
 
109
-    private function get_function(string $rule,int &$item) : array
109
+    private function get_function(string $rule, int &$item) : array
110 110
     {
111 111
         // function is : __function(param1,param2...)
112 112
         $start=$item; 
113
-        while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '('
113
+        while (($item < strlen($rule)) && ($rule[$item] != '(')) // Not end of string AND not opening '('
114 114
         {
115 115
             $item++;
116 116
         }        
117
-        if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);}
117
+        if ($item == strlen($rule)) {throw new Exception("no opening () for function in ".$rule." at ".$item); }
118 118
         
119 119
         // get parameters between parenthesis
120 120
         
121 121
         $params=$this->parse_parenthesis($rule, $item);
122 122
         
123
-        $val=substr($rule,$start,$item-$start);
123
+        $val=substr($rule, $start, $item - $start);
124 124
         
125
-        $this->logging->log('got function ' . $val . ' returning true for now',DEBUG);
125
+        $this->logging->log('got function '.$val.' returning true for now', DEBUG);
126 126
         
127
-        return array(2,true);
127
+        return array(2, true);
128 128
         
129 129
     }
130 130
     
131
-    protected function eval_getElement($rule,&$item)
131
+    protected function eval_getElement($rule, &$item)
132 132
     {
133 133
         if ($item >= strlen($rule))
134 134
         {
135
-            throw new Exception("Early end of string ".$rule ." at " .$item );
135
+            throw new Exception("Early end of string ".$rule." at ".$item);
136 136
         }
137
-        while ($rule[$item]==' ') $item++;
138
-        if (preg_match('/[\-0-9\.]/',$rule[$item]))
137
+        while ($rule[$item] == ' ') $item++;
138
+        if (preg_match('/[\-0-9\.]/', $rule[$item]))
139 139
         { // number
140 140
             return $this->get_number($rule, $item);
141 141
         }
@@ -152,54 +152,54 @@  discard block
 block discarded – undo
152 152
         { // function
153 153
             return $this->get_function($rule, $item);
154 154
         }
155
-        throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]);
155
+        throw new Exception("number/string not found in ".$rule." at ".$item.' : '.$rule[$item]);
156 156
         
157 157
     }
158 158
     
159
-    protected function eval_getNext($rule,$item,$tok)
159
+    protected function eval_getNext($rule, $item, $tok)
160 160
     {
161 161
         while (
162
-            ($rule[$item] != $tok ) 
162
+            ($rule[$item] != $tok) 
163 163
             && ($item < strlen($rule))) 
164 164
         { 
165 165
             $item++;
166 166
         }
167
-        if ($item==strlen($rule)) {
168
-            throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item);
167
+        if ($item == strlen($rule)) {
168
+            throw new Exception("closing '".$tok."' not found in ".$rule." at ".$item);
169 169
         }
170
-        return $item+1;
170
+        return $item + 1;
171 171
     }
172 172
     
173
-    protected function eval_getOper($rule,&$item)
173
+    protected function eval_getOper($rule, &$item)
174 174
     {
175
-        while ($rule[$item]==' ') $item++;
175
+        while ($rule[$item] == ' ') $item++;
176 176
         switch ($rule[$item])
177 177
         {
178 178
             case '<':
179
-                if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");}
180
-                $item++; return array(0,"<");
179
+                if ($rule[$item + 1] == '=') { $item+=2; return array(0, "<="); }
180
+                $item++; return array(0, "<");
181 181
             case '>':
182
-                if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");}
183
-                $item++; return array(0,">");
182
+                if ($rule[$item + 1] == '=') { $item+=2; return array(0, ">="); }
183
+                $item++; return array(0, ">");
184 184
             case '=':
185
-                $item++; return array(0,"=");
185
+                $item++; return array(0, "=");
186 186
             case '!':
187
-                if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");}
188
-                throw new Exception("Erreur in expr - incorrect operator '!'  found in ".$rule ." at " .$item);
187
+                if ($rule[$item + 1] == '=') { $item+=2; return array(0, "!="); }
188
+                throw new Exception("Erreur in expr - incorrect operator '!'  found in ".$rule." at ".$item);
189 189
             case '~':
190
-                $item++; return array(0,"~");
190
+                $item++; return array(0, "~");
191 191
             case '|':
192
-                $item++; return array(1,"|");
192
+                $item++; return array(1, "|");
193 193
             case '&':
194
-                $item++; return array(1,"&");
194
+                $item++; return array(1, "&");
195 195
             default	:
196
-                throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item);
196
+                throw new Exception("Erreur in expr - operator not found in ".$rule." at ".$item);
197 197
         }
198 198
     }
199 199
     
200
-    private function check_negate_first($rule,&$item)
200
+    private function check_negate_first($rule, &$item)
201 201
     {
202
-        if ( $rule[$item] == '!') // If '!' found, negate next expression.
202
+        if ($rule[$item] == '!') // If '!' found, negate next expression.
203 203
         {
204 204
             $item++;
205 205
             return true;
@@ -210,21 +210,21 @@  discard block
 block discarded – undo
210 210
         }
211 211
     }
212 212
 
213
-    private function do_compare($val1,$val2,$comp,$negate)
213
+    private function do_compare($val1, $val2, $comp, $negate)
214 214
     {
215
-        switch ($comp){
216
-            case '<':	$retVal= ($val1 < $val2); break;
217
-            case '<=':	$retVal= ($val1 <= $val2); break;
218
-            case '>':	$retVal= ($val1 > $val2); break;
219
-            case '>=':	$retVal= ($val1 >= $val2); break;
220
-            case '=':	$retVal= ($val1 == $val2); break;
221
-            case '!=':	$retVal= ($val1 != $val2); break;
222
-            case '~':	$retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break;
223
-            case '|':	$retVal= ($val1 || $val2); break;
224
-            case '&':	$retVal= ($val1 && $val2); break;
215
+        switch ($comp) {
216
+            case '<':	$retVal=($val1 < $val2); break;
217
+            case '<=':	$retVal=($val1 <= $val2); break;
218
+            case '>':	$retVal=($val1 > $val2); break;
219
+            case '>=':	$retVal=($val1 >= $val2); break;
220
+            case '=':	$retVal=($val1 == $val2); break;
221
+            case '!=':	$retVal=($val1 != $val2); break;
222
+            case '~':	$retVal=(preg_match('/'.preg_replace('/"/', '', $val2).'/', $val1)); break;
223
+            case '|':	$retVal=($val1 || $val2); break;
224
+            case '&':	$retVal=($val1 && $val2); break;
225 225
             default:  throw new Exception("Error in expression - unknown comp : ".$comp);
226 226
         }
227
-        if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression
227
+        if ($negate === true) $retVal=!$retVal; // Inverse result if negate before expression
228 228
         
229 229
         return $retVal;
230 230
     }
@@ -237,56 +237,56 @@  discard block
 block discarded – undo
237 237
      *   comparison int vs strings will return null (error)
238 238
      *	return : bool or null on error
239 239
      */
240
-    public function evaluation($rule,&$item)
240
+    public function evaluation($rule, &$item)
241 241
     {
242 242
         //echo "Evaluation of ".substr($rule,$item)."\n";
243 243
         $negate=$this->check_negate_first($rule, $item);
244 244
         // First element : number, string or ()
245
-        list($type1,$val1) = $this->eval_getElement($rule,$item);
245
+        list($type1, $val1)=$this->eval_getElement($rule, $item);
246 246
         //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n";
247 247
         
248
-        if ($item==strlen($rule)) // If only element, return value, but only boolean
248
+        if ($item == strlen($rule)) // If only element, return value, but only boolean
249 249
         {
250 250
             if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule);
251
-            if ($negate === true) $val1= ! $val1;
251
+            if ($negate === true) $val1=!$val1;
252 252
             return $val1;
253 253
         }
254 254
         
255 255
         // Second element : operator
256
-        list($typec,$comp) = $this->eval_getOper($rule,$item);
256
+        list($typec, $comp)=$this->eval_getOper($rule, $item);
257 257
         //echo "Comp : ".$comp." : ".substr($rule,$item)."\n";
258 258
         
259 259
         // Third element : number, string or ()
260
-        if ( $rule[$item] == '!') // starts with a ! so evaluate whats next
260
+        if ($rule[$item] == '!') // starts with a ! so evaluate whats next
261 261
         {
262 262
             $item++;
263 263
             if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule);
264
-            $val2= ! $this->evaluation($rule,$item);
264
+            $val2=!$this->evaluation($rule, $item);
265 265
             $type2=2; // result is a boolean
266 266
         }
267 267
         else
268 268
         {
269
-            list($type2,$val2) = $this->eval_getElement($rule,$item);
269
+            list($type2, $val2)=$this->eval_getElement($rule, $item);
270 270
         }
271 271
         //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n";
272 272
         
273
-        if ($type1!=$type2)  // cannot compare different types
273
+        if ($type1 != $type2)  // cannot compare different types
274 274
         {
275 275
             throw new Exception("Cannot compare string & number : ".$rule);
276 276
         }
277
-        if ($typec==1 && $type1 !=2) // cannot use & or | with string/number
277
+        if ($typec == 1 && $type1 != 2) // cannot use & or | with string/number
278 278
         {
279 279
             throw new Exception("Cannot use boolean operators with string & number : ".$rule);
280 280
         }
281 281
         
282
-        $retVal = $this->do_compare($val1, $val2, $comp, $negate);
282
+        $retVal=$this->do_compare($val1, $val2, $comp, $negate);
283 283
         
284
-        if ($item==strlen($rule)) return $retVal; // End of string : return evaluation
284
+        if ($item == strlen($rule)) return $retVal; // End of string : return evaluation
285 285
         // check for logical operator :
286 286
         switch ($rule[$item])
287 287
         {
288
-            case '|':	$item++; return ($retVal || $this->evaluation($rule,$item) );
289
-            case '&':	$item++; return ($retVal && $this->evaluation($rule,$item) );
288
+            case '|':	$item++; return ($retVal || $this->evaluation($rule, $item));
289
+            case '&':	$item++; return ($retVal && $this->evaluation($rule, $item));
290 290
             
291 291
             default:  throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]);
292 292
         }
@@ -299,17 +299,17 @@  discard block
 block discarded – undo
299 299
         $rule2='';
300 300
         while ($item < strlen($rule))
301 301
         {
302
-            if ($rule[$item]==' ') { $item++; continue; }
303
-            if ($rule[$item]=='"')
302
+            if ($rule[$item] == ' ') { $item++; continue; }
303
+            if ($rule[$item] == '"')
304 304
             {
305 305
                 $rule2.=$rule[$item];
306 306
                 $item++;
307
-                while (($item < strlen($rule)) && ($rule[$item]!='"') )
307
+                while (($item < strlen($rule)) && ($rule[$item] != '"'))
308 308
                 {
309 309
                     $rule2.=$rule[$item];
310 310
                     $item++;
311 311
                 }
312
-                if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item);
312
+                if ($item == strlen($rule)) throw new Exception("closing '\"' not found in ".$rule." at ".$item);
313 313
                 $rule2.=$rule[$item];
314 314
                 $item++;
315 315
                 continue;
@@ -328,14 +328,14 @@  discard block
 block discarded – undo
328 328
      *	@return bool : true : rule match, false : rule don't match , throw exception on error.
329 329
      */
330 330
     
331
-    public function eval_rule($rule,$oidList)
331
+    public function eval_rule($rule, $oidList)
332 332
     {
333
-        if ($rule==null || $rule == '') // Empty rule is always true
333
+        if ($rule == null || $rule == '') // Empty rule is always true
334 334
         {
335 335
             return true;
336 336
         }
337 337
         $matches=array();
338
-        while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1)
338
+        while (preg_match('/_OID\(([0-9\.\*]+)\)/', $rule, $matches) == 1)
339 339
         {
340 340
             $oid=$matches[1];
341 341
             $found=0;
@@ -347,38 +347,38 @@  discard block
 block discarded – undo
347 347
             // replace * with \* in oid for preg_replace
348 348
             $oid=preg_replace('/\*/', '\*', $oid);
349 349
             
350
-            $this->logging->log('OID in rule : '.$oid.' / '.$oidR,DEBUG );
350
+            $this->logging->log('OID in rule : '.$oid.' / '.$oidR, DEBUG);
351 351
             
352
-            foreach($oidList as $val)
352
+            foreach ($oidList as $val)
353 353
             {
354
-                if (preg_match("/^$oidR$/",$val->oid) == 1)
354
+                if (preg_match("/^$oidR$/", $val->oid) == 1)
355 355
                 {
356
-                    if (!preg_match('/^-?[0-9]*\.?[0-9]+$/',$val->value))
356
+                    if (!preg_match('/^-?[0-9]*\.?[0-9]+$/', $val->value))
357 357
                     { // If not a number, change " to ' and put " around it
358
-                        $val->value=preg_replace('/"/',"'",$val->value);
358
+                        $val->value=preg_replace('/"/', "'", $val->value);
359 359
                         $val->value='"'.$val->value.'"';
360 360
                     }
361 361
                     $rep=0;
362
-                    $rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep);
363
-                    if ($rep==0)
362
+                    $rule=preg_replace('/_OID\('.$oid.'\)/', $val->value, $rule, -1, $rep);
363
+                    if ($rep == 0)
364 364
                     {
365
-                        $this->logging->log("Error in rule_eval",WARN,'');
365
+                        $this->logging->log("Error in rule_eval", WARN, '');
366 366
                         return false;
367 367
                     }
368 368
                     $found=1;
369 369
                     break;
370 370
                 }
371 371
             }
372
-            if ($found==0)
372
+            if ($found == 0)
373 373
             {	// OID not found : throw error
374 374
                 throw new Exception('OID '.$oid.' not found in trap');
375 375
             }
376 376
         }
377 377
         $item=0;
378 378
         $rule=$this->eval_cleanup($rule);
379
-        $this->logging->log('Rule after clenup: '.$rule,INFO );
379
+        $this->logging->log('Rule after clenup: '.$rule, INFO);
380 380
         
381
-        return  $this->evaluation($rule,$item);
381
+        return  $this->evaluation($rule, $item);
382 382
     }
383 383
     
384 384
 }
385 385
\ No newline at end of file
Please login to merge, or discard this patch.
library/Trapdirector/Plugins/NetworkRule.php 1 patch
Spacing   +17 added lines, -17 removed lines patch added patch discarded remove patch
@@ -53,7 +53,7 @@  discard block
 block discarded – undo
53 53
      */
54 54
     function __construct()
55 55
     {
56
-        $this->name=basename(__FILE__,'.php');
56
+        $this->name=basename(__FILE__, '.php');
57 57
         return $this;
58 58
     }
59 59
     
@@ -67,41 +67,41 @@  discard block
 block discarded – undo
67 67
     public function isInNetwork(array $params) : bool
68 68
     {
69 69
         // Check param numbers and thrown exception if not correct.
70
-        if (count($params)!=3)
70
+        if (count($params) != 3)
71 71
         {
72
-            throw new Exception('Invalid number of parameters : ' . count($params));
72
+            throw new Exception('Invalid number of parameters : '.count($params));
73 73
         }
74 74
         
75
-        $ip = $params[0];
76
-        $net = $params[1];
77
-        $masq = $params[2];
75
+        $ip=$params[0];
76
+        $net=$params[1];
77
+        $masq=$params[2];
78 78
         
79 79
         
80
-        $this->log('#'. $ip . '# / #' . $net . '# / #' . $masq,DEBUG);
80
+        $this->log('#'.$ip.'# / #'.$net.'# / #'.$masq, DEBUG);
81 81
         
82
-        $ip2 = ip2long($ip);
83
-        $net2 = ip2long($net);
82
+        $ip2=ip2long($ip);
83
+        $net2=ip2long($net);
84 84
         
85
-        if ($ip2 === false )
85
+        if ($ip2 === false)
86 86
         {
87
-            $this->log('Invalid IP : #' . $ip.'#',WARN);
87
+            $this->log('Invalid IP : #'.$ip.'#', WARN);
88 88
             throw new Exception('Invalid IP');
89 89
         }
90 90
         if ($net2 === false)
91 91
         {
92
-            $this->log('Invalid network',WARN);
92
+            $this->log('Invalid network', WARN);
93 93
             throw new Exception('Invalid net');
94 94
         }
95
-        if ($masq<1 || $masq > 32)
95
+        if ($masq < 1 || $masq > 32)
96 96
         {
97
-            $this->log('Invalid masq',WARN);
97
+            $this->log('Invalid masq', WARN);
98 98
             throw new Exception('Invalid net masq');
99 99
         }
100 100
         // $range is in IP/CIDR format eg 127.0.0.1/24
101 101
 
102
-        $masq = pow( 2, ( 32 - $masq ) ) - 1;
103
-        $masq = ~ $masq;
104
-        return ( ( $ip2 & $masq ) == ( $net2 & $masq ) );
102
+        $masq=pow(2, (32 - $masq)) - 1;
103
+        $masq=~ $masq;
104
+        return (($ip2 & $masq) == ($net2 & $masq));
105 105
         
106 106
     }
107 107
 }
Please login to merge, or discard this patch.