forked from BlogEngine/BlogEngine.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.aspx.cs
More file actions
267 lines (224 loc) · 9.59 KB
/
post.aspx.cs
File metadata and controls
267 lines (224 loc) · 9.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using System;
using System.Web.UI;
using BlogEngine.Core;
using BlogEngine.Core.Web.Controls;
using System.Collections.Generic;
public partial class post : BlogBasePage
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (!Security.IsAuthorizedTo(Rights.ViewPublicPosts))
{
Response.Redirect(Utils.RelativeWebRoot);
}
bool shouldThrow404 = false;
ucCommentList.Visible = ShowBlogengineComments;
var requestId = Request.QueryString["id"];
Guid id;
if ((!String.IsNullOrWhiteSpace(requestId)) && requestId.TryParse(out id))
{
Post post = Post.ApplicablePosts.Find(p => p.Id == id);
if (post != null)
{
if (!Page.IsPostBack && !Page.IsCallback && Request.RawUrl.Contains("?id="))
{
// If there's more than one post that has the same RelativeLink
// this post has then don't do a 301 redirect.
if (Post.Posts.FindAll(delegate(Post p)
{ return p.RelativeLink.Equals(post.RelativeLink); }
).Count < 2)
{
Response.Clear();
Response.StatusCode = 301;
Response.AppendHeader("location", post.RelativeLink.ToString());
Response.End();
}
}
else if (!post.IsVisible)
{
Response.Redirect(Utils.RelativeWebRoot + "default.aspx", true);
//shouldThrow404 = true;
}
else
{
this.Post = post;
// SEO redirct, discussion #446011
int idx = Request.RawUrl.IndexOf("?");
var rawUrl = idx > 0 ? Request.RawUrl.Substring(0, idx) : Request.RawUrl;
if (rawUrl != post.RelativeLink.ToString())
{
Response.Clear();
Response.StatusCode = 301;
Response.AppendHeader("location", post.RelativeLink.ToString());
Response.End();
}
var settings = BlogSettings.Instance;
string encodedPostTitle = Server.HtmlEncode(Post.Title);
string path = Utils.ApplicationRelativeWebRoot + "Custom/Themes/" + BlogSettings.Instance.GetThemeWithAdjustments(null) + "/PostView.ascx";
if (!System.IO.File.Exists(Server.MapPath(path)))
path = string.Format("{0}Custom/Controls/Defaults/PostView.ascx", Utils.ApplicationRelativeWebRoot);
PostViewBase postView = (PostViewBase)LoadControl(path);
postView.Post = Post;
postView.ID = Post.Id.ToString().Replace("-", string.Empty);
postView.Location = ServingLocation.SinglePost;
pwPost.Controls.Add(postView);
// Related posts
if (settings.EnableRelatedPosts)
{
try
{
// Try to load RelatedPosts view from theme folder
string relatedPath = Utils.ApplicationRelativeWebRoot + "Custom/Themes/" + BlogSettings.Instance.GetThemeWithAdjustments(null) + "/RelatedPosts.ascx";
RelatedPostsBase relatedView = (RelatedPostsBase)LoadControl(relatedPath);
relatedView.PostItem = this.Post;
phRelatedPosts.Controls.Add(relatedView);
}
catch (Exception)
{
// fall back to legacy code
related.Visible = true;
related.Item = this.Post;
}
}
ucCommentList.Post = Post;
Page.Title = encodedPostTitle;
AddMetaKeywords();
AddMetaDescription();
base.AddMetaTag("author", Server.HtmlEncode(Post.AuthorProfile == null ? Post.Author : Post.AuthorProfile.FullName));
List<Post> visiblePosts = Post.Posts.FindAll(delegate(Post p) { return p.IsVisible; });
if (visiblePosts.Count > 0)
{
AddGenericLink("last", visiblePosts[0].Title, visiblePosts[0].RelativeLink);
AddGenericLink("first", visiblePosts[visiblePosts.Count - 1].Title, visiblePosts[visiblePosts.Count - 1].RelativeLink);
}
InitNavigationLinks();
phRDF.Visible = settings.EnableTrackBackReceive;
base.AddGenericLink("application/rss+xml", "alternate", encodedPostTitle + " (RSS)", postView.CommentFeed + "?format=ATOM");
base.AddGenericLink("application/rss+xml", "alternate", encodedPostTitle + " (ATOM)", postView.CommentFeed + "?format=ATOM");
if (BlogSettings.Instance.EnablePingBackReceive)
{
Response.AppendHeader("x-pingback", "http://" + Request.Url.Authority + Utils.RelativeWebRoot + "pingback.axd");
}
string commentNotificationUnsubscribeEmailAddress = Request.QueryString["unsubscribe-email"];
if (!string.IsNullOrEmpty(commentNotificationUnsubscribeEmailAddress))
{
if (Post.NotificationEmails.Contains(commentNotificationUnsubscribeEmailAddress))
{
Post.NotificationEmails.Remove(commentNotificationUnsubscribeEmailAddress);
Post.Save();
phCommentNotificationUnsubscription.Visible = true;
}
}
}
}
}
else
{
shouldThrow404 = true;
}
if (shouldThrow404)
{
Response.Redirect(Utils.RelativeWebRoot + "error404.aspx", true);
}
}
/// <summary>
/// Gets the next post filtered for invisible posts.
/// </summary>
private Post GetNextPost(Post post)
{
if (post.Next == null)
return null;
if (post.Next.IsVisible)
return post.Next;
return GetNextPost(post.Next);
}
/// <summary>
/// Gets the prev post filtered for invisible posts.
/// </summary>
private Post GetPrevPost(Post post)
{
if (post.Previous == null)
return null;
if (post.Previous.IsVisible)
return post.Previous;
return GetPrevPost(post.Previous);
}
/// <summary>
/// Inits the navigation links above the post and in the HTML head section.
/// </summary>
private void InitNavigationLinks()
{
if (BlogSettings.Instance.ShowPostNavigation)
{
Post next = GetNextPost(Post);
Post prev = GetPrevPost(Post);
if ((next != null && !next.Deleted) || (prev != null && !prev.Deleted))
{
try
{
// Try to load PostNavigation from theme folder
var template = BlogSettings.Instance.IsRazorTheme ? "PostNavigation.cshtml" : "PostNavigation.ascx";
var path =$"{Utils.ApplicationRelativeWebRoot}Custom/Themes/{BlogSettings.Instance.Theme}/{template}";
if (!System.IO.File.Exists(Server.MapPath(path)))
path = Utils.ApplicationRelativeWebRoot + "Custom/Controls/Defaults/PostNavigation.ascx";
else
path = Utils.ApplicationRelativeWebRoot + "Custom/Themes/" + BlogSettings.Instance.GetThemeWithAdjustments(null) + "/PostNavigation.ascx";
var navView = (PostNavigationBase)LoadControl(path);
navView.CurrentPost = this.Post;
phPostNavigation.Controls.Add(navView);
phPostNavigation.Visible = true;
}
catch (Exception ex)
{
Utils.Log("Error loading PostNavigation template", ex);
}
}
}
}
/// <summary>
/// Adds the post's description as the description metatag.
/// </summary>
private void AddMetaDescription()
{
var desc = BlogSettings.Instance.Name + " - " + BlogSettings.Instance.Description + " - " + Post.Description;
base.AddMetaTag("description", Server.HtmlEncode(desc));
}
/// <summary>
/// Adds the post's tags as meta keywords.
/// </summary>
private void AddMetaKeywords()
{
if (Post.Tags.Count > 0)
{
base.AddMetaTag("keywords", Server.HtmlEncode(string.Join(",", Post.Tags.ToArray())));
}
if (ShowFacebookComments)
{
var tag = "\n\t<meta property=\"fb:app_id\" content=\"{0}\" />";
Header.Controls.Add(new LiteralControl(string.Format(tag, BlogSettings.Instance.FacebookAppId)));
}
}
public Post Post;
public static bool ShowBlogengineComments
{
get
{
return BlogSettings.Instance.CommentProvider == BlogSettings.CommentsBy.BlogEngine;
}
}
public static bool ShowDisqusComments
{
get
{
return BlogSettings.Instance.CommentProvider == BlogSettings.CommentsBy.Disqus;
}
}
public static bool ShowFacebookComments
{
get
{
return BlogSettings.Instance.CommentProvider == BlogSettings.CommentsBy.Facebook;
}
}
}