建站代码网

热门标签

KindEditor上传文件加cookies/session权限验证

KindEditor编辑器上传文件是没有用户登录验证和权限验证的,即任何人都可以上传,这样很不安全,会有文件上传漏洞。于是,我们就需要加一个权限验证了。

以下是KindEditor 4.1.11的ASP接口示例:

KindEditor中单个图片及文件上传,可以直接在  KindEditor/asp/upload_json.asp 这个文件顶部加入如下代码:


<% '……省略包含文件及打开数据库和DB函数的代码 if len(request.cookies("username"))>0 and len(request.cookies("password"))>0 then dim rs Set Rs = DB("Select [password] From [Admin] Where [Username]='" & username & "'",1) If Not Rs.Eof Then if rs(0)<>password then response.End() else response.End() End If : Rs.Close else response.End() end if %> 说明:单个图片及文件上传时,upload_json.asp文件可以获取cookies和session,因此可以直接写入验证程序。

然而,KindEditor批量图片上传功能,采用的是FLASH方式,FLASH无法直接获取cookies和session,需要传值进去才能不丢失cookies和session。

所以,KindEditor上传文件加入cookies/session权限验证的最终解决方法如下:

调用KindEditor的页面中,如:



<link rel="stylesheet" href="kindeditor/themes/default/default.css" /> <script charset="utf-8" src="kindeditor/kindeditor-all.js"></script> <script charset="utf-8" src="kindeditor/lang/zh-CN.js"></script> <script> var KE; KindEditor.ready(function(K) { KE = K.create('#Content',{ uploadJson : 'kindeditor/asp/upload_json.asp?username=<%=request.cookies("username")%>&password=<%=request.cookies("password")%>', fileManagerJson : 'kindeditor/asp/file_manager_json.asp', allowImageUpload: true, //多图上传 allowFileManager : true, //浏览图片空间 filterMode : false, //HTML特殊代码过滤 afterBlur: function(){ this.sync(); } //编辑器失去焦点(blur)时执行的回调函数(将编辑器的HTML数据同步到textarea) }); }); </script> <textarea id="Content" name="Content" style=" width: 99%; height: 500px; visibility: hidden; display: block; "></textarea>

KindEditor/asp/upload_json.asp 这个文件中,在顶部加入如下代码:

<% '……省略包含文件及打开数据库和DB函数的代码 dim username,password username=trim(request.QueryString("username")) password=trim(request.QueryString("password")) username=Replace(Replace(Replace(Replace(Replace(Replace(username,"'",""),")",""),">",""),"*",""),"?",""),"%","") password=Replace(Replace(Replace(Replace(Replace(Replace(password,"'",""),")",""),">",""),"*",""),"?",""),"%","") If Len(username)>0 And Len(password)>0 Then dim rs Set Rs = DB("Select [password] From [Admin] Where [Username]='" & username & "'",1) If Not Rs.Eof Then If rs(0)<>password Then response.End() else response.End() End If : Rs.Close else response.End() End If %>


X