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."); } } } }
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); }