1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Casbin\Util; |
6
|
|
|
|
7
|
|
|
use Casbin\Exceptions\CasbinException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Util. |
11
|
|
|
* |
12
|
|
|
* @author [email protected] |
|
|
|
|
13
|
|
|
*/ |
|
|
|
|
14
|
|
|
class Util |
15
|
|
|
{ |
16
|
|
|
const REGEXP = '/\beval\((?P<rule>[^),]*)\)/'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* escapes the dots in the assertion, because the expression evaluation doesn't support such variable names. |
|
|
|
|
20
|
|
|
* |
21
|
|
|
* @param string $s |
|
|
|
|
22
|
|
|
* |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
210 |
|
public static function escapeAssertion(string $s): string |
26
|
|
|
{ |
27
|
210 |
|
if (0 === strpos($s, 'r.')) { |
28
|
105 |
|
$s = substr_replace($s, 'r_', 0, 2); |
29
|
|
|
} |
30
|
210 |
|
if (0 === strpos($s, 'p.')) { |
31
|
3 |
|
$s = substr_replace($s, 'p_', 0, 2); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$s = preg_replace_callback("~(\|| |=|\)|\(|&|<|>|,|\+|-|!|\*|\/)(r|p)(\.)~", function ($m) { |
|
|
|
|
35
|
210 |
|
return $m[1].$m[2].'_'; |
36
|
210 |
|
}, $s); |
|
|
|
|
37
|
|
|
|
38
|
210 |
|
if (null === $s) { |
39
|
|
|
throw new CasbinException(sprintf('Unable to escape assertion "%s"', $s)); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
210 |
|
return $s; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* removes the comments starting with # in the text. |
|
|
|
|
47
|
|
|
* |
48
|
|
|
* @param string $s |
|
|
|
|
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
207 |
|
public static function removeComments(string $s): string |
53
|
|
|
{ |
54
|
207 |
|
$pos = strpos($s, '#'); |
55
|
|
|
|
56
|
207 |
|
return false === $pos ? $s : trim(substr($s, 0, $pos)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* gets a printable string for a string array. |
|
|
|
|
61
|
|
|
* |
62
|
|
|
* @param array $s |
|
|
|
|
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
6 |
|
public static function arrayToString(array $s): string |
67
|
|
|
{ |
68
|
6 |
|
return implode(', ', $s); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* removes any duplicated elements in a string array. |
|
|
|
|
73
|
|
|
* |
74
|
|
|
* @param array $s |
|
|
|
|
75
|
|
|
*/ |
|
|
|
|
76
|
12 |
|
public static function arrayRemoveDuplicates(array &$s): void |
77
|
|
|
{ |
78
|
12 |
|
$s = array_keys(array_flip($s)); |
79
|
12 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* determine whether matcher contains function eval. |
|
|
|
|
83
|
|
|
* |
84
|
|
|
* @param string $s |
|
|
|
|
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
126 |
|
public static function hasEval(string $s): bool |
89
|
|
|
{ |
90
|
126 |
|
return (bool) preg_match(self::REGEXP, $s); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* replace function eval with the value of its parameters. |
|
|
|
|
95
|
|
|
* |
96
|
|
|
* @param string $s |
|
|
|
|
97
|
|
|
* @param string $rule |
|
|
|
|
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
6 |
|
public static function replaceEval(string $s, string $rule): string |
102
|
|
|
{ |
103
|
6 |
|
return preg_replace(self::REGEXP, '('.$rule.')', $s); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* returns the parameters of function eval. |
|
|
|
|
108
|
|
|
* |
109
|
|
|
* @param string $s |
|
|
|
|
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
6 |
|
public static function getEvalValue(string $s): array |
114
|
|
|
{ |
115
|
6 |
|
preg_match_all(self::REGEXP, $s, $matches); |
116
|
|
|
|
117
|
6 |
|
return $matches['rule']; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|