Exemplo n.º 1
0
        public void Process(CreateCommentArgs args)
        {
            Assert.IsNotNull(args.CommentItem, "Comment Item cannot be null");

            if (!string.IsNullOrEmpty(Settings.AkismetAPIKey) && !string.IsNullOrEmpty(Settings.CommentWorkflowCommandSpam))
            {
                var workflow = args.Database.WorkflowProvider.GetWorkflow(args.CommentItem);

                if (workflow != null)
                {
                    var api = new Akismet(Settings.AkismetAPIKey, ManagerFactory.BlogManagerInstance.GetCurrentBlog().SafeGet(x => x.Url), "WeBlog/2.1");
                    var isSpam = api.CommentCheck(args.CommentItem);

                    if (isSpam)
                    {
                        //Need to switch to shell website to execute workflow
                        using (new SiteContextSwitcher(SiteContextFactory.GetSiteContext(Sitecore.Constants.ShellSiteName)))
                        {
                            workflow.Execute(Settings.CommentWorkflowCommandSpam, args.CommentItem, "Akismet classified this comment as spam", false, new object[0]);
                        }

                        args.AbortPipeline();
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void Process(CreateCommentArgs args)
        {
            Assert.IsNotNull(args.Comment, "Comment DTO cannot be null");

            if (HttpContext.Current != null)
            {
                var cookie = HttpContext.Current.Request.Cookies[Constants.CookieName];
                if (cookie == null)
                    cookie = new HttpCookie(Constants.CookieName);

                var hashes = new List<string>();

                if (cookie.Value != null)
                    hashes.AddRange(cookie.Value.Split(new char[] { ';' }));

                hashes.Add(args.Comment.GetHash());

                if (hashes.Count > CommentCount)
                    hashes.RemoveRange(0, hashes.Count - CommentCount);

                cookie.Value = string.Join(";", hashes);
                cookie.Expires = DateTime.Now.AddMinutes(CookieExpirationAge);

                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
Exemplo n.º 3
0
        public void Process(CreateCommentArgs args)
        {
            Assert.IsNotNull(args.Database, "Database cannot be null");
            Assert.IsNotNull(args.Comment, "Comment cannot be null");
            Assert.IsNotNull(args.EntryID, "Entry ID cannot be null");

            var entryItem = args.Database.GetItem(args.EntryID, args.Language);
            if (entryItem != null)
            {
                var blogItem = ManagerFactory.BlogManagerInstance.GetCurrentBlog(entryItem);
                if (blogItem != null)
                {
                    var template = args.Database.GetTemplate(blogItem.BlogSettings.CommentTemplateID);
                    string itemName = ItemUtil.ProposeValidItemName("Comment by " + args.Comment.AuthorName + " at " + DateTime.Now.ToString("d"));

                    //need to emulate creation within shell site to ensure workflow is applied to comment
                    using (new SiteContextSwitcher(SiteContextFactory.GetSiteContext("shell")))
                    {
                        using (new SecurityDisabler())
                        {
                            var newItem = entryItem.Add(itemName, template);

                            var newComment = new CommentItem(newItem);
                            newComment.BeginEdit();
                            newComment.Name.Field.Value = args.Comment.AuthorName;
                            newComment.Email.Field.Value = args.Comment.AuthorEmail;
                            newComment.Comment.Field.Value = args.Comment.Text;

                            foreach (var entry in args.Comment.Fields)
                            {
                                newComment.InnerItem[entry.Key] = entry.Value;
                            }

                            newComment.EndEdit();

                            args.CommentItem = newComment;
                        }
                    }
                }
                else
                {
                    string message = "Failed to find blog for entry {0}\r\nIgnoring comment: name='{1}', email='{2}', commentText='{3}'";
                    Log.Error(string.Format(message, args.EntryID, args.Comment.AuthorName, args.Comment.AuthorEmail, args.Comment.Text), typeof(CreateCommentItem));
                }
            }
            else
            {
                string message = "Failed to find blog entry {0}\r\nIgnoring comment: name='{1}', email='{2}', commentText='{3}'";
                Log.Error(string.Format(message, args.EntryID, args.Comment.AuthorName, args.Comment.AuthorEmail, args.Comment.Text), typeof(CreateCommentItem));
            }
        }
Exemplo n.º 4
0
        public void Process(CreateCommentArgs args)
        {
            if (!string.IsNullOrEmpty(Settings.CommentWorkflowCommandCreated))
            {
                var workflow = args.Database.WorkflowProvider.GetWorkflow(args.CommentItem);

                if (workflow != null)
                {
                    //Need to switch to shell website to execute workflow
                    using (new SiteContextSwitcher(SiteContextFactory.GetSiteContext(Sitecore.Constants.ShellSiteName)))
                    {
                        workflow.Execute(Settings.CommentWorkflowCommandCreated, args.CommentItem, "WeBlog automated submit", false, new object[0]);
                    }
                    //workflow should take care of publishing the item following this, if it's configured to do so
                }
            }
        }
Exemplo n.º 5
0
        public void Process(CreateCommentArgs args)
        {
            Assert.IsNotNull(args.Database, "Database cannot be null");
            Assert.IsNotNull(args.Comment, "Comment cannot be null");
            Assert.IsNotNull(args.EntryID, "Entry ID cannot be null");

            var entryItem = args.Database.GetItem(args.EntryID, args.Language);
            if (entryItem != null)
            {
                var query = "fast:{0}//*[@email='{1}' and @name='{2}' and @comment='{3}']".FormatWith(
                    entryItem.Paths.FullPath, args.Comment.AuthorEmail, args.Comment.AuthorName, args.Comment.Text);
                var comments = args.Database.SelectItems(query);

                if(comments.Length > 0)
                    args.AbortPipeline();
            }
        }
Exemplo n.º 6
0
        public void Process(CreateCommentArgs args)
        {
            Assert.IsNotNull(args.Comment, "Comment DTO cannot be null");

            if (HttpContext.Current != null)
            {
                // Get cookie
                var cookie = HttpContext.Current.Request.Cookies[Constants.CookieName];
                if (cookie != null)
                {
                    // get the most recent comment hashes
                    var hashes = cookie.Value.Split(new char[] {';'});
                    var currentHash = args.Comment.GetHash();

                    if(hashes.Contains(currentHash))
                        args.AbortPipeline();
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Adds a comment to a blog
        /// </summary>
        /// <param name="entryId">The ID of the entry to add the comment to</param>
        /// <param name="comment">The comment to add to the entry</param>
        /// <param name="language">The language to create the comment in</param>
        /// <returns>The ID of the created comment item, or ID.Null if creation failed</returns>
        public ID AddCommentToEntry(ID entryId, Model.Comment comment, Language language = null)
        {
            var args = new CreateCommentArgs();
            args.EntryID = entryId;
            args.Comment = comment;
            args.Database = ContentHelper.GetContentDatabase();
            args.Language = language ?? Context.Language;

            #if PRE_65
            CorePipeline.Run("weblogCreateComment", args);
            #else
            CorePipeline.Run("weblogCreateComment", args, true);
            #endif

            if (args.CommentItem != null)
                return args.CommentItem.ID;
            else
                return ID.Null;
        }
Exemplo n.º 8
0
    public void Process(CreateCommentArgs args)
    {
      Assert.IsNotNull(args.Database, "Database cannot be null");
      Assert.IsNotNull(args.Comment, "Comment cannot be null");
      Assert.IsNotNull(args.EntryID, "Entry ID cannot be null");

      var entryItem = args.Database.GetItem(args.EntryID, args.Language);
      if (entryItem != null)
      {
        // End at end of today to make sure we cover comments from today properly
        var dateEnd = System.DateTime.UtcNow.Date.AddDays(1);
        var dateStart = dateEnd - TimeSpan;

        var blog = ManagerFactory.BlogManagerInstance.GetCurrentBlog(entryItem);
        if (blog != null)
        {
          var commentTemplate = blog.BlogSettings.CommentTemplateID;

          var query = "{0}//*[@@templateid='{1}' and @__created > '{2}' and @__created < '{3}']".FormatWith(
            ContentHelper.EscapePath(entryItem.Paths.FullPath), commentTemplate, DateUtil.ToIsoDate(dateStart), DateUtil.ToIsoDate(dateEnd));

          var comments = args.Database.SelectItems(query);

          var match = false;

          foreach (var item in comments)
          {
            var commentItem = (CommentItem) item;
            if (string.Compare(commentItem.AuthorName, args.Comment.AuthorName, StringComparison.OrdinalIgnoreCase) == 0 &&
                string.Compare(commentItem.Email.Raw, args.Comment.AuthorEmail, StringComparison.OrdinalIgnoreCase) == 0 &&
                string.Compare(commentItem.Comment.Raw, args.Comment.Text, StringComparison.OrdinalIgnoreCase) == 0)
            {
              match = true;
              Log.Warn("[WeBlog] Duplicate comment submission. Existing item: {0}".FormatWith(commentItem.ID), this);
            }
          }

          if (match)
            args.AbortPipeline();
        }
      }
    }
Exemplo n.º 9
0
		public void Process(CreateCommentArgs args)
		{
			Assert.IsNotNull(args.Database, "Database cannot be null");
			Assert.IsNotNull(args.Comment, "Comment cannot be null");
			Assert.IsNotNull(args.EntryID, "Entry ID cannot be null");

			var entryItem = args.Database.GetItem(args.EntryID, args.Language);
			if (entryItem != null)
			{
				var blogItem = ManagerFactory.BlogManagerInstance.GetCurrentBlog(entryItem);
				if (blogItem != null)
				{
					var template = args.Database.GetTemplate(blogItem.BlogSettings.CommentTemplateID);
					var itemName = ItemUtil.ProposeValidItemName(string.Format("Comment at {0} by {1}", DateTime.Now.ToString("yyyyMMdd HHmmss"), args.Comment.AuthorName));

					// verify the comment item name is unique for this entry
					var query = "{0}//{1}".FormatWith(ContentHelper.EscapePath(entryItem.Paths.FullPath), itemName);
#if FEATURE_FAST_QUERY
					query = "fast:" + query;
#endif

					var num = 1;
					var nondupItemName = itemName;
					while (entryItem.Database.SelectSingleItem(query) != null)
					{
						nondupItemName = itemName + " " + num;
						num++;
						query = "fast:{0}//{1}".FormatWith(entryItem.Paths.FullPath, nondupItemName);
					}

					//need to emulate creation within shell site to ensure workflow is applied to comment
					using (new SiteContextSwitcher(SiteContextFactory.GetSiteContext(Sitecore.Constants.ShellSiteName)))
					{
						using (new SecurityDisabler())
						{
							var newItem = entryItem.Add(nondupItemName, template);

							var newComment = new CommentItem(newItem);
							newComment.BeginEdit();
							newComment.Name.Field.Value = args.Comment.AuthorName;
							newComment.Email.Field.Value = args.Comment.AuthorEmail;
							newComment.Comment.Field.Value = args.Comment.Text;

							foreach (var entry in args.Comment.Fields)
							{
								newComment.InnerItem[entry.Key] = entry.Value;
							}

							newComment.EndEdit();

							args.CommentItem = newComment;
						}
					}
				}
				else
				{
					var message = "Failed to find blog for entry {0}\r\nIgnoring comment: name='{1}', email='{2}', commentText='{3}'";
					Log.Error(string.Format(message, args.EntryID, args.Comment.AuthorName, args.Comment.AuthorEmail, args.Comment.Text), typeof(CreateCommentItem));
				}
			}
			else
			{
				var message = "Failed to find blog entry {0}\r\nIgnoring comment: name='{1}', email='{2}', commentText='{3}'";
				Log.Error(string.Format(message, args.EntryID, args.Comment.AuthorName, args.Comment.AuthorEmail, args.Comment.Text), typeof(CreateCommentItem));
			}
		}
Exemplo n.º 10
0
        public void Process(CreateCommentArgs args)
        {
            Assert.ArgumentNotNull(args, "args cannot be null");
            Assert.IsNotNull(args.Database, "Database cannot be null");
            Assert.IsNotNull(args.Comment, "Comment cannot be null");
            Assert.IsNotNull(args.EntryID, "Entry ID cannot be null");
            Assert.IsNotNull(args.Language, "Language cannot be null");

            var entryItem = args.Database.GetItem(args.EntryID, args.Language);
            if (entryItem != null)
            {
                var blogItem = BlogManager.GetCurrentBlog(entryItem);
                if (blogItem != null)
                {
                    var template = args.Database.GetTemplate(blogItem.BlogSettings.CommentTemplateID);
                    var itemName =
                        ItemUtil.ProposeValidItemName(string.Format("Comment at {0} by {1}",
                            GetDateTime().ToString("yyyyMMdd HHmmss"), args.Comment.AuthorName));
                    if (itemName.Length > 100)
                    {
                        itemName = itemName.Substring(0, 100);
                    }

                    // verify the comment item name is unique for this entry
                    var query = BuildFastQuery(entryItem, itemName);

                    var num = 1;
                    var nondupItemName = itemName;
                    while (entryItem.Database.SelectSingleItem(query) != null)
                    {
                        nondupItemName = itemName + " " + num;
                        num++;
                        query = BuildFastQuery(entryItem, nondupItemName);
                    }

                    // need to create the comment within the shell site to ensure workflow is applied to comment
                    var shellSite = SiteContextFactory.GetSiteContext(Sitecore.Constants.ShellSiteName);
                    SiteContextSwitcher siteSwitcher = null;

                    try
                    {
                        if (shellSite != null)
                        {
                            siteSwitcher = new SiteContextSwitcher(shellSite);
                        }

                        using (new SecurityDisabler())
                        {
                            var newItem = entryItem.Add(nondupItemName, template);

                            var newComment = new CommentItem(newItem);
                            newComment.BeginEdit();
                            newComment.Name.Field.Value = args.Comment.AuthorName;
                            newComment.Email.Field.Value = args.Comment.AuthorEmail;
                            newComment.Comment.Field.Value = args.Comment.Text;

                            foreach (var entry in args.Comment.Fields)
                            {
                                newComment.InnerItem[entry.Key] = entry.Value;
                            }

                            newComment.EndEdit();

                            args.CommentItem = newComment;
                        }
                    }
                    finally
                    {
                        siteSwitcher?.Dispose();
                    }
                }
                else
                {
                    var message = "Failed to find blog for entry {0}\r\nIgnoring comment: name='{1}', email='{2}', commentText='{3}'";
                    Logger.Error(string.Format(message, args.EntryID, args.Comment.AuthorName, args.Comment.AuthorEmail, args.Comment.Text), typeof(CreateCommentItem));
                }
            }
            else
            {
                var message = "Failed to find blog entry {0}\r\nIgnoring comment: name='{1}', email='{2}', commentText='{3}'";
                Logger.Error(string.Format(message, args.EntryID, args.Comment.AuthorName, args.Comment.AuthorEmail, args.Comment.Text), typeof(CreateCommentItem));
            }
        }