PageRenderTime 34ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Ping/Manager.cs

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