Advertisement
Guest User

Untitled

a guest
Jan 25th, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.24 KB | None | 0 0
  1. using System;
  2. using Codebot.Web;
  3. using System.IO;
  4. using System.Text.RegularExpressions;
  5. using System.Text;
  6. using System.Web.SessionState;
  7.  
  8. namespace Codebot.Lazarus.Web
  9. {
  10.     [DefaultPage("/templates/main.html", IsTemplate = true)]
  11.     public class HomePage : Codebot.Web.PageHandler
  12.     {
  13.         #region Some utility functions to keep things clean
  14.         protected string EditedTitleName
  15.         {
  16.             get
  17.             {
  18.                 return MapPath("title." + Request.UserHostAddress + ".txt");
  19.             }
  20.         }
  21.  
  22.         protected string EditedContentName
  23.         {
  24.             get
  25.             {
  26.                 return MapPath("home." + Request.UserHostAddress + ".txt");
  27.             }
  28.         }
  29.  
  30.         protected bool IsAdmin
  31.         {
  32.             get
  33.             {
  34.                 var address = Request.UserHostAddress;
  35.                 return address.Equals("127.0.0.1") || address.StartsWith("192.168.1.");
  36.             }
  37.         }
  38.  
  39.         protected static bool SameText(string a, string b)
  40.         {
  41.             a = Regex.Replace(a, "\\b+", " ");
  42.             b = Regex.Replace(b, "\\b+", " ");
  43.             return a.Equals(b);
  44.         }
  45.         #endregion
  46.  
  47.         #region Action methods invoked by ajax calls on the client
  48.         [MethodPage("title")]
  49.         public void ActionTitle()
  50.         {
  51.             if (IsPost)
  52.             {
  53.                 string edit = HtmlEscape(ReadStream().Trim());
  54.                 if (String.IsNullOrEmpty(edit))
  55.                     return;
  56.                 string title = Title;
  57.                 if (!SameText(edit, title))
  58.                 {
  59.                     string filename = EditedTitleName;
  60.                     File.WriteAllText(filename, edit);
  61.                 }
  62.             }
  63.             else
  64.                 Write(EditedTitle);
  65.         }
  66.  
  67.         [MethodPage("edit")]
  68.         public void ActionEdit()
  69.         {
  70.             if (IsPost)
  71.             {
  72.                 string edit = ReadStream().Trim();
  73.                 if (String.IsNullOrEmpty(edit))
  74.                     return;
  75.                 string original = IncludeRead("home.html");
  76.                 if (!SameText(edit, original))
  77.                     File.WriteAllText(EditedContentName, edit);
  78.             }
  79.             else
  80.                 Include("/templates/edit.html", true);
  81.         }
  82.  
  83.         [MethodPage("original")]
  84.         public void ActionOriginal()
  85.         {
  86.             Include("home.html");
  87.         }
  88.  
  89.         [MethodPage("preview")]
  90.         public void ActionPreview()
  91.         {
  92.             Write(Content);
  93.         }
  94.  
  95.         [MethodPage("delete")]
  96.         public void ActionDelete()
  97.         {
  98.             if (IsPost)
  99.             {
  100.                 if (File.Exists(EditedTitleName))
  101.                     File.Delete(EditedTitleName);
  102.                 if (File.Exists(EditedContentName))
  103.                     File.Delete(EditedContentName);
  104.             }
  105.         }
  106.  
  107.         [MethodPage("apply")]
  108.         public void ActionApply()
  109.         {
  110.             if (IsPost && IsAdmin)
  111.             {
  112.                 string dest = MapPath("title.txt");
  113.                 string folder = Path.GetDirectoryName(dest);
  114.                 string stamp = DateTime.UtcNow.ToString("yyyyMMddHHmmss");
  115.                 if (File.Exists(EditedTitleName))
  116.                 {
  117.                     if (File.Exists(dest))
  118.                         File.Move(dest, Path.Combine(folder, "title." + stamp + ".txt"));
  119.                     File.Move(EditedTitleName, dest);
  120.                 }
  121.                 dest = MapPath("home.html");
  122.                 if (File.Exists(EditedContentName))
  123.                 {
  124.                     if (File.Exists(dest))
  125.                         File.Move(dest, Path.Combine(folder, "home." + stamp + ".txt"));
  126.                     File.Move(EditedContentName, dest);
  127.                 }
  128.             }
  129.         }
  130.         #endregion
  131.  
  132.         #region Template properties
  133.         public string BreadCrumb
  134.         {
  135.             get
  136.             {
  137.                 string s = String.Empty;
  138.                 string current = MapPath("title.txt");
  139.                 string last = MapPath("/title.txt");
  140.                 while (true)
  141.                 {
  142.                     string title = File.Exists(current) ? File.ReadAllText(current) : Settings.Read("defaults/title", "Title");
  143.                     string path = Path.GetDirectoryName(current);
  144.                     string id = String.Empty;
  145.                     if (s.Length == 0)
  146.                         id = "id =\"path\" ";
  147.                     s = String.Format("<a {0}href=\"{1}\">{2}</a>", id, ReverseMapPath(path), title) + s;
  148.                     if (current.Equals(last))
  149.                         return s;
  150.                     current = Path.GetDirectoryName(path);
  151.                     current = Path.Combine(current, "title.txt");
  152.                 }
  153.             }
  154.         }
  155.  
  156.         public string EditedTitle
  157.         {
  158.             get
  159.             {
  160.                 return File.Exists(EditedTitleName) ? HtmlEscape(File.ReadAllText(EditedTitleName)) : Title;
  161.             }
  162.         }
  163.  
  164.         public string Title
  165.         {
  166.             get
  167.             {
  168.                 return File.Exists(MapPath("title.txt")) ? HtmlEscape(IncludeRead("title.txt")) : Settings.Read("defaults/title", "Title");
  169.             }
  170.         }
  171.  
  172.         public string Content
  173.         {
  174.             get
  175.             {
  176.                 if (File.Exists(EditedContentName))
  177.                     return File.ReadAllText(EditedContentName);
  178.                 else
  179.                     return IncludeRead("home.html");
  180.             }
  181.         }
  182.  
  183.         public string EditorTools
  184.         {
  185.             get
  186.             {
  187.                 string tools = IsAdmin ? "/templates/editor-tools-admin.html" : "/templates/editor-tools.html";
  188.                 return IncludeRead(tools);
  189.             }
  190.         }
  191.  
  192.         public string EscapedContent
  193.         {
  194.             get
  195.             {
  196.                 return Content
  197.                     .Replace("&lt;", "&amp;lt;")
  198.                     .Replace("&gt;", "&amp;gt;")
  199.                     .Replace("<", "&lt;")
  200.                     .Replace(">", "&gt;");
  201.             }
  202.         }
  203.         #endregion
  204.     }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement