You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.2 KiB

  1. using System;
  2. using System.Globalization;
  3. using System.Text.RegularExpressions;
  4. using System.Web.Security;
  5. public class Comment
  6. {
  7. private static readonly Regex _linkRegex = new Regex("((http://|https://|www\\.)([A-Z0-9.\\-]{1,})\\.[0-9A-Z?;~&%\\(\\)#,=\\-_\\./\\+]{2,}[0-9A-Z?~&%#=\\-_/\\+])", RegexOptions.Compiled | RegexOptions.IgnoreCase);
  8. private const string Link = "<a href=\"{0}{1}\" rel=\"nofollow\">{2}</a>";
  9. public Comment()
  10. {
  11. ID = Guid.NewGuid().ToString();
  12. PubDate = DateTime.UtcNow;
  13. }
  14. public string ID { get; set; }
  15. public string Author { get; set; }
  16. public string Email { get; set; }
  17. public string Website { get; set; }
  18. public string Content { get; set; }
  19. public DateTime PubDate { get; set; }
  20. public string Ip { get; set; }
  21. public string UserAgent { get; set; }
  22. public bool IsAdmin { get; set; }
  23. public bool IsApproved { get; set; }
  24. public string GravatarUrl(int size)
  25. {
  26. var hash = FormsAuthentication.HashPasswordForStoringInConfigFile(Email.ToLowerInvariant(), "MD5").ToLower();
  27. return string.Format("http://gravatar.com/avatar/{0}?s={1}&d=mm", hash, size);
  28. }
  29. public string ContentWithLinks()
  30. {
  31. return _linkRegex.Replace(Content, new MatchEvaluator(Evaluator));
  32. }
  33. private static string Evaluator(Match match)
  34. {
  35. var info = CultureInfo.InvariantCulture;
  36. return string.Format(info, Link, !match.Value.Contains("://") ? "http://" : string.Empty, match.Value, ShortenUrl(match.Value, 50));
  37. }
  38. private static string ShortenUrl(string url, int max)
  39. {
  40. if (url.Length <= max)
  41. {
  42. return url;
  43. }
  44. // Remove the protocal
  45. var startIndex = url.IndexOf("://");
  46. if (startIndex > -1)
  47. {
  48. url = url.Substring(startIndex + 3);
  49. }
  50. if (url.Length <= max)
  51. {
  52. return url;
  53. }
  54. // Compress folder structure
  55. var firstIndex = url.IndexOf("/") + 1;
  56. var lastIndex = url.LastIndexOf("/");
  57. if (firstIndex < lastIndex)
  58. {
  59. url = url.Remove(firstIndex, lastIndex - firstIndex);
  60. url = url.Insert(firstIndex, "...");
  61. }
  62. if (url.Length <= max)
  63. {
  64. return url;
  65. }
  66. // Remove URL parameters
  67. var queryIndex = url.IndexOf("?");
  68. if (queryIndex > -1)
  69. {
  70. url = url.Substring(0, queryIndex);
  71. }
  72. if (url.Length <= max)
  73. {
  74. return url;
  75. }
  76. // Remove URL fragment
  77. var fragmentIndex = url.IndexOf("#");
  78. if (fragmentIndex > -1)
  79. {
  80. url = url.Substring(0, fragmentIndex);
  81. }
  82. if (url.Length <= max)
  83. {
  84. return url;
  85. }
  86. // Compress page
  87. firstIndex = url.LastIndexOf("/") + 1;
  88. lastIndex = url.LastIndexOf(".");
  89. if (lastIndex - firstIndex > 10)
  90. {
  91. var page = url.Substring(firstIndex, lastIndex - firstIndex);
  92. var length = url.Length - max + 3;
  93. if (page.Length > length)
  94. {
  95. url = url.Replace(page, string.Format("...{0}", page.Substring(length)));
  96. }
  97. }
  98. return url;
  99. }
  100. }