@@ -7,378 +7,378 @@ |
||
| 7 | 7 | class Rule |
| 8 | 8 | { |
| 9 | 9 | |
| 10 | - protected $logging; //< logging class |
|
| 10 | + protected $logging; //< logging class |
|
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * Setup Rule Class |
|
| 14 | - * @param Logging $logClass : where to log |
|
| 15 | - */ |
|
| 16 | - function __construct($logClass) |
|
| 17 | - { |
|
| 18 | - $this->logging=$logClass; |
|
| 12 | + /** |
|
| 13 | + * Setup Rule Class |
|
| 14 | + * @param Logging $logClass : where to log |
|
| 15 | + */ |
|
| 16 | + function __construct($logClass) |
|
| 17 | + { |
|
| 18 | + $this->logging=$logClass; |
|
| 19 | 19 | |
| 20 | - } |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | 23 | * Get full number |
| 24 | 24 | * @return array<number,string> |
| 25 | 25 | */ |
| 26 | - private function get_number($rule,&$item) |
|
| 27 | - { |
|
| 28 | - $item2=$item+1; |
|
| 29 | - while ( |
|
| 30 | - ($item2!=strlen($rule)) |
|
| 31 | - && (preg_match('/[\-0-9\.]/',$rule[$item2]))) |
|
| 32 | - { |
|
| 33 | - $item2++ ; |
|
| 34 | - } |
|
| 35 | - $val=substr($rule,$item,$item2-$item); |
|
| 36 | - $item=$item2; |
|
| 37 | - //echo "number ".$val."\n"; |
|
| 26 | + private function get_number($rule,&$item) |
|
| 27 | + { |
|
| 28 | + $item2=$item+1; |
|
| 29 | + while ( |
|
| 30 | + ($item2!=strlen($rule)) |
|
| 31 | + && (preg_match('/[\-0-9\.]/',$rule[$item2]))) |
|
| 32 | + { |
|
| 33 | + $item2++ ; |
|
| 34 | + } |
|
| 35 | + $val=substr($rule,$item,$item2-$item); |
|
| 36 | + $item=$item2; |
|
| 37 | + //echo "number ".$val."\n"; |
|
| 38 | 38 | |
| 39 | - return array(0,$val); |
|
| 40 | - } |
|
| 39 | + return array(0,$val); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - private function get_string($rule,&$item) |
|
| 43 | - { |
|
| 44 | - $item++; |
|
| 45 | - $item2=$this->eval_getNext($rule,$item,'"'); |
|
| 46 | - $val=substr($rule,$item,$item2-$item-1); |
|
| 47 | - $item=$item2; |
|
| 48 | - //echo "string : ".$val."\n"; |
|
| 49 | - return array(1,$val); |
|
| 42 | + private function get_string($rule,&$item) |
|
| 43 | + { |
|
| 44 | + $item++; |
|
| 45 | + $item2=$this->eval_getNext($rule,$item,'"'); |
|
| 46 | + $val=substr($rule,$item,$item2-$item-1); |
|
| 47 | + $item=$item2; |
|
| 48 | + //echo "string : ".$val."\n"; |
|
| 49 | + return array(1,$val); |
|
| 50 | 50 | |
| 51 | - } |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Parse elements inside () : jumps over "" and count parenthesis. |
|
| 55 | - * Ex : ( "test" != ")test" & (1==2) ) will return "test" != ")test" & (1==2) |
|
| 56 | - * @param string $rule : the current rule |
|
| 57 | - * @param int $item : actual position in rule |
|
| 58 | - * @throws Exception |
|
| 59 | - * @return string : everything inside parenthesis |
|
| 60 | - */ |
|
| 61 | - private function parse_parenthesis(string $rule,int &$item) : string |
|
| 62 | - { |
|
| 63 | - $item++; |
|
| 64 | - $start=$item; |
|
| 65 | - $parenthesis_count=0; |
|
| 66 | - while (($item < strlen($rule)) // Not end of string AND |
|
| 67 | - && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
| 68 | - { |
|
| 69 | - if ($rule[$item] == '"' ) |
|
| 70 | - { // pass through string |
|
| 71 | - $item++; |
|
| 72 | - $item=$this->eval_getNext($rule,$item,'"'); |
|
| 73 | - } |
|
| 74 | - else{ |
|
| 75 | - if ($rule[$item] == '(') |
|
| 76 | - { |
|
| 77 | - $parenthesis_count++; |
|
| 78 | - } |
|
| 79 | - if ($rule[$item] == ')') |
|
| 80 | - { |
|
| 81 | - $parenthesis_count--; |
|
| 82 | - } |
|
| 83 | - $item++; |
|
| 84 | - } |
|
| 85 | - } |
|
| 53 | + /** |
|
| 54 | + * Parse elements inside () : jumps over "" and count parenthesis. |
|
| 55 | + * Ex : ( "test" != ")test" & (1==2) ) will return "test" != ")test" & (1==2) |
|
| 56 | + * @param string $rule : the current rule |
|
| 57 | + * @param int $item : actual position in rule |
|
| 58 | + * @throws Exception |
|
| 59 | + * @return string : everything inside parenthesis |
|
| 60 | + */ |
|
| 61 | + private function parse_parenthesis(string $rule,int &$item) : string |
|
| 62 | + { |
|
| 63 | + $item++; |
|
| 64 | + $start=$item; |
|
| 65 | + $parenthesis_count=0; |
|
| 66 | + while (($item < strlen($rule)) // Not end of string AND |
|
| 67 | + && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
| 68 | + { |
|
| 69 | + if ($rule[$item] == '"' ) |
|
| 70 | + { // pass through string |
|
| 71 | + $item++; |
|
| 72 | + $item=$this->eval_getNext($rule,$item,'"'); |
|
| 73 | + } |
|
| 74 | + else{ |
|
| 75 | + if ($rule[$item] == '(') |
|
| 76 | + { |
|
| 77 | + $parenthesis_count++; |
|
| 78 | + } |
|
| 79 | + if ($rule[$item] == ')') |
|
| 80 | + { |
|
| 81 | + $parenthesis_count--; |
|
| 82 | + } |
|
| 83 | + $item++; |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} |
|
| 88 | - $val=substr($rule,$start,$item-$start); |
|
| 89 | - $item++; |
|
| 90 | - return $val; |
|
| 91 | - } |
|
| 87 | + if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} |
|
| 88 | + $val=substr($rule,$start,$item-$start); |
|
| 89 | + $item++; |
|
| 90 | + return $val; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Get and eval a grouped condition - ex : (1==1) |
|
| 96 | - * @param string $rule |
|
| 97 | - * @param int $item |
|
| 98 | - * @return array |
|
| 99 | - */ |
|
| 100 | - private function get_group(string $rule,int &$item) : array |
|
| 101 | - { |
|
| 102 | - // gets eveything inside parenthesis |
|
| 103 | - $val=$this->parse_parenthesis($rule, $item); |
|
| 104 | - // Returns boolean with evaluation of all inside parenthesis |
|
| 105 | - $start=0; |
|
| 106 | - return array(2,$this->evaluation($val,$start)); |
|
| 107 | - } |
|
| 94 | + /** |
|
| 95 | + * Get and eval a grouped condition - ex : (1==1) |
|
| 96 | + * @param string $rule |
|
| 97 | + * @param int $item |
|
| 98 | + * @return array |
|
| 99 | + */ |
|
| 100 | + private function get_group(string $rule,int &$item) : array |
|
| 101 | + { |
|
| 102 | + // gets eveything inside parenthesis |
|
| 103 | + $val=$this->parse_parenthesis($rule, $item); |
|
| 104 | + // Returns boolean with evaluation of all inside parenthesis |
|
| 105 | + $start=0; |
|
| 106 | + return array(2,$this->evaluation($val,$start)); |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - private function get_function(string $rule,int &$item) : array |
|
| 110 | - { |
|
| 111 | - // function is : __function(param1,param2...) |
|
| 112 | - $start=$item; |
|
| 113 | - while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' |
|
| 114 | - { |
|
| 115 | - $item++; |
|
| 116 | - } |
|
| 117 | - if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} |
|
| 109 | + private function get_function(string $rule,int &$item) : array |
|
| 110 | + { |
|
| 111 | + // function is : __function(param1,param2...) |
|
| 112 | + $start=$item; |
|
| 113 | + while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' |
|
| 114 | + { |
|
| 115 | + $item++; |
|
| 116 | + } |
|
| 117 | + if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} |
|
| 118 | 118 | |
| 119 | - // get parameters between parenthesis |
|
| 119 | + // get parameters between parenthesis |
|
| 120 | 120 | |
| 121 | - $params=$this->parse_parenthesis($rule, $item); |
|
| 121 | + $params=$this->parse_parenthesis($rule, $item); |
|
| 122 | 122 | |
| 123 | - $val=substr($rule,$start,$item-$start); |
|
| 123 | + $val=substr($rule,$start,$item-$start); |
|
| 124 | 124 | |
| 125 | - $this->logging->log('got function ' . $val . ' returning true for now',DEBUG); |
|
| 125 | + $this->logging->log('got function ' . $val . ' returning true for now',DEBUG); |
|
| 126 | 126 | |
| 127 | - return array(2,true); |
|
| 127 | + return array(2,true); |
|
| 128 | 128 | |
| 129 | - } |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | - protected function eval_getElement($rule,&$item) |
|
| 132 | - { |
|
| 133 | - if ($item >= strlen($rule)) |
|
| 134 | - { |
|
| 135 | - throw new Exception("Early end of string ".$rule ." at " .$item ); |
|
| 136 | - } |
|
| 137 | - while ($rule[$item]==' ') $item++; |
|
| 138 | - if (preg_match('/[\-0-9\.]/',$rule[$item])) |
|
| 139 | - { // number |
|
| 140 | - return $this->get_number($rule, $item); |
|
| 141 | - } |
|
| 142 | - if ($rule[$item] == '"') |
|
| 143 | - { // string |
|
| 144 | - return $this->get_string($rule, $item); |
|
| 145 | - } |
|
| 131 | + protected function eval_getElement($rule,&$item) |
|
| 132 | + { |
|
| 133 | + if ($item >= strlen($rule)) |
|
| 134 | + { |
|
| 135 | + throw new Exception("Early end of string ".$rule ." at " .$item ); |
|
| 136 | + } |
|
| 137 | + while ($rule[$item]==' ') $item++; |
|
| 138 | + if (preg_match('/[\-0-9\.]/',$rule[$item])) |
|
| 139 | + { // number |
|
| 140 | + return $this->get_number($rule, $item); |
|
| 141 | + } |
|
| 142 | + if ($rule[$item] == '"') |
|
| 143 | + { // string |
|
| 144 | + return $this->get_string($rule, $item); |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - if ($rule[$item] == '(') |
|
| 148 | - { // grouping |
|
| 149 | - return $this->get_group($rule, $item); |
|
| 150 | - } |
|
| 151 | - if ($rule[$item] == '_') |
|
| 152 | - { // function |
|
| 153 | - return $this->get_function($rule, $item); |
|
| 154 | - } |
|
| 155 | - throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]); |
|
| 147 | + if ($rule[$item] == '(') |
|
| 148 | + { // grouping |
|
| 149 | + return $this->get_group($rule, $item); |
|
| 150 | + } |
|
| 151 | + if ($rule[$item] == '_') |
|
| 152 | + { // function |
|
| 153 | + return $this->get_function($rule, $item); |
|
| 154 | + } |
|
| 155 | + throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]); |
|
| 156 | 156 | |
| 157 | - } |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - protected function eval_getNext($rule,$item,$tok) |
|
| 160 | - { |
|
| 161 | - while ( |
|
| 162 | - ($rule[$item] != $tok ) |
|
| 163 | - && ($item < strlen($rule))) |
|
| 164 | - { |
|
| 165 | - $item++; |
|
| 166 | - } |
|
| 167 | - if ($item==strlen($rule)) { |
|
| 168 | - throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
| 169 | - } |
|
| 170 | - return $item+1; |
|
| 171 | - } |
|
| 159 | + protected function eval_getNext($rule,$item,$tok) |
|
| 160 | + { |
|
| 161 | + while ( |
|
| 162 | + ($rule[$item] != $tok ) |
|
| 163 | + && ($item < strlen($rule))) |
|
| 164 | + { |
|
| 165 | + $item++; |
|
| 166 | + } |
|
| 167 | + if ($item==strlen($rule)) { |
|
| 168 | + throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
| 169 | + } |
|
| 170 | + return $item+1; |
|
| 171 | + } |
|
| 172 | 172 | |
| 173 | - protected function eval_getOper($rule,&$item) |
|
| 174 | - { |
|
| 175 | - while ($rule[$item]==' ') $item++; |
|
| 176 | - switch ($rule[$item]) |
|
| 177 | - { |
|
| 178 | - case '<': |
|
| 179 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");} |
|
| 180 | - $item++; return array(0,"<"); |
|
| 181 | - case '>': |
|
| 182 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");} |
|
| 183 | - $item++; return array(0,">"); |
|
| 184 | - case '=': |
|
| 185 | - $item++; return array(0,"="); |
|
| 186 | - case '!': |
|
| 187 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");} |
|
| 188 | - throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule ." at " .$item); |
|
| 189 | - case '~': |
|
| 190 | - $item++; return array(0,"~"); |
|
| 191 | - case '|': |
|
| 192 | - $item++; return array(1,"|"); |
|
| 193 | - case '&': |
|
| 194 | - $item++; return array(1,"&"); |
|
| 195 | - default : |
|
| 196 | - throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item); |
|
| 197 | - } |
|
| 198 | - } |
|
| 173 | + protected function eval_getOper($rule,&$item) |
|
| 174 | + { |
|
| 175 | + while ($rule[$item]==' ') $item++; |
|
| 176 | + switch ($rule[$item]) |
|
| 177 | + { |
|
| 178 | + case '<': |
|
| 179 | + if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");} |
|
| 180 | + $item++; return array(0,"<"); |
|
| 181 | + case '>': |
|
| 182 | + if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");} |
|
| 183 | + $item++; return array(0,">"); |
|
| 184 | + case '=': |
|
| 185 | + $item++; return array(0,"="); |
|
| 186 | + case '!': |
|
| 187 | + if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");} |
|
| 188 | + throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule ." at " .$item); |
|
| 189 | + case '~': |
|
| 190 | + $item++; return array(0,"~"); |
|
| 191 | + case '|': |
|
| 192 | + $item++; return array(1,"|"); |
|
| 193 | + case '&': |
|
| 194 | + $item++; return array(1,"&"); |
|
| 195 | + default : |
|
| 196 | + throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item); |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | - private function check_negate_first($rule,&$item) |
|
| 201 | - { |
|
| 202 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
| 203 | - { |
|
| 204 | - $item++; |
|
| 205 | - return true; |
|
| 206 | - } |
|
| 207 | - else |
|
| 208 | - { |
|
| 209 | - return false; |
|
| 210 | - } |
|
| 211 | - } |
|
| 200 | + private function check_negate_first($rule,&$item) |
|
| 201 | + { |
|
| 202 | + if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
| 203 | + { |
|
| 204 | + $item++; |
|
| 205 | + return true; |
|
| 206 | + } |
|
| 207 | + else |
|
| 208 | + { |
|
| 209 | + return false; |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | 212 | |
| 213 | - private function do_compare($val1,$val2,$comp,$negate) |
|
| 214 | - { |
|
| 215 | - switch ($comp){ |
|
| 216 | - case '<': $retVal= ($val1 < $val2); break; |
|
| 217 | - case '<=': $retVal= ($val1 <= $val2); break; |
|
| 218 | - case '>': $retVal= ($val1 > $val2); break; |
|
| 219 | - case '>=': $retVal= ($val1 >= $val2); break; |
|
| 220 | - case '=': $retVal= ($val1 == $val2); break; |
|
| 221 | - case '!=': $retVal= ($val1 != $val2); break; |
|
| 222 | - case '~': $retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break; |
|
| 223 | - case '|': $retVal= ($val1 || $val2); break; |
|
| 224 | - case '&': $retVal= ($val1 && $val2); break; |
|
| 225 | - default: throw new Exception("Error in expression - unknown comp : ".$comp); |
|
| 226 | - } |
|
| 227 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
| 213 | + private function do_compare($val1,$val2,$comp,$negate) |
|
| 214 | + { |
|
| 215 | + switch ($comp){ |
|
| 216 | + case '<': $retVal= ($val1 < $val2); break; |
|
| 217 | + case '<=': $retVal= ($val1 <= $val2); break; |
|
| 218 | + case '>': $retVal= ($val1 > $val2); break; |
|
| 219 | + case '>=': $retVal= ($val1 >= $val2); break; |
|
| 220 | + case '=': $retVal= ($val1 == $val2); break; |
|
| 221 | + case '!=': $retVal= ($val1 != $val2); break; |
|
| 222 | + case '~': $retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break; |
|
| 223 | + case '|': $retVal= ($val1 || $val2); break; |
|
| 224 | + case '&': $retVal= ($val1 && $val2); break; |
|
| 225 | + default: throw new Exception("Error in expression - unknown comp : ".$comp); |
|
| 226 | + } |
|
| 227 | + if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
| 228 | 228 | |
| 229 | - return $retVal; |
|
| 230 | - } |
|
| 229 | + return $retVal; |
|
| 230 | + } |
|
| 231 | 231 | |
| 232 | - /** Evaluation : makes token and evaluate. |
|
| 233 | - * Public function for expressions testing |
|
| 234 | - * accepts : < > = <= >= != (typec = 0) |
|
| 235 | - * operators : & | (typec=1) |
|
| 236 | - * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
|
| 237 | - * comparison int vs strings will return null (error) |
|
| 238 | - * return : bool or null on error |
|
| 239 | - */ |
|
| 240 | - public function evaluation($rule,&$item) |
|
| 241 | - { |
|
| 242 | - //echo "Evaluation of ".substr($rule,$item)."\n"; |
|
| 243 | - $negate=$this->check_negate_first($rule, $item); |
|
| 244 | - // First element : number, string or () |
|
| 245 | - list($type1,$val1) = $this->eval_getElement($rule,$item); |
|
| 246 | - //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
|
| 232 | + /** Evaluation : makes token and evaluate. |
|
| 233 | + * Public function for expressions testing |
|
| 234 | + * accepts : < > = <= >= != (typec = 0) |
|
| 235 | + * operators : & | (typec=1) |
|
| 236 | + * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
|
| 237 | + * comparison int vs strings will return null (error) |
|
| 238 | + * return : bool or null on error |
|
| 239 | + */ |
|
| 240 | + public function evaluation($rule,&$item) |
|
| 241 | + { |
|
| 242 | + //echo "Evaluation of ".substr($rule,$item)."\n"; |
|
| 243 | + $negate=$this->check_negate_first($rule, $item); |
|
| 244 | + // First element : number, string or () |
|
| 245 | + list($type1,$val1) = $this->eval_getElement($rule,$item); |
|
| 246 | + //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
|
| 247 | 247 | |
| 248 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
| 249 | - { |
|
| 250 | - if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
|
| 251 | - if ($negate === true) $val1= ! $val1; |
|
| 252 | - return $val1; |
|
| 253 | - } |
|
| 248 | + if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
| 249 | + { |
|
| 250 | + if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
|
| 251 | + if ($negate === true) $val1= ! $val1; |
|
| 252 | + return $val1; |
|
| 253 | + } |
|
| 254 | 254 | |
| 255 | - // Second element : operator |
|
| 256 | - list($typec,$comp) = $this->eval_getOper($rule,$item); |
|
| 257 | - //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
|
| 255 | + // Second element : operator |
|
| 256 | + list($typec,$comp) = $this->eval_getOper($rule,$item); |
|
| 257 | + //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
|
| 258 | 258 | |
| 259 | - // Third element : number, string or () |
|
| 260 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
| 261 | - { |
|
| 262 | - $item++; |
|
| 263 | - if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 264 | - $val2= ! $this->evaluation($rule,$item); |
|
| 265 | - $type2=2; // result is a boolean |
|
| 266 | - } |
|
| 267 | - else |
|
| 268 | - { |
|
| 269 | - list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
| 270 | - } |
|
| 271 | - //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
|
| 259 | + // Third element : number, string or () |
|
| 260 | + if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
| 261 | + { |
|
| 262 | + $item++; |
|
| 263 | + if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 264 | + $val2= ! $this->evaluation($rule,$item); |
|
| 265 | + $type2=2; // result is a boolean |
|
| 266 | + } |
|
| 267 | + else |
|
| 268 | + { |
|
| 269 | + list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
| 270 | + } |
|
| 271 | + //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
|
| 272 | 272 | |
| 273 | - if ($type1!=$type2) // cannot compare different types |
|
| 274 | - { |
|
| 275 | - throw new Exception("Cannot compare string & number : ".$rule); |
|
| 276 | - } |
|
| 277 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
| 278 | - { |
|
| 279 | - throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
|
| 280 | - } |
|
| 273 | + if ($type1!=$type2) // cannot compare different types |
|
| 274 | + { |
|
| 275 | + throw new Exception("Cannot compare string & number : ".$rule); |
|
| 276 | + } |
|
| 277 | + if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
| 278 | + { |
|
| 279 | + throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
|
| 280 | + } |
|
| 281 | 281 | |
| 282 | - $retVal = $this->do_compare($val1, $val2, $comp, $negate); |
|
| 282 | + $retVal = $this->do_compare($val1, $val2, $comp, $negate); |
|
| 283 | 283 | |
| 284 | - if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
| 285 | - // check for logical operator : |
|
| 286 | - switch ($rule[$item]) |
|
| 287 | - { |
|
| 288 | - case '|': $item++; return ($retVal || $this->evaluation($rule,$item) ); |
|
| 289 | - case '&': $item++; return ($retVal && $this->evaluation($rule,$item) ); |
|
| 284 | + if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
| 285 | + // check for logical operator : |
|
| 286 | + switch ($rule[$item]) |
|
| 287 | + { |
|
| 288 | + case '|': $item++; return ($retVal || $this->evaluation($rule,$item) ); |
|
| 289 | + case '&': $item++; return ($retVal && $this->evaluation($rule,$item) ); |
|
| 290 | 290 | |
| 291 | - default: throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]); |
|
| 292 | - } |
|
| 293 | - } |
|
| 291 | + default: throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]); |
|
| 292 | + } |
|
| 293 | + } |
|
| 294 | 294 | |
| 295 | - // Remove all whitespaces (when not quoted) |
|
| 296 | - public function eval_cleanup($rule) |
|
| 297 | - { |
|
| 298 | - $item=0; |
|
| 299 | - $rule2=''; |
|
| 300 | - while ($item < strlen($rule)) |
|
| 301 | - { |
|
| 302 | - if ($rule[$item]==' ') { $item++; continue; } |
|
| 303 | - if ($rule[$item]=='"') |
|
| 304 | - { |
|
| 305 | - $rule2.=$rule[$item]; |
|
| 306 | - $item++; |
|
| 307 | - while (($item < strlen($rule)) && ($rule[$item]!='"') ) |
|
| 308 | - { |
|
| 309 | - $rule2.=$rule[$item]; |
|
| 310 | - $item++; |
|
| 311 | - } |
|
| 312 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
| 313 | - $rule2.=$rule[$item]; |
|
| 314 | - $item++; |
|
| 315 | - continue; |
|
| 316 | - } |
|
| 295 | + // Remove all whitespaces (when not quoted) |
|
| 296 | + public function eval_cleanup($rule) |
|
| 297 | + { |
|
| 298 | + $item=0; |
|
| 299 | + $rule2=''; |
|
| 300 | + while ($item < strlen($rule)) |
|
| 301 | + { |
|
| 302 | + if ($rule[$item]==' ') { $item++; continue; } |
|
| 303 | + if ($rule[$item]=='"') |
|
| 304 | + { |
|
| 305 | + $rule2.=$rule[$item]; |
|
| 306 | + $item++; |
|
| 307 | + while (($item < strlen($rule)) && ($rule[$item]!='"') ) |
|
| 308 | + { |
|
| 309 | + $rule2.=$rule[$item]; |
|
| 310 | + $item++; |
|
| 311 | + } |
|
| 312 | + if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
| 313 | + $rule2.=$rule[$item]; |
|
| 314 | + $item++; |
|
| 315 | + continue; |
|
| 316 | + } |
|
| 317 | 317 | |
| 318 | - $rule2.=$rule[$item]; |
|
| 319 | - $item++; |
|
| 320 | - } |
|
| 318 | + $rule2.=$rule[$item]; |
|
| 319 | + $item++; |
|
| 320 | + } |
|
| 321 | 321 | |
| 322 | - return $rule2; |
|
| 323 | - } |
|
| 322 | + return $rule2; |
|
| 323 | + } |
|
| 324 | 324 | |
| 325 | - /** Evaluation rule (uses eval_* functions recursively) |
|
| 326 | - * @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) ) |
|
| 327 | - * @param array $oidList : OIDs values to sustitute. |
|
| 328 | - * @return bool : true : rule match, false : rule don't match , throw exception on error. |
|
| 329 | - */ |
|
| 325 | + /** Evaluation rule (uses eval_* functions recursively) |
|
| 326 | + * @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) ) |
|
| 327 | + * @param array $oidList : OIDs values to sustitute. |
|
| 328 | + * @return bool : true : rule match, false : rule don't match , throw exception on error. |
|
| 329 | + */ |
|
| 330 | 330 | |
| 331 | - public function eval_rule($rule,$oidList) |
|
| 332 | - { |
|
| 333 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
| 334 | - { |
|
| 335 | - return true; |
|
| 336 | - } |
|
| 337 | - $matches=array(); |
|
| 338 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
|
| 339 | - { |
|
| 340 | - $oid=$matches[1]; |
|
| 341 | - $found=0; |
|
| 342 | - // ** replaced by .* |
|
| 343 | - $oidR=preg_replace('/\*\*/', '.*', $oid); |
|
| 344 | - // * replaced by [0-9]+ |
|
| 345 | - $oidR=preg_replace('/\*/', '[0-9]+', $oidR); |
|
| 331 | + public function eval_rule($rule,$oidList) |
|
| 332 | + { |
|
| 333 | + if ($rule==null || $rule == '') // Empty rule is always true |
|
| 334 | + { |
|
| 335 | + return true; |
|
| 336 | + } |
|
| 337 | + $matches=array(); |
|
| 338 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
|
| 339 | + { |
|
| 340 | + $oid=$matches[1]; |
|
| 341 | + $found=0; |
|
| 342 | + // ** replaced by .* |
|
| 343 | + $oidR=preg_replace('/\*\*/', '.*', $oid); |
|
| 344 | + // * replaced by [0-9]+ |
|
| 345 | + $oidR=preg_replace('/\*/', '[0-9]+', $oidR); |
|
| 346 | 346 | |
| 347 | - // replace * with \* in oid for preg_replace |
|
| 348 | - $oid=preg_replace('/\*/', '\*', $oid); |
|
| 347 | + // replace * with \* in oid for preg_replace |
|
| 348 | + $oid=preg_replace('/\*/', '\*', $oid); |
|
| 349 | 349 | |
| 350 | - $this->logging->log('OID in rule : '.$oid.' / '.$oidR,DEBUG ); |
|
| 350 | + $this->logging->log('OID in rule : '.$oid.' / '.$oidR,DEBUG ); |
|
| 351 | 351 | |
| 352 | - foreach($oidList as $val) |
|
| 353 | - { |
|
| 354 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
| 355 | - { |
|
| 356 | - if (!preg_match('/^-?[0-9]*\.?[0-9]+$/',$val->value)) |
|
| 357 | - { // If not a number, change " to ' and put " around it |
|
| 358 | - $val->value=preg_replace('/"/',"'",$val->value); |
|
| 359 | - $val->value='"'.$val->value.'"'; |
|
| 360 | - } |
|
| 361 | - $rep=0; |
|
| 362 | - $rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep); |
|
| 363 | - if ($rep==0) |
|
| 364 | - { |
|
| 365 | - $this->logging->log("Error in rule_eval",WARN,''); |
|
| 366 | - return false; |
|
| 367 | - } |
|
| 368 | - $found=1; |
|
| 369 | - break; |
|
| 370 | - } |
|
| 371 | - } |
|
| 372 | - if ($found==0) |
|
| 373 | - { // OID not found : throw error |
|
| 374 | - throw new Exception('OID '.$oid.' not found in trap'); |
|
| 375 | - } |
|
| 376 | - } |
|
| 377 | - $item=0; |
|
| 378 | - $rule=$this->eval_cleanup($rule); |
|
| 379 | - $this->logging->log('Rule after clenup: '.$rule,INFO ); |
|
| 352 | + foreach($oidList as $val) |
|
| 353 | + { |
|
| 354 | + if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
| 355 | + { |
|
| 356 | + if (!preg_match('/^-?[0-9]*\.?[0-9]+$/',$val->value)) |
|
| 357 | + { // If not a number, change " to ' and put " around it |
|
| 358 | + $val->value=preg_replace('/"/',"'",$val->value); |
|
| 359 | + $val->value='"'.$val->value.'"'; |
|
| 360 | + } |
|
| 361 | + $rep=0; |
|
| 362 | + $rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep); |
|
| 363 | + if ($rep==0) |
|
| 364 | + { |
|
| 365 | + $this->logging->log("Error in rule_eval",WARN,''); |
|
| 366 | + return false; |
|
| 367 | + } |
|
| 368 | + $found=1; |
|
| 369 | + break; |
|
| 370 | + } |
|
| 371 | + } |
|
| 372 | + if ($found==0) |
|
| 373 | + { // OID not found : throw error |
|
| 374 | + throw new Exception('OID '.$oid.' not found in trap'); |
|
| 375 | + } |
|
| 376 | + } |
|
| 377 | + $item=0; |
|
| 378 | + $rule=$this->eval_cleanup($rule); |
|
| 379 | + $this->logging->log('Rule after clenup: '.$rule,INFO ); |
|
| 380 | 380 | |
| 381 | - return $this->evaluation($rule,$item); |
|
| 382 | - } |
|
| 381 | + return $this->evaluation($rule,$item); |
|
| 382 | + } |
|
| 383 | 383 | |
| 384 | 384 | } |
| 385 | 385 | \ No newline at end of file |
@@ -23,30 +23,30 @@ discard block |
||
| 23 | 23 | * Get full number |
| 24 | 24 | * @return array<number,string> |
| 25 | 25 | */ |
| 26 | - private function get_number($rule,&$item) |
|
| 26 | + private function get_number($rule, &$item) |
|
| 27 | 27 | { |
| 28 | - $item2=$item+1; |
|
| 28 | + $item2=$item + 1; |
|
| 29 | 29 | while ( |
| 30 | - ($item2!=strlen($rule)) |
|
| 31 | - && (preg_match('/[\-0-9\.]/',$rule[$item2]))) |
|
| 30 | + ($item2 != strlen($rule)) |
|
| 31 | + && (preg_match('/[\-0-9\.]/', $rule[$item2]))) |
|
| 32 | 32 | { |
| 33 | - $item2++ ; |
|
| 33 | + $item2++; |
|
| 34 | 34 | } |
| 35 | - $val=substr($rule,$item,$item2-$item); |
|
| 35 | + $val=substr($rule, $item, $item2 - $item); |
|
| 36 | 36 | $item=$item2; |
| 37 | 37 | //echo "number ".$val."\n"; |
| 38 | 38 | |
| 39 | - return array(0,$val); |
|
| 39 | + return array(0, $val); |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | - private function get_string($rule,&$item) |
|
| 42 | + private function get_string($rule, &$item) |
|
| 43 | 43 | { |
| 44 | 44 | $item++; |
| 45 | - $item2=$this->eval_getNext($rule,$item,'"'); |
|
| 46 | - $val=substr($rule,$item,$item2-$item-1); |
|
| 45 | + $item2=$this->eval_getNext($rule, $item, '"'); |
|
| 46 | + $val=substr($rule, $item, $item2 - $item - 1); |
|
| 47 | 47 | $item=$item2; |
| 48 | 48 | //echo "string : ".$val."\n"; |
| 49 | - return array(1,$val); |
|
| 49 | + return array(1, $val); |
|
| 50 | 50 | |
| 51 | 51 | } |
| 52 | 52 | |
@@ -58,20 +58,20 @@ discard block |
||
| 58 | 58 | * @throws Exception |
| 59 | 59 | * @return string : everything inside parenthesis |
| 60 | 60 | */ |
| 61 | - private function parse_parenthesis(string $rule,int &$item) : string |
|
| 61 | + private function parse_parenthesis(string $rule, int &$item) : string |
|
| 62 | 62 | { |
| 63 | 63 | $item++; |
| 64 | 64 | $start=$item; |
| 65 | 65 | $parenthesis_count=0; |
| 66 | 66 | while (($item < strlen($rule)) // Not end of string AND |
| 67 | - && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
| 67 | + && (($rule[$item] != ')') || $parenthesis_count > 0)) // Closing ')' or embeded () |
|
| 68 | 68 | { |
| 69 | - if ($rule[$item] == '"' ) |
|
| 69 | + if ($rule[$item] == '"') |
|
| 70 | 70 | { // pass through string |
| 71 | 71 | $item++; |
| 72 | - $item=$this->eval_getNext($rule,$item,'"'); |
|
| 72 | + $item=$this->eval_getNext($rule, $item, '"'); |
|
| 73 | 73 | } |
| 74 | - else{ |
|
| 74 | + else { |
|
| 75 | 75 | if ($rule[$item] == '(') |
| 76 | 76 | { |
| 77 | 77 | $parenthesis_count++; |
@@ -84,8 +84,8 @@ discard block |
||
| 84 | 84 | } |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | - if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} |
|
| 88 | - $val=substr($rule,$start,$item-$start); |
|
| 87 | + if ($item == strlen($rule)) {throw new Exception("no closing () in ".$rule." at ".$item); } |
|
| 88 | + $val=substr($rule, $start, $item - $start); |
|
| 89 | 89 | $item++; |
| 90 | 90 | return $val; |
| 91 | 91 | } |
@@ -97,45 +97,45 @@ discard block |
||
| 97 | 97 | * @param int $item |
| 98 | 98 | * @return array |
| 99 | 99 | */ |
| 100 | - private function get_group(string $rule,int &$item) : array |
|
| 100 | + private function get_group(string $rule, int &$item) : array |
|
| 101 | 101 | { |
| 102 | 102 | // gets eveything inside parenthesis |
| 103 | 103 | $val=$this->parse_parenthesis($rule, $item); |
| 104 | 104 | // Returns boolean with evaluation of all inside parenthesis |
| 105 | 105 | $start=0; |
| 106 | - return array(2,$this->evaluation($val,$start)); |
|
| 106 | + return array(2, $this->evaluation($val, $start)); |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - private function get_function(string $rule,int &$item) : array |
|
| 109 | + private function get_function(string $rule, int &$item) : array |
|
| 110 | 110 | { |
| 111 | 111 | // function is : __function(param1,param2...) |
| 112 | 112 | $start=$item; |
| 113 | - while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' |
|
| 113 | + while (($item < strlen($rule)) && ($rule[$item] != '(')) // Not end of string AND not opening '(' |
|
| 114 | 114 | { |
| 115 | 115 | $item++; |
| 116 | 116 | } |
| 117 | - if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} |
|
| 117 | + if ($item == strlen($rule)) {throw new Exception("no opening () for function in ".$rule." at ".$item); } |
|
| 118 | 118 | |
| 119 | 119 | // get parameters between parenthesis |
| 120 | 120 | |
| 121 | 121 | $params=$this->parse_parenthesis($rule, $item); |
| 122 | 122 | |
| 123 | - $val=substr($rule,$start,$item-$start); |
|
| 123 | + $val=substr($rule, $start, $item - $start); |
|
| 124 | 124 | |
| 125 | - $this->logging->log('got function ' . $val . ' returning true for now',DEBUG); |
|
| 125 | + $this->logging->log('got function '.$val.' returning true for now', DEBUG); |
|
| 126 | 126 | |
| 127 | - return array(2,true); |
|
| 127 | + return array(2, true); |
|
| 128 | 128 | |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | - protected function eval_getElement($rule,&$item) |
|
| 131 | + protected function eval_getElement($rule, &$item) |
|
| 132 | 132 | { |
| 133 | 133 | if ($item >= strlen($rule)) |
| 134 | 134 | { |
| 135 | - throw new Exception("Early end of string ".$rule ." at " .$item ); |
|
| 135 | + throw new Exception("Early end of string ".$rule." at ".$item); |
|
| 136 | 136 | } |
| 137 | - while ($rule[$item]==' ') $item++; |
|
| 138 | - if (preg_match('/[\-0-9\.]/',$rule[$item])) |
|
| 137 | + while ($rule[$item] == ' ') $item++; |
|
| 138 | + if (preg_match('/[\-0-9\.]/', $rule[$item])) |
|
| 139 | 139 | { // number |
| 140 | 140 | return $this->get_number($rule, $item); |
| 141 | 141 | } |
@@ -152,54 +152,54 @@ discard block |
||
| 152 | 152 | { // function |
| 153 | 153 | return $this->get_function($rule, $item); |
| 154 | 154 | } |
| 155 | - throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]); |
|
| 155 | + throw new Exception("number/string not found in ".$rule." at ".$item.' : '.$rule[$item]); |
|
| 156 | 156 | |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | - protected function eval_getNext($rule,$item,$tok) |
|
| 159 | + protected function eval_getNext($rule, $item, $tok) |
|
| 160 | 160 | { |
| 161 | 161 | while ( |
| 162 | - ($rule[$item] != $tok ) |
|
| 162 | + ($rule[$item] != $tok) |
|
| 163 | 163 | && ($item < strlen($rule))) |
| 164 | 164 | { |
| 165 | 165 | $item++; |
| 166 | 166 | } |
| 167 | - if ($item==strlen($rule)) { |
|
| 168 | - throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
|
| 167 | + if ($item == strlen($rule)) { |
|
| 168 | + throw new Exception("closing '".$tok."' not found in ".$rule." at ".$item); |
|
| 169 | 169 | } |
| 170 | - return $item+1; |
|
| 170 | + return $item + 1; |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | - protected function eval_getOper($rule,&$item) |
|
| 173 | + protected function eval_getOper($rule, &$item) |
|
| 174 | 174 | { |
| 175 | - while ($rule[$item]==' ') $item++; |
|
| 175 | + while ($rule[$item] == ' ') $item++; |
|
| 176 | 176 | switch ($rule[$item]) |
| 177 | 177 | { |
| 178 | 178 | case '<': |
| 179 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"<=");} |
|
| 180 | - $item++; return array(0,"<"); |
|
| 179 | + if ($rule[$item + 1] == '=') { $item+=2; return array(0, "<="); } |
|
| 180 | + $item++; return array(0, "<"); |
|
| 181 | 181 | case '>': |
| 182 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,">=");} |
|
| 183 | - $item++; return array(0,">"); |
|
| 182 | + if ($rule[$item + 1] == '=') { $item+=2; return array(0, ">="); } |
|
| 183 | + $item++; return array(0, ">"); |
|
| 184 | 184 | case '=': |
| 185 | - $item++; return array(0,"="); |
|
| 185 | + $item++; return array(0, "="); |
|
| 186 | 186 | case '!': |
| 187 | - if ($rule[$item+1]=='=') { $item+=2; return array(0,"!=");} |
|
| 188 | - throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule ." at " .$item); |
|
| 187 | + if ($rule[$item + 1] == '=') { $item+=2; return array(0, "!="); } |
|
| 188 | + throw new Exception("Erreur in expr - incorrect operator '!' found in ".$rule." at ".$item); |
|
| 189 | 189 | case '~': |
| 190 | - $item++; return array(0,"~"); |
|
| 190 | + $item++; return array(0, "~"); |
|
| 191 | 191 | case '|': |
| 192 | - $item++; return array(1,"|"); |
|
| 192 | + $item++; return array(1, "|"); |
|
| 193 | 193 | case '&': |
| 194 | - $item++; return array(1,"&"); |
|
| 194 | + $item++; return array(1, "&"); |
|
| 195 | 195 | default : |
| 196 | - throw new Exception("Erreur in expr - operator not found in ".$rule ." at " .$item); |
|
| 196 | + throw new Exception("Erreur in expr - operator not found in ".$rule." at ".$item); |
|
| 197 | 197 | } |
| 198 | 198 | } |
| 199 | 199 | |
| 200 | - private function check_negate_first($rule,&$item) |
|
| 200 | + private function check_negate_first($rule, &$item) |
|
| 201 | 201 | { |
| 202 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
| 202 | + if ($rule[$item] == '!') // If '!' found, negate next expression. |
|
| 203 | 203 | { |
| 204 | 204 | $item++; |
| 205 | 205 | return true; |
@@ -210,21 +210,21 @@ discard block |
||
| 210 | 210 | } |
| 211 | 211 | } |
| 212 | 212 | |
| 213 | - private function do_compare($val1,$val2,$comp,$negate) |
|
| 213 | + private function do_compare($val1, $val2, $comp, $negate) |
|
| 214 | 214 | { |
| 215 | - switch ($comp){ |
|
| 216 | - case '<': $retVal= ($val1 < $val2); break; |
|
| 217 | - case '<=': $retVal= ($val1 <= $val2); break; |
|
| 218 | - case '>': $retVal= ($val1 > $val2); break; |
|
| 219 | - case '>=': $retVal= ($val1 >= $val2); break; |
|
| 220 | - case '=': $retVal= ($val1 == $val2); break; |
|
| 221 | - case '!=': $retVal= ($val1 != $val2); break; |
|
| 222 | - case '~': $retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break; |
|
| 223 | - case '|': $retVal= ($val1 || $val2); break; |
|
| 224 | - case '&': $retVal= ($val1 && $val2); break; |
|
| 215 | + switch ($comp) { |
|
| 216 | + case '<': $retVal=($val1 < $val2); break; |
|
| 217 | + case '<=': $retVal=($val1 <= $val2); break; |
|
| 218 | + case '>': $retVal=($val1 > $val2); break; |
|
| 219 | + case '>=': $retVal=($val1 >= $val2); break; |
|
| 220 | + case '=': $retVal=($val1 == $val2); break; |
|
| 221 | + case '!=': $retVal=($val1 != $val2); break; |
|
| 222 | + case '~': $retVal=(preg_match('/'.preg_replace('/"/', '', $val2).'/', $val1)); break; |
|
| 223 | + case '|': $retVal=($val1 || $val2); break; |
|
| 224 | + case '&': $retVal=($val1 && $val2); break; |
|
| 225 | 225 | default: throw new Exception("Error in expression - unknown comp : ".$comp); |
| 226 | 226 | } |
| 227 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
| 227 | + if ($negate === true) $retVal=!$retVal; // Inverse result if negate before expression |
|
| 228 | 228 | |
| 229 | 229 | return $retVal; |
| 230 | 230 | } |
@@ -237,56 +237,56 @@ discard block |
||
| 237 | 237 | * comparison int vs strings will return null (error) |
| 238 | 238 | * return : bool or null on error |
| 239 | 239 | */ |
| 240 | - public function evaluation($rule,&$item) |
|
| 240 | + public function evaluation($rule, &$item) |
|
| 241 | 241 | { |
| 242 | 242 | //echo "Evaluation of ".substr($rule,$item)."\n"; |
| 243 | 243 | $negate=$this->check_negate_first($rule, $item); |
| 244 | 244 | // First element : number, string or () |
| 245 | - list($type1,$val1) = $this->eval_getElement($rule,$item); |
|
| 245 | + list($type1, $val1)=$this->eval_getElement($rule, $item); |
|
| 246 | 246 | //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
| 247 | 247 | |
| 248 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
| 248 | + if ($item == strlen($rule)) // If only element, return value, but only boolean |
|
| 249 | 249 | { |
| 250 | 250 | if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
| 251 | - if ($negate === true) $val1= ! $val1; |
|
| 251 | + if ($negate === true) $val1=!$val1; |
|
| 252 | 252 | return $val1; |
| 253 | 253 | } |
| 254 | 254 | |
| 255 | 255 | // Second element : operator |
| 256 | - list($typec,$comp) = $this->eval_getOper($rule,$item); |
|
| 256 | + list($typec, $comp)=$this->eval_getOper($rule, $item); |
|
| 257 | 257 | //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
| 258 | 258 | |
| 259 | 259 | // Third element : number, string or () |
| 260 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
| 260 | + if ($rule[$item] == '!') // starts with a ! so evaluate whats next |
|
| 261 | 261 | { |
| 262 | 262 | $item++; |
| 263 | 263 | if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
| 264 | - $val2= ! $this->evaluation($rule,$item); |
|
| 264 | + $val2=!$this->evaluation($rule, $item); |
|
| 265 | 265 | $type2=2; // result is a boolean |
| 266 | 266 | } |
| 267 | 267 | else |
| 268 | 268 | { |
| 269 | - list($type2,$val2) = $this->eval_getElement($rule,$item); |
|
| 269 | + list($type2, $val2)=$this->eval_getElement($rule, $item); |
|
| 270 | 270 | } |
| 271 | 271 | //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
| 272 | 272 | |
| 273 | - if ($type1!=$type2) // cannot compare different types |
|
| 273 | + if ($type1 != $type2) // cannot compare different types |
|
| 274 | 274 | { |
| 275 | 275 | throw new Exception("Cannot compare string & number : ".$rule); |
| 276 | 276 | } |
| 277 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
| 277 | + if ($typec == 1 && $type1 != 2) // cannot use & or | with string/number |
|
| 278 | 278 | { |
| 279 | 279 | throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
| 280 | 280 | } |
| 281 | 281 | |
| 282 | - $retVal = $this->do_compare($val1, $val2, $comp, $negate); |
|
| 282 | + $retVal=$this->do_compare($val1, $val2, $comp, $negate); |
|
| 283 | 283 | |
| 284 | - if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
| 284 | + if ($item == strlen($rule)) return $retVal; // End of string : return evaluation |
|
| 285 | 285 | // check for logical operator : |
| 286 | 286 | switch ($rule[$item]) |
| 287 | 287 | { |
| 288 | - case '|': $item++; return ($retVal || $this->evaluation($rule,$item) ); |
|
| 289 | - case '&': $item++; return ($retVal && $this->evaluation($rule,$item) ); |
|
| 288 | + case '|': $item++; return ($retVal || $this->evaluation($rule, $item)); |
|
| 289 | + case '&': $item++; return ($retVal && $this->evaluation($rule, $item)); |
|
| 290 | 290 | |
| 291 | 291 | default: throw new Exception("Erreur in expr - garbadge at end of expression : ".$rule[$item]); |
| 292 | 292 | } |
@@ -299,17 +299,17 @@ discard block |
||
| 299 | 299 | $rule2=''; |
| 300 | 300 | while ($item < strlen($rule)) |
| 301 | 301 | { |
| 302 | - if ($rule[$item]==' ') { $item++; continue; } |
|
| 303 | - if ($rule[$item]=='"') |
|
| 302 | + if ($rule[$item] == ' ') { $item++; continue; } |
|
| 303 | + if ($rule[$item] == '"') |
|
| 304 | 304 | { |
| 305 | 305 | $rule2.=$rule[$item]; |
| 306 | 306 | $item++; |
| 307 | - while (($item < strlen($rule)) && ($rule[$item]!='"') ) |
|
| 307 | + while (($item < strlen($rule)) && ($rule[$item] != '"')) |
|
| 308 | 308 | { |
| 309 | 309 | $rule2.=$rule[$item]; |
| 310 | 310 | $item++; |
| 311 | 311 | } |
| 312 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
| 312 | + if ($item == strlen($rule)) throw new Exception("closing '\"' not found in ".$rule." at ".$item); |
|
| 313 | 313 | $rule2.=$rule[$item]; |
| 314 | 314 | $item++; |
| 315 | 315 | continue; |
@@ -328,14 +328,14 @@ discard block |
||
| 328 | 328 | * @return bool : true : rule match, false : rule don't match , throw exception on error. |
| 329 | 329 | */ |
| 330 | 330 | |
| 331 | - public function eval_rule($rule,$oidList) |
|
| 331 | + public function eval_rule($rule, $oidList) |
|
| 332 | 332 | { |
| 333 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
| 333 | + if ($rule == null || $rule == '') // Empty rule is always true |
|
| 334 | 334 | { |
| 335 | 335 | return true; |
| 336 | 336 | } |
| 337 | 337 | $matches=array(); |
| 338 | - while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
|
| 338 | + while (preg_match('/_OID\(([0-9\.\*]+)\)/', $rule, $matches) == 1) |
|
| 339 | 339 | { |
| 340 | 340 | $oid=$matches[1]; |
| 341 | 341 | $found=0; |
@@ -347,38 +347,38 @@ discard block |
||
| 347 | 347 | // replace * with \* in oid for preg_replace |
| 348 | 348 | $oid=preg_replace('/\*/', '\*', $oid); |
| 349 | 349 | |
| 350 | - $this->logging->log('OID in rule : '.$oid.' / '.$oidR,DEBUG ); |
|
| 350 | + $this->logging->log('OID in rule : '.$oid.' / '.$oidR, DEBUG); |
|
| 351 | 351 | |
| 352 | - foreach($oidList as $val) |
|
| 352 | + foreach ($oidList as $val) |
|
| 353 | 353 | { |
| 354 | - if (preg_match("/^$oidR$/",$val->oid) == 1) |
|
| 354 | + if (preg_match("/^$oidR$/", $val->oid) == 1) |
|
| 355 | 355 | { |
| 356 | - if (!preg_match('/^-?[0-9]*\.?[0-9]+$/',$val->value)) |
|
| 356 | + if (!preg_match('/^-?[0-9]*\.?[0-9]+$/', $val->value)) |
|
| 357 | 357 | { // If not a number, change " to ' and put " around it |
| 358 | - $val->value=preg_replace('/"/',"'",$val->value); |
|
| 358 | + $val->value=preg_replace('/"/', "'", $val->value); |
|
| 359 | 359 | $val->value='"'.$val->value.'"'; |
| 360 | 360 | } |
| 361 | 361 | $rep=0; |
| 362 | - $rule=preg_replace('/_OID\('.$oid.'\)/',$val->value,$rule,-1,$rep); |
|
| 363 | - if ($rep==0) |
|
| 362 | + $rule=preg_replace('/_OID\('.$oid.'\)/', $val->value, $rule, -1, $rep); |
|
| 363 | + if ($rep == 0) |
|
| 364 | 364 | { |
| 365 | - $this->logging->log("Error in rule_eval",WARN,''); |
|
| 365 | + $this->logging->log("Error in rule_eval", WARN, ''); |
|
| 366 | 366 | return false; |
| 367 | 367 | } |
| 368 | 368 | $found=1; |
| 369 | 369 | break; |
| 370 | 370 | } |
| 371 | 371 | } |
| 372 | - if ($found==0) |
|
| 372 | + if ($found == 0) |
|
| 373 | 373 | { // OID not found : throw error |
| 374 | 374 | throw new Exception('OID '.$oid.' not found in trap'); |
| 375 | 375 | } |
| 376 | 376 | } |
| 377 | 377 | $item=0; |
| 378 | 378 | $rule=$this->eval_cleanup($rule); |
| 379 | - $this->logging->log('Rule after clenup: '.$rule,INFO ); |
|
| 379 | + $this->logging->log('Rule after clenup: '.$rule, INFO); |
|
| 380 | 380 | |
| 381 | - return $this->evaluation($rule,$item); |
|
| 381 | + return $this->evaluation($rule, $item); |
|
| 382 | 382 | } |
| 383 | 383 | |
| 384 | 384 | } |
| 385 | 385 | \ No newline at end of file |
@@ -64,14 +64,15 @@ discard block |
||
| 64 | 64 | $start=$item; |
| 65 | 65 | $parenthesis_count=0; |
| 66 | 66 | while (($item < strlen($rule)) // Not end of string AND |
| 67 | - && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
|
| 67 | + && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) { |
|
| 68 | + // Closing ')' or embeded () |
|
| 68 | 69 | { |
| 69 | 70 | if ($rule[$item] == '"' ) |
| 70 | 71 | { // pass through string |
| 71 | 72 | $item++; |
| 73 | + } |
|
| 72 | 74 | $item=$this->eval_getNext($rule,$item,'"'); |
| 73 | - } |
|
| 74 | - else{ |
|
| 75 | + } else{ |
|
| 75 | 76 | if ($rule[$item] == '(') |
| 76 | 77 | { |
| 77 | 78 | $parenthesis_count++; |
@@ -110,9 +111,11 @@ discard block |
||
| 110 | 111 | { |
| 111 | 112 | // function is : __function(param1,param2...) |
| 112 | 113 | $start=$item; |
| 113 | - while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' |
|
| 114 | + while (($item < strlen($rule)) && ($rule[$item] != '(' )) { |
|
| 115 | + // Not end of string AND not opening '(' |
|
| 114 | 116 | { |
| 115 | 117 | $item++; |
| 118 | + } |
|
| 116 | 119 | } |
| 117 | 120 | if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} |
| 118 | 121 | |
@@ -134,7 +137,9 @@ discard block |
||
| 134 | 137 | { |
| 135 | 138 | throw new Exception("Early end of string ".$rule ." at " .$item ); |
| 136 | 139 | } |
| 137 | - while ($rule[$item]==' ') $item++; |
|
| 140 | + while ($rule[$item]==' ') { |
|
| 141 | + $item++; |
|
| 142 | + } |
|
| 138 | 143 | if (preg_match('/[\-0-9\.]/',$rule[$item])) |
| 139 | 144 | { // number |
| 140 | 145 | return $this->get_number($rule, $item); |
@@ -172,7 +177,9 @@ discard block |
||
| 172 | 177 | |
| 173 | 178 | protected function eval_getOper($rule,&$item) |
| 174 | 179 | { |
| 175 | - while ($rule[$item]==' ') $item++; |
|
| 180 | + while ($rule[$item]==' ') { |
|
| 181 | + $item++; |
|
| 182 | + } |
|
| 176 | 183 | switch ($rule[$item]) |
| 177 | 184 | { |
| 178 | 185 | case '<': |
@@ -199,12 +206,13 @@ discard block |
||
| 199 | 206 | |
| 200 | 207 | private function check_negate_first($rule,&$item) |
| 201 | 208 | { |
| 202 | - if ( $rule[$item] == '!') // If '!' found, negate next expression. |
|
| 209 | + if ( $rule[$item] == '!') { |
|
| 210 | + // If '!' found, negate next expression. |
|
| 203 | 211 | { |
| 204 | 212 | $item++; |
| 205 | - return true; |
|
| 206 | 213 | } |
| 207 | - else |
|
| 214 | + return true; |
|
| 215 | + } else |
|
| 208 | 216 | { |
| 209 | 217 | return false; |
| 210 | 218 | } |
@@ -224,7 +232,10 @@ discard block |
||
| 224 | 232 | case '&': $retVal= ($val1 && $val2); break; |
| 225 | 233 | default: throw new Exception("Error in expression - unknown comp : ".$comp); |
| 226 | 234 | } |
| 227 | - if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
|
| 235 | + if ($negate === true) { |
|
| 236 | + $retVal = ! $retVal; |
|
| 237 | + } |
|
| 238 | + // Inverse result if negate before expression |
|
| 228 | 239 | |
| 229 | 240 | return $retVal; |
| 230 | 241 | } |
@@ -245,10 +256,14 @@ discard block |
||
| 245 | 256 | list($type1,$val1) = $this->eval_getElement($rule,$item); |
| 246 | 257 | //echo "Elmt1: ".$val1."/".$type1." : ".substr($rule,$item)."\n"; |
| 247 | 258 | |
| 248 | - if ($item==strlen($rule)) // If only element, return value, but only boolean |
|
| 259 | + if ($item==strlen($rule)) { |
|
| 260 | + // If only element, return value, but only boolean |
|
| 249 | 261 | { |
| 250 | 262 | if ($type1 != 2) throw new Exception("Cannot use num/string as boolean : ".$rule); |
| 251 | - if ($negate === true) $val1= ! $val1; |
|
| 263 | + } |
|
| 264 | + if ($negate === true) { |
|
| 265 | + $val1= ! $val1; |
|
| 266 | + } |
|
| 252 | 267 | return $val1; |
| 253 | 268 | } |
| 254 | 269 | |
@@ -257,31 +272,41 @@ discard block |
||
| 257 | 272 | //echo "Comp : ".$comp." : ".substr($rule,$item)."\n"; |
| 258 | 273 | |
| 259 | 274 | // Third element : number, string or () |
| 260 | - if ( $rule[$item] == '!') // starts with a ! so evaluate whats next |
|
| 275 | + if ( $rule[$item] == '!') { |
|
| 276 | + // starts with a ! so evaluate whats next |
|
| 261 | 277 | { |
| 262 | 278 | $item++; |
| 263 | - if ($typec != 1) throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 279 | + } |
|
| 280 | + if ($typec != 1) { |
|
| 281 | + throw new Exception("Mixing boolean and comparison : ".$rule); |
|
| 282 | + } |
|
| 264 | 283 | $val2= ! $this->evaluation($rule,$item); |
| 265 | 284 | $type2=2; // result is a boolean |
| 266 | - } |
|
| 267 | - else |
|
| 285 | + } else |
|
| 268 | 286 | { |
| 269 | 287 | list($type2,$val2) = $this->eval_getElement($rule,$item); |
| 270 | 288 | } |
| 271 | 289 | //echo "Elmt2: ".$val2."/".$type2." : ".substr($rule,$item)."\n"; |
| 272 | 290 | |
| 273 | - if ($type1!=$type2) // cannot compare different types |
|
| 291 | + if ($type1!=$type2) { |
|
| 292 | + // cannot compare different types |
|
| 274 | 293 | { |
| 275 | 294 | throw new Exception("Cannot compare string & number : ".$rule); |
| 276 | 295 | } |
| 277 | - if ($typec==1 && $type1 !=2) // cannot use & or | with string/number |
|
| 296 | + } |
|
| 297 | + if ($typec==1 && $type1 !=2) { |
|
| 298 | + // cannot use & or | with string/number |
|
| 278 | 299 | { |
| 279 | 300 | throw new Exception("Cannot use boolean operators with string & number : ".$rule); |
| 280 | 301 | } |
| 302 | + } |
|
| 281 | 303 | |
| 282 | 304 | $retVal = $this->do_compare($val1, $val2, $comp, $negate); |
| 283 | 305 | |
| 284 | - if ($item==strlen($rule)) return $retVal; // End of string : return evaluation |
|
| 306 | + if ($item==strlen($rule)) { |
|
| 307 | + return $retVal; |
|
| 308 | + } |
|
| 309 | + // End of string : return evaluation |
|
| 285 | 310 | // check for logical operator : |
| 286 | 311 | switch ($rule[$item]) |
| 287 | 312 | { |
@@ -309,7 +334,9 @@ discard block |
||
| 309 | 334 | $rule2.=$rule[$item]; |
| 310 | 335 | $item++; |
| 311 | 336 | } |
| 312 | - if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
| 337 | + if ($item == strlen ($rule)) { |
|
| 338 | + throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
|
| 339 | + } |
|
| 313 | 340 | $rule2.=$rule[$item]; |
| 314 | 341 | $item++; |
| 315 | 342 | continue; |
@@ -330,10 +357,12 @@ discard block |
||
| 330 | 357 | |
| 331 | 358 | public function eval_rule($rule,$oidList) |
| 332 | 359 | { |
| 333 | - if ($rule==null || $rule == '') // Empty rule is always true |
|
| 360 | + if ($rule==null || $rule == '') { |
|
| 361 | + // Empty rule is always true |
|
| 334 | 362 | { |
| 335 | 363 | return true; |
| 336 | 364 | } |
| 365 | + } |
|
| 337 | 366 | $matches=array(); |
| 338 | 367 | while (preg_match('/_OID\(([0-9\.\*]+)\)/',$rule,$matches) == 1) |
| 339 | 368 | { |
@@ -21,89 +21,89 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class NetworkRule extends PluginTemplate |
| 23 | 23 | { |
| 24 | - /** @var string $description Description of plugin */ |
|
| 25 | - public $description='Network functions to use into rules |
|
| 24 | + /** @var string $description Description of plugin */ |
|
| 25 | + public $description='Network functions to use into rules |
|
| 26 | 26 | test test test'; |
| 27 | 27 | |
| 28 | - /** @var array[] $functions Functions of this plugin for rule eval. |
|
| 29 | - * If no functions are declared, set to empty array |
|
| 30 | - * $functions[<name>]['function'] : Name of the function to be called in this class |
|
| 31 | - * $functions[<name>]['params'] : Description of input parameters of function. |
|
| 32 | - * $functions[<name>]['description'] : Description. Can be multiline. |
|
| 33 | - */ |
|
| 34 | - public $functions=array( |
|
| 35 | - 'inNetwork' => array( // The name of the function in rules |
|
| 36 | - 'function' => 'isInNetwork', // Name of the function |
|
| 37 | - 'params' => '<IP to test>,<Network IP>,<Network mask (CIDR)>', // parameters description |
|
| 38 | - 'description' => 'Test if IP is in network, ex : __inNetwork(192.168.123.5,192.168.123.0,24) returns true |
|
| 28 | + /** @var array[] $functions Functions of this plugin for rule eval. |
|
| 29 | + * If no functions are declared, set to empty array |
|
| 30 | + * $functions[<name>]['function'] : Name of the function to be called in this class |
|
| 31 | + * $functions[<name>]['params'] : Description of input parameters of function. |
|
| 32 | + * $functions[<name>]['description'] : Description. Can be multiline. |
|
| 33 | + */ |
|
| 34 | + public $functions=array( |
|
| 35 | + 'inNetwork' => array( // The name of the function in rules |
|
| 36 | + 'function' => 'isInNetwork', // Name of the function |
|
| 37 | + 'params' => '<IP to test>,<Network IP>,<Network mask (CIDR)>', // parameters description |
|
| 38 | + 'description' => 'Test if IP is in network, ex : __inNetwork(192.168.123.5,192.168.123.0,24) returns true |
|
| 39 | 39 | Does not work with IPV6' // Description (can be multiline). |
| 40 | - ) |
|
| 41 | - ); |
|
| 40 | + ) |
|
| 41 | + ); |
|
| 42 | 42 | |
| 43 | - /** @var boolean $catchAllTraps Set to true if all traps will be sent to the plugin NOT IMPLEMENTED */ |
|
| 44 | - public $catchAllTraps=false; |
|
| 43 | + /** @var boolean $catchAllTraps Set to true if all traps will be sent to the plugin NOT IMPLEMENTED */ |
|
| 44 | + public $catchAllTraps=false; |
|
| 45 | 45 | |
| 46 | - /** @var boolean $processTraps Set to true if plugins can handle traps NOT IMPLEMENTED */ |
|
| 47 | - public $processTraps=false; |
|
| 46 | + /** @var boolean $processTraps Set to true if plugins can handle traps NOT IMPLEMENTED */ |
|
| 47 | + public $processTraps=false; |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Constructor. Can throw exceptions on error, but no logging at this point. |
|
| 51 | - * @throws \Exception |
|
| 52 | - * @return \Trapdirector\Plugins\NetworkRule |
|
| 53 | - */ |
|
| 54 | - function __construct() |
|
| 55 | - { |
|
| 56 | - $this->name=basename(__FILE__,'.php'); |
|
| 57 | - return $this; |
|
| 58 | - } |
|
| 49 | + /** |
|
| 50 | + * Constructor. Can throw exceptions on error, but no logging at this point. |
|
| 51 | + * @throws \Exception |
|
| 52 | + * @return \Trapdirector\Plugins\NetworkRule |
|
| 53 | + */ |
|
| 54 | + function __construct() |
|
| 55 | + { |
|
| 56 | + $this->name=basename(__FILE__,'.php'); |
|
| 57 | + return $this; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Function called by trapdirector if found in rules |
|
| 62 | - * Parameters check has to be done in function. |
|
| 63 | - * @param array $params Function parameters |
|
| 64 | - * @throws Exception |
|
| 65 | - * @return bool Evaluation |
|
| 66 | - */ |
|
| 67 | - public function isInNetwork(array $params) : bool |
|
| 68 | - { |
|
| 69 | - // Check param numbers and thrown exception if not correct. |
|
| 70 | - if (count($params)!=3) |
|
| 71 | - { |
|
| 72 | - throw new Exception('Invalid number of parameters : ' . count($params)); |
|
| 73 | - } |
|
| 60 | + /** |
|
| 61 | + * Function called by trapdirector if found in rules |
|
| 62 | + * Parameters check has to be done in function. |
|
| 63 | + * @param array $params Function parameters |
|
| 64 | + * @throws Exception |
|
| 65 | + * @return bool Evaluation |
|
| 66 | + */ |
|
| 67 | + public function isInNetwork(array $params) : bool |
|
| 68 | + { |
|
| 69 | + // Check param numbers and thrown exception if not correct. |
|
| 70 | + if (count($params)!=3) |
|
| 71 | + { |
|
| 72 | + throw new Exception('Invalid number of parameters : ' . count($params)); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - $ip = $params[0]; |
|
| 76 | - $net = $params[1]; |
|
| 77 | - $masq = $params[2]; |
|
| 75 | + $ip = $params[0]; |
|
| 76 | + $net = $params[1]; |
|
| 77 | + $masq = $params[2]; |
|
| 78 | 78 | |
| 79 | 79 | |
| 80 | - $this->log('#'. $ip . '# / #' . $net . '# / #' . $masq,DEBUG); |
|
| 80 | + $this->log('#'. $ip . '# / #' . $net . '# / #' . $masq,DEBUG); |
|
| 81 | 81 | |
| 82 | - $ip2 = ip2long($ip); |
|
| 83 | - $net2 = ip2long($net); |
|
| 82 | + $ip2 = ip2long($ip); |
|
| 83 | + $net2 = ip2long($net); |
|
| 84 | 84 | |
| 85 | - if ($ip2 === false ) |
|
| 86 | - { |
|
| 87 | - $this->log('Invalid IP : #' . $ip.'#',WARN); |
|
| 88 | - throw new Exception('Invalid IP'); |
|
| 89 | - } |
|
| 90 | - if ($net2 === false) |
|
| 91 | - { |
|
| 92 | - $this->log('Invalid network',WARN); |
|
| 93 | - throw new Exception('Invalid net'); |
|
| 94 | - } |
|
| 95 | - if ($masq<1 || $masq > 32) |
|
| 96 | - { |
|
| 97 | - $this->log('Invalid masq',WARN); |
|
| 98 | - throw new Exception('Invalid net masq'); |
|
| 99 | - } |
|
| 100 | - // $range is in IP/CIDR format eg 127.0.0.1/24 |
|
| 85 | + if ($ip2 === false ) |
|
| 86 | + { |
|
| 87 | + $this->log('Invalid IP : #' . $ip.'#',WARN); |
|
| 88 | + throw new Exception('Invalid IP'); |
|
| 89 | + } |
|
| 90 | + if ($net2 === false) |
|
| 91 | + { |
|
| 92 | + $this->log('Invalid network',WARN); |
|
| 93 | + throw new Exception('Invalid net'); |
|
| 94 | + } |
|
| 95 | + if ($masq<1 || $masq > 32) |
|
| 96 | + { |
|
| 97 | + $this->log('Invalid masq',WARN); |
|
| 98 | + throw new Exception('Invalid net masq'); |
|
| 99 | + } |
|
| 100 | + // $range is in IP/CIDR format eg 127.0.0.1/24 |
|
| 101 | 101 | |
| 102 | - $masq = pow( 2, ( 32 - $masq ) ) - 1; |
|
| 103 | - $masq = ~ $masq; |
|
| 104 | - return ( ( $ip2 & $masq ) == ( $net2 & $masq ) ); |
|
| 102 | + $masq = pow( 2, ( 32 - $masq ) ) - 1; |
|
| 103 | + $masq = ~ $masq; |
|
| 104 | + return ( ( $ip2 & $masq ) == ( $net2 & $masq ) ); |
|
| 105 | 105 | |
| 106 | - } |
|
| 106 | + } |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | */ |
| 54 | 54 | function __construct() |
| 55 | 55 | { |
| 56 | - $this->name=basename(__FILE__,'.php'); |
|
| 56 | + $this->name=basename(__FILE__, '.php'); |
|
| 57 | 57 | return $this; |
| 58 | 58 | } |
| 59 | 59 | |
@@ -67,41 +67,41 @@ discard block |
||
| 67 | 67 | public function isInNetwork(array $params) : bool |
| 68 | 68 | { |
| 69 | 69 | // Check param numbers and thrown exception if not correct. |
| 70 | - if (count($params)!=3) |
|
| 70 | + if (count($params) != 3) |
|
| 71 | 71 | { |
| 72 | - throw new Exception('Invalid number of parameters : ' . count($params)); |
|
| 72 | + throw new Exception('Invalid number of parameters : '.count($params)); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | - $ip = $params[0]; |
|
| 76 | - $net = $params[1]; |
|
| 77 | - $masq = $params[2]; |
|
| 75 | + $ip=$params[0]; |
|
| 76 | + $net=$params[1]; |
|
| 77 | + $masq=$params[2]; |
|
| 78 | 78 | |
| 79 | 79 | |
| 80 | - $this->log('#'. $ip . '# / #' . $net . '# / #' . $masq,DEBUG); |
|
| 80 | + $this->log('#'.$ip.'# / #'.$net.'# / #'.$masq, DEBUG); |
|
| 81 | 81 | |
| 82 | - $ip2 = ip2long($ip); |
|
| 83 | - $net2 = ip2long($net); |
|
| 82 | + $ip2=ip2long($ip); |
|
| 83 | + $net2=ip2long($net); |
|
| 84 | 84 | |
| 85 | - if ($ip2 === false ) |
|
| 85 | + if ($ip2 === false) |
|
| 86 | 86 | { |
| 87 | - $this->log('Invalid IP : #' . $ip.'#',WARN); |
|
| 87 | + $this->log('Invalid IP : #'.$ip.'#', WARN); |
|
| 88 | 88 | throw new Exception('Invalid IP'); |
| 89 | 89 | } |
| 90 | 90 | if ($net2 === false) |
| 91 | 91 | { |
| 92 | - $this->log('Invalid network',WARN); |
|
| 92 | + $this->log('Invalid network', WARN); |
|
| 93 | 93 | throw new Exception('Invalid net'); |
| 94 | 94 | } |
| 95 | - if ($masq<1 || $masq > 32) |
|
| 95 | + if ($masq < 1 || $masq > 32) |
|
| 96 | 96 | { |
| 97 | - $this->log('Invalid masq',WARN); |
|
| 97 | + $this->log('Invalid masq', WARN); |
|
| 98 | 98 | throw new Exception('Invalid net masq'); |
| 99 | 99 | } |
| 100 | 100 | // $range is in IP/CIDR format eg 127.0.0.1/24 |
| 101 | 101 | |
| 102 | - $masq = pow( 2, ( 32 - $masq ) ) - 1; |
|
| 103 | - $masq = ~ $masq; |
|
| 104 | - return ( ( $ip2 & $masq ) == ( $net2 & $masq ) ); |
|
| 102 | + $masq=pow(2, (32 - $masq)) - 1; |
|
| 103 | + $masq=~ $masq; |
|
| 104 | + return (($ip2 & $masq) == ($net2 & $masq)); |
|
| 105 | 105 | |
| 106 | 106 | } |
| 107 | 107 | } |