/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
- // --------------------------------------------------------------------------------------------------------------------
- // <summary>
- // Avatar support.
- // </summary>
- // --------------------------------------------------------------------------------------------------------------------
-
- namespace BlogEngine.Core
- {
- using System;
- using System.Globalization;
- using System.Web;
- using System.Web.Security;
-
- /// <summary>
- /// Avatar support.
- /// </summary>
- public class Avatar
- {
- #region Constants and Fields
-
- /// <summary>
- /// The avatar image.
- /// </summary>
- private const string AvatarImage = "<img class=\"photo\" src=\"{0}\" alt=\"{1}\" />";
-
- /// <summary>
- /// Gets or sets the URL to the Avatar image.
- /// </summary>
- public Uri Url { get; set; }
-
- /// <summary>
- /// Gets or sets the image tag for the Avatar image.
- /// </summary>
- public string ImageTag { get; set; }
-
- /// <summary>
- /// Gets or sets a value indicating whether there is not a specific image available.
- /// </summary>
- public bool HasNoImage { get; set; }
-
- #endregion
-
- #region Public Methods
-
- /// <summary>
- /// Returns the avatar/gravatar that matches the specified email, website or avatar Url.
- /// </summary>
- /// <param name="size">
- /// The image size.
- /// </param>
- /// <param name="email">
- /// Email address.
- /// </param>
- /// <param name="website">
- /// The website URL.
- /// </param>
- /// <param name="avatarUrl">
- /// An optional avatar URL to use instead of the default.
- /// </param>
- /// <param name="description">
- /// Description used for the Alt/Title attributes.
- /// </param>
- /// <returns>
- /// The avatar/gravatar image.
- /// </returns>
- public static Avatar GetAvatar(int size, string email, Uri website, string avatarUrl, string description)
- {
- if (BlogSettings.Instance.Avatar == "none")
- {
- return new Avatar { HasNoImage = true };
- }
-
- string imageTag;
- Uri url;
-
- if (!string.IsNullOrEmpty(avatarUrl) && Uri.TryCreate(avatarUrl, UriKind.RelativeOrAbsolute, out url))
- {
- imageTag = string.Format(
- CultureInfo.InvariantCulture, AvatarImage, url, HttpUtility.HtmlEncode(description));
-
- return new Avatar { Url = url, ImageTag = imageTag };
- }
-
- if (string.IsNullOrEmpty(email) || !email.Contains("@"))
- {
- if (website != null && website.ToString().Length > 0 && website.ToString().Contains("http://"))
- {
- url =
- new Uri(
- string.Format(
- "http://cligs.websnapr.com/?url={0}&size=t",
- HttpUtility.UrlEncode(website.ToString())));
-
- imageTag = string.Format(
- CultureInfo.InvariantCulture, "<img class=\"thumb\" src=\"{0}\" alt=\"{1}\" />", url, email);
-
- return new Avatar { Url = url, ImageTag = imageTag };
- }
-
- var themeAvatar = HttpContext.Current.Server.MapPath(string.Format("{0}themes/{1}/noavatar.jpg", Utils.RelativeWebRoot, BlogSettings.Instance.Theme));
-
- var uri =
- new Uri(
- System.IO.File.Exists(themeAvatar) ?
- string.Format("{0}themes/{1}/noavatar.jpg", Utils.AbsoluteWebRoot, BlogSettings.Instance.Theme) :
- string.Format("{0}pics/noavatar.jpg", Utils.AbsoluteWebRoot));
-
- imageTag = string.Format("<img src=\"{0}\" alt=\"{1}\" />", uri, description);
-
- return new Avatar { Url = uri, ImageTag = imageTag, HasNoImage = true };
- }
-
- var hash = FormsAuthentication.HashPasswordForStoringInConfigFile(email.ToLowerInvariant().Trim(), "MD5");
- if (hash != null)
- {
- hash = hash.ToLowerInvariant();
- }
-
- var gravatar = string.Format("http://www.gravatar.com/avatar/{0}.jpg?s={1}&d=", hash, size);
-
- string link;
- switch (BlogSettings.Instance.Avatar)
- {
- case "identicon":
- link = string.Format("{0}identicon", gravatar);
- break;
-
- case "wavatar":
- link = string.Format("{0}wavatar", gravatar);
- break;
-
- default:
- link = string.Format("{0}monsterid", gravatar);
- break;
- }
-
- imageTag = string.Format(
- CultureInfo.InvariantCulture, AvatarImage, link, HttpUtility.HtmlEncode(description));
-
- return new Avatar { Url = new Uri(link), ImageTag = imageTag };
- }
-
- /// <summary>
- /// Returns the avatar/gravatar image tag that matches the specified email, website or avatar Url.
- /// </summary>
- /// <param name="size">
- /// The image size.
- /// </param>
- /// <param name="email">
- /// Email address.
- /// </param>
- /// <param name="website">
- /// The website URL.
- /// </param>
- /// <param name="avatarUrl">
- /// An optional avatar URL to use instead of the default.
- /// </param>
- /// <param name="description">
- /// Description used for the Alt/Title attributes.
- /// </param>
- /// <returns>
- /// The avatar/gravatar image.
- /// </returns>
- public static string GetAvatarImageTag(
- int size, string email, Uri website, string avatarUrl, string description)
- {
- var avatar = GetAvatar(size, email, website, avatarUrl, description);
- return avatar == null ? string.Empty : avatar.ImageTag;
- }
-
- #endregion
- }
- }