Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
226 | daniel-mar | 1 | <?php |
2 | |||
3 | # if (!interface_exists('VolcanoAuthProvider')) throw new Exception('Required interface "VolcanoAuthProvider" not found.'); |
||
4 | # if (!class_exists('VolcanoDB')) throw new Exception('Required class "VolcanoDB" not found.'); |
||
5 | |||
6 | require_once __DIR__ . '/../../includes/ip_function.inc.php'; |
||
7 | require_once __DIR__ . '/../../includes/ipv4_functions.inc.php'; |
||
8 | require_once __DIR__ . '/../../includes/ipv6_functions.inc.php'; |
||
9 | |||
10 | require_once __DIR__ . '/../../core/VolcanoAuthProvider.class.php'; |
||
11 | |||
12 | class VolcanoAuthIP implements VolcanoAuthProvider { |
||
13 | public static function checkId($id) { |
||
14 | return self::strEqual($id, 'ip', true); // is always case insensitive |
||
15 | } |
||
16 | |||
17 | public static function checkAuth($candidate, $token) { |
||
18 | $ip = get_real_ip(); |
||
19 | if (strpos($candidate, ':') !== false) { |
||
20 | return ipv6_in_cidr($candidate, $ip); |
||
21 | } else { |
||
22 | return ipv4_in_cidr($candidate, $ip); |
||
23 | } |
||
24 | } |
||
25 | |||
26 | protected static function strEqual($str1, $str2, $caseInsensitive = false) { |
||
27 | if ($caseInsensitive) { |
||
28 | return strtolower($str1) == strtolower($str2); |
||
29 | } else { |
||
30 | return $str1 == $str2; |
||
31 | } |
||
32 | } |
||
33 | } |
||
34 | |||
35 | require_once __DIR__ . '/../../core/1_VolcanoDB.class.php'; |
||
36 | VolcanoDB::registerAuthProvider(new VolcanoAuthIP()); |
||
37 | |||
38 | # --- TEST |
||
39 | |||
40 | /* |
||
41 | $x = new VolcanoAuthIP(); |
||
42 | assert( $x->checkAuth('217/8', '')); |
||
43 | assert( $x->checkAuth('::1/128', '')); |
||
44 | */ |