Skip to content

Commit 1d2200b

Browse files
committed
ENH: Refactor af_post.PrepareReply to avoid null object references
1 parent 86d4502 commit 1d2200b

File tree

1 file changed

+79
-94
lines changed

1 file changed

+79
-94
lines changed

Dnn.CommunityForums/controls/af_post.ascx.cs

+79-94
Original file line numberDiff line numberDiff line change
@@ -568,113 +568,98 @@ private void PrepareReply()
568568
this.Response.Redirect(this.NavigateUrl(this.TabId), false);
569569
this.Context.ApplicationInstance.CompleteRequest();
570570
}
571-
572-
this.ctlForm.Subject = Utilities.GetSharedResource("[RESX:SubjectPrefix]") + " " + System.Net.WebUtility.HtmlDecode(ti.Content.Subject);
573-
this.ctlForm.TopicSubject = System.Net.WebUtility.HtmlDecode(ti.Content.Subject);
574-
var body = string.Empty;
575-
576-
if (ti.IsLocked && (this.ForumUser.CurrentUserType == CurrentUserTypes.Anon || this.ForumUser.CurrentUserType == CurrentUserTypes.Auth))
577-
{
578-
this.Response.Redirect(this.NavigateUrl(this.TabId), false);
579-
this.Context.ApplicationInstance.CompleteRequest();
580-
}
581-
582-
if (this.Request.Params[ParamKeys.QuoteId] != null | this.Request.Params[ParamKeys.ReplyId] != null | this.Request.Params[ParamKeys.PostId] != null)
571+
else
583572
{
584-
// Setup form for Quote or Reply with body display
585-
var isQuote = false;
586-
var postId = 0;
587-
var sPostedBy = Utilities.GetSharedResource("[RESX:PostedBy]") + " {0} {1} {2}";
588-
if (this.Request.Params[ParamKeys.QuoteId] != null)
589-
{
590-
isQuote = true;
591-
if (SimulateIsNumeric.IsNumeric(this.Request.Params[ParamKeys.QuoteId]))
592-
{
593-
postId = Convert.ToInt32(this.Request.Params[ParamKeys.QuoteId]);
594-
}
595-
}
596-
else if (this.Request.Params[ParamKeys.ReplyId] != null)
597-
{
598-
if (SimulateIsNumeric.IsNumeric(this.Request.Params[ParamKeys.ReplyId]))
599-
{
600-
postId = Convert.ToInt32(this.Request.Params[ParamKeys.ReplyId]);
601-
}
602-
}
603-
else if (this.Request.Params[ParamKeys.PostId] != null)
604-
{
605-
if (SimulateIsNumeric.IsNumeric(this.Request.Params[ParamKeys.PostId]))
606-
{
607-
postId = Convert.ToInt32(this.Request.Params[ParamKeys.PostId]);
608-
}
609-
}
610-
611-
if (postId != 0)
612-
{
613-
var userDisplay = this.MainSettings.UserNameDisplay;
614-
if (this.editorType == EditorTypes.TEXTBOX)
615-
{
616-
userDisplay = "none";
617-
}
618-
619-
DotNetNuke.Modules.ActiveForums.Entities.ContentInfo ci;
620-
if (postId == TopicId)
621-
{
622-
ci = ti.Content;
623-
}
624-
else
625-
{
626-
var ri = new DotNetNuke.Modules.ActiveForums.Controllers.ReplyController(this.ForumModuleId).GetById(postId);
627-
ci = ri.Content;
628-
}
629-
630-
if (ci != null)
631-
{
632-
body = ci.Body;
633-
}
634-
635-
var post = postId == this.TopicId ? ti : (DotNetNuke.Modules.ActiveForums.Entities.IPostInfo)new DotNetNuke.Modules.ActiveForums.Controllers.ReplyController(this.ForumModuleId).GetById(postId);
636-
sPostedBy = string.Format(sPostedBy, DotNetNuke.Modules.ActiveForums.Controllers.ForumUserController.GetDisplayName(this.PortalSettings, this.MainSettings, false, false, post.Author.AuthorId, post.Author.Username, post.Author.FirstName, post.Author.LastName, post.Author.DisplayName), Utilities.GetSharedResource("On.Text"), Utilities.GetUserFormattedDateTime(post.Content.DateCreated, this.PortalId, this.UserId));
637-
}
573+
this.ctlForm.Subject = Utilities.GetSharedResource("[RESX:SubjectPrefix]") + " " + System.Net.WebUtility.HtmlDecode(ti.Content.Subject);
574+
this.ctlForm.TopicSubject = System.Net.WebUtility.HtmlDecode(ti.Content.Subject);
575+
var body = string.Empty;
638576

639-
if (this.allowHTML && this.editorType != EditorTypes.TEXTBOX)
577+
if (ti.IsLocked && (this.ForumUser.CurrentUserType == CurrentUserTypes.Anon || this.ForumUser.CurrentUserType == CurrentUserTypes.Auth))
640578
{
641-
if (body.ToUpper().Contains("<CODE") | body.ToUpper().Contains("[CODE]"))
642-
{
643-
var objCode = new CodeParser();
644-
body = CodeParser.ParseCode(System.Net.WebUtility.HtmlDecode(body));
645-
}
579+
this.Response.Redirect(this.NavigateUrl(this.TabId), false);
580+
this.Context.ApplicationInstance.CompleteRequest();
646581
}
647582
else
648583
{
649-
body = Utilities.PrepareForEdit(this.PortalId, this.ForumModuleId, this.ImagePath, body, this.allowHTML, this.editorType);
650-
}
651-
652-
if (isQuote)
653-
{
654-
this.ctlForm.EditorMode = SubmitForm.EditorModes.Quote;
655-
if (this.allowHTML && this.editorType != EditorTypes.TEXTBOX)
656-
{
657-
body = "<blockquote>" + System.Environment.NewLine + sPostedBy + System.Environment.NewLine + "<br />" + System.Environment.NewLine + body + System.Environment.NewLine + "</blockquote><br /><br />";
658-
}
659-
else
584+
if (this.Request.Params[ParamKeys.QuoteId] != null | this.Request.Params[ParamKeys.ReplyId] != null | this.Request.Params[ParamKeys.PostId] != null)
660585
{
661-
body = "[quote]" + System.Environment.NewLine + sPostedBy + System.Environment.NewLine + body + System.Environment.NewLine + "[/quote]" + System.Environment.NewLine;
586+
// Setup form for Quote or Reply with body display
587+
var isQuote = false;
588+
var postId = 0;
589+
var sPostedBy = Utilities.GetSharedResource("[RESX:PostedBy]") + " {0} {1} {2}";
590+
if (this.Request.Params[ParamKeys.QuoteId] != null)
591+
{
592+
isQuote = true;
593+
if (SimulateIsNumeric.IsNumeric(this.Request.Params[ParamKeys.QuoteId]))
594+
{
595+
postId = Convert.ToInt32(this.Request.Params[ParamKeys.QuoteId]);
596+
}
597+
}
598+
else if (this.Request.Params[ParamKeys.ReplyId] != null)
599+
{
600+
if (SimulateIsNumeric.IsNumeric(this.Request.Params[ParamKeys.ReplyId]))
601+
{
602+
postId = Convert.ToInt32(this.Request.Params[ParamKeys.ReplyId]);
603+
}
604+
}
605+
else if (this.Request.Params[ParamKeys.PostId] != null)
606+
{
607+
if (SimulateIsNumeric.IsNumeric(this.Request.Params[ParamKeys.PostId]))
608+
{
609+
postId = Convert.ToInt32(this.Request.Params[ParamKeys.PostId]);
610+
}
611+
}
612+
613+
if (postId != 0)
614+
{
615+
var post = postId == this.TopicId ? ti : (DotNetNuke.Modules.ActiveForums.Entities.IPostInfo)new DotNetNuke.Modules.ActiveForums.Controllers.ReplyController(this.ForumModuleId).GetById(postId);
616+
if (post != null)
617+
{
618+
sPostedBy = string.Format(sPostedBy, DotNetNuke.Modules.ActiveForums.Controllers.ForumUserController.GetDisplayName(this.PortalSettings, this.MainSettings, false, false, post.Author.AuthorId, post.Author.Username, post.Author.FirstName, post.Author.LastName, post.Author.DisplayName), Utilities.GetSharedResource("On.Text"), Utilities.GetUserFormattedDateTime(post.Content.DateCreated, this.PortalId, this.UserId));
619+
body = post.Content.Body;
620+
}
621+
}
622+
623+
if (this.allowHTML && this.editorType != EditorTypes.TEXTBOX)
624+
{
625+
if (body.ToUpper().Contains("<CODE") | body.ToUpper().Contains("[CODE]"))
626+
{
627+
body = CodeParser.ParseCode(System.Net.WebUtility.HtmlDecode(body));
628+
}
629+
}
630+
else
631+
{
632+
body = Utilities.PrepareForEdit(this.PortalId, this.ForumModuleId, this.ImagePath, body, this.allowHTML, this.editorType);
633+
}
634+
635+
if (isQuote)
636+
{
637+
this.ctlForm.EditorMode = SubmitForm.EditorModes.Quote;
638+
if (this.allowHTML && this.editorType != EditorTypes.TEXTBOX)
639+
{
640+
body = "<blockquote>" + System.Environment.NewLine + sPostedBy + System.Environment.NewLine + "<br />" + System.Environment.NewLine + body + System.Environment.NewLine + "</blockquote><br /><br />";
641+
}
642+
else
643+
{
644+
body = "[quote]" + System.Environment.NewLine + sPostedBy + System.Environment.NewLine + body + System.Environment.NewLine + "[/quote]" + System.Environment.NewLine;
645+
}
646+
}
647+
else
648+
{
649+
this.ctlForm.EditorMode = SubmitForm.EditorModes.ReplyWithBody;
650+
body = sPostedBy + "<br />" + body;
651+
}
652+
653+
this.ctlForm.Body = body;
662654
}
663655
}
664-
else
656+
657+
if (this.ctlForm.EditorMode != SubmitForm.EditorModes.EditReply && this.canModApprove)
665658
{
666-
this.ctlForm.EditorMode = SubmitForm.EditorModes.ReplyWithBody;
667-
body = sPostedBy + "<br />" + body;
659+
this.ctlForm.ShowModOptions = false;
668660
}
669-
670-
this.ctlForm.Body = body;
671661
}
672662
}
673-
674-
if (this.ctlForm.EditorMode != SubmitForm.EditorModes.EditReply && this.canModApprove)
675-
{
676-
this.ctlForm.ShowModOptions = false;
677-
}
678663
}
679664

680665
private void SaveTopic()

0 commit comments

Comments
 (0)