Пример #1
0
        void ga_AfterInsert(DataBuddyBase dataObject, EventArgs e)
        {
            Post post = dataObject as Post;

            if (post != null)
            {
                // Check that post
                //  1. Is Published
                //  2. The current verison is set to be published
                //  3. Is not a future dated post
                if (post.IsPublished && post.PostStatus == PostStatus.Publish && post.Published.CompareTo(DateTime.UtcNow.AddMinutes(1.0)) <= 0)
                {
                    // Blog Pings
                    if (EnablePings)
                    {
                        XmlRpcPings.SendPings(post, PingUrls);
                    }

                    // Check for links to send Trackbacks & Pingbacks
                    if (EnableTrackbacks)
                    {
                        LinkParser.CheckPost(post);
                    }
                }
            }
        }
Пример #2
0
        void ga_AfterUpdate(DataBuddyBase dataObject, EventArgs e)
        {
            Post post = dataObject as Post;

            if (post != null)
            {
                // Check that post
                //  1. Is Published
                //  2. The current verison is set to be published
                //  3. Was not published within the last 10 seconds (some plugins update the post immediately after it is created,
                //     which can cause double trackbacks/pings)
                if (post.IsPublished && post.PostStatus == PostStatus.Publish && post.Published.CompareTo(DateTime.UtcNow.AddSeconds(-10.0)) <= 0)
                {
                    // Blog Pings
                    if (EnablePings)
                    {
                        XmlRpcPings.SendPings(post, PingUrls);
                    }

                    // Check for links to send Trackbacks & Pingbacks
                    if (EnableTrackbacks)
                    {
                        LinkParser.CheckPost(post);
                    }
                }
            }
        }
Пример #3
0
        public void ProcessRequest(HttpContext context)
        {
            Macros macros = new Macros();

            context.Response.ContentType = "text/xml";

            int postId = 0;

            try { postId = int.Parse(context.Request.QueryString["id"]); }
            catch { }

            if (postId <= 0)
            {
                TrackbackResponse(context, "PostId is invalid or missing");
            }

            if (context.Request.HttpMethod == "POST")
            {
                string title     = SafeParam(context, "title");
                string excerpt   = SafeParam(context, "excerpt");
                string url       = SafeParam(context, "url");
                string blog_name = SafeParam(context, "blog_name");

                try
                {
                    // Check if params are valid
                    if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(title) || string.IsNullOrEmpty(blog_name) || string.IsNullOrEmpty(excerpt))
                    {
                        TrackbackResponse(context, "One or more parameters are invalid or missing");
                    }

                    Post trackedEntry = Post.GetCachedPost(postId);
                    if (trackedEntry == null)
                    {
                        TrackbackResponse(context, "The link does not exist");
                        return;
                    }

                    if (!trackedEntry.EnableComments || !trackedEntry.EnableNewComments)
                    {
                        TrackbackResponse(context, "Trackbacks are not enabled");
                        return;
                    }

                    if (!IsNewTrackBack(trackedEntry.Id, url))
                    {
                        TrackbackResponse(context, "Trackbacks already exists");
                        return;
                    }

                    string pageTitle = null;
                    if (!LinkParser.SourceContainsTarget(url, macros.FullUrl(trackedEntry.Url), out pageTitle))
                    {
                        TrackbackResponse(context, "Sorry couldn't find a relevant link in " + url);
                    }

                    if (string.IsNullOrEmpty(pageTitle))
                    {
                        TrackbackResponse(context, "Could not find a readable HTML title in the remote page at " + url);
                        return;
                    }

                    if (!string.IsNullOrEmpty(excerpt))
                    {
                        excerpt = Util.RemoveHtml(excerpt, 250);
                    }

                    // Create the Trackback item
                    Comment comment = new Comment();
                    comment.IsTrackback = true;
                    comment.PostId      = trackedEntry.Id;
                    comment.Name        = title;
                    comment.WebSite     = url;
                    comment.Body        = excerpt;
                    comment.IPAddress   = context.Request.UserHostAddress;
                    comment.Published   = DateTime.Now.AddHours(SiteSettings.Get().TimeZoneOffSet);
                    comment.Save();

                    // Log success message to EventLog
                    string message = String.Format("Trackback request received from {0} and saved to post {1}.", url, trackedEntry.Title);
                    Log.Info("Trackback Received", message);

                    context.Response.Write(successResponseXML);
                    context.Response.End();
                }
                catch (System.Threading.ThreadAbortException) { }
                catch (System.Exception ex)
                {
                    if (ex.Message != null)
                    {
                        TrackbackResponse(context, string.Format("Error occurred while processing Trackback: {0}", ex.Message));
                    }
                    else
                    {
                        TrackbackResponse(context, "Unknown error occurred while processing Trackback.");
                    }
                }
            }
        }
Пример #4
0
 public static void CheckPost(Post post)
 {
     LinkParser lp = new LinkParser(SiteSettings.Get().Title, post.Title, new Macros().FullUrl(post.Url), post.Body, post.Excerpt("", "", "", 300), SiteSettings.BaseUrl);
     ManagedThreadPool.QueueUserWorkItem(new WaitCallback(lp.CheckPost));
 }
Пример #5
0
        private void CreatePingBack(string sourceURI, string targetURI)
        {
            // Check Parameters
            if (string.IsNullOrEmpty(sourceURI))
            {
                throw new XmlRpcFaultException(errorCode_SourceURIDoesNotExist, "No source URI parameter found, please try harder!");
            }
            if (string.IsNullOrEmpty(targetURI))
            {
                throw new XmlRpcFaultException(errorCode_TargetURIDoesNotExist, "The target URI does not exist!");
            }

            // Retrieve referenced post
            Post trackedEntry = null;

            try
            {
                trackedEntry = GetPostFromUrl(targetURI);
            }
            catch
            {
                throw new XmlRpcFaultException(errorCode_TargetURIInvalid, "The target URI is invalid.");
            }
            if (trackedEntry == null)
            {
                throw new XmlRpcFaultException(errorCode_TargetURIInvalid, "The target URI is invalid.");
            }

            // Check if trackbacks/pingbacks are enabled
            if (!trackedEntry.EnableComments || !trackedEntry.EnableNewComments)
            {
                throw new XmlRpcFaultException(errorCode_AccessDenied, "Pingbacks are not enabled.");
            }

            // Check if this is a duplicate pingback (or trackback)
            if (!IsNewTrackBack(trackedEntry.Id, sourceURI))
            {
                throw new XmlRpcFaultException(errorCode_DuplicatePingBack, "A pingback for this source URI already exists.");
            }

            // Retrieve the source document and check if it actually contains a link to the target
            string pageTitle = null;

            if (!LinkParser.SourceContainsTarget(sourceURI, new Macros().FullUrl(trackedEntry.Url), out pageTitle))
            {
                throw new XmlRpcFaultException(errorCode_SourceDoesNotContainTarget, "Sorry couldn't find a relevant link in " + sourceURI);
            }

            if (string.IsNullOrEmpty(pageTitle))
            {
                throw new XmlRpcFaultException(errorCode_SourceDoesNotContainTarget, "Could not find a readable HTML title in the remote page at " + sourceURI);
            }

            // Create the Trackback item
            Comment comment = new Comment();

            comment.IsTrackback = true;
            comment.PostId      = trackedEntry.Id;
            comment.Name        = pageTitle;
            comment.WebSite     = sourceURI;
            comment.Body        = "Pingback from " + pageTitle;
            comment.IPAddress   = Context.Request.UserHostAddress;
            comment.Published   = DateTime.Now.AddHours(SiteSettings.Get().TimeZoneOffSet);
            comment.Save();

            // Log success message to EventLog
            string message = String.Format("Pingback request received from {0} and saved to post {1}.", sourceURI, trackedEntry.Title);

            Log.Info("Pingback Received", message);
        }
Пример #6
0
        public static void CheckPost(Post post)
        {
            LinkParser lp = new LinkParser(SiteSettings.Get().Title, post.Title, new Macros().FullUrl(post.Url), post.Body, post.Excerpt("", "", "", 300), SiteSettings.BaseUrl);

            ManagedThreadPool.QueueUserWorkItem(new WaitCallback(lp.CheckPost));
        }
Пример #7
0
        /// <summary>
        /// Retrieves a remote html page and determines if it contains a link to a given target url. Also gets the page title.
        /// </summary>
        internal static bool SourceContainsTarget(string sourceURI, string targetURI, out string pageTitle)
        {
            pageTitle = string.Empty;
            Uri    baseUri = new Uri(sourceURI);
            string baseUrl = string.Format("{0}://{1}{2}", baseUri.Scheme, baseUri.Host, baseUri.Port == 80 ? string.Empty : ":" + baseUri.Port.ToString());

            string         page    = null;
            HttpWebRequest request = GRequest.CreateSafeRequest(sourceURI, targetURI);

            request.MaximumAutomaticRedirections = 3;
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                if (response != null)
                {
                    if (GRequest.IsContentTypeValid(response.ContentType, GRequest.ContentTypeCategory.HTML) && response.ContentLength < 300000)
                    {
                        page = GRequest.GetPageText(response);
                    }

                    response.Close();
                }
            }

            if (string.IsNullOrEmpty(page))
            {
                return(false);
            }

            if (page.IndexOf(targetURI) < 0)
            {
                // The direct link to the Graffiti post was not found in the body of the source post
                // BUT, the source may have linked to a url that redirected to the Graffiti post (i.e. Feedburner url)
                List <string> links           = LinkParser.GetLinks(page, baseUrl);
                bool          targetLinkFound = false;

                foreach (string externalUrl in links)
                {
                    if (string.IsNullOrEmpty(externalUrl))
                    {
                        continue;
                    }

                    try
                    {
                        string         responseUrl = null;
                        HttpWebRequest request2    = GRequest.CreateSafeRequest(externalUrl, targetURI);
                        request2.MaximumAutomaticRedirections = 3;
                        using (HttpWebResponse response2 = request2.GetResponse() as HttpWebResponse)
                        {
                            if (response2 != null)
                            {
                                if (GRequest.IsContentTypeValid(response2.ContentType, GRequest.ContentTypeCategory.HTML) && response2.ContentLength < 300000)
                                {
                                    responseUrl = response2.ResponseUri.ToString();
                                }

                                response2.Close();
                            }
                        }

                        if (responseUrl != null && string.Compare(targetURI, responseUrl, StringComparison.InvariantCultureIgnoreCase) == 0)
                        {
                            targetLinkFound = true;
                            break;
                        }
                    }
                    catch { }
                }

                if (!targetLinkFound)
                {
                    return(false);
                }
            }

            // Look for the HTML Title of the remote page
            string pat = @"<head.*?>.*<title.*?>(.*)</title.*?>.*</head.*?>";
            Regex  reg = new Regex(pat, RegexOptions.IgnoreCase | RegexOptions.Singleline);
            Match  m   = reg.Match(page);

            if (m.Success)
            {
                pageTitle = HttpUtility.HtmlDecode(Graffiti.Core.Util.RemoveHtml(m.Result("$1").Replace("\r", "").Replace("\n", "").Replace("\t", "").Trim(), 250));
            }

            return(true);
        }