mask = ((1<<($mask))-1) << (32 - $mask); $this->addr = ip2long($addr) & $this->mask; fprintf(STDERR, "New ipclass item: %x (%x)\n", $this->addr, $this->mask); } function match($ip) { // fprintf(STDERR, "IP: %x, ADDR: %x, MASK: %x -> (%d)\n", // $ip, $this->addr, $this->mask, ((ip2long($ip) & $this->mask) == $this->addr)); return (($ip & $this->mask) == $this->addr); } } class IPClass { var $ipcl; function IPClass() { $this->ipcl = NULL; } static function create($ip_in=NULL) { $thiz = new IPClass(); if ($ip_in != NULL) $thiz->update($ip_in); return ($thiz); } function update($ip_in) { $this->clean(); $this->ipcl = array(); for ($i = 0 ; $i < count($ip_in) ; $i++) { $this->ipcl[$i] = new IPClassItem($ip_in[$i]); } } function clean() { if ($this->ipcl != NULL) { $ct = count($this->ipcl); for ($i = 0 ; $i < $ct ; $i++) { unset($this->ipcl[$i]); } $this->ipcl = NULL; } } function check($ip_str) { $ip = ip2long($ip_str); for ($i = 0 ; $i < count($this->ipcl) ; $i++) { if ($this->ipcl[$i]->match($ip)) { fprintf(STDERR, "ban_list[%d] = %x (%x) MATCH\n", $i, $this->ipcl[$i]->addr, $this->ipcl[$i]->mask); return(TRUE); } } return (FALSE); } } ?>