Example #1
0
        public HttpResponseMessage GetContent(string url, string language = null)
        {
            language = language ?? WebHelper.GetLanguageFromRequestUrl(url) ?? WebHelper.GetClientLanguage(Request);

            // prevent XSS
            url      = HttpUtility.HtmlEncode(url.Trim());
            language = HttpUtility.HtmlEncode(language);

            var statusCode  = HttpStatusCode.OK;
            var contentHtml = string.Empty;

            var relativeUrl = url;

            try
            {
                var html = "";

                if (!IsForbiddenUrl(relativeUrl) && WebHelper.SupportedLanguages.Contains(language?.ToLower()))
                {
                    html = StaticContentHelper.GetContentMarkupFor(relativeUrl, language);
                }
                else
                {
                    Log.Warning("User tried to enter a forbidden URL {URL} with LANGUAGE {LANGUAGE}", relativeUrl, language);
                }

                if (string.IsNullOrEmpty(html))
                {
                    html        = StaticContentHelper.GetContentMarkupFor($"{language}/404.html", language);
                    relativeUrl = $"{language}/404.html";
                    statusCode  = HttpStatusCode.NotFound;
                }

                var contentNode = StaticContentHelper.FindStaticContentNode(FrontendSettingsViaduc.Instance, html);
                contentHtml = StaticContentHelper.ProcessStaticMarkupForSpa(FrontendSettingsViaduc.Instance, RequestContext, relativeUrl, language,
                                                                            contentNode.OuterHtml);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "StaticController.GetContent failed for {url}/{language}", url, language);
                var html = StaticContentHelper.GetContentMarkupFor($"{language}/500.html", language);
                relativeUrl = $"{language}/500.html";

                if (string.IsNullOrEmpty(html))
                {
                    statusCode = HttpStatusCode.InternalServerError;
                }
                else
                {
                    var contentNode = StaticContentHelper.FindStaticContentNode(FrontendSettingsViaduc.Instance, html);
                    contentHtml = StaticContentHelper.ProcessStaticMarkupForSpa(FrontendSettingsViaduc.Instance, RequestContext, relativeUrl,
                                                                                language, contentNode.OuterHtml);
                }
            }

            return(CreateResponse(statusCode, contentHtml, "text/html"));
        }
        public async Task <ActionResult> NotFound(string url = "")
        {
            var language      = WebHelper.GetClientLanguage(Request);
            var contentMarkup = StaticContentHelper.GetContentMarkupFor($"{language}/404.html", language);
            var viewModel     = CreateErrorViewModel(contentMarkup, language, url, true);

            Log.Warning("Non-Existing Route {ROUTE} was called", viewModel.Url);

            var viewResult = await Task.FromResult(View("Index", viewModel));

            return(viewResult);
        }
        public async Task <ActionResult> Index(string exId, string url = "")
        {
            var language      = WebHelper.GetClientLanguage(Request);
            var contentMarkup = StaticContentHelper.GetContentMarkupFor($"{language}/500.html", language);
            var viewModel     = CreateErrorViewModel(contentMarkup, language, url);

            viewModel.ErrorId = exId;

            var viewResult = await Task.FromResult(View(viewModel));

            return(viewResult);
        }
Example #4
0
        public HttpResponseMessage GetFile(string url)
        {
            var contentPathAdjuster = new Regex(@"(href|src)(=[\""'].*?)(\/?content\/?)");
            var language            = WebHelper.GetLanguageFromRequestUrl(url) ?? WebHelper.GetClientLanguage(Request);

            // prevent XSS
            url = HttpUtility.HtmlEncode(url.Trim());

            var statusCode = HttpStatusCode.OK;
            var content    = "";

            try
            {
                if (!IsForbiddenUrl(url))
                {
                    content = StaticContentHelper.GetContent(url);
                    content = contentPathAdjuster.Replace(content, "$1$2" + DirectoryHelper.Instance.StaticDefaultPath);
                }
                else
                {
                    Log.Warning("User tried to enter a forbidden URL {URL} with LANGUAGE {LANGUAGE}", url, language);
                }

                if (string.IsNullOrEmpty(content))
                {
                    return(CreateResponse(HttpStatusCode.NotFound, "", MimeMapping.GetMimeMapping(url)));
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "StaticController.GetResource failed for {url}/{language}", url, language);
                var html = StaticContentHelper.GetContentMarkupFor($"{language}/500.html", language);
                url = $"{language}/500.html";

                statusCode = HttpStatusCode.InternalServerError;
                if (!string.IsNullOrEmpty(html))
                {
                    var contentNode = StaticContentHelper.FindStaticContentNode(FrontendSettingsViaduc.Instance, html);
                    content = StaticContentHelper.ProcessStaticMarkupForSpa(FrontendSettingsViaduc.Instance, RequestContext, url, language,
                                                                            contentNode.OuterHtml);
                }
            }

            return(CreateResponse(statusCode, content, MimeMapping.GetMimeMapping(url)));
        }
        public ActionResult Index()
        {
            var language = WebHelper.GetClientLanguage(Request);

            ViewBag.Language = language;
            ViewBag.Title    =
                FrontendSettingsViaduc.Instance.GetTranslation(language, "page.title",
                                                               "Schweizerisches Bundesarchiv BAR");

            var viewModel = new ContentIndexModel();

            var contentHtml = string.Empty;

            var subRequestPath = WebHelper.GetApplicationSubRequestPath(Request);
            var relativeUrl    = StaticContentHelper.GetRelativeUrl(Request.ApplicationPath, subRequestPath, true);

            var contentMarkup = string.Empty;

            try
            {
                contentMarkup = StaticContentHelper.GetContentMarkupFor(relativeUrl, language);
                if (string.IsNullOrEmpty(contentMarkup))
                {
                    contentMarkup = StaticContentHelper.GetContentMarkupFor($"{language}/404.html", language);
                }
            }
            catch (Exception ex)
            {
                ViewBag.Error = ex.Message;
                contentMarkup = StaticContentHelper.GetContentMarkupFor($"{language}/500.html", language);
            }

            try
            {
                var contentNode =
                    StaticContentHelper.FindStaticContentNode(FrontendSettingsViaduc.Instance, contentMarkup);

                contentHtml = StaticContentHelper.ProcessStaticMarkupForMvc(FrontendSettingsViaduc.Instance, Request,
                                                                            relativeUrl, language, contentNode.OuterHtml);
            }
            catch (Exception ex)
            {
                contentHtml = "<textarea>" + ServiceHelper.GetExceptionInfo(ex) + "</textarea>";
            }

            ViewBag.Html = contentHtml;

            var editMode = viewModel.EditMode = Session.HasFeatureStaticContentEdit();

            viewModel.BaseUrl = Request.ApplicationPath;

            ViewBag.HtmlClasses = editMode ? "edit-mode" : string.Empty;

            var normalizedUrl = RoutingHelper.NormalizePath(language, relativeUrl);

            viewModel.LanguageLinks = new Dictionary <string, string>();
            WebHelper.SupportedLanguages.ForEach(lang =>
            {
                viewModel.LanguageLinks[lang] = StringHelper.AddToString(Request.ApplicationPath, "/",
                                                                         RoutingHelper.LocalizePath(lang, normalizedUrl));
            });

            return(View(viewModel));
        }