Beispiel #1
0
        public static List<Comment> LoadById(int targetId, CommentTarget target)
        {
            List<Comment> comments = new List<Comment>();

            IList<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("@Target_ID", targetId));
            parameters.Add(new SqlParameter("@Target", target.ToString()));

            DataTable commentsTable = new DataTable();
            DataAccessor.Instance.ExecSPDataTable("SP_LoadComments", parameters, commentsTable);
            foreach (DataRow row in commentsTable.Rows)
            {
                Comment c = new Comment();
                c.PosterName = DBUtil.DBToString(row["Poster_Name"]);
                c.PosterIP = DBUtil.DBToString(row["Poster_IP"]);
                c.Email = DBUtil.DBToString(row["Email"]);
                c.Url = DBUtil.DBToString(row["URL"]);
                c.Content = DBUtil.DBToString(row["Content"]);
                c.PostDate = DBUtil.DBToDateTime(row["Post_Date"]);
                c.Locale = DBUtil.DBToString(row["Locale"]);

                comments.Add(c);
            }

            return comments;
        }
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Clear();
            context.Response.Expires = 0;
            context.Response.Cache.SetExpires(DateTime.Now.Subtract(TimeSpan.FromMinutes(1d)));
            context.Response.CacheControl = "No-Cache";
            //string debugCaptcha = String.Empty;
            try
            {
                if (context.Request.Form["Action"] == "PostComment")
                {
                    int targetId = 0;
                    String target = context.Request.Form["Target"];
                    String commentLink = HttpUtility.UrlDecode(context.Request.Form["CommentLink"]);
                    if (!Int32.TryParse(context.Request.Form["TargetID"], out targetId) || String.IsNullOrEmpty(target))
                        context.Response.End();

                    //string captcha = context.Request.Form["Captcha"];
                    //debugCaptcha = captcha;
                    //if (String.IsNullOrWhiteSpace(captcha) ||
                    //    HttpUtility.UrlDecode(captcha).ToLower() != context.Session["ValidateCode"].ToString().ToLower())
                    //{
                    //    WebUtil.WriteAjaxContent(context, "text/plain", "WrongCaptcha");
                    //    return;
                    //}
                    //else
                    //{
                    CultureInfo clientCulture = CultureInfo.GetCultureInfo(WaynePageBase.CurrentCulture);

                    // 1. Store the comment into DB
                    Comment c = new Comment()
                    {
                        TargetId = targetId,
                        PosterName = HttpUtility.UrlDecode(context.Request.Form["PosterName"]),
                        Email = HttpUtility.UrlDecode(context.Request.Form["Email"]),
                        Url = HttpUtility.UrlDecode(context.Request.Form["URL"]),
                        PosterIP = WebUtil.GetVisitorIp(),
                        Content = context.Server.HtmlEncode(HttpUtility.UrlDecode(context.Request.Form["Content"])),
                        Locale = WaynePageBase.CurrentCulture
                    };

                    switch ((CommentTarget)Enum.Parse(typeof(CommentTarget), target))
                    {
                        case CommentTarget.Blog:
                            c.Target = CommentTarget.Blog;
                            break;
                        case CommentTarget.Album:
                            c.Target = CommentTarget.Album;
                            break;
                        case CommentTarget.Video:
                            c.Target = CommentTarget.Video;
                            break;
                    }

                    c.Create();

                    // 2. Return postCommentcontext.Response to client
                    PostCommentResponse commentResponse = new PostCommentResponse()
                    {
                        PostTimeStr =
                            WaynePageBase.CurrentCulture == "zh-CN"
                                ? DateTime.Now.ToChineseTimeString(true)
                                : DateTime.Now.ToEnglishTimeString(true),
                        GravatarLink = WebUtil.ComputeGravatarLink(c.Email)
                    };
                    WebUtil.WriteAjaxContent(context, "application/json", (new JavaScriptSerializer()).Serialize(commentResponse));

                    // 3. Send email to the commente poster as well as myself by employing the ASP.NET Fire-And-Forgot pattern (http://www.eggheadcafe.com/articles/20050818.asp)
                    String emailMeAddress = WebConfigurationManager.AppSettings["EmailMeAddress"];
                    if (!String.IsNullOrEmpty(c.Email))
                    {
                        SendEmailAsync(c.Email, emailMeAddress, Resources.AjaxComment.ResourceManager.GetString("EmailTitle", clientCulture),
                                       Resources.AjaxComment.ResourceManager.GetString("EmailContent", clientCulture).Replace("#NickName#", c.PosterName));
                    }

                    // 4. Notify other guys who has had posted comment(s) on this specific blog, as long as it is not local testing
                    List<Comment> postedComments =
                    Comment.LoadById(targetId, (CommentTarget)Enum.Parse(typeof(CommentTarget), target));
                    postedComments.Reverse(); // I need get all the comments desc, since user might change their locale preferences, I follow the latest change:)
                    Dictionary<String, Comment> distinctComments = new Dictionary<String, Comment>();
                    foreach (Comment postedComment in postedComments) // Distincts posters' emails
                        if (!String.IsNullOrEmpty(postedComment.Email) && !distinctComments.ContainsKey(postedComment.Email) && !IsMyComment(postedComment.Email))
                            distinctComments.Add(postedComment.Email, postedComment);

                    foreach (String email in distinctComments.Keys)
                    {
                        // Sends email to distincts posters
                        CultureInfo commenterCulture =
                            CultureInfo.GetCultureInfo((distinctComments[email].Locale == "zh-CN") ? "zh-CN" :  "en-US");

                        string notidyEmailTitle = Resources.AjaxComment.ResourceManager.GetString("NewCommentNotifyEmailTitle", commenterCulture);
                        string notifyEmailContent = Resources.AjaxComment.ResourceManager.GetString("NewCommentNotifyEmailContent",
                            commenterCulture).Replace("#NickName#", distinctComments[email].PosterName).Replace("#CommentLink#", commentLink);

                        SendEmailAsync(email, emailMeAddress, notidyEmailTitle, notifyEmailContent);
                    }
                }
            }
            catch (Exception ex)
            {
                WebUtil.WriteAjaxContent(context, "text/plain", "ServerErr");

                //string debugInfo =
                //    String.Format(
                //        "captchar: {0}, HttpUtility.UrlDecode(captcha): {1}, context.Session[\"ValidateCode\"]: {2}",
                //        debugCaptcha,
                //        HttpUtility.UrlDecode(debugCaptcha) ?? "",
                //        context.Session["ValidateCode"] ?? "");
                WayneLogger.WriteLog(ex, "CommentErr.log");
            }
        }