예제 #1
0
        public async Task <ActionResult> Submit(LeaveMessageCommand dto)
        {
            var match = Regex.Match(dto.NickName + dto.Content.RemoveHtmlTag(), CommonHelper.BanRegex);

            if (match.Success)
            {
                LogManager.Info($"提交内容:{dto.NickName}/{dto.Content},敏感词:{match.Value}");
                return(ResultData(null, false, "您提交的内容包含敏感词,被禁止发表,请检查您的内容后尝试重新提交!"));
            }

            dto.Content = dto.Content.Trim().Replace("<p><br></p>", string.Empty);
            if (MsgFeq.GetOrAdd("Comments:" + ClientIP, 1) > 2)
            {
                MsgFeq.Expire("Comments:" + ClientIP, TimeSpan.FromMinutes(1));
                return(ResultData(null, false, "您的发言频率过快,请稍后再发表吧!"));
            }

            var msg = dto.Mapper <LeaveMessage>();

            if (Regex.Match(dto.NickName + dto.Content, CommonHelper.ModRegex).Length <= 0)
            {
                msg.Status = Status.Published;
            }

            msg.PostDate = DateTime.Now;
            var user = HttpContext.Session.Get <UserInfoDto>(SessionKey.UserInfo);

            if (user != null)
            {
                msg.NickName   = user.NickName;
                msg.QQorWechat = user.QQorWechat;
                msg.Email      = user.Email;
                if (user.IsAdmin)
                {
                    msg.Status   = Status.Published;
                    msg.IsMaster = true;
                }
            }

            msg.Content  = dto.Content.HtmlSantinizerStandard().ClearImgAttributes();
            msg.Browser  = dto.Browser ?? Request.Headers[HeaderNames.UserAgent];
            msg.IP       = ClientIP;
            msg.Location = Request.Location();
            msg          = LeaveMessageService.AddEntitySaved(msg);
            if (msg == null)
            {
                return(ResultData(null, false, "留言发表失败!"));
            }

            MsgFeq.AddOrUpdate("Comments:" + ClientIP, 1, i => i + 1, 5);
            MsgFeq.Expire("Comments:" + ClientIP, TimeSpan.FromMinutes(1));
            var email   = CommonHelper.SystemSettings["ReceiveEmail"];
            var content = new Template(await System.IO.File.ReadAllTextAsync(HostEnvironment.WebRootPath + "/template/notify.html")).Set("title", "网站留言板").Set("time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")).Set("nickname", msg.NickName).Set("content", msg.Content);

            if (msg.Status == Status.Published)
            {
                if (!msg.IsMaster)
                {
                    await MessageService.AddEntitySavedAsync(new InternalMessage()
                    {
                        Title   = $"来自【{msg.NickName}】的新留言",
                        Content = msg.Content,
                        Link    = Url.Action("Index", "Msg", new { cid = msg.Id }, Request.Scheme)
                    });
                }
                if (msg.ParentId == 0)
                {
                    //新评论,只通知博主
                    BackgroundJob.Enqueue(() => CommonHelper.SendMail(Request.Host + "|博客新留言:", content.Set("link", Url.Action("Index", "Msg", new { cid = msg.Id }, Request.Scheme)).Render(false), email, ClientIP));
                }
                else
                {
                    //通知博主和上层所有关联的评论访客
                    var    pid    = LeaveMessageService.GetParentMessageIdByChildId(msg.Id);
                    var    emails = LeaveMessageService.GetSelfAndAllChildrenMessagesByParentId(pid).Select(c => c.Email).Append(email).Except(new[] { msg.Email }).ToHashSet();
                    string link   = Url.Action("Index", "Msg", new { cid = msg.Id }, Request.Scheme);
                    foreach (var s in emails)
                    {
                        BackgroundJob.Enqueue(() => CommonHelper.SendMail($"{Request.Host}{CommonHelper.SystemSettings["Title"]} 留言回复:", content.Set("link", link).Render(false), s, ClientIP));
                    }
                }
                return(ResultData(null, true, "留言发表成功,服务器正在后台处理中,这会有一定的延迟,稍后将会显示到列表中!"));
            }

            BackgroundJob.Enqueue(() => CommonHelper.SendMail(Request.Host + "|博客新留言(待审核):", content.Set("link", Url.Action("Index", "Msg", new
            {
                cid = msg.Id
            }, Request.Scheme)).Render(false) + "<p style='color:red;'>(待审核)</p>", email, ClientIP));
            return(ResultData(null, true, "留言发表成功,待站长审核通过以后将显示到列表中!"));
        }
예제 #2
0
        public ActionResult Put(LeaveMessageInputDto msg)
        {
            UserInfoOutputDto user = HttpContext.Session.Get <UserInfoOutputDto>(SessionKey.UserInfo);

            msg.Content = msg.Content.Trim().Replace("<p><br></p>", string.Empty);
            if (msg.Content.RemoveHtml().Trim().Equals(HttpContext.Session.Get <string>("msg")))
            {
                return(ResultData(null, false, "您刚才已经发表过一次留言了!"));
            }
            if (Regex.Match(msg.Content, CommonHelper.ModRegex).Length <= 0)
            {
                msg.Status = Status.Pended;
            }

            if (user != null)
            {
                msg.NickName   = user.NickName;
                msg.QQorWechat = user.QQorWechat;
                msg.Email      = user.Email;
                if (user.IsAdmin)
                {
                    msg.Status   = Status.Pended;
                    msg.IsMaster = true;
                }
            }
            msg.PostDate = DateTime.Now;
            msg.Content  = Regex.Replace(msg.Content.HtmlSantinizerStandard().ConvertImgSrcToRelativePath(), @"<img\s+[^>]*\s*src\s*=\s*['""]?(\S+\.\w{3,4})['""]?[^/>]*/>", "<img src=\"$1\"/>");
            msg.Browser  = msg.Browser ?? Request.Headers[HeaderNames.UserAgent];
            msg.IP       = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
            LeaveMessage msg2 = LeaveMessageService.AddEntitySaved(msg.Mapper <LeaveMessage>());

            if (msg2 != null)
            {
                HttpContext.Session.Set("msg", msg.Content.RemoveHtml().Trim());
                var    email   = CommonHelper.SystemSettings["ReceiveEmail"];
                string content = System.IO.File.ReadAllText(_hostingEnvironment.WebRootPath + "/template/notify.html").Replace("{{title}}", "网站留言板").Replace("{{time}}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")).Replace("{{nickname}}", msg2.NickName).Replace("{{content}}", msg2.Content);
                if (msg.Status == Status.Pended)
                {
                    if (!msg2.IsMaster)
                    {
                        MessageService.AddEntitySaved(new InternalMessage()
                        {
                            Title   = $"来自【{msg2.NickName}】的新留言",
                            Content = msg2.Content,
                            Link    = Url.Action("Index", "Msg", new
                            {
                                cid = msg2.Id
                            }, Request.Scheme)
                        });
                    }
#if !DEBUG
                    if (msg.ParentId == 0)
                    {
                        //新评论,只通知博主
                        BackgroundJob.Enqueue(() => CommonHelper.SendMail(HttpUtility.UrlDecode(Request.Headers[HeaderNames.Referer]) + "|博客新留言:", content.Replace("{{link}}", Url.Action("Index", "Msg", new { cid = msg2.Id }, Request.Scheme)), email));
                    }
                    else
                    {
                        //通知博主和上层所有关联的评论访客
                        var pid    = LeaveMessageService.GetParentMessageIdByChildId(msg2.Id);
                        var emails = LeaveMessageService.GetSelfAndAllChildrenMessagesByParentId(pid).Select(c => c.Email).ToList();
                        emails.Add(email);
                        string link = Url.Action("Index", "Msg", new { cid = msg2.Id }, Request.Scheme);
                        foreach (var s in emails.Distinct().Except(new[] { msg2.Email }))
                        {
                            BackgroundJob.Enqueue(() => CommonHelper.SendMail($"{HttpUtility.UrlDecode(Request.Headers[HeaderNames.Referer])}{CommonHelper.SystemSettings["Title"]} 留言回复:", content.Replace("{{link}}", link), s));
                        }
                    }
#endif
                    return(ResultData(null, true, "留言发表成功,服务器正在后台处理中,这会有一定的延迟,稍后将会显示到列表中!"));
                }
                BackgroundJob.Enqueue(() => CommonHelper.SendMail(HttpUtility.UrlDecode(Request.Headers[HeaderNames.Referer]) + "|博客新留言(待审核):", content.Replace("{{link}}", Url.Action("Index", "Msg", new
                {
                    cid = msg2.Id
                }, Request.Scheme)) + "<p style='color:red;'>(待审核)</p>", email));
                return(ResultData(null, true, "留言发表成功,待站长审核通过以后将显示到列表中!"));
            }
            return(ResultData(null, false, "留言发表失败!"));
        }
예제 #3
0
        public ActionResult Put(LeaveMessageInputDto dto)
        {
            if (Regex.Match(dto.Content, CommonHelper.BanRegex).Length > 0)
            {
                return(ResultData(null, false, "您提交的内容包含敏感词,被禁止发表,请注意改善您的言辞!"));
            }

            dto.Content = dto.Content.Trim().Replace("<p><br></p>", string.Empty);
            if (dto.Content.RemoveHtmlTag().Trim().Equals(HttpContext.Session.Get <string>("msg")))
            {
                return(ResultData(null, false, "您刚才已经发表过一次留言了!"));
            }

            var msg = dto.Mapper <LeaveMessage>();

            if (Regex.Match(dto.Content, CommonHelper.ModRegex).Length <= 0)
            {
                msg.Status = Status.Pended;
            }

            msg.PostDate = DateTime.Now;
            var user = HttpContext.Session.Get <UserInfoOutputDto>(SessionKey.UserInfo);

            if (user != null)
            {
                msg.NickName   = user.NickName;
                msg.QQorWechat = user.QQorWechat;
                msg.Email      = user.Email;
                if (user.IsAdmin)
                {
                    msg.Status   = Status.Pended;
                    msg.IsMaster = true;
                }
            }

            msg.Content  = dto.Content.HtmlSantinizerStandard().ClearImgAttributes();
            msg.Browser  = dto.Browser ?? Request.Headers[HeaderNames.UserAgent];
            msg.IP       = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
            msg.Location = msg.IP.GetIPLocation().Split("|").Where(s => !int.TryParse(s, out _)).ToHashSet().Join("|");
            msg          = LeaveMessageService.AddEntitySaved(msg);
            if (msg == null)
            {
                return(ResultData(null, false, "留言发表失败!"));
            }

            HttpContext.Session.Set("msg", msg.Content.RemoveHtmlTag().Trim());
            var email   = CommonHelper.SystemSettings["ReceiveEmail"];
            var content = System.IO.File.ReadAllText(HostingEnvironment.WebRootPath + "/template/notify.html").Replace("{{title}}", "网站留言板").Replace("{{time}}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")).Replace("{{nickname}}", msg.NickName).Replace("{{content}}", msg.Content);

            if (msg.Status == Status.Pended)
            {
                if (!msg.IsMaster)
                {
                    MessageService.AddEntitySaved(new InternalMessage()
                    {
                        Title   = $"来自【{msg.NickName}】的新留言",
                        Content = msg.Content,
                        Link    = Url.Action("Index", "Msg", new { cid = msg.Id }, Request.Scheme)
                    });
                }
#if !DEBUG
                if (msg.ParentId == 0)
                {
                    //新评论,只通知博主
                    BackgroundJob.Enqueue(() => CommonHelper.SendMail(CommonHelper.SystemSettings["Domain"] + "|博客新留言:", content.Replace("{{link}}", Url.Action("Index", "Msg", new { cid = msg.Id }, Request.Scheme)), email));
                }
                else
                {
                    //通知博主和上层所有关联的评论访客
                    var    pid    = LeaveMessageService.GetParentMessageIdByChildId(msg.Id);
                    var    emails = LeaveMessageService.GetSelfAndAllChildrenMessagesByParentId(pid).Select(c => c.Email).Append(email).Except(new[] { msg.Email }).ToHashSet();
                    string link   = Url.Action("Index", "Msg", new { cid = msg.Id }, Request.Scheme);
                    foreach (var s in emails)
                    {
                        BackgroundJob.Enqueue(() => CommonHelper.SendMail($"{CommonHelper.SystemSettings["Domain"]}{CommonHelper.SystemSettings["Title"]} 留言回复:", content.Replace("{{link}}", link), s));
                    }
                }
#endif
                return(ResultData(null, true, "留言发表成功,服务器正在后台处理中,这会有一定的延迟,稍后将会显示到列表中!"));
            }

            BackgroundJob.Enqueue(() => CommonHelper.SendMail(CommonHelper.SystemSettings["Domain"] + "|博客新留言(待审核):", content.Replace("{{link}}", Url.Action("Index", "Msg", new
            {
                cid = msg.Id
            }, Request.Scheme)) + "<p style='color:red;'>(待审核)</p>", email));
            return(ResultData(null, true, "留言发表成功,待站长审核通过以后将显示到列表中!"));
        }
예제 #4
0
        public async Task <ActionResult> Submit([FromServices] IMailSender mailSender, LeaveMessageCommand cmd)
        {
            var match = Regex.Match(cmd.NickName + cmd.Content.RemoveHtmlTag(), CommonHelper.BanRegex);

            if (match.Success)
            {
                LogManager.Info($"提交内容:{cmd.NickName}/{cmd.Content},敏感词:{match.Value}");
                return(ResultData(null, false, "您提交的内容包含敏感词,被禁止发表,请检查您的内容后尝试重新提交!"));
            }

            var error = await ValidateEmailCode(mailSender, cmd.Email, cmd.Code);

            if (!string.IsNullOrEmpty(error))
            {
                return(ResultData(null, false, error));
            }

            if (cmd.ParentId > 0 && DateTime.Now - LeaveMessageService[cmd.ParentId.Value, m => m.PostDate] > TimeSpan.FromDays(180))
            {
                return(ResultData(null, false, "当前留言过于久远,不再允许回复!"));
            }

            cmd.Content = cmd.Content.Trim().Replace("<p><br></p>", string.Empty);
            if (MsgFeq.GetOrAdd("Comments:" + ClientIP, 1) > 2)
            {
                MsgFeq.Expire("Comments:" + ClientIP, TimeSpan.FromMinutes(1));
                return(ResultData(null, false, "您的发言频率过快,请稍后再发表吧!"));
            }

            var msg = cmd.Mapper <LeaveMessage>();

            if (cmd.ParentId > 0)
            {
                msg.GroupTag = LeaveMessageService.GetQuery(c => c.Id == cmd.ParentId).Select(c => c.GroupTag).FirstOrDefault();
                msg.Path     = (LeaveMessageService.GetQuery(c => c.Id == cmd.ParentId).Select(c => c.Path).FirstOrDefault() + "," + cmd.ParentId).Trim(',');
            }
            else
            {
                msg.GroupTag = SnowFlake.NewId;
                msg.Path     = SnowFlake.NewId;
            }

            if (Regex.Match(cmd.NickName + cmd.Content, CommonHelper.ModRegex).Length <= 0)
            {
                msg.Status = Status.Published;
            }

            msg.PostDate = DateTime.Now;
            var user = HttpContext.Session.Get <UserInfoDto>(SessionKey.UserInfo);

            if (user != null)
            {
                msg.NickName = user.NickName;
                msg.Email    = user.Email;
                if (user.IsAdmin)
                {
                    msg.Status   = Status.Published;
                    msg.IsMaster = true;
                }
            }

            msg.Content = await cmd.Content.HtmlSantinizerStandard().ClearImgAttributes();

            msg.Browser  = cmd.Browser ?? Request.Headers[HeaderNames.UserAgent];
            msg.IP       = ClientIP;
            msg.Location = Request.Location();
            msg          = LeaveMessageService.AddEntitySaved(msg);
            if (msg == null)
            {
                return(ResultData(null, false, "留言发表失败!"));
            }

            Response.Cookies.Append("NickName", msg.NickName, new CookieOptions()
            {
                Expires  = DateTimeOffset.Now.AddYears(1),
                SameSite = SameSiteMode.Lax
            });
            WriteEmailKeyCookie(cmd.Email);
            MsgFeq.AddOrUpdate("Comments:" + ClientIP, 1, i => i + 1, 5);
            MsgFeq.Expire("Comments:" + ClientIP, TimeSpan.FromMinutes(1));
            var email   = CommonHelper.SystemSettings["ReceiveEmail"];
            var content = new Template(await new FileInfo(HostEnvironment.WebRootPath + "/template/notify.html").ShareReadWrite().ReadAllTextAsync(Encoding.UTF8)).Set("title", "网站留言板").Set("time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")).Set("nickname", msg.NickName).Set("content", msg.Content);

            if (msg.Status == Status.Published)
            {
                if (!msg.IsMaster)
                {
                    await MessageService.AddEntitySavedAsync(new InternalMessage()
                    {
                        Title   = $"来自【{msg.NickName}】的新留言",
                        Content = msg.Content,
                        Link    = Url.Action("Index", "Msg", new { cid = msg.Id })
                    });
                }
                if (msg.ParentId == null)
                {
                    //新评论,只通知博主
                    BackgroundJob.Enqueue(() => CommonHelper.SendMail(Request.Host + "|博客新留言:", content.Set("link", Url.Action("Index", "Msg", new { cid = msg.Id }, Request.Scheme)).Render(false), email, ClientIP));
                }
                else
                {
                    //通知博主和上层所有关联的评论访客
                    using var emails = LeaveMessageService.GetQuery(e => e.GroupTag == msg.GroupTag).Select(c => c.Email).Distinct().AsEnumerable().Append(email).Except(new[] { msg.Email }).ToPooledSet();
                    string link = Url.Action("Index", "Msg", new { cid = msg.Id }, Request.Scheme);
                    foreach (var s in emails)
                    {
                        BackgroundJob.Enqueue(() => CommonHelper.SendMail($"{Request.Host}{CommonHelper.SystemSettings["Title"]} 留言回复:", content.Set("link", link).Render(false), s, ClientIP));
                    }
                }
                return(ResultData(null, true, "留言发表成功,服务器正在后台处理中,这会有一定的延迟,稍后将会显示到列表中!"));
            }

            BackgroundJob.Enqueue(() => CommonHelper.SendMail(Request.Host + "|博客新留言(待审核):", content.Set("link", Url.Action("Index", "Msg", new
            {
                cid = msg.Id
            }, Request.Scheme)).Render(false) + "<p style='color:red;'>(待审核)</p>", email, ClientIP));
            return(ResultData(null, true, "留言发表成功,待站长审核通过以后将显示到列表中!"));
        }