Passed
Push — master ( 278791...f4c14e )
by Patrick
01:54
created
library/Trapdirector/TrapsProcess/Rule.php 1 patch
Spacing   +88 added lines, -88 removed lines patch added patch discarded remove patch
@@ -27,47 +27,47 @@  discard block
 block discarded – undo
27 27
  * Get full number 
28 28
  * @return array<number,string>
29 29
  */
30
-    private function get_number($rule,&$item)
30
+    private function get_number($rule, &$item)
31 31
     {
32
-        $item2=$item+1;
32
+        $item2=$item + 1;
33 33
         while (
34
-            ($item2!=strlen($rule)) 
35
-            && (preg_match('/[0-9\.]/',$rule[$item2]))) 
34
+            ($item2 != strlen($rule)) 
35
+            && (preg_match('/[0-9\.]/', $rule[$item2]))) 
36 36
         { 
37
-            $item2++ ;
37
+            $item2++;
38 38
         }
39
-        $val=substr($rule,$item,$item2-$item);
39
+        $val=substr($rule, $item, $item2 - $item);
40 40
         $item=$item2;
41 41
         //echo "number ".$val."\n";
42 42
         
43
-        return array(0,$val);
43
+        return array(0, $val);
44 44
     }
45 45
 
46
-    private function get_string($rule,&$item)
46
+    private function get_string($rule, &$item)
47 47
     {
48 48
         $item++;
49
-        $item2=$this->eval_getNext($rule,$item,'"');
50
-        $val=substr($rule,$item,$item2-$item-1);
49
+        $item2=$this->eval_getNext($rule, $item, '"');
50
+        $val=substr($rule, $item, $item2 - $item - 1);
51 51
         $item=$item2;
52 52
         //echo "string : ".$val."\n";
53
-        return array(1,$val);
53
+        return array(1, $val);
54 54
         
55 55
     }
56 56
     
57
-    private function get_group($rule,&$item)
57
+    private function get_group($rule, &$item)
58 58
     {
59 59
         $item++;
60 60
         $start=$item;
61 61
         $parenthesis_count=0;
62 62
         while (($item < strlen($rule)) // Not end of string AND
63
-            && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded ()
63
+            && (($rule[$item] != ')') || $parenthesis_count > 0)) // Closing ')' or embeded ()
64 64
         {
65
-            if ($rule[$item] == '"' )
65
+            if ($rule[$item] == '"')
66 66
             { // pass through string
67 67
                 $item++;
68
-                $item=$this->eval_getNext($rule,$item,'"');
68
+                $item=$this->eval_getNext($rule, $item, '"');
69 69
             }
70
-            else{
70
+            else {
71 71
                 if ($rule[$item] == '(')
72 72
                 {
73 73
                     $parenthesis_count++;
@@ -80,23 +80,23 @@  discard block
 block discarded – undo
80 80
             }
81 81
         }
82 82
         
83
-        if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);}
84
-        $val=substr($rule,$start,$item-$start);
83
+        if ($item == strlen($rule)) {throw new Exception("no closing () in ".$rule." at ".$item); }
84
+        $val=substr($rule, $start, $item - $start);
85 85
         $item++;
86 86
         $start=0;
87 87
         //echo "group : ".$val."\n";
88 88
         // returns evaluation of group as type 2 (boolean)
89
-        return array(2,$this->evaluation($val,$start));
89
+        return array(2, $this->evaluation($val, $start));
90 90
     }
91 91
     
92
-    protected function eval_getElement($rule,&$item)
92
+    protected function eval_getElement($rule, &$item)
93 93
     {
94 94
         if ($item >= strlen($rule))
95 95
         {
96
-            throw new Exception("Early end of string ".$rule ." at " .$item );
96
+            throw new Exception("Early end of string ".$rule." at ".$item);
97 97
         }
98
-        while ($rule[$item]==' ') $item++;
99
-        if (preg_match('/[0-9\.]/',$rule[$item]))
98
+        while ($rule[$item] == ' ') $item++;
99
+        if (preg_match('/[0-9\.]/', $rule[$item]))
100 100
         { // number
101 101
             return $this->get_number($rule, $item);
102 102
         }
@@ -109,54 +109,54 @@  discard block
 block discarded – undo
109 109
         { // grouping
110 110
             return $this->get_group($rule, $item);
111 111
         }
112
-        throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]);
112
+        throw new Exception("number/string not found in ".$rule." at ".$item.' : '.$rule[$item]);
113 113
         
114 114
     }
115 115
     
116
-    protected function eval_getNext($rule,$item,$tok)
116
+    protected function eval_getNext($rule, $item, $tok)
117 117
     {
118 118
         while (
119
-            ($rule[$item] != $tok ) 
119
+            ($rule[$item] != $tok) 
120 120
             && ($item < strlen($rule))) 
121 121
         { 
122 122
             $item++;
123 123
         }
124
-        if ($item==strlen($rule)) {
125
-            throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item);
124
+        if ($item == strlen($rule)) {
125
+            throw new Exception("closing '".$tok."' not found in ".$rule." at ".$item);
126 126
         }
127
-        return $item+1;
127
+        return $item + 1;
128 128
     }
129 129
     
130
-    protected function eval_getOper($rule,&$item)
130
+    protected function eval_getOper($rule, &$item)
131 131
     {
132
-        while ($rule[$item]==' ') $item++;
132
+        while ($rule[$item] == ' ') $item++;
133 133
         switch ($rule[$item])
134 134
         {
135 135
             case '<':
136
-                if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");}
137
-                $item++; return array(0,"<");
136
+                if ($rule[$item + 1] == '=') { $item+=2; return array(0, "<="); }
137
+                $item++; return array(0, "<");
138 138
             case '>':
139
-                if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");}
140
-                $item++; return array(0,">");
139
+                if ($rule[$item + 1] == '=') { $item+=2; return array(0, ">="); }
140
+                $item++; return array(0, ">");
141 141
             case '=':
142
-                $item++; return array(0,"=");
142
+                $item++; return array(0, "=");
143 143
             case '!':
144
-                if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");}
145
-                throw new Exception("Erreur in expr - incorrect operator '!'  found in ".$rule ." at " .$item);
144
+                if ($rule[$item + 1] == '=') { $item+=2; return array(0, "!="); }
145
+                throw new Exception("Erreur in expr - incorrect operator '!'  found in ".$rule." at ".$item);
146 146
             case '~':
147
-                $item++; return array(0,"~");
147
+                $item++; return array(0, "~");
148 148
             case '|':
149
-                $item++; return array(1,"|");
149
+                $item++; return array(1, "|");
150 150
             case '&':
151
-                $item++; return array(1,"&");
151
+                $item++; return array(1, "&");
152 152
             default	:
153
-                throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item);
153
+                throw new Exception("Erreur in expr - operator not found in ".$rule." at ".$item);
154 154
         }
155 155
     }
156 156
     
157
-    private function check_negate_first($rule,&$item)
157
+    private function check_negate_first($rule, &$item)
158 158
     {
159
-        if ( $rule[$item] == '!') // If '!' found, negate next expression.
159
+        if ($rule[$item] == '!') // If '!' found, negate next expression.
160 160
         {
161 161
             $item++;
162 162
             return true;
@@ -167,21 +167,21 @@  discard block
 block discarded – undo
167 167
         }
168 168
     }
169 169
 
170
-    private function do_compare($val1,$val2,$comp,$negate)
170
+    private function do_compare($val1, $val2, $comp, $negate)
171 171
     {
172
-        switch ($comp){
173
-            case '<':	$retVal= ($val1 < $val2); break;
174
-            case '<=':	$retVal= ($val1 <= $val2); break;
175
-            case '>':	$retVal= ($val1 > $val2); break;
176
-            case '>=':	$retVal= ($val1 >= $val2); break;
177
-            case '=':	$retVal= ($val1 == $val2); break;
178
-            case '!=':	$retVal= ($val1 != $val2); break;
179
-            case '~':	$retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break;
180
-            case '|':	$retVal= ($val1 || $val2); break;
181
-            case '&':	$retVal= ($val1 && $val2); break;
172
+        switch ($comp) {
173
+            case '<':	$retVal=($val1 < $val2); break;
174
+            case '<=':	$retVal=($val1 <= $val2); break;
175
+            case '>':	$retVal=($val1 > $val2); break;
176
+            case '>=':	$retVal=($val1 >= $val2); break;
177
+            case '=':	$retVal=($val1 == $val2); break;
178
+            case '!=':	$retVal=($val1 != $val2); break;
179
+            case '~':	$retVal=(preg_match('/'.preg_replace('/"/', '', $val2).'/', $val1)); break;
180
+            case '|':	$retVal=($val1 || $val2); break;
181
+            case '&':	$retVal=($val1 && $val2); break;
182 182
             default:  throw new Exception("Error in expression - unknown comp : ".$comp);
183 183
         }
184
-        if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression
184
+        if ($negate === true) $retVal=!$retVal; // Inverse result if negate before expression
185 185
         
186 186
         return $retVal;
187 187
     }
@@ -194,56 +194,56 @@  discard block
 block discarded – undo
194 194
      *   comparison int vs strings will return null (error)
195 195
      *	return : bool or null on error
196 196
      */
197
-    public function evaluation($rule,&$item)
197
+    public function evaluation($rule, &$item)
198 198
     {
199 199
         //echo "Evaluation of ".substr($rule,$item)."\n";
200 200
         $negate=$this->check_negate_first($rule, $item);
201 201
         // First element : number, string or ()
202
-        list($type1,$val1) = $this->eval_getElement($rule,$item);
202
+        list($type1, $val1)=$this->eval_getElement($rule, $item);
203 203
         //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n";
204 204
         
205
-        if ($item==strlen($rule)) // If only element, return value, but only boolean
205
+        if ($item == strlen($rule)) // If only element, return value, but only boolean
206 206
         {
207 207
             if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule);
208
-            if ($negate === true) $val1= ! $val1;
208
+            if ($negate === true) $val1=!$val1;
209 209
             return $val1;
210 210
         }
211 211
         
212 212
         // Second element : operator
213
-        list($typec,$comp) = $this->eval_getOper($rule,$item);
213
+        list($typec, $comp)=$this->eval_getOper($rule, $item);
214 214
         //echo "Comp : ".$comp." : ".substr($rule,$item)."\n";
215 215
         
216 216
         // Third element : number, string or ()
217
-        if ( $rule[$item] == '!') // starts with a ! so evaluate whats next
217
+        if ($rule[$item] == '!') // starts with a ! so evaluate whats next
218 218
         {
219 219
             $item++;
220 220
             if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule);
221
-            $val2= ! $this->evaluation($rule,$item);
221
+            $val2=!$this->evaluation($rule, $item);
222 222
             $type2=2; // result is a boolean
223 223
         }
224 224
         else
225 225
         {
226
-            list($type2,$val2) = $this->eval_getElement($rule,$item);
226
+            list($type2, $val2)=$this->eval_getElement($rule, $item);
227 227
         }
228 228
         //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n";
229 229
         
230
-        if ($type1!=$type2)  // cannot compare different types
230
+        if ($type1 != $type2)  // cannot compare different types
231 231
         {
232 232
             throw new Exception("Cannot compare string & number : ".$rule);
233 233
         }
234
-        if ($typec==1 && $type1 !=2) // cannot use & or | with string/number
234
+        if ($typec == 1 && $type1 != 2) // cannot use & or | with string/number
235 235
         {
236 236
             throw new Exception("Cannot use boolean operators with string & number : ".$rule);
237 237
         }
238 238
         
239
-        $retVal = $this->do_compare($val1, $val2, $comp, $negate);
239
+        $retVal=$this->do_compare($val1, $val2, $comp, $negate);
240 240
         
241
-        if ($item==strlen($rule)) return $retVal; // End of string : return evaluation
241
+        if ($item == strlen($rule)) return $retVal; // End of string : return evaluation
242 242
         // check for logical operator :
243 243
         switch ($rule[$item])
244 244
         {
245
-            case '|':	$item++; return ($retVal || $this->evaluation($rule,$item) );
246
-            case '&':	$item++; return ($retVal && $this->evaluation($rule,$item) );
245
+            case '|':	$item++; return ($retVal || $this->evaluation($rule, $item));
246
+            case '&':	$item++; return ($retVal && $this->evaluation($rule, $item));
247 247
             
248 248
             default:  throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]);
249 249
         }
@@ -256,17 +256,17 @@  discard block
 block discarded – undo
256 256
         $rule2='';
257 257
         while ($item < strlen($rule))
258 258
         {
259
-            if ($rule[$item]==' ') { $item++; continue; }
260
-            if ($rule[$item]=='"')
259
+            if ($rule[$item] == ' ') { $item++; continue; }
260
+            if ($rule[$item] == '"')
261 261
             {
262 262
                 $rule2.=$rule[$item];
263 263
                 $item++;
264
-                while (($item < strlen($rule)) && ($rule[$item]!='"') )
264
+                while (($item < strlen($rule)) && ($rule[$item] != '"'))
265 265
                 {
266 266
                     $rule2.=$rule[$item];
267 267
                     $item++;
268 268
                 }
269
-                if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item);
269
+                if ($item == strlen($rule)) throw new Exception("closing '\"' not found in ".$rule." at ".$item);
270 270
                 $rule2.=$rule[$item];
271 271
                 $item++;
272 272
                 continue;
@@ -285,14 +285,14 @@  discard block
 block discarded – undo
285 285
      *	@return bool : true : rule match, false : rule don't match , throw exception on error.
286 286
      */
287 287
     
288
-    public function eval_rule($rule,$oidList)
288
+    public function eval_rule($rule, $oidList)
289 289
     {
290
-        if ($rule==null || $rule == '') // Empty rule is always true
290
+        if ($rule == null || $rule == '') // Empty rule is always true
291 291
         {
292 292
             return true;
293 293
         }
294 294
         $matches=array();
295
-        while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1)
295
+        while (preg_match('/_OID\(([0-9\.\*]+)\)/', $rule, $matches) == 1)
296 296
         {
297 297
             $oid=$matches[1];
298 298
             $found=0;
@@ -304,38 +304,38 @@  discard block
 block discarded – undo
304 304
             // replace * with \* in oid for preg_replace
305 305
             $oid=preg_replace('/\*/', '\*', $oid);
306 306
             
307
-            $this->logging->log('OID in rule : '.$oid.' / '.$oidR,DEBUG );
307
+            $this->logging->log('OID in rule : '.$oid.' / '.$oidR, DEBUG);
308 308
             
309
-            foreach($oidList as $val)
309
+            foreach ($oidList as $val)
310 310
             {
311
-                if (preg_match("/^$oidR$/",$val->oid) == 1)
311
+                if (preg_match("/^$oidR$/", $val->oid) == 1)
312 312
                 {
313
-                    if (!preg_match('/^[0-9]*\.?[0-9]+$/',$val->value))
313
+                    if (!preg_match('/^[0-9]*\.?[0-9]+$/', $val->value))
314 314
                     { // If not a number, change " to ' and put " around it
315
-                        $val->value=preg_replace('/"/',"'",$val->value);
315
+                        $val->value=preg_replace('/"/', "'", $val->value);
316 316
                         $val->value='"'.$val->value.'"';
317 317
                     }
318 318
                     $rep=0;
319
-                    $rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep);
320
-                    if ($rep==0)
319
+                    $rule=preg_replace('/_OID\('.$oid.'\)/', $val->value, $rule, -1, $rep);
320
+                    if ($rep == 0)
321 321
                     {
322
-                        $this->logging->log("Error in rule_eval",WARN,'');
322
+                        $this->logging->log("Error in rule_eval", WARN, '');
323 323
                         return false;
324 324
                     }
325 325
                     $found=1;
326 326
                     break;
327 327
                 }
328 328
             }
329
-            if ($found==0)
329
+            if ($found == 0)
330 330
             {	// OID not found : throw error
331 331
                 throw new Exception('OID '.$oid.' not found in trap');
332 332
             }
333 333
         }
334 334
         $item=0;
335 335
         $rule=$this->eval_cleanup($rule);
336
-        $this->logging->log('Rule after clenup: '.$rule,INFO );
336
+        $this->logging->log('Rule after clenup: '.$rule, INFO);
337 337
         
338
-        return  $this->evaluation($rule,$item);
338
+        return  $this->evaluation($rule, $item);
339 339
     }
340 340
     
341 341
 }
342 342
\ No newline at end of file
Please login to merge, or discard this patch.