PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/page.aspx.cs

#
C# | 219 lines | 154 code | 32 blank | 33 comment | 31 complexity | f9c6ddc292e0f884cd05494f315b2222 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. #region Using
  2. using System;
  3. using System.Globalization;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading;
  7. using System.Web.UI;
  8. using BlogEngine.Core;
  9. using BlogEngine.Core.Web.Controls;
  10. using Resources;
  11. using Page = BlogEngine.Core.Page;
  12. #endregion
  13. /// <summary>
  14. /// The page.
  15. /// </summary>
  16. public partial class page : BlogBasePage
  17. {
  18. /// <summary>
  19. /// Raises the <see cref="E:System.Web.UI.Control.Init"/> event to initialize the page.
  20. /// </summary>
  21. /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
  22. protected override void OnInit(EventArgs e)
  23. {
  24. var queryString = this.Request.QueryString;
  25. var qsDeletePage = queryString["deletepage"];
  26. if (qsDeletePage != null && qsDeletePage.Length == 36)
  27. {
  28. this.DeletePage(new Guid(qsDeletePage));
  29. }
  30. Guid id = GetPageId();
  31. if (id != Guid.Empty)
  32. {
  33. this.ServePage(id);
  34. this.AddMetaTags();
  35. }
  36. else if (!this.IsCallback)
  37. {
  38. this.Response.Redirect(Utils.RelativeWebRoot);
  39. }
  40. base.OnInit(e);
  41. }
  42. protected Guid GetPageId()
  43. {
  44. string id = Request.QueryString["id"];
  45. Guid result;
  46. return id != null && Guid.TryParse(id, out result) ? result : Guid.Empty;
  47. }
  48. /// <summary>
  49. /// Serves the page to the containing DIV tag on the page.
  50. /// </summary>
  51. /// <param name="id">
  52. /// The id of the page to serve.
  53. /// </param>
  54. private void ServePage(Guid id)
  55. {
  56. var pg = this.Page;
  57. if (pg == null || (!pg.IsVisible))
  58. {
  59. this.Response.Redirect(string.Format("{0}error404.aspx", Utils.RelativeWebRoot), true);
  60. return; // WLF: ReSharper is stupid and doesn't know that redirect returns this method.... or does it not...?
  61. }
  62. this.h1Title.InnerHtml = System.Web.HttpContext.Current.Server.HtmlEncode(pg.Title);
  63. var arg = new ServingEventArgs(pg.Content, ServingLocation.SinglePage);
  64. BlogEngine.Core.Page.OnServing(pg, arg);
  65. if (arg.Cancel)
  66. {
  67. this.Response.Redirect("error404.aspx", true);
  68. }
  69. if (arg.Body.Contains("[usercontrol", StringComparison.OrdinalIgnoreCase))
  70. {
  71. Utils.InjectUserControls(this.divText, arg.Body);
  72. // this.InjectUserControls(arg.Body);
  73. }
  74. else
  75. {
  76. this.divText.InnerHtml = arg.Body;
  77. }
  78. }
  79. /// <summary>
  80. /// Adds the meta tags and title to the HTML header.
  81. /// </summary>
  82. private void AddMetaTags()
  83. {
  84. if (this.Page == null)
  85. {
  86. return;
  87. }
  88. this.Title = this.Server.HtmlEncode(this.Page.Title);
  89. this.AddMetaTag("keywords", this.Server.HtmlEncode(this.Page.Keywords));
  90. this.AddMetaTag("description", this.Server.HtmlEncode(this.Page.Description));
  91. }
  92. /// <summary>
  93. /// Deletes the page.
  94. /// </summary>
  95. /// <param name="id">
  96. /// The page id.
  97. /// </param>
  98. private void DeletePage(Guid id)
  99. {
  100. var page = BlogEngine.Core.Page.GetPage(id);
  101. if (page == null)
  102. {
  103. return;
  104. }
  105. if (!page.CanUserDelete)
  106. {
  107. Response.Redirect(Utils.RelativeWebRoot);
  108. return;
  109. }
  110. if (page.HasChildPages)
  111. {
  112. return;
  113. }
  114. page.Delete();
  115. page.Save();
  116. this.Response.Redirect(Utils.RelativeWebRoot, true);
  117. }
  118. private Page _page;
  119. private bool _pageLoaded;
  120. /// <summary>
  121. /// The Page instance to render on the page.
  122. /// </summary>
  123. public new Page Page
  124. {
  125. get
  126. {
  127. if (!_pageLoaded)
  128. {
  129. _pageLoaded = true;
  130. Guid id = GetPageId();
  131. if (id != Guid.Empty)
  132. {
  133. _page = BlogEngine.Core.Page.GetPage(id);
  134. }
  135. }
  136. return _page;
  137. }
  138. }
  139. /// <summary>
  140. /// Gets the admin links to edit and delete a page.
  141. /// </summary>
  142. /// <value>The admin links.</value>
  143. public string AdminLinks
  144. {
  145. get
  146. {
  147. if (!Security.IsAuthenticated)
  148. {
  149. return string.Empty;
  150. }
  151. var sb = new StringBuilder();
  152. if (this.Page.CanUserEdit)
  153. {
  154. if (sb.Length > 0) { sb.Append(" | "); }
  155. sb.AppendFormat(
  156. "<a href=\"{0}admin/editor/page.cshtml?id={1}\">{2}</a>",
  157. Utils.RelativeWebRoot,
  158. this.Page.Id,
  159. labels.edit);
  160. }
  161. if (this.Page.CanUserDelete && !this.Page.HasChildPages)
  162. {
  163. if (sb.Length > 0) { sb.Append(" | "); }
  164. sb.AppendFormat(
  165. String.Concat("<a href=\"javascript:void(0);\" onclick=\"if (confirm('", labels.areYouSureDeletePage, "')) location.href='?deletepage={0}'\">{1}</a>"),
  166. this.Page.Id,
  167. labels.delete);
  168. }
  169. if (sb.Length > 0)
  170. {
  171. sb.Insert(0, "<div id=\"admin\">");
  172. sb.Append("</div>");
  173. }
  174. return sb.ToString();
  175. }
  176. }
  177. /// <summary>
  178. /// Gets PermaLink.
  179. /// </summary>
  180. public string PermaLink
  181. {
  182. get
  183. {
  184. return string.Format("{0}page.aspx?id={1}", Utils.AbsoluteWebRoot, this.Page.Id);
  185. }
  186. }
  187. }