Beispiel #1
0
        private static string GenerateCache(string cacheKey)
        {
            String html;

            lock (locker)
            {
                ICmsPageGenerator cmsPage = new PageGeneratorObject(Cms.Context);
                html = cmsPage.GetIndex();
                Cms.Cache.Insert(cacheKey, html);
                TemplateUtils.SaveFile(html, Cms.PhysicPath + "index.html", Encoding.UTF8);
            }

            return(html);
        }
Beispiel #2
0
 /// <summary>
 /// 呈现标签页
 /// </summary>
 /// <param name="context"></param>
 /// <param name="t"></param>
 public static void RenderTag(CmsContext context, string t)
 {
     //检测网站状态
     if (!context.CheckSiteState())
     {
         return;
     }
     try
     {
         ICmsPageGenerator cmsPage = new PageGeneratorObject(context);
         var html = cmsPage.GetTagArchive(t ?? string.Empty);
         context.HttpContext.Response.WriteAsync(html);
     }
     catch (TemplateException ex)
     {
         RenderError(context, ex, false);
     }
 }
Beispiel #3
0
        /// <summary>
        /// 呈现首页
        /// </summary>
        /// <param name="context"></param>
        public static void RenderIndex(CmsContext context)
        {
            try
            {
                ICmsPageGenerator cmsPage = new PageGeneratorObject(context);

                /*
                 * var site = context.CurrentSite;
                 * var siteId = site.SiteId;
                 * var cacheId = $"cms_s{siteId}_{(int) Cms.Context.DeviceType}_index_page";
                 * if (context.HttpContext.Request.Query("cache") == "0") Cms.Cache.Rebuilt();
                 * string html;
                 * if (Settings.Opti_IndexCacheSeconds > 0)
                 * {
                 *  ICmsCache cache = Cms.Cache;
                 *  var obj = cache.Get(cacheId);
                 *  if (obj == null)
                 *  {
                 *      html = cmsPage.GetIndex();
                 *      cache.Insert(cacheId, html, DateTime.Now.AddSeconds(Settings.Opti_IndexCacheSeconds));
                 *  }
                 *  else
                 *  {
                 *      html = obj as string;
                 *  }
                 * }
                 * else
                 * {
                 *  html = cmsPage.GetIndex();
                 * }*/

                var html = cmsPage.GetIndex();

                context.HttpContext.Response.WriteAsync(html);
            }
            catch (TemplateException ex)
            {
                RenderError(context, ex, false);
            }
        }
Beispiel #4
0
        /// <summary>
        /// 呈现搜索页
        /// </summary>
        /// <param name="context"></param>
        /// <param name="c"></param>
        /// <param name="w"></param>
        public static void RenderSearch(CmsContext context, string c, string w)
        {
            //检测网站状态
            if (!context.CheckSiteState())
            {
                return;
            }

            ICmsPageGenerator cmsPage = new PageGeneratorObject(context);

            try
            {
                context.HttpContext.Response.WriteAsync(
                    cmsPage.GetSearch(
                        c ?? string.Empty
                        , w ?? string.Empty
                        )
                    );
            }
            catch (TemplateException ex)
            {
                RenderError(context, ex, false);
            }
        }
Beispiel #5
0
        /// <summary>
        /// 呈现分类页
        /// </summary>
        /// <param name="context"></param>
        /// <param name="catPath"></param>
        /// <param name="page"></param>
        public static void RenderCategory(CmsContext context, string catPath, int page)
        {
            if (catPath.StartsWith("/"))
            {
                catPath = catPath.Substring(1);
            }
            // 去掉末尾的"/"
            if (catPath.EndsWith("/"))
            {
                catPath = catPath.Substring(0, catPath.Length - 1);
            }
            var siteId   = context.CurrentSite.SiteId;
            var category = ServiceCall.Instance.SiteService.GetCategory(siteId, catPath);

            if (!(category.ID > 0))
            {
                RenderNotFound(context);
                return;
            }

            if (!catPath.StartsWith(category.Path))
            {
                RenderNotFound(context);
                return;
            }

            ICmsPageGenerator cmsPage = new PageGeneratorObject(context);

            /*********************************
            *  @ 单页,跳到第一个特殊文档,
            *  @ 如果未设置则最新创建的文档,
            *  @ 如未添加文档则返回404
            *********************************/
            if (string.IsNullOrEmpty(category.Location))
            {
                try
                {
                    var html = cmsPage.GetCategory(category, page);
                    context.HttpContext.Response.WriteAsync(html);
                }
                catch (TemplateException ex)
                {
                    RenderError(context, ex, false);
                }
            }
            else
            {
                var url = category.Location;
                if (category.Location.IndexOf("://") != -1)
                {
                    url = category.Location;
                }
                else
                {
                    if (!category.Location.StartsWith("/"))
                    {
                        url = string.Concat(context.SiteAppPath, context.SiteAppPath == "/" ? "" : "/",
                                            category.Location);
                    }
                }

                context.HttpContext.Response.Redirect(url, false); //302
            }
        }
Beispiel #6
0
        /// <summary>
        /// 访问文档
        /// </summary>
        /// <param name="context"></param>
        /// <param name="archivePath">文档路径</param>
        public static void RenderArchive(CmsContext context, string archivePath)
        {
            if (archivePath.StartsWith("/"))
            {
                archivePath = archivePath.Substring(1);
            }
            var siteId  = context.CurrentSite.SiteId;
            var archive = ServiceCall.Instance.ArchiveService.GetArchiveByIdOrAlias(siteId, archivePath);

            if (archive.Id <= 0)
            {
                RenderNotFound(context);
                return;
            }

            // 如果设置了302跳转
            if (!string.IsNullOrEmpty(archive.Location))
            {
                string url;
                if (Regex.IsMatch(archive.Location, "^http://", RegexOptions.IgnoreCase))
                {
                    url = archive.Location;
                }
                else
                {
                    if (archive.Location.StartsWith("/"))
                    {
                        throw new Exception("URL不能以\"/\"开头!");
                    }
                    url = string.Concat(context.SiteDomain, "/", archive.Location);
                }

                context.HttpContext.Response.Redirect(url, false); //302
                return;
            }

            ICmsPageGenerator cmsPage = new PageGeneratorObject(context);

            if (!FlagAnd(archive.Flag, BuiltInArchiveFlags.Visible))
            {
                RenderNotFound(context);
                return;
            }

            var category = archive.Category;

            if (!(category.ID > 0))
            {
                RenderNotFound(context);
                return;
            }

            if (FlagAnd(archive.Flag, BuiltInArchiveFlags.AsPage))
            {
                var pagePattern = "^[\\.0-9A-Za-z_-]+\\.html$";

                if (!Regex.IsMatch(context.HttpContext.Request.GetPath(), pagePattern))
                {
                    context.HttpContext.Response.StatusCode(301);
                    context.HttpContext.Response.AppendHeader("Location",
                                                              $"{(string.IsNullOrEmpty(archive.Alias) ? archive.StrId : archive.Alias)}.html");
                    return;
                }
            }
            else
            {
                //校验栏目是否正确
                if (!archivePath.StartsWith(category.Path + "/"))
                {
                    // 如果栏目和文档路径不匹配
                    ServiceCall.Instance.ArchiveService.UpdateArchivePath(siteId, archive.Id);
                    RenderError(context, new Exception("文档路径不匹配, 已自动纠正为正确地址, 请重新打开访问"), false);
                    return;
                }
            }

            //增加浏览次数
            ++archive.ViewCount;
            ServiceCall.Instance.ArchiveService.AddCountForArchive(siteId, archive.Id, 1);
            try
            {
                //显示页面
                var html = cmsPage.GetArchive(archive);
                context.HttpContext.Response.WriteAsync(html);
            }
            catch (TemplateException ex)
            {
                RenderError(context, ex, false);
            }
        }