@@ -12,204 +12,204 @@ |
||
12 | 12 | trait RuleUtils |
13 | 13 | { |
14 | 14 | |
15 | - /** rule eval starting from $token |
|
16 | - * @param string $rule |
|
17 | - * @param int $item |
|
18 | - */ |
|
19 | - abstract public function evaluation($rule,&$item); |
|
15 | + /** rule eval starting from $token |
|
16 | + * @param string $rule |
|
17 | + * @param int $item |
|
18 | + */ |
|
19 | + abstract public function evaluation($rule,&$item); |
|
20 | 20 | |
21 | - /** |
|
22 | - * Get full number |
|
23 | - * @param string $rule Rule as string |
|
24 | - * @param int $item current eval position |
|
25 | - * @return array<int,string> |
|
26 | - */ |
|
27 | - private function get_number(string $rule,int &$item) |
|
28 | - { |
|
29 | - $item2=$item+1; |
|
30 | - while ( |
|
31 | - ($item2!=strlen($rule)) |
|
32 | - && (preg_match('/[\-0-9\.]/',$rule[$item2]))) |
|
33 | - { |
|
34 | - $item2++ ; |
|
35 | - } |
|
36 | - $val=substr($rule,$item,$item2-$item); |
|
37 | - $item=$item2; |
|
38 | - //echo "number ".$val."\n"; |
|
21 | + /** |
|
22 | + * Get full number |
|
23 | + * @param string $rule Rule as string |
|
24 | + * @param int $item current eval position |
|
25 | + * @return array<int,string> |
|
26 | + */ |
|
27 | + private function get_number(string $rule,int &$item) |
|
28 | + { |
|
29 | + $item2=$item+1; |
|
30 | + while ( |
|
31 | + ($item2!=strlen($rule)) |
|
32 | + && (preg_match('/[\-0-9\.]/',$rule[$item2]))) |
|
33 | + { |
|
34 | + $item2++ ; |
|
35 | + } |
|
36 | + $val=substr($rule,$item,$item2-$item); |
|
37 | + $item=$item2; |
|
38 | + //echo "number ".$val."\n"; |
|
39 | 39 | |
40 | - return array(0,$val); |
|
41 | - } |
|
40 | + return array(0,$val); |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Get a string (between ") |
|
45 | - * @param string $rule Rule as string |
|
46 | - * @param int $item current eval position |
|
47 | - * @return array<int,string> |
|
48 | - */ |
|
49 | - private function get_string(string $rule,int &$item) |
|
50 | - { |
|
51 | - $item++; |
|
52 | - $item2=$this->eval_getNext($rule,$item,'"'); |
|
53 | - $val=substr($rule,$item,$item2-$item-1); |
|
54 | - $item=$item2; |
|
55 | - //echo "string : ".$val."\n"; |
|
56 | - return array(1,$val); |
|
43 | + /** |
|
44 | + * Get a string (between ") |
|
45 | + * @param string $rule Rule as string |
|
46 | + * @param int $item current eval position |
|
47 | + * @return array<int,string> |
|
48 | + */ |
|
49 | + private function get_string(string $rule,int &$item) |
|
50 | + { |
|
51 | + $item++; |
|
52 | + $item2=$this->eval_getNext($rule,$item,'"'); |
|
53 | + $val=substr($rule,$item,$item2-$item-1); |
|
54 | + $item=$item2; |
|
55 | + //echo "string : ".$val."\n"; |
|
56 | + return array(1,$val); |
|
57 | 57 | |
58 | - } |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Parse elements inside () : jumps over "" and count parenthesis. |
|
62 | - * Ex : ( "test" != ")test" & (1==2) ) will return "test" != ")test" & (1==2) |
|
63 | - * @param string $rule : the current rule |
|
64 | - * @param int $item : actual position in rule |
|
65 | - * @throws Exception |
|
66 | - * @return string : everything inside parenthesis |
|
67 | - */ |
|
68 | - private function parse_parenthesis(string $rule,int &$item) : string |
|
69 | - { |
|
70 | - $item++; |
|
71 | - $start=$item; |
|
72 | - $parenthesisCount=0; |
|
73 | - while (($item < strlen($rule)) // Not end of string AND |
|
74 | - && ( ($rule[$item] != ')' ) || $parenthesisCount > 0) ) // Closing ')' or embeded () |
|
75 | - { |
|
76 | - if ($rule[$item] == '"' ) |
|
77 | - { // pass through string |
|
78 | - $item++; |
|
79 | - $item=$this->eval_getNext($rule,$item,'"'); |
|
80 | - continue; |
|
81 | - } |
|
82 | - if ($rule[$item] == '(') |
|
83 | - { |
|
84 | - $parenthesisCount++; |
|
85 | - } |
|
86 | - if ($rule[$item] == ')') |
|
87 | - { |
|
88 | - $parenthesisCount--; |
|
89 | - } |
|
90 | - $item++; |
|
91 | - } |
|
60 | + /** |
|
61 | + * Parse elements inside () : jumps over "" and count parenthesis. |
|
62 | + * Ex : ( "test" != ")test" & (1==2) ) will return "test" != ")test" & (1==2) |
|
63 | + * @param string $rule : the current rule |
|
64 | + * @param int $item : actual position in rule |
|
65 | + * @throws Exception |
|
66 | + * @return string : everything inside parenthesis |
|
67 | + */ |
|
68 | + private function parse_parenthesis(string $rule,int &$item) : string |
|
69 | + { |
|
70 | + $item++; |
|
71 | + $start=$item; |
|
72 | + $parenthesisCount=0; |
|
73 | + while (($item < strlen($rule)) // Not end of string AND |
|
74 | + && ( ($rule[$item] != ')' ) || $parenthesisCount > 0) ) // Closing ')' or embeded () |
|
75 | + { |
|
76 | + if ($rule[$item] == '"' ) |
|
77 | + { // pass through string |
|
78 | + $item++; |
|
79 | + $item=$this->eval_getNext($rule,$item,'"'); |
|
80 | + continue; |
|
81 | + } |
|
82 | + if ($rule[$item] == '(') |
|
83 | + { |
|
84 | + $parenthesisCount++; |
|
85 | + } |
|
86 | + if ($rule[$item] == ')') |
|
87 | + { |
|
88 | + $parenthesisCount--; |
|
89 | + } |
|
90 | + $item++; |
|
91 | + } |
|
92 | 92 | |
93 | - if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} |
|
94 | - $val=substr($rule,$start,$item-$start); |
|
95 | - $item++; |
|
96 | - return $val; |
|
97 | - } |
|
93 | + if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} |
|
94 | + $val=substr($rule,$start,$item-$start); |
|
95 | + $item++; |
|
96 | + return $val; |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * Get and eval a grouped condition - ex : (1==1) |
|
101 | - * @param string $rule |
|
102 | - * @param int $item |
|
103 | - * @return array<int,string> |
|
104 | - */ |
|
105 | - private function get_group(string $rule,int &$item) : array |
|
106 | - { |
|
107 | - // gets eveything inside parenthesis |
|
108 | - $val=$this->parse_parenthesis($rule, $item); |
|
109 | - // Returns boolean with evaluation of all inside parenthesis |
|
110 | - $start=0; |
|
111 | - return array(2,$this->evaluation($val,$start)); |
|
112 | - } |
|
99 | + /** |
|
100 | + * Get and eval a grouped condition - ex : (1==1) |
|
101 | + * @param string $rule |
|
102 | + * @param int $item |
|
103 | + * @return array<int,string> |
|
104 | + */ |
|
105 | + private function get_group(string $rule,int &$item) : array |
|
106 | + { |
|
107 | + // gets eveything inside parenthesis |
|
108 | + $val=$this->parse_parenthesis($rule, $item); |
|
109 | + // Returns boolean with evaluation of all inside parenthesis |
|
110 | + $start=0; |
|
111 | + return array(2,$this->evaluation($val,$start)); |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * @param string $rule |
|
116 | - * @param int $item |
|
117 | - * @throws Exception |
|
118 | - * @return array<int,string> |
|
119 | - */ |
|
120 | - private function get_function(string $rule,int &$item) : array |
|
121 | - { |
|
122 | - // function is : __function(param1,param2...) |
|
123 | - $start=$item; |
|
124 | - while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' |
|
125 | - { |
|
126 | - $item++; |
|
127 | - } |
|
128 | - if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} |
|
114 | + /** |
|
115 | + * @param string $rule |
|
116 | + * @param int $item |
|
117 | + * @throws Exception |
|
118 | + * @return array<int,string> |
|
119 | + */ |
|
120 | + private function get_function(string $rule,int &$item) : array |
|
121 | + { |
|
122 | + // function is : __function(param1,param2...) |
|
123 | + $start=$item; |
|
124 | + while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' |
|
125 | + { |
|
126 | + $item++; |
|
127 | + } |
|
128 | + if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} |
|
129 | 129 | |
130 | - // get parameters between parenthesis |
|
130 | + // get parameters between parenthesis |
|
131 | 131 | |
132 | - $this->parse_parenthesis($rule, $item); |
|
132 | + $this->parse_parenthesis($rule, $item); |
|
133 | 133 | |
134 | - $val=substr($rule,$start,$item-$start); |
|
134 | + $val=substr($rule,$start,$item-$start); |
|
135 | 135 | |
136 | - $this->logging->log('got function ' . $val,DEBUG); |
|
136 | + $this->logging->log('got function ' . $val,DEBUG); |
|
137 | 137 | |
138 | - return array(2,$this->trapClass->pluginClass->evaluateFunctionString($val)); |
|
138 | + return array(2,$this->trapClass->pluginClass->evaluateFunctionString($val)); |
|
139 | 139 | |
140 | - } |
|
140 | + } |
|
141 | 141 | |
142 | - /** Find next token $tok in $rule starting at $item |
|
143 | - * @param string $rule |
|
144 | - * @param int $item |
|
145 | - * @param string $tok : token to search for |
|
146 | - * @throws Exception |
|
147 | - * @return array<int,string> |
|
148 | - */ |
|
149 | - protected function eval_getNext(string $rule,int $item,string $tok) |
|
150 | - { |
|
151 | - while ( |
|
152 | - ($rule[$item] != $tok ) |
|
153 | - && ($item < strlen($rule))) |
|
154 | - { |
|
155 | - $item++; |
|
156 | - } |
|
157 | - if ($item==strlen($rule)) { |
|
158 | - throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
159 | - } |
|
160 | - return $item+1; |
|
161 | - } |
|
142 | + /** Find next token $tok in $rule starting at $item |
|
143 | + * @param string $rule |
|
144 | + * @param int $item |
|
145 | + * @param string $tok : token to search for |
|
146 | + * @throws Exception |
|
147 | + * @return array<int,string> |
|
148 | + */ |
|
149 | + protected function eval_getNext(string $rule,int $item,string $tok) |
|
150 | + { |
|
151 | + while ( |
|
152 | + ($rule[$item] != $tok ) |
|
153 | + && ($item < strlen($rule))) |
|
154 | + { |
|
155 | + $item++; |
|
156 | + } |
|
157 | + if ($item==strlen($rule)) { |
|
158 | + throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
159 | + } |
|
160 | + return $item+1; |
|
161 | + } |
|
162 | 162 | |
163 | - /** get negate (!) and return true if found - and pass it with item++ - |
|
164 | - * @param string $rule |
|
165 | - * @param int $item |
|
166 | - * @return boolean |
|
167 | - */ |
|
168 | - private function check_negate_first(string $rule,int &$item) |
|
169 | - { |
|
170 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
171 | - { |
|
172 | - $item++; |
|
173 | - return true; |
|
174 | - } |
|
175 | - else |
|
176 | - { |
|
177 | - return false; |
|
178 | - } |
|
179 | - } |
|
163 | + /** get negate (!) and return true if found - and pass it with item++ - |
|
164 | + * @param string $rule |
|
165 | + * @param int $item |
|
166 | + * @return boolean |
|
167 | + */ |
|
168 | + private function check_negate_first(string $rule,int &$item) |
|
169 | + { |
|
170 | + if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
171 | + { |
|
172 | + $item++; |
|
173 | + return true; |
|
174 | + } |
|
175 | + else |
|
176 | + { |
|
177 | + return false; |
|
178 | + } |
|
179 | + } |
|
180 | 180 | |
181 | - /** Remove all whitespaces (when not quoted) |
|
182 | - * @param string $rule |
|
183 | - * @throws Exception |
|
184 | - * @return string |
|
185 | - */ |
|
186 | - public function eval_cleanup($rule) |
|
187 | - { |
|
188 | - $item=0; |
|
189 | - $rule2=''; |
|
190 | - while ($item < strlen($rule)) |
|
191 | - { |
|
192 | - if ($rule[$item]==' ') { $item++; continue; } |
|
193 | - if ($rule[$item]=='"') |
|
194 | - { |
|
195 | - $rule2.=$rule[$item]; |
|
196 | - $item++; |
|
197 | - while (($item < strlen($rule)) && ($rule[$item]!='"') ) |
|
198 | - { |
|
199 | - $rule2.=$rule[$item]; |
|
200 | - $item++; |
|
201 | - } |
|
202 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
203 | - $rule2.=$rule[$item]; |
|
204 | - $item++; |
|
205 | - continue; |
|
206 | - } |
|
181 | + /** Remove all whitespaces (when not quoted) |
|
182 | + * @param string $rule |
|
183 | + * @throws Exception |
|
184 | + * @return string |
|
185 | + */ |
|
186 | + public function eval_cleanup($rule) |
|
187 | + { |
|
188 | + $item=0; |
|
189 | + $rule2=''; |
|
190 | + while ($item < strlen($rule)) |
|
191 | + { |
|
192 | + if ($rule[$item]==' ') { $item++; continue; } |
|
193 | + if ($rule[$item]=='"') |
|
194 | + { |
|
195 | + $rule2.=$rule[$item]; |
|
196 | + $item++; |
|
197 | + while (($item < strlen($rule)) && ($rule[$item]!='"') ) |
|
198 | + { |
|
199 | + $rule2.=$rule[$item]; |
|
200 | + $item++; |
|
201 | + } |
|
202 | + if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
203 | + $rule2.=$rule[$item]; |
|
204 | + $item++; |
|
205 | + continue; |
|
206 | + } |
|
207 | 207 | |
208 | - $rule2.=$rule[$item]; |
|
209 | - $item++; |
|
210 | - } |
|
208 | + $rule2.=$rule[$item]; |
|
209 | + $item++; |
|
210 | + } |
|
211 | 211 | |
212 | - return $rule2; |
|
213 | - } |
|
212 | + return $rule2; |
|
213 | + } |
|
214 | 214 | |
215 | 215 | } |
216 | 216 | \ No newline at end of file |
@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | * @param string $rule |
17 | 17 | * @param int $item |
18 | 18 | */ |
19 | - abstract public function evaluation($rule,&$item); |
|
19 | + abstract public function evaluation($rule, &$item); |
|
20 | 20 | |
21 | 21 | /** |
22 | 22 | * Get full number |
@@ -24,20 +24,20 @@ discard block |
||
24 | 24 | * @param int $item current eval position |
25 | 25 | * @return array<int,string> |
26 | 26 | */ |
27 | - private function get_number(string $rule,int &$item) |
|
27 | + private function get_number(string $rule, int &$item) |
|
28 | 28 | { |
29 | - $item2=$item+1; |
|
29 | + $item2=$item + 1; |
|
30 | 30 | while ( |
31 | - ($item2!=strlen($rule)) |
|
32 | - && (preg_match('/[\-0-9\.]/',$rule[$item2]))) |
|
31 | + ($item2 != strlen($rule)) |
|
32 | + && (preg_match('/[\-0-9\.]/', $rule[$item2]))) |
|
33 | 33 | { |
34 | - $item2++ ; |
|
34 | + $item2++; |
|
35 | 35 | } |
36 | - $val=substr($rule,$item,$item2-$item); |
|
36 | + $val=substr($rule, $item, $item2 - $item); |
|
37 | 37 | $item=$item2; |
38 | 38 | //echo "number ".$val."\n"; |
39 | 39 | |
40 | - return array(0,$val); |
|
40 | + return array(0, $val); |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | /** |
@@ -46,14 +46,14 @@ discard block |
||
46 | 46 | * @param int $item current eval position |
47 | 47 | * @return array<int,string> |
48 | 48 | */ |
49 | - private function get_string(string $rule,int &$item) |
|
49 | + private function get_string(string $rule, int &$item) |
|
50 | 50 | { |
51 | 51 | $item++; |
52 | - $item2=$this->eval_getNext($rule,$item,'"'); |
|
53 | - $val=substr($rule,$item,$item2-$item-1); |
|
52 | + $item2=$this->eval_getNext($rule, $item, '"'); |
|
53 | + $val=substr($rule, $item, $item2 - $item - 1); |
|
54 | 54 | $item=$item2; |
55 | 55 | //echo "string : ".$val."\n"; |
56 | - return array(1,$val); |
|
56 | + return array(1, $val); |
|
57 | 57 | |
58 | 58 | } |
59 | 59 | |
@@ -65,18 +65,18 @@ discard block |
||
65 | 65 | * @throws Exception |
66 | 66 | * @return string : everything inside parenthesis |
67 | 67 | */ |
68 | - private function parse_parenthesis(string $rule,int &$item) : string |
|
68 | + private function parse_parenthesis(string $rule, int &$item) : string |
|
69 | 69 | { |
70 | 70 | $item++; |
71 | 71 | $start=$item; |
72 | 72 | $parenthesisCount=0; |
73 | 73 | while (($item < strlen($rule)) // Not end of string AND |
74 | - && ( ($rule[$item] != ')' ) || $parenthesisCount > 0) ) // Closing ')' or embeded () |
|
74 | + && (($rule[$item] != ')') || $parenthesisCount > 0)) // Closing ')' or embeded () |
|
75 | 75 | { |
76 | - if ($rule[$item] == '"' ) |
|
76 | + if ($rule[$item] == '"') |
|
77 | 77 | { // pass through string |
78 | 78 | $item++; |
79 | - $item=$this->eval_getNext($rule,$item,'"'); |
|
79 | + $item=$this->eval_getNext($rule, $item, '"'); |
|
80 | 80 | continue; |
81 | 81 | } |
82 | 82 | if ($rule[$item] == '(') |
@@ -90,8 +90,8 @@ discard block |
||
90 | 90 | $item++; |
91 | 91 | } |
92 | 92 | |
93 | - if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} |
|
94 | - $val=substr($rule,$start,$item-$start); |
|
93 | + if ($item == strlen($rule)) {throw new Exception("no closing () in ".$rule." at ".$item); } |
|
94 | + $val=substr($rule, $start, $item - $start); |
|
95 | 95 | $item++; |
96 | 96 | return $val; |
97 | 97 | } |
@@ -102,13 +102,13 @@ discard block |
||
102 | 102 | * @param int $item |
103 | 103 | * @return array<int,string> |
104 | 104 | */ |
105 | - private function get_group(string $rule,int &$item) : array |
|
105 | + private function get_group(string $rule, int &$item) : array |
|
106 | 106 | { |
107 | 107 | // gets eveything inside parenthesis |
108 | 108 | $val=$this->parse_parenthesis($rule, $item); |
109 | 109 | // Returns boolean with evaluation of all inside parenthesis |
110 | 110 | $start=0; |
111 | - return array(2,$this->evaluation($val,$start)); |
|
111 | + return array(2, $this->evaluation($val, $start)); |
|
112 | 112 | } |
113 | 113 | |
114 | 114 | /** |
@@ -117,25 +117,25 @@ discard block |
||
117 | 117 | * @throws Exception |
118 | 118 | * @return array<int,string> |
119 | 119 | */ |
120 | - private function get_function(string $rule,int &$item) : array |
|
120 | + private function get_function(string $rule, int &$item) : array |
|
121 | 121 | { |
122 | 122 | // function is : __function(param1,param2...) |
123 | 123 | $start=$item; |
124 | - while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' |
|
124 | + while (($item < strlen($rule)) && ($rule[$item] != '(')) // Not end of string AND not opening '(' |
|
125 | 125 | { |
126 | 126 | $item++; |
127 | 127 | } |
128 | - if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} |
|
128 | + if ($item == strlen($rule)) {throw new Exception("no opening () for function in ".$rule." at ".$item); } |
|
129 | 129 | |
130 | 130 | // get parameters between parenthesis |
131 | 131 | |
132 | 132 | $this->parse_parenthesis($rule, $item); |
133 | 133 | |
134 | - $val=substr($rule,$start,$item-$start); |
|
134 | + $val=substr($rule, $start, $item - $start); |
|
135 | 135 | |
136 | - $this->logging->log('got function ' . $val,DEBUG); |
|
136 | + $this->logging->log('got function '.$val, DEBUG); |
|
137 | 137 | |
138 | - return array(2,$this->trapClass->pluginClass->evaluateFunctionString($val)); |
|
138 | + return array(2, $this->trapClass->pluginClass->evaluateFunctionString($val)); |
|
139 | 139 | |
140 | 140 | } |
141 | 141 | |
@@ -146,18 +146,18 @@ discard block |
||
146 | 146 | * @throws Exception |
147 | 147 | * @return array<int,string> |
148 | 148 | */ |
149 | - protected function eval_getNext(string $rule,int $item,string $tok) |
|
149 | + protected function eval_getNext(string $rule, int $item, string $tok) |
|
150 | 150 | { |
151 | 151 | while ( |
152 | - ($rule[$item] != $tok ) |
|
152 | + ($rule[$item] != $tok) |
|
153 | 153 | && ($item < strlen($rule))) |
154 | 154 | { |
155 | 155 | $item++; |
156 | 156 | } |
157 | - if ($item==strlen($rule)) { |
|
158 | - throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
157 | + if ($item == strlen($rule)) { |
|
158 | + throw new Exception("closing '".$tok."' not found in ".$rule." at ".$item); |
|
159 | 159 | } |
160 | - return $item+1; |
|
160 | + return $item + 1; |
|
161 | 161 | } |
162 | 162 | |
163 | 163 | /** get negate (!) and return true if found - and pass it with item++ - |
@@ -165,9 +165,9 @@ discard block |
||
165 | 165 | * @param int $item |
166 | 166 | * @return boolean |
167 | 167 | */ |
168 | - private function check_negate_first(string $rule,int &$item) |
|
168 | + private function check_negate_first(string $rule, int &$item) |
|
169 | 169 | { |
170 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
170 | + if ($rule[$item] == '!') // If '!' found, negate next expression. |
|
171 | 171 | { |
172 | 172 | $item++; |
173 | 173 | return true; |
@@ -189,17 +189,17 @@ discard block |
||
189 | 189 | $rule2=''; |
190 | 190 | while ($item < strlen($rule)) |
191 | 191 | { |
192 | - if ($rule[$item]==' ') { $item++; continue; } |
|
193 | - if ($rule[$item]=='"') |
|
192 | + if ($rule[$item] == ' ') { $item++; continue; } |
|
193 | + if ($rule[$item] == '"') |
|
194 | 194 | { |
195 | 195 | $rule2.=$rule[$item]; |
196 | 196 | $item++; |
197 | - while (($item < strlen($rule)) && ($rule[$item]!='"') ) |
|
197 | + while (($item < strlen($rule)) && ($rule[$item] != '"')) |
|
198 | 198 | { |
199 | 199 | $rule2.=$rule[$item]; |
200 | 200 | $item++; |
201 | 201 | } |
202 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
202 | + if ($item == strlen($rule)) throw new Exception("closing '\"' not found in ".$rule." at ".$item); |
|
203 | 203 | $rule2.=$rule[$item]; |
204 | 204 | $item++; |
205 | 205 | continue; |
@@ -71,11 +71,13 @@ discard block |
||
71 | 71 | $start=$item; |
72 | 72 | $parenthesisCount=0; |
73 | 73 | while (($item < strlen($rule)) // Not end of string AND |
74 | - && ( ($rule[$item] != ')' ) || $parenthesisCount > 0) ) // Closing ')' or embeded () |
|
74 | + && ( ($rule[$item] != ')' ) || $parenthesisCount > 0) ) { |
|
75 | + // Closing ')' or embeded () |
|
75 | 76 | { |
76 | 77 | if ($rule[$item] == '"' ) |
77 | 78 | { // pass through string |
78 | 79 | $item++; |
80 | + } |
|
79 | 81 | $item=$this->eval_getNext($rule,$item,'"'); |
80 | 82 | continue; |
81 | 83 | } |
@@ -121,10 +123,12 @@ discard block |
||
121 | 123 | { |
122 | 124 | // function is : __function(param1,param2...) |
123 | 125 | $start=$item; |
124 | - while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' |
|
126 | + while (($item < strlen($rule)) && ($rule[$item] != '(' )) { |
|
127 | + // Not end of string AND not opening '(' |
|
125 | 128 | { |
126 | 129 | $item++; |
127 | 130 | } |
131 | + } |
|
128 | 132 | if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} |
129 | 133 | |
130 | 134 | // get parameters between parenthesis |
@@ -167,12 +171,13 @@ discard block |
||
167 | 171 | */ |
168 | 172 | private function check_negate_first(string $rule,int &$item) |
169 | 173 | { |
170 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
174 | + if ( $rule[$item] == '!') { |
|
175 | + // If '!' found, negate next expression. |
|
171 | 176 | { |
172 | 177 | $item++; |
173 | - return true; |
|
174 | 178 | } |
175 | - else |
|
179 | + return true; |
|
180 | + } else |
|
176 | 181 | { |
177 | 182 | return false; |
178 | 183 | } |
@@ -199,7 +204,9 @@ discard block |
||
199 | 204 | $rule2.=$rule[$item]; |
200 | 205 | $item++; |
201 | 206 | } |
202 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
207 | + if ($item == strlen ($rule)) { |
|
208 | + throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
209 | + } |
|
203 | 210 | $rule2.=$rule[$item]; |
204 | 211 | $item++; |
205 | 212 | continue; |
@@ -6,233 +6,233 @@ |
||
6 | 6 | |
7 | 7 | class Rule |
8 | 8 | { |
9 | - use \RuleUtils; |
|
9 | + use \RuleUtils; |
|
10 | 10 | |
11 | - /** @var Logging $logging logging class*/ |
|
12 | - protected $logging; |
|
11 | + /** @var Logging $logging logging class*/ |
|
12 | + protected $logging; |
|
13 | 13 | |
14 | - /** @var Trap $trapClass */ |
|
15 | - protected $trapClass; |
|
14 | + /** @var Trap $trapClass */ |
|
15 | + protected $trapClass; |
|
16 | 16 | |
17 | 17 | |
18 | - /** |
|
19 | - * Setup Rule Class |
|
20 | - * @param Trap $trapClass : To get logging class & plugin class |
|
21 | - */ |
|
22 | - function __construct($trapClass) |
|
23 | - { |
|
24 | - $this->trapClass=$trapClass; |
|
25 | - $this->logging=$trapClass->logging; |
|
26 | - } |
|
18 | + /** |
|
19 | + * Setup Rule Class |
|
20 | + * @param Trap $trapClass : To get logging class & plugin class |
|
21 | + */ |
|
22 | + function __construct($trapClass) |
|
23 | + { |
|
24 | + $this->trapClass=$trapClass; |
|
25 | + $this->logging=$trapClass->logging; |
|
26 | + } |
|
27 | 27 | |
28 | 28 | |
29 | - protected function eval_getElement($rule,&$item) |
|
30 | - { |
|
31 | - if ($item >= strlen($rule)) |
|
32 | - { |
|
33 | - throw new Exception("Early end of string ".$rule ." at " .$item ); |
|
34 | - } |
|
35 | - while ($rule[$item]==' ') $item++; |
|
36 | - if (preg_match('/[\-0-9\.]/',$rule[$item])) |
|
37 | - { // number |
|
38 | - return $this->get_number($rule, $item); |
|
39 | - } |
|
40 | - if ($rule[$item] == '"') |
|
41 | - { // string |
|
42 | - return $this->get_string($rule, $item); |
|
43 | - } |
|
29 | + protected function eval_getElement($rule,&$item) |
|
30 | + { |
|
31 | + if ($item >= strlen($rule)) |
|
32 | + { |
|
33 | + throw new Exception("Early end of string ".$rule ." at " .$item ); |
|
34 | + } |
|
35 | + while ($rule[$item]==' ') $item++; |
|
36 | + if (preg_match('/[\-0-9\.]/',$rule[$item])) |
|
37 | + { // number |
|
38 | + return $this->get_number($rule, $item); |
|
39 | + } |
|
40 | + if ($rule[$item] == '"') |
|
41 | + { // string |
|
42 | + return $this->get_string($rule, $item); |
|
43 | + } |
|
44 | 44 | |
45 | - if ($rule[$item] == '(') |
|
46 | - { // grouping |
|
47 | - return $this->get_group($rule, $item); |
|
48 | - } |
|
49 | - if ($rule[$item] == '_') |
|
50 | - { // function |
|
51 | - return $this->get_function($rule, $item); |
|
52 | - } |
|
53 | - throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]); |
|
45 | + if ($rule[$item] == '(') |
|
46 | + { // grouping |
|
47 | + return $this->get_group($rule, $item); |
|
48 | + } |
|
49 | + if ($rule[$item] == '_') |
|
50 | + { // function |
|
51 | + return $this->get_function($rule, $item); |
|
52 | + } |
|
53 | + throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]); |
|
54 | 54 | |
55 | - } |
|
55 | + } |
|
56 | 56 | |
57 | - protected function eval_getOper($rule,&$item) |
|
58 | - { |
|
59 | - while ($rule[$item]==' ') $item++; |
|
60 | - switch ($rule[$item]) |
|
61 | - { |
|
62 | - case '<': |
|
63 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");} |
|
64 | - $item++; return array(0,"<"); |
|
65 | - case '>': |
|
66 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");} |
|
67 | - $item++; return array(0,">"); |
|
68 | - case '=': |
|
69 | - $item++; return array(0,"="); |
|
70 | - case '!': |
|
71 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");} |
|
72 | - throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule ." at " .$item); |
|
73 | - case '~': |
|
74 | - $item++; return array(0,"~"); |
|
75 | - case '|': |
|
76 | - $item++; return array(1,"|"); |
|
77 | - case '&': |
|
78 | - $item++; return array(1,"&"); |
|
79 | - default : |
|
80 | - throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item); |
|
81 | - } |
|
82 | - } |
|
57 | + protected function eval_getOper($rule,&$item) |
|
58 | + { |
|
59 | + while ($rule[$item]==' ') $item++; |
|
60 | + switch ($rule[$item]) |
|
61 | + { |
|
62 | + case '<': |
|
63 | + if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");} |
|
64 | + $item++; return array(0,"<"); |
|
65 | + case '>': |
|
66 | + if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");} |
|
67 | + $item++; return array(0,">"); |
|
68 | + case '=': |
|
69 | + $item++; return array(0,"="); |
|
70 | + case '!': |
|
71 | + if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");} |
|
72 | + throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule ." at " .$item); |
|
73 | + case '~': |
|
74 | + $item++; return array(0,"~"); |
|
75 | + case '|': |
|
76 | + $item++; return array(1,"|"); |
|
77 | + case '&': |
|
78 | + $item++; return array(1,"&"); |
|
79 | + default : |
|
80 | + throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item); |
|
81 | + } |
|
82 | + } |
|
83 | 83 | |
84 | - private function do_compare($val1,$val2,$comp,$negate) |
|
85 | - { |
|
86 | - switch ($comp){ |
|
87 | - case '<': $retVal= ($val1 < $val2); break; |
|
88 | - case '<=': $retVal= ($val1 <= $val2); break; |
|
89 | - case '>': $retVal= ($val1 > $val2); break; |
|
90 | - case '>=': $retVal= ($val1 >= $val2); break; |
|
91 | - case '=': $retVal= ($val1 == $val2); break; |
|
92 | - case '!=': $retVal= ($val1 != $val2); break; |
|
93 | - case '~': $retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break; |
|
94 | - case '|': $retVal= ($val1 || $val2); break; |
|
95 | - case '&': $retVal= ($val1 && $val2); break; |
|
96 | - default: throw new Exception("Error in expression - unknown comp : ".$comp); |
|
97 | - } |
|
98 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
84 | + private function do_compare($val1,$val2,$comp,$negate) |
|
85 | + { |
|
86 | + switch ($comp){ |
|
87 | + case '<': $retVal= ($val1 < $val2); break; |
|
88 | + case '<=': $retVal= ($val1 <= $val2); break; |
|
89 | + case '>': $retVal= ($val1 > $val2); break; |
|
90 | + case '>=': $retVal= ($val1 >= $val2); break; |
|
91 | + case '=': $retVal= ($val1 == $val2); break; |
|
92 | + case '!=': $retVal= ($val1 != $val2); break; |
|
93 | + case '~': $retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break; |
|
94 | + case '|': $retVal= ($val1 || $val2); break; |
|
95 | + case '&': $retVal= ($val1 && $val2); break; |
|
96 | + default: throw new Exception("Error in expression - unknown comp : ".$comp); |
|
97 | + } |
|
98 | + if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
99 | 99 | |
100 | - return $retVal; |
|
101 | - } |
|
100 | + return $retVal; |
|
101 | + } |
|
102 | 102 | |
103 | - /** Evaluation : makes token and evaluate. |
|
104 | - * Public function for expressions testing |
|
105 | - * accepts : < > = <= >= != (typec = 0) |
|
106 | - * operators : & | (typec=1) |
|
107 | - * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
|
108 | - * comparison int vs strings will return null (error) |
|
109 | - * return : bool or null on error |
|
110 | - */ |
|
111 | - public function evaluation($rule,&$item) |
|
112 | - { |
|
113 | - //echo "Evaluation of ".substr($rule,$item)."\n"; |
|
114 | - $negate=$this->check_negate_first($rule, $item); |
|
115 | - // First element : number, string or () |
|
116 | - list($type1,$val1) = $this->eval_getElement($rule,$item); |
|
117 | - //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
|
103 | + /** Evaluation : makes token and evaluate. |
|
104 | + * Public function for expressions testing |
|
105 | + * accepts : < > = <= >= != (typec = 0) |
|
106 | + * operators : & | (typec=1) |
|
107 | + * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
|
108 | + * comparison int vs strings will return null (error) |
|
109 | + * return : bool or null on error |
|
110 | + */ |
|
111 | + public function evaluation($rule,&$item) |
|
112 | + { |
|
113 | + //echo "Evaluation of ".substr($rule,$item)."\n"; |
|
114 | + $negate=$this->check_negate_first($rule, $item); |
|
115 | + // First element : number, string or () |
|
116 | + list($type1,$val1) = $this->eval_getElement($rule,$item); |
|
117 | + //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
|
118 | 118 | |
119 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
120 | - { |
|
121 | - if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
|
122 | - if ($negate === true) $val1= ! $val1; |
|
123 | - return $val1; |
|
124 | - } |
|
119 | + if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
120 | + { |
|
121 | + if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
|
122 | + if ($negate === true) $val1= ! $val1; |
|
123 | + return $val1; |
|
124 | + } |
|
125 | 125 | |
126 | - // Second element : operator |
|
127 | - list($typec,$comp) = $this->eval_getOper($rule,$item); |
|
128 | - //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
|
126 | + // Second element : operator |
|
127 | + list($typec,$comp) = $this->eval_getOper($rule,$item); |
|
128 | + //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
|
129 | 129 | |
130 | - // Third element : number, string or () |
|
131 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
132 | - { |
|
133 | - $item++; |
|
134 | - if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
135 | - $val2= ! $this->evaluation($rule,$item); |
|
136 | - $type2=2; // result is a boolean |
|
137 | - } |
|
138 | - else |
|
139 | - { |
|
140 | - list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
141 | - } |
|
142 | - //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
|
130 | + // Third element : number, string or () |
|
131 | + if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
132 | + { |
|
133 | + $item++; |
|
134 | + if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
135 | + $val2= ! $this->evaluation($rule,$item); |
|
136 | + $type2=2; // result is a boolean |
|
137 | + } |
|
138 | + else |
|
139 | + { |
|
140 | + list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
141 | + } |
|
142 | + //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
|
143 | 143 | |
144 | - if ($type1!=$type2) // cannot compare different types |
|
145 | - { |
|
146 | - throw new Exception("Cannot compare string & number : ".$rule); |
|
147 | - } |
|
148 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
149 | - { |
|
150 | - throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
|
151 | - } |
|
144 | + if ($type1!=$type2) // cannot compare different types |
|
145 | + { |
|
146 | + throw new Exception("Cannot compare string & number : ".$rule); |
|
147 | + } |
|
148 | + if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
149 | + { |
|
150 | + throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
|
151 | + } |
|
152 | 152 | |
153 | - $retVal = $this->do_compare($val1, $val2, $comp, $negate); |
|
153 | + $retVal = $this->do_compare($val1, $val2, $comp, $negate); |
|
154 | 154 | |
155 | - if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
156 | - // check for logical operator : |
|
157 | - switch ($rule[$item]) |
|
158 | - { |
|
159 | - case '|': $item++; return ($retVal || $this->evaluation($rule,$item) ); |
|
160 | - case '&': $item++; return ($retVal && $this->evaluation($rule,$item) ); |
|
155 | + if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
156 | + // check for logical operator : |
|
157 | + switch ($rule[$item]) |
|
158 | + { |
|
159 | + case '|': $item++; return ($retVal || $this->evaluation($rule,$item) ); |
|
160 | + case '&': $item++; return ($retVal && $this->evaluation($rule,$item) ); |
|
161 | 161 | |
162 | - default: throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]); |
|
163 | - } |
|
164 | - } |
|
162 | + default: throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]); |
|
163 | + } |
|
164 | + } |
|
165 | 165 | |
166 | - /** |
|
167 | - * Get '*' or '**' and transform in [0-9]+ or .* in return string |
|
168 | - * @param string $oid OID in normal or regexp format. '*' will be escaped ('\*') |
|
169 | - * @return string correct regexp format |
|
170 | - */ |
|
171 | - public function regexp_eval(string &$oid) |
|
172 | - { |
|
173 | - // ** replaced by .* |
|
174 | - $oidR=preg_replace('/\*\*/', '.*', $oid); |
|
175 | - // * replaced by [0-9]+ |
|
176 | - $oidR=preg_replace('/\*/', '[0-9]+', $oidR); |
|
166 | + /** |
|
167 | + * Get '*' or '**' and transform in [0-9]+ or .* in return string |
|
168 | + * @param string $oid OID in normal or regexp format. '*' will be escaped ('\*') |
|
169 | + * @return string correct regexp format |
|
170 | + */ |
|
171 | + public function regexp_eval(string &$oid) |
|
172 | + { |
|
173 | + // ** replaced by .* |
|
174 | + $oidR=preg_replace('/\*\*/', '.*', $oid); |
|
175 | + // * replaced by [0-9]+ |
|
176 | + $oidR=preg_replace('/\*/', '[0-9]+', $oidR); |
|
177 | 177 | |
178 | - // replace * with \* in oid for preg_replace |
|
179 | - $oid=preg_replace('/\*/', '\*', $oid); |
|
178 | + // replace * with \* in oid for preg_replace |
|
179 | + $oid=preg_replace('/\*/', '\*', $oid); |
|
180 | 180 | |
181 | - $this->logging->log('Regexp eval : '.$oid.' / '.$oidR,DEBUG ); |
|
181 | + $this->logging->log('Regexp eval : '.$oid.' / '.$oidR,DEBUG ); |
|
182 | 182 | |
183 | - return $oidR; |
|
184 | - } |
|
183 | + return $oidR; |
|
184 | + } |
|
185 | 185 | |
186 | 186 | |
187 | - /** Evaluation rule (uses eval_* functions recursively) |
|
188 | - * @param string $rule : rule ( _OID(.1.3.6.1.4.1.8072.2.3.2.1)=_OID(.1.3.6.1.2.1.1.3.0) ) |
|
189 | - * @param array $oidList : OIDs values to sustitute. |
|
190 | - * @return bool : true : rule match, false : rule don't match , throw exception on error. |
|
191 | - */ |
|
192 | - public function eval_rule($rule,$oidList) |
|
193 | - { |
|
194 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
195 | - { |
|
196 | - return true; |
|
197 | - } |
|
198 | - $matches=array(); |
|
199 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
|
200 | - { |
|
201 | - $oid=$matches[1]; |
|
202 | - $found=0; |
|
203 | - // Test and transform regexp |
|
204 | - $oidR = $this->regexp_eval($oid); |
|
187 | + /** Evaluation rule (uses eval_* functions recursively) |
|
188 | + * @param string $rule : rule ( _OID(.1.3.6.1.4.1.8072.2.3.2.1)=_OID(.1.3.6.1.2.1.1.3.0) ) |
|
189 | + * @param array $oidList : OIDs values to sustitute. |
|
190 | + * @return bool : true : rule match, false : rule don't match , throw exception on error. |
|
191 | + */ |
|
192 | + public function eval_rule($rule,$oidList) |
|
193 | + { |
|
194 | + if ($rule==null || $rule == '') // Empty rule is always true |
|
195 | + { |
|
196 | + return true; |
|
197 | + } |
|
198 | + $matches=array(); |
|
199 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
|
200 | + { |
|
201 | + $oid=$matches[1]; |
|
202 | + $found=0; |
|
203 | + // Test and transform regexp |
|
204 | + $oidR = $this->regexp_eval($oid); |
|
205 | 205 | |
206 | - foreach($oidList as $val) |
|
207 | - { |
|
208 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
209 | - { |
|
210 | - if (!preg_match('/^-?[0-9]*\.?[0-9]+$/',$val->value)) |
|
211 | - { // If not a number, change " to ' and put " around it |
|
212 | - $val->value=preg_replace('/"/',"'",$val->value); |
|
213 | - $val->value='"'.$val->value.'"'; |
|
214 | - } |
|
215 | - $rep=0; |
|
216 | - $rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep); |
|
217 | - if ($rep==0) |
|
218 | - { |
|
219 | - $this->logging->log("Error in rule_eval",WARN,''); |
|
220 | - return false; |
|
221 | - } |
|
222 | - $found=1; |
|
223 | - break; |
|
224 | - } |
|
225 | - } |
|
226 | - if ($found==0) |
|
227 | - { // OID not found : throw error |
|
228 | - throw new Exception('OID '.$oid.' not found in trap'); |
|
229 | - } |
|
230 | - } |
|
231 | - $item=0; |
|
232 | - $rule=$this->eval_cleanup($rule); |
|
233 | - $this->logging->log('Rule after clenup: '.$rule,INFO ); |
|
206 | + foreach($oidList as $val) |
|
207 | + { |
|
208 | + if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
209 | + { |
|
210 | + if (!preg_match('/^-?[0-9]*\.?[0-9]+$/',$val->value)) |
|
211 | + { // If not a number, change " to ' and put " around it |
|
212 | + $val->value=preg_replace('/"/',"'",$val->value); |
|
213 | + $val->value='"'.$val->value.'"'; |
|
214 | + } |
|
215 | + $rep=0; |
|
216 | + $rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep); |
|
217 | + if ($rep==0) |
|
218 | + { |
|
219 | + $this->logging->log("Error in rule_eval",WARN,''); |
|
220 | + return false; |
|
221 | + } |
|
222 | + $found=1; |
|
223 | + break; |
|
224 | + } |
|
225 | + } |
|
226 | + if ($found==0) |
|
227 | + { // OID not found : throw error |
|
228 | + throw new Exception('OID '.$oid.' not found in trap'); |
|
229 | + } |
|
230 | + } |
|
231 | + $item=0; |
|
232 | + $rule=$this->eval_cleanup($rule); |
|
233 | + $this->logging->log('Rule after clenup: '.$rule,INFO ); |
|
234 | 234 | |
235 | - return $this->evaluation($rule,$item); |
|
236 | - } |
|
235 | + return $this->evaluation($rule,$item); |
|
236 | + } |
|
237 | 237 | |
238 | 238 | } |
239 | 239 | \ No newline at end of file |
@@ -26,14 +26,14 @@ discard block |
||
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,52 +50,52 @@ discard block |
||
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_getOper($rule,&$item) |
|
57 | + protected function eval_getOper($rule, &$item) |
|
58 | 58 | { |
59 | - while ($rule[$item]==' ') $item++; |
|
59 | + while ($rule[$item] == ' ') $item++; |
|
60 | 60 | switch ($rule[$item]) |
61 | 61 | { |
62 | 62 | case '<': |
63 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");} |
|
64 | - $item++; return array(0,"<"); |
|
63 | + if ($rule[$item + 1] == '=') { $item+=2; return array(0, "<="); } |
|
64 | + $item++; return array(0, "<"); |
|
65 | 65 | case '>': |
66 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");} |
|
67 | - $item++; return array(0,">"); |
|
66 | + if ($rule[$item + 1] == '=') { $item+=2; return array(0, ">="); } |
|
67 | + $item++; return array(0, ">"); |
|
68 | 68 | case '=': |
69 | - $item++; return array(0,"="); |
|
69 | + $item++; return array(0, "="); |
|
70 | 70 | case '!': |
71 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");} |
|
72 | - throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule ." at " .$item); |
|
71 | + if ($rule[$item + 1] == '=') { $item+=2; return array(0, "!="); } |
|
72 | + throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule." at ".$item); |
|
73 | 73 | case '~': |
74 | - $item++; return array(0,"~"); |
|
74 | + $item++; return array(0, "~"); |
|
75 | 75 | case '|': |
76 | - $item++; return array(1,"|"); |
|
76 | + $item++; return array(1, "|"); |
|
77 | 77 | case '&': |
78 | - $item++; return array(1,"&"); |
|
78 | + $item++; return array(1, "&"); |
|
79 | 79 | default : |
80 | - throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item); |
|
80 | + throw new Exception("Erreur in expr - operator not found in ".$rule." at ".$item); |
|
81 | 81 | } |
82 | 82 | } |
83 | 83 | |
84 | - private function do_compare($val1,$val2,$comp,$negate) |
|
84 | + private function do_compare($val1, $val2, $comp, $negate) |
|
85 | 85 | { |
86 | - switch ($comp){ |
|
87 | - case '<': $retVal= ($val1 < $val2); break; |
|
88 | - case '<=': $retVal= ($val1 <= $val2); break; |
|
89 | - case '>': $retVal= ($val1 > $val2); break; |
|
90 | - case '>=': $retVal= ($val1 >= $val2); break; |
|
91 | - case '=': $retVal= ($val1 == $val2); break; |
|
92 | - case '!=': $retVal= ($val1 != $val2); break; |
|
93 | - case '~': $retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break; |
|
94 | - case '|': $retVal= ($val1 || $val2); break; |
|
95 | - case '&': $retVal= ($val1 && $val2); break; |
|
86 | + switch ($comp) { |
|
87 | + case '<': $retVal=($val1 < $val2); break; |
|
88 | + case '<=': $retVal=($val1 <= $val2); break; |
|
89 | + case '>': $retVal=($val1 > $val2); break; |
|
90 | + case '>=': $retVal=($val1 >= $val2); break; |
|
91 | + case '=': $retVal=($val1 == $val2); break; |
|
92 | + case '!=': $retVal=($val1 != $val2); break; |
|
93 | + case '~': $retVal=(preg_match('/'.preg_replace('/"/', '', $val2).'/', $val1)); break; |
|
94 | + case '|': $retVal=($val1 || $val2); break; |
|
95 | + case '&': $retVal=($val1 && $val2); break; |
|
96 | 96 | default: throw new Exception("Error in expression - unknown comp : ".$comp); |
97 | 97 | } |
98 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
98 | + if ($negate === true) $retVal=!$retVal; // Inverse result if negate before expression |
|
99 | 99 | |
100 | 100 | return $retVal; |
101 | 101 | } |
@@ -108,56 +108,56 @@ discard block |
||
108 | 108 | * comparison int vs strings will return null (error) |
109 | 109 | * return : bool or null on error |
110 | 110 | */ |
111 | - public function evaluation($rule,&$item) |
|
111 | + public function evaluation($rule, &$item) |
|
112 | 112 | { |
113 | 113 | //echo "Evaluation of ".substr($rule,$item)."\n"; |
114 | 114 | $negate=$this->check_negate_first($rule, $item); |
115 | 115 | // First element : number, string or () |
116 | - list($type1,$val1) = $this->eval_getElement($rule,$item); |
|
116 | + list($type1, $val1)=$this->eval_getElement($rule, $item); |
|
117 | 117 | //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
118 | 118 | |
119 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
119 | + if ($item == strlen($rule)) // If only element, return value, but only boolean |
|
120 | 120 | { |
121 | 121 | if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
122 | - if ($negate === true) $val1= ! $val1; |
|
122 | + if ($negate === true) $val1=!$val1; |
|
123 | 123 | return $val1; |
124 | 124 | } |
125 | 125 | |
126 | 126 | // Second element : operator |
127 | - list($typec,$comp) = $this->eval_getOper($rule,$item); |
|
127 | + list($typec, $comp)=$this->eval_getOper($rule, $item); |
|
128 | 128 | //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
129 | 129 | |
130 | 130 | // Third element : number, string or () |
131 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
131 | + if ($rule[$item] == '!') // starts with a ! so evaluate whats next |
|
132 | 132 | { |
133 | 133 | $item++; |
134 | 134 | if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
135 | - $val2= ! $this->evaluation($rule,$item); |
|
135 | + $val2=!$this->evaluation($rule, $item); |
|
136 | 136 | $type2=2; // result is a boolean |
137 | 137 | } |
138 | 138 | else |
139 | 139 | { |
140 | - list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
140 | + list($type2, $val2)=$this->eval_getElement($rule, $item); |
|
141 | 141 | } |
142 | 142 | //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
143 | 143 | |
144 | - if ($type1!=$type2) // cannot compare different types |
|
144 | + if ($type1 != $type2) // cannot compare different types |
|
145 | 145 | { |
146 | 146 | throw new Exception("Cannot compare string & number : ".$rule); |
147 | 147 | } |
148 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
148 | + if ($typec == 1 && $type1 != 2) // cannot use & or | with string/number |
|
149 | 149 | { |
150 | 150 | throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
151 | 151 | } |
152 | 152 | |
153 | - $retVal = $this->do_compare($val1, $val2, $comp, $negate); |
|
153 | + $retVal=$this->do_compare($val1, $val2, $comp, $negate); |
|
154 | 154 | |
155 | - if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
155 | + if ($item == strlen($rule)) return $retVal; // End of string : return evaluation |
|
156 | 156 | // check for logical operator : |
157 | 157 | switch ($rule[$item]) |
158 | 158 | { |
159 | - case '|': $item++; return ($retVal || $this->evaluation($rule,$item) ); |
|
160 | - case '&': $item++; return ($retVal && $this->evaluation($rule,$item) ); |
|
159 | + case '|': $item++; return ($retVal || $this->evaluation($rule, $item)); |
|
160 | + case '&': $item++; return ($retVal && $this->evaluation($rule, $item)); |
|
161 | 161 | |
162 | 162 | default: throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]); |
163 | 163 | } |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | // replace * with \* in oid for preg_replace |
179 | 179 | $oid=preg_replace('/\*/', '\*', $oid); |
180 | 180 | |
181 | - $this->logging->log('Regexp eval : '.$oid.' / '.$oidR,DEBUG ); |
|
181 | + $this->logging->log('Regexp eval : '.$oid.' / '.$oidR, DEBUG); |
|
182 | 182 | |
183 | 183 | return $oidR; |
184 | 184 | } |
@@ -189,50 +189,50 @@ discard block |
||
189 | 189 | * @param array $oidList : OIDs values to sustitute. |
190 | 190 | * @return bool : true : rule match, false : rule don't match , throw exception on error. |
191 | 191 | */ |
192 | - public function eval_rule($rule,$oidList) |
|
192 | + public function eval_rule($rule, $oidList) |
|
193 | 193 | { |
194 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
194 | + if ($rule == null || $rule == '') // Empty rule is always true |
|
195 | 195 | { |
196 | 196 | return true; |
197 | 197 | } |
198 | 198 | $matches=array(); |
199 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
|
199 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/', $rule, $matches) == 1) |
|
200 | 200 | { |
201 | 201 | $oid=$matches[1]; |
202 | 202 | $found=0; |
203 | 203 | // Test and transform regexp |
204 | - $oidR = $this->regexp_eval($oid); |
|
204 | + $oidR=$this->regexp_eval($oid); |
|
205 | 205 | |
206 | - foreach($oidList as $val) |
|
206 | + foreach ($oidList as $val) |
|
207 | 207 | { |
208 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
208 | + if (preg_match("/^$oidR$/", $val->oid) == 1) |
|
209 | 209 | { |
210 | - if (!preg_match('/^-?[0-9]*\.?[0-9]+$/',$val->value)) |
|
210 | + if (!preg_match('/^-?[0-9]*\.?[0-9]+$/', $val->value)) |
|
211 | 211 | { // If not a number, change " to ' and put " around it |
212 | - $val->value=preg_replace('/"/',"'",$val->value); |
|
212 | + $val->value=preg_replace('/"/', "'", $val->value); |
|
213 | 213 | $val->value='"'.$val->value.'"'; |
214 | 214 | } |
215 | 215 | $rep=0; |
216 | - $rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep); |
|
217 | - if ($rep==0) |
|
216 | + $rule=preg_replace('/_OID\('.$oid.'\)/', $val->value, $rule, -1, $rep); |
|
217 | + if ($rep == 0) |
|
218 | 218 | { |
219 | - $this->logging->log("Error in rule_eval",WARN,''); |
|
219 | + $this->logging->log("Error in rule_eval", WARN, ''); |
|
220 | 220 | return false; |
221 | 221 | } |
222 | 222 | $found=1; |
223 | 223 | break; |
224 | 224 | } |
225 | 225 | } |
226 | - if ($found==0) |
|
226 | + if ($found == 0) |
|
227 | 227 | { // OID not found : throw error |
228 | 228 | throw new Exception('OID '.$oid.' not found in trap'); |
229 | 229 | } |
230 | 230 | } |
231 | 231 | $item=0; |
232 | 232 | $rule=$this->eval_cleanup($rule); |
233 | - $this->logging->log('Rule after clenup: '.$rule,INFO ); |
|
233 | + $this->logging->log('Rule after clenup: '.$rule, INFO); |
|
234 | 234 | |
235 | - return $this->evaluation($rule,$item); |
|
235 | + return $this->evaluation($rule, $item); |
|
236 | 236 | } |
237 | 237 | |
238 | 238 | } |
239 | 239 | \ No newline at end of file |
@@ -32,7 +32,9 @@ discard block |
||
32 | 32 | { |
33 | 33 | throw new Exception("Early end of string ".$rule ." at " .$item ); |
34 | 34 | } |
35 | - while ($rule[$item]==' ') $item++; |
|
35 | + while ($rule[$item]==' ') { |
|
36 | + $item++; |
|
37 | + } |
|
36 | 38 | if (preg_match('/[\-0-9\.]/',$rule[$item])) |
37 | 39 | { // number |
38 | 40 | return $this->get_number($rule, $item); |
@@ -56,7 +58,9 @@ discard block |
||
56 | 58 | |
57 | 59 | protected function eval_getOper($rule,&$item) |
58 | 60 | { |
59 | - while ($rule[$item]==' ') $item++; |
|
61 | + while ($rule[$item]==' ') { |
|
62 | + $item++; |
|
63 | + } |
|
60 | 64 | switch ($rule[$item]) |
61 | 65 | { |
62 | 66 | case '<': |
@@ -95,7 +99,10 @@ discard block |
||
95 | 99 | case '&': $retVal= ($val1 && $val2); break; |
96 | 100 | default: throw new Exception("Error in expression - unknown comp : ".$comp); |
97 | 101 | } |
98 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
102 | + if ($negate === true) { |
|
103 | + $retVal = ! $retVal; |
|
104 | + } |
|
105 | + // Inverse result if negate before expression |
|
99 | 106 | |
100 | 107 | return $retVal; |
101 | 108 | } |
@@ -116,10 +123,14 @@ discard block |
||
116 | 123 | list($type1,$val1) = $this->eval_getElement($rule,$item); |
117 | 124 | //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
118 | 125 | |
119 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
126 | + if ($item==strlen($rule)) { |
|
127 | + // If only element, return value, but only boolean |
|
120 | 128 | { |
121 | 129 | if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
122 | - if ($negate === true) $val1= ! $val1; |
|
130 | + } |
|
131 | + if ($negate === true) { |
|
132 | + $val1= ! $val1; |
|
133 | + } |
|
123 | 134 | return $val1; |
124 | 135 | } |
125 | 136 | |
@@ -128,31 +139,41 @@ discard block |
||
128 | 139 | //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
129 | 140 | |
130 | 141 | // Third element : number, string or () |
131 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
142 | + if ( $rule[$item] == '!') { |
|
143 | + // starts with a ! so evaluate whats next |
|
132 | 144 | { |
133 | 145 | $item++; |
134 | - if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
146 | + } |
|
147 | + if ($typec != 1) { |
|
148 | + throw new Exception("Mixing boolean and comparison : ".$rule); |
|
149 | + } |
|
135 | 150 | $val2= ! $this->evaluation($rule,$item); |
136 | 151 | $type2=2; // result is a boolean |
137 | - } |
|
138 | - else |
|
152 | + } else |
|
139 | 153 | { |
140 | 154 | list($type2,$val2) = $this->eval_getElement($rule,$item); |
141 | 155 | } |
142 | 156 | //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
143 | 157 | |
144 | - if ($type1!=$type2) // cannot compare different types |
|
158 | + if ($type1!=$type2) { |
|
159 | + // cannot compare different types |
|
145 | 160 | { |
146 | 161 | throw new Exception("Cannot compare string & number : ".$rule); |
147 | 162 | } |
148 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
163 | + } |
|
164 | + if ($typec==1 && $type1 !=2) { |
|
165 | + // cannot use & or | with string/number |
|
149 | 166 | { |
150 | 167 | throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
151 | 168 | } |
169 | + } |
|
152 | 170 | |
153 | 171 | $retVal = $this->do_compare($val1, $val2, $comp, $negate); |
154 | 172 | |
155 | - if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
173 | + if ($item==strlen($rule)) { |
|
174 | + return $retVal; |
|
175 | + } |
|
176 | + // End of string : return evaluation |
|
156 | 177 | // check for logical operator : |
157 | 178 | switch ($rule[$item]) |
158 | 179 | { |
@@ -191,10 +212,12 @@ discard block |
||
191 | 212 | */ |
192 | 213 | public function eval_rule($rule,$oidList) |
193 | 214 | { |
194 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
215 | + if ($rule==null || $rule == '') { |
|
216 | + // Empty rule is always true |
|
195 | 217 | { |
196 | 218 | return true; |
197 | 219 | } |
220 | + } |
|
198 | 221 | $matches=array(); |
199 | 222 | while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
200 | 223 | { |