很久以前写了个asp操作类,就研究java去了,前段时间由于要帮朋友弄个东西,又捡起那个类一看,唉~还是差点什么,于是,改写了一下:
里面还有些扩展应用,注意Map这个概念,和java里的map是一样。具体能否看懂,我就不管了,多说无益,放在这里,可以自己参考下。
<%
//基本操作类
//清缓存
Response.Expires=-1000;
Response.CacheControl="no-cache";
/**
* 用Javascript实现的一个数据库操作类
* @author zxub
* 修改于2005-11-24
*/
function DBOperate()
{
this.connString=""; //连接字符串
this.connString="Provider=microsoft.jet.oledb.4.0;data source="+Server.MapPath("/sms/dbo/aaa@163.com/#data.asp");
//this.connString="driver={SQL SERVER};server=localhost;database=user;uid=study;pwd=study;";
this.conn; //数据库连接对象
this.rs; //数据集
this.cursorType=1; //纪录集移动方式:
//0为只读,当前数据记录只能向下移动;
//1为只读,当前数据记录可自由移动;
//2为可读写,当前数据记录可自由移动;
//3为可读写,当前数据记录可自由移动,可看到新增记录
//RS.MoveFirst(),RS.MoveLast(),RS.MoveNext(),RS.MovePrevious().
//RS.Move(n) 方法:从当前位置移动n条记录,n>0为正向,n<0为反向,
//RS.Move(n,1) 方法:从第一条纪录处移动n条记录,n>0,后面的参数只能为1
this.lockType=1; //纪录集锁定方式:
//1为缺省锁定类型,记录集是只读的,不能修改记录
//2为悲观锁定,当修改记录时,数据提供者将尝试锁定记录以确保成功地编辑记录。只要编辑一开始,则立即锁住记录。
//3为乐观锁定,直到用Update方法提交更新记录时才锁定记录。
//4为批量乐观锁定,允许修改多个记录,只有调用UpdateBatch方法后才锁定记录。
//*************************************************************************
//当分页的时候,计算totalRecordCount和totalPageCount
//*************************************************************************
this.totalRecordCount=0; //纪录集总纪录数,开始由于没纪录集,所以为0
this.pageSize=10; //每页最大纪录数,默认为10
this.totalPageCount=0; //最大页数.
//*************************************************************************
this.currentPageID=1; //当前页码,默认为1
this.currentPageTag="CurrentPageID";
this.gotoPageName="?"; //显示纪录的页面名称,默认为当前页面
this.recordIndexInPage=0; //用于分页时的数据下移
//打开数据库
this.openDatabase=function(_dbPath)
{
if (_dbPath!=undefined)
{
this.connString="Provider=microsoft.jet.oledb.4.0;data source="+Server.MapPath(_dbPath);
}
try
{
this.conn=Server.CreateObject("ADODB.Connection");
this.conn.Open(this.connString);
}
catch (e)
{
Response.Write("数据库连接错误!");
Response.End();
}
}
//关闭数据库
this.closeDatabase=function()
{
if (this.rs!=null)
{
this.rs.Close();
this.rs=null;
}
if (this.conn!=null)
{
this.conn.Close();
this.conn=null;
}
}
//获取一个数据集对象
this.getRecordSet=function(_sqlString)
{
if (this.conn!=null)
{
var i=Request.QueryString(this.currentPageTag).Count;
if (i>0)
{
this.currentPageID=parseInt(Request.QueryString(this.currentPageTag));
if (this.currentPageID<=0)
{
this.closeDatabase();
Response.Write("页码超出合法范围!");
Response.End();
}
}
this.rs=Server.CreateObject("ADODB.RecordSet");
this.rs.Open(_sqlString,this.conn,this.cursorType,this.lockType);
this.totalRecordCount=this.rs.RecordCount;
this.totalPageCount=Math.ceil(this.totalRecordCount/this.pageSize);
var endPage=this.totalPageCount;
if (endPage==0) endPage=1;
if (this.currentPageID>endPage)
{
this.closeDatabase();
Response.Write("页码超出合法范围!"+this.pageSize);
Response.End();
}
//指针位置调整
if (!this.rsIsEmpty() && this.currentPageID>1)
{
this.rs.MoveFirst();
this.rs.Move((this.currentPageID-1)*this.pageSize,1);
}
}
else
{
Response.Write("没有连接到数据库!");
Response.End();
}
}
//判断分页的时候记录是否已到一页的末尾
this.isPageEnd=function()
{
this.rs.moveNext();
this.recordIndexInPage++;
if (this.recordIndexInPage<this.pageSize)
{
return false;
}
else
{
return true;
}
}
//关闭数据集对象
this.closeRecordSet=function()
{
if (this.rs!=null)
{
this.rs.Close();
this.rs=null;
}
}
//执行SQL语句,用于执行添加、删除、修改操作
this.executeSql=function(_sqlString)
{
if (this.conn!=null)
{
this.conn.Execute(_sqlString);
}
else
{
Response.Write("没有连接到数据库!");
Response.End();
}
}
this.addNewAndReturnPK=function(_parameterMap,_tableName,_pkName)
{
var returnValue="";
if (this.rs==null)
{
this.rs=Server.CreateObject("ADODB.RecordSet");
this.rs.ActiveConnection=this.conn;
this.rs.CursorType=this.cursorType;
}
this.rs.LockType=3;
this.rs.Source=_tableName;
this.rs.Open();
this.rs.AddNew();
var keys=_parameterMap.keys();
for (var i=0;i<keys.length;i++)
{
//Response.Write(keys[i]+":");
//Response.Write(_parameterMap.get(keys[i])+"<br>");
try
{
this.rs(keys[i])=_parameterMap.get(keys[i]);
}
catch (e)
{}
}
this.rs.Update();
returnValue=this.rs(_pkName).value;
this.rs.Close();
this.rs.LockType=this.lockType;
this.rs.Open();
return returnValue;
}
this.updateRecord=function(_parameterMap,_tableName,_pkValue,_pkName)
{
if (this.rs==null)
{
this.rs=Server.CreateObject("ADODB.RecordSet");
this.rs.ActiveConnection=this.conn;
this.rs.CursorType=this.cursorType;
}
this.rs.LockType=3;
this.rs.Source=_tableName;
this.rs.Open();
this.rs.Find(_pkName+"="+_pkValue);
var keys=_parameterMap.keys();
for (var i=0;i<keys.length;i++)
{
try
{
this.rs(keys[i])=_parameterMap.get(keys[i]);
}
catch (e)
{}
}
this.rs.Update();
this.rs.Close();
this.rs.LockType=this.lockType;
this.rs.Open();
}
//判断获取的数据集对象是否为空
this.rsIsEmpty=function()
{
if (this.rs!=null)
{
if ((this.rs.BOF) && (this.rs.EOF))
{
//RS is empty
return true;
}
else
{
//RS not empty
return false;
}
}
else
{
Response.Write("没有连接到数据库!");
Response.End();
}
}
//打印分页导航条
this.setRecordNavigator=function()
{
var previousPageID=this.currentPageID-1;
var nextPageID=this.currentPageID+1;
var homePage=1;
var endPage=this.totalPageCount;
//************************************************************
//分页操作如下,8为每页记录数
/*var obj=getConn("select * from modules",8);
while (!obj.rs.eof)
{
Response.Write(obj.rs("name").value+"<br>");
if (obj.isPageEnd()) break;
}
obj.setRecordNavigator();
obj.closeDatabase();
obj=null;*/
//************************************************************
var navigatorString="";
navigatorString+="<table width=\"100%\" height=\"25\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"font-size:12px\">";
navigatorString+="<tr>";
navigatorString+="<td align=\"right\" valign=\"middle\">合计 <strong><font color=\"#FF0000\">";
navigatorString+=this.totalRecordCount;
navigatorString+="</font></strong> 个 | ";
if (this.currentPageID==homePage)
{
navigatorString+="<A disabled>首页</A> <A disabled>上一页</A> ";
}
else
{
navigatorString+="<A href="+this.gotoPageName+this.currentPageTag+"="+homePage+">首页</A> <A href="+this.gotoPageName+this.currentPageTag+"="+previousPageID+">上一页</A> ";
}
if (this.currentPageID==endPage)
{
navigatorString+="<A disabled>下一页</A> <A disabled>尾页</A> </td>";
}
else
{
navigatorString+="<A href="+this.gotoPageName+this.currentPageTag+"="+nextPageID+">下一页</A> <A href="+this.gotoPageName+this.currentPageTag+"="+endPage+">尾页</A> </td>";
}
navigatorString+="<td width=\"140\" align=\"center\" valign=\"middle\">页次:<strong><font color=\"#FF0000\">";
navigatorString+=this.currentPageID;
navigatorString+="</font>/";
navigatorString+=this.totalPageCount+" ";
navigatorString+=this.pageSize+"</strong>个/页 </td>"
navigatorString+="<td width=\"36\" align=\"right\" valign=\"middle\">转到: </td>"
navigatorString+="<td width=\"76\" align=\"left\" valign=\"middle\">";
navigatorString+="<select name=CurrentPage style='font-size:12px;width:60px' onchange=\"document.location=\'"+this.gotoPageName+this.currentPageTag+"=\'+this.options[this.selectedIndex].value\">";
for (i=1;i<=this.totalPageCount;i++)
{
if(this.currentPageID==i)
{
navigatorString+="<option selected value="+i+">第"+i+"页</option>";
}
else
{
navigatorString+="<option value="+i+">第"+i+"页</option>";
}
}
navigatorString+="</select>";
navigatorString+="</td>";
navigatorString+="</tr>";
navigatorString+="</table>"
if (endPage>0)
{
Response.Write(navigatorString);
}
}
}
%>
<!------------------------------------------------------------------------------------------------->
<%
/**
* 字符串截取
* _inputString:要截取的字符串
* _appendString:截取后附加的字符串,如
* _cutLen:截取的字符数,一个全角字符及汉字占2个字符
*/
function cutString(_inputString,_appendString,_cutLen)
{
_inputString=String(_inputString);
_appendString=String(_appendString);
var _factLen=0,_tempString="",i;
var strLength=_inputString.length;
for (i=0;i<strLength;i++)
{
if (_inputString.charCodeAt(i)>255)
{
_factLen+=2;
}
else
{
_factLen+=1;
}
if (_factLen>_cutLen)
break;
try
{
_tempString+=_inputString.substr(i,1);
}
catch (e)
{
break;
}
}
if (_factLen>_cutLen)
{
_tempString+=_appendString;
}
return _tempString;
}
/**
* 换行及回车的显示
* _string:要显示的部分
*/
function alignString(_string)
{
return String(_string).replace( /[\" \"]/g," ").replace( /[\" \"]/g," ").replace(/[\r]/g,"<br>");
}
%>
<!----------------------------------------------------------------------------------------------------------->
<%
//基于基础类的扩展操作函数
function addNewAndReturnPK(_parameterMap,_tableName,_pkName)
{
var returnValue="";
if (_pkName==undefined) _pkName="id";
var object=new DBOperate();
object.openDatabase();
returnValue=object.addNewAndReturnPK(_parameterMap,_tableName,_pkName);
object.closeDatabase();
object=null;
return returnValue;
}
function updateRecord(_parameterMap,_tableName,_pkValue,_pkName)
{
if (_pkName==undefined) _pkName="id";
var object=new DBOperate();
object.openDatabase();
returnValue=object.updateRecord(_parameterMap,_tableName,_pkValue,_pkName);
object.closeDatabase();
object=null;
}
/**
* 执行一条sql语句
* _sqlString:要执行的sql语句
*/
function execSql(_sqlString)
{
var object=new DBOperate();
object.openDatabase();
object.executeSql(_sqlString);
object.closeDatabase();
object=null;
}
/**
* 批处理执行sql语句,传递一个数组(Array),数组里存放sql语句
* _sqlArr:存放sql语句的数组
*/
function execBatchSql(_sqlArr)
{
var object=new DBOperate();
object.openDatabase();
object.conn.BeginTrans();
for (var i=0;i<_sqlArr.length;i++)
{
object.conn.Execute(_sqlArr[i]);
if (object.conn.Errors.Count>0)
{
object.conn.Errors.Clear();
object.conn.RollBackTrans();
}
}
object.conn.CommitTrans();
object.closeDatabase();
object=null;
}
/**
* 获取一个指定的数据库操作对象,并进而获取一个数据集对象
* _sqlString:一条select语句,用于获取数据集
* _pageSize:数据集分页大小,若要获取所有,则该项不填
* _curPageTag:指定分页标记名称,不填,则用默认
*/
function getConn(_sqlString,_pageSize,_curPageTag)
{
var object=new DBOperate();
if (_curPageTag!=undefined)
{
object.currentPageTag=_curPageTag;
}
if (_pageSize!=undefined)
{
object.pageSize=_pageSize;
}
else
{
object.pageSize=999990;
}
object.openDatabase();
object.getRecordSet(_sqlString);
return object;
}
function getRecordData(_sqlString,_fieldArray)
{
var returnValue=new Array();
var object=new DBOperate();
object.openDatabase();
object.getRecordSet(_sqlString);
if (_fieldArray==undefined)
{
while (!object.rs.eof)
{
var data=new HashTable();
for (var i=0;i<object.rs.fields.count;i++)
{
if (object.rs(i).value==null)
{
data.put(object.rs(i).name,"");
}
else
{
data.put(object.rs(i).name,object.rs(i).value);
}
}
returnValue.push(data);
object.rs.moveNext();
}
}
else
{
while (!object.rs.eof)
{
var data=new HashTable();
for (var i=0;i<_fieldArray.length;i++)
{
if (object.rs(_fieldArray[i]).value==null)
{
data.put(_fieldArray[i],"");
}
else
{
data.put(_fieldArray[i],object.rs(_fieldArray[i]).value);
}
}
returnValue.push(data);
object.rs.moveNext();
}
}
object.closeDatabase();
return returnValue;
}
//获取参数,行成Map,执行持久化
//获取post及get的数据,形成map返回
function parametersToMap()
{
var returnMap=new HashTable();
var parameter;
for (var posts=new Enumerator(Request.Form);!posts.atEnd();posts.moveNext())
{
parameter=posts.item();
returnMap.put(parameter,Request.Form(parameter));
}
for (var gets=new Enumerator(Request.QueryString);!gets.atEnd();gets.moveNext())
{
parameter=gets.item();
returnMap.put(parameter,Request.QueryString(parameter));
}
return returnMap;
}
/**
* 获取记录并形成map返回,若_pkValue为"",则获取表结构
* _entityName:数据表名称
* _pkValue:数据表的主键值,默认主键名称为"id"
*/
function getEntityMap(_entityName,_pkValue,_pkName)
{
if (_pkName==undefined)
{
_pkName="id";
}
var returnMap=new HashTable();
var sqlString;
var sqlString="select top 1 * from "+_entityName;
var columns=getConn(sqlString,1);
for (var i=0;i<columns.rs.fields.count;i++)
{
//returnMap.put(columns.rs(i).name,columns.rs.Fields(i).Type);
returnMap.put(columns.rs(i).name,"");
}
//获取表结构完成
//若_pkValue有值,则再获取所需数据
if (_pkValue!=undefined)
{
sqlString="select * from "+_entityName+" where "+_pkName+"="+_pkValue;
entity=getConn(sqlString,1);
if (!entity.rsIsEmpty())
{
for (var j=0;j<entity.rs.fields.count;j++)
{
if (entity.rs(j).value==null)
{
returnMap.put(entity.rs(j).name,"");
}
else
{
returnMap.put(entity.rs(j).name,entity.rs(j).value);
if ((""+entity.rs(j).value).indexOf("UTC")>0)
{
var getDate=new Date(entity.rs(j).value)
var dateString=getDate.getYear()+"-"+(getDate.getMonth()+1)+"-"+getDate.getDate();
returnMap.put(entity.rs(j).name,dateString);
}
}
}
}
entity.closeDatabase();
entity=null;
}
else
{
var keys=returnMap.keys();
for (var j=0;j<keys.length;j++)
{
returnMap.put(keys[j],"");
}
}
columns.closeDatabase();
columns=null;
return returnMap;
}
/**
* 获取post及get的数据,根据是否存在"id"字段进行更新或插入操作
* _entityName:要操作的数据表名
*/
function parametersToEntity(_entityName,_return,_parameters)
{
if (_parameters==undefined)
{
_parameters=parametersToMap();
}
if (!_parameters.containsKey("action")) return;
checkEntityPermission(_parameters.get("action"));
mapToEntity(_parameters,_entityName,_return);
}
/**
* 指定map到数据库的操作映射
* _map:指定的map
* _entityName:数据表名
*/
function mapToEntity(_map,_entityName,_return)
{
if (_return==undefined)
{
_return=1;
}
var sqlString="";
var action=_map.get("action");
if (action=="" || action==null) return;
_map.remove("action");
checkEntityPermission(action);
if (action=="insert" || action=="ins") //添加记录
{
try
{
_map.remove("id");
}
catch (e)
{}
var nodeId=addNewAndReturnPK(_map,_entityName);
Response.Write('<script language="javascript">alert("添加记录成功!");');
if (parseInt(_return)>0)
{
Response.Write('history.go(-'+_return+');');
}
else
{
Response.Write('document.location="'+_return+'";');
}
Response.Write('</script>');
return;
}
if (action=="update" && _map.containsKey("id")) //修改记录
{
var id=_map.get("id");
_map.remove("id");
updateRecord(_map,_entityName,id);
Response.Write('<script language="javascript">alert("更新记录成功!");');
if (parseInt(_return)>0)
{
Response.Write('history.go(-'+_return+');');
}
else
{
Response.Write('document.location="'+_return+'";');
}
Response.Write('</script>');
return;
}
if (action=="delete" && _map.containsKey("id")) //删除记录
{
sqlString="delete from "+_entityName+" where id="+parseInt(_map.get("id"));
execSql(sqlString);
Response.Write('<script language="javascript">alert("删除记录成功!");');
if (parseInt(_return)>0)
{
Response.Write('history.go(-'+_return+');');
}
else
{
Response.Write('document.location="'+_return+'";');
}
Response.Write('</script>');
return;
}
}
%>
<!------------------------------------------------------------------------------------------------------->
<%
/**
* HashTable的javascript实现
* @author zxub
* 2005-8-18
*/
function HashTable()
{
//表对象
this.hashtable=new Array();
//清空表
this.clear=function()
{
this.hashtable=new Array();
}
//获取表长度
this.size=function()
{
var size=0;
for (var i in this.hashtable)
{
if (this.hashtable[i]!=null)
size++;
}
return size;
}
//判断表是否为空
this.isEmpty=function()
{
return (this.size()==0)?true:false;
}
//插入数据
this.put=function(_key,_value)
{
if (_key==null || _value==null)
{
throw "NullPointerException {" + _key + "},{" + _value + "}";
}
else
{
this.hashtable[_key]=_value;
}
}
//删除键值
this.remove=function(_key)
{
this.hashtable[_key]=null;
}
//获取所选键的键值
this.get=function(_key)
{
return this.hashtable[_key];
}
this.getKeyByValue=function(_value)
{
var keys=this.keys();
var key="";
for (var i=0;i<keys.length;i++)
{
if (this.get(keys[i])==_value)
{
key=keys[i];
}
}
return key;
}
//获取键名数组
this.keys=function()
{
var keys = new Array();
for (var i in this.hashtable)
{
if (this.hashtable[i]!=null)
keys.push(i);
}
return keys;
}
//获取键值数组
this.values=function()
{
var values=new Array();
for (var i in this.hashtable)
{
if (this.hashtable[i] != null)
values.push(this.hashtable[i]);
}
return values;
}
//判断表中是否存在一个选定键名
this.containsKey=function(_key)
{
var exists=false;
for (var i in this.hashtable)
{
if (i==_key && this.hashtable[i]!=null)
{
exists=true;
break;
}
}
return exists;
}
//判断表中是否存在键值为当前值的键
this.containsValue=function(_value)
{
var contains=false;
if (_value!=null)
{
for (var i in this.hashtable)
{
if (this.hashtable[i]==_value)
{
contains=true;
break;
}
}
}
return contains;
}
//获取表中数据字符串表现形式
this.toString=function()
{
var result="";
for (var i in this.hashtable)
{
if (this.hashtable[i] != null)
result+=""+i+":"+this.hashtable[i]+"<br>";
}
return result;
}
}
%>
<!------------------------------------------------------------------------------------------------------->
<%
/**
* md5加密
*/
function md5(sMessage) {
function RotateLeft(lValue, iShiftBits) { return (lValue<<iShiftBits) | (lValue>>>(32-iShiftBits)); }
function AddUnsigned(lX,lY) {
var lX4,lY4,lX8,lY8,lResult;
lX8 = (lX & 0x80000000);
lY8 = (lY & 0x80000000);
lX4 = (lX & 0x40000000);
lY4 = (lY & 0x40000000);
lResult = (lX & 0x3FFFFFFF)+(lY & 0x3FFFFFFF);
if (lX4 & lY4) return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
if (lX4 | lY4) {
if (lResult & 0x40000000) return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);
else return (lResult ^ 0x40000000 ^ lX8 ^ lY8);
} else return (lResult ^ lX8 ^ lY8);
}
function F(x,y,z) { return (x & y) | ((~x) & z); }
function G(x,y,z) { return (x & z) | (y & (~z)); }
function H(x,y,z) { return (x ^ y ^ z); }
function I(x,y,z) { return (y ^ (x | (~z))); }
function FF(a,b,c,d,x,s,ac) {
a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac));
return AddUnsigned(RotateLeft(a, s), b);
}
function GG(a,b,c,d,x,s,ac) {
a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac));
return AddUnsigned(RotateLeft(a, s), b);
}
function HH(a,b,c,d,x,s,ac) {
a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, c, d), x), ac));
return AddUnsigned(RotateLeft(a, s), b);
}
function II(a,b,c,d,x,s,ac) {
a = AddUnsigned(a, AddUnsigned(AddUnsigned(I(b, c, d), x), ac));
return AddUnsigned(RotateLeft(a, s), b);
}
function ConvertToWordArray(sMessage) {
var lWordCount;
var lMessageLength = sMessage.length;
var lNumberOfWords_temp1=lMessageLength + 8;
var lNumberOfWords_temp2=(lNumberOfWords_temp1-(lNumberOfWords_temp1 % 64))/64;
var lNumberOfWords = (lNumberOfWords_temp2+1)*16;
var lWordArray=Array(lNumberOfWords-1);
var lBytePosition = 0;
var lByteCount = 0;
while ( lByteCount < lMessageLength ) {
lWordCount = (lByteCount-(lByteCount % 4))/4;
lBytePosition = (lByteCount % 4)*8;
lWordArray[lWordCount] = (lWordArray[lWordCount] | (sMessage.charCodeAt(lByteCount)<<lBytePosition));
lByteCount++;
}
lWordCount = (lByteCount-(lByteCount % 4))/4;
lBytePosition = (lByteCount % 4)*8;
lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80<<lBytePosition);
lWordArray[lNumberOfWords-2] = lMessageLength<<3;
lWordArray[lNumberOfWords-1] = lMessageLength>>>29;
return lWordArray;
}
function WordToHex(lValue) {
var WordToHexValue="",WordToHexValue_temp="",lByte,lCount;
for (lCount = 0;lCount<=3;lCount++) {
lByte = (lValue>>>(lCount*8)) & 255;
WordToHexValue_temp = "0" + lByte.toString(16);
WordToHexValue = WordToHexValue + WordToHexValue_temp.substr(WordToHexValue_temp.length-2,2);
}
return WordToHexValue;
}
var x=Array();
var k,AA,BB,CC,DD,a,b,c,d
var S11=7, S12=12, S13=17, S14=22;
var S21=5, S22=9 , S23=14, S24=20;
var S31=4, S32=11, S33=16, S34=23;
var S41=6, S42=10, S43=15, S44=21;
x = ConvertToWordArray(sMessage);
a = 0x67452301; b = 0xEFCDAB89; c = 0x98BADCFE; d = 0x10325476;
for (k=0;k<x.length;k+=16) {
AA=a; BB=b; CC=c; DD=d;
a=FF(a,b,c,d,x[k+0], S11,0xD76AA478);
d=FF(d,a,b,c,x[k+1], S12,0xE8C7B756);
c=FF(c,d,a,b,x[k+2], S13,0x242070DB);
b=FF(b,c,d,a,x[k+3], S14,0xC1BDCEEE);
a=FF(a,b,c,d,x[k+4], S11,0xF57C0FAF);
d=FF(d,a,b,c,x[k+5], S12,0x4787C62A);
c=FF(c,d,a,b,x[k+6], S13,0xA8304613);
b=FF(b,c,d,a,x[k+7], S14,0xFD469501);
a=FF(a,b,c,d,x[k+8], S11,0x698098D8);
d=FF(d,a,b,c,x[k+9], S12,0x8B44F7AF);
c=FF(c,d,a,b,x[k+10],S13,0xFFFF5BB1);
b=FF(b,c,d,a,x[k+11],S14,0x895CD7BE);
a=FF(a,b,c,d,x[k+12],S11,0x6B901122);
d=FF(d,a,b,c,x[k+13],S12,0xFD987193);
c=FF(c,d,a,b,x[k+14],S13,0xA679438E);
b=FF(b,c,d,a,x[k+15],S14,0x49B40821);
a=GG(a,b,c,d,x[k+1], S21,0xF61E2562);
d=GG(d,a,b,c,x[k+6], S22,0xC040B340);
c=GG(c,d,a,b,x[k+11],S23,0x265E5A51);
b=GG(b,c,d,a,x[k+0], S24,0xE9B6C7AA);
a=GG(a,b,c,d,x[k+5], S21,0xD62F105D);
d=GG(d,a,b,c,x[k+10],S22,0x2441453);
c=GG(c,d,a,b,x[k+15],S23,0xD8A1E681);
b=GG(b,c,d,a,x[k+4], S24,0xE7D3FBC8);
a=GG(a,b,c,d,x[k+9], S21,0x21E1CDE6);
d=GG(d,a,b,c,x[k+14],S22,0xC33707D6);
c=GG(c,d,a,b,x[k+3], S23,0xF4D50D87);
b=GG(b,c,d,a,x[k+8], S24,0x455A14ED);
a=GG(a,b,c,d,x[k+13],S21,0xA9E3E905);
d=GG(d,a,b,c,x[k+2], S22,0xFCEFA3F8);
c=GG(c,d,a,b,x[k+7], S23,0x676F02D9);
b=GG(b,c,d,a,x[k+12],S24,0x8D2A4C8A);
a=HH(a,b,c,d,x[k+5], S31,0xFFFA3942);
d=HH(d,a,b,c,x[k+8], S32,0x8771F681);
c=HH(c,d,a,b,x[k+11],S33,0x6D9D6122);
b=HH(b,c,d,a,x[k+14],S34,0xFDE5380C);
a=HH(a,b,c,d,x[k+1], S31,0xA4BEEA44);
d=HH(d,a,b,c,x[k+4], S32,0x4BDECFA9);
c=HH(c,d,a,b,x[k+7], S33,0xF6BB4B60);
b=HH(b,c,d,a,x[k+10],S34,0xBEBFBC70);
a=HH(a,b,c,d,x[k+13],S31,0x289B7EC6);
d=HH(d,a,b,c,x[k+0], S32,0xEAA127FA);
c=HH(c,d,a,b,x[k+3], S33,0xD4EF3085);
b=HH(b,c,d,a,x[k+6], S34,0x4881D05);
a=HH(a,b,c,d,x[k+9], S31,0xD9D4D039);
d=HH(d,a,b,c,x[k+12],S32,0xE6DB99E5);
c=HH(c,d,a,b,x[k+15],S33,0x1FA27CF8);
b=HH(b,c,d,a,x[k+2], S34,0xC4AC5665);
a=II(a,b,c,d,x[k+0], S41,0xF4292244);
d=II(d,a,b,c,x[k+7], S42,0x432AFF97);
c=II(c,d,a,b,x[k+14],S43,0xAB9423A7);
b=II(b,c,d,a,x[k+5], S44,0xFC93A039);
a=II(a,b,c,d,x[k+12],S41,0x655B59C3);
d=II(d,a,b,c,x[k+3], S42,0x8F0CCC92);
c=II(c,d,a,b,x[k+10],S43,0xFFEFF47D);
b=II(b,c,d,a,x[k+1], S44,0x85845DD1);
a=II(a,b,c,d,x[k+8], S41,0x6FA87E4F);
d=II(d,a,b,c,x[k+15],S42,0xFE2CE6E0);
c=II(c,d,a,b,x[k+6], S43,0xA3014314);
b=II(b,c,d,a,x[k+13],S44,0x4E0811A1);
a=II(a,b,c,d,x[k+4], S41,0xF7537E82);
d=II(d,a,b,c,x[k+11],S42,0xBD3AF235);
c=II(c,d,a,b,x[k+2], S43,0x2AD7D2BB);
b=II(b,c,d,a,x[k+9], S44,0xEB86D391);
a=AddUnsigned(a,AA); b=AddUnsigned(b,BB); c=AddUnsigned(c,CC); d=AddUnsigned(d,DD);
}
var temp= WordToHex(a)+WordToHex(b)+WordToHex(c)+WordToHex(d);
return temp.toLowerCase();
}
%>
<%
function showGetMap(_map)
{
Response.Write('<script language="javascript">');
Response.Write('function setValue(objectName,value)');
Response.Write('{');
Response.Write('if (!document.getElementById(objectName)) return;');
Response.Write('var object=document.getElementById(objectName);');
Response.Write('if (object.type=="text" || object.type=="textarea" || object.type=="hidden") {object.value=value;}');
Response.Write('else if (object.type=="select-one")');
Response.Write('{');
Response.Write('var selectOptions=object.options;');
Response.Write('for (var i=0;i<selectOptions.length;i++)');
Response.Write('{');
Response.Write('if (selectOptions[i].value==value) {object.selectedIndex=i;break;}');
Response.Write('}');
Response.Write('}');
Response.Write('}');
Response.Write('window.onload=function()');
Response.Write('{');
var keys=_map.keys();
for (var i=0;i<keys.length;i++)
{
Response.Write('setValue("'+keys[i]+'","'+_map.get(keys[i])+'");');
}
Response.Write('}');
Response.Write('</script>');
}
%>
<%
//sql语句防注入
function safeguard()
{
var dangerousString="'|;|and|(|)|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare";
var dangerousArray=dangerousString.split("|");
var killIp=true;
var writeSql=true;
var inIp=getIp();
if (killIp)
{
var alert=false;
var history=getConn("select * from sqlIn where inIp='"+inIp+"'",99999,"##");
while (!history.rs.eof)
{
if (history.rs("killIp").value=="true")
{
alert=true;
break;
}
history.rs.moveNext();
}
history.closeDatabase();
history=null;
if (alert)
{
Response.write("<Script Language=JavaScript>alert('由于你曾经意图修改数据库,你的Ip已经被本系统自动锁定!如想正常访问本站请和管理员联系!');</Script>");
Response.End();
}
}
var parameters=parametersToMap();
var keys=parameters.keys();
for (var i=0;i<keys.length;i++)
{
for (var j=0;j<dangerousArray.length;j++)
{
if ((parameters.get(keys[i])+"").toLowerCase().indexOf(dangerousArray[j])>-1)
{
if (writeSql)
{
var sqlString="Insert into sqlIn (inIp,visitUrl,inKey,inValue,killIp) values('"+inIp+"','"+Request.ServerVariables("URL")+"','"+keys[i]+"','"+(parameters.get(keys[i])+"").replace( /[\']/g,"\"")+"','true')";
execSql(sqlString);
}
Response.Write("<Script Language=JavaScript>alert('请不要在参数中包含非法字符尝试注入!');</Script>");
Response.Write("非法操作!系统做了如下记录:<br>");
Response.Write("操作IP:"+inIp+"<br>");
now=new Date();
H=now.getHours();
M=now.getMinutes();
S=now.getSeconds();
timestr=(H<10)?("0"+H):H;
timestr+=":";
timestr+=(M<10)?("0"+M):M;
timestr+=":";
timestr+=(S<10)?("0"+S):S;
Response.Write("操作时间:"+timestr+"<br>");
Response.Write("操作页面:"+Request.ServerVariables("URL")+"<br>");
Response.Write("提交参数:"+keys[i]+"<br>");
Response.Write("提交数据:"+parameters.get(keys[i]));
Response.End();
}
}
}
}
%>
<!------------------------------------------------------------------------------------------------->
<%
//获取客户端参数
function getIp()
{
var ip=Request.ServerVariables("HTTP_X_FORWARDED_FOR");
if ((ip+"").indexOf(".")<0)
{
ip=Request.ServerVariables("REMOTE_ADDR");
}
return ip;
}
function getOS()
{
var os=Request.ServerVariables("OS");
if ((""+os)=="undefined")
{
os="WIN2003?";
}
return os;
}
//递归创建文件夹
function createFolder(folderPath)
{
var entry;
var path="";
var f;
var fso=Server.CreateObject("Scripting.FileSystemObject");
for (var entries=new Enumerator(folderPath.split("/"));!entries.atEnd();entries.moveNext())
{
path+=("/"+entries.item());
if (!fso.folderexists(Server.mappath(path)))
{
f = fso.CreateFolder(Server.mappath(path));
}
}
}
function getNowTimeString()
{
var now=new Date();
return now.getYear()+"-"+(now.getMonth()+1)+"-"+now.getDate();
}
function fixDate(_dateString)
{
var d=new Date(_dateString);
return d.getYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
}
%>
//基本操作类
//清缓存
Response.Expires=-1000;
Response.CacheControl="no-cache";
/**
* 用Javascript实现的一个数据库操作类
* @author zxub
* 修改于2005-11-24
*/
function DBOperate()
{
this.connString=""; //连接字符串
this.connString="Provider=microsoft.jet.oledb.4.0;data source="+Server.MapPath("/sms/dbo/aaa@163.com/#data.asp");
//this.connString="driver={SQL SERVER};server=localhost;database=user;uid=study;pwd=study;";
this.conn; //数据库连接对象
this.rs; //数据集
this.cursorType=1; //纪录集移动方式:
//0为只读,当前数据记录只能向下移动;
//1为只读,当前数据记录可自由移动;
//2为可读写,当前数据记录可自由移动;
//3为可读写,当前数据记录可自由移动,可看到新增记录
//RS.MoveFirst(),RS.MoveLast(),RS.MoveNext(),RS.MovePrevious().
//RS.Move(n) 方法:从当前位置移动n条记录,n>0为正向,n<0为反向,
//RS.Move(n,1) 方法:从第一条纪录处移动n条记录,n>0,后面的参数只能为1
this.lockType=1; //纪录集锁定方式:
//1为缺省锁定类型,记录集是只读的,不能修改记录
//2为悲观锁定,当修改记录时,数据提供者将尝试锁定记录以确保成功地编辑记录。只要编辑一开始,则立即锁住记录。
//3为乐观锁定,直到用Update方法提交更新记录时才锁定记录。
//4为批量乐观锁定,允许修改多个记录,只有调用UpdateBatch方法后才锁定记录。
//*************************************************************************
//当分页的时候,计算totalRecordCount和totalPageCount
//*************************************************************************
this.totalRecordCount=0; //纪录集总纪录数,开始由于没纪录集,所以为0
this.pageSize=10; //每页最大纪录数,默认为10
this.totalPageCount=0; //最大页数.
//*************************************************************************
this.currentPageID=1; //当前页码,默认为1
this.currentPageTag="CurrentPageID";
this.gotoPageName="?"; //显示纪录的页面名称,默认为当前页面
this.recordIndexInPage=0; //用于分页时的数据下移
//打开数据库
this.openDatabase=function(_dbPath)
{
if (_dbPath!=undefined)
{
this.connString="Provider=microsoft.jet.oledb.4.0;data source="+Server.MapPath(_dbPath);
}
try
{
this.conn=Server.CreateObject("ADODB.Connection");
this.conn.Open(this.connString);
}
catch (e)
{
Response.Write("数据库连接错误!");
Response.End();
}
}
//关闭数据库
this.closeDatabase=function()
{
if (this.rs!=null)
{
this.rs.Close();
this.rs=null;
}
if (this.conn!=null)
{
this.conn.Close();
this.conn=null;
}
}
//获取一个数据集对象
this.getRecordSet=function(_sqlString)
{
if (this.conn!=null)
{
var i=Request.QueryString(this.currentPageTag).Count;
if (i>0)
{
this.currentPageID=parseInt(Request.QueryString(this.currentPageTag));
if (this.currentPageID<=0)
{
this.closeDatabase();
Response.Write("页码超出合法范围!");
Response.End();
}
}
this.rs=Server.CreateObject("ADODB.RecordSet");
this.rs.Open(_sqlString,this.conn,this.cursorType,this.lockType);
this.totalRecordCount=this.rs.RecordCount;
this.totalPageCount=Math.ceil(this.totalRecordCount/this.pageSize);
var endPage=this.totalPageCount;
if (endPage==0) endPage=1;
if (this.currentPageID>endPage)
{
this.closeDatabase();
Response.Write("页码超出合法范围!"+this.pageSize);
Response.End();
}
//指针位置调整
if (!this.rsIsEmpty() && this.currentPageID>1)
{
this.rs.MoveFirst();
this.rs.Move((this.currentPageID-1)*this.pageSize,1);
}
}
else
{
Response.Write("没有连接到数据库!");
Response.End();
}
}
//判断分页的时候记录是否已到一页的末尾
this.isPageEnd=function()
{
this.rs.moveNext();
this.recordIndexInPage++;
if (this.recordIndexInPage<this.pageSize)
{
return false;
}
else
{
return true;
}
}
//关闭数据集对象
this.closeRecordSet=function()
{
if (this.rs!=null)
{
this.rs.Close();
this.rs=null;
}
}
//执行SQL语句,用于执行添加、删除、修改操作
this.executeSql=function(_sqlString)
{
if (this.conn!=null)
{
this.conn.Execute(_sqlString);
}
else
{
Response.Write("没有连接到数据库!");
Response.End();
}
}
this.addNewAndReturnPK=function(_parameterMap,_tableName,_pkName)
{
var returnValue="";
if (this.rs==null)
{
this.rs=Server.CreateObject("ADODB.RecordSet");
this.rs.ActiveConnection=this.conn;
this.rs.CursorType=this.cursorType;
}
this.rs.LockType=3;
this.rs.Source=_tableName;
this.rs.Open();
this.rs.AddNew();
var keys=_parameterMap.keys();
for (var i=0;i<keys.length;i++)
{
//Response.Write(keys[i]+":");
//Response.Write(_parameterMap.get(keys[i])+"<br>");
try
{
this.rs(keys[i])=_parameterMap.get(keys[i]);
}
catch (e)
{}
}
this.rs.Update();
returnValue=this.rs(_pkName).value;
this.rs.Close();
this.rs.LockType=this.lockType;
this.rs.Open();
return returnValue;
}
this.updateRecord=function(_parameterMap,_tableName,_pkValue,_pkName)
{
if (this.rs==null)
{
this.rs=Server.CreateObject("ADODB.RecordSet");
this.rs.ActiveConnection=this.conn;
this.rs.CursorType=this.cursorType;
}
this.rs.LockType=3;
this.rs.Source=_tableName;
this.rs.Open();
this.rs.Find(_pkName+"="+_pkValue);
var keys=_parameterMap.keys();
for (var i=0;i<keys.length;i++)
{
try
{
this.rs(keys[i])=_parameterMap.get(keys[i]);
}
catch (e)
{}
}
this.rs.Update();
this.rs.Close();
this.rs.LockType=this.lockType;
this.rs.Open();
}
//判断获取的数据集对象是否为空
this.rsIsEmpty=function()
{
if (this.rs!=null)
{
if ((this.rs.BOF) && (this.rs.EOF))
{
//RS is empty
return true;
}
else
{
//RS not empty
return false;
}
}
else
{
Response.Write("没有连接到数据库!");
Response.End();
}
}
//打印分页导航条
this.setRecordNavigator=function()
{
var previousPageID=this.currentPageID-1;
var nextPageID=this.currentPageID+1;
var homePage=1;
var endPage=this.totalPageCount;
//************************************************************
//分页操作如下,8为每页记录数
/*var obj=getConn("select * from modules",8);
while (!obj.rs.eof)
{
Response.Write(obj.rs("name").value+"<br>");
if (obj.isPageEnd()) break;
}
obj.setRecordNavigator();
obj.closeDatabase();
obj=null;*/
//************************************************************
var navigatorString="";
navigatorString+="<table width=\"100%\" height=\"25\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"font-size:12px\">";
navigatorString+="<tr>";
navigatorString+="<td align=\"right\" valign=\"middle\">合计 <strong><font color=\"#FF0000\">";
navigatorString+=this.totalRecordCount;
navigatorString+="</font></strong> 个 | ";
if (this.currentPageID==homePage)
{
navigatorString+="<A disabled>首页</A> <A disabled>上一页</A> ";
}
else
{
navigatorString+="<A href="+this.gotoPageName+this.currentPageTag+"="+homePage+">首页</A> <A href="+this.gotoPageName+this.currentPageTag+"="+previousPageID+">上一页</A> ";
}
if (this.currentPageID==endPage)
{
navigatorString+="<A disabled>下一页</A> <A disabled>尾页</A> </td>";
}
else
{
navigatorString+="<A href="+this.gotoPageName+this.currentPageTag+"="+nextPageID+">下一页</A> <A href="+this.gotoPageName+this.currentPageTag+"="+endPage+">尾页</A> </td>";
}
navigatorString+="<td width=\"140\" align=\"center\" valign=\"middle\">页次:<strong><font color=\"#FF0000\">";
navigatorString+=this.currentPageID;
navigatorString+="</font>/";
navigatorString+=this.totalPageCount+" ";
navigatorString+=this.pageSize+"</strong>个/页 </td>"
navigatorString+="<td width=\"36\" align=\"right\" valign=\"middle\">转到: </td>"
navigatorString+="<td width=\"76\" align=\"left\" valign=\"middle\">";
navigatorString+="<select name=CurrentPage style='font-size:12px;width:60px' onchange=\"document.location=\'"+this.gotoPageName+this.currentPageTag+"=\'+this.options[this.selectedIndex].value\">";
for (i=1;i<=this.totalPageCount;i++)
{
if(this.currentPageID==i)
{
navigatorString+="<option selected value="+i+">第"+i+"页</option>";
}
else
{
navigatorString+="<option value="+i+">第"+i+"页</option>";
}
}
navigatorString+="</select>";
navigatorString+="</td>";
navigatorString+="</tr>";
navigatorString+="</table>"
if (endPage>0)
{
Response.Write(navigatorString);
}
}
}
%>
<!------------------------------------------------------------------------------------------------->
<%
/**
* 字符串截取
* _inputString:要截取的字符串
* _appendString:截取后附加的字符串,如
* _cutLen:截取的字符数,一个全角字符及汉字占2个字符
*/
function cutString(_inputString,_appendString,_cutLen)
{
_inputString=String(_inputString);
_appendString=String(_appendString);
var _factLen=0,_tempString="",i;
var strLength=_inputString.length;
for (i=0;i<strLength;i++)
{
if (_inputString.charCodeAt(i)>255)
{
_factLen+=2;
}
else
{
_factLen+=1;
}
if (_factLen>_cutLen)
break;
try
{
_tempString+=_inputString.substr(i,1);
}
catch (e)
{
break;
}
}
if (_factLen>_cutLen)
{
_tempString+=_appendString;
}
return _tempString;
}
/**
* 换行及回车的显示
* _string:要显示的部分
*/
function alignString(_string)
{
return String(_string).replace( /[\" \"]/g," ").replace( /[\" \"]/g," ").replace(/[\r]/g,"<br>");
}
%>
<!----------------------------------------------------------------------------------------------------------->
<%
//基于基础类的扩展操作函数
function addNewAndReturnPK(_parameterMap,_tableName,_pkName)
{
var returnValue="";
if (_pkName==undefined) _pkName="id";
var object=new DBOperate();
object.openDatabase();
returnValue=object.addNewAndReturnPK(_parameterMap,_tableName,_pkName);
object.closeDatabase();
object=null;
return returnValue;
}
function updateRecord(_parameterMap,_tableName,_pkValue,_pkName)
{
if (_pkName==undefined) _pkName="id";
var object=new DBOperate();
object.openDatabase();
returnValue=object.updateRecord(_parameterMap,_tableName,_pkValue,_pkName);
object.closeDatabase();
object=null;
}
/**
* 执行一条sql语句
* _sqlString:要执行的sql语句
*/
function execSql(_sqlString)
{
var object=new DBOperate();
object.openDatabase();
object.executeSql(_sqlString);
object.closeDatabase();
object=null;
}
/**
* 批处理执行sql语句,传递一个数组(Array),数组里存放sql语句
* _sqlArr:存放sql语句的数组
*/
function execBatchSql(_sqlArr)
{
var object=new DBOperate();
object.openDatabase();
object.conn.BeginTrans();
for (var i=0;i<_sqlArr.length;i++)
{
object.conn.Execute(_sqlArr[i]);
if (object.conn.Errors.Count>0)
{
object.conn.Errors.Clear();
object.conn.RollBackTrans();
}
}
object.conn.CommitTrans();
object.closeDatabase();
object=null;
}
/**
* 获取一个指定的数据库操作对象,并进而获取一个数据集对象
* _sqlString:一条select语句,用于获取数据集
* _pageSize:数据集分页大小,若要获取所有,则该项不填
* _curPageTag:指定分页标记名称,不填,则用默认
*/
function getConn(_sqlString,_pageSize,_curPageTag)
{
var object=new DBOperate();
if (_curPageTag!=undefined)
{
object.currentPageTag=_curPageTag;
}
if (_pageSize!=undefined)
{
object.pageSize=_pageSize;
}
else
{
object.pageSize=999990;
}
object.openDatabase();
object.getRecordSet(_sqlString);
return object;
}
function getRecordData(_sqlString,_fieldArray)
{
var returnValue=new Array();
var object=new DBOperate();
object.openDatabase();
object.getRecordSet(_sqlString);
if (_fieldArray==undefined)
{
while (!object.rs.eof)
{
var data=new HashTable();
for (var i=0;i<object.rs.fields.count;i++)
{
if (object.rs(i).value==null)
{
data.put(object.rs(i).name,"");
}
else
{
data.put(object.rs(i).name,object.rs(i).value);
}
}
returnValue.push(data);
object.rs.moveNext();
}
}
else
{
while (!object.rs.eof)
{
var data=new HashTable();
for (var i=0;i<_fieldArray.length;i++)
{
if (object.rs(_fieldArray[i]).value==null)
{
data.put(_fieldArray[i],"");
}
else
{
data.put(_fieldArray[i],object.rs(_fieldArray[i]).value);
}
}
returnValue.push(data);
object.rs.moveNext();
}
}
object.closeDatabase();
return returnValue;
}
//获取参数,行成Map,执行持久化
//获取post及get的数据,形成map返回
function parametersToMap()
{
var returnMap=new HashTable();
var parameter;
for (var posts=new Enumerator(Request.Form);!posts.atEnd();posts.moveNext())
{
parameter=posts.item();
returnMap.put(parameter,Request.Form(parameter));
}
for (var gets=new Enumerator(Request.QueryString);!gets.atEnd();gets.moveNext())
{
parameter=gets.item();
returnMap.put(parameter,Request.QueryString(parameter));
}
return returnMap;
}
/**
* 获取记录并形成map返回,若_pkValue为"",则获取表结构
* _entityName:数据表名称
* _pkValue:数据表的主键值,默认主键名称为"id"
*/
function getEntityMap(_entityName,_pkValue,_pkName)
{
if (_pkName==undefined)
{
_pkName="id";
}
var returnMap=new HashTable();
var sqlString;
var sqlString="select top 1 * from "+_entityName;
var columns=getConn(sqlString,1);
for (var i=0;i<columns.rs.fields.count;i++)
{
//returnMap.put(columns.rs(i).name,columns.rs.Fields(i).Type);
returnMap.put(columns.rs(i).name,"");
}
//获取表结构完成
//若_pkValue有值,则再获取所需数据
if (_pkValue!=undefined)
{
sqlString="select * from "+_entityName+" where "+_pkName+"="+_pkValue;
entity=getConn(sqlString,1);
if (!entity.rsIsEmpty())
{
for (var j=0;j<entity.rs.fields.count;j++)
{
if (entity.rs(j).value==null)
{
returnMap.put(entity.rs(j).name,"");
}
else
{
returnMap.put(entity.rs(j).name,entity.rs(j).value);
if ((""+entity.rs(j).value).indexOf("UTC")>0)
{
var getDate=new Date(entity.rs(j).value)
var dateString=getDate.getYear()+"-"+(getDate.getMonth()+1)+"-"+getDate.getDate();
returnMap.put(entity.rs(j).name,dateString);
}
}
}
}
entity.closeDatabase();
entity=null;
}
else
{
var keys=returnMap.keys();
for (var j=0;j<keys.length;j++)
{
returnMap.put(keys[j],"");
}
}
columns.closeDatabase();
columns=null;
return returnMap;
}
/**
* 获取post及get的数据,根据是否存在"id"字段进行更新或插入操作
* _entityName:要操作的数据表名
*/
function parametersToEntity(_entityName,_return,_parameters)
{
if (_parameters==undefined)
{
_parameters=parametersToMap();
}
if (!_parameters.containsKey("action")) return;
checkEntityPermission(_parameters.get("action"));
mapToEntity(_parameters,_entityName,_return);
}
/**
* 指定map到数据库的操作映射
* _map:指定的map
* _entityName:数据表名
*/
function mapToEntity(_map,_entityName,_return)
{
if (_return==undefined)
{
_return=1;
}
var sqlString="";
var action=_map.get("action");
if (action=="" || action==null) return;
_map.remove("action");
checkEntityPermission(action);
if (action=="insert" || action=="ins") //添加记录
{
try
{
_map.remove("id");
}
catch (e)
{}
var nodeId=addNewAndReturnPK(_map,_entityName);
Response.Write('<script language="javascript">alert("添加记录成功!");');
if (parseInt(_return)>0)
{
Response.Write('history.go(-'+_return+');');
}
else
{
Response.Write('document.location="'+_return+'";');
}
Response.Write('</script>');
return;
}
if (action=="update" && _map.containsKey("id")) //修改记录
{
var id=_map.get("id");
_map.remove("id");
updateRecord(_map,_entityName,id);
Response.Write('<script language="javascript">alert("更新记录成功!");');
if (parseInt(_return)>0)
{
Response.Write('history.go(-'+_return+');');
}
else
{
Response.Write('document.location="'+_return+'";');
}
Response.Write('</script>');
return;
}
if (action=="delete" && _map.containsKey("id")) //删除记录
{
sqlString="delete from "+_entityName+" where id="+parseInt(_map.get("id"));
execSql(sqlString);
Response.Write('<script language="javascript">alert("删除记录成功!");');
if (parseInt(_return)>0)
{
Response.Write('history.go(-'+_return+');');
}
else
{
Response.Write('document.location="'+_return+'";');
}
Response.Write('</script>');
return;
}
}
%>
<!------------------------------------------------------------------------------------------------------->
<%
/**
* HashTable的javascript实现
* @author zxub
* 2005-8-18
*/
function HashTable()
{
//表对象
this.hashtable=new Array();
//清空表
this.clear=function()
{
this.hashtable=new Array();
}
//获取表长度
this.size=function()
{
var size=0;
for (var i in this.hashtable)
{
if (this.hashtable[i]!=null)
size++;
}
return size;
}
//判断表是否为空
this.isEmpty=function()
{
return (this.size()==0)?true:false;
}
//插入数据
this.put=function(_key,_value)
{
if (_key==null || _value==null)
{
throw "NullPointerException {" + _key + "},{" + _value + "}";
}
else
{
this.hashtable[_key]=_value;
}
}
//删除键值
this.remove=function(_key)
{
this.hashtable[_key]=null;
}
//获取所选键的键值
this.get=function(_key)
{
return this.hashtable[_key];
}
this.getKeyByValue=function(_value)
{
var keys=this.keys();
var key="";
for (var i=0;i<keys.length;i++)
{
if (this.get(keys[i])==_value)
{
key=keys[i];
}
}
return key;
}
//获取键名数组
this.keys=function()
{
var keys = new Array();
for (var i in this.hashtable)
{
if (this.hashtable[i]!=null)
keys.push(i);
}
return keys;
}
//获取键值数组
this.values=function()
{
var values=new Array();
for (var i in this.hashtable)
{
if (this.hashtable[i] != null)
values.push(this.hashtable[i]);
}
return values;
}
//判断表中是否存在一个选定键名
this.containsKey=function(_key)
{
var exists=false;
for (var i in this.hashtable)
{
if (i==_key && this.hashtable[i]!=null)
{
exists=true;
break;
}
}
return exists;
}
//判断表中是否存在键值为当前值的键
this.containsValue=function(_value)
{
var contains=false;
if (_value!=null)
{
for (var i in this.hashtable)
{
if (this.hashtable[i]==_value)
{
contains=true;
break;
}
}
}
return contains;
}
//获取表中数据字符串表现形式
this.toString=function()
{
var result="";
for (var i in this.hashtable)
{
if (this.hashtable[i] != null)
result+=""+i+":"+this.hashtable[i]+"<br>";
}
return result;
}
}
%>
<!------------------------------------------------------------------------------------------------------->
<%
/**
* md5加密
*/
function md5(sMessage) {
function RotateLeft(lValue, iShiftBits) { return (lValue<<iShiftBits) | (lValue>>>(32-iShiftBits)); }
function AddUnsigned(lX,lY) {
var lX4,lY4,lX8,lY8,lResult;
lX8 = (lX & 0x80000000);
lY8 = (lY & 0x80000000);
lX4 = (lX & 0x40000000);
lY4 = (lY & 0x40000000);
lResult = (lX & 0x3FFFFFFF)+(lY & 0x3FFFFFFF);
if (lX4 & lY4) return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
if (lX4 | lY4) {
if (lResult & 0x40000000) return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);
else return (lResult ^ 0x40000000 ^ lX8 ^ lY8);
} else return (lResult ^ lX8 ^ lY8);
}
function F(x,y,z) { return (x & y) | ((~x) & z); }
function G(x,y,z) { return (x & z) | (y & (~z)); }
function H(x,y,z) { return (x ^ y ^ z); }
function I(x,y,z) { return (y ^ (x | (~z))); }
function FF(a,b,c,d,x,s,ac) {
a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac));
return AddUnsigned(RotateLeft(a, s), b);
}
function GG(a,b,c,d,x,s,ac) {
a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac));
return AddUnsigned(RotateLeft(a, s), b);
}
function HH(a,b,c,d,x,s,ac) {
a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, c, d), x), ac));
return AddUnsigned(RotateLeft(a, s), b);
}
function II(a,b,c,d,x,s,ac) {
a = AddUnsigned(a, AddUnsigned(AddUnsigned(I(b, c, d), x), ac));
return AddUnsigned(RotateLeft(a, s), b);
}
function ConvertToWordArray(sMessage) {
var lWordCount;
var lMessageLength = sMessage.length;
var lNumberOfWords_temp1=lMessageLength + 8;
var lNumberOfWords_temp2=(lNumberOfWords_temp1-(lNumberOfWords_temp1 % 64))/64;
var lNumberOfWords = (lNumberOfWords_temp2+1)*16;
var lWordArray=Array(lNumberOfWords-1);
var lBytePosition = 0;
var lByteCount = 0;
while ( lByteCount < lMessageLength ) {
lWordCount = (lByteCount-(lByteCount % 4))/4;
lBytePosition = (lByteCount % 4)*8;
lWordArray[lWordCount] = (lWordArray[lWordCount] | (sMessage.charCodeAt(lByteCount)<<lBytePosition));
lByteCount++;
}
lWordCount = (lByteCount-(lByteCount % 4))/4;
lBytePosition = (lByteCount % 4)*8;
lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80<<lBytePosition);
lWordArray[lNumberOfWords-2] = lMessageLength<<3;
lWordArray[lNumberOfWords-1] = lMessageLength>>>29;
return lWordArray;
}
function WordToHex(lValue) {
var WordToHexValue="",WordToHexValue_temp="",lByte,lCount;
for (lCount = 0;lCount<=3;lCount++) {
lByte = (lValue>>>(lCount*8)) & 255;
WordToHexValue_temp = "0" + lByte.toString(16);
WordToHexValue = WordToHexValue + WordToHexValue_temp.substr(WordToHexValue_temp.length-2,2);
}
return WordToHexValue;
}
var x=Array();
var k,AA,BB,CC,DD,a,b,c,d
var S11=7, S12=12, S13=17, S14=22;
var S21=5, S22=9 , S23=14, S24=20;
var S31=4, S32=11, S33=16, S34=23;
var S41=6, S42=10, S43=15, S44=21;
x = ConvertToWordArray(sMessage);
a = 0x67452301; b = 0xEFCDAB89; c = 0x98BADCFE; d = 0x10325476;
for (k=0;k<x.length;k+=16) {
AA=a; BB=b; CC=c; DD=d;
a=FF(a,b,c,d,x[k+0], S11,0xD76AA478);
d=FF(d,a,b,c,x[k+1], S12,0xE8C7B756);
c=FF(c,d,a,b,x[k+2], S13,0x242070DB);
b=FF(b,c,d,a,x[k+3], S14,0xC1BDCEEE);
a=FF(a,b,c,d,x[k+4], S11,0xF57C0FAF);
d=FF(d,a,b,c,x[k+5], S12,0x4787C62A);
c=FF(c,d,a,b,x[k+6], S13,0xA8304613);
b=FF(b,c,d,a,x[k+7], S14,0xFD469501);
a=FF(a,b,c,d,x[k+8], S11,0x698098D8);
d=FF(d,a,b,c,x[k+9], S12,0x8B44F7AF);
c=FF(c,d,a,b,x[k+10],S13,0xFFFF5BB1);
b=FF(b,c,d,a,x[k+11],S14,0x895CD7BE);
a=FF(a,b,c,d,x[k+12],S11,0x6B901122);
d=FF(d,a,b,c,x[k+13],S12,0xFD987193);
c=FF(c,d,a,b,x[k+14],S13,0xA679438E);
b=FF(b,c,d,a,x[k+15],S14,0x49B40821);
a=GG(a,b,c,d,x[k+1], S21,0xF61E2562);
d=GG(d,a,b,c,x[k+6], S22,0xC040B340);
c=GG(c,d,a,b,x[k+11],S23,0x265E5A51);
b=GG(b,c,d,a,x[k+0], S24,0xE9B6C7AA);
a=GG(a,b,c,d,x[k+5], S21,0xD62F105D);
d=GG(d,a,b,c,x[k+10],S22,0x2441453);
c=GG(c,d,a,b,x[k+15],S23,0xD8A1E681);
b=GG(b,c,d,a,x[k+4], S24,0xE7D3FBC8);
a=GG(a,b,c,d,x[k+9], S21,0x21E1CDE6);
d=GG(d,a,b,c,x[k+14],S22,0xC33707D6);
c=GG(c,d,a,b,x[k+3], S23,0xF4D50D87);
b=GG(b,c,d,a,x[k+8], S24,0x455A14ED);
a=GG(a,b,c,d,x[k+13],S21,0xA9E3E905);
d=GG(d,a,b,c,x[k+2], S22,0xFCEFA3F8);
c=GG(c,d,a,b,x[k+7], S23,0x676F02D9);
b=GG(b,c,d,a,x[k+12],S24,0x8D2A4C8A);
a=HH(a,b,c,d,x[k+5], S31,0xFFFA3942);
d=HH(d,a,b,c,x[k+8], S32,0x8771F681);
c=HH(c,d,a,b,x[k+11],S33,0x6D9D6122);
b=HH(b,c,d,a,x[k+14],S34,0xFDE5380C);
a=HH(a,b,c,d,x[k+1], S31,0xA4BEEA44);
d=HH(d,a,b,c,x[k+4], S32,0x4BDECFA9);
c=HH(c,d,a,b,x[k+7], S33,0xF6BB4B60);
b=HH(b,c,d,a,x[k+10],S34,0xBEBFBC70);
a=HH(a,b,c,d,x[k+13],S31,0x289B7EC6);
d=HH(d,a,b,c,x[k+0], S32,0xEAA127FA);
c=HH(c,d,a,b,x[k+3], S33,0xD4EF3085);
b=HH(b,c,d,a,x[k+6], S34,0x4881D05);
a=HH(a,b,c,d,x[k+9], S31,0xD9D4D039);
d=HH(d,a,b,c,x[k+12],S32,0xE6DB99E5);
c=HH(c,d,a,b,x[k+15],S33,0x1FA27CF8);
b=HH(b,c,d,a,x[k+2], S34,0xC4AC5665);
a=II(a,b,c,d,x[k+0], S41,0xF4292244);
d=II(d,a,b,c,x[k+7], S42,0x432AFF97);
c=II(c,d,a,b,x[k+14],S43,0xAB9423A7);
b=II(b,c,d,a,x[k+5], S44,0xFC93A039);
a=II(a,b,c,d,x[k+12],S41,0x655B59C3);
d=II(d,a,b,c,x[k+3], S42,0x8F0CCC92);
c=II(c,d,a,b,x[k+10],S43,0xFFEFF47D);
b=II(b,c,d,a,x[k+1], S44,0x85845DD1);
a=II(a,b,c,d,x[k+8], S41,0x6FA87E4F);
d=II(d,a,b,c,x[k+15],S42,0xFE2CE6E0);
c=II(c,d,a,b,x[k+6], S43,0xA3014314);
b=II(b,c,d,a,x[k+13],S44,0x4E0811A1);
a=II(a,b,c,d,x[k+4], S41,0xF7537E82);
d=II(d,a,b,c,x[k+11],S42,0xBD3AF235);
c=II(c,d,a,b,x[k+2], S43,0x2AD7D2BB);
b=II(b,c,d,a,x[k+9], S44,0xEB86D391);
a=AddUnsigned(a,AA); b=AddUnsigned(b,BB); c=AddUnsigned(c,CC); d=AddUnsigned(d,DD);
}
var temp= WordToHex(a)+WordToHex(b)+WordToHex(c)+WordToHex(d);
return temp.toLowerCase();
}
%>
<%
function showGetMap(_map)
{
Response.Write('<script language="javascript">');
Response.Write('function setValue(objectName,value)');
Response.Write('{');
Response.Write('if (!document.getElementById(objectName)) return;');
Response.Write('var object=document.getElementById(objectName);');
Response.Write('if (object.type=="text" || object.type=="textarea" || object.type=="hidden") {object.value=value;}');
Response.Write('else if (object.type=="select-one")');
Response.Write('{');
Response.Write('var selectOptions=object.options;');
Response.Write('for (var i=0;i<selectOptions.length;i++)');
Response.Write('{');
Response.Write('if (selectOptions[i].value==value) {object.selectedIndex=i;break;}');
Response.Write('}');
Response.Write('}');
Response.Write('}');
Response.Write('window.onload=function()');
Response.Write('{');
var keys=_map.keys();
for (var i=0;i<keys.length;i++)
{
Response.Write('setValue("'+keys[i]+'","'+_map.get(keys[i])+'");');
}
Response.Write('}');
Response.Write('</script>');
}
%>
<%
//sql语句防注入
function safeguard()
{
var dangerousString="'|;|and|(|)|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare";
var dangerousArray=dangerousString.split("|");
var killIp=true;
var writeSql=true;
var inIp=getIp();
if (killIp)
{
var alert=false;
var history=getConn("select * from sqlIn where inIp='"+inIp+"'",99999,"##");
while (!history.rs.eof)
{
if (history.rs("killIp").value=="true")
{
alert=true;
break;
}
history.rs.moveNext();
}
history.closeDatabase();
history=null;
if (alert)
{
Response.write("<Script Language=JavaScript>alert('由于你曾经意图修改数据库,你的Ip已经被本系统自动锁定!如想正常访问本站请和管理员联系!');</Script>");
Response.End();
}
}
var parameters=parametersToMap();
var keys=parameters.keys();
for (var i=0;i<keys.length;i++)
{
for (var j=0;j<dangerousArray.length;j++)
{
if ((parameters.get(keys[i])+"").toLowerCase().indexOf(dangerousArray[j])>-1)
{
if (writeSql)
{
var sqlString="Insert into sqlIn (inIp,visitUrl,inKey,inValue,killIp) values('"+inIp+"','"+Request.ServerVariables("URL")+"','"+keys[i]+"','"+(parameters.get(keys[i])+"").replace( /[\']/g,"\"")+"','true')";
execSql(sqlString);
}
Response.Write("<Script Language=JavaScript>alert('请不要在参数中包含非法字符尝试注入!');</Script>");
Response.Write("非法操作!系统做了如下记录:<br>");
Response.Write("操作IP:"+inIp+"<br>");
now=new Date();
H=now.getHours();
M=now.getMinutes();
S=now.getSeconds();
timestr=(H<10)?("0"+H):H;
timestr+=":";
timestr+=(M<10)?("0"+M):M;
timestr+=":";
timestr+=(S<10)?("0"+S):S;
Response.Write("操作时间:"+timestr+"<br>");
Response.Write("操作页面:"+Request.ServerVariables("URL")+"<br>");
Response.Write("提交参数:"+keys[i]+"<br>");
Response.Write("提交数据:"+parameters.get(keys[i]));
Response.End();
}
}
}
}
%>
<!------------------------------------------------------------------------------------------------->
<%
//获取客户端参数
function getIp()
{
var ip=Request.ServerVariables("HTTP_X_FORWARDED_FOR");
if ((ip+"").indexOf(".")<0)
{
ip=Request.ServerVariables("REMOTE_ADDR");
}
return ip;
}
function getOS()
{
var os=Request.ServerVariables("OS");
if ((""+os)=="undefined")
{
os="WIN2003?";
}
return os;
}
//递归创建文件夹
function createFolder(folderPath)
{
var entry;
var path="";
var f;
var fso=Server.CreateObject("Scripting.FileSystemObject");
for (var entries=new Enumerator(folderPath.split("/"));!entries.atEnd();entries.moveNext())
{
path+=("/"+entries.item());
if (!fso.folderexists(Server.mappath(path)))
{
f = fso.CreateFolder(Server.mappath(path));
}
}
}
function getNowTimeString()
{
var now=new Date();
return now.getYear()+"-"+(now.getMonth()+1)+"-"+now.getDate();
}
function fixDate(_dateString)
{
var d=new Date(_dateString);
return d.getYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
}
%>
里面还有些扩展应用,注意Map这个概念,和java里的map是一样。具体能否看懂,我就不管了,多说无益,放在这里,可以自己参考下。