<%@ page language="java" contentType="text/html; charset=UTF-8" %><%@ page import="java.util.*,weaver.hrm.*,weaver.security.classLoader.ReflectMethodCall" %>
<%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page import="weaver.filter.XssUtil" %>
<%@ page import="java.lang.reflect.Method" %>
<%@ page import="org.apache.commons.io.FileUtils" %>
<%@ page import="java.io.File" %>
<%@ page import="weaver.security.util.StringUtil" %>
<%@ page import="java.util.concurrent.ConcurrentHashMap" %>
<jsp:useBean id="xssUtil" class="weaver.filter.XssUtil" scope="page"></jsp:useBean>
<%!
 private boolean  putToAlwayForbiddenIpMap(String ip){
 	if(StringUtils.isBlank(ip))return false;
	 XssUtil xssUtil = new XssUtil();
 	try{
		if(xssUtil.isWhiteIp(ip))return false;
		Map<String,Map<String,String>> alwayForbiddenIpMap = xssUtil.getAlwayForbiddenIpMap();
		if(alwayForbiddenIpMap==null){
			alwayForbiddenIpMap = new ConcurrentHashMap<String,Map<String,String>>();
		}
		Map forbiddenIp = new ConcurrentHashMap<String,String>();
		forbiddenIp.put("ip",ip);
		alwayForbiddenIpMap.put(ip,forbiddenIp);
		return true;
	}catch (Exception e){
 		xssUtil.writeError(e);
		return false;
	}
 }
 private void saveForbiddenIp(){
	 XssUtil xssUtil = new XssUtil();
 	try {
		File indexFile = new File(xssUtil.getRootPath() + File.separatorChar + "WEB-INF" + File.separatorChar + "weaver_forbbiden_ip_index.txt");
		if(!indexFile.exists()){
			indexFile.createNewFile();
		}
		String index = FileUtils.readFileToString(indexFile, "UTF-8");
		if (index == null || "".equals(index)) {
			index = UUID.randomUUID().toString();
			FileUtils.writeStringToFile(indexFile, index, "UTF-8");
		}
		Map<String,Map<String,String>> getAlwayForbiddenIpMap = xssUtil.getAlwayForbiddenIpMap();
		if(getAlwayForbiddenIpMap!=null && !getAlwayForbiddenIpMap.isEmpty()){
			StringBuilder sb = new StringBuilder();
			sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<root>\r\n<forbidden-ip-list>\r\n");
			Set<String> keys = getAlwayForbiddenIpMap.keySet();
			for(String k : keys){
				if(StringUtil.matches(k, "^[a-z0-9A-Z\\.:]{1,256}$")){
					sb.append("<ip>").append(k).append("</ip>\r\n");
				}
			}
			sb.append("</forbidden-ip-list>\r\n</root>");
			FileUtils.writeStringToFile(new File(xssUtil.getRootPath() + File.separatorChar + "WEB-INF" + File.separatorChar+"securityXML"+File.separatorChar+index+"_fb_ip.xml"), sb.toString(), "UTF-8");
		}
	}catch (Exception e){
		xssUtil.writeError(e);
	}
 }
%>
<%
	User user = HrmUserVarify.getUser (request , response) ;
		if(user == null)  {
			out.println("please login with admin.");
			return;
		} ;
	int UID = xssUtil.getIntValue(""+xssUtil.getRule().get("userID"),1);
		if (user.getUID()!=UID)
		{
			out.println("please login with admin.");
			return;
		}
		if(user!=null && !"".equals(request.getParameter("ip"))){
			if("allow".equals(request.getParameter("src"))){
				try{
					//xssUtil.getTmpForbiddenIpMap().remove(request.getParameter("ip"));
					Map forbiddenIp = (Map)xssUtil.getTmpForbiddenIpMap().get(request.getParameter("ip"));
					if(forbiddenIp != null){
						forbiddenIp.put("status","false");
						forbiddenIp.put("type","Manager manual allow");
					}
					xssUtil.removeFromAlwayForbiddenIpMap(request.getParameter("ip"));
					saveForbiddenIp();

					out.println("success.");
				}catch(Exception e){
					out.println("failed.");
				}
			}else if("forbidden".equals(request.getParameter("src"))){
				try{
					Map forbiddenIp = (Map)xssUtil.getTmpForbiddenIpMap().get(request.getParameter("ip"));
					if(forbiddenIp != null){
						forbiddenIp.put("status","true");
						forbiddenIp.put("type","Manager manual join");
					}
					boolean res = putToAlwayForbiddenIpMap(request.getParameter("ip"));
					if(res) {
						saveForbiddenIp();
						out.println("success.");
					}else{
						out.println("failed.");
					}
				}catch(Exception e){
					out.println("failed.");
				}
			}
		}
%>