forked from BlogEngine/BlogEngine.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManager.cs
More file actions
115 lines (98 loc) · 4.1 KB
/
Manager.cs
File metadata and controls
115 lines (98 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
namespace BlogEngine.Core.Ping
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
/// <summary>
/// Manages to send out trackbacks and then pingbacks if trackbacks aren't supported by the linked site.
/// </summary>
public static class Manager
{
#region Constants and Fields
/// <summary>
/// Regex used to find the trackback link on a remote web page.
/// </summary>
private static readonly Regex TrackbackLinkRegex = new Regex(
"trackback:ping=\"([^\"]+)\"", RegexOptions.IgnoreCase | RegexOptions.Compiled);
// private static readonly Regex urlsRegex = new Regex(@"\<a\s+href=""(http://.*?)"".*\>.+\<\/a\>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
// private static readonly Regex urlsRegex = new Regex(@"<a[^(href)]?href=""([^""]+)""[^>]?>([^<]+)</a>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
/// <summary>
/// Regex used to find all hyperlinks.
/// </summary>
private static readonly Regex UrlsRegex = new Regex(
@"<a.*?href=[""'](?<url>.*?)[""'].*?>(?<name>.*?)</a>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
#endregion
#region Public Methods
/// <summary>
/// Sends the trackback or pingback message.
/// <remarks>
/// It will try to send a trackback message first, and if the refered web page
/// doesn't support trackbacks, a pingback is sent.
/// </remarks>
/// </summary>
/// <param name="item">
/// The publishable item.
/// </param>
/// <param name="itemUrl">
/// The item Url.
/// </param>
public static void Send(IPublishable item, Uri itemUrl)
{
foreach (var url in GetUrlsFromContent(item.Content))
{
var trackbackSent = false;
if (BlogSettings.Instance.EnableTrackBackSend)
{
// ignoreRemoteDownloadSettings should be set to true
// for backwards compatibilty with Utils.DownloadWebPage.
var remoteFile = new RemoteFile(url, true);
var pageContent = remoteFile.GetFileAsString(); // ReadFromWeb(url);
var trackbackUrl = GetTrackBackUrlFromPage(pageContent);
if (trackbackUrl != null)
{
var message = new TrackbackMessage(item, trackbackUrl, itemUrl);
trackbackSent = Trackback.Send(message);
}
}
if (!trackbackSent && BlogSettings.Instance.EnablePingBackSend)
{
Pingback.Send(itemUrl, url);
}
}
}
#endregion
#region Methods
/// <summary>
/// Examines the web page source code to retrieve the trackback link from the RDF.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>The trackback Uri</returns>
private static Uri GetTrackBackUrlFromPage(string input)
{
var url = TrackbackLinkRegex.Match(input).Groups[1].ToString().Trim();
Uri uri;
return Uri.TryCreate(url, UriKind.Absolute, out uri) ? uri : null;
}
/// <summary>
/// Gets all the URLs from the specified string.
/// </summary>
/// <param name="content">The content.</param>
/// <returns>A list of Uri</returns>
private static IEnumerable<Uri> GetUrlsFromContent(string content)
{
var urlsList = new List<Uri>();
foreach (var url in
UrlsRegex.Matches(content).Cast<Match>().Select(myMatch => myMatch.Groups["url"].ToString().Trim()))
{
Uri uri;
if (Uri.TryCreate(url, UriKind.Absolute, out uri))
{
urlsList.Add(uri);
}
}
return urlsList;
}
#endregion
}
}