protected function getInput()
	{
	    $this->app = JFactory::getApplication();
            $this->db = JFactory::getDbo();
	    $this->formfields = $this->form->getFieldset();
            $blacklistnets = array();
            $blacklistaddresses = array();
            $blacklistinput = preg_replace(array('/\r\n$/','/\r$/'),"\n",$this->_getField('blacklist'));
            foreach(explode("\n",$blacklistinput) as $blacklistitem) {
		$blacklistitem = trim($blacklistitem);
                if(preg_match('/\//',$blacklistitem)) {
		    if(function_exists('gmp_pow')) {
                        require_once(JPATH_ROOT.'/plugins/system/adminexile/IPv6Net.class.php');
                        $blacklistnets[$blacklistitem]=new IPv6Net($blacklistitem);
		    } else {
                        require_once(JPATH_ROOT.'/plugins/system/adminexile/simplecidr.class.php');
                        $blacklistnets[$blacklistitem]=SimpleCIDR::getInstance($blacklistitem);		      
		    }
                } else {
                    $blacklistaddresses[trim($blacklistitem)]=$blacklistitem;
                }
            }
            $query = $this->db->getQuery(true);
            $query->select('*')->from('#__plg_system_adminexile')->where('penalty = 0')->order('lastattempt DESC');
            $this->db->setQuery($query);
            $blocked = $this->db->loadObjectList();
            $return=array();
	    if(function_exists('gmp_pow')) {
	      $return[]='<h3 style="float:left;clear:left;">'.JText::_('PLG_SYS_ADMINEXILE_IPV46').'</h3>';
	    } else {
	      $return[]='<h3 style="float:left;clear:left;">'.JText::_('PLG_SYS_ADMINEXILE_IPV4').'</h3>';
	    }
            if(!count($blocked)) return implode("\n",$return);
            $version = new JVersion;
            $shortversion = explode('.',$version->getShortVersion());
            $deletetext = '';
            if($shortversion[0] == 2) {
                $deletetext = JText::_('JACTION_DELETE');
            }
            $return[]='<table class="table table-condensed table-striped ipsecurity"><tr><th>IP</th><th>'.JText::_('PLG_SYS_ADMINEXILE_MATCH_RULE').'</th><th>'.JText::_('PLG_SYS_ADMINEXILE_LAST_ATTEMPT').'</th><th>'.JText::_('PLG_SYS_ADMINEXILE_ATTEMPTS').'</th><td></td></tr>';
            foreach($blocked as $match) {
                $buttons = '<button class="btn btn-mini removeblock hasTip" data-block="'.htmlentities(json_encode(array('ip'=>$match->ip,'firstattempt'=>$match->firstattempt))).'" data-toggle="tooltip" title="'.JText::_('JACTION_DELETE').'"><i class="icon-trash"></i>'.$deletetext.'</button>';
                if(in_array($match->ip,$blacklistaddresses)) {
                    $return[]='<td>'.$match->ip.'</td><td></td><td>'.$match->lastattempt.'</td><td>'.$match->attempts.'</td><td>'.$buttons.'</td></tr>';
                } else {
                    $matchnet=false;
                    foreach(array_keys($blacklistnets) as $key) {
                        if($matchnet) continue;
                        if($blacklistnets[$key]->contains(trim((string)$match->ip))) {
                            $matchnet = $key;
                            $return[]='<td>'.$match->ip.'</td><td>'.$key.'</td><td>'.$match->lastattempt.'</td><td>'.$match->attempts.'</td><td>'.$buttons.'</td></tr>';                           
                        }
                    }
                }
            }
            $return[]='</table>';
            return implode("\n",$return);
	}
	protected function getInput()
	{
	    $this->app = JFactory::getApplication();
            $this->db = JFactory::getDbo();
	    $this->formfields = $this->form->getFieldset();
            $blacklistnets = array();
            $blacklistaddresses = array();
            $blacklistinputarray=AdminExileHelper::ipArray($this->_getField('blacklist'));
            $gmp = AdminExileHelper::gmp();
            require_once(JPATH_PLUGINS.'/system/adminexile/classes/'.($gmp?'IPv6Net.class.php':'simplecidr.class.php'));
            foreach($blacklistinputarray as $blacklistitem) {
		$blacklistitem = trim($blacklistitem);
                if(preg_match('/\//',$blacklistitem)) {
                    try{
                        $blacklistnets[$blacklistitem]=$gmp?(new IPv6Net($blacklistitem)):SimpleCIDR::getInstance($blacklistitem);
                    } catch (Exception $e) {
                        error_log("AdminExile cannot process ".$blacklistitem." due to:".$e->getMessage());
                        $blacklistaddresses[trim($blacklistitem)]=$blacklistitem;
                    }
                } else {
                    $blacklistaddresses[trim($blacklistitem)]=$blacklistitem;
                }
            }
            $query = $this->db->getQuery(true);
            $query->select('*')->from('#__plg_system_adminexile')->where('penalty = 0')->order('lastattempt DESC');
            $this->db->setQuery($query);
            $blocked = $this->db->loadObjectList();
            $return=array();
            $return[]='<h3 style="float:left;clear:left;">'.($gmp?JText::_('PLG_SYS_ADMINEXILE_IPV46'):JText::_('PLG_SYS_ADMINEXILE_IPV4')).'</h3>';
            $attempts = new stdClass();
            foreach($blocked as $match) {
                if(in_array($match->ip,$blacklistaddresses)) {
                    $attempts->{$match->ip} = new stdClass();
                    $attempts->{$match->ip}->lastattempt = $match->lastattempt;
                    $attempts->{$match->ip}->firstattempt = $match->firstattempt;
                    $attempts->{$match->ip}->attempts = $match->attempts;
                } else {
                    foreach(array_keys($blacklistnets) as $key) {
                        if($blacklistnets[$key]->contains(trim((string)$match->ip))) { 
                            if(!property_exists($attempts,$key)) {
                                $attempts->$key = new stdClass();
                                $attempts->$key->attempts = 0;
                                $attempts->$key->addresses = new stdClass();
                            }                           
                            $attempts->$key->addresses->{$match->ip}->lastattempt = $match->lastattempt;
                            $attempts->$key->addresses->{$match->ip}->firstattempt = $match->firstattempt;
                            $attempts->$key->addresses->{$match->ip}->attempts = $match->attempts;
                            $attempts->$key->attempts += $match->attempts;
                        }
                    }
                }
            }
            JFactory::getDocument()->addScriptDeclaration('window.plg_sys_adminexile_blacklist = '.json_encode($attempts).';');
            return implode("\n",$return);
	}
 function _bwnet($net) {
     switch($this->_gmp) {
         case true:
             $newnet = new IPv6Net($net);
             break;
         default:
             $newnet = SimpleCIDR::getInstance($net);
             break;
     }
     return $newnet;
 }
    public function _blackwhite() {
        if ($this->params->get('ipsecurity', 0)) {
            $visitor = $_SERVER['REMOTE_ADDR'];
            foreach (array('whitelist', 'blacklist') as $list) {
                $listnet = $list . 'net';
                $$listnet = array();
                $$list = explode("\n", $this->params->get($list, ''));
                foreach ($$list as $key => $item) {
                    if (preg_match('/\//', $item)) {
                        unset($$list[$key]);
                        array_push($$listnet, $item);
                    }
                }
            }
            if (count(array_merge($whitelistnet, $blacklistnet))) {
	      if(function_exists('gmp_pow')) {
                require_once('IPv6Net.class.php');
              } else {
		require_once('simplecidr.class.php');
              }
            }
            // white first
            if (in_array($visitor, $whitelist)) {
                $this->_authorized();
                return true;
            }
            foreach ($whitelistnet as $net) {
		if(function_exists('gmp_pow')) {
		  $ipnet = new IPv6Net($net);
		} else {
		  $ipnet = SimpleCIDR::getInstance($net);
		}
                if ($ipnet->contains($visitor)) {
                    $this->_authorized();
                    return true;
                }
            }
            if (in_array($visitor, $blacklist)) {
                $options = array('blacklist' => true);
                $this->_logInvalid($options);
                $this->_redirect();
                return true;
            }
            foreach ($blacklistnet as $net) {
		if(function_exists('gmp_pow')) {
		  $ipnet = new IPv6Net($net);
		} else {
		  $ipnet = SimpleCIDR::getInstance($net);
		}
                if ($ipnet->contains($visitor)) {
                    $options = array('blacklist' => true);
                    $this->_logInvalid($options);
                    $this->_redirect();
                    return true;
                }
            }
        }
        return false;
    }
 private function _bwnet($net) {
     try {
         $ret = $this->_gmp?(new IPv6Net($net)):SimpleCIDR::getInstance($net);
     } catch (Exception $e) {
         error_log("AdminExile cannot process ".$net." due to:".$e->getMessage());
         $ret = false;
     }
     return $ret;
 }