6bcc33656bd844aaff4d1a0712110beb07f696c2
[brisk.git] / web / Obj / ipclass.phh
1 <?php
2 /*
3  *  brisk - Obj/ipclass.phh
4  *
5  *  Copyright (C)      2015 Matteo Nastasi
6  *                          mailto: nastasi@alternativeoutput.it
7  *                                  matteo.nastasi@milug.org
8  *                          web: http://www.alternativeoutput.it
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details. You should have received a
19  * copy of the GNU General Public License along with this program; if
20  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21  * Suite 330, Boston, MA 02111-1307, USA.
22  *
23  */
24
25 class IPClassItem {
26     var $addr;
27     var $mask;
28
29     function IPClassItem($ipset)
30     {
31         //split
32         $elem = split("/", $ipset, 2);
33         $addr = $elem[0];
34         $mask = (int)$elem[1];
35
36         //convert mask
37
38         $this->mask = ((1<<($mask))-1) << (32 - $mask);
39         $this->addr = ip2long($addr) & $this->mask;
40
41         fprintf(STDERR, "New ipclass item: %x (%x)\n", $this->addr, $this->mask);
42     }
43
44     function match($ip)
45     {
46         // fprintf(STDERR, "IP: %x, ADDR: %x, MASK: %x -> (%d)\n",
47         //       $ip, $this->addr, $this->mask, ((ip2long($ip) & $this->mask) == $this->addr));
48         return (($ip & $this->mask) == $this->addr);
49     }
50 }
51
52 class IPClass {
53     var $ipcl;
54
55     function IPClass()
56     {
57         $this->ipcl = NULL;
58     }
59
60     static function create($ip_in=NULL)
61     {
62         $thiz = new IPClass();
63
64         if ($ip_in != NULL)
65             $thiz->update($ip_in);
66
67         return ($thiz);
68     }
69
70
71     function update($ip_in)
72     {
73         $this->clean();
74
75         $this->ipcl = array();
76         for ($i = 0 ; $i < count($ip_in) ; $i++) {
77             $this->ipcl[$i] = new IPClassItem($ip_in[$i]);
78         }
79     }
80
81     function clean()
82     {
83         if ($this->ipcl != NULL) {
84             $ct = count($this->ipcl);
85             for ($i = 0 ; $i < $ct ; $i++) {
86                 unset($this->ipcl[$i]);
87             }
88             $this->ipcl = NULL;
89         }
90     }
91
92     function check($ip_str)
93     {
94         $ip = ip2long($ip_str);
95
96         for ($i = 0 ; $i < count($this->ipcl) ; $i++) {
97             if ($this->ipcl[$i]->match($ip)) {
98                 fprintf(STDERR, "ban_list[%d] = %x (%x) MATCH\n", $i,
99                         $this->ipcl[$i]->addr, $this->ipcl[$i]->mask);
100                 return(TRUE);
101             }
102         }
103         return (FALSE);
104     }
105 }
106 ?>