PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Avatar.cs

#
C# | 173 lines | 83 code | 28 blank | 62 comment | 14 complexity | 014a273791705debbbf9e2ab6f9193a4 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <summary>
  3. // Avatar support.
  4. // </summary>
  5. // --------------------------------------------------------------------------------------------------------------------
  6. namespace BlogEngine.Core
  7. {
  8. using System;
  9. using System.Globalization;
  10. using System.Web;
  11. using System.Web.Security;
  12. /// <summary>
  13. /// Avatar support.
  14. /// </summary>
  15. public class Avatar
  16. {
  17. #region Constants and Fields
  18. /// <summary>
  19. /// The avatar image.
  20. /// </summary>
  21. private const string AvatarImage = "<img class=\"photo\" src=\"{0}\" alt=\"{1}\" />";
  22. /// <summary>
  23. /// Gets or sets the URL to the Avatar image.
  24. /// </summary>
  25. public Uri Url { get; set; }
  26. /// <summary>
  27. /// Gets or sets the image tag for the Avatar image.
  28. /// </summary>
  29. public string ImageTag { get; set; }
  30. /// <summary>
  31. /// Gets or sets a value indicating whether there is not a specific image available.
  32. /// </summary>
  33. public bool HasNoImage { get; set; }
  34. #endregion
  35. #region Public Methods
  36. /// <summary>
  37. /// Returns the avatar/gravatar that matches the specified email, website or avatar Url.
  38. /// </summary>
  39. /// <param name="size">
  40. /// The image size.
  41. /// </param>
  42. /// <param name="email">
  43. /// Email address.
  44. /// </param>
  45. /// <param name="website">
  46. /// The website URL.
  47. /// </param>
  48. /// <param name="avatarUrl">
  49. /// An optional avatar URL to use instead of the default.
  50. /// </param>
  51. /// <param name="description">
  52. /// Description used for the Alt/Title attributes.
  53. /// </param>
  54. /// <returns>
  55. /// The avatar/gravatar image.
  56. /// </returns>
  57. public static Avatar GetAvatar(int size, string email, Uri website, string avatarUrl, string description)
  58. {
  59. if (BlogSettings.Instance.Avatar == "none")
  60. {
  61. return new Avatar { HasNoImage = true };
  62. }
  63. string imageTag;
  64. Uri url;
  65. if (!string.IsNullOrEmpty(avatarUrl) && Uri.TryCreate(avatarUrl, UriKind.RelativeOrAbsolute, out url))
  66. {
  67. imageTag = string.Format(
  68. CultureInfo.InvariantCulture, AvatarImage, url, HttpUtility.HtmlEncode(description));
  69. return new Avatar { Url = url, ImageTag = imageTag };
  70. }
  71. if (string.IsNullOrEmpty(email) || !email.Contains("@"))
  72. {
  73. if (website != null && website.ToString().Length > 0 && website.ToString().Contains("http://"))
  74. {
  75. url =
  76. new Uri(
  77. string.Format(
  78. "http://cligs.websnapr.com/?url={0}&amp;size=t",
  79. HttpUtility.UrlEncode(website.ToString())));
  80. imageTag = string.Format(
  81. CultureInfo.InvariantCulture, "<img class=\"thumb\" src=\"{0}\" alt=\"{1}\" />", url, email);
  82. return new Avatar { Url = url, ImageTag = imageTag };
  83. }
  84. var themeAvatar = HttpContext.Current.Server.MapPath(string.Format("{0}themes/{1}/noavatar.jpg", Utils.RelativeWebRoot, BlogSettings.Instance.Theme));
  85. var uri =
  86. new Uri(
  87. System.IO.File.Exists(themeAvatar) ?
  88. string.Format("{0}themes/{1}/noavatar.jpg", Utils.AbsoluteWebRoot, BlogSettings.Instance.Theme) :
  89. string.Format("{0}pics/noavatar.jpg", Utils.AbsoluteWebRoot));
  90. imageTag = string.Format("<img src=\"{0}\" alt=\"{1}\" />", uri, description);
  91. return new Avatar { Url = uri, ImageTag = imageTag, HasNoImage = true };
  92. }
  93. var hash = FormsAuthentication.HashPasswordForStoringInConfigFile(email.ToLowerInvariant().Trim(), "MD5");
  94. if (hash != null)
  95. {
  96. hash = hash.ToLowerInvariant();
  97. }
  98. var gravatar = string.Format("http://www.gravatar.com/avatar/{0}.jpg?s={1}&amp;d=", hash, size);
  99. string link;
  100. switch (BlogSettings.Instance.Avatar)
  101. {
  102. case "identicon":
  103. link = string.Format("{0}identicon", gravatar);
  104. break;
  105. case "wavatar":
  106. link = string.Format("{0}wavatar", gravatar);
  107. break;
  108. default:
  109. link = string.Format("{0}monsterid", gravatar);
  110. break;
  111. }
  112. imageTag = string.Format(
  113. CultureInfo.InvariantCulture, AvatarImage, link, HttpUtility.HtmlEncode(description));
  114. return new Avatar { Url = new Uri(link), ImageTag = imageTag };
  115. }
  116. /// <summary>
  117. /// Returns the avatar/gravatar image tag that matches the specified email, website or avatar Url.
  118. /// </summary>
  119. /// <param name="size">
  120. /// The image size.
  121. /// </param>
  122. /// <param name="email">
  123. /// Email address.
  124. /// </param>
  125. /// <param name="website">
  126. /// The website URL.
  127. /// </param>
  128. /// <param name="avatarUrl">
  129. /// An optional avatar URL to use instead of the default.
  130. /// </param>
  131. /// <param name="description">
  132. /// Description used for the Alt/Title attributes.
  133. /// </param>
  134. /// <returns>
  135. /// The avatar/gravatar image.
  136. /// </returns>
  137. public static string GetAvatarImageTag(
  138. int size, string email, Uri website, string avatarUrl, string description)
  139. {
  140. var avatar = GetAvatar(size, email, website, avatarUrl, description);
  141. return avatar == null ? string.Empty : avatar.ImageTag;
  142. }
  143. #endregion
  144. }
  145. }