/// <summary> /// 更新 /// </summary> /// <param name="comment"></param> /// <returns></returns> public static int UpdateComment(CommentInfo comment) { _recentcomments = null; return dao.UpdateComment(comment); }
/// <summary> /// 添加 /// </summary> /// <param name="comment"></param> /// <returns></returns> public static int InsertComment(CommentInfo comment) { int result = dao.InsertComment(comment); //统计 StatisticsManager.UpdateStatisticsCommentCount(1); //用户 UserManager.UpdateUserCommentCount(comment.UserId, 1); //文章 PostManager.UpdatePostCommentCount(comment.PostId, 1); _recentcomments = null; return result; }
/// <summary> /// 添加评论 /// </summary> protected void AddComment() { int contentMaxLength = 1000; //内容最长长度 int postid = RequestHelper.FormInt("postid", 0); string author = StringHelper.CutString(RequestHelper.FormString("commentauthor"), 20); string email = StringHelper.CutString(RequestHelper.FormString("commentemail"), 50); string siteurl = StringHelper.CutString(RequestHelper.FormString("commentsiteurl"), 100); string content = RequestHelper.FormString("commentcontent"); int remeber = RequestHelper.FormInt("commentremember", 0); int emailnotify = RequestHelper.FormInt("commentemailnotify", 0); string verifycode = RequestHelper.FormString("commentverifycode"); int expires = 525600; //一年 if (remeber == 0) { expires = -1; } if (!PageUtils.IsLogin) { PageUtils.SetCookie(TagFields.COMMENT_AUTHOR, Server.UrlEncode(author), expires); PageUtils.SetCookie(TagFields.COMMENT_EMAIL, Server.UrlEncode(email), expires); PageUtils.SetCookie(TagFields.COMMENT_SITEURL, Server.UrlEncode(siteurl), expires); } // PageUtils.SetCookie("commentcontent", Server.UrlEncode(StringHelper.CutString(content, contentMaxLength, "...")), expires); //保留 //第一次发表评论,并失败时用到 th.Put(TagFields.COMMENT_AUTHOR, author); th.Put(TagFields.COMMENT_EMAIL, email); th.Put(TagFields.COMMENT_SITEURL, siteurl); th.Put(TagFields.COMMENT_CONTENT, content); if (SettingManager.GetSetting().EnableVerifyCode == 1 && (verifycode != PageUtils.VerifyCode || string.IsNullOrEmpty(verifycode))) { th.Put(TagFields.COMMENT_MESSAGE, "<div>验证码输入错误!</div>"); return; } if (!StringHelper.IsEmail(email)) { th.Put(TagFields.COMMENT_MESSAGE, "<div>邮箱格式错误!</div>"); return; } if (content.Length > contentMaxLength || content.Length == 0) { th.Put(TagFields.COMMENT_MESSAGE, "<div>评论不能为空且限制在" + contentMaxLength + "字以内!</div>"); return; } // PageUtils.SetCookie("commentcontent", string.Empty, -1); //清空 th.Put(TagFields.COMMENT_CONTENT, string.Empty); //清空 PostInfo post = PostManager.GetPost(postid); if (post == null) { Response.Redirect(ConfigHelper.SitePath); } CommentInfo c = new CommentInfo(); c.Content = StringHelper.TextToHtml(StringHelper.CutString(content, contentMaxLength, "...")); c.CreateDate = DateTime.Now; c.Email = StringHelper.HtmlEncode(email); c.EmailNotify = emailnotify; c.IpAddress = RequestHelper.IPAddress; c.ParentId = 0; c.PostId = postid; c.UserId = PageUtils.CurrentUserId; c.Name = author; if (!string.IsNullOrEmpty(siteurl) && siteurl.IndexOf("http://") == -1) { siteurl = "http://" + siteurl; } c.SiteUrl = StringHelper.HtmlEncode(siteurl); switch (SettingManager.GetSetting().CommentApproved) { case 1: c.Approved = (int)ApprovedStatus.Success; break; case 2: string[] blackwords = SettingManager.GetSetting().CommentSpamwords.Split(','); bool hasBlackword = false; foreach (string word in blackwords) { if (c.Content.IndexOf(word) != -1) { hasBlackword = true; break; } } c.Approved = hasBlackword ? (int)ApprovedStatus.Wait : (int)ApprovedStatus.Success; break; case 3: default: c.Approved = (int)ApprovedStatus.Wait; break; } int newID = CommentManager.InsertComment(c); #region 发邮件 if (SettingManager.GetSetting().SendMailNotifyByComment == 1) //给订阅者发邮件 { //先不考虑审核的问题 List<CommentInfo> list = CommentManager.GetCommentList(int.MaxValue, 1, -1, postid, 0, -1, 1, string.Empty); List<string> emailList = new List<string>(); foreach (CommentInfo cmt in list) { if (!StringHelper.IsEmail(cmt.Email)) { continue; } //自己不用发 if (email == cmt.Email) { continue; } //不重复发送 if (emailList.Contains(cmt.Email)) { continue; } emailList.Add(cmt.Email); string subject = string.Empty; string body = string.Empty; subject = string.Format("[评论订阅通知]{0}", post.Title); body += string.Format("您订阅的{0}有新评论了:<br/>", post.Title); body += "<hr/>"; body += content; body += "<hr/>"; body += "<br />评论作者: " + author; if (!string.IsNullOrEmpty(siteurl)) { body += string.Format(" (<a href=\"{0}\">{0}</a>)", siteurl); } body += "<br />评论时间: " + DateTime.Now; body += string.Format("<br />原文连接: <a href=\"{0}\" title=\"{1}\" >{1}</a>", post.Url, post.Title); body += "<br />注:系统自动通知邮件,不要回复。"; EmailHelper.SendAsync(cmt.Email, subject, body); } } if (SettingManager.GetSetting().SendMailAuthorByComment == 1) //给文章作者发邮件 { string subject = string.Empty; string body = string.Empty; subject = string.Format("[新评论通知]{0}", post.Title); body += string.Format("您发表的{0}有新评论了:<br/>", post.Title); body += "<hr/>"; body += content; body += "<hr/>"; body += "<br />评论作者: " + author; if (!string.IsNullOrEmpty(siteurl)) { body += string.Format(" (<a href=\"{0}\">{0}</a>)", siteurl); } body += "<br />评论时间: " + DateTime.Now; body += string.Format("<br />原文连接: <a href=\"{0}\" title=\"{1}\" >{1}</a>", post.Url, post.Title); body += "<br />注:系统自动通知邮件,不要回复。"; UserInfo user = UserManager.GetUser(post.UserId); if (user != null && StringHelper.IsEmail(user.Email)) { EmailHelper.SendAsync(user.Email, subject, body); } } #endregion if (newID > 0) { if (post != null) { if (SettingManager.GetSetting().CommentOrder == 1) { Response.Redirect(post.Url + "#comment-" + newID); } else { int commentCount = CommentManager.GetCommentCount(postid, false); int pageCount = commentCount / SettingManager.GetSetting().PageSizeCommentCount; if (commentCount % SettingManager.GetSetting().PageSizeCommentCount > 0) { pageCount += 1; } string url = string.Format(post.PageUrl + "#comment-" + newID, pageCount); Response.Redirect(url); } } } }
/// <summary> /// 数据转换 /// </summary> /// <param name="read"></param> /// <returns></returns> private List<CommentInfo> DataReaderToCommentList(OleDbDataReader read) { List<CommentInfo> list = new List<CommentInfo>(); while (read.Read()) { CommentInfo comment = new CommentInfo(); comment.CommentId = Convert.ToInt32(read["CommentId"]); comment.ParentId = Convert.ToInt32(read["ParentId"]); comment.PostId = Convert.ToInt32(read["PostId"]); comment.UserId = Convert.ToInt32(read["UserId"]); comment.Name = Convert.ToString(read["Name"]); comment.Email = Convert.ToString(read["Email"]); comment.SiteUrl = Convert.ToString(read["SiteUrl"]); comment.Content = Convert.ToString(read["Content"]); comment.EmailNotify = Convert.ToInt32(read["EmailNotify"]); comment.IpAddress = Convert.ToString(read["IpAddress"]); comment.CreateDate = Convert.ToDateTime(read["CreateDate"]); comment.Approved = Convert.ToInt32(read["Approved"]); list.Add(comment); } read.Close(); return list; }
/// <summary> /// 更新 /// </summary> /// <param name="comment"></param> /// <returns></returns> public int UpdateComment(CommentInfo comment) { string cmdText = @"update [loachs_comments] set PostId=@PostId, ParentId=@ParentId, UserId=@UserId, Name=@Name, Email=@Email, SiteUrl=@SiteUrl, Content=@Content, EmailNotify=@EmailNotify, IpAddress=@IpAddress, CreateDate=@CreateDate, Approved=@Approved where CommentId=@CommentId "; OleDbParameter[] prams = { OleDbHelper.MakeInParam("@PostId", OleDbType.Integer,4, comment.PostId), OleDbHelper.MakeInParam("@ParentId", OleDbType.Integer,4, comment.ParentId), OleDbHelper.MakeInParam("@UserId", OleDbType.Integer,4, comment.UserId), OleDbHelper.MakeInParam("@Name", OleDbType.VarWChar,255, comment.Name), OleDbHelper.MakeInParam("@Email", OleDbType.VarWChar,255, comment.Email), OleDbHelper.MakeInParam("@SiteUrl", OleDbType.VarWChar,255, comment.SiteUrl), OleDbHelper.MakeInParam("@Content", OleDbType.VarWChar,255, comment.Content), OleDbHelper.MakeInParam("@EmailNotify", OleDbType.Integer,4 , comment.EmailNotify), OleDbHelper.MakeInParam("@IpAddress", OleDbType.VarWChar,255, comment.IpAddress), OleDbHelper.MakeInParam("@CreateDate", OleDbType.Date,8, comment.CreateDate), OleDbHelper.MakeInParam("@Approved", OleDbType.Integer,4 , comment.Approved), OleDbHelper.MakeInParam("@CommentId", OleDbType.Integer,4, comment.CommentId), }; return OleDbHelper.ExecuteNonQuery(CommandType.Text, cmdText, prams); }
/// <summary> /// 添加 /// </summary> /// <param name="comment"></param> /// <returns></returns> public int InsertComment(CommentInfo comment) { string cmdText = @"insert into [loachs_comments]( PostId, ParentId,UserId,Name,Email,SiteUrl,Content,EmailNotify,IpAddress,CreateDate,Approved) values ( @PostId, @ParentId,@UserId,@Name,@Email,@SiteUrl,@Content,@EmailNotify,@IpAddress,@CreateDate,@Approved)"; OleDbParameter[] prams = { OleDbHelper.MakeInParam("@PostId", OleDbType.Integer,4, comment.PostId), OleDbHelper.MakeInParam("@ParentId", OleDbType.Integer,4, comment.ParentId), OleDbHelper.MakeInParam("@UserId", OleDbType.Integer,4, comment.UserId), OleDbHelper.MakeInParam("@Name", OleDbType.VarWChar,255, comment.Name), OleDbHelper.MakeInParam("@Email", OleDbType.VarWChar,255, comment.Email), OleDbHelper.MakeInParam("@SiteUrl", OleDbType.VarWChar,255, comment.SiteUrl), OleDbHelper.MakeInParam("@Content", OleDbType.VarWChar,255, comment.Content), OleDbHelper.MakeInParam("@EmailNotify", OleDbType.Integer,4 , comment.EmailNotify), OleDbHelper.MakeInParam("@IpAddress", OleDbType.VarWChar,255, comment.IpAddress), OleDbHelper.MakeInParam("@CreateDate", OleDbType.Date,8, comment.CreateDate), OleDbHelper.MakeInParam("@Approved", OleDbType.Integer,4 , comment.Approved), }; OleDbHelper.ExecuteNonQuery(CommandType.Text, cmdText, prams); int newId = Convert.ToInt32(OleDbHelper.ExecuteScalar("select top 1 [CommentId] from [loachs_comments] order by [CommentId] desc")); return newId; }