SqlCheckAjax.jsp 6.36 KB
<%@page import="java.util.regex.Pattern"%>
<%@page import="sun.misc.BASE64Decoder"%>
<%@ page import="weaver.general.*,weaver.conn.*" %>
<%@ page import="weaver.hrm.*" %>
<%@page import="java.sql.*"%>
<%@page import="net.sf.json.JSONObject"%>
<jsp:useBean id="automaticconnect" class="weaver.workflow.automatic.automaticconnect" scope="page" />
<%
out.clear();
response.setContentType("text/xml;charset=UTF-8");
String sqlcontent = Util.null2String(request.getParameter("sql"));
BASE64Decoder decoder = new BASE64Decoder();
try {
	String base64Pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
	if(Pattern.matches(base64Pattern, sqlcontent)){
		sqlcontent=new String(decoder.decodeBuffer(sqlcontent), "UTF-8").replace("%2B","+");
	}
} catch (Exception e1) {

}
String datasourceid = Util.null2String(request.getParameter("datasourceid"));
String isFormMode = Util.null2String(request.getParameter("isFormMode"));
boolean isCanUse = false;
User user = HrmUserVarify.getUser (request , response) ;
JSONObject jsonObject = new JSONObject();
if(user==null){
	if("1".equals(isFormMode)){
		jsonObject.put("isCanUse",isCanUse);
		jsonObject.put("errormsg","-101");
		response.getWriter().write(jsonObject.toString());
	}
	return;
}
boolean userRight = HrmUserVarify.checkUserRight("WorkflowManage:All", user);
if (!userRight && !HrmUserVarify.checkUserRight("ModeSetting:All", user)) {
	if("1".equals(isFormMode)){
		jsonObject.put("isCanUse",isCanUse);
		jsonObject.put("errormsg","-102");
		response.getWriter().write(jsonObject.toString());
	}
	return;
}

String errormsg = "";
try{
	ConnStatement statement = null;
	int index = sqlcontent.indexOf("doFieldSQL(\"");
	if(index > -1){
		sqlcontent = sqlcontent.substring(index+12);
		index = sqlcontent.lastIndexOf("\")");
		if(index > -1){
			sqlcontent = sqlcontent.substring(0, index);
		}
	}
	
	sqlcontent = sqlcontent.trim();
	if(!"".equals(sqlcontent) && !sqlcontent.toLowerCase().startsWith("select")){
		if("1".equals(isFormMode)){
			jsonObject.put("isCanUse",isCanUse);
			jsonObject.put("errormsg","");
			response.getWriter().write(jsonObject.toString());
		}
		return;
	}
	sqlcontent = sqlcontent.replaceAll("\\'\\$([0-9]*)\\$\\'", "'19700101'");
	sqlcontent = sqlcontent.replaceAll("\\$([0-9]*)\\$", "'19700101'");
	sqlcontent = sqlcontent.replaceAll("\\$(-[0-9]*)\\$", "'19700101'");
	sqlcontent = sqlcontent.replaceAll("\\$([currentdate]*)\\$", "1970-01-01");
	sqlcontent = sqlcontent.replaceAll("\\$([billid|currentuser|currentdept|wfcreater|wfcredept|id|requestid]*)\\$", "0");
    sqlcontent = sqlcontent.replaceAll("PARM\\(.*?\\)","0");
	if(!"".equals(sqlcontent)){
		if("".equals(datasourceid) || "null".equals(datasourceid)){
			try{
				statement = new ConnStatement();
				statement.setStatementSql(sqlcontent);
				statement.executeQuery();
				isCanUse = true;
			}catch(Exception e){
				errormsg = e.getMessage();
				isCanUse = false;
			}finally{
				try{
					statement.close();
					statement = null;
				}catch(Exception e){
					errormsg = e.getMessage();
					//极有可能出错,出错时不做任何处理
				}
			}
		}else{//数据源
			Connection conn = null;
			Statement stmt = null;
			
			try{
				String sql = "select a.*,b.driverclass, b.driverurl  from datasourcesetting a,datasource_type b where a.pointid ='"+datasourceid+"' and b.dbtype=a.type";
				String dbtype = "";
				RecordSet RecordSet = new RecordSet();
				RecordSet.execute(sql);
				if(RecordSet.next()){
					dbtype = RecordSet.getString("type");
				}
				if(dbtype.equals("db2")){
					String url = Util.null2String(RecordSet.getString("url"));
					String host = Util.null2String(RecordSet.getString("host"));
					String port = Util.null2String(RecordSet.getString("port"));
					String dbname = Util.null2String(RecordSet.getString("dbname"));
					url = SecurityHelper.decrypt(SecurityHelper.KEY, url);
					host = SecurityHelper.decrypt(SecurityHelper.KEY, host);
					port = SecurityHelper.decrypt(SecurityHelper.KEY, port);
					dbname = SecurityHelper.decrypt(SecurityHelper.KEY, dbname);
					
		        	String forname = Util.null2String(RecordSet.getString("driverclass"));
		        	String turl = Util.null2String(RecordSet.getString("driverurl"));
		        	String username = Util.null2String(RecordSet.getString("username"));
		        	String password = Util.null2String(RecordSet.getString("password"));
		        	String iscode = Util.null2String(RecordSet.getString("iscode"));
		        	if("1".equals(iscode)) {
		        		username = SecurityHelper.decrypt(SecurityHelper.KEY, username);
		            	password = SecurityHelper.decrypt(SecurityHelper.KEY, password);
		        	}
		        	
		        	if(!"".equals(turl)) {
		            	turl = turl.replace("[host]", host).replace("[port]", port);
		            	
		            	if(turl.indexOf("[servicename]") > -1) {// informix专用
		            		String servicename = "myserver";
		                    if(dbname.indexOf("@$") > -1) {
		                    	String dbnametemp = dbname;
		                    	dbname = dbnametemp.substring(0, dbnametemp.indexOf("@$"));
		                        servicename = dbnametemp.substring(dbnametemp.indexOf("@$") + 2);
		                    }
		                    turl = turl.replace("[dbname]", dbname).replace("[servicename]", servicename);
		            	} else {
		            		turl = turl.replace("[dbname]", dbname);
		            	}
		            }
		        	Driver driver = (Driver)Class.forName(forname).newInstance();
		        	DriverManager.registerDriver(driver);
		        	conn = DriverManager.getConnection(turl, username, password);
				}else{
					conn = automaticconnect.getConnection("datasource."+datasourceid);//获得外部连接
				}
				stmt = conn.createStatement();
				stmt.executeQuery(sqlcontent);
				isCanUse = true;
			}catch(Exception e){
				errormsg = e.getMessage();
				isCanUse = false;
			}finally{
				try{
					stmt.close();
					conn.close();
				}catch(Exception e){
					errormsg = e.getMessage();
				}
			}
		}
	}else{
		isCanUse = true;
	}
}catch(Exception e){
	errormsg = e.getMessage();
	isCanUse = false;
}
if(isCanUse){
	errormsg = "";
}else{
	if(errormsg.equals("null")){
		errormsg = "";
	}
}
if("1".equals(isFormMode)){
	jsonObject.put("isCanUse",isCanUse);
	jsonObject.put("errormsg",errormsg);
	response.getWriter().write(jsonObject.toString());
	return;
}
%>
<information>
<iscanuse><%=isCanUse%></iscanuse>
</information>