📄 trackbackhandler.cs
字号:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
// Copyright (c) Telligent Systems Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;
namespace CommunityServer.Blogs.Components
{
/// <summary>
/// Summary description for TrackBackHandler.
/// </summary>
public class TrackBackHandler : IHttpHandler
{
public TrackBackHandler()
{
}
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType="text/xml" ;
User user = CSContext.Current.User;
int postId = 0 ;
try
{
postId = int.Parse(context.Request.QueryString["postid"]) ;
}
catch
{
trackbackResponse (context, 1, "EntryID 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") ;
// is the url valid ?
if ( url == "" )
{
trackbackResponse (context, 1, "no url parameter found, please try harder!") ;
}
string pageTitle = null;
WeblogPost trackedEntry = WeblogPosts.GetPost(postId,false,true,false);
if(trackedEntry == null)
trackbackResponse (context, 2, "The link does not exist" ) ;
Weblog wl = trackedEntry.Section as Weblog;
Permissions.AccessCheck(wl,Permission.View, user );
if(!wl.EnableTrackbacks)
trackbackResponse (context, 2, "Trackbacks are not enabled" ) ;
if (trackedEntry != null && !SourceContainsTarget(url,Globals.FullPath(BlogUrls.Instance().Post(trackedEntry,wl)), out pageTitle))
{
trackbackResponse (context, 2, "Sorry couldn't find a relevant link in " + url ) ;
}
WeblogPost entry = new WeblogPost();
entry.BlogPostType = BlogPostType.Trackback;
entry.SectionID = wl.SectionID;
entry.ParentID = postId;
entry.Subject = title;
entry.TitleUrl = url;
entry.TrackBackName = blog_name;
entry.Body = excerpt;
entry.IsApproved = true;
entry.PostDate = DateTime.Now;
entry.BloggerTime = DateTime.Now;
WeblogPosts.Add(entry,user);
}
else
{
WeblogPost entry = WeblogPosts.GetPost(postId,false,true,false);
Weblog wl = entry.Section as Weblog;
Permissions.AccessCheck(wl,Permission.View, user);
XmlTextWriter w = new XmlTextWriter(context.Response.Output) ;
w.Formatting = Formatting.Indented;
w.WriteStartDocument() ;
w.WriteStartElement("response") ;
w.WriteElementString("error", "0") ;
w.WriteStartElement("rss") ;
w.WriteAttributeString("version", "0.91") ;
w.WriteStartElement("channel") ;
w.WriteElementString("title", entry.Subject ) ;
w.WriteElementString("link", BlogUrls.Instance().TrackbackPingUrl(postId));
w.WriteElementString("description", "" ) ;
w.WriteElementString("language", "en-us" ) ;
w.WriteEndElement() ; // channel
w.WriteEndElement() ; // rss
w.WriteEndElement() ; // response
w.WriteEndDocument() ;
}
}
public bool IsReusable
{
get { return true; }
}
private void trackbackResponse(HttpContext context, int errNum, string errText)
{
XmlDocument d = new XmlDocument() ;
XmlElement root = d.CreateElement("response") ;
d.AppendChild(root) ;
XmlElement er = d.CreateElement("error") ;
root.AppendChild(er) ;
er.AppendChild(d.CreateTextNode(errNum.ToString())) ;
if ( errText != "" )
{
XmlElement msg = d.CreateElement("message") ;
root.AppendChild(msg) ;
msg.AppendChild(d.CreateTextNode(errText)) ;
}
d.Save ( context.Response.Output ) ;
context.Response.End() ;
}
private string safeParam(HttpContext context,string pName)
{
if ( context.Request.Form[pName] != null )
return Globals.HtmlEncode(context.Request.Form[pName]);
return string.Empty;
}
public static bool SourceContainsTarget(string sURI, string tURI, out string pageTitle)
{
pageTitle = string.Empty ;
string page = CSRequest.GetPageText(sURI,tURI);
if (page == null || page.IndexOf(tURI) < 0 )
return false ;
string pat = @"<head.*?>.*<title.*?>(.*)</title.*?>.*</head.*?>" ;
Regex reg = new Regex(pat, RegexOptions.IgnoreCase | RegexOptions.Singleline ) ;
Match m = reg.Match(page) ;
if ( m.Success )
{
pageTitle = m.Result("$1") ;
return true ;
}
return false ;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -