PageRenderTime 17ms CodeModel.GetById 9ms app.highlight 6ms RepoModel.GetById 0ms app.codeStats 1ms

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

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