Esempio n. 1
0
        /// <summary>
        /// metaWeblog.newPost method
        /// </summary>
        /// <param name="blogId">
        /// always 1000 in BlogEngine since it is a singlar blog instance
        /// </param>
        /// <param name="userName">
        /// login username
        /// </param>
        /// <param name="password">
        /// login password
        /// </param>
        /// <param name="sentPost">
        /// struct with post details
        /// </param>
        /// <param name="publish">
        /// mark as published?
        /// </param>
        /// <returns>
        /// postID as string
        /// </returns>
        internal string NewPost(string blogId, string userName, string password, MWAPost sentPost, bool publish)
        {
            int moduleId = Convert.ToInt32(blogId);

            if (!UserCanPostToBlog(userName, moduleId))
            {
                throw new MetaWeblogException("11", MetaweblogResources.AccessDenied);
            }

            try
            {
                Module module = new Module(moduleId);

                Blog post = new Blog();
                post.ModuleId = module.ModuleId;
                post.ModuleGuid = module.ModuleGuid;

                post.UserGuid = siteUser.UserGuid;
                post.LastModUserGuid = siteUser.UserGuid;

                if ((sentPost.postDate != null) && (sentPost.postDate > DateTime.MinValue) && (sentPost.postDate < DateTime.MaxValue))
                {
                    if (!WebConfigSettings.DisableUseOfPassedInDateForMetaWeblogApi) { post.StartDate = sentPost.postDate; }
                }

                post.Title = sentPost.title;
                post.Description = SiteUtils.ChangeFullyQualifiedLocalUrlsToRelative(navigationSiteRoot, imageSiteRoot, sentPost.description);

                if (!string.IsNullOrEmpty(sentPost.excerpt))
                {
                    if (BlogConfiguration.UseExcerptFromMetawblogAsMetaDescription)
                    {
                        post.MetaDescription = UIHelper.CreateExcerpt(sentPost.excerpt, BlogConfiguration.MetaDescriptionMaxLengthToGenerate);
                    }
                    post.Excerpt = SiteUtils.ChangeFullyQualifiedLocalUrlsToRelative(navigationSiteRoot, imageSiteRoot, sentPost.excerpt);
                }
                post.IncludeInFeed = true;
                post.IsPublished = publish;

                //post.Slug = sentPost.slug;
                //string author = String.IsNullOrEmpty(sentPost.author) ? userName : sentPost.author;

                switch (sentPost.commentPolicy)
                {
                    //closed
                    case "2":
                        post.AllowCommentsForDays = -1; // closed
                        break;

                    // open
                    case "1":
                    default:

                        Hashtable moduleSettings = ModuleSettings.GetModuleSettings(post.ModuleId);
                        BlogConfiguration config = new BlogConfiguration(moduleSettings);
                        post.AllowCommentsForDays = config.DefaultCommentDaysAllowed;

                        break;

                }

                string newUrl = SiteUtils.SuggestFriendlyUrl(post.Title, siteSettings);

                post.ItemUrl = "~/" + newUrl;

                if (!post.Title.Contains("Theme Detection")) // don't index a temp post from livewriter that will be deleted
                {
                    post.ContentChanged += new ContentChangedEventHandler(blog_ContentChanged);
                }

                post.Save();

                int pageId = GetPageIdForModule(moduleId);

                FriendlyUrl newFriendlyUrl = new FriendlyUrl();
                newFriendlyUrl.SiteId = siteSettings.SiteId;
                newFriendlyUrl.SiteGuid = siteSettings.SiteGuid;
                newFriendlyUrl.PageGuid = post.BlogGuid;
                newFriendlyUrl.Url = newUrl;
                newFriendlyUrl.RealUrl = "~/Blog/ViewPost.aspx?pageid="
                    + pageId.ToInvariantString()
                    + "&mid=" + post.ModuleId.ToInvariantString()
                    + "&ItemID=" + post.ItemId.ToInvariantString();

                if (pageId > -1)
                {
                    newFriendlyUrl.Save();
                }

                SetCategories(post, sentPost);

                //post.Tags.Clear();
                //foreach (var item in sentPost.tags.Where(item => item != null && item.Trim() != string.Empty))
                //{
                //    post.Tags.Add(item);
                //}

                if (!post.Title.Contains("Theme Detection")) // don't index a temp post from livewriter that will be deleted
                {
                    SiteUtils.QueueIndexing();
                }

                return post.ItemId.ToString();

            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw new MetaWeblogException("12", string.Format("Create new post failed.  Error: {0}", ex.Message));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// metaWeblog.editPost method
        /// </summary>
        /// <param name="postId">
        /// post guid in string format
        /// </param>
        /// <param name="userName">
        /// login username
        /// </param>
        /// <param name="password">
        /// login password
        /// </param>
        /// <param name="sentPost">
        /// struct with post details
        /// </param>
        /// <param name="publish">
        /// mark as published?
        /// </param>
        /// <returns>
        /// 1 if successful
        /// </returns>
        internal bool EditPost(string postId, string userName, string password, MWAPost sentPost, bool publish)
        {
            int blogid = Convert.ToInt32(postId);

            if (!UserCanEditPost(userName, blogid))
            {
                throw new MetaWeblogException("11", MetaweblogResources.AccessDenied);
            }

            string author = String.IsNullOrEmpty(sentPost.author) ? userName : sentPost.author;

            Blog post = new Blog(blogid);

            if (post.ItemId == -1)
            {
                //not found
                throw new MetaWeblogException("11", MetaweblogResources.PostNotFound);
            }

            try
            {
                Hashtable moduleSettings = ModuleSettings.GetModuleSettings(post.ModuleId);
                BlogConfiguration config = new BlogConfiguration(moduleSettings);

                post.LastModUserGuid = siteUser.UserGuid;
                post.Title = sentPost.title;
                post.Description = SiteUtils.ChangeFullyQualifiedLocalUrlsToRelative(navigationSiteRoot, imageSiteRoot, sentPost.description);
                post.IsPublished = publish;
                //post.Slug = sentPost.slug;

                if (!string.IsNullOrEmpty(sentPost.excerpt))
                {
                    if (BlogConfiguration.UseExcerptFromMetawblogAsMetaDescription)
                    {
                        post.MetaDescription = UIHelper.CreateExcerpt(sentPost.excerpt, BlogConfiguration.MetaDescriptionMaxLengthToGenerate);
                    }
                    post.Excerpt = SiteUtils.ChangeFullyQualifiedLocalUrlsToRelative(navigationSiteRoot, imageSiteRoot, sentPost.excerpt);
                }

                switch (sentPost.commentPolicy)
                {
                    //closed
                    case "2":
                        post.AllowCommentsForDays = -1; // closed
                        break;
                    // open
                    case "1":
                        // if the post was previously closed to comments
                        // re-open it using the default allowed days
                        if (post.AllowCommentsForDays < 0)
                        {
                            post.AllowCommentsForDays = config.DefaultCommentDaysAllowed;
                        }

                        break;
                    //else unspecified, no change
                }

                post.ContentChanged += new ContentChangedEventHandler(blog_ContentChanged);

                bool enableContentVersioning = config.EnableContentVersioning;

                if ((siteSettings.ForceContentVersioning) || (WebConfigSettings.EnforceContentVersioningGlobally))
                {
                    enableContentVersioning = true;
                }

                if (enableContentVersioning)
                {
                    post.CreateHistory(siteSettings.SiteGuid);
                }

                post.Save();

                SetCategories(post, sentPost);

                SiteUtils.QueueIndexing();

                return true;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw new MetaWeblogException("12", string.Format("EditPost failed.  Error: {0}", ex.Message));
            }
        }
        public void InstallContent(Module module, string configInfo)
        {
            if (string.IsNullOrEmpty(configInfo)) { return; }

            SiteSettings siteSettings = new SiteSettings(module.SiteId);
            SiteUser admin = SiteUser.GetNewestUser(siteSettings);

            XmlDocument xml = new XmlDocument();

            using (StreamReader stream = File.OpenText(HostingEnvironment.MapPath(configInfo)))
            {
                xml.LoadXml(stream.ReadToEnd());
            }

            XmlNode postsNode = null;
            foreach (XmlNode n in xml.DocumentElement.ChildNodes)
            {
                if (n.Name == "posts")
                {
                    postsNode = n;
                    break;
                }
            }

            if (postsNode != null)
            {
                foreach (XmlNode node in postsNode.ChildNodes)
                {
                    if (node.Name == "post")
                    {
                        XmlAttributeCollection postAttrributes = node.Attributes;

                        Blog b = new Blog();
                        b.ModuleGuid = module.ModuleGuid;
                        b.ModuleId = module.ModuleId;

                        if ((postAttrributes["title"] != null) && (postAttrributes["title"].Value.Length > 0))
                        {
                            b.Title = postAttrributes["title"].Value;
                        }

                        b.ItemUrl = "~/" + SiteUtils.SuggestFriendlyUrl(b.Title, siteSettings);

                        foreach (XmlNode descriptionNode in node.ChildNodes)
                        {
                            if (descriptionNode.Name == "excerpt")
                            {
                                b.Excerpt = descriptionNode.InnerText;
                                break;
                            }
                        }

                        foreach (XmlNode descriptionNode in node.ChildNodes)
                        {
                            if (descriptionNode.Name == "description")
                            {
                                b.Description = descriptionNode.InnerText;
                                break;
                            }
                        }

                        b.UserGuid = admin.UserGuid;

                        if (b.Save())
                        {
                            FriendlyUrl newFriendlyUrl = new FriendlyUrl();
                            newFriendlyUrl.SiteId = siteSettings.SiteId;
                            newFriendlyUrl.SiteGuid = siteSettings.SiteGuid;
                            newFriendlyUrl.PageGuid = b.BlogGuid;
                            newFriendlyUrl.Url = b.ItemUrl.Replace("~/", string.Empty); ;
                            newFriendlyUrl.RealUrl = "~/Blog/ViewPost.aspx?pageid="
                                + module.PageId.ToInvariantString()
                                + "&mid=" + b.ModuleId.ToInvariantString()
                                + "&ItemID=" + b.ItemId.ToInvariantString();

                            newFriendlyUrl.Save();

                        }

                    }
                }
            }

            foreach (XmlNode node in xml.DocumentElement.ChildNodes)
            {
                if (node.Name == "moduleSetting")
                {
                    XmlAttributeCollection settingAttributes = node.Attributes;

                    if ((settingAttributes["settingKey"] != null) && (settingAttributes["settingKey"].Value.Length > 0))
                    {
                        string key = settingAttributes["settingKey"].Value;
                        string val = string.Empty;
                        if (settingAttributes["settingValue"] != null)
                        {
                            val = settingAttributes["settingValue"].Value;
                        }

                        ModuleSettings.UpdateModuleSetting(module.ModuleGuid, module.ModuleId, key, val);
                    }
                }
            }
        }