Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Codebot.Web;
- using System.IO;
- using System.Text.RegularExpressions;
- using System.Text;
- using System.Web.SessionState;
- namespace Codebot.Lazarus.Web
- {
- [DefaultPage("/templates/main.html", IsTemplate = true)]
- public class HomePage : Codebot.Web.PageHandler
- {
- #region Some utility functions to keep things clean
- protected string EditedTitleName
- {
- get
- {
- return MapPath("title." + Request.UserHostAddress + ".txt");
- }
- }
- protected string EditedContentName
- {
- get
- {
- return MapPath("home." + Request.UserHostAddress + ".txt");
- }
- }
- protected bool IsAdmin
- {
- get
- {
- var address = Request.UserHostAddress;
- return address.Equals("127.0.0.1") || address.StartsWith("192.168.1.");
- }
- }
- protected static bool SameText(string a, string b)
- {
- a = Regex.Replace(a, "\\b+", " ");
- b = Regex.Replace(b, "\\b+", " ");
- return a.Equals(b);
- }
- #endregion
- #region Action methods invoked by ajax calls on the client
- [MethodPage("title")]
- public void ActionTitle()
- {
- if (IsPost)
- {
- string edit = HtmlEscape(ReadStream().Trim());
- if (String.IsNullOrEmpty(edit))
- return;
- string title = Title;
- if (!SameText(edit, title))
- {
- string filename = EditedTitleName;
- File.WriteAllText(filename, edit);
- }
- }
- else
- Write(EditedTitle);
- }
- [MethodPage("edit")]
- public void ActionEdit()
- {
- if (IsPost)
- {
- string edit = ReadStream().Trim();
- if (String.IsNullOrEmpty(edit))
- return;
- string original = IncludeRead("home.html");
- if (!SameText(edit, original))
- File.WriteAllText(EditedContentName, edit);
- }
- else
- Include("/templates/edit.html", true);
- }
- [MethodPage("original")]
- public void ActionOriginal()
- {
- Include("home.html");
- }
- [MethodPage("preview")]
- public void ActionPreview()
- {
- Write(Content);
- }
- [MethodPage("delete")]
- public void ActionDelete()
- {
- if (IsPost)
- {
- if (File.Exists(EditedTitleName))
- File.Delete(EditedTitleName);
- if (File.Exists(EditedContentName))
- File.Delete(EditedContentName);
- }
- }
- [MethodPage("apply")]
- public void ActionApply()
- {
- if (IsPost && IsAdmin)
- {
- string dest = MapPath("title.txt");
- string folder = Path.GetDirectoryName(dest);
- string stamp = DateTime.UtcNow.ToString("yyyyMMddHHmmss");
- if (File.Exists(EditedTitleName))
- {
- if (File.Exists(dest))
- File.Move(dest, Path.Combine(folder, "title." + stamp + ".txt"));
- File.Move(EditedTitleName, dest);
- }
- dest = MapPath("home.html");
- if (File.Exists(EditedContentName))
- {
- if (File.Exists(dest))
- File.Move(dest, Path.Combine(folder, "home." + stamp + ".txt"));
- File.Move(EditedContentName, dest);
- }
- }
- }
- #endregion
- #region Template properties
- public string BreadCrumb
- {
- get
- {
- string s = String.Empty;
- string current = MapPath("title.txt");
- string last = MapPath("/title.txt");
- while (true)
- {
- string title = File.Exists(current) ? File.ReadAllText(current) : Settings.Read("defaults/title", "Title");
- string path = Path.GetDirectoryName(current);
- string id = String.Empty;
- if (s.Length == 0)
- id = "id =\"path\" ";
- s = String.Format("<a {0}href=\"{1}\">{2}</a>", id, ReverseMapPath(path), title) + s;
- if (current.Equals(last))
- return s;
- current = Path.GetDirectoryName(path);
- current = Path.Combine(current, "title.txt");
- }
- }
- }
- public string EditedTitle
- {
- get
- {
- return File.Exists(EditedTitleName) ? HtmlEscape(File.ReadAllText(EditedTitleName)) : Title;
- }
- }
- public string Title
- {
- get
- {
- return File.Exists(MapPath("title.txt")) ? HtmlEscape(IncludeRead("title.txt")) : Settings.Read("defaults/title", "Title");
- }
- }
- public string Content
- {
- get
- {
- if (File.Exists(EditedContentName))
- return File.ReadAllText(EditedContentName);
- else
- return IncludeRead("home.html");
- }
- }
- public string EditorTools
- {
- get
- {
- string tools = IsAdmin ? "/templates/editor-tools-admin.html" : "/templates/editor-tools.html";
- return IncludeRead(tools);
- }
- }
- public string EscapedContent
- {
- get
- {
- return Content
- .Replace("<", "&lt;")
- .Replace(">", "&gt;")
- .Replace("<", "<")
- .Replace(">", ">");
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement