PageRenderTime 109ms CodeModel.GetById 98ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Avatar.cs

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