<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ page import="net.sf.json.JSONObject,weaver.security.classLoader.ReflectMethodCall,weaver.filter.SecurityCheckList,java.io.*" %>
<%@ page import="java.util.*,weaver.filter.XssUtil,java.lang.reflect.*" %>
<%@ page import="java.util.jar.JarEntry" %>
<%@ page import="java.util.jar.JarFile" %>
<jsp:useBean id="xssUtil" class="weaver.filter.XssUtil"></jsp:useBean>
<%!
	private long getVersion(String fileuploadPath) throws IOException {
		String path = this.getClass().getResource(fileuploadPath).getPath();
		if (path == null) {
			return -1;
		}

		Properties props = System.getProperties();
		String Osname = props.getProperty("os.name");
		if (Osname.contains("Windows")) {
			path = path.substring("file:/".length(), path.indexOf("jar") + "jar".length());
		} else {
			path = path.substring("file:".length(), path.indexOf("jar") + "jar".length());
		}
		JarFile jarFileFileupload = new JarFile(path);
		long version = 0;
		Enumeration<JarEntry> enume = jarFileFileupload.entries();

		while (enume.hasMoreElements()) {
			JarEntry entry = enume.nextElement();
			if ("META-INF/MANIFEST.MF".equals(entry.getName())) {
				InputStream input = jarFileFileupload.getInputStream(entry);
				BufferedReader readerMF = new BufferedReader(new InputStreamReader(input));
				String line = readerMF.readLine();
				while (null != line) {
					if (line.contains("Implementation-Version:")) {
						String versionNow = line.substring(line.lastIndexOf("Implementation-Version:") + "Implementation-Version:".length());
						long versionNowInt = 0;
						String[] tmps = versionNow.trim().split("\\.");
						if (1 == tmps.length) {
							int i = 12 - tmps[0].length();
							while (0 < i) {
								tmps[0] += "0";
								i--;
							}
							versionNowInt = Long.parseLong(tmps[0]);
						} else if (2 == tmps.length) {
							int a = 3 - tmps[0].length();
							while (0 < a) {
								tmps[0] += "0";
								a--;
							}
							int b = 9 - tmps[1].length();
							while (0 < b) {
								tmps[1] += "0";
								b--;
							}
							versionNowInt = Long.parseLong(tmps[0] + tmps[1]);
						} else if (3 == tmps.length) {
							int a = 3 - tmps[0].length();
							while (0 < a) {
								tmps[0] += "0";
								a--;
							}

							int b = 3 - tmps[1].length();
							while (0 < b) {
								tmps[1] += "0";
								b--;
							}

							int c = 6 - tmps[2].length();
							while (0 < c) {
								tmps[2] += "0";
								c--;
							}
							versionNowInt = Long.parseLong(tmps[0] + tmps[1] + tmps[2]);
						} else if (4 == tmps.length) {

							int a = 3 - tmps[0].length();
							while (0 < a) {
								tmps[0] += "0";
								a--;
							}
							int b = 3 - tmps[1].length();
							while (0 < b) {
								tmps[1] += "0";
								b--;
							}
							int c = 3 - tmps[2].length();
							while (0 < c) {
								tmps[2] += "0";
								c--;
							}
							int d = 3 - tmps[3].length();
							while (0 < d) {
								tmps[3] += "0";
								d--;
							}
							versionNowInt = Long.parseLong(tmps[0] + tmps[1] + tmps[2] + tmps[3]);
						}
						return versionNowInt;
					}
					line = readerMF.readLine();
				}
				break;
			}
		}
		return 0;
	}

	

	public String[] getCid(XssUtil xssUtil){
		String cid = "";
		String companyName = "";
		String licenseCode = "";
		String[] info = new String[]{cid,companyName,licenseCode};
			try{
				Class lnClass = Class.forName("ln.LN");
				Object lnObject = lnClass.newInstance();
				Method ckHrmnumMethod = lnClass.getMethod("CkHrmnum");
				ckHrmnumMethod.invoke(lnObject);
				Method getCompanynameMethod = lnClass.getMethod("getCompanyname");
				companyName = xssUtil.null2String(getCompanynameMethod.invoke(lnObject));
				Method getCidMethod = lnClass.getMethod("getCid");
				cid = xssUtil.null2String(getCidMethod.invoke(lnObject));
				Method getLicenseCodeMethod = lnClass.getMethod("getLicensecode");
				licenseCode = xssUtil.null2String(getLicenseCodeMethod.invoke(lnObject));
				info[0] = cid;
				info[1] = companyName;
				info[2] = licenseCode;
//				companyName = xssUtil.getCompanyname();
				
			}catch(Exception e){
				//xssUtil.writeError(e);
			}
		return info;
		
	}

	 private long getSpecialJarVersion(String fileuploadPath, String vPath, String preStr, String mark) throws IOException {
        String path = this.getClass().getResource(fileuploadPath).getPath();
        if (path == null) {
            return -1;
        }

        Properties props = System.getProperties();
        String Osname = props.getProperty("os.name");
        weaver.filter.XssUtil util = new weaver.filter.XssUtil();
        File weblogicFile = new File(util.getRootPath() + "WEB-INF/weblogic.xml");
        if (!weblogicFile.exists()) {
            if (Osname.contains("Windows")) {
                path = path.substring("file:/".length(), path.indexOf("jar") + "jar".length());
            } else {
                path = path.substring("file:".length(), path.indexOf("jar") + "jar".length());
            }
        } else {
            path = path.substring(0,path.indexOf("jar") + "jar".length());
        }
        JarFile jarFileFileupload = new JarFile(path);
        long version = 0;
        Enumeration<JarEntry> enume = jarFileFileupload.entries();

        while (enume.hasMoreElements()) {
            JarEntry entry = enume.nextElement();
            if (vPath.equals(entry.getName())) {
                InputStream input = jarFileFileupload.getInputStream(entry);
                BufferedReader readerMF = new BufferedReader(new InputStreamReader(input));
                String line = readerMF.readLine();
                while (null != line) {
                    if (line.contains(preStr)) {
                        String versionNow = line.split(mark)[1].trim();
                        long versionNowInt = 0;
                        String[] tmps = versionNow.trim().split("\\.");
                        if (1 == tmps.length) {

                            versionNowInt = Long.parseLong(tmps[0] + "000000000");
                        } else if (2 == tmps.length) {
                            int a = 3 - tmps[1].length();
                            while (0 < a) {
                                tmps[1] = "0" + tmps[1];
                                a--;
                            }
                            versionNowInt = Long.parseLong(tmps[0] + tmps[1] + "000000");
                        } else if (3 == tmps.length) {
                            int a = 3 - tmps[1].length();
                            while (0 < a) {
                                tmps[1] = "0" + tmps[1];
                                a--;
                            }

                            int c = 3 - tmps[2].length();
                            while (0 < c) {
                                tmps[2] = "0" + tmps[2];
                                c--;
                            }
                            versionNowInt = Long.parseLong(tmps[0] + tmps[1] + tmps[2] + "000");
                        } else if (4 == tmps.length) {

                            int a = 3 - tmps[1].length();
                            while (0 < a) {
                                tmps[1] = "0" + tmps[1];
                                a--;
                            }

                            int c = 3 - tmps[2].length();
                            while (0 < c) {
                                tmps[2] = "0" + tmps[2];
                                c--;
                            }

                            int d = 3 - tmps[3].length();
                            while (0 < d) {
                                tmps[3] = "0" + tmps[3];
                                d--;
                            }
                            versionNowInt = Long.parseLong(tmps[0] + tmps[1] + tmps[2] + tmps[3]);
                        }
                        return versionNowInt;
                    }
                    line = readerMF.readLine();
                }
                break;
            }
        }
        return 0;
    }



%>
<%
	ReflectMethodCall rmc = new ReflectMethodCall();
	Boolean isJoinSystemSecurity = (Boolean)rmc.call("weaver.security.core.SecurityCore",xssUtil.getSecurityCore(),"joinSystemSecurity",new Class[]{},null);
	if(isJoinSystemSecurity==null)isJoinSystemSecurity = new Boolean(true);
	JSONObject json = new JSONObject();
	if(new Boolean(true).compareTo(isJoinSystemSecurity)==0){
		String sysid = (String)rmc.call("weaver.security.core.SecurityCore",xssUtil.getSecurityCore(),"uuid",
								new Class[]{},null);
		//
		Integer checkCommonCollection = 0;

		String commonCollectionPath = "/org/apache/commons/collections/CollectionUtils.class";

		long commonCollectionVersion = getVersion(commonCollectionPath);
		json.put("ccVersion",commonCollectionVersion);
		if (commonCollectionVersion >= 300200200000L) {
			checkCommonCollection = 1;
		}
		//
		String fileuploadPath = "/org/apache/commons/fileupload/DiskFileUpload.class";
		long version = getVersion(fileuploadPath);
		int checkCommonFileUpload = 0;
		json.put("cfuVersion",version);
		if (version >= 100300300000L) {
			checkCommonFileUpload = 1;
		}

		//
		 String xStreamPath = "/com/thoughtworks/xstream/XStream.class";
		version = getVersion(xStreamPath);
		json.put("stVersion",version);
		int checkXStream = 0;
		if (version >= 100400150000L) {
			checkXStream = 1;
		}

	//
		int checkFastJson = 0;

		 File dirFile = new File(xssUtil.getRootPath() + "/WEB-INF/lib");
		 StringBuffer fastjsonVersion = new StringBuffer();
		for (File f : dirFile.listFiles()) {
			if(f.getName().contains("fastjson")){
				String name = f.getName();
				int pos = name.indexOf("-");
				if(pos!=-1){
					fastjsonVersion.append(name.substring(pos+1).replaceAll("\\.","00").replaceAll("jar","")).append("00").append(",");
				}
			}
			if (f.getName().contains("fastjson-1.2.74")) {
				checkFastJson = 1;
				//break;
			}
		}

		String fastjsonPath = "/com/alibaba/fastjson/JSONObject.class";
		String vPath = "META-INF/maven/com.alibaba/fastjson/pom.properties";
        String preStr = "version";
        String mark = "=";
		long fastjsonJarVersion = getSpecialJarVersion(fastjsonPath,vPath,preStr,mark);
		 if (fastjsonJarVersion >= 1002076000L) {
			checkFastJson = 1;
		 }
		fastjsonVersion.append(fastjsonJarVersion);
		json.put("ftVersion",fastjsonVersion.toString());
		json.put("useFtVersion", fastjsonJarVersion);

		//csui.getRemoteServerVersion();
		rmc.call("weaver.security.msg.CheckSecurityUpdateInfoUtil","getRemoteServerVersion",
				new Class[]{},null);
		json.put("sysid",sysid);
		json.put("cc",checkCommonCollection);
		json.put("cfu",checkCommonFileUpload);
		json.put("stream",checkXStream);
		json.put("ft",checkFastJson);
		String[] cidname = getCid(xssUtil);
		String companyname = cidname[1];
		String cid = cidname[0];
		String licenseCode = cidname[2];
		if(!"".equals(cid)){
			json.put("cid",cid);
		}
		if("".equals(companyname)){
			companyname = xssUtil.getCompanyname();
		}
		if(!"".equals(licenseCode)){
			json.put("licenseCode",licenseCode);
		}
		json.put("companyName",companyname);
		json.put("ecVersion",xssUtil.getEcDetailVersion());
		json.put("firewallStatus",new Boolean(xssUtil.enableFirewall()));
		json.put("autoUpdateStatus",new Boolean(xssUtil.isAutoUpdateRules()));
		json.put("softwareVersion",rmc.call("weaver.security.msg.CheckSecurityUpdateInfoUtil","getVersion",
				new Class[]{},null));
		json.put("remoteVersion",rmc.call("weaver.security.msg.CheckSecurityUpdateInfoUtil","getNewversion",
				new Class[]{},null));
		json.put("ruleVersion",rmc.call("weaver.security.msg.CheckSecurityUpdateInfoUtil","getRuleVersion",
				new Class[]{},null));
		json.put("loginStatus",new Boolean(xssUtil.enableFirewall() && xssUtil.isLoginCheck()));
		json.put("pageStatus",new Boolean(xssUtil.enableFirewall() && xssUtil.getMustXss()));
		json.put("dataStatus",new Boolean(xssUtil.enableFirewall() && !xssUtil.getIsSkipRule()));
		json.put("enableServiceCheck",new Boolean(xssUtil.enableFirewall() && xssUtil.getEnableWebserviceCheck()));
		json.put("isUseESAPISQL",new Boolean(xssUtil.enableFirewall() && xssUtil.isUseESAPISQL()));
		json.put("isUseESAPIXSS",new Boolean(xssUtil.enableFirewall() && xssUtil.isUseESAPIXSS()));
		json.put("isResetCookie",new Boolean(xssUtil.enableFirewall() && xssUtil.isResetCookie()));
		json.put("httpOnly",new Boolean(xssUtil.enableFirewall() && xssUtil.getHttpOnly()));
		json.put("hostStatus",new Boolean(xssUtil.enableFirewall() && !xssUtil.getIsSkipHost()));
		json.put("isRefAll",new Boolean(xssUtil.enableFirewall() && !xssUtil.getIsRefAll()));
		json.put("httpSep",new Boolean(xssUtil.enableFirewall() && xssUtil.getHttpSep()));
		json.put("isCheckSessionTimeout",new Boolean(xssUtil.enableFirewall() && xssUtil.isCheckSessionTimeout()));
		json.put("isEnableForbiddenIp",new Boolean(xssUtil.enableFirewall() && xssUtil.isEnableForbiddenIp()>1));
		json.put("autoRemind",new Boolean(xssUtil.enableFirewall() && xssUtil.getAutoRemind()));
		SecurityCheckList scl = new SecurityCheckList();
		json.put("isConfigFirewall",new Boolean(scl.isConfigFirewall()));
		json.put("isEnableAccessLog",new Boolean(scl.isEnableAccessLog()));
		json.put("checkSocketTimeout",new Boolean(scl.checkSocketTimeout()));
		json.put("isResinAdmin",new Boolean(!scl.isResinAdmin()));
		json.put("is404PageConfig",new Boolean(scl.is404PageConfig()));
		json.put("is500PageConfig",new Boolean(scl.is500PageConfig()));
		json.put("isDisabledHttpMethod",new Boolean(scl.isDisabledHttpMethod()));
		json.put("joinSystemSecurity",isJoinSystemSecurity);
		Boolean testServer = (Boolean)rmc.call("weaver.security.core.SecurityCheckList","testNetwork",
		            		new Class[]{},null);
		if(testServer==null)testServer=new Boolean(false);
		json.put("isWlanNetwork",testServer);
		
	}else{
		json.put("result", false);
	}
	//System.out.println(json);
	String sk = (String)rmc.call("weaver.security.file.AESForCbc",null,"createSymmetricSecurityKey",
			new Class[]{}, null);
	String iv = (String)rmc.call("weaver.security.file.AESForCbc",null,"createSymmetricIvKey",
			new Class[]{}, null);
	String info = (String)rmc.call("weaver.security.file.AESForCbc",null,"encrypt",
								new Class[]{String.class,String.class,String.class,String.class},
								new String[]{json.toString(),"GBK",sk, iv});
	String esk =(String)rmc.call("weaver.security.file.AESForCbc",null,"encryptRSA",
			new Class[]{String.class,String.class},
			new String[]{sk,null});

	String eiv =(String)rmc.call("weaver.security.file.AESForCbc",null,"encryptRSA",
			new Class[]{String.class,String.class},
			new String[]{iv, null});
	JSONObject result = new JSONObject();
	result.put("info", info);
	result.put("sk", esk);
	result.put("iv", eiv);
	out.println(result);
%>