1: using System;
2: using System.Web;
3: using System.Web.UI;
4: using BlogEngine.Core.Web.Controls;
5: using BlogEngine.Core;
6: using System.Text;
7:
8:
9:
10:
11: [Extension("SecurePost", "1.0", "<a href=\"http://columns.chicken-house.net\">chicken</a>")]
12: public class SecurePost
13: {
14: private static string SecurePostMessage { get { return _settings.GetSingleValue("SecurePostMessage"); } }
15: private static string Password { get { return _settings.GetSingleValue("PasswordValue"); } }
16: private static string PasswordHint { get { return _settings.GetSingleValue("PasswordHint"); } }
17:
18: private static ExtensionSettings _settings = null;
19:
20: static SecurePost()
21: {
22: Post.Serving += new EventHandler<ServingEventArgs>(Post_Serving);
23:
24: ExtensionSettings settings = new ExtensionSettings("SecurePost");
25:
26: settings.AddParameter(
27: "SecurePostMessage",
28: "顯示訊息:");
29: settings.AddParameter(
30: "PasswordHint",
31: "密碼提示:");
32: settings.AddParameter(
33: "PasswordValue",
34: "指定密碼:");
35:
36: settings.AddValues(new string[] {
37: "本篇文章已受密碼保護,請依照題示輸入密碼。",
38: "一二三四",
39: "1234"});
40:
41: //settings.ShowAdd = false;
42: //settings.ShowDelete = false;
43: //settings.ShowEdit = true;
44: settings.IsScalar = true;
45: settings.Help = "用密碼保護文章的內容。";
46:
47: ExtensionManager.ImportSettings(settings);
48:
49: _settings = ExtensionManager.GetSettings("SecurePost");
50:
51: }
52:
53: private static void Post_Serving(object sender, ServingEventArgs e)
54: {
55: Post post = sender as Post;
56:
57:
58: if (HttpContext.Current.User.Identity.IsAuthenticated == true) return;
59: if (HttpContext.Current.Request["pwd"] == Password) return;
60: if (!e.Body.StartsWith("[password]", StringComparison.CurrentCultureIgnoreCase)) return;
61:
62:
63: StringBuilder bodySB = new StringBuilder();
64: {
65: bodySB.AppendFormat(
66: "<b>{0}</b><p/>",
67: HtmlEncode(SecurePostMessage));
68:
69: if (e.Location == ServingLocation.Feed)
70: {
71: }
72: else
73: {
74: bodySB.Append("<div>");
75: bodySB.AppendFormat(
76: @"請輸入密碼(提示: <b>{0}</b>): <input id=""postpwd"" type=""password""/><button onclick=""document.location.href='{1}'+'?pwd='+escape(this.parentNode.all.postpwd.value);"">GO</button>",
77: PasswordHint,
78: post.AbsoluteLink);
79: bodySB.Append("</div>");
80: }
81: }
82: e.Body = bodySB.ToString();
83: }
84:
85: private static string HtmlEncode(string text)
86: {
87: return HttpContext.Current.Server.HtmlEncode(text);
88: }
89: }