public void CancelUpload(CredentialInfo creinfo, string fileName, string forumid) { if (AuthenticateUser(creinfo)) { string uploadFolder = GetUploadFolder(fileName, forumid); string tempFileName = fileName + _tempExtension; if (File.Exists(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + tempFileName)) { File.Delete(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + tempFileName); } } }
/// <summary> /// WEB权限认证 /// </summary> /// <param name="creinfo">认证信息</param> /// <returns>是否通过验正</returns> private bool AuthenticateUser(CredentialInfo creinfo) { if (creinfo.ForumID > 0) { int olid = Discuz.Forum.OnlineUsers.GetOlidByUid(creinfo.UserID); if (olid > 0) { OnlineUserInfo oluserinfo = Discuz.Forum.OnlineUsers.GetOnlineUser(olid); if (oluserinfo.Userid == creinfo.UserID && Utils.UrlEncode(Discuz.Forum.ForumUtils.SetCookiePassword(oluserinfo.Password.Trim(), GeneralConfigs.GetConfig().Passwordkey)) == creinfo.Password && //检测用户id和口令 creinfo.AuthToken == DES.Encode(string.Format("{0},{1}", oluserinfo.Olid.ToString(), oluserinfo.Username.ToString()), oluserinfo.Password.Substring(0, 10)).Replace("+", "[")) //检查认证信息 { return(true); } } } return(false); }
/// <summary> /// 添加附件 /// </summary> /// <param name="savedfileName">上传之后保存的文件名称</param> /// <param name="fileName">文件名称</param> /// <param name="creinfo">认证信息</param> /// <returns>返回当前插入的附件id</returns> private int AddAttachment(string savedFileName, string fileName, CredentialInfo creinfo) { string UploadDir = GetUploadFolder(savedFileName, creinfo.ForumID.ToString()); AttachmentInfo attachmentinfo = new AttachmentInfo(); string fileextname = Utils.CutString(savedFileName, savedFileName.LastIndexOf(".") + 1).ToLower(); Random random = new Random(unchecked((int)DateTime.Now.Ticks)); string newfilename = string.Format("{0}{1}{2}.{3}", (Environment.TickCount & int.MaxValue).ToString(), random.Next(1000, 99999), random.Next(1000, 99999), fileextname); try { // 如果是bmp jpg png图片类型 if ((fileextname == "bmp" || fileextname == "jpg" || fileextname == "jpeg" || fileextname == "png")) { if (Discuz.Common.Utils.FileExists(UploadDir + savedFileName)) { System.Drawing.Image img = System.Drawing.Image.FromFile(UploadDir + savedFileName); //System.IO.File.Copy(UploadDir + savedFileName, UploadDir + newfilename, true); if (config.Attachimgmaxwidth > 0 && img.Width > config.Attachimgmaxwidth) attachmentinfo.Sys_noupload = "图片宽度为" + img.Width.ToString() + ", 系统允许的最大宽度为" + config.Attachimgmaxwidth.ToString(); if (config.Attachimgmaxheight > 0 && img.Height > config.Attachimgmaxheight) attachmentinfo.Sys_noupload = "图片高度为" + img.Width.ToString() + ", 系统允许的最大高度为" + config.Attachimgmaxheight.ToString(); attachmentinfo.Width = img.Width; attachmentinfo.Height = img.Height; if (config.Watermarkstatus == 0) { img.Dispose(); File.Move(UploadDir + savedFileName, UploadDir + newfilename); } else { if (config.Watermarktype == 1 && File.Exists(Utils.GetMapPath(BaseConfigs.GetForumPath + "watermark/" + config.Watermarkpic))) Discuz.Forum.ForumUtils.AddImageSignPic(img, UploadDir + newfilename, Utils.GetMapPath(BaseConfigs.GetForumPath + "watermark/" + config.Watermarkpic), config.Watermarkstatus, config.Attachimgquality, config.Watermarktransparency); else { string watermarkText; watermarkText = config.Watermarktext.Replace("{1}", config.Forumtitle); watermarkText = watermarkText.Replace("{2}", "http://" + DNTRequest.GetCurrentFullHost() + "/"); watermarkText = watermarkText.Replace("{3}", Utils.GetDate()); watermarkText = watermarkText.Replace("{4}", Utils.GetTime()); Discuz.Forum.ForumUtils.AddImageSignText(img, UploadDir + newfilename, watermarkText, config.Watermarkstatus, config.Attachimgquality, config.Watermarkfontname, config.Watermarkfontsize); } System.IO.File.Delete(UploadDir + savedFileName); } // 获得文件长度 attachmentinfo.Filesize = new FileInfo(UploadDir + newfilename).Length; } } else { System.IO.File.Move(UploadDir + savedFileName, UploadDir + newfilename); attachmentinfo.Filesize = new FileInfo(UploadDir + newfilename).Length; } } catch {} if (Discuz.Common.Utils.FileExists(UploadDir + savedFileName)) { attachmentinfo.Filesize = new FileInfo(UploadDir + savedFileName).Length; attachmentinfo.Filename = GetDirInfo(savedFileName, creinfo.ForumID.ToString()) + savedFileName; } if (Discuz.Common.Utils.FileExists(UploadDir + newfilename)) { attachmentinfo.Filesize = new FileInfo(UploadDir + newfilename).Length; attachmentinfo.Filename = GetDirInfo(newfilename, creinfo.ForumID.ToString()) + newfilename; } //当支持FTP上传附件时 if (FTPs.GetForumAttachInfo != null && FTPs.GetForumAttachInfo.Allowupload == 1) attachmentinfo.Filename = FTPs.GetForumAttachInfo.Remoteurl + "/" + newfilename.Replace("\\", "/"); attachmentinfo.Uid = creinfo.UserID; attachmentinfo.Description = fileextname; attachmentinfo.Filetype = GetContentType(fileextname); attachmentinfo.Attachment = fileName; attachmentinfo.Downloads = 0; attachmentinfo.Postdatetime = DateTime.Now.ToString(); attachmentinfo.Sys_index = 0; //return Discuz.Data.DatabaseProvider.GetInstance().CreateAttachment(attachmentinfo); return Discuz.Data.Attachments.CreateAttachments(attachmentinfo); }
public void CancelUpload(CredentialInfo creinfo, string fileName, string forumid) { if (AuthenticateUser(creinfo)) { string uploadFolder = GetUploadFolder(fileName, forumid); string tempFileName = fileName + _tempExtension; if (File.Exists(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + tempFileName)) File.Delete(@HostingEnvironment.ApplicationPhysicalPath + "/" + uploadFolder + "/" + tempFileName); } }
public AttachmentInfo StoreFileAdvanced(string fileName, byte[] data, int dataLength, string parameters, bool firstChunk, bool lastChunk, CredentialInfo creinfo) { if (AuthenticateUser(creinfo)) { UploadSetInfo uploadSetInfo = GetAttachmentUploadSet(creinfo); string fileextname = Utils.CutString(fileName, fileName.LastIndexOf(".") + 1).ToLower(); if (uploadSetInfo.CanPostAttach && uploadSetInfo.AttachExtensionsNoSize.IndexOf(fileextname) >= 0 && uploadSetInfo.AttachSize > dataLength && Utils.StrIsNullOrEmpty(uploadSetInfo.ErrMessage)) { string uploadFolder = GetUploadFolder(fileName, creinfo.ForumID.ToString()); string tempFileName = fileName + _tempExtension; if (firstChunk) { //删除临时文件 if (File.Exists(@HostingEnvironment.ApplicationPhysicalPath + "/upload/temp/" + tempFileName)) File.Delete(@HostingEnvironment.ApplicationPhysicalPath + "/upload/temp/" + tempFileName); //删除目录文件 if (File.Exists(uploadFolder + "/" + fileName)) File.Delete(uploadFolder + "/" + fileName); } FileStream fs = File.Open(@HostingEnvironment.ApplicationPhysicalPath + "/upload/temp/" + tempFileName, FileMode.Append); fs.Write(data, 0, dataLength); fs.Close(); fs.Dispose(); if (lastChunk) { lock (lockHelper) { string newfilename = (Environment.TickCount & int.MaxValue).ToString() + new Random().Next(1000, 9999) + "." + fileextname; File.Move(HostingEnvironment.ApplicationPhysicalPath + "/upload/temp/" + tempFileName, uploadFolder + "/" + newfilename); try { //当支持FTP上传附件时,使用FTP上传远程附件 if (FTPs.GetForumAttachInfo != null && FTPs.GetForumAttachInfo.Allowupload == 1) { FTPs ftps = new FTPs(); //当不保留本地附件模式时,在上传完成之后删除本地tempfilename文件 ftps.UpLoadFile(newfilename, uploadFolder + "/" + newfilename, FTPs.FTPUploadEnum.ForumAttach); } } catch { ; } return Attachments.GetAttachmentInfo(AddAttachment(newfilename, fileName, creinfo)); } } } } return null; }
public UploadSetInfo GetAttachmentUploadSet(CredentialInfo creinfo) { if (AuthenticateUser(creinfo)) { UserInfo userinfo = Discuz.Forum.Users.GetUserInfo(creinfo.UserID); if (userinfo == null) return new UploadSetInfo("", "", 0, 0, false, 0, "当前用户信息无效,请尝试刷新"); UserGroupInfo usergroupinfo = Discuz.Forum.UserGroups.GetUserGroupInfo(userinfo.Groupid); if (usergroupinfo == null) return new UploadSetInfo("", "", 0, 0, false, 0, "当前用户所属用户组信息无效"); ForumInfo forum = Discuz.Forum.Forums.GetForumInfo(creinfo.ForumID); if (forum == null) return new UploadSetInfo(null, null, 0, 0, false, 0, "当前版块信息无效,请尝试刷新"); //得到用户可以上传的文件类型 StringBuilder sbAttachmentTypeSelect = new StringBuilder(); if (!usergroupinfo.Attachextensions.Trim().Equals("")) { sbAttachmentTypeSelect.Append("[id] in ("); sbAttachmentTypeSelect.Append(usergroupinfo.Attachextensions); sbAttachmentTypeSelect.Append(")"); } if (!forum.Attachextensions.Equals("")) { if (sbAttachmentTypeSelect.Length > 0) sbAttachmentTypeSelect.Append(" AND "); sbAttachmentTypeSelect.Append("[id] in ("); sbAttachmentTypeSelect.Append(forum.Attachextensions); sbAttachmentTypeSelect.Append(")"); } string attachextensions = Discuz.Forum.Attachments.GetAttachmentTypeArray(sbAttachmentTypeSelect.ToString()); string attachextensionsnosize = Discuz.Forum.Attachments.GetAttachmentTypeString(sbAttachmentTypeSelect.ToString()); //得到今天允许用户上传的附件总大小(字节) int MaxTodaySize = 0; if (creinfo.UserID > 0) MaxTodaySize = Discuz.Forum.Attachments.GetUploadFileSizeByuserid(creinfo.UserID); int attachsize = usergroupinfo.Maxsizeperday - MaxTodaySize;//今天可上传大小 bool canpostattach = false; //是否允许上传附件 //是否有上传附件的权限 if (Discuz.Forum.Forums.AllowPostAttachByUserID(forum.Permuserlist, creinfo.UserID)) canpostattach = true; else { if (forum.Postattachperm == "") { if (usergroupinfo.Allowpostattach == 1) canpostattach = true; } else { if (Discuz.Forum.Forums.AllowPostAttach(forum.Postattachperm, usergroupinfo.Groupid)) canpostattach = true; } } return new UploadSetInfo(attachextensions, attachextensionsnosize, MaxTodaySize, attachsize, canpostattach, usergroupinfo.Maxattachsize, ""); } return new UploadSetInfo("", "", 0, 0, false, 0, "当前用户信息无效,请尝试刷新"); }
/// <summary> /// WEB权限认证 /// </summary> /// <param name="creinfo">认证信息</param> /// <returns>是否通过验正</returns> private bool AuthenticateUser(CredentialInfo creinfo) { if (creinfo.ForumID > 0) { int olid = Discuz.Forum.OnlineUsers.GetOlidByUid(creinfo.UserID); if (olid > 0) { OnlineUserInfo oluserinfo = Discuz.Forum.OnlineUsers.GetOnlineUser(olid); if (oluserinfo.Userid == creinfo.UserID && Utils.UrlEncode(Discuz.Forum.ForumUtils.SetCookiePassword(oluserinfo.Password.Trim(), GeneralConfigs.GetConfig().Passwordkey)) == creinfo.Password &&//检测用户id和口令 creinfo.AuthToken == DES.Encode(string.Format("{0},{1}", oluserinfo.Olid.ToString(), oluserinfo.Username.ToString()), oluserinfo.Password.Substring(0, 10)).Replace("+", "["))//检查认证信息 { return true; } } } return false; }
/// <summary> /// 添加附件 /// </summary> /// <param name="savedfileName">上传之后保存的文件名称</param> /// <param name="fileName">文件名称</param> /// <param name="creinfo">认证信息</param> /// <returns>返回当前插入的附件id</returns> private int AddAttachment(string savedFileName, string fileName, CredentialInfo creinfo) { string UploadDir = GetUploadFolder(savedFileName, creinfo.ForumID.ToString()); AttachmentInfo attachmentinfo = new AttachmentInfo(); string fileextname = Utils.CutString(savedFileName, savedFileName.LastIndexOf(".") + 1).ToLower(); Random random = new Random(unchecked ((int)DateTime.Now.Ticks)); string newfilename = string.Format("{0}{1}{2}.{3}", (Environment.TickCount & int.MaxValue).ToString(), random.Next(1000, 99999), random.Next(1000, 99999), fileextname); try { // 如果是bmp jpg png图片类型 if ((fileextname == "bmp" || fileextname == "jpg" || fileextname == "jpeg" || fileextname == "png")) { if (Discuz.Common.Utils.FileExists(UploadDir + savedFileName)) { System.Drawing.Image img = System.Drawing.Image.FromFile(UploadDir + savedFileName); //System.IO.File.Copy(UploadDir + savedFileName, UploadDir + newfilename, true); if (config.Attachimgmaxwidth > 0 && img.Width > config.Attachimgmaxwidth) { attachmentinfo.Sys_noupload = "图片宽度为" + img.Width.ToString() + ", 系统允许的最大宽度为" + config.Attachimgmaxwidth.ToString(); } if (config.Attachimgmaxheight > 0 && img.Height > config.Attachimgmaxheight) { attachmentinfo.Sys_noupload = "图片高度为" + img.Width.ToString() + ", 系统允许的最大高度为" + config.Attachimgmaxheight.ToString(); } attachmentinfo.Width = img.Width; attachmentinfo.Height = img.Height; if (config.Watermarkstatus == 0) { img.Dispose(); File.Move(UploadDir + savedFileName, UploadDir + newfilename); } else { if (config.Watermarktype == 1 && File.Exists(Utils.GetMapPath(BaseConfigs.GetForumPath + "watermark/" + config.Watermarkpic))) { Discuz.Forum.ForumUtils.AddImageSignPic(img, UploadDir + newfilename, Utils.GetMapPath(BaseConfigs.GetForumPath + "watermark/" + config.Watermarkpic), config.Watermarkstatus, config.Attachimgquality, config.Watermarktransparency); } else { string watermarkText; watermarkText = config.Watermarktext.Replace("{1}", config.Forumtitle); watermarkText = watermarkText.Replace("{2}", "http://" + DNTRequest.GetCurrentFullHost() + "/"); watermarkText = watermarkText.Replace("{3}", Utils.GetDate()); watermarkText = watermarkText.Replace("{4}", Utils.GetTime()); Discuz.Forum.ForumUtils.AddImageSignText(img, UploadDir + newfilename, watermarkText, config.Watermarkstatus, config.Attachimgquality, config.Watermarkfontname, config.Watermarkfontsize); } System.IO.File.Delete(UploadDir + savedFileName); } // 获得文件长度 attachmentinfo.Filesize = new FileInfo(UploadDir + newfilename).Length; } } else { System.IO.File.Move(UploadDir + savedFileName, UploadDir + newfilename); attachmentinfo.Filesize = new FileInfo(UploadDir + newfilename).Length; } } catch {} if (Discuz.Common.Utils.FileExists(UploadDir + savedFileName)) { attachmentinfo.Filesize = new FileInfo(UploadDir + savedFileName).Length; attachmentinfo.Filename = GetDirInfo(savedFileName, creinfo.ForumID.ToString()) + savedFileName; } if (Discuz.Common.Utils.FileExists(UploadDir + newfilename)) { attachmentinfo.Filesize = new FileInfo(UploadDir + newfilename).Length; attachmentinfo.Filename = GetDirInfo(newfilename, creinfo.ForumID.ToString()) + newfilename; } //当支持FTP上传附件时 if (FTPs.GetForumAttachInfo != null && FTPs.GetForumAttachInfo.Allowupload == 1) { attachmentinfo.Filename = FTPs.GetForumAttachInfo.Remoteurl + "/" + newfilename.Replace("\\", "/"); } attachmentinfo.Uid = creinfo.UserID; attachmentinfo.Description = fileextname; attachmentinfo.Filetype = GetContentType(fileextname); attachmentinfo.Attachment = fileName; attachmentinfo.Downloads = 0; attachmentinfo.Postdatetime = DateTime.Now.ToString(); attachmentinfo.Sys_index = 0; //return Discuz.Data.DatabaseProvider.GetInstance().CreateAttachment(attachmentinfo); return(Discuz.Data.Attachments.CreateAttachments(attachmentinfo)); }
public AttachmentInfo StoreFileAdvanced(string fileName, byte[] data, int dataLength, string parameters, bool firstChunk, bool lastChunk, CredentialInfo creinfo) { if (AuthenticateUser(creinfo)) { UploadSetInfo uploadSetInfo = GetAttachmentUploadSet(creinfo); string fileextname = Utils.CutString(fileName, fileName.LastIndexOf(".") + 1).ToLower(); if (uploadSetInfo.CanPostAttach && uploadSetInfo.AttachExtensionsNoSize.IndexOf(fileextname) >= 0 && uploadSetInfo.AttachSize > dataLength && Utils.StrIsNullOrEmpty(uploadSetInfo.ErrMessage)) { string uploadFolder = GetUploadFolder(fileName, creinfo.ForumID.ToString()); string tempFileName = fileName + _tempExtension; if (firstChunk) { //删除临时文件 if (File.Exists(@HostingEnvironment.ApplicationPhysicalPath + "/upload/temp/" + tempFileName)) { File.Delete(@HostingEnvironment.ApplicationPhysicalPath + "/upload/temp/" + tempFileName); } //删除目录文件 if (File.Exists(uploadFolder + "/" + fileName)) { File.Delete(uploadFolder + "/" + fileName); } } FileStream fs = File.Open(@HostingEnvironment.ApplicationPhysicalPath + "/upload/temp/" + tempFileName, FileMode.Append); fs.Write(data, 0, dataLength); fs.Close(); fs.Dispose(); if (lastChunk) { lock (lockHelper) { string newfilename = (Environment.TickCount & int.MaxValue).ToString() + new Random().Next(1000, 9999) + "." + fileextname; File.Move(HostingEnvironment.ApplicationPhysicalPath + "/upload/temp/" + tempFileName, uploadFolder + "/" + newfilename); try { //当支持FTP上传附件时,使用FTP上传远程附件 if (FTPs.GetForumAttachInfo != null && FTPs.GetForumAttachInfo.Allowupload == 1) { FTPs ftps = new FTPs(); //当不保留本地附件模式时,在上传完成之后删除本地tempfilename文件 ftps.UpLoadFile(newfilename, uploadFolder + "/" + newfilename, FTPs.FTPUploadEnum.ForumAttach); } } catch { ; } return(Attachments.GetAttachmentInfo(AddAttachment(newfilename, fileName, creinfo))); } } } } return(null); }
public UploadSetInfo GetAttachmentUploadSet(CredentialInfo creinfo) { if (AuthenticateUser(creinfo)) { UserInfo userinfo = Discuz.Forum.Users.GetUserInfo(creinfo.UserID); if (userinfo == null) { return(new UploadSetInfo("", "", 0, 0, false, 0, "当前用户信息无效,请尝试刷新")); } UserGroupInfo usergroupinfo = Discuz.Forum.UserGroups.GetUserGroupInfo(userinfo.Groupid); if (usergroupinfo == null) { return(new UploadSetInfo("", "", 0, 0, false, 0, "当前用户所属用户组信息无效")); } ForumInfo forum = Discuz.Forum.Forums.GetForumInfo(creinfo.ForumID); if (forum == null) { return(new UploadSetInfo(null, null, 0, 0, false, 0, "当前版块信息无效,请尝试刷新")); } //得到用户可以上传的文件类型 StringBuilder sbAttachmentTypeSelect = new StringBuilder(); if (!usergroupinfo.Attachextensions.Trim().Equals("")) { sbAttachmentTypeSelect.Append("[id] in ("); sbAttachmentTypeSelect.Append(usergroupinfo.Attachextensions); sbAttachmentTypeSelect.Append(")"); } if (!forum.Attachextensions.Equals("")) { if (sbAttachmentTypeSelect.Length > 0) { sbAttachmentTypeSelect.Append(" AND "); } sbAttachmentTypeSelect.Append("[id] in ("); sbAttachmentTypeSelect.Append(forum.Attachextensions); sbAttachmentTypeSelect.Append(")"); } string attachextensions = Discuz.Forum.Attachments.GetAttachmentTypeArray(sbAttachmentTypeSelect.ToString()); string attachextensionsnosize = Discuz.Forum.Attachments.GetAttachmentTypeString(sbAttachmentTypeSelect.ToString()); //得到今天允许用户上传的附件总大小(字节) int MaxTodaySize = 0; if (creinfo.UserID > 0) { MaxTodaySize = Discuz.Forum.Attachments.GetUploadFileSizeByuserid(creinfo.UserID); } int attachsize = usergroupinfo.Maxsizeperday - MaxTodaySize; //今天可上传大小 bool canpostattach = false; //是否允许上传附件 //是否有上传附件的权限 if (Discuz.Forum.Forums.AllowPostAttachByUserID(forum.Permuserlist, creinfo.UserID)) { canpostattach = true; } else { if (forum.Postattachperm == "") { if (usergroupinfo.Allowpostattach == 1) { canpostattach = true; } } else { if (Discuz.Forum.Forums.AllowPostAttach(forum.Postattachperm, usergroupinfo.Groupid)) { canpostattach = true; } } } return(new UploadSetInfo(attachextensions, attachextensionsnosize, MaxTodaySize, attachsize, canpostattach, usergroupinfo.Maxattachsize, "")); } return(new UploadSetInfo("", "", 0, 0, false, 0, "当前用户信息无效,请尝试刷新")); }