Exemple #1
0
        /// <summary>
        /// 获得论坛最新的20个主题的Rss描述
        /// </summary>
        /// <param name="ttl">TTL数值</param>
        /// <returns>Rss描述</returns>
        public static string GetRssXml(int ttl)
        {
            Discuz.Cache.DNTCache cache = Discuz.Cache.DNTCache.GetCacheService();

            string str = cache.RetrieveObject("/RSS/Index") as string;

            if (str != null)
            {
                return(str);
            }
            ForumInfo[]   forumlist   = Forums.GetForumList();
            UserGroupInfo guestinfo   = UserGroups.GetUserGroupInfo(7);
            StringBuilder sbforumlist = new StringBuilder();            //不允许游客访问的板块Id列表
            StringBuilder sbRss       = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n");

            foreach (ForumInfo f in forumlist)
            {
                if (f.Allowrss == 0)
                {
                    sbforumlist.AppendFormat(",{0}", f.Fid);
                }
                else
                {
                    if (f.Viewperm == null || f.Viewperm == string.Empty)
                    {
                        //板块权限设置为空,按照用户组权限走,RSS仅检查游客权限
                        if (guestinfo.Allowvisit == 0)
                        {
                            sbforumlist.AppendFormat(",{0}", f.Fid);
                        }
                    }
                    else
                    {
                        if (!Utils.InArray("7", f.Viewperm, ","))
                        {
                            sbforumlist.AppendFormat(",{0}", f.Fid);
                        }
                    }
                }
            }

            if (sbforumlist.Length > 0)
            {
                sbforumlist.Remove(0, 1);
            }
            string forumurl = "http://" + DNTRequest.GetCurrentFullHost() + ("/").ToLower();

            sbRss.Append("<?xml-stylesheet type=\"text/xsl\" href=\"rss.xsl\" media=\"screen\"?>\r\n");
            sbRss.Append("<rss version=\"2.0\">\r\n");
            sbRss.Append("  <channel>\r\n");
            sbRss.Append("    <title>");
            sbRss.Append(Utils.HtmlEncode(config.Forumtitle));
            sbRss.Append("</title>\r\n");
            sbRss.Append("    <link>");
            sbRss.Append(forumurl);
            sbRss.Append("</link>\r\n");
            sbRss.Append("    <description>Latest 20 threads</description>\r\n");
            sbRss.Append("    <copyright>Copyright (c) ");
            sbRss.Append(Utils.HtmlEncode(config.Forumtitle));
            sbRss.Append("</copyright>\r\n");
            sbRss.Append("    <generator>");
            sbRss.Append("Discuz!NT");
            sbRss.Append("</generator>\r\n");
            sbRss.Append("    <pubDate>");
            //sbRss.Append(DateTime.Now.ToUniversalTime().ToString("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'"));
            sbRss.Append(DateTime.Now.ToString("r"));
            sbRss.Append("</pubDate>\r\n");
            sbRss.AppendFormat("    <ttl>{0}</ttl>\r\n", ttl.ToString());


            //声明新的缓存策略接口
            Discuz.Cache.ICacheStrategy ics = new RssCacheStrategy();
            ics.TimeOut = ttl;
            cache.LoadCacheStrategy(ics);

            IDataReader reader = DatabaseProvider.GetInstance().GetNewTopics(sbforumlist.ToString());

            if (reader != null)
            {
                while (reader.Read())
                {
                    sbRss.Append("    <item>\r\n");
                    sbRss.Append("      <title>");
                    sbRss.Append(Utils.HtmlEncode(reader["title"].ToString().Trim()));
                    sbRss.Append("</title>\r\n");
                    sbRss.Append("    <description><![CDATA[");
                    if (reader["message"].ToString().IndexOf("[hide]") > -1)
                    {
                        sbRss.Append("***内容隐藏***");
                    }
                    else
                    {
                        sbRss.Append(Utils.HtmlEncode(Utils.GetSubString(UBB.ClearUBB(reader["message"].ToString()), 200, "......")));
                    }
                    sbRss.Append("]]></description>\r\n");
                    sbRss.Append("      <link>");
                    sbRss.Append(Utils.HtmlEncode(forumurl));

                    if (config.Aspxrewrite == 1)
                    {
                        sbRss.Append("showtopic-");
                        sbRss.Append(reader["tid"].ToString());
                        sbRss.Append(config.Extname);
                    }
                    else
                    {
                        sbRss.Append("showtopic.aspx?topicid=");
                        sbRss.Append(reader["tid"].ToString());
                    }

                    sbRss.Append("</link>\r\n");
                    sbRss.Append("      <category>");
                    sbRss.Append(Utils.HtmlEncode(reader["name"].ToString().Trim()));
                    sbRss.Append("</category>\r\n");
                    sbRss.Append("      <author>");
                    sbRss.Append(Utils.HtmlEncode(reader["poster"].ToString().Trim()));
                    sbRss.Append("</author>\r\n");
                    sbRss.Append("      <pubDate>");
                    sbRss.Append(Utils.HtmlEncode(Convert.ToDateTime(reader["postdatetime"]).ToString("r").Trim()));
                    sbRss.Append("</pubDate>\r\n");
                    sbRss.Append("    </item>\r\n");
                }
                reader.Close();
            }
            else
            {
                sbRss.Length = 0;
                sbRss.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
                sbRss.Append("<Rss>Error</Rss>\r\n");
                cache.AddObject("/RSS/Index", sbRss.ToString());
                cache.LoadDefaultCacheStrategy();
                return(sbRss.ToString());
            }


            sbRss.Append("  </channel>\r\n");
            sbRss.Append("</rss>");

            cache.AddObject("/RSS/Index", sbRss.ToString());
            cache.LoadDefaultCacheStrategy();

            return(sbRss.ToString());
        }
Exemple #2
0
        /// <summary>
        /// 获得指定版块最新的20个主题的Rss描述
        /// </summary>
        /// <param name="ttl">TTL数值</param>
        /// <param name="fid">版块id</param>
        /// <returns>Rss描述</returns>
        public static string GetForumRssXml(int ttl, int fid)
        {
            Discuz.Cache.DNTCache cache = Discuz.Cache.DNTCache.GetCacheService();

            string str = cache.RetrieveObject("/RSS/Forum" + fid) as string;

            if (str != null)
            {
                return(str);
            }

            string forumurl = "http://" + DNTRequest.GetCurrentFullHost() + ("/").ToLower();//GeneralConfigs.GetConfig().Forumurl;
            //if (!forumurl.EndsWith("/"))
            //{
            //    forumurl = forumurl + "/";
            //}
            ForumInfo forum = Forums.GetForumInfo(fid);

            if (forum == null)
            {
                return("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<Rss>Specified forum not found</Rss>\r\n");
            }

            if (forum.Viewperm == null || forum.Viewperm == string.Empty)
            {
                //板块权限设置为空,按照用户组权限走,RSS仅检查游客权限
                UserGroupInfo guestinfo = UserGroups.GetUserGroupInfo(7);
                if (guestinfo.Allowvisit == 0)
                {
                    return("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<Rss>Guest Denied</Rss>\r\n");
                }
            }
            else
            {
                if (!Utils.InArray("7", forum.Viewperm, ","))
                {
                    return("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<Rss>Guest Denied</Rss>\r\n");
                }
            }
            StringBuilder sbRss = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n");

            sbRss.Append("<?xml-stylesheet type=\"text/xsl\" href=\"rss.xsl\" media=\"screen\"?>\r\n");
            sbRss.Append("<rss version=\"2.0\">\r\n");
            sbRss.Append("  <channel>\r\n");
            sbRss.Append("    <title>");
            sbRss.Append(Utils.HtmlEncode(config.Forumtitle));
            sbRss.Append(" - ");
            sbRss.Append(Utils.HtmlEncode(forum.Name));
            sbRss.Append("</title>\r\n");
            sbRss.Append("    <link>");
            sbRss.Append(forumurl);

            if (config.Aspxrewrite == 1)
            {
                sbRss.Append("showforum-");
                sbRss.Append(fid.ToString());
                sbRss.Append(config.Extname);
            }
            else
            {
                sbRss.Append("showforum.aspx?forumid=");
                sbRss.Append(fid.ToString());
            }

            sbRss.Append("</link>\r\n");
            sbRss.Append("    <description>Latest 20 threads</description>\r\n");
            sbRss.Append("    <copyright>Copyright (c) ");
            sbRss.Append(Utils.HtmlEncode(config.Forumtitle));
            sbRss.Append("</copyright>\r\n");
            sbRss.Append("    <generator>");
            sbRss.Append("Discuz!NT");
            sbRss.Append("</generator>\r\n");
            sbRss.Append("    <pubDate>");
            //sbRss.Append(DateTime.Now.ToUniversalTime().ToString("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'"));
            sbRss.Append(DateTime.Now.ToString("r"));
            sbRss.Append("</pubDate>\r\n");
            sbRss.AppendFormat("    <ttl>{0}</ttl>\r\n", ttl.ToString());

            IDataReader reader = DatabaseProvider.GetInstance().GetForumNewTopics(fid);

            if (reader != null)
            {
                while (reader.Read())
                {
                    sbRss.Append("    <item>\r\n");
                    sbRss.Append("      <title>");
                    sbRss.Append(Utils.HtmlEncode(reader["title"].ToString().Trim()));
                    sbRss.Append("</title>\r\n");
                    sbRss.Append("      <description><![CDATA[");
                    if (reader["message"].ToString().IndexOf("[hide]") > -1)
                    {
                        sbRss.Append("***内容隐藏***");
                    }
                    else
                    {
                        sbRss.Append(Utils.HtmlEncode(Utils.GetSubString(UBB.ClearUBB(reader["message"].ToString()), 200, "......")));
                    }
                    sbRss.Append("]]></description>\r\n");
                    sbRss.Append("      <link>");
                    sbRss.Append(Utils.HtmlEncode(forumurl));

                    if (config.Aspxrewrite == 1)
                    {
                        sbRss.Append("showtopic-");
                        sbRss.Append(reader["tid"].ToString());
                        sbRss.Append(config.Extname);
                    }
                    else
                    {
                        sbRss.Append("showtopic.aspx?topicid=");
                        sbRss.Append(reader["tid"].ToString());
                    }

                    sbRss.Append("</link>\r\n");
                    sbRss.Append("      <category>");
                    sbRss.Append(Utils.HtmlEncode(forum.Name));
                    sbRss.Append("</category>\r\n");
                    sbRss.Append("      <author>");
                    sbRss.Append(Utils.HtmlEncode(reader["poster"].ToString().Trim()));
                    sbRss.Append("</author>\r\n");
                    sbRss.Append("      <pubDate>");
                    sbRss.Append(Utils.HtmlEncode(Convert.ToDateTime(reader["postdatetime"]).ToString("r").Trim()));
                    sbRss.Append("</pubDate>\r\n");
                    sbRss.Append("    </item>\r\n");
                }
                reader.Close();
            }
            sbRss.Append("  </channel>\r\n");
            sbRss.Append("</rss>");


            Discuz.Cache.ICacheStrategy ics = new RssCacheStrategy();
            ics.TimeOut = ttl;
            cache.LoadCacheStrategy(ics);
            cache.AddObject("/RSS/Forum" + fid, sbRss.ToString());
            cache.LoadDefaultCacheStrategy();

            return(sbRss.ToString());
        }