Пример #1
0
 protected void WriteChannel(XmlTextWriter writer, string tag, SiteSettings settings)
 {
     writer.WriteElementString("title", string.Concat(settings.Title, ": ", tag));
     writer.WriteElementString("link", new Urls().Tags + tag.ToLower());
     writer.WriteElementString("description", "Posts tagged with " + tag);
     writer.WriteElementString("generator", SiteSettings.VersionDescription);
 }
Пример #2
0
        protected void WriteItem(XmlTextWriter writer, Post p, SiteSettings settings, string baseUrl)
        {
            string link = macros.FullUrl(p.Url);
            writer.WriteElementString("title", HttpUtility.HtmlDecode(p.Title));
            writer.WriteElementString("link", link);
            writer.WriteElementString("pubDate", p.Published.AddHours(-1 * settings.TimeZoneOffSet).ToUniversalTime().ToString("r"));

            writer.WriteStartElement("guid");
            writer.WriteAttributeString("isPermaLink", "true");
            writer.WriteString(link);
            writer.WriteEndElement();

            if (!string.IsNullOrEmpty(p.UserName) && !string.IsNullOrEmpty(p.User.ProperName))
                writer.WriteElementString("dc:creator", p.User.ProperName);

            if (p.EnableComments)
            {
                writer.WriteElementString("slash:comments", p.CommentCount.ToString());
            }

            writer.WriteStartElement("category");
            writer.WriteAttributeString("domain", macros.FullUrl(p.Category.Url));
            writer.WriteString(p.Category.Name);
            writer.WriteEndElement();

            writer.WriteElementString("description", Util.FullyQualifyRelativeUrls(p.RenderBody(PostRenderLocation.Feed), baseUrl));
        }
Пример #3
0
 protected void WriteChannel(XmlTextWriter writer, Category category, SiteSettings settings)
 {
     writer.WriteElementString("title", category == null ? settings.Title : string.Concat(settings.Title, ": ", category.Name));
     writer.WriteElementString("link", macros.FullUrl(category == null ? new Urls().Home : category.Url));
     writer.WriteElementString("description", category == null ? settings.TagLine : Util.Truncate(category.Body, 250));
     writer.WriteElementString("generator", SiteSettings.VersionDescription);
     writer.WriteElementString("lastBuildDate", lastModified.AddHours(-1 * settings.TimeZoneOffSet).ToUniversalTime().ToString("r"));
 }
Пример #4
0
 protected void ValidateAndSetHeaders(PostCollection pc, SiteSettings settings, HttpContext context)
 {
     if (pc.Count > 0) {
         string lastMod = context.Request.Headers["If-Modified-Since"];
         if (lastMod != null) {
             if (lastMod == pc[0].Published.AddHours(-1 * settings.TimeZoneOffSet).ToUniversalTime().ToString("r")) {
                 context.Response.StatusCode = 304;
                 context.Response.Status = "304 Not Modified";
                 context.Response.End();
             }
         }
         DateTime lastModified = pc[0].Published.AddHours(-1 * settings.TimeZoneOffSet);
         context.Response.Clear();
         context.Response.Cache.SetCacheability(HttpCacheability.Public);
         context.Response.Cache.SetLastModified(lastModified);
         context.Response.Cache.SetETag(lastModified.ToString());
     }
 }
Пример #5
0
        public bool editPost(string postid, string username, string password, Post post, bool publish)
        {
            if (ValidateUser(username, password))
            {
                Core.Post     wp   = new Core.Post(postid);
                IGraffitiUser user = GraffitiUsers.Current;

                if (post.categories != null && post.categories.Length > 0)
                {
                    wp.CategoryId = AddOrFetchCategory(post.categories[0], user).Id;
                }

                wp.Name = post.wp_slug ?? wp.Name;

                if (!string.IsNullOrEmpty(post.mt_text_more))
                {
                    wp.ExtendedBody = post.mt_text_more;
                }
                else
                {
                    wp.ExtendedBody = null;
                }
                wp.PostBody = post.description;

                wp.Title       = post.title;
                wp.PostStatus  = (publish ? PostStatus.Publish : PostStatus.Draft);
                wp.IsPublished = publish;
                wp.TagList     = post.GetTagList() ?? wp.TagList;

                try
                {
                    if (post.dateCreated != DateTime.MinValue)
                    {
                        DateTime dtUTC   = post.dateCreated;
                        DateTime dtLocal = dtUTC.ToLocalTime();
                        wp.Published = dtLocal.AddHours(SiteSettings.Get().TimeZoneOffSet);
                        //wp.Published = post.dateCreated;
                    }
                }
                catch
                {
                }

                try
                {
                    PostRevisionManager.CommitPost(wp, user, SiteSettings.Get().FeaturedId == wp.Id, wp.Category.FeaturedId == wp.Id);
                    return(true);
                }
                catch (Exception ex)
                {
                    if (ex.Message.IndexOf("UNIQUE") > -1)
                    {
                        throw new XmlRpcFaultException(2,
                                                       "Sorry, but the name of this post is not unqiue and the post was not saved");
                    }

                    else
                    {
                        Log.Error("MetaBlog Error", "An error occored editing the post {0}. Exception: {1} Stack: {2}", post.postid,
                                  ex.Message, ex.StackTrace);
                        throw;
                    }
                }
            }

            throw new XmlRpcFaultException(0, "User does not exist");
        }
Пример #6
0
        public string newPost(string blogid, string username, string password, Post post, bool publish)
        {
            if (ValidateUser(username, password))
            {
                IGraffitiUser user      = GraffitiUsers.Current;
                Core.Post     postToAdd = new Core.Post();
                postToAdd.ContentType = "text/html";

                postToAdd.PostStatus     = (publish ? PostStatus.Publish : PostStatus.Draft);
                postToAdd.IsPublished    = publish;
                postToAdd.PostBody       = post.description;
                postToAdd.Title          = post.title;
                postToAdd.TagList        = post.GetTagList();
                postToAdd.UserName       = username;
                postToAdd.EnableComments = CommentSettings.Get().EnableCommentsDefault;

                if (post.categories != null && post.categories.Length > 0)
                {
                    postToAdd.CategoryId = AddOrFetchCategory(post.categories[0], user).Id;
                }
                else
                {
                    postToAdd.CategoryId = CategoryController.UnCategorizedId;
                }

                postToAdd.Name = post.GetSlug();

                if (!string.IsNullOrEmpty(post.mt_text_more))
                {
                    postToAdd.ExtendedBody = post.mt_text_more;
                }


                // Get UserTime safely (some clients pass in a DateTime that is not valid)
                try
                {
                    if (post.dateCreated != DateTime.MinValue)
                    {
                        DateTime dtUTC   = post.dateCreated;
                        DateTime dtLocal = dtUTC.ToLocalTime();
                        postToAdd.Published = dtLocal.AddHours(SiteSettings.Get().TimeZoneOffSet);
                    }
                }
                catch
                {
                    postToAdd.Published = DateTime.Now.AddHours(SiteSettings.Get().TimeZoneOffSet);
                }

                if (postToAdd.Published <= new DateTime(2000, 1, 1))
                {
                    postToAdd.Published = DateTime.Now.AddHours(SiteSettings.Get().TimeZoneOffSet);
                }

                try
                {
                    return(PostRevisionManager.CommitPost(postToAdd, user, false, false).ToString());
                }
                catch (Exception ex)
                {
                    if (ex.Message.IndexOf("UNIQUE") > -1)
                    {
                        throw new XmlRpcFaultException(2, "Duplicate Post Name");
                    }

                    else
                    {
                        Log.Error("MetaBlog Error", "An error occored editing the post {0}. Exception: {1} Stack: {2}", post.postid,
                                  ex.Message, ex.StackTrace);
                        throw;
                    }
                }
            }


            throw new XmlRpcFaultException(0, "User does not exist");
        }
Пример #7
0
        protected override void OnLoad(EventArgs e)
        {
            Initialize();

            SiteSettings settings = SiteSettings.Get();

            string baseUrl = SiteSettings.BaseUrl;

            if (string.IsNullOrEmpty(TagName))
            {
                Category category = null;
                if (CategoryID > -1)
                {
                    category = new CategoryController().GetCachedCategory(CategoryID, false);
                }

                if (category == null)
                {
                    if (!string.IsNullOrEmpty(settings.ExternalFeedUrl) &&
                        Request.UserAgent.IndexOf("FeedBurner", StringComparison.InvariantCultureIgnoreCase) == -1)
                    {
                        Context.Response.RedirectLocation = settings.ExternalFeedUrl;
                        Context.Response.StatusCode       = 301;
                        Context.Response.End();
                    }
                }
                else if (!string.IsNullOrEmpty(category.FeedUrlOverride) &&
                         Request.UserAgent.IndexOf("FeedBurner", StringComparison.InvariantCultureIgnoreCase) == -1)
                {
                    Context.Response.RedirectLocation = category.FeedUrlOverride;
                    Context.Response.StatusCode       = 301;
                    Context.Response.End();
                }
                else if (CategoryName != null && !Util.AreEqualIgnoreCase(CategoryName, category.LinkName))
                {
                    Context.Response.RedirectLocation = new Uri(Context.Request.Url, category.Url).ToString();
                    Context.Response.StatusCode       = 301;
                    Context.Response.End();
                }

                string cacheKey = CategoryID > -1
                                      ? "Posts-Index-" + Util.PageSize + "-" + CategoryID.ToString()
                                      : string.Format("Posts-Categories-P:{0}-C:{1}-T:{2}-PS:{3}", 1, CategoryID,
                                                      SortOrderType.Descending, Util.PageSize);

                PostCollection pc = ZCache.Get <PostCollection>(cacheKey);

                if (pc == null)
                {
                    Query q = PostCollection.DefaultQuery();
                    q.Top = Util.PageSize.ToString();

                    if (SiteSettings.Get().IncludeChildPosts&& macros.IsNotNull(category))
                    {
                        if (category.ParentId > 0)
                        {
                            q.AndWhere(Post.Columns.CategoryId, CategoryID);
                        }
                        else
                        {
                            List <int> ids = new List <int>(category.Children.Count + 1);
                            foreach (Category child in category.Children)
                            {
                                ids.Add(child.Id);
                            }

                            ids.Add(category.Id);

                            q.AndInWhere(Post.Columns.CategoryId, ids.ToArray());
                        }
                    }
                    else
                    {
                        if (CategoryID > 0)
                        {
                            q.AndWhere(Post.Columns.CategoryId, CategoryID);
                        }
                    }

                    pc = new PostCollection();
                    pc.LoadAndCloseReader(q.ExecuteReader());

                    PostCollection permissionsFiltered = new PostCollection();

                    permissionsFiltered.AddRange(pc);

                    foreach (Post p in pc)
                    {
                        if (!RolePermissionManager.GetPermissions(p.CategoryId, GraffitiUsers.Current).Read)
                        {
                            permissionsFiltered.Remove(p);
                        }
                    }

                    ZCache.InsertCache(cacheKey, permissionsFiltered, 90);
                    pc = permissionsFiltered;
                }

                ValidateAndSetHeaders(pc, settings, Context);

                StringWriter sw = new StringWriter();
                sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
                XmlTextWriter writer = new XmlTextWriter(sw);

                writer.WriteStartElement("rss");
                writer.WriteAttributeString("version", "2.0");
                writer.WriteAttributeString("xmlns:dc", "http://purl.org/dc/elements/1.1/");
                writer.WriteAttributeString("xmlns:slash", "http://purl.org/rss/1.0/modules/slash/");

                // Allow plugins to add additional xml namespaces
                Core.Events.Instance().ExecuteRssNamespace(writer);

                writer.WriteStartElement("channel");
                WriteChannel(writer, category, settings);

                // Allow plugins to add additional xml to the <channel>
                Core.Events.Instance().ExecuteRssChannel(writer);

                foreach (Post p in pc)
                {
                    writer.WriteStartElement("item");
                    WriteItem(writer, p, settings, baseUrl);

                    // Allow plugins to add additional xml to the <item>
                    Core.Events.Instance().ExecuteRssItem(writer, p);

                    writer.WriteEndElement(); // End Item
                }

                writer.WriteEndElement(); // End Channel
                writer.WriteEndElement(); // End Document

                // save XML into response
                Context.Response.ContentEncoding = System.Text.Encoding.UTF8;
                Context.Response.ContentType     = "application/rss+xml";
                Context.Response.Write(sw.ToString());
            }
            else
            {
                PostCollection pc = GetTaggedPosts(TagName);

                ValidateAndSetHeaders(pc, settings, Context);

                StringWriter sw = new StringWriter();
                sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
                XmlTextWriter writer = new XmlTextWriter(sw);

                writer.WriteStartElement("rss");
                writer.WriteAttributeString("version", "2.0");
                writer.WriteAttributeString("xmlns:dc", "http://purl.org/dc/elements/1.1/");
                writer.WriteAttributeString("xmlns:slash", "http://purl.org/rss/1.0/modules/slash/");

                Core.Events.Instance().ExecuteRssNamespace(writer);

                writer.WriteStartElement("channel");
                WriteChannel(writer, TagName, settings);

                // Allow plugins to add additional xml to the <channel>
                Core.Events.Instance().ExecuteRssChannel(writer);

                foreach (Post p in pc)
                {
                    writer.WriteStartElement("item");
                    WriteItem(writer, p, settings, baseUrl);

                    Core.Events.Instance().ExecuteRssItem(writer, p);

                    writer.WriteEndElement(); // End Item
                }

                writer.WriteEndElement(); // End Channel
                writer.WriteEndElement(); // End Document

                Context.Response.ContentEncoding = Encoding.UTF8;
                Context.Response.ContentType     = "application/rss+xml";
                Context.Response.Write(sw.ToString());
            }
        }