PluginProbe ʕ •ᴥ•ʔ
Advanced Access Manager – Access Governance for WordPress / 5.9
Advanced Access Manager – Access Governance for WordPress v5.9
6.8.4 6.8.5 6.9.0 6.9.1 6.9.10 6.9.11 6.9.12 6.9.13 6.9.14 6.9.15 6.9.16 6.9.17 6.9.18 6.9.19 6.9.2 6.9.20 6.9.21 6.9.22 6.9.23 6.9.24 6.9.25 6.9.26 6.9.27 6.9.28 6.9.29 6.9.3 6.9.30 6.9.31 6.9.32 6.9.33 6.9.34 6.9.35 6.9.36 6.9.37 6.9.38 6.9.39 6.9.4 6.9.41 6.9.42 6.9.43 6.9.44 6.9.45 6.9.46 6.9.47 6.9.48 6.9.49 6.9.5 6.9.51 6.9.6 6.9.7 6.9.8 6.9.9 7.0.0 7.0.0-alpha.6 7.0.0-alpha.7 7.0.0-beta.1 7.0.0-rc1 7.0.0-rc2 7.0.0-rc3 7.0.1 7.0.10 7.0.11 7.0.2 7.0.3 7.0.4 7.0.5 7.0.6 7.0.7 7.0.8 7.0.9 7.1.0 7.1.1 trunk 3.0 4.0 4.0.1 4.1 4.2 4.3 4.4 4.4.1 4.5 4.6 4.6.1 4.6.2 4.7 4.7.1 4.7.2 4.7.5 4.7.6 4.8 4.8.1 4.9 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.5.1 4.9.5.2 5.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1 5.1.1 5.10 5.11 5.2 5.2.1 5.2.5 5.2.6 5.2.7 5.3 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.4 5.4.1 5.4.2 5.4.3 5.4.3.1 5.4.3.2 5.5 5.5.1 5.5.2 5.6 5.6.1 5.6.1.1 5.7 5.7.1 5.7.2 5.7.3 5.8 5.8.1 5.8.2 5.8.3 5.9 5.9.1 5.9.1.1 5.9.2 5.9.2.1 5.9.3 5.9.4 5.9.5 5.9.6 5.9.6.1 5.9.6.2 5.9.6.3 5.9.7 5.9.7.1 5.9.7.2 5.9.7.3 5.9.8 5.9.8.1 5.9.9 5.9.9.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.2.0 6.2.1 6.2.2 6.3.0 6.3.1 6.3.2 6.3.3 6.4.0 6.4.1 6.4.2 6.4.3 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.6.0 6.6.1 6.6.2 6.6.3 6.6.4 6.7.0 6.7.1 6.7.2 6.7.3 6.7.4 6.7.5 6.7.6 6.7.7 6.7.8 6.7.9 6.8.0 6.8.1 6.8.2 6.8.3
advanced-access-manager / Application / Core / Policy / Condition.php
advanced-access-manager / Application / Core / Policy Last commit date
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 }