Condition.php
7 years ago
Factory.php
7 years ago
Manager.php
7 years ago
Token.php
7 years ago
Validator.php
7 years ago
Condition.php
428 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * ====================================================================== |
| 5 | * LICENSE: This file is subject to the terms and conditions defined in * |
| 6 | * file 'license.txt', which is part of this source code package. * |
| 7 | * ====================================================================== |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * AAM core policy condition evaluator |
| 12 | * |
| 13 | * @package AAM |
| 14 | * @author Vasyl Martyniuk <vasyl@vasyltech.com> |
| 15 | * @since AAM v5.8.2 |
| 16 | */ |
| 17 | final class AAM_Core_Policy_Condition { |
| 18 | |
| 19 | /** |
| 20 | * Single instance of itself |
| 21 | * |
| 22 | * @var AAM_Core_Policy_Condition |
| 23 | * |
| 24 | * @access protected |
| 25 | * @static |
| 26 | */ |
| 27 | protected static $instance = null; |
| 28 | |
| 29 | /** |
| 30 | * Map between condition type and method that evaluates the |
| 31 | * group of conditions |
| 32 | * |
| 33 | * @var array |
| 34 | * |
| 35 | * @access protected |
| 36 | */ |
| 37 | protected $map = array( |
| 38 | 'between' => 'evaluateBetweenConditions', |
| 39 | 'equals' => 'evaluateEqualsConditions', |
| 40 | 'notequals' => 'evaluateNotEqualsConditions', |
| 41 | 'greater' => 'evaluateGreaterConditions', |
| 42 | 'less' => 'evaluateLessConditions', |
| 43 | 'greaterorequals' => 'evaluateGreaterOrEqualsConditions', |
| 44 | 'lessorequals' => 'evaluateLessOrEqualsConditions', |
| 45 | 'in' => 'evaluateInConditions', |
| 46 | 'notin' => 'evaluateNotInConditions', |
| 47 | 'like' => 'evaluateLikeConditions', |
| 48 | 'notlike' => 'evaluateNotLikeConditions', |
| 49 | 'regex' => 'evaluateRegexConditions' |
| 50 | ); |
| 51 | |
| 52 | /** |
| 53 | * Constructor |
| 54 | * |
| 55 | * @return void |
| 56 | * |
| 57 | * @access protected |
| 58 | */ |
| 59 | protected function __construct() {} |
| 60 | |
| 61 | /** |
| 62 | * Evaluate the group of conditions based on type |
| 63 | * |
| 64 | * @param array $conditions List of conditions |
| 65 | * @param array $args Since 5.9 - Inline args for evaluation |
| 66 | * |
| 67 | * @return boolean |
| 68 | * |
| 69 | * @access public |
| 70 | */ |
| 71 | public function evaluate($conditions, $args = array()) { |
| 72 | $result = true; |
| 73 | |
| 74 | foreach($conditions as $type => $conditions) { |
| 75 | $type = strtolower($type); |
| 76 | |
| 77 | if (isset($this->map[$type])) { |
| 78 | $callback = array($this, $this->map[$type]); |
| 79 | $result = $result && call_user_func($callback, $conditions, $args); |
| 80 | } else { |
| 81 | $result = false; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return $result; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Evaluate group of BETWEEN conditions |
| 90 | * |
| 91 | * @param array $conditions |
| 92 | * @param array $args |
| 93 | * |
| 94 | * @return boolean |
| 95 | * |
| 96 | * @access protected |
| 97 | */ |
| 98 | protected function evaluateBetweenConditions($conditions, $args) { |
| 99 | $result = false; |
| 100 | |
| 101 | foreach($this->prepareConditions($conditions, $args) as $condition) { |
| 102 | foreach((array)$condition['right'] as $subset) { |
| 103 | $min = (is_array($subset) ? array_shift($subset) : $subset); |
| 104 | $max = (is_array($subset) ? end($subset) : $subset); |
| 105 | |
| 106 | $result = $result || ($condition['left'] >= $min && $condition['left'] <= $max); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return $result; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Evaluate group of EQUALS conditions |
| 115 | * |
| 116 | * The values have to be identical |
| 117 | * |
| 118 | * @param array $conditions |
| 119 | * @param array $args |
| 120 | * |
| 121 | * @return boolean |
| 122 | * |
| 123 | * @access protected |
| 124 | */ |
| 125 | protected function evaluateEqualsConditions($conditions, $args) { |
| 126 | $result = false; |
| 127 | |
| 128 | foreach($this->prepareConditions($conditions, $args) as $condition) { |
| 129 | $result = $result || ($condition['left'] === $condition['right']); |
| 130 | } |
| 131 | |
| 132 | return $result; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Evaluate group of NOT EQUALs conditions |
| 137 | * |
| 138 | * @param array $conditions |
| 139 | * @param array $args |
| 140 | * |
| 141 | * @return boolean |
| 142 | * |
| 143 | * @access protected |
| 144 | */ |
| 145 | protected function evaluateNotEqualsConditions($conditions, $args) { |
| 146 | return !$this->evaluateEqualsConditions($conditions, $args); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Evaluate group of GREATER THEN conditions |
| 151 | * |
| 152 | * @param array $conditions |
| 153 | * @param array $args |
| 154 | * |
| 155 | * @return boolean |
| 156 | * |
| 157 | * @access protected |
| 158 | */ |
| 159 | protected function evaluateGreaterConditions($conditions, $args) { |
| 160 | $result = false; |
| 161 | |
| 162 | foreach($this->prepareConditions($conditions, $args) as $condition) { |
| 163 | $result = $result || ($condition['left'] > $condition['right']); |
| 164 | } |
| 165 | |
| 166 | return $result; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Evaluate group of LESS THEN conditions |
| 171 | * |
| 172 | * @param array $conditions |
| 173 | * @param array $args |
| 174 | * |
| 175 | * @return boolean |
| 176 | * |
| 177 | * @access protected |
| 178 | */ |
| 179 | protected function evaluateLessConditions($conditions, $args) { |
| 180 | $result = false; |
| 181 | |
| 182 | foreach($this->prepareConditions($conditions, $args) as $condition) { |
| 183 | $result = $result || ($condition['left'] < $condition['right']); |
| 184 | } |
| 185 | |
| 186 | return $result; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Evaluate group of GREATER OR EQUALS THEN conditions |
| 191 | * |
| 192 | * @param array $conditions |
| 193 | * @param array $args |
| 194 | * |
| 195 | * @return boolean |
| 196 | * |
| 197 | * @access protected |
| 198 | */ |
| 199 | protected function evaluateGreaterOrEqualsConditions($conditions, $args) { |
| 200 | $result = false; |
| 201 | |
| 202 | foreach($this->prepareConditions($conditions, $args) as $condition) { |
| 203 | $result = $result || ($condition['left'] >= $condition['right']); |
| 204 | } |
| 205 | |
| 206 | return $result; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Evaluate group of LESS OR EQUALS THEN conditions |
| 211 | * |
| 212 | * @param array $conditions |
| 213 | * @param array $args |
| 214 | * |
| 215 | * @return boolean |
| 216 | * |
| 217 | * @access protected |
| 218 | */ |
| 219 | protected function evaluateLessOrEqualsConditions($conditions, $args) { |
| 220 | $result = false; |
| 221 | |
| 222 | foreach($this->prepareConditions($conditions, $args) as $condition) { |
| 223 | $result = $result || ($condition['left'] <= $condition['right']); |
| 224 | } |
| 225 | |
| 226 | return $result; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Evaluate group of IN conditions |
| 231 | * |
| 232 | * @param array $conditions |
| 233 | * @param array $args |
| 234 | * |
| 235 | * @return boolean |
| 236 | * |
| 237 | * @access protected |
| 238 | */ |
| 239 | protected function evaluateInConditions($conditions, $args) { |
| 240 | $result = false; |
| 241 | |
| 242 | foreach($this->prepareConditions($conditions, $args) as $condition) { |
| 243 | $result = $result || in_array($condition['left'], (array)$condition['right'], true); |
| 244 | } |
| 245 | |
| 246 | return $result; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Evaluate group of NOT IN conditions |
| 251 | * |
| 252 | * @param array $conditions |
| 253 | * @param array $args |
| 254 | * |
| 255 | * @return boolean |
| 256 | * |
| 257 | * @access protected |
| 258 | */ |
| 259 | protected function evaluateNotInConditions($conditions, $args) { |
| 260 | return !$this->evaluateInConditions($conditions, $args); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Evaluate group of LIKE conditions |
| 265 | * |
| 266 | * @param array $conditions |
| 267 | * @param array $args |
| 268 | * |
| 269 | * @return boolean |
| 270 | * |
| 271 | * @access protected |
| 272 | */ |
| 273 | protected function evaluateLikeConditions($conditions, $args) { |
| 274 | $result = false; |
| 275 | |
| 276 | foreach($this->prepareConditions($conditions, $args) as $condition) { |
| 277 | foreach((array)$condition['right'] as $el) { |
| 278 | $sub = str_replace('\*', '.*', preg_quote($el)); |
| 279 | $result = $result || preg_match('@^' . $sub . '$@', $condition['left']); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | return $result; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Evaluate group of NOT LIKE conditions |
| 288 | * |
| 289 | * @param array $conditions |
| 290 | * @param array $args |
| 291 | * |
| 292 | * @return boolean |
| 293 | * |
| 294 | * @access protected |
| 295 | */ |
| 296 | protected function evaluateNotLikeConditions($conditions, $args) { |
| 297 | return !$this->evaluateLikeConditions($conditions, $args); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Evaluate group of REGEX conditions |
| 302 | * |
| 303 | * @param array $conditions |
| 304 | * @param array $args |
| 305 | * |
| 306 | * @return boolean |
| 307 | * |
| 308 | * @access protected |
| 309 | */ |
| 310 | protected function evaluateRegexConditions($conditions, $args) { |
| 311 | $result = false; |
| 312 | |
| 313 | foreach($this->prepareConditions($conditions, $args) as $condition) { |
| 314 | $result = $result || preg_match($condition['right'], $condition['left']); |
| 315 | } |
| 316 | |
| 317 | return $result; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Prepare conditions by replacing all defined tokens |
| 322 | * |
| 323 | * @param array $conditions |
| 324 | * @param array $args |
| 325 | * |
| 326 | * @return array |
| 327 | * |
| 328 | * @access protected |
| 329 | */ |
| 330 | protected function prepareConditions($conditions, $args) { |
| 331 | $result = array(); |
| 332 | |
| 333 | if (is_array($conditions)) { |
| 334 | foreach($conditions as $left => $right) { |
| 335 | $result[] = array( |
| 336 | 'left' => $this->parseExpression($left, $args), |
| 337 | 'right' => $this->parseExpression($right, $args) |
| 338 | ); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | return $result; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Parse condition and try to replace all defined tokens |
| 347 | * |
| 348 | * @param mixed $exp Part of the condition (either left or right) |
| 349 | * @param array $args Inline arguments |
| 350 | * |
| 351 | * @return mixed Prepared part of the condition or false on failure |
| 352 | * |
| 353 | * @access protected |
| 354 | */ |
| 355 | protected function parseExpression($exp, $args) { |
| 356 | if (is_scalar($exp)) { |
| 357 | if (preg_match_all('/(\$\{[^}]+\})/', $exp, $match)) { |
| 358 | $exp = AAM_Core_Policy_Token::evaluate($exp, $match[1], $args); |
| 359 | } |
| 360 | // If there is type scaling, perform it too |
| 361 | if (preg_match('/^\(\*(string|ip|int|boolean|bool|array)\)(.*)/i', $exp, $scale)) { |
| 362 | $exp = $this->scaleValue($scale[2], $scale[1]); |
| 363 | } |
| 364 | } elseif (is_array($exp) || is_object($exp)) { |
| 365 | foreach($exp as &$value) { |
| 366 | $value = $this->parseExpression($value, $args); |
| 367 | } |
| 368 | } else { |
| 369 | $exp = false; |
| 370 | } |
| 371 | |
| 372 | return $exp; |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Scale value to specific type |
| 377 | * |
| 378 | * @param mixed $value |
| 379 | * @param string $type |
| 380 | * |
| 381 | * @return mixed |
| 382 | * |
| 383 | * @access protected |
| 384 | */ |
| 385 | protected function scaleValue($value, $type) { |
| 386 | switch(strtolower($type)) { |
| 387 | case 'string': |
| 388 | $value = (string)$value; |
| 389 | break; |
| 390 | |
| 391 | case 'ip': |
| 392 | $value = inet_pton($value); |
| 393 | break; |
| 394 | |
| 395 | case 'int': |
| 396 | $value = (int)$value; |
| 397 | break; |
| 398 | |
| 399 | case 'boolean': |
| 400 | case 'bool': |
| 401 | $value = (bool)$value; |
| 402 | break; |
| 403 | |
| 404 | case 'array': |
| 405 | $value = json_decode($value, true); |
| 406 | break; |
| 407 | } |
| 408 | |
| 409 | return $value; |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Get single instance of itself |
| 414 | * |
| 415 | * @return AAM_Core_Policy_Condition |
| 416 | * |
| 417 | * @access public |
| 418 | * @static |
| 419 | */ |
| 420 | public static function getInstance() { |
| 421 | if (is_null(self::$instance)) { |
| 422 | self::$instance = new self; |
| 423 | } |
| 424 | |
| 425 | return self::$instance; |
| 426 | } |
| 427 | |
| 428 | } |