@@ -11,331 +11,331 @@ |
||
| 11 | 11 | class Rule |
| 12 | 12 | { |
| 13 | 13 | |
| 14 | - protected $logging; //< logging class |
|
| 14 | + protected $logging; //< logging class |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Setup Rule Class |
|
| 18 | - * @param Logging $logClass : where to log |
|
| 19 | - */ |
|
| 20 | - function __construct($logClass) |
|
| 21 | - { |
|
| 22 | - $this->logging=$logClass; |
|
| 16 | + /** |
|
| 17 | + * Setup Rule Class |
|
| 18 | + * @param Logging $logClass : where to log |
|
| 19 | + */ |
|
| 20 | + function __construct($logClass) |
|
| 21 | + { |
|
| 22 | + $this->logging=$logClass; |
|
| 23 | 23 | |
| 24 | - } |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | 27 | * Get full number |
| 28 | 28 | * @return array<number,string> |
| 29 | 29 | */ |
| 30 | - private function get_number($rule,&$item) |
|
| 31 | - { |
|
| 32 | - $item2=$item+1; |
|
| 33 | - while ( |
|
| 34 | - ($item2!=strlen($rule)) |
|
| 35 | - && (preg_match('/[0-9\.]/',$rule[$item2]))) |
|
| 36 | - { |
|
| 37 | - $item2++ ; |
|
| 38 | - } |
|
| 39 | - $val=substr($rule,$item,$item2-$item); |
|
| 40 | - $item=$item2; |
|
| 41 | - //echo "number ".$val."\n"; |
|
| 30 | + private function get_number($rule,&$item) |
|
| 31 | + { |
|
| 32 | + $item2=$item+1; |
|
| 33 | + while ( |
|
| 34 | + ($item2!=strlen($rule)) |
|
| 35 | + && (preg_match('/[0-9\.]/',$rule[$item2]))) |
|
| 36 | + { |
|
| 37 | + $item2++ ; |
|
| 38 | + } |
|
| 39 | + $val=substr($rule,$item,$item2-$item); |
|
| 40 | + $item=$item2; |
|
| 41 | + //echo "number ".$val."\n"; |
|
| 42 | 42 | |
| 43 | - return array(0,$val); |
|
| 44 | - } |
|
| 43 | + return array(0,$val); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - private function get_string($rule,&$item) |
|
| 47 | - { |
|
| 48 | - $item++; |
|
| 49 | - $item2=$this->eval_getNext($rule,$item,'"'); |
|
| 50 | - $val=substr($rule,$item,$item2-$item-1); |
|
| 51 | - $item=$item2; |
|
| 52 | - //echo "string : ".$val."\n"; |
|
| 53 | - return array(1,$val); |
|
| 46 | + private function get_string($rule,&$item) |
|
| 47 | + { |
|
| 48 | + $item++; |
|
| 49 | + $item2=$this->eval_getNext($rule,$item,'"'); |
|
| 50 | + $val=substr($rule,$item,$item2-$item-1); |
|
| 51 | + $item=$item2; |
|
| 52 | + //echo "string : ".$val."\n"; |
|
| 53 | + return array(1,$val); |
|
| 54 | 54 | |
| 55 | - } |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - private function get_group($rule,&$item) |
|
| 58 | - { |
|
| 59 | - $item++; |
|
| 60 | - $start=$item; |
|
| 61 | - $parenthesis_count=0; |
|
| 62 | - while (($item < strlen($rule)) // Not end of string AND |
|
| 63 | - && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
| 64 | - { |
|
| 65 | - if ($rule[$item] == '"' ) |
|
| 66 | - { // pass through string |
|
| 67 | - $item++; |
|
| 68 | - $item=$this->eval_getNext($rule,$item,'"'); |
|
| 69 | - } |
|
| 70 | - else{ |
|
| 71 | - if ($rule[$item] == '(') |
|
| 72 | - { |
|
| 73 | - $parenthesis_count++; |
|
| 74 | - } |
|
| 75 | - if ($rule[$item] == ')') |
|
| 76 | - { |
|
| 77 | - $parenthesis_count--; |
|
| 78 | - } |
|
| 79 | - $item++; |
|
| 80 | - } |
|
| 81 | - } |
|
| 57 | + private function get_group($rule,&$item) |
|
| 58 | + { |
|
| 59 | + $item++; |
|
| 60 | + $start=$item; |
|
| 61 | + $parenthesis_count=0; |
|
| 62 | + while (($item < strlen($rule)) // Not end of string AND |
|
| 63 | + && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
| 64 | + { |
|
| 65 | + if ($rule[$item] == '"' ) |
|
| 66 | + { // pass through string |
|
| 67 | + $item++; |
|
| 68 | + $item=$this->eval_getNext($rule,$item,'"'); |
|
| 69 | + } |
|
| 70 | + else{ |
|
| 71 | + if ($rule[$item] == '(') |
|
| 72 | + { |
|
| 73 | + $parenthesis_count++; |
|
| 74 | + } |
|
| 75 | + if ($rule[$item] == ')') |
|
| 76 | + { |
|
| 77 | + $parenthesis_count--; |
|
| 78 | + } |
|
| 79 | + $item++; |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} |
|
| 84 | - $val=substr($rule,$start,$item-$start); |
|
| 85 | - $item++; |
|
| 86 | - $start=0; |
|
| 87 | - //echo "group : ".$val."\n"; |
|
| 88 | - // returns evaluation of group as type 2 (boolean) |
|
| 89 | - return array(2,$this->evaluation($val,$start)); |
|
| 90 | - } |
|
| 83 | + if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} |
|
| 84 | + $val=substr($rule,$start,$item-$start); |
|
| 85 | + $item++; |
|
| 86 | + $start=0; |
|
| 87 | + //echo "group : ".$val."\n"; |
|
| 88 | + // returns evaluation of group as type 2 (boolean) |
|
| 89 | + return array(2,$this->evaluation($val,$start)); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - protected function eval_getElement($rule,&$item) |
|
| 93 | - { |
|
| 94 | - if ($item >= strlen($rule)) |
|
| 95 | - { |
|
| 96 | - throw new Exception("Early end of string ".$rule ." at " .$item ); |
|
| 97 | - } |
|
| 98 | - while ($rule[$item]==' ') $item++; |
|
| 99 | - if (preg_match('/[0-9\.]/',$rule[$item])) |
|
| 100 | - { // number |
|
| 101 | - return $this->get_number($rule, $item); |
|
| 102 | - } |
|
| 103 | - if ($rule[$item] == '"') |
|
| 104 | - { // string |
|
| 105 | - return $this->get_string($rule, $item); |
|
| 106 | - } |
|
| 92 | + protected function eval_getElement($rule,&$item) |
|
| 93 | + { |
|
| 94 | + if ($item >= strlen($rule)) |
|
| 95 | + { |
|
| 96 | + throw new Exception("Early end of string ".$rule ." at " .$item ); |
|
| 97 | + } |
|
| 98 | + while ($rule[$item]==' ') $item++; |
|
| 99 | + if (preg_match('/[0-9\.]/',$rule[$item])) |
|
| 100 | + { // number |
|
| 101 | + return $this->get_number($rule, $item); |
|
| 102 | + } |
|
| 103 | + if ($rule[$item] == '"') |
|
| 104 | + { // string |
|
| 105 | + return $this->get_string($rule, $item); |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - if ($rule[$item] == '(') |
|
| 109 | - { // grouping |
|
| 110 | - return $this->get_group($rule, $item); |
|
| 111 | - } |
|
| 112 | - throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]); |
|
| 108 | + if ($rule[$item] == '(') |
|
| 109 | + { // grouping |
|
| 110 | + return $this->get_group($rule, $item); |
|
| 111 | + } |
|
| 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) |
|
| 117 | - { |
|
| 118 | - while ( |
|
| 119 | - ($rule[$item] != $tok ) |
|
| 120 | - && ($item < strlen($rule))) |
|
| 121 | - { |
|
| 122 | - $item++; |
|
| 123 | - } |
|
| 124 | - if ($item==strlen($rule)) { |
|
| 125 | - throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
| 126 | - } |
|
| 127 | - return $item+1; |
|
| 128 | - } |
|
| 116 | + protected function eval_getNext($rule,$item,$tok) |
|
| 117 | + { |
|
| 118 | + while ( |
|
| 119 | + ($rule[$item] != $tok ) |
|
| 120 | + && ($item < strlen($rule))) |
|
| 121 | + { |
|
| 122 | + $item++; |
|
| 123 | + } |
|
| 124 | + if ($item==strlen($rule)) { |
|
| 125 | + throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
| 126 | + } |
|
| 127 | + return $item+1; |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - protected function eval_getOper($rule,&$item) |
|
| 131 | - { |
|
| 132 | - while ($rule[$item]==' ') $item++; |
|
| 133 | - switch ($rule[$item]) |
|
| 134 | - { |
|
| 135 | - case '<': |
|
| 136 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");} |
|
| 137 | - $item++; return array(0,"<"); |
|
| 138 | - case '>': |
|
| 139 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");} |
|
| 140 | - $item++; return array(0,">"); |
|
| 141 | - case '=': |
|
| 142 | - $item++; return array(0,"="); |
|
| 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); |
|
| 146 | - case '~': |
|
| 147 | - $item++; return array(0,"~"); |
|
| 148 | - case '|': |
|
| 149 | - $item++; return array(1,"|"); |
|
| 150 | - case '&': |
|
| 151 | - $item++; return array(1,"&"); |
|
| 152 | - default : |
|
| 153 | - throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item); |
|
| 154 | - } |
|
| 155 | - } |
|
| 130 | + protected function eval_getOper($rule,&$item) |
|
| 131 | + { |
|
| 132 | + while ($rule[$item]==' ') $item++; |
|
| 133 | + switch ($rule[$item]) |
|
| 134 | + { |
|
| 135 | + case '<': |
|
| 136 | + if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");} |
|
| 137 | + $item++; return array(0,"<"); |
|
| 138 | + case '>': |
|
| 139 | + if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");} |
|
| 140 | + $item++; return array(0,">"); |
|
| 141 | + case '=': |
|
| 142 | + $item++; return array(0,"="); |
|
| 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); |
|
| 146 | + case '~': |
|
| 147 | + $item++; return array(0,"~"); |
|
| 148 | + case '|': |
|
| 149 | + $item++; return array(1,"|"); |
|
| 150 | + case '&': |
|
| 151 | + $item++; return array(1,"&"); |
|
| 152 | + default : |
|
| 153 | + throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item); |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | 156 | |
| 157 | - private function check_negate_first($rule,&$item) |
|
| 158 | - { |
|
| 159 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
| 160 | - { |
|
| 161 | - $item++; |
|
| 162 | - return true; |
|
| 163 | - } |
|
| 164 | - else |
|
| 165 | - { |
|
| 166 | - return false; |
|
| 167 | - } |
|
| 168 | - } |
|
| 157 | + private function check_negate_first($rule,&$item) |
|
| 158 | + { |
|
| 159 | + if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
| 160 | + { |
|
| 161 | + $item++; |
|
| 162 | + return true; |
|
| 163 | + } |
|
| 164 | + else |
|
| 165 | + { |
|
| 166 | + return false; |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | - private function do_compare($val1,$val2,$comp,$negate) |
|
| 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; |
|
| 182 | - default: throw new Exception("Error in expression - unknown comp : ".$comp); |
|
| 183 | - } |
|
| 184 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
| 170 | + private function do_compare($val1,$val2,$comp,$negate) |
|
| 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; |
|
| 182 | + default: throw new Exception("Error in expression - unknown comp : ".$comp); |
|
| 183 | + } |
|
| 184 | + if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
| 185 | 185 | |
| 186 | - return $retVal; |
|
| 187 | - } |
|
| 186 | + return $retVal; |
|
| 187 | + } |
|
| 188 | 188 | |
| 189 | - /** Evaluation : makes token and evaluate. |
|
| 190 | - * Public function for expressions testing |
|
| 191 | - * accepts : < > = <= >= != (typec = 0) |
|
| 192 | - * operators : & | (typec=1) |
|
| 193 | - * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
|
| 194 | - * comparison int vs strings will return null (error) |
|
| 195 | - * return : bool or null on error |
|
| 196 | - */ |
|
| 197 | - public function evaluation($rule,&$item) |
|
| 198 | - { |
|
| 199 | - //echo "Evaluation of ".substr($rule,$item)."\n"; |
|
| 200 | - $negate=$this->check_negate_first($rule, $item); |
|
| 201 | - // First element : number, string or () |
|
| 202 | - list($type1,$val1) = $this->eval_getElement($rule,$item); |
|
| 203 | - //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
|
| 189 | + /** Evaluation : makes token and evaluate. |
|
| 190 | + * Public function for expressions testing |
|
| 191 | + * accepts : < > = <= >= != (typec = 0) |
|
| 192 | + * operators : & | (typec=1) |
|
| 193 | + * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
|
| 194 | + * comparison int vs strings will return null (error) |
|
| 195 | + * return : bool or null on error |
|
| 196 | + */ |
|
| 197 | + public function evaluation($rule,&$item) |
|
| 198 | + { |
|
| 199 | + //echo "Evaluation of ".substr($rule,$item)."\n"; |
|
| 200 | + $negate=$this->check_negate_first($rule, $item); |
|
| 201 | + // First element : number, string or () |
|
| 202 | + list($type1,$val1) = $this->eval_getElement($rule,$item); |
|
| 203 | + //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
|
| 204 | 204 | |
| 205 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
| 206 | - { |
|
| 207 | - if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
|
| 208 | - if ($negate === true) $val1= ! $val1; |
|
| 209 | - return $val1; |
|
| 210 | - } |
|
| 205 | + if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
| 206 | + { |
|
| 207 | + if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
|
| 208 | + if ($negate === true) $val1= ! $val1; |
|
| 209 | + return $val1; |
|
| 210 | + } |
|
| 211 | 211 | |
| 212 | - // Second element : operator |
|
| 213 | - list($typec,$comp) = $this->eval_getOper($rule,$item); |
|
| 214 | - //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
|
| 212 | + // Second element : operator |
|
| 213 | + list($typec,$comp) = $this->eval_getOper($rule,$item); |
|
| 214 | + //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
|
| 215 | 215 | |
| 216 | - // Third element : number, string or () |
|
| 217 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
| 218 | - { |
|
| 219 | - $item++; |
|
| 220 | - if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 221 | - $val2= ! $this->evaluation($rule,$item); |
|
| 222 | - $type2=2; // result is a boolean |
|
| 223 | - } |
|
| 224 | - else |
|
| 225 | - { |
|
| 226 | - list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
| 227 | - } |
|
| 228 | - //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
|
| 216 | + // Third element : number, string or () |
|
| 217 | + if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
| 218 | + { |
|
| 219 | + $item++; |
|
| 220 | + if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 221 | + $val2= ! $this->evaluation($rule,$item); |
|
| 222 | + $type2=2; // result is a boolean |
|
| 223 | + } |
|
| 224 | + else |
|
| 225 | + { |
|
| 226 | + list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
| 227 | + } |
|
| 228 | + //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
|
| 229 | 229 | |
| 230 | - if ($type1!=$type2) // cannot compare different types |
|
| 231 | - { |
|
| 232 | - throw new Exception("Cannot compare string & number : ".$rule); |
|
| 233 | - } |
|
| 234 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
| 235 | - { |
|
| 236 | - throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
|
| 237 | - } |
|
| 230 | + if ($type1!=$type2) // cannot compare different types |
|
| 231 | + { |
|
| 232 | + throw new Exception("Cannot compare string & number : ".$rule); |
|
| 233 | + } |
|
| 234 | + if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
| 235 | + { |
|
| 236 | + throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
|
| 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 |
|
| 242 | - // check for logical operator : |
|
| 243 | - switch ($rule[$item]) |
|
| 244 | - { |
|
| 245 | - case '|': $item++; return ($retVal || $this->evaluation($rule,$item) ); |
|
| 246 | - case '&': $item++; return ($retVal && $this->evaluation($rule,$item) ); |
|
| 241 | + if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
| 242 | + // check for logical operator : |
|
| 243 | + switch ($rule[$item]) |
|
| 244 | + { |
|
| 245 | + case '|': $item++; return ($retVal || $this->evaluation($rule,$item) ); |
|
| 246 | + case '&': $item++; return ($retVal && $this->evaluation($rule,$item) ); |
|
| 247 | 247 | |
| 248 | - default: throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]); |
|
| 249 | - } |
|
| 250 | - } |
|
| 248 | + default: throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]); |
|
| 249 | + } |
|
| 250 | + } |
|
| 251 | 251 | |
| 252 | - // Remove all whitespaces (when not quoted) |
|
| 253 | - public function eval_cleanup($rule) |
|
| 254 | - { |
|
| 255 | - $item=0; |
|
| 256 | - $rule2=''; |
|
| 257 | - while ($item < strlen($rule)) |
|
| 258 | - { |
|
| 259 | - if ($rule[$item]==' ') { $item++; continue; } |
|
| 260 | - if ($rule[$item]=='"') |
|
| 261 | - { |
|
| 262 | - $rule2.=$rule[$item]; |
|
| 263 | - $item++; |
|
| 264 | - while (($item < strlen($rule)) && ($rule[$item]!='"') ) |
|
| 265 | - { |
|
| 266 | - $rule2.=$rule[$item]; |
|
| 267 | - $item++; |
|
| 268 | - } |
|
| 269 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
| 270 | - $rule2.=$rule[$item]; |
|
| 271 | - $item++; |
|
| 272 | - continue; |
|
| 273 | - } |
|
| 252 | + // Remove all whitespaces (when not quoted) |
|
| 253 | + public function eval_cleanup($rule) |
|
| 254 | + { |
|
| 255 | + $item=0; |
|
| 256 | + $rule2=''; |
|
| 257 | + while ($item < strlen($rule)) |
|
| 258 | + { |
|
| 259 | + if ($rule[$item]==' ') { $item++; continue; } |
|
| 260 | + if ($rule[$item]=='"') |
|
| 261 | + { |
|
| 262 | + $rule2.=$rule[$item]; |
|
| 263 | + $item++; |
|
| 264 | + while (($item < strlen($rule)) && ($rule[$item]!='"') ) |
|
| 265 | + { |
|
| 266 | + $rule2.=$rule[$item]; |
|
| 267 | + $item++; |
|
| 268 | + } |
|
| 269 | + if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
| 270 | + $rule2.=$rule[$item]; |
|
| 271 | + $item++; |
|
| 272 | + continue; |
|
| 273 | + } |
|
| 274 | 274 | |
| 275 | - $rule2.=$rule[$item]; |
|
| 276 | - $item++; |
|
| 277 | - } |
|
| 275 | + $rule2.=$rule[$item]; |
|
| 276 | + $item++; |
|
| 277 | + } |
|
| 278 | 278 | |
| 279 | - return $rule2; |
|
| 280 | - } |
|
| 279 | + return $rule2; |
|
| 280 | + } |
|
| 281 | 281 | |
| 282 | - /** Evaluation rule (uses eval_* functions recursively) |
|
| 283 | - * @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) ) |
|
| 284 | - * @param array $oidList : OIDs values to sustitute. |
|
| 285 | - * @return bool : true : rule match, false : rule don't match , throw exception on error. |
|
| 286 | - */ |
|
| 282 | + /** Evaluation rule (uses eval_* functions recursively) |
|
| 283 | + * @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) ) |
|
| 284 | + * @param array $oidList : OIDs values to sustitute. |
|
| 285 | + * @return bool : true : rule match, false : rule don't match , throw exception on error. |
|
| 286 | + */ |
|
| 287 | 287 | |
| 288 | - public function eval_rule($rule,$oidList) |
|
| 289 | - { |
|
| 290 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
| 291 | - { |
|
| 292 | - return true; |
|
| 293 | - } |
|
| 294 | - $matches=array(); |
|
| 295 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
|
| 296 | - { |
|
| 297 | - $oid=$matches[1]; |
|
| 298 | - $found=0; |
|
| 299 | - // ** replaced by .* |
|
| 300 | - $oidR=preg_replace('/\*\*/', '.*', $oid); |
|
| 301 | - // * replaced by [^.]* |
|
| 302 | - $oidR=preg_replace('/\*/', '[0-9]+', $oidR); |
|
| 288 | + public function eval_rule($rule,$oidList) |
|
| 289 | + { |
|
| 290 | + if ($rule==null || $rule == '') // Empty rule is always true |
|
| 291 | + { |
|
| 292 | + return true; |
|
| 293 | + } |
|
| 294 | + $matches=array(); |
|
| 295 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
|
| 296 | + { |
|
| 297 | + $oid=$matches[1]; |
|
| 298 | + $found=0; |
|
| 299 | + // ** replaced by .* |
|
| 300 | + $oidR=preg_replace('/\*\*/', '.*', $oid); |
|
| 301 | + // * replaced by [^.]* |
|
| 302 | + $oidR=preg_replace('/\*/', '[0-9]+', $oidR); |
|
| 303 | 303 | |
| 304 | - // replace * with \* in oid for preg_replace |
|
| 305 | - $oid=preg_replace('/\*/', '\*', $oid); |
|
| 304 | + // replace * with \* in oid for preg_replace |
|
| 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) |
|
| 310 | - { |
|
| 311 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
| 312 | - { |
|
| 313 | - if (!preg_match('/^[0-9]*\.?[0-9]+$/',$val->value)) |
|
| 314 | - { // If not a number, change " to ' and put " around it |
|
| 315 | - $val->value=preg_replace('/"/',"'",$val->value); |
|
| 316 | - $val->value='"'.$val->value.'"'; |
|
| 317 | - } |
|
| 318 | - $rep=0; |
|
| 319 | - $rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep); |
|
| 320 | - if ($rep==0) |
|
| 321 | - { |
|
| 322 | - $this->logging->log("Error in rule_eval",WARN,''); |
|
| 323 | - return false; |
|
| 324 | - } |
|
| 325 | - $found=1; |
|
| 326 | - break; |
|
| 327 | - } |
|
| 328 | - } |
|
| 329 | - if ($found==0) |
|
| 330 | - { // OID not found : throw error |
|
| 331 | - throw new Exception('OID '.$oid.' not found in trap'); |
|
| 332 | - } |
|
| 333 | - } |
|
| 334 | - $item=0; |
|
| 335 | - $rule=$this->eval_cleanup($rule); |
|
| 336 | - $this->logging->log('Rule after clenup: '.$rule,INFO ); |
|
| 309 | + foreach($oidList as $val) |
|
| 310 | + { |
|
| 311 | + if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
| 312 | + { |
|
| 313 | + if (!preg_match('/^[0-9]*\.?[0-9]+$/',$val->value)) |
|
| 314 | + { // If not a number, change " to ' and put " around it |
|
| 315 | + $val->value=preg_replace('/"/',"'",$val->value); |
|
| 316 | + $val->value='"'.$val->value.'"'; |
|
| 317 | + } |
|
| 318 | + $rep=0; |
|
| 319 | + $rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep); |
|
| 320 | + if ($rep==0) |
|
| 321 | + { |
|
| 322 | + $this->logging->log("Error in rule_eval",WARN,''); |
|
| 323 | + return false; |
|
| 324 | + } |
|
| 325 | + $found=1; |
|
| 326 | + break; |
|
| 327 | + } |
|
| 328 | + } |
|
| 329 | + if ($found==0) |
|
| 330 | + { // OID not found : throw error |
|
| 331 | + throw new Exception('OID '.$oid.' not found in trap'); |
|
| 332 | + } |
|
| 333 | + } |
|
| 334 | + $item=0; |
|
| 335 | + $rule=$this->eval_cleanup($rule); |
|
| 336 | + $this->logging->log('Rule after clenup: '.$rule,INFO ); |
|
| 337 | 337 | |
| 338 | - return $this->evaluation($rule,$item); |
|
| 339 | - } |
|
| 338 | + return $this->evaluation($rule,$item); |
|
| 339 | + } |
|
| 340 | 340 | |
| 341 | 341 | } |
| 342 | 342 | \ No newline at end of file |
@@ -27,47 +27,47 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
@@ -60,14 +60,15 @@ discard block |
||
| 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) ) { |
|
| 64 | + // Closing ')' or embeded () |
|
| 64 | 65 | { |
| 65 | 66 | if ($rule[$item] == '"' ) |
| 66 | 67 | { // pass through string |
| 67 | 68 | $item++; |
| 69 | + } |
|
| 68 | 70 | $item=$this->eval_getNext($rule,$item,'"'); |
| 69 | - } |
|
| 70 | - else{ |
|
| 71 | + } else{ |
|
| 71 | 72 | if ($rule[$item] == '(') |
| 72 | 73 | { |
| 73 | 74 | $parenthesis_count++; |
@@ -95,7 +96,9 @@ discard block |
||
| 95 | 96 | { |
| 96 | 97 | throw new Exception("Early end of string ".$rule ." at " .$item ); |
| 97 | 98 | } |
| 98 | - while ($rule[$item]==' ') $item++; |
|
| 99 | + while ($rule[$item]==' ') { |
|
| 100 | + $item++; |
|
| 101 | + } |
|
| 99 | 102 | if (preg_match('/[0-9\.]/',$rule[$item])) |
| 100 | 103 | { // number |
| 101 | 104 | return $this->get_number($rule, $item); |
@@ -129,7 +132,9 @@ discard block |
||
| 129 | 132 | |
| 130 | 133 | protected function eval_getOper($rule,&$item) |
| 131 | 134 | { |
| 132 | - while ($rule[$item]==' ') $item++; |
|
| 135 | + while ($rule[$item]==' ') { |
|
| 136 | + $item++; |
|
| 137 | + } |
|
| 133 | 138 | switch ($rule[$item]) |
| 134 | 139 | { |
| 135 | 140 | case '<': |
@@ -156,12 +161,13 @@ discard block |
||
| 156 | 161 | |
| 157 | 162 | private function check_negate_first($rule,&$item) |
| 158 | 163 | { |
| 159 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
| 164 | + if ( $rule[$item] == '!') { |
|
| 165 | + // If '!' found, negate next expression. |
|
| 160 | 166 | { |
| 161 | 167 | $item++; |
| 162 | - return true; |
|
| 163 | 168 | } |
| 164 | - else |
|
| 169 | + return true; |
|
| 170 | + } else |
|
| 165 | 171 | { |
| 166 | 172 | return false; |
| 167 | 173 | } |
@@ -181,7 +187,10 @@ discard block |
||
| 181 | 187 | case '&': $retVal= ($val1 && $val2); break; |
| 182 | 188 | default: throw new Exception("Error in expression - unknown comp : ".$comp); |
| 183 | 189 | } |
| 184 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
| 190 | + if ($negate === true) { |
|
| 191 | + $retVal = ! $retVal; |
|
| 192 | + } |
|
| 193 | + // Inverse result if negate before expression |
|
| 185 | 194 | |
| 186 | 195 | return $retVal; |
| 187 | 196 | } |
@@ -202,10 +211,14 @@ discard block |
||
| 202 | 211 | list($type1,$val1) = $this->eval_getElement($rule,$item); |
| 203 | 212 | //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
| 204 | 213 | |
| 205 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
| 214 | + if ($item==strlen($rule)) { |
|
| 215 | + // If only element, return value, but only boolean |
|
| 206 | 216 | { |
| 207 | 217 | if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
| 208 | - if ($negate === true) $val1= ! $val1; |
|
| 218 | + } |
|
| 219 | + if ($negate === true) { |
|
| 220 | + $val1= ! $val1; |
|
| 221 | + } |
|
| 209 | 222 | return $val1; |
| 210 | 223 | } |
| 211 | 224 | |
@@ -214,31 +227,41 @@ discard block |
||
| 214 | 227 | //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
| 215 | 228 | |
| 216 | 229 | // Third element : number, string or () |
| 217 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
| 230 | + if ( $rule[$item] == '!') { |
|
| 231 | + // starts with a ! so evaluate whats next |
|
| 218 | 232 | { |
| 219 | 233 | $item++; |
| 220 | - if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 234 | + } |
|
| 235 | + if ($typec != 1) { |
|
| 236 | + throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 237 | + } |
|
| 221 | 238 | $val2= ! $this->evaluation($rule,$item); |
| 222 | 239 | $type2=2; // result is a boolean |
| 223 | - } |
|
| 224 | - else |
|
| 240 | + } else |
|
| 225 | 241 | { |
| 226 | 242 | list($type2,$val2) = $this->eval_getElement($rule,$item); |
| 227 | 243 | } |
| 228 | 244 | //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
| 229 | 245 | |
| 230 | - if ($type1!=$type2) // cannot compare different types |
|
| 246 | + if ($type1!=$type2) { |
|
| 247 | + // cannot compare different types |
|
| 231 | 248 | { |
| 232 | 249 | throw new Exception("Cannot compare string & number : ".$rule); |
| 233 | 250 | } |
| 234 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
| 251 | + } |
|
| 252 | + if ($typec==1 && $type1 !=2) { |
|
| 253 | + // cannot use & or | with string/number |
|
| 235 | 254 | { |
| 236 | 255 | throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
| 237 | 256 | } |
| 257 | + } |
|
| 238 | 258 | |
| 239 | 259 | $retVal = $this->do_compare($val1, $val2, $comp, $negate); |
| 240 | 260 | |
| 241 | - if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
| 261 | + if ($item==strlen($rule)) { |
|
| 262 | + return $retVal; |
|
| 263 | + } |
|
| 264 | + // End of string : return evaluation |
|
| 242 | 265 | // check for logical operator : |
| 243 | 266 | switch ($rule[$item]) |
| 244 | 267 | { |
@@ -266,7 +289,9 @@ discard block |
||
| 266 | 289 | $rule2.=$rule[$item]; |
| 267 | 290 | $item++; |
| 268 | 291 | } |
| 269 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
| 292 | + if ($item == strlen ($rule)) { |
|
| 293 | + throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
| 294 | + } |
|
| 270 | 295 | $rule2.=$rule[$item]; |
| 271 | 296 | $item++; |
| 272 | 297 | continue; |
@@ -287,10 +312,12 @@ discard block |
||
| 287 | 312 | |
| 288 | 313 | public function eval_rule($rule,$oidList) |
| 289 | 314 | { |
| 290 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
| 315 | + if ($rule==null || $rule == '') { |
|
| 316 | + // Empty rule is always true |
|
| 291 | 317 | { |
| 292 | 318 | return true; |
| 293 | 319 | } |
| 320 | + } |
|
| 294 | 321 | $matches=array(); |
| 295 | 322 | while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
| 296 | 323 | { |