Passed
Push — master ( 782f23...8a769b )
by Patrick
02:09
created
library/Trapdirector/TrapsProcess/Rule.php 1 patch
Spacing   +70 added lines, -70 removed lines patch added patch discarded remove patch
@@ -26,14 +26,14 @@  discard block
 block discarded – undo
26 26
     }
27 27
     
28 28
     
29
-    protected function eval_getElement($rule,&$item)
29
+    protected function eval_getElement($rule, &$item)
30 30
     {
31 31
         if ($item >= strlen($rule))
32 32
         {
33
-            throw new Exception("Early end of string ".$rule ." at " .$item );
33
+            throw new Exception("Early end of string ".$rule." at ".$item);
34 34
         }
35
-        while ($rule[$item]==' ') $item++;
36
-        if (preg_match('/[\-0-9\.]/',$rule[$item]))
35
+        while ($rule[$item] == ' ') $item++;
36
+        if (preg_match('/[\-0-9\.]/', $rule[$item]))
37 37
         { // number
38 38
             return $this->get_number($rule, $item);
39 39
         }
@@ -50,54 +50,54 @@  discard block
 block discarded – undo
50 50
         { // function
51 51
             return $this->get_function($rule, $item);
52 52
         }
53
-        throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]);
53
+        throw new Exception("number/string not found in ".$rule." at ".$item.' : '.$rule[$item]);
54 54
         
55 55
     }
56 56
     
57
-    protected function eval_getNext($rule,$item,$tok)
57
+    protected function eval_getNext($rule, $item, $tok)
58 58
     {
59 59
         while (
60
-            ($rule[$item] != $tok ) 
60
+            ($rule[$item] != $tok) 
61 61
             && ($item < strlen($rule))) 
62 62
         { 
63 63
             $item++;
64 64
         }
65
-        if ($item==strlen($rule)) {
66
-            throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item);
65
+        if ($item == strlen($rule)) {
66
+            throw new Exception("closing '".$tok."' not found in ".$rule." at ".$item);
67 67
         }
68
-        return $item+1;
68
+        return $item + 1;
69 69
     }
70 70
     
71
-    protected function eval_getOper($rule,&$item)
71
+    protected function eval_getOper($rule, &$item)
72 72
     {
73
-        while ($rule[$item]==' ') $item++;
73
+        while ($rule[$item] == ' ') $item++;
74 74
         switch ($rule[$item])
75 75
         {
76 76
             case '<':
77
-                if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");}
78
-                $item++; return array(0,"<");
77
+                if ($rule[$item + 1] == '=') { $item+=2; return array(0, "<="); }
78
+                $item++; return array(0, "<");
79 79
             case '>':
80
-                if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");}
81
-                $item++; return array(0,">");
80
+                if ($rule[$item + 1] == '=') { $item+=2; return array(0, ">="); }
81
+                $item++; return array(0, ">");
82 82
             case '=':
83
-                $item++; return array(0,"=");
83
+                $item++; return array(0, "=");
84 84
             case '!':
85
-                if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");}
86
-                throw new Exception("Erreur in expr - incorrect operator '!'  found in ".$rule ." at " .$item);
85
+                if ($rule[$item + 1] == '=') { $item+=2; return array(0, "!="); }
86
+                throw new Exception("Erreur in expr - incorrect operator '!'  found in ".$rule." at ".$item);
87 87
             case '~':
88
-                $item++; return array(0,"~");
88
+                $item++; return array(0, "~");
89 89
             case '|':
90
-                $item++; return array(1,"|");
90
+                $item++; return array(1, "|");
91 91
             case '&':
92
-                $item++; return array(1,"&");
92
+                $item++; return array(1, "&");
93 93
             default	:
94
-                throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item);
94
+                throw new Exception("Erreur in expr - operator not found in ".$rule." at ".$item);
95 95
         }
96 96
     }
97 97
     
98
-    private function check_negate_first($rule,&$item)
98
+    private function check_negate_first($rule, &$item)
99 99
     {
100
-        if ( $rule[$item] == '!') // If '!' found, negate next expression.
100
+        if ($rule[$item] == '!') // If '!' found, negate next expression.
101 101
         {
102 102
             $item++;
103 103
             return true;
@@ -108,21 +108,21 @@  discard block
 block discarded – undo
108 108
         }
109 109
     }
110 110
 
111
-    private function do_compare($val1,$val2,$comp,$negate)
111
+    private function do_compare($val1, $val2, $comp, $negate)
112 112
     {
113
-        switch ($comp){
114
-            case '<':	$retVal= ($val1 < $val2); break;
115
-            case '<=':	$retVal= ($val1 <= $val2); break;
116
-            case '>':	$retVal= ($val1 > $val2); break;
117
-            case '>=':	$retVal= ($val1 >= $val2); break;
118
-            case '=':	$retVal= ($val1 == $val2); break;
119
-            case '!=':	$retVal= ($val1 != $val2); break;
120
-            case '~':	$retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break;
121
-            case '|':	$retVal= ($val1 || $val2); break;
122
-            case '&':	$retVal= ($val1 && $val2); break;
113
+        switch ($comp) {
114
+            case '<':	$retVal=($val1 < $val2); break;
115
+            case '<=':	$retVal=($val1 <= $val2); break;
116
+            case '>':	$retVal=($val1 > $val2); break;
117
+            case '>=':	$retVal=($val1 >= $val2); break;
118
+            case '=':	$retVal=($val1 == $val2); break;
119
+            case '!=':	$retVal=($val1 != $val2); break;
120
+            case '~':	$retVal=(preg_match('/'.preg_replace('/"/', '', $val2).'/', $val1)); break;
121
+            case '|':	$retVal=($val1 || $val2); break;
122
+            case '&':	$retVal=($val1 && $val2); break;
123 123
             default:  throw new Exception("Error in expression - unknown comp : ".$comp);
124 124
         }
125
-        if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression
125
+        if ($negate === true) $retVal=!$retVal; // Inverse result if negate before expression
126 126
         
127 127
         return $retVal;
128 128
     }
@@ -135,56 +135,56 @@  discard block
 block discarded – undo
135 135
      *   comparison int vs strings will return null (error)
136 136
      *	return : bool or null on error
137 137
      */
138
-    public function evaluation($rule,&$item)
138
+    public function evaluation($rule, &$item)
139 139
     {
140 140
         //echo "Evaluation of ".substr($rule,$item)."\n";
141 141
         $negate=$this->check_negate_first($rule, $item);
142 142
         // First element : number, string or ()
143
-        list($type1,$val1) = $this->eval_getElement($rule,$item);
143
+        list($type1, $val1)=$this->eval_getElement($rule, $item);
144 144
         //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n";
145 145
         
146
-        if ($item==strlen($rule)) // If only element, return value, but only boolean
146
+        if ($item == strlen($rule)) // If only element, return value, but only boolean
147 147
         {
148 148
             if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule);
149
-            if ($negate === true) $val1= ! $val1;
149
+            if ($negate === true) $val1=!$val1;
150 150
             return $val1;
151 151
         }
152 152
         
153 153
         // Second element : operator
154
-        list($typec,$comp) = $this->eval_getOper($rule,$item);
154
+        list($typec, $comp)=$this->eval_getOper($rule, $item);
155 155
         //echo "Comp : ".$comp." : ".substr($rule,$item)."\n";
156 156
         
157 157
         // Third element : number, string or ()
158
-        if ( $rule[$item] == '!') // starts with a ! so evaluate whats next
158
+        if ($rule[$item] == '!') // starts with a ! so evaluate whats next
159 159
         {
160 160
             $item++;
161 161
             if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule);
162
-            $val2= ! $this->evaluation($rule,$item);
162
+            $val2=!$this->evaluation($rule, $item);
163 163
             $type2=2; // result is a boolean
164 164
         }
165 165
         else
166 166
         {
167
-            list($type2,$val2) = $this->eval_getElement($rule,$item);
167
+            list($type2, $val2)=$this->eval_getElement($rule, $item);
168 168
         }
169 169
         //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n";
170 170
         
171
-        if ($type1!=$type2)  // cannot compare different types
171
+        if ($type1 != $type2)  // cannot compare different types
172 172
         {
173 173
             throw new Exception("Cannot compare string & number : ".$rule);
174 174
         }
175
-        if ($typec==1 && $type1 !=2) // cannot use & or | with string/number
175
+        if ($typec == 1 && $type1 != 2) // cannot use & or | with string/number
176 176
         {
177 177
             throw new Exception("Cannot use boolean operators with string & number : ".$rule);
178 178
         }
179 179
         
180
-        $retVal = $this->do_compare($val1, $val2, $comp, $negate);
180
+        $retVal=$this->do_compare($val1, $val2, $comp, $negate);
181 181
         
182
-        if ($item==strlen($rule)) return $retVal; // End of string : return evaluation
182
+        if ($item == strlen($rule)) return $retVal; // End of string : return evaluation
183 183
         // check for logical operator :
184 184
         switch ($rule[$item])
185 185
         {
186
-            case '|':	$item++; return ($retVal || $this->evaluation($rule,$item) );
187
-            case '&':	$item++; return ($retVal && $this->evaluation($rule,$item) );
186
+            case '|':	$item++; return ($retVal || $this->evaluation($rule, $item));
187
+            case '&':	$item++; return ($retVal && $this->evaluation($rule, $item));
188 188
             
189 189
             default:  throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]);
190 190
         }
@@ -197,17 +197,17 @@  discard block
 block discarded – undo
197 197
         $rule2='';
198 198
         while ($item < strlen($rule))
199 199
         {
200
-            if ($rule[$item]==' ') { $item++; continue; }
201
-            if ($rule[$item]=='"')
200
+            if ($rule[$item] == ' ') { $item++; continue; }
201
+            if ($rule[$item] == '"')
202 202
             {
203 203
                 $rule2.=$rule[$item];
204 204
                 $item++;
205
-                while (($item < strlen($rule)) && ($rule[$item]!='"') )
205
+                while (($item < strlen($rule)) && ($rule[$item] != '"'))
206 206
                 {
207 207
                     $rule2.=$rule[$item];
208 208
                     $item++;
209 209
                 }
210
-                if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item);
210
+                if ($item == strlen($rule)) throw new Exception("closing '\"' not found in ".$rule." at ".$item);
211 211
                 $rule2.=$rule[$item];
212 212
                 $item++;
213 213
                 continue;
@@ -236,7 +236,7 @@  discard block
 block discarded – undo
236 236
         // replace * with \* in oid for preg_replace
237 237
         $oid=preg_replace('/\*/', '\*', $oid);
238 238
         
239
-        $this->logging->log('Regexp eval : '.$oid.' / '.$oidR,DEBUG );
239
+        $this->logging->log('Regexp eval : '.$oid.' / '.$oidR, DEBUG);
240 240
         
241 241
         return $oidR;
242 242
     }
@@ -247,50 +247,50 @@  discard block
 block discarded – undo
247 247
      *  @param array $oidList : OIDs values to sustitute.
248 248
      *	@return bool : true : rule match, false : rule don't match , throw exception on error.
249 249
      */   
250
-    public function eval_rule($rule,$oidList)
250
+    public function eval_rule($rule, $oidList)
251 251
     {
252
-        if ($rule==null || $rule == '') // Empty rule is always true
252
+        if ($rule == null || $rule == '') // Empty rule is always true
253 253
         {
254 254
             return true;
255 255
         }
256 256
         $matches=array();
257
-        while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1)
257
+        while (preg_match('/_OID\(([0-9\.\*]+)\)/', $rule, $matches) == 1)
258 258
         {
259 259
             $oid=$matches[1];
260 260
             $found=0;
261 261
             // Test and transform regexp
262
-            $oidR = $this->regexp_eval($oid);
262
+            $oidR=$this->regexp_eval($oid);
263 263
             
264
-            foreach($oidList as $val)
264
+            foreach ($oidList as $val)
265 265
             {
266
-                if (preg_match("/^$oidR$/",$val->oid) == 1)
266
+                if (preg_match("/^$oidR$/", $val->oid) == 1)
267 267
                 {
268
-                    if (!preg_match('/^-?[0-9]*\.?[0-9]+$/',$val->value))
268
+                    if (!preg_match('/^-?[0-9]*\.?[0-9]+$/', $val->value))
269 269
                     { // If not a number, change " to ' and put " around it
270
-                        $val->value=preg_replace('/"/',"'",$val->value);
270
+                        $val->value=preg_replace('/"/', "'", $val->value);
271 271
                         $val->value='"'.$val->value.'"';
272 272
                     }
273 273
                     $rep=0;
274
-                    $rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep);
275
-                    if ($rep==0)
274
+                    $rule=preg_replace('/_OID\('.$oid.'\)/', $val->value, $rule, -1, $rep);
275
+                    if ($rep == 0)
276 276
                     {
277
-                        $this->logging->log("Error in rule_eval",WARN,'');
277
+                        $this->logging->log("Error in rule_eval", WARN, '');
278 278
                         return false;
279 279
                     }
280 280
                     $found=1;
281 281
                     break;
282 282
                 }
283 283
             }
284
-            if ($found==0)
284
+            if ($found == 0)
285 285
             {	// OID not found : throw error
286 286
                 throw new Exception('OID '.$oid.' not found in trap');
287 287
             }
288 288
         }
289 289
         $item=0;
290 290
         $rule=$this->eval_cleanup($rule);
291
-        $this->logging->log('Rule after clenup: '.$rule,INFO );
291
+        $this->logging->log('Rule after clenup: '.$rule, INFO);
292 292
         
293
-        return  $this->evaluation($rule,$item);
293
+        return  $this->evaluation($rule, $item);
294 294
     }
295 295
     
296 296
 }
297 297
\ No newline at end of file
Please login to merge, or discard this patch.
library/Trapdirector/TrapsProcess/RuleUtils.php 1 patch
Spacing   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -18,20 +18,20 @@  discard block
 block discarded – undo
18 18
      * @param int item current eval position
19 19
      * @return array<int,string>
20 20
      */
21
-    private function get_number(string $rule,int &$item)
21
+    private function get_number(string $rule, int &$item)
22 22
     {
23
-        $item2=$item+1;
23
+        $item2=$item + 1;
24 24
         while (
25
-            ($item2!=strlen($rule))
26
-            && (preg_match('/[\-0-9\.]/',$rule[$item2])))
25
+            ($item2 != strlen($rule))
26
+            && (preg_match('/[\-0-9\.]/', $rule[$item2])))
27 27
         {
28
-            $item2++ ;
28
+            $item2++;
29 29
         }
30
-        $val=substr($rule,$item,$item2-$item);
30
+        $val=substr($rule, $item, $item2 - $item);
31 31
         $item=$item2;
32 32
         //echo "number ".$val."\n";
33 33
         
34
-        return array(0,$val);
34
+        return array(0, $val);
35 35
     }
36 36
 
37 37
     /**
@@ -40,14 +40,14 @@  discard block
 block discarded – undo
40 40
      * @param int item current eval position
41 41
      * @return array<int,string>
42 42
      */
43
-    private function get_string(string $rule,int &$item)
43
+    private function get_string(string $rule, int &$item)
44 44
     {
45 45
         $item++;
46
-        $item2=$this->eval_getNext($rule,$item,'"');
47
-        $val=substr($rule,$item,$item2-$item-1);
46
+        $item2=$this->eval_getNext($rule, $item, '"');
47
+        $val=substr($rule, $item, $item2 - $item - 1);
48 48
         $item=$item2;
49 49
         //echo "string : ".$val."\n";
50
-        return array(1,$val);
50
+        return array(1, $val);
51 51
         
52 52
     }
53 53
     
@@ -59,20 +59,20 @@  discard block
 block discarded – undo
59 59
      * @throws Exception
60 60
      * @return string : everything inside parenthesis
61 61
      */
62
-    private function parse_parenthesis(string $rule,int &$item) : string
62
+    private function parse_parenthesis(string $rule, int &$item) : string
63 63
     {
64 64
         $item++;
65 65
         $start=$item;
66 66
         $parenthesis_count=0;
67 67
         while (($item < strlen($rule)) // Not end of string AND
68
-            && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded ()
68
+            && (($rule[$item] != ')') || $parenthesis_count > 0)) // Closing ')' or embeded ()
69 69
         {
70
-            if ($rule[$item] == '"' )
70
+            if ($rule[$item] == '"')
71 71
             { // pass through string
72 72
                 $item++;
73
-                $item=$this->eval_getNext($rule,$item,'"');
73
+                $item=$this->eval_getNext($rule, $item, '"');
74 74
             }
75
-            else{
75
+            else {
76 76
                 if ($rule[$item] == '(')
77 77
                 {
78 78
                     $parenthesis_count++;
@@ -85,8 +85,8 @@  discard block
 block discarded – undo
85 85
             }
86 86
         }
87 87
         
88
-        if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);}
89
-        $val=substr($rule,$start,$item-$start);
88
+        if ($item == strlen($rule)) {throw new Exception("no closing () in ".$rule." at ".$item); }
89
+        $val=substr($rule, $start, $item - $start);
90 90
         $item++;
91 91
         return $val;
92 92
     }
@@ -97,13 +97,13 @@  discard block
 block discarded – undo
97 97
      * @param int $item
98 98
      * @return array<int,string>
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 109
     /**
@@ -112,25 +112,25 @@  discard block
 block discarded – undo
112 112
      * @throws Exception
113 113
      * @return array<int,string>
114 114
      */
115
-    private function get_function(string $rule,int &$item) : array
115
+    private function get_function(string $rule, int &$item) : array
116 116
     {
117 117
         // function is : __function(param1,param2...)
118 118
         $start=$item;
119
-        while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '('
119
+        while (($item < strlen($rule)) && ($rule[$item] != '(')) // Not end of string AND not opening '('
120 120
         {
121 121
             $item++;
122 122
         }
123
-        if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);}
123
+        if ($item == strlen($rule)) {throw new Exception("no opening () for function in ".$rule." at ".$item); }
124 124
         
125 125
         // get parameters between parenthesis
126 126
         
127 127
         $this->parse_parenthesis($rule, $item);
128 128
         
129
-        $val=substr($rule,$start,$item-$start);
129
+        $val=substr($rule, $start, $item - $start);
130 130
         
131
-        $this->logging->log('got function ' . $val,DEBUG);
131
+        $this->logging->log('got function '.$val, DEBUG);
132 132
         
133
-        return array(2,$this->trapClass->pluginClass->evaluateFunctionString($val));
133
+        return array(2, $this->trapClass->pluginClass->evaluateFunctionString($val));
134 134
         
135 135
     }
136 136
     
Please login to merge, or discard this patch.