public ActionResult Submit(string url, string title, string category, string description, string tags) { string captchaChallenge = null; string captchaResponse = null; bool captchaEnabled = !CurrentUser.ShouldHideCaptcha(); if (captchaEnabled) { captchaChallenge = HttpContext.Request.Form[CaptchaValidator.ChallengeInputName]; captchaResponse = HttpContext.Request.Form[CaptchaValidator.ResponseInputName]; } JsonViewData viewData = Validate <JsonViewData>( new Validation(() => captchaEnabled && string.IsNullOrEmpty(captchaChallenge), "Captcha challenge cannot be blank."), new Validation(() => captchaEnabled && string.IsNullOrEmpty(captchaResponse), "Captcha verification words cannot be blank."), new Validation(() => !IsCurrentUserAuthenticated, "You are currently not authenticated."), new Validation(() => captchaEnabled && !CaptchaValidator.Validate(CurrentUserIPAddress, captchaChallenge, captchaResponse), "Captcha verification words are incorrect.") ); if (viewData == null) { try { StoryCreateResult result = _storyService.Create( CurrentUser, url.NullSafe(), title.NullSafe(), category.NullSafe(), description.NullSafe(), tags.NullSafe(), CurrentUserIPAddress, HttpContext.Request.UserAgent, ((HttpContext.Request.UrlReferrer != null) ? HttpContext.Request.UrlReferrer.ToString() : null), HttpContext.Request.ServerVariables, story => string.Concat(Settings.RootUrl, Url.RouteUrl("Detail", new { name = story.UniqueName })) ); viewData = new JsonCreateViewData { isSuccessful = string.IsNullOrEmpty(result.ErrorMessage), errorMessage = result.ErrorMessage, url = result.DetailUrl }; } catch (Exception e) { Log.Exception(e); viewData = new JsonViewData { errorMessage = FormatStrings.UnknownError.FormatWith("submitting story") }; } } return(Json(viewData)); }
public async Task <IActionResult> OnPostAsync(string id) { if (id is null) { return(NotFound()); } Question = await database.Questions.FirstOrDefaultAsync(q => q.Identifier == id); if (Question is null) { return(NotFound()); } if (Question.QuestionState == Question.State.Blocked) { return(Forbid()); } Messages = Question.Messages; if (!ModelState.IsValid) { return(Page()); } #if !DEBUG if (!IsAdmin && !await validator.Validate()) { ModelState.AddModelError(string.Empty, "Recaptcha nicht valide"); return(Page()); } #endif await questionHandler.SendMessageMarkdown(Question, Input.Title, Input.Message, IsAdmin?Message.Author.Answerer : Message.Author.Asker); return(Page()); }
public ActionResult Submit(string url, string title, string category, string description, string tags) { string captchaChallenge = null; string captchaResponse = null; bool captchaEnabled = !CurrentUser.ShouldHideCaptcha(); if (captchaEnabled) { captchaChallenge = HttpContext.Request.Form[CaptchaValidator.ChallengeInputName]; captchaResponse = HttpContext.Request.Form[CaptchaValidator.ResponseInputName]; } JsonViewData viewData = Validate <JsonViewData>( new Validation(() => captchaEnabled && string.IsNullOrEmpty(captchaChallenge), "Pole Captcha nie mo¿e byæ puste."), new Validation(() => captchaEnabled && string.IsNullOrEmpty(captchaResponse), "Pole Captcha nie mo¿e byæ puste."), new Validation(() => !IsCurrentUserAuthenticated, "Nie jesteœ zalogowany"), new Validation(() => captchaEnabled && !CaptchaValidator.Validate(CurrentUserIPAddress, captchaChallenge, captchaResponse), "Nieudana weryfikacja Captcha") ); if (viewData == null) { try { using (IUnitOfWork unitOfWork = UnitOfWork.Get()) { StoryCreateResult result = _storyService.Create( CurrentUser, url.NullSafe(), title.NullSafe(), category.NullSafe(), description.NullSafe(), tags.NullSafe(), CurrentUserIPAddress, HttpContext.Request.UserAgent, ((HttpContext.Request.UrlReferrer != null) ? HttpContext.Request.UrlReferrer.ToString() : null), HttpContext.Request.ServerVariables, story => string.Concat(Settings.RootUrl, Url.RouteUrl("Detail", new { name = story.UniqueName })) ); viewData = new JsonCreateViewData { isSuccessful = string.IsNullOrEmpty(result.ErrorMessage), errorMessage = result.ErrorMessage, url = result.DetailUrl }; unitOfWork.Commit(); } } catch (Exception e) { Log.Exception(e); viewData = new JsonViewData { errorMessage = FormatStrings.UnknownError.FormatWith("dodania artyku³u") }; } } return(Json(viewData)); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } #if !DEBUG if (!await validator.Validate()) { ModelState.AddModelError(string.Empty, "Recaptcha nicht valide"); return(Page()); } #endif if (!Input.Consent) { ModelState.AddModelError(string.Empty, "Bitte stimme der Veröffentlichung deiner Frage zu."); return(Page()); } string dirty = Markdown.ToHtml(Input.Question, markdown); string html = sanitizer.Sanitize(dirty); string text = Markdown.ToPlainText(Input.Question, markdown); List <string> tags = Input.Tags?.Split(',').Select(s => s.Trim()).Select(s => s.Substring(0, Math.Min(10, s.Length))).ToList() ?? new List <string>(); string shortName = Regex.Replace(Input.Title, @"[^\u0000-\u007F]+", string.Empty); // Strip non-ascii characters shortName = Regex.Replace(shortName.ToLower(), @"\s+", "-"); const int maxLength = 50; const int minLength = 20; // Find the highest cut point that lies under maxlength and on a word boundary int cut = Math.Min(maxLength, shortName.Length); int c = 0; while ((c = shortName.IndexOf('-', c + 1)) != -1) { if (c <= maxLength) { cut = c; } else { break; } } shortName = shortName.Substring(0, cut); while (shortName.Length < minLength) { shortName += "-" + Guid.NewGuid().ToString().Substring(0, minLength - shortName.Length); } while (await database.Questions.AnyAsync(q => q.ShortName == shortName)) { shortName += "-" + Guid.NewGuid().ToString().Substring(0, 6); } string id = await database.GetNewID(); Question question; database.Questions.Add(question = new() { Name = Input.Name, Title = Input.Title, TagString = string.Join(',', Input.Tags?.Split(',').Select(s => s.Trim()) ?? Array.Empty <string>()), Email = Input.Email, QuestionHtml = html, QuestionText = text, QuestionSource = Input.Question, QuestionDate = DateTime.Now, Identifier = id, QuestionState = Question.State.Asked, ShortName = shortName }); await database.SaveChangesAsync(); if (!string.IsNullOrWhiteSpace(Input.Email)) { notificationBuilder.PushForQuestion("NewQuestionUser", question); } notificationBuilder.PushForQuestion("NewQuestionAdmin", question, false, true); homeAssistant.NotifyForQuestion(question); return(Redirect($"/QuestionConfirm?id={id}&email={!string.IsNullOrWhiteSpace(Input.Email)}" + (Input.SaveId ? $"&save=1&name=" + HttpUtility.UrlEncode(Input.Title) : ""))); }