PageRenderTime 46ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/Web/Controls/MetaContent.ascx.cs

#
C# | 249 lines | 160 code | 49 blank | 40 comment | 31 complexity | 28e548ae80f734ddb43ef665c3d32ad1 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause, CPL-1.0, CC-BY-SA-3.0, GPL-2.0
  1. /// Author: Joe Audette
  2. /// Created: 2004-08-28
  3. ///
  4. ///
  5. /// The use and distribution terms for this software are covered by the
  6. /// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
  7. /// which can be found in the file CPL.TXT at the root of this distribution.
  8. /// By using this software in any fashion, you are agreeing to be bound by
  9. /// the terms of this license.
  10. ///
  11. /// You must not remove this notice, or any other, from this software.
  12. ///
  13. /// 2005-09-24 added RSS link feature provided by Philip Gear
  14. /// for enhanced RSS access in FireFox and Netscape
  15. ///
  16. /// 2009-05-26 added autodiscovery link for OpenSearch support https://developer.mozilla.org/en/Creating_OpenSearch_plugins_for_Firefox
  17. /// 2009-07-27 added config option for content type so its possible to serve as text/html in anticipation of moving to Html 5
  18. /// 2010-04-27 added logic to automatically add <meta name="viewport" content="width=670, initial-scale=0.45, minimum-scale=0.45"/> for iphone
  19. /// 2011-11-23 added option to automatically add open graph protocol description based on regular meta description, enabled by default
  20. using System;
  21. using System.Globalization;
  22. using System.Text;
  23. using System.Web;
  24. using System.Web.UI;
  25. using System.Web.UI.WebControls;
  26. using log4net;
  27. using mojoPortal.Business;
  28. using mojoPortal.Business.WebHelpers;
  29. using Resources;
  30. namespace mojoPortal.Web.UI
  31. {
  32. public partial class MetaContent : UserControl
  33. {
  34. private static readonly ILog log = LogManager.GetLogger(typeof(MetaContent));
  35. //private const string metaEncodingXhtml = "\n<meta http-equiv=\"Content-Type\" content=\"application/xhtml+xml; charset=utf-8\" />";
  36. //private const string metaEncodingHtml = "\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
  37. private string keywordCsv = string.Empty;
  38. private string description = string.Empty;
  39. private string additionalMetaMarkup = string.Empty;
  40. private StringBuilder keywords = null;
  41. private SiteSettings siteSettings = null;
  42. private bool preZoomForIPhone = true;
  43. public bool PreZoomForIPhone
  44. {
  45. get { return preZoomForIPhone; }
  46. set { preZoomForIPhone = value; }
  47. }
  48. public string KeywordCsv
  49. {
  50. get { return keywordCsv; }
  51. set { keywordCsv = value; }
  52. }
  53. public string Description
  54. {
  55. get { return description; }
  56. set { description = value; }
  57. }
  58. private bool addOpenGraphDescription = true;
  59. /// <summary>
  60. /// see
  61. /// http://www.mojoportal.com/Forums/Thread.aspx?thread=9301&mid=34&pageid=5&ItemID=2&pagenumber=1#post38648
  62. /// </summary>
  63. public bool AddOpenGraphDescription
  64. {
  65. get { return addOpenGraphDescription; }
  66. set { addOpenGraphDescription = value; }
  67. }
  68. public string AdditionalMetaMarkup
  69. {
  70. get { return additionalMetaMarkup; }
  71. set { additionalMetaMarkup = value; }
  72. }
  73. private bool disableContentType = false;
  74. public bool DisableContentType
  75. {
  76. get { return disableContentType; }
  77. set { disableContentType = value; }
  78. }
  79. public void AddKeword(string keyword)
  80. {
  81. if (keyword == null) { return; }
  82. if (keywords == null)
  83. {
  84. keywords = new StringBuilder();
  85. keywords.Append(keyword);
  86. return;
  87. }
  88. if (keywords.Length > 0)
  89. {
  90. keywords.Append("," + keyword);
  91. }
  92. else
  93. {
  94. keywords.Append(keyword);
  95. }
  96. }
  97. protected void Page_Load(object sender, EventArgs e)
  98. {}
  99. protected override void OnPreRender(EventArgs e)
  100. {
  101. base.OnPreRender(e);
  102. if (WebConfigSettings.AutoSetContentType) { AddEncoding(); }
  103. AddDescription();
  104. AddKeywords();
  105. if (additionalMetaMarkup.Length > 0)
  106. {
  107. Literal additionalMeta = new Literal();
  108. additionalMeta.Text = additionalMetaMarkup;
  109. this.Controls.Add(additionalMeta);
  110. }
  111. AddOpenSearchLink();
  112. AddIPhoneZoom();
  113. }
  114. private void AddIPhoneZoom()
  115. {
  116. if (!preZoomForIPhone) { return; }
  117. if (HttpContext.Current == null) { return; }
  118. if (Request.UserAgent == null) { return; }
  119. if (Request.UserAgent.Length == 0) { return; }
  120. if (!Request.UserAgent.Contains("iPhone")) { return; }
  121. //http://developer.apple.com/library/ios/#technotes/tn2010/tn2262/_index.html
  122. //<meta name="viewport" content="width=device-width" />
  123. Literal lit = new Literal();
  124. lit.Text = "\n<meta name=\"viewport\" content=\"width=670, initial-scale=0.45, minimum-scale=0.45\" />";
  125. this.Controls.Add(lit);
  126. }
  127. private void AddKeywords()
  128. {
  129. if (keywords != null)
  130. {
  131. if (keywordCsv.Length > 0)
  132. {
  133. keywordCsv = keywordCsv + "," + keywords.ToString();
  134. }
  135. else
  136. {
  137. keywordCsv = keywords.ToString();
  138. }
  139. }
  140. if (keywordCsv.Length == 0) { return; }
  141. Literal metaKeywordsLiteral = new Literal();
  142. metaKeywordsLiteral.Text = "\n<meta name=\"keywords\" content=\"" + keywordCsv + "\" />";
  143. this.Controls.Add(metaKeywordsLiteral);
  144. }
  145. private void AddDescription()
  146. {
  147. if (description.Length == 0) { return; }
  148. Literal metaDescriptionLiteral = new Literal();
  149. metaDescriptionLiteral.Text = "\n<meta name=\"description\" content=\"" + description + "\" />";
  150. if (addOpenGraphDescription)
  151. {
  152. metaDescriptionLiteral.Text += "\n<meta name=\"og:description\" content=\"" + description + "\" />";
  153. }
  154. this.Controls.Add(metaDescriptionLiteral);
  155. }
  156. private void AddEncoding()
  157. {
  158. if (disableContentType) { return; }
  159. string contentTypeMeta = "\n<meta http-equiv=\"Content-Type\" content=\""
  160. + WebConfigSettings.ContentMimeType
  161. + "; charset=" + WebConfigSettings.ContentEncoding + "\" />";
  162. Literal metaEncodingLiteral = new Literal();
  163. metaEncodingLiteral.Text = contentTypeMeta;
  164. this.Controls.Add(metaEncodingLiteral);
  165. }
  166. private void AddOpenSearchLink()
  167. {
  168. if (WebConfigSettings.DisableSearchIndex) { return; }
  169. if (WebConfigSettings.DisableOpenSearchAutoDiscovery) { return; }
  170. if (siteSettings == null) { siteSettings = CacheHelper.GetCurrentSiteSettings(); }
  171. if (siteSettings == null) { return; }
  172. string searchTitle;
  173. if (siteSettings.OpenSearchName.Length > 0)
  174. {
  175. searchTitle = siteSettings.OpenSearchName;
  176. }
  177. else
  178. {
  179. searchTitle = string.Format(CultureInfo.InvariantCulture, Resource.SearchDiscoveryTitleFormat, siteSettings.SiteName);
  180. }
  181. Literal openSearchLink = new Literal();
  182. openSearchLink.Text = "\n<link rel=\"search\" type=\"application/opensearchdescription+xml\" title=\""
  183. + searchTitle + "\" href=\"" + SiteUtils.GetNavigationSiteRoot() + "/SearchEngineInfo.ashx" + "\" />";
  184. this.Controls.Add(openSearchLink);
  185. }
  186. // TODO: http://www.w3.org/P3P/validator/20020128/document implement xml
  187. //http://www.w3.org/P3P/validator.html
  188. //private void AddP3PLink()
  189. //{
  190. // if (WebConfigSettings.DisableSearchIndex) { return; }
  191. // if (WebConfigSettings.DisableOpenSearchAutoDiscovery) { return; }
  192. // if (siteSettings == null) { siteSettings = CacheHelper.GetCurrentSiteSettings(); }
  193. // if (siteSettings == null) { return; }
  194. // string searchTitle = string.Format(CultureInfo.InvariantCulture, Resource.SearchDiscoveryTitleFormat, siteSettings.SiteName);
  195. // Literal openSearchLink = new Literal();
  196. // openSearchLink.Text = "\n<link rel=\"P3Pv1\" href=\"" + SiteUtils.GetNavigationSiteRoot() + "/SearchEngineInfo.ashx" + "\" />";
  197. // this.Controls.Add(openSearchLink);
  198. //}
  199. }
  200. }