Exemplo n.º 1
0
        public object SaveComment(string commentText, string snippetId)
        {
            if (string.IsNullOrEmpty(commentText))
            {
                throw new InvalidOperationException("Please enter some comment text before submitting.");
            }

            using (busCodeSnippet Snippet = new busCodeSnippet())
            {
                if (Snippet.Load(snippetId) == null)
                {
                    throw new InvalidOperationException("Invalid snippet specified");
                }

                if (string.IsNullOrEmpty(this.AppUserState.Name))
                {
                    throw new UnauthorizedAccessException("You have to be signed in in order to add comments.");
                }

                if (!Snippet.AddComment(commentText, this.AppUserState.UserId))
                {
                    throw new ApplicationException("Couldn't add comment: " + Snippet.ErrorMessage);
                }
            }

            return(new
            {
                commentText = HtmlUtils.DisplayMemoEncoded(commentText),
                headerText = "by " + this.AppUserState.Name + "  " + TimeUtils.FriendlyDateString(DateTime.Now, true)
            });
        }
Exemplo n.º 2
0
        public string SaveCode(string snippetId, string code)
        {
            using (busCodeSnippet busSnippet = new busCodeSnippet())
            {
                if (busSnippet.Load(snippetId) == null)
                {
                    throw new ArgumentException("Invalid snippetId passed.");
                }
                if (!IsEditAllowed(busSnippet.Entity) && !this.AppUserState.IsAdmin)
                {
                    throw new AccessViolationException("You are not allowed to edit this snippet.");
                }
                busSnippet.Entity.Code = StringUtils.NormalizeIndentation(code);

                if (busSnippet.IsSpam())
                {
                    throw new InvalidOperationException("Invalid content.");
                }

                if (!busSnippet.Save())
                {
                    throw new InvalidOperationException("Unable to save snippet: " + busSnippet.ErrorMessage);
                }
            }
            return("ok");
        }
Exemplo n.º 3
0
 public string GetCode(string snippetId)
 {
     using (busCodeSnippet busSnippet = new busCodeSnippet())
     {
         if (busSnippet.Load(snippetId) == null)
         {
             throw new ArgumentException("Invalid snippetId passed.");
         }
         return(busSnippet.Entity.Code);
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// Returns an individual snippet based on an id
 /// </summary>
 /// <param name="snippetId"></param>
 /// <returns></returns>
 public CodeSnippet GetSnippet(string id)
 {
     using (busCodeSnippet codesnippet = CodePasteFactory.GetCodeSnippet())
     {
         if (codesnippet.Load(id) == null)
         {
             this.ThrowException("Invalid code snippet id");
         }
         codesnippet.GetComments();
         codesnippet.StripSensitiveUserInformation();
         return(codesnippet.Entity);
     }
 }
Exemplo n.º 5
0
 public string SaveTitle(string snippetId,string newTitle)
 {
     using (busCodeSnippet busSnippet = new busCodeSnippet())
     {
         if (busSnippet.Load(snippetId) == null)
             throw new ArgumentException("Invalid snippetId passed.");
         if (!IsEditAllowed(busSnippet.Entity) && !AppUserState.IsAdmin)
             throw new AccessViolationException("You are not allowed to edit this snippet.");
         busSnippet.Entity.Title = newTitle;
         if (!busSnippet.Validate())
             throw new InvalidOperationException(busSnippet.ErrorMessage);
         if (!busSnippet.Save())
             throw new InvalidOperationException(busSnippet.ErrorMessage);
         return !string.IsNullOrEmpty(busSnippet.Entity.Title) ? busSnippet.Entity.Title : "No Title";
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// Allows deletion of an individual snippet by the author.
        /// </summary>
        /// <param name="snippetId"></param>
        /// <param name="?"></param>
        /// <returns></returns>
        public bool DeleteSnippet(string snippetId, string sessionKey)
        {
            User user = this.ValidateToken(sessionKey);

            using (busCodeSnippet codeSnippet = CodePasteFactory.GetCodeSnippet())
            {
                if (codeSnippet.Load(snippetId) == null)
                {
                    this.ThrowException("Invalid snippet specified");
                }
                if (codeSnippet.Entity.UserId != user.Id)
                {
                    this.ThrowException("Access denied: You can only delete snippets you posted with this user account");
                }
                return(codeSnippet.Delete());
            }
        }
Exemplo n.º 7
0
        public bool RemoveSnippet(string snippetId)
        {
            using (busCodeSnippet Snippet = new busCodeSnippet())
            {
                if (Snippet.Load(snippetId) == null)
                {
                    throw new InvalidOperationException("Unable to delete snippet");
                }

                if (!AppUserState.IsAdmin && !this.IsEditAllowed(Snippet.Entity))
                {
                    throw new UnauthorizedAccessException("Unauthorized Access: You have to be signed in as an administrator in delete snippets.");
                }

                Snippet.Delete();
            }

            return(true);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Displays a snippet as raw HTML
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ShowHtml(string id)
        {
            using (busCodeSnippet busSnippet = new busCodeSnippet())
            {
                var snippet = busSnippet.Load(id);

                if (snippet == null)
                {
                    return(new HttpNotFoundResult());
                }

                if (snippet.Language.ToLower() != "html")
                {
                    return(new HttpNotFoundResult("Invalid snippet type"));
                }

                return(this.Content(snippet.Code));
            }
        }
Exemplo n.º 9
0
 public string SaveLanguage(string snippetId, string lang)
 {
     using (busCodeSnippet busSnippet = new busCodeSnippet())
     {
         if (busSnippet.Load(snippetId) == null)
         {
             throw new ArgumentException("Invalid snippetId passed.");
         }
         if (!IsEditAllowed(busSnippet.Entity) && !AppUserState.IsAdmin)
         {
             throw new AccessViolationException("You are not allowed to edit this snippet.");
         }
         busSnippet.Entity.Language = lang;
         if (!busSnippet.Save())
         {
             throw new InvalidOperationException("Unable to save snippet: " + busSnippet.ErrorMessage);
         }
         return("ok");
     }
 }
Exemplo n.º 10
0
 public string SaveTags(string snippetId, string tags)
 {
     using (busCodeSnippet busSnippet = new busCodeSnippet())
     {
         if (busSnippet.Load(snippetId) == null)
         {
             throw new ArgumentException("Invalid snippetId passed.");
         }
         if (!IsEditAllowed(busSnippet.Entity) && !AppUserState.IsAdmin)
         {
             throw new AccessViolationException("You are not allowed to edit this snippet.");
         }
         busSnippet.Entity.Tags = tags;
         if (!busSnippet.Save())
         {
             throw new InvalidOperationException("Unable to save snippet: " + busSnippet.ErrorMessage);
         }
         string tagResult = busSnippet.GetTagLinkList(tags);
         return(tagResult);
     }
 }
Exemplo n.º 11
0
 public bool ReportAbuse(string snippetId)
 {
     using (busCodeSnippet busSnippet = new busCodeSnippet())
     {
         if (busSnippet.Load(snippetId) == null)
         {
             throw new ArgumentException("Invalid snippetId passed.");
         }
         var snippet = busSnippet.Entity;
         // switch value
         snippet.IsAbuse = !snippet.IsAbuse;
         if (snippet.IsAbuse)
         {
             AppWebUtils.SendEmail("CodePaste.NET Abuse: " + busSnippet.Entity.Title, "Abuse reported for this snippet \r\n\r\n" + WebUtils.ResolveServerUrl("~/" + busSnippet.Entity.Id), App.Configuration.AdminEmailAddress);
         }
         if (!busSnippet.Save())
         {
             throw new ApplicationException(busSnippet.ErrorMessage);
         }
         return(snippet.IsAbuse);
     }
 }
Exemplo n.º 12
0
        public ActionResult CodeOnly(string id)
        {
            ShowSnippetViewModel model = new ShowSnippetViewModel(this);

            model.AppUserState = this.AppUserState;

            // Since this is our default handler anything invalid will
            // run through here. No path - go to new
            if (string.IsNullOrEmpty(id) || id == "0")
            {
                return(this.New());
            }

            using (busCodeSnippet busSnippet = new busCodeSnippet())
            {
                if (busSnippet.Load(id) == null)
                {
                    ErrorDisplay.ShowError("Invalid snippet id specified.");
                    model.Snippet = new CodeSnippet();
                    return(View(model));
                }

                model.Snippet = busSnippet.Entity;

                // Update the code so it's formatted
                model.FormattedCode = busSnippet.Entity.FormattedCode;

                if (!string.IsNullOrEmpty(AppUserState.UserId) && AppUserState.UserId == busSnippet.Entity.UserId || AppUserState.IsAdmin)
                {
                    model.AllowEdit = true;
                }

                ActionResult result = View(model);
                string       output = result.ToString();

                return(result);
            }
        }
Exemplo n.º 13
0
 public string SaveTitle(string snippetId, string newTitle)
 {
     using (busCodeSnippet busSnippet = new busCodeSnippet())
     {
         if (busSnippet.Load(snippetId) == null)
         {
             throw new ArgumentException("Invalid snippetId passed.");
         }
         if (!IsEditAllowed(busSnippet.Entity) && !AppUserState.IsAdmin)
         {
             throw new AccessViolationException("You are not allowed to edit this snippet.");
         }
         busSnippet.Entity.Title = newTitle;
         if (!busSnippet.Validate())
         {
             throw new InvalidOperationException(busSnippet.ErrorMessage);
         }
         if (!busSnippet.Save())
         {
             throw new InvalidOperationException(busSnippet.ErrorMessage);
         }
         return(!string.IsNullOrEmpty(busSnippet.Entity.Title) ? busSnippet.Entity.Title : "No Title");
     }
 }
Exemplo n.º 14
0
        public string SaveMainComment(string snippetId, string comment)
        {
            busCodeSnippet busSnippet = new busCodeSnippet();

            if (busSnippet.Load(snippetId) == null)
            {
                throw new ArgumentException("Invalid snippetId passed.");
            }

            if (!IsEditAllowed(busSnippet.Entity) && !AppUserState.IsAdmin)
            {
                throw new AccessViolationException("You are not allowed to edit this snippet.");
            }

            busSnippet.Entity.Comment = comment.Replace("\n", "\r\n");
            if (!busSnippet.Save())
            {
                throw new InvalidOperationException("Unable to save snippet: " + busSnippet.ErrorMessage);
            }

            string tagResult = HtmlUtils.DisplayMemo(comment);

            return(tagResult);
        }
Exemplo n.º 15
0
 public string SaveLanguage(string snippetId, string lang)
 {
     using (busCodeSnippet busSnippet = new busCodeSnippet())
     {
         if (busSnippet.Load(snippetId) == null)
             throw new ArgumentException("Invalid snippetId passed.");
         if (!IsEditAllowed(busSnippet.Entity) && !AppUserState.IsAdmin)
             throw new AccessViolationException("You are not allowed to edit this snippet.");
         busSnippet.Entity.Language = lang;
         if (!busSnippet.Save())
             throw new InvalidOperationException("Unable to save snippet: " + busSnippet.ErrorMessage);
         return "ok";
     }
 }
Exemplo n.º 16
0
        public string SaveCode(string snippetId, string code )
        {
            using (busCodeSnippet busSnippet = new busCodeSnippet())
            {
                if (busSnippet.Load(snippetId) == null)
                    throw new ArgumentException("Invalid snippetId passed.");
                if (!IsEditAllowed(busSnippet.Entity) && !this.AppUserState.IsAdmin)
                    throw new AccessViolationException("You are not allowed to edit this snippet.");
                busSnippet.Entity.Code = StringUtils.NormalizeIndentation(code);

                if (busSnippet.IsSpam())
                    throw new InvalidOperationException("Invalid content.");

                if (!busSnippet.Save())
                    throw new InvalidOperationException("Unable to save snippet: " + busSnippet.ErrorMessage);
            }
            return "ok";
        }
Exemplo n.º 17
0
        public object SaveComment(string commentText, string snippetId)
        {
            if (string.IsNullOrEmpty(commentText))
                throw new InvalidOperationException("Please enter some comment text before submitting.");

            using (busCodeSnippet Snippet = new busCodeSnippet())
            {
                if (Snippet.Load(snippetId) == null)
                    throw new InvalidOperationException("Invalid snippet specified");

                if (string.IsNullOrEmpty(this.AppUserState.Name))
                    throw new UnauthorizedAccessException("You have to be signed in in order to add comments.");

                if (!Snippet.AddComment(commentText, this.AppUserState.UserId))
                    throw new ApplicationException("Couldn't add comment: " + Snippet.ErrorMessage);
            }

            return  new
            {
                commentText = HtmlUtils.DisplayMemoEncoded(commentText),
                headerText = "by " + this.AppUserState.Name + " &nbsp;" + TimeUtils.FriendlyDateString(DateTime.Now,true)
            };
        }
Exemplo n.º 18
0
        public bool RemoveSnippet(string snippetId)
        {
            using (busCodeSnippet Snippet = new busCodeSnippet())
            {
                if (Snippet.Load(snippetId) == null)
                    throw new InvalidOperationException("Unable to delete snippet");

                if (!this.AppUserState.IsAdmin && !this.IsEditAllowed(Snippet.Entity))
                    throw new UnauthorizedAccessException("Unauthorized Access: You have to be signed in as an administrator in delete snippets.");

                Snippet.Delete();
            }

            return true;
        }
Exemplo n.º 19
0
 public bool ReportAbuse(string snippetId)
 {
     using (busCodeSnippet busSnippet = new busCodeSnippet())
     {
         if (busSnippet.Load(snippetId) == null)
             throw new ArgumentException("Invalid snippetId passed.");
         var snippet = busSnippet.Entity;
         // switch value
         snippet.IsAbuse = !snippet.IsAbuse;
         if (snippet.IsAbuse)
         {
             AppWebUtils.SendEmail("CodePaste.NET Abuse: " + busSnippet.Entity.Title, "Abuse reported for this snippet \r\n\r\n" + WebUtils.ResolveServerUrl("~/" + busSnippet.Entity.Id), App.Configuration.AdminEmailAddress);
         }
         if (!busSnippet.Save())
             throw new ApplicationException(busSnippet.ErrorMessage);
         return snippet.IsAbuse;
     }
 }
Exemplo n.º 20
0
 public string GetCode(string snippetId)
 {
     using (busCodeSnippet busSnippet = new busCodeSnippet())
     {
         if (busSnippet.Load(snippetId) == null)
             throw new ArgumentException("Invalid snippetId passed.");
         return busSnippet.Entity.Code;
     }
 }
Exemplo n.º 21
0
        public ActionResult Show(string id)
        {
            ShowSnippetViewModel model = new ShowSnippetViewModel(this);

            model.AppUserState = AppUserState;

            // Since this is our default handler anything invalid will
            // run through here. No path - go to new
            if (string.IsNullOrEmpty(id) || id == "0")
            {
                return(this.New());
            }

            using (busCodeSnippet busSnippet = new busCodeSnippet())
            {
                var snippet = busSnippet.Load(id);
                if (snippet == null)
                {
                    return(this.DisplayErrorPage("Invalid Snippet Id specified",
                                                 "You specified a snippet id or link that is invalid and cannot be displayed. " +
                                                 "Please using the <a href='./recent' class='hoverbutton'>Recent Snippets</a> or " +
                                                 "<a href='mysnippets' class='hoverbutton'>My Snippets</a> buttons to look up valid snippets.", null));
                }

                bool allowWordWrap   = false;
                bool showLineNumbers = busSnippet.Entity.ShowLineNumbers;

                string ua = Request.UserAgent.ToLower();
                if (ua.Contains("iphone") ||
                    ua.Contains("blackberry") ||
                    ua.Contains("mobile"))
                {
                    allowWordWrap   = true;
                    showLineNumbers = false;
                }


                // Update the code so it's formatted
                model.FormattedCode = busSnippet.Entity.FormattedCode;
                if (!AppUserState.IsEmpty())
                {
                    model.IsFavoritedByUser = busSnippet.IsFavorite(busSnippet.Entity.Id, AppUserState.UserId);
                }


                if (!string.IsNullOrEmpty(AppUserState.UserId) &&
                    AppUserState.UserId == busSnippet.Entity.UserId || AppUserState.IsAdmin)
                {
                    model.AllowEdit = true;
                }

                // explicitly load up comments
                busSnippet.Entity.Comments = busSnippet.GetComments();

                // For API result we have to make sure email and password are not included
                if (!string.IsNullOrEmpty(Format) && snippet.User != null)
                {
                    busSnippet.StripSensitiveUserInformation();
                }
                if (snippet.User != null)
                {
                    if (!string.IsNullOrEmpty(snippet.User.Theme))
                    {
                        model.Theme = snippet.User.Theme;
                    }
                }

                ActionResult actionResult = this.ApiResult(busSnippet.Entity);
                if (actionResult != null)
                {
                    return(actionResult);
                }

                model.Snippet = busSnippet.Entity;

                // Fix up for Ace Editor
                model.Snippet.Language = busSnippet.FixUpLanguage(model.Snippet.Language).ToLower();

                // Log views for all but poster
                if (model.Snippet.User == null ||
                    model.Snippet.User.Id != AppUserState.UserId)
                {
                    busSnippet.LogSnippetView(busSnippet.Entity.Id, Request.UserHostAddress, Request.UserAgent);
                }

                return(View("Show", model));
            }
        }
Exemplo n.º 22
0
        public ActionResult CodeOnly(string id)
        {
            ShowSnippetViewModel model = new ShowSnippetViewModel(this);
            model.AppUserState = this.AppUserState;

            // Since this is our default handler anything invalid will
            // run through here. No path - go to new
            if (string.IsNullOrEmpty(id) || id == "0")
                return this.New();

            using (busCodeSnippet busSnippet = new busCodeSnippet())
            {
                if (busSnippet.Load(id) == null)
                {
                    ErrorDisplay.ShowError("Invalid snippet id specified.");
                    model.Snippet = new CodeSnippet();
                    return View(model);
                }

                model.Snippet = busSnippet.Entity;

                // Update the code so it's formatted
                model.FormattedCode = busSnippet.Entity.FormattedCode;

                if (!string.IsNullOrEmpty(AppUserState.UserId) && AppUserState.UserId == busSnippet.Entity.UserId || AppUserState.IsAdmin)
                    model.AllowEdit = true;

                ActionResult result = View(model);
                string output = result.ToString();

                return result;
            }
        }
Exemplo n.º 23
0
        /// <summary>
        /// Displays a snippet as raw HTML
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ShowHtml(string id)
        {
            using (busCodeSnippet busSnippet = new busCodeSnippet())
            {
                var snippet = busSnippet.Load(id);

                if (snippet == null)
                    return new HttpNotFoundResult();

                if (snippet.Language.ToLower() != "html")
                    return new HttpNotFoundResult("Invalid snippet type");

                return this.Content(snippet.Code);
            }
        }
Exemplo n.º 24
0
 public string SaveTags(string snippetId, string tags)
 {
     using (busCodeSnippet busSnippet = new busCodeSnippet())
     {
         if (busSnippet.Load(snippetId) == null)
             throw new ArgumentException("Invalid snippetId passed.");
         if (!IsEditAllowed(busSnippet.Entity) && !AppUserState.IsAdmin)
             throw new AccessViolationException("You are not allowed to edit this snippet.");
         busSnippet.Entity.Tags = tags;
         if (!busSnippet.Save())
             throw new InvalidOperationException("Unable to save snippet: " + busSnippet.ErrorMessage);
         string tagResult = busSnippet.GetTagLinkList(tags);
         return tagResult;
     }
 }
Exemplo n.º 25
0
        public ActionResult Show(string id)
        {
            ShowSnippetViewModel model = new ShowSnippetViewModel(this);
            model.AppUserState = AppUserState;
            
            // Since this is our default handler anything invalid will
            // run through here. No path - go to new
            if (string.IsNullOrEmpty(id) || id == "0")
                return this.New();

            using (busCodeSnippet busSnippet = new busCodeSnippet())
            {
                var snippet = busSnippet.Load(id);
                if (snippet == null)
                {
                    return this.DisplayErrorPage("Invalid Snippet Id specified",
                        "You specified a snippet id or link that is invalid and cannot be displayed. " +
                        "Please using the <a href='./recent' class='hoverbutton'>Recent Snippets</a> or " +
                        "<a href='mysnippets' class='hoverbutton'>My Snippets</a> buttons to look up valid snippets.", null);
                }

                bool allowWordWrap = false;
                bool showLineNumbers = busSnippet.Entity.ShowLineNumbers;

                string ua = Request.UserAgent.ToLower();
                if (ua.Contains("iphone") ||
                    ua.Contains("blackberry") ||
                    ua.Contains("mobile"))
                {
                    allowWordWrap = true;
                    showLineNumbers = false;
                }
                

                // Update the code so it's formatted
                model.FormattedCode = busSnippet.Entity.FormattedCode;
                if (!AppUserState.IsEmpty())
                    model.IsFavoritedByUser = busSnippet.IsFavorite(busSnippet.Entity.Id, AppUserState.UserId);


                if (!string.IsNullOrEmpty(AppUserState.UserId) &&
                    AppUserState.UserId == busSnippet.Entity.UserId || AppUserState.IsAdmin)
                    model.AllowEdit = true;

                // explicitly load up comments
                busSnippet.Entity.Comments = busSnippet.GetComments();

                // For API result we have to make sure email and password are not included            
                if (!string.IsNullOrEmpty(Format) && snippet.User != null)
                {
                    busSnippet.StripSensitiveUserInformation();  
                }
                if (snippet.User != null)
                {
                    if (!string.IsNullOrEmpty(snippet.User.Theme))
                        model.Theme = snippet.User.Theme;
                }

                ActionResult actionResult = this.ApiResult(busSnippet.Entity);
                if (actionResult != null)
                    return actionResult;

                model.Snippet = busSnippet.Entity;

                // Fix up for Ace Editor
                model.Snippet.Language = busSnippet.FixUpLanguage(model.Snippet.Language).ToLower();

                // Log views for all but poster
                if (model.Snippet.User == null ||
                    model.Snippet.User.Id != AppUserState.UserId)
                    busSnippet.LogSnippetView(busSnippet.Entity.Id, Request.UserHostAddress, Request.UserAgent);

                return View("Show",model);
            }
        }
Exemplo n.º 26
0
        public string SaveMainComment(string snippetId, string comment)
        {
            busCodeSnippet busSnippet = new busCodeSnippet();
            if (busSnippet.Load(snippetId) == null)
                throw new ArgumentException("Invalid snippetId passed.");

            if (!IsEditAllowed(busSnippet.Entity) && !AppUserState.IsAdmin)
                throw new AccessViolationException("You are not allowed to edit this snippet.");

            busSnippet.Entity.Comment = comment.Replace("\n","\r\n");
            if (!busSnippet.Save())
                throw new InvalidOperationException("Unable to save snippet: " + busSnippet.ErrorMessage);

            string tagResult = HtmlUtils.DisplayMemo(comment);
            return tagResult;
        }