• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

php-casbin / php-casbin / 11444189732

21 Oct 2024 04:08PM UTC coverage: 94.006% (+0.2%) from 93.837%
11444189732

push

github

web-flow
Merge pull request #167 from php-casbin/4.x

BREAKING CHANGE: Upgrade the minimum PHP version to 8.0.

716 of 758 new or added lines in 23 files covered. (94.46%)

10 existing lines in 4 files now uncovered.

1976 of 2102 relevant lines covered (94.01%)

134.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.92
/src/Util/Util.php
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 techlee@qq.com
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
    public static function escapeAssertion(string $s): string
26
    {
27
        if (str_starts_with($s, 'r') || str_starts_with($s, 'p')) {
828✔
28
            $pos = strpos($s, '.');
336✔
29
            if ($pos !== false) {
336✔
30
                $s[$pos] = '_';
336✔
31
            }
32
        }
33

34
        $ss = preg_replace_callback("~(\|| |=|\)|\(|&|<|>|,|\+|-|!|\*|\/)((r|p)[0-9]*)(\.)~", function ($m) {
828✔
35
            return $m[1] . $m[2] . '_';
828✔
36
        }, $s);
828✔
37

38
        return is_string($ss) ? $ss : $s;
828✔
39
    }
40

41
    /**
42
     *Removes the comments starting with # in the text.
43
     *
44
     * @param string $s
45
     *
46
     * @return string
47
     */
48
    public static function removeComments(string $s): string
49
    {
50
        $pos = strpos($s, '#');
822✔
51

52
        return false === $pos ? $s : trim(substr($s, 0, $pos));
822✔
53
    }
54

55
    /**
56
     * Gets a printable string for a string array.
57
     *
58
     * @param array $s
59
     *
60
     * @return string
61
     */
62
    public static function arrayToString(array $s): string
63
    {
64
        return implode(', ', $s);
24✔
65
    }
66

67
    /**
68
     * Removes any duplicated elements in a string array.
69
     *
70
     * @param array $s
71
     */
72
    public static function arrayRemoveDuplicates(array &$s): void
73
    {
74
        $s = array_keys(array_flip($s));
60✔
75
    }
76

77
    /**
78
     * Determine whether matcher contains function eval.
79
     *
80
     * @param string $s
81
     *
82
     * @return bool
83
     */
84
    public static function hasEval(string $s): bool
85
    {
86
        return (bool)preg_match(self::REGEXP, $s);
450✔
87
    }
88

89
    /**
90
     * Replace function eval with the value of its parameters.
91
     *
92
     * @param string $s
93
     * @param string $rule
94
     *
95
     * @return string
96
     */
97
    public static function replaceEval(string $s, string $rule): string
98
    {
99
        return (string)preg_replace(self::REGEXP, '(' . $rule . ')', $s);
6✔
100
    }
101

102
    /**
103
     * Returns the parameters of function eval.
104
     *
105
     * @param string $s
106
     *
107
     * @return array
108
     */
109
    public static function getEvalValue(string $s): array
110
    {
111
        preg_match_all(self::REGEXP, $s, $matches);
18✔
112

113
        return $matches['rule'];
18✔
114
    }
115

116
    /**
117
     * ReplaceEvalWithMap replace function eval with the value of its parameters via given sets.
118
     *
119
     * @param string $src
120
     * @param array $sets
121
     * @return string
122
     */
123
    public static function replaceEvalWithMap(string $src, array $sets): string
124
    {
125
        return preg_replace_callback(self::REGEXP, function ($matches) use ($sets): string {
18✔
126
            $key = $matches['rule'];
18✔
127
    
128
            if (isset($sets[$key])) {
18✔
129
                $value = $sets[$key];
18✔
130
                return '(' . $value . ')';
18✔
131
            }
132
    
133
            return $matches[0];
6✔
134
        }, $src);
18✔
135
    }
136

137
    /**
138
     * Remove duplicate elements from an array.
139
     *
140
     * @param array $s
141
     * @return array
142
     */
143
    public static function removeDumplicateElement(array $s): array
144
    {
145
        return array_unique($s);
114✔
146
    }
147

148
    /**
149
     * Check if two arrays are equal, order-insensitive.
150
     *
151
     * @param array $a
152
     * @param array $b
153
     *
154
     * @return bool
155
     */
156
    public static function setEquals(array $a, array $b): bool
157
    {
158
        if (count($a) !== count($b)) {
18✔
159
            return false;
6✔
160
        }
161

162
        sort($a);
18✔
163
        sort($b);
18✔
164
        return $a == $b;
18✔
165
    }
166

167
    /**
168
     * Determines whether IP address ip1 matches the pattern of IP address ip2, ip2 can be an IP address or a CIDR pattern.
169
     *
170
     * @param string $ipAddress
171
     * @param string $cidrAddress
172
     *
173
     * @return bool
174
     */
175
    public static function ipInSubnet(string $ipAddress, string $cidrAddress): bool
176
    {
177
        if (!str_contains($cidrAddress, '/')) {
18✔
178
            return $ipAddress === $cidrAddress;
6✔
179
        }
180

181
        list($subnet, $prefixLength) = explode('/', $cidrAddress);
18✔
182
        $prefixLength = intval($prefixLength);
18✔
183
        // IPv6
184
        if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && filter_var($subnet, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
18✔
185

186
            $ip = inet_pton($ipAddress);
6✔
187
            $subnet = inet_pton($subnet);
6✔
188

189
            if ($ip === false || $subnet === false) {
6✔
NEW
190
                return false;
×
191
            }
192

193
            $mask = str_repeat("f", intdiv($prefixLength, 4));
6✔
194
            $mask .= ['', '8', 'c', 'e'][$prefixLength % 4];
6✔
195
            $mask = str_pad($mask, 32, '0');
6✔
196
            $mask = pack("H*", $mask);
6✔
197

198
            return ($ip & $mask) === ($subnet & $mask);
6✔
199
        }
200
        // IPv4
201
        if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) && filter_var($subnet, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
18✔
202

203
            $ip = ip2long($ipAddress);
18✔
204
            $subnet = ip2long($subnet);
18✔
205
            $mask = 0xffffffff << (32 - $prefixLength);
18✔
206

207
            return ($ip & $mask) === ($subnet & $mask);
18✔
208
        }
209

NEW
210
        return false;
×
211
    }
212
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc