-
Notifications
You must be signed in to change notification settings - Fork 569
Expand file tree
/
Copy pathdefault.aspx.cs
More file actions
308 lines (282 loc) · 11.6 KB
/
default.aspx.cs
File metadata and controls
308 lines (282 loc) · 11.6 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Xml;
using System.Collections.Generic;
using BlogEngine.Core;
using System.Web.UI;
//TODO Remove All URL redirects to Business layer? To speed up page loading instead of having the most visited page handling so many tasks
public partial class _default : BlogEngine.Core.Web.Controls.BlogBasePage
{
bool SearchEngine = false;
protected void Page_Load(object sender, EventArgs e)
{
//Check to see if Client is a SearchEngine or Bot trying to craw the website
CheckBrowserCaps();
if (Page.IsCallback)
return;
// If Client is a SearchEngine or Bot Start the Processing of SearchEngine
if (SearchEngine == true)
{
ProcessSearchEngine();
}
if (Request.RawUrl.ToLowerInvariant().Contains("/category/"))
{
DisplayCategories();
}
else if (Request.RawUrl.ToLowerInvariant().Contains("/author/"))
{
DisplayAuthors();
}
else if (Request.RawUrl.ToLowerInvariant().Contains("?tag="))
{
DisplayTags();
}
else if (Request.QueryString["year"] != null || Request.QueryString["date"] != null || Request.QueryString["calendar"] != null)
{
if (Request.RawUrl.Contains("year="))
Redirect();
else
DisplayDateRange();
}
else if (Request.QueryString["apml"] != null)
{
DisplayApmlFiltering();
}
else
{
if (!BlogSettings.Instance.UseBlogNameInPageTitles)
Page.Title = BlogSettings.Instance.Name + " | ";
if (!string.IsNullOrEmpty(BlogSettings.Instance.Description))
Page.Title += Server.HtmlEncode(BlogSettings.Instance.Description);
AddMetaDescription(BlogSettings.Instance.Description);
}
AddMetaKeywords();
base.AddMetaTag("author", Server.HtmlEncode(BlogSettings.Instance.AuthorName));
}
void CheckBrowserCaps()
{
System.Web.HttpBrowserCapabilities myBrowserCaps = Request.Browser;
if (((System.Web.Configuration.HttpCapabilitiesBase)myBrowserCaps).Crawler)
{
SearchEngine = true;
}
}
/// <summary>
/// Blocks SearchEngine and Bots from indexing Human Only webpages
/// pager,keywords,tags,categories,search and archive page
/// </summary>
private void ProcessSearchEngine ()
{
string CrawlerUrl = Request.RawUrl.ToLowerInvariant();
if (CrawlerUrl.Contains("/category/") ||
CrawlerUrl.Contains("?tag") ||
CrawlerUrl.Contains("?page=") ||
// Post with Date has format http://MainWebsite.com/post/2014/02/13/post-2
CrawlerUrl.Contains(Utils.RelativeWebRoot + "/2013/", StringComparison.OrdinalIgnoreCase) || //Stops Calendar from being indexed
CrawlerUrl.Contains(Utils.RelativeWebRoot + "/2014/", StringComparison.OrdinalIgnoreCase) ||
CrawlerUrl.StartsWith(Utils.RelativeWebRoot + "search.aspx", StringComparison.OrdinalIgnoreCase) ||
CrawlerUrl.StartsWith(Utils.RelativeWebRoot + "archive.aspx", StringComparison.OrdinalIgnoreCase))
{
base.AddMetaTag("ROBOT", "NOINDEX, NOFOLLOW");
//Sends SearchEngine or Bot to the default page
Response.RedirectPermanent(Utils.RelativeWebRoot);
}
}
private void DisplayApmlFiltering()
{
Uri url = null;
if (Uri.TryCreate(Request.QueryString["apml"], UriKind.Absolute, out url))
{
Page.Title = Resources.labels.apmlFilteredList;
try
{
Dictionary<Uri, XmlDocument> docs = Utils.FindSemanticDocuments(url, "apml");
if (docs.Count > 0)
{
foreach (Uri key in docs.Keys)
{
PostList1.ContentBy = ServingContentBy.Apml;
PostList1.Posts = Search.ApmlMatches(docs[key], 30).FindAll(delegate(IPublishable p) { return p is Post; });
PostList1.Posts.Sort(delegate(IPublishable ip1, IPublishable ip2) { return ip2.DateCreated.CompareTo(ip1.DateCreated); });
Page.Title += Resources.labels.per + Server.HtmlEncode(key.Host);
break;
}
}
else
{
divError.InnerHtml = "<h1 style=\"text-align:center\">"+Resources.labels.apmlNotFoundDesc+"</h1><br /><br />";
Page.Title = Resources.labels.apmlNotFound;
}
}
catch (NotSupportedException)
{
divError.InnerHtml = "<h1 style=\"text-align:center\">"+Resources.labels.apmlNoInfoWebsite+"</h1><br /><br />";
Page.Title = Resources.labels.apmlNotFound;
}
catch (System.Net.WebException)
{
divError.InnerHtml = "<h1 style=\"text-align:center\">"+Resources.labels.apmlNoConnWebsite+"</h1><br /><br />";
Page.Title = Resources.labels.apmlAddrInvalid;
}
catch (XmlException)
{
divError.InnerHtml = "<h1 style=\"text-align:center\">"+Resources.labels.apmlInvalidXml+"</h1><br /><br />";
Page.Title = Resources.labels.apmlDocErr;
}
}
else if (PostList1.Posts == null || PostList1.Posts.Count == 0)
{
divError.InnerHtml = "<h1 style=\"text-align:center\">"+Resources.labels.apmlInvalidUrl+"</h1><br /><br />";
Page.Title = Resources.labels.apmlNotFound;
}
}
//TODO Does the old URL redirect still needed with BE 2.9 and above?
/// <summary>
/// Permanently redirects to the correct URL format if the page is requested with
/// the old URL: /default.aspx?year=2007&month=12
/// <remarks>
/// The redirection is important so that we don't end up having 2 URLs
/// to the same resource. It's for SEO purposes.
/// </remarks>
/// </summary>
private void Redirect()
{
string year = Request.QueryString["year"];
string month = Request.QueryString["month"];
string date = Request.QueryString["date"];
string page = string.IsNullOrEmpty(Request.QueryString["page"]) ? string.Empty : "?page=" + Request.QueryString["page"];
string rewrite = null;
if (!string.IsNullOrEmpty(date))
{
DateTime dateParsed = DateTime.Parse(date);
rewrite = Utils.RelativeWebRoot + dateParsed.Year + "/" + dateParsed.Month + "/" + dateParsed.Day + "/default.aspx";
}
else if (!string.IsNullOrEmpty(year) && !string.IsNullOrEmpty(month))
{
rewrite = Utils.RelativeWebRoot + year + "/" + month + "/default.aspx";
}
else if (!string.IsNullOrEmpty(year))
{
rewrite = Utils.RelativeWebRoot + year + "/default.aspx";
}
if (rewrite != null)
{
//TODO Replace this Block of code with Response.RedirectPermanent? Since Asp.net 4.0 has new method saves on code.
Response.Clear();
Response.StatusCode = 301;
Response.AppendHeader("location", rewrite + page);
Response.End();
}
}
private static readonly Regex YEAR_MONTH = new Regex("/([0-9][0-9][0-9][0-9])/([0-1][0-9])", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static readonly Regex YEAR_MONTH_DAY = new Regex("/([0-9][0-9][0-9][0-9])/([0-1][0-9])/([0-3][0-9])", RegexOptions.IgnoreCase | RegexOptions.Compiled);
/// <summary>
/// Adds the post's tags as meta keywords.
/// </summary>
private void AddMetaKeywords()
{
if (Category.Categories.Count > 0)
{
string[] categories = new string[Category.Categories.Count];
for (int i = 0; i < Category.Categories.Count; i++)
{
categories[i] = Category.Categories[i].Title;
}
string metakeywords = Server.HtmlEncode(string.Join(",", categories));
System.Web.UI.HtmlControls.HtmlMeta tag = null;
foreach (Control c in Page.Header.Controls)
{
if (c is System.Web.UI.HtmlControls.HtmlMeta && (c as System.Web.UI.HtmlControls.HtmlMeta).Name.ToLower() == "keywords")
{
tag = c as System.Web.UI.HtmlControls.HtmlMeta;
tag.Content += ", " + metakeywords;
break;
}
}
if (tag == null)
{
base.AddMetaTag("keywords", metakeywords);
}
}
}
private void DisplayCategories()
{
if (!String.IsNullOrEmpty(Request.QueryString["id"]))
{
Guid categoryId = new Guid(Request.QueryString["id"]);
PostList1.ContentBy = ServingContentBy.Category;
Category category = Category.GetCategory(categoryId, Blog.CurrentInstance.IsSiteAggregation);
PostList1.Posts = Post.GetPostsByCategory(category).ConvertAll(new Converter<Post, IPublishable>(delegate(Post p) { return p as IPublishable; }));
Page.Title = category.Title;
AddMetaDescription(string.IsNullOrWhiteSpace(category.Description) ? category.Title : category.Description);
}
}
private void DisplayAuthors()
{
if (!string.IsNullOrEmpty(Request.QueryString["name"]))
{
string author = Server.UrlDecode(Request.QueryString["name"]);
PostList1.ContentBy = ServingContentBy.Author;
PostList1.Posts = Post.GetPostsByAuthor(author).ConvertAll(new Converter<Post, IPublishable>(delegate(Post p) { return p as IPublishable; }));
Title = Resources.labels.AllPostsBy +" " + Server.HtmlEncode(author);
AddMetaDescription(Title);
}
}
private void DisplayTags()
{
if (!string.IsNullOrEmpty(Request.QueryString["tag"]))
{
var tag = Request.QueryString["tag"];
tag = tag.StartsWith("/") ? tag.Substring(1) : tag;
PostList1.ContentBy = ServingContentBy.Tag;
PostList1.Posts = Post.GetPostsByTag(tag).ConvertAll(new Converter<Post, IPublishable>(delegate(Post p) { return p as IPublishable; }));
Title = string.Format("{0} '{1}'", Resources.labels.AllPostsTagged, tag);
AddMetaDescription(Title);
}
}
private void DisplayDateRange()
{
string year = Request.QueryString["year"];
string month = Request.QueryString["month"];
string specificDate = Request.QueryString["date"];
if (!string.IsNullOrEmpty(year) && !string.IsNullOrEmpty(month))
{
DateTime dateFrom = DateTime.Parse(year + "-" + month + "-01", CultureInfo.InvariantCulture);
DateTime dateTo = dateFrom.AddMonths(1).AddMilliseconds(-1);
PostList1.ContentBy = ServingContentBy.DateRange;
PostList1.Posts = Post.GetPostsByDate(dateFrom, dateTo).ConvertAll(new Converter<Post, IPublishable>(delegate(Post p) { return p as IPublishable; }));
Title = dateFrom.ToString("MMMM yyyy");
}
else if (!string.IsNullOrEmpty(year))
{
DateTime dateFrom = DateTime.Parse(year + "-01-01", CultureInfo.InvariantCulture);
DateTime dateTo = dateFrom.AddYears(1).AddMilliseconds(-1);
PostList1.ContentBy = ServingContentBy.DateRange;
PostList1.Posts = Post.GetPostsByDate(dateFrom, dateTo).ConvertAll(new Converter<Post, IPublishable>(delegate(Post p) { return p as IPublishable; })); ;
Title = dateFrom.ToString("yyyy");
}
else if (!string.IsNullOrEmpty(specificDate) && specificDate.Length == 10)
{
DateTime date = DateTime.Parse(specificDate, CultureInfo.InvariantCulture);
PostList1.ContentBy = ServingContentBy.DateRange;
PostList1.Posts = Post.GetPostsByDate(date, date).ConvertAll(new Converter<Post, IPublishable>(delegate(Post p) { return p as IPublishable; })); ;
Title = date.ToString("MMMM d. yyyy");
}
else if (!string.IsNullOrEmpty(Request.QueryString["calendar"]))
{
calendar.Visible = true;
PostList1.Visible = false;
Title = Server.HtmlEncode(Resources.labels.calendar);
}
AddMetaDescription(Title + " - " + BlogSettings.Instance.Description);
}
private void AddMetaDescription(string desc)
{
if (string.IsNullOrEmpty(desc))
desc = BlogSettings.Instance.Name + " - " + BlogSettings.Instance.Description;
else
desc = BlogSettings.Instance.Name + " - " + BlogSettings.Instance.Description + " - " + desc;
base.AddMetaTag("description", Server.HtmlEncode(desc));
}
}