<commit_before>﻿using System;
using System.Linq;
using System.Web.Mvc;
using FunnelWeb.Eventing;
using FunnelWeb.Filters;
using FunnelWeb.Model;
using FunnelWeb.Model.Repositories;
using FunnelWeb.Model.Strings;
using FunnelWeb.Settings;
using FunnelWeb.Web.Application.Mvc;
using FunnelWeb.Web.Application.Mvc.ActionResults;
using FunnelWeb.Web.Application.Spam;
using FunnelWeb.Web.Views.Wiki;

namespace FunnelWeb.Web.Controllers
{
    [FunnelWebRequest]
    [HandleError]
    [ValidateInput(false)]
    public class WikiController : Controller
    {
        private const int ItemsPerPage = 30;
        public IEntryRepository EntryRepository { get; set; }
        public ITagRepository TagRepository { get; set; }
        public IFeedRepository FeedRepository { get; set; }
        public ISpamChecker SpamChecker { get; set; }
        public IEventPublisher EventPublisher { get; set; }
        public ISettingsProvider SettingsProvider { get; set; }

        public virtual ActionResult Home(int? pageNumber)
        {
            var settings = SettingsProvider.GetSettings<FunnelWebSettings>();
            if (!string.IsNullOrWhiteSpace(settings.CustomHomePage))
            {
                var entry = EntryRepository.GetEntry(settings.CustomHomePage);
                if (entry != null)
                {
                    ViewData.Model = new PageModel(entry.Name, entry);
                    return new PageTemplateActionResult(
                        pageTemplate: entry.PageTemplate, 
                        actionName: "Page"
                    );
                }
            }

            return Recent(pageNumber ?? 0);
        }

        public virtual ActionResult Recent(int pageNumber)
        {
            var entries = FeedRepository.GetRecentEntries(pageNumber * ItemsPerPage, ItemsPerPage);
            var totalItems = FeedRepository.GetEntryCount();
            ViewData.Model = new RecentModel("Recent Posts", entries, pageNumber, (int)((decimal)totalItems / ItemsPerPage + 1), ControllerContext.RouteData.Values["action"].ToString());
            return View("Recent");
        }

        public virtual ActionResult Search([Bind(Prefix = "q")] string searchText, bool? is404)
        {
            var results = EntryRepository.Search(searchText);
            return View("Search", new SearchModel(searchText, is404 ?? false, results));
        }

        public virtual ActionResult Page(PageName page, int? revision)
        {
            if (revision != null && !SettingsProvider.GetSettings<FunnelWebSettings>().EnablePublicHistory)
            {
                return RedirectToAction("Page", "Wiki", new { page, revision = (int?)null});
            }

            var entry = EntryRepository.GetEntry(page, revision ?? 0);
            if (entry == null)
            {
                if (HttpContext.User.Identity.IsAuthenticated)
                {
					return RedirectToAction("Edit", "WikiAdmin", new { Area = "Admin", page });
                }
                return Search(page, true);
            }

            if (entry.Status == EntryStatus.Private && !HttpContext.User.Identity.IsAuthenticated)
            {
                return Search(page, true);
            }

            ViewData.Model = new PageModel(page, entry, revision);
            return new PageTemplateActionResult(
                pageTemplate: entry.PageTemplate
            );
        }

        // Posting a comment
        [HttpPost]
        public virtual ActionResult Page(PageName page, PageModel model)
        {
            var entry = EntryRepository.GetEntry(page);
            if (entry == null)
                return RedirectToAction("Recent");

            if (!ModelState.IsValid)
            {
                model.Entry = entry;
                model.IsPriorVersion = false;
                model.Page = page;
                ViewData.Model = model;
                return new PageTemplateActionResult(entry.PageTemplate, "Page");
            }

            var comment = entry.Comment();
            comment.AuthorEmail = model.CommenterEmail ?? string.Empty;
            comment.AuthorName = model.CommenterName ?? string.Empty;
            comment.AuthorUrl = model.CommenterBlog ?? string.Empty;
            comment.AuthorIp = Request.UserHostAddress;
            comment.EntryRevisionNumber = entry.LatestRevision.RevisionNumber;
            comment.Body = model.Comments;

            try
            {
                SpamChecker.Verify(comment);
            }
            catch (Exception ex)
            {
                HttpContext.Trace.Warn("Akismet is offline, comment cannot be validated: " + ex);
            }

            // Anything posted after the disable date is considered spam (the comment box shouldn't be visible anyway)
            var settings = SettingsProvider.GetSettings<FunnelWebSettings>();
            if (settings.DisableCommentsOlderThan > 0 && DateTime.UtcNow.AddDays(settings.DisableCommentsOlderThan) > entry.Published)
            {
                comment.IsSpam = true;
            }<commit_msg>Fixed a bug that occurs when someone leaves a comment that fails validation.<commit_after>﻿using System;
using System.Linq;
using System.Web.Mvc;
using FunnelWeb.Eventing;
using FunnelWeb.Filters;
using FunnelWeb.Model;
using FunnelWeb.Model.Repositories;
using FunnelWeb.Model.Strings;
using FunnelWeb.Settings;
using FunnelWeb.Web.Application.Mvc;
using FunnelWeb.Web.Application.Mvc.ActionResults;
using FunnelWeb.Web.Application.Spam;
using FunnelWeb.Web.Views.Wiki;

namespace FunnelWeb.Web.Controllers
{
    [FunnelWebRequest]
    [HandleError]
    [ValidateInput(false)]
    public class WikiController : Controller
    {
        private const int ItemsPerPage = 30;
        public IEntryRepository EntryRepository { get; set; }
        public ITagRepository TagRepository { get; set; }
        public IFeedRepository FeedRepository { get; set; }
        public ISpamChecker SpamChecker { get; set; }
        public IEventPublisher EventPublisher { get; set; }
        public ISettingsProvider SettingsProvider { get; set; }

        public virtual ActionResult Home(int? pageNumber)
        {
            var settings = SettingsProvider.GetSettings<FunnelWebSettings>();
            if (!string.IsNullOrWhiteSpace(settings.CustomHomePage))
            {
                var entry = EntryRepository.GetEntry(settings.CustomHomePage);
                if (entry != null)
                {
                    ViewData.Model = new PageModel(entry.Name, entry);
                    return new PageTemplateActionResult(
                        pageTemplate: entry.PageTemplate, 
                        actionName: "Page"
                    );
                }
            }

            return Recent(pageNumber ?? 0);
        }

        public virtual ActionResult Recent(int pageNumber)
        {
            var entries = FeedRepository.GetRecentEntries(pageNumber * ItemsPerPage, ItemsPerPage);
            var totalItems = FeedRepository.GetEntryCount();
            ViewData.Model = new RecentModel("Recent Posts", entries, pageNumber, (int)((decimal)totalItems / ItemsPerPage + 1), ControllerContext.RouteData.Values["action"].ToString());
            return View("Recent");
        }

        public virtual ActionResult Search([Bind(Prefix = "q")] string searchText, bool? is404)
        {
            var results = EntryRepository.Search(searchText);
            return View("Search", new SearchModel(searchText, is404 ?? false, results));
        }

        public virtual ActionResult Page(PageName page, int? revision)
        {
            if (revision != null && !SettingsProvider.GetSettings<FunnelWebSettings>().EnablePublicHistory)
            {
                return RedirectToAction("Page", "Wiki", new { page, revision = (int?)null});
            }

            var entry = EntryRepository.GetEntry(page, revision ?? 0);
            if (entry == null)
            {
                if (HttpContext.User.Identity.IsAuthenticated)
                {
					return RedirectToAction("Edit", "WikiAdmin", new { Area = "Admin", page });
                }
                return Search(page, true);
            }

            if (entry.Status == EntryStatus.Private && !HttpContext.User.Identity.IsAuthenticated)
            {
                return Search(page, true);
            }

            ViewData.Model = new PageModel(page, entry, revision);
            return new PageTemplateActionResult(
                pageTemplate: entry.PageTemplate
            );
        }

        // Posting a comment
        [HttpPost]
        public virtual ActionResult Page(PageName page, PageModel model)
        {
            var entry = EntryRepository.GetEntry(page);
            if (entry == null)
                return RedirectToAction("Recent");

            if (!ModelState.IsValid)
            {
                model.Entry = entry;
                model.IsPriorVersion = false;
                model.Page = page;
                model.Revision = entry.Revisions.Last();
                ViewData.Model = model;
                return new PageTemplateActionResult(entry.PageTemplate, "Page");
            }

            var comment = entry.Comment();
            comment.AuthorEmail = model.CommenterEmail ?? string.Empty;
            comment.AuthorName = model.CommenterName ?? string.Empty;
            comment.AuthorUrl = model.CommenterBlog ?? string.Empty;
            comment.AuthorIp = Request.UserHostAddress;
            comment.EntryRevisionNumber = entry.LatestRevision.RevisionNumber;
            comment.Body = model.Comments;

            try
            {
                SpamChecker.Verify(comment);
            }
            catch (Exception ex)
            {
                HttpContext.Trace.Warn("Akismet is offline, comment cannot be validated: " + ex);
            }

            // Anything posted after the disable date is considered spam (the comment box shouldn't be visible anyway)
            var settings = SettingsProvider.GetSettings<FunnelWebSettings>();
            if (settings.DisableCommentsOlderThan > 0 && DateTime.UtcNow.AddDays(settings.DisableCommentsOlderThan) > entry.Published)
            {
                comment.IsSpam = true;
            }
{'fraction_non_alphanumeric': 0.08940719144800778, 'fraction_numerical': 0.0023323615160349854, 'mean_word_length': 2.002042007001167, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3513332', 'n_tokens_mistral': 2628, 'n_tokens_neox': 2482, 'n_words': 594}