/// <summary> /// metaWeblog.getRecentPosts 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="numberOfPosts"> /// number of posts to return /// </param> /// <returns> /// array of post structs /// </returns> internal List <MWAPost> GetRecentPosts(string blogId, string userName, string password, int numberOfPosts) { var sendPosts = new List <MWAPost>(); var posts = Post.Posts.Where(p => p.IsVisible).ToList(); // Set End Point var stop = numberOfPosts; if (stop > posts.Count) { stop = posts.Count; } foreach (var post in posts.GetRange(0, stop)) { var tempPost = new MWAPost { postID = post.Id.ToString(), postDate = post.DateCreated, title = post.Title, description = post.Content, link = post.AbsoluteLink.AbsoluteUri, slug = post.Slug, excerpt = post.Description, commentPolicy = post.HasCommentsEnabled ? string.Empty : "0", publish = post.IsPublished }; var tempCats = post.Categories.Select(t => Category.GetCategory(t.Id).ToString()).ToList(); tempPost.categories = tempCats; var tempTags = post.Tags.ToList(); tempPost.tags = tempTags; sendPosts.Add(tempPost); } return(sendPosts); }
/// <summary> /// metaWeblog.getPost method /// </summary> /// <param name="postId"> /// post guid in string format /// </param> /// <param name="userName"> /// login username /// </param> /// <param name="password"> /// login password /// </param> /// <returns> /// struct with post details /// </returns> internal MWAPost GetPost(string postId, string userName, string password) { var sendPost = new MWAPost(); var post = Post.GetPost(new Guid(postId)); if (!post.IsVisible) { throw new MetaWeblogException("11", "User authentication failed"); } sendPost.postID = post.Id.ToString(); sendPost.postDate = post.DateCreated; sendPost.title = post.Title; sendPost.description = post.Content; sendPost.link = post.AbsoluteLink.AbsoluteUri; sendPost.slug = post.Slug; sendPost.excerpt = post.Description; sendPost.commentPolicy = post.HasCommentsEnabled ? "1" : "0"; sendPost.publish = post.IsPublished; var cats = post.Categories.Select(t => Category.GetCategory(t.Id).ToString()).ToList(); sendPost.categories = cats; var tags = post.Tags.ToList(); sendPost.tags = tags; return sendPost; }
/// <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) { var post = Post.GetPost(new Guid(postId)); if (!post.CanUserEdit) { throw new MetaWeblogException("11", "User authentication failed"); } string author = String.IsNullOrEmpty(sentPost.author) ? userName : sentPost.author; if (!post.IsPublished && publish) { if (!post.CanPublish(author)) { throw new MetaWeblogException("11", "Not authorized to publish this Post."); } } post.Author = author; post.Title = sentPost.title; post.Content = sentPost.description; post.IsPublished = publish; post.Slug = sentPost.slug; post.Description = sentPost.excerpt; if (sentPost.commentPolicy != string.Empty) { post.HasCommentsEnabled = sentPost.commentPolicy == "1"; } post.Categories.Clear(); foreach (var item in sentPost.categories.Where(c => c != null && c.Trim() != string.Empty)) { Category cat; if (LookupCategoryGuidByName(item, out cat)) { post.Categories.Add(cat); } else { // Allowing new categories to be added. (This breaks spec, but is supported via WLW) using (var newcat = new Category(item, string.Empty)) { newcat.Save(); post.Categories.Add(newcat); } } } post.Tags.Clear(); foreach (var item in sentPost.tags.Where(item => item != null && item.Trim() != string.Empty)) { post.Tags.Add(item); } if (sentPost.postDate != new DateTime()) { post.DateCreated = sentPost.postDate.AddHours(-BlogSettings.Instance.Timezone); } post.Save(); return true; }
/// <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) { if (!Security.IsAuthorizedTo(Rights.CreateNewPosts)) { throw new MetaWeblogException("11", "User authentication failed"); } string author = String.IsNullOrEmpty(sentPost.author) ? userName : sentPost.author; var post = new Post { Author = author, Title = sentPost.title, Content = sentPost.description, IsPublished = publish, Slug = sentPost.slug, Description = sentPost.excerpt }; if (publish) { if (!post.CanPublish(author)) { throw new MetaWeblogException("11", "Not authorized to publish this Post."); } } if (sentPost.commentPolicy != string.Empty) { post.HasCommentsEnabled = sentPost.commentPolicy == "1"; } post.Categories.Clear(); foreach (var item in sentPost.categories) { Category cat; if (LookupCategoryGuidByName(item, out cat)) { post.Categories.Add(cat); } else { // Allowing new categories to be added. (This breaks spec, but is supported via WLW) var newcat = new Category(item, string.Empty); newcat.Save(); post.Categories.Add(newcat); } } post.Tags.Clear(); foreach (var item in sentPost.tags.Where(item => item != null && item.Trim() != string.Empty)) { post.Tags.Add(item); } if (sentPost.postDate != new DateTime()) { post.DateCreated = sentPost.postDate; } post.Save(); return(post.Id.ToString()); }
/// <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) { var post = Post.GetPost(new Guid(postId)); if (!post.CanUserEdit) { throw new MetaWeblogException("11", "User authentication failed"); } string author = String.IsNullOrEmpty(sentPost.author) ? userName : sentPost.author; if (!post.IsPublished && publish) { if (!post.CanPublish(author)) { throw new MetaWeblogException("11", "Not authorized to publish this Post."); } } post.Author = author; post.Title = sentPost.title; post.Content = sentPost.description; post.IsPublished = publish; post.Slug = sentPost.slug; post.Description = sentPost.excerpt; if (sentPost.commentPolicy != string.Empty) { post.HasCommentsEnabled = sentPost.commentPolicy == "1"; } post.Categories.Clear(); foreach (var item in sentPost.categories) { Category cat; if (LookupCategoryGuidByName(item, out cat)) { post.Categories.Add(cat); } else { // Allowing new categories to be added. (This breaks spec, but is supported via WLW) using (var newcat = new Category(item, string.Empty)) { newcat.Save(); post.Categories.Add(newcat); } } } post.Tags.Clear(); foreach (var item in sentPost.tags.Where(item => item != null && item.Trim() != string.Empty)) { post.Tags.Add(item); } if (sentPost.postDate != new DateTime()) { post.DateCreated = sentPost.postDate.AddHours(-BlogSettings.Instance.Timezone); } post.Save(); return(true); }
/// <summary> /// metaWeblog.getRecentPosts /// </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="numberOfPosts">number of posts to return</param> /// <returns>array of post structs</returns> internal List<MWAPost> GetRecentPosts(string blogID, string userName, string password, int numberOfPosts) { ValidateRequest(userName, password); List<MWAPost> sendPosts = new List<MWAPost>(); List<Post> posts = Post.Posts; // Set End Point int stop = numberOfPosts; if (stop > posts.Count) stop = posts.Count; foreach (Post post in posts.GetRange(0, stop)) { MWAPost tempPost = new MWAPost(); List<string> tempCats = new List<string>(); List<string> tempTags = new List<string>(); tempPost.postID = post.Id.ToString(); tempPost.postDate = post.DateCreated; tempPost.title = post.Title; tempPost.description = post.Content; tempPost.link = post.AbsoluteLink.AbsoluteUri; tempPost.slug = post.Slug; tempPost.excerpt = post.Description; if (post.IsCommentsEnabled) tempPost.commentPolicy = ""; else tempPost.commentPolicy = "0"; tempPost.publish = post.IsPublished; for (int i = 0; i < post.Categories.Count; i++) { tempCats.Add(Category.GetCategory(post.Categories[i].Id).ToString()); } tempPost.categories = tempCats; for (int i = 0; i < post.Tags.Count; i++) { tempTags.Add(post.Tags[i]); } tempPost.tags = tempTags; sendPosts.Add(tempPost); } return sendPosts; }
/// <summary> /// metaWeblog.getPost /// </summary> /// <param name="postID">post guid in string format</param> /// <param name="userName">login username</param> /// <param name="password">login password</param> /// <returns>struct with post details</returns> internal MWAPost GetPost(string postID, string userName, string password) { ValidateRequest(userName, password); MWAPost sendPost = new MWAPost(); Post post = Post.GetPost(new Guid(postID)); sendPost.postID = post.Id.ToString(); sendPost.postDate = post.DateCreated; sendPost.title = post.Title; sendPost.description = post.Content; sendPost.link = post.AbsoluteLink.AbsoluteUri; sendPost.slug = post.Slug; sendPost.excerpt = post.Description; if (post.IsCommentsEnabled) sendPost.commentPolicy = "1"; else sendPost.commentPolicy = "0"; sendPost.publish = post.IsPublished; List<string> cats = new List<string>(); for (int i = 0; i < post.Categories.Count; i++) { cats.Add(Category.GetCategory(post.Categories[i].Id).ToString()); } sendPost.categories = cats; List<string> tags = new List<string>(); for (int i = 0; i < post.Tags.Count; i++) { tags.Add(post.Tags[i]); } sendPost.tags = tags; return sendPost; }
/// <summary> /// Creates a Metaweblog Post object from the XML struct /// </summary> /// <param name="node">XML contains a Metaweblog Post Struct</param> /// <returns>Metaweblog Post Struct Obejct</returns> private MWAPost GetPost(XmlNode node) { MWAPost temp = new MWAPost(); List<string> cats = new List<string>(); List<string> tags = new List<string>(); // Require Title and Description try { temp.title = node.SelectSingleNode("value/struct/member[name='title']").LastChild.InnerText; temp.description = node.SelectSingleNode("value/struct/member[name='description']").LastChild.InnerText; } catch (Exception ex) { throw new MetaWeblogException("05", "Post Struct Element, Title or Description, not Sent. (" + ex.Message + ")"); } if (node.SelectSingleNode("value/struct/member[name='link']") == null) temp.link = ""; else temp.link = node.SelectSingleNode("value/struct/member[name='link']").LastChild.InnerText; if (node.SelectSingleNode("value/struct/member[name='mt_allow_comments']") == null) temp.commentPolicy = ""; else temp.commentPolicy = node.SelectSingleNode("value/struct/member[name='mt_allow_comments']").LastChild.InnerText; if (node.SelectSingleNode("value/struct/member[name='mt_excerpt']") == null) temp.excerpt = ""; else temp.excerpt = node.SelectSingleNode("value/struct/member[name='mt_excerpt']").LastChild.InnerText; if (node.SelectSingleNode("value/struct/member[name='wp_slug']") == null) temp.slug = ""; else temp.slug = node.SelectSingleNode("value/struct/member[name='wp_slug']").LastChild.InnerText; if (node.SelectSingleNode("value/struct/member[name='wp_author_id']") == null) temp.author = ""; else temp.author = node.SelectSingleNode("value/struct/member[name='wp_author_id']").LastChild.InnerText; if (node.SelectSingleNode("value/struct/member[name='categories']") != null) { XmlNode categoryArray = node.SelectSingleNode("value/struct/member[name='categories']").LastChild; foreach (XmlNode catnode in categoryArray.SelectNodes("array/data/value/string")) { cats.Add(catnode.InnerText); } } temp.categories = cats; // postDate has a few different names to worry about if (node.SelectSingleNode("value/struct/member[name='dateCreated']") != null) { try { string tempDate = node.SelectSingleNode("value/struct/member[name='dateCreated']").LastChild.InnerText; temp.postDate = DateTime.ParseExact(tempDate, "yyyyMMdd'T'HH':'mm':'ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); } catch { // Ignore PubDate Error } } else if (node.SelectSingleNode("value/struct/member[name='pubDate']") != null) { try { string tempPubDate = node.SelectSingleNode("value/struct/member[name='pubDate']").LastChild.InnerText; temp.postDate = DateTime.ParseExact(tempPubDate, "yyyyMMdd'T'HH':'mm':'ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); } catch { // Ignore PubDate Error } } // WLW tags implementation using mt_keywords if (node.SelectSingleNode("value/struct/member[name='mt_keywords']") != null) { string tagsList = node.SelectSingleNode("value/struct/member[name='mt_keywords']").LastChild.InnerText; foreach (string item in tagsList.Split(',')) { if (string.IsNullOrEmpty(tags.Find(delegate(string t) { return t.Equals(item.Trim(), StringComparison.OrdinalIgnoreCase); }))) { tags.Add(item.Trim()); } } } temp.tags = tags; return temp; }
/// <summary> /// Creates a Metaweblog Post object from the XML struct /// </summary> /// <param name="node">XML contains a Metaweblog Post Struct</param> /// <returns>Metaweblog Post Struct Obejct</returns> private MWAPost GetPost(XmlNode node) { MWAPost temp = new MWAPost(); List <string> cats = new List <string>(); List <string> tags = new List <string>(); // Require Title and Description try { temp.title = node.SelectSingleNode("value/struct/member[name='title']").LastChild.InnerText; temp.description = node.SelectSingleNode("value/struct/member[name='description']").LastChild.InnerText; } catch (Exception ex) { throw new MetaWeblogException("05", "Post Struct Element, Title or Description, not Sent. (" + ex.Message + ")"); } if (node.SelectSingleNode("value/struct/member[name='link']") == null) { temp.link = ""; } else { temp.link = node.SelectSingleNode("value/struct/member[name='link']").LastChild.InnerText; } if (node.SelectSingleNode("value/struct/member[name='mt_allow_comments']") == null) { temp.commentPolicy = ""; } else { temp.commentPolicy = node.SelectSingleNode("value/struct/member[name='mt_allow_comments']").LastChild.InnerText; } if (node.SelectSingleNode("value/struct/member[name='mt_excerpt']") == null) { temp.excerpt = ""; } else { temp.excerpt = node.SelectSingleNode("value/struct/member[name='mt_excerpt']").LastChild.InnerText; } if (node.SelectSingleNode("value/struct/member[name='wp_slug']") == null) { temp.slug = ""; } else { temp.slug = node.SelectSingleNode("value/struct/member[name='wp_slug']").LastChild.InnerText; } if (node.SelectSingleNode("value/struct/member[name='wp_author_id']") == null) { temp.author = ""; } else { temp.author = node.SelectSingleNode("value/struct/member[name='wp_author_id']").LastChild.InnerText; } if (node.SelectSingleNode("value/struct/member[name='categories']") != null) { XmlNode categoryArray = node.SelectSingleNode("value/struct/member[name='categories']").LastChild; foreach (XmlNode catnode in categoryArray.SelectNodes("array/data/value/string")) { cats.Add(catnode.InnerText); } } temp.categories = cats; // postDate has a few different names to worry about if (node.SelectSingleNode("value/struct/member[name='dateCreated']") != null) { try { string tempDate = node.SelectSingleNode("value/struct/member[name='dateCreated']").LastChild.InnerText; temp.postDate = DateTime.ParseExact(tempDate, "yyyyMMdd'T'HH':'mm':'ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); } catch { // Ignore PubDate Error } } else if (node.SelectSingleNode("value/struct/member[name='pubDate']") != null) { try { string tempPubDate = node.SelectSingleNode("value/struct/member[name='pubDate']").LastChild.InnerText; temp.postDate = DateTime.ParseExact(tempPubDate, "yyyyMMdd'T'HH':'mm':'ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); } catch { // Ignore PubDate Error } } // WLW tags implementation using mt_keywords if (node.SelectSingleNode("value/struct/member[name='mt_keywords']") != null) { string tagsList = node.SelectSingleNode("value/struct/member[name='mt_keywords']").LastChild.InnerText; foreach (string item in tagsList.Split(',')) { tags.Add(item.Trim().ToLowerInvariant()); } } temp.tags = tags; return(temp); }
/// <summary> /// Loads object properties with contents of passed xml /// </summary> /// <param name="xml">xml doc with methodname and parameters</param> private void LoadXMLRequest(string xml) { XmlDocument request = new XmlDocument(); try { if (!(xml.StartsWith("<?xml") || xml.StartsWith("<method"))) { xml = xml.Substring(xml.IndexOf("<?xml")); } request.LoadXml(xml); } catch (Exception ex) { throw new MetaWeblogException("01", "Invalid XMLRPC Request. (" + ex.Message + ")"); } // Method name is always first _methodName = request.DocumentElement.ChildNodes[0].InnerText; // Parameters are next (and last) _inputParams = new List <XmlNode>(); foreach (XmlNode node in request.SelectNodes("/methodCall/params/param")) { _inputParams.Add(node); } // Determine what params are what by method name switch (_methodName) { case "metaWeblog.newPost": _blogID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; _post = GetPost(_inputParams[3]); if (_inputParams[4].InnerText == "0" || _inputParams[4].InnerText == "false") { _publish = false; } else { _publish = true; } break; case "metaWeblog.editPost": _postID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; _post = GetPost(_inputParams[3]); if (_inputParams[4].InnerText == "0" || _inputParams[4].InnerText == "false") { _publish = false; } else { _publish = true; } break; case "metaWeblog.getPost": _postID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; break; case "metaWeblog.newMediaObject": _blogID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; _media = GetMediaObject(_inputParams[3]); break; case "metaWeblog.getCategories": case "wp.getAuthors": case "wp.getPageList": case "wp.getPages": case "wp.getTags": _blogID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; break; case "metaWeblog.getRecentPosts": _blogID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; _numberOfPosts = Int32.Parse(_inputParams[3].InnerText, CultureInfo.InvariantCulture); break; case "blogger.getUsersBlogs": case "metaWeblog.getUsersBlogs": _appKey = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; break; case "blogger.deletePost": _appKey = _inputParams[0].InnerText; _postID = _inputParams[1].InnerText; _userName = _inputParams[2].InnerText; _password = _inputParams[3].InnerText; if (_inputParams[4].InnerText == "0" || _inputParams[4].InnerText == "false") { _publish = false; } else { _publish = true; } break; case "blogger.getUserInfo": _appKey = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; break; case "wp.newPage": _blogID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; _page = GetPage(_inputParams[3]); if (_inputParams[4].InnerText == "0" || _inputParams[4].InnerText == "false") { _publish = false; } else { _publish = true; } break; case "wp.getPage": _blogID = _inputParams[0].InnerText; _pageID = _inputParams[1].InnerText; _userName = _inputParams[2].InnerText; _password = _inputParams[3].InnerText; break; case "wp.editPage": _blogID = _inputParams[0].InnerText; _pageID = _inputParams[1].InnerText; _userName = _inputParams[2].InnerText; _password = _inputParams[3].InnerText; _page = GetPage(_inputParams[4]); if (_inputParams[5].InnerText == "0" || _inputParams[5].InnerText == "false") { _publish = false; } else { _publish = true; } break; case "wp.deletePage": _blogID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; _pageID = _inputParams[3].InnerText; break; default: throw new MetaWeblogException("02", "Unknown Method. (" + _methodName + ")"); } }
/// <summary> /// Creates a Metaweblog Post object from the XML struct /// </summary> /// <param name="node"> /// XML contains a Metaweblog Post Struct /// </param> /// <returns> /// Metaweblog Post Struct Obejct /// </returns> private static MWAPost GetPost(XmlNode node) { var temp = new MWAPost(); // Require Title and Description var title = node.SelectSingleNode("value/struct/member[name='title']"); if (title == null) { throw new MetaWeblogException("05", "Page Struct Element, Title, not Sent."); } temp.title = title.LastChild.InnerText; var description = node.SelectSingleNode("value/struct/member[name='description']"); if (description == null) { throw new MetaWeblogException("05", "Page Struct Element, Description, not Sent."); } temp.description = description.LastChild.InnerText; var link = node.SelectSingleNode("value/struct/member[name='link']"); temp.link = link == null ? string.Empty : link.LastChild.InnerText; var allowComments = node.SelectSingleNode("value/struct/member[name='mt_allow_comments']"); temp.commentPolicy = allowComments == null ? string.Empty : allowComments.LastChild.InnerText; var excerpt = node.SelectSingleNode("value/struct/member[name='mt_excerpt']"); temp.excerpt = excerpt == null ? string.Empty : excerpt.LastChild.InnerText; var slug = node.SelectSingleNode("value/struct/member[name='wp_slug']"); temp.slug = slug == null ? string.Empty : slug.LastChild.InnerText; var authorId = node.SelectSingleNode("value/struct/member[name='wp_author_id']"); temp.author = authorId == null ? string.Empty : authorId.LastChild.InnerText; var cats = new List <string>(); var categories = node.SelectSingleNode("value/struct/member[name='categories']"); if (categories != null) { var categoryArray = categories.LastChild; var categoryArrayNodes = categoryArray.SelectNodes("array/data/value/string"); if (categoryArrayNodes != null) { cats.AddRange(categoryArrayNodes.Cast <XmlNode>().Select( catnode => catnode.InnerText)); } } temp.categories = cats; // postDate has a few different names to worry about var dateCreated = node.SelectSingleNode("value/struct/member[name='dateCreated']"); var pubDate = node.SelectSingleNode("value/struct/member[name='pubDate']"); if (dateCreated != null) { try { var tempDate = dateCreated.LastChild.InnerText; temp.postDate = DateTime.ParseExact( tempDate, "yyyyMMdd'T'HH':'mm':'ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); } catch (Exception ex) { // Ignore PubDate Error Debug.WriteLine(ex.Message); } } else if (pubDate != null) { try { var tempPubDate = pubDate.LastChild.InnerText; temp.postDate = DateTime.ParseExact( tempPubDate, "yyyyMMdd'T'HH':'mm':'ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); } catch (Exception ex) { // Ignore PubDate Error Debug.WriteLine(ex.Message); } } // WLW tags implementation using mt_keywords var tags = new List <string>(); var keywords = node.SelectSingleNode("value/struct/member[name='mt_keywords']"); if (keywords != null) { var tagsList = keywords.LastChild.InnerText; foreach (var item in tagsList.Split(',').Where(item => string.IsNullOrEmpty(tags.Find(t => t.Equals(item.Trim(), StringComparison.OrdinalIgnoreCase))))) { tags.Add(item.Trim()); } } temp.tags = tags; return(temp); }
/// <summary> /// metaWeblog.editPost /// </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) { ValidateRequest(userName, password); Post post = Post.GetPost(new Guid(postID)); if (String.IsNullOrEmpty(sentPost.author)) { post.Author = userName; } else { post.Author = sentPost.author; } post.Title = sentPost.title; post.Content = sentPost.description; post.IsPublished = publish; post.Slug = sentPost.slug; post.Description = sentPost.excerpt; if (sentPost.commentPolicy != "") { if (sentPost.commentPolicy == "1") { post.IsCommentsEnabled = true; } else { post.IsCommentsEnabled = false; } } post.Categories.Clear(); foreach (string item in sentPost.categories) { Category cat; if (LookupCategoryGuidByName(item, out cat)) { post.Categories.Add(cat); } else { // Allowing new categories to be added. (This breaks spec, but is supported via WLW) Category newcat = new Category(item, ""); newcat.Save(); post.Categories.Add(newcat); } } post.Tags.Clear(); foreach (string item in sentPost.tags) { if (item != null && item.Trim() != string.Empty) { post.Tags.Add(item); } } if (sentPost.postDate != new DateTime()) { post.DateCreated = sentPost.postDate.AddHours(-BlogSettings.Instance.Timezone); } post.Save(); return(true); }
/// <summary> /// Creates a Metaweblog Post object from the XML struct /// </summary> /// <param name="node"> /// XML contains a Metaweblog Post Struct /// </param> /// <returns> /// Metaweblog Post Struct Obejct /// </returns> private static MWAPost GetPost(XmlNode node) { var temp = new MWAPost(); // Require Title and Description var title = node.SelectSingleNode("value/struct/member[name='title']"); if (title == null) { throw new MetaWeblogException("05", "Page Struct Element, Title, not Sent."); } temp.title = title.LastChild.InnerText; var description = node.SelectSingleNode("value/struct/member[name='description']"); if (description == null) { throw new MetaWeblogException("05", "Page Struct Element, Description, not Sent."); } temp.description = description.LastChild.InnerText; var link = node.SelectSingleNode("value/struct/member[name='link']"); temp.link = link == null ? string.Empty : link.LastChild.InnerText; var allowComments = node.SelectSingleNode("value/struct/member[name='mt_allow_comments']"); temp.commentPolicy = allowComments == null ? string.Empty : allowComments.LastChild.InnerText; var excerpt = node.SelectSingleNode("value/struct/member[name='mt_excerpt']"); temp.excerpt = excerpt == null ? string.Empty : excerpt.LastChild.InnerText; var slug = node.SelectSingleNode("value/struct/member[name='wp_slug']"); temp.slug = slug == null ? string.Empty : slug.LastChild.InnerText; var authorId = node.SelectSingleNode("value/struct/member[name='wp_author_id']"); temp.author = authorId == null ? string.Empty : authorId.LastChild.InnerText; var cats = new List<string>(); var categories = node.SelectSingleNode("value/struct/member[name='categories']"); if (categories != null) { var categoryArray = categories.LastChild; var categoryArrayNodes = categoryArray.SelectNodes("array/data/value/string"); if (categoryArrayNodes != null) { cats.AddRange(categoryArrayNodes.Cast<XmlNode>().Select( catnode => catnode.InnerText)); } } temp.categories = cats; // postDate has a few different names to worry about var dateCreated = node.SelectSingleNode("value/struct/member[name='dateCreated']"); var pubDate = node.SelectSingleNode("value/struct/member[name='pubDate']"); if (dateCreated != null) { try { var tempDate = dateCreated.LastChild.InnerText; temp.postDate = DateTime.ParseExact( tempDate, "yyyyMMdd'T'HH':'mm':'ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); } catch (Exception ex) { // Ignore PubDate Error Debug.WriteLine(ex.Message); } } else if (pubDate != null) { try { var tempPubDate = pubDate.LastChild.InnerText; temp.postDate = DateTime.ParseExact( tempPubDate, "yyyyMMdd'T'HH':'mm':'ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); } catch (Exception ex) { // Ignore PubDate Error Debug.WriteLine(ex.Message); } } // WLW tags implementation using mt_keywords var tags = new List<string>(); var keywords = node.SelectSingleNode("value/struct/member[name='mt_keywords']"); if (keywords != null) { var tagsList = keywords.LastChild.InnerText; foreach (var item in tagsList.Split(',').Where(item => string.IsNullOrEmpty(tags.Find(t => t.Equals(item.Trim(), StringComparison.OrdinalIgnoreCase))))) { tags.Add(item.Trim()); } } temp.tags = tags; return temp; }
/// <summary> /// Loads object properties with contents of passed xml /// </summary> /// <param name="xml">xml doc with methodname and parameters</param> private void LoadXMLRequest(string xml) { XmlDocument request = new XmlDocument(); try { if (!(xml.StartsWith("<?xml") || xml.StartsWith("<method"))) { xml = xml.Substring(xml.IndexOf("<?xml")); } request.LoadXml(xml); } catch (Exception ex) { throw new MetaWeblogException("01", "Invalid XMLRPC Request. (" + ex.Message + ")"); } // Method name is always first _methodName = request.DocumentElement.ChildNodes[0].InnerText; // Parameters are next (and last) _inputParams = new List<XmlNode>(); foreach (XmlNode node in request.SelectNodes("/methodCall/params/param")) { _inputParams.Add(node); } // Determine what params are what by method name switch (_methodName) { case "metaWeblog.newPost": _blogID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; _post = GetPost(_inputParams[3]); if (_inputParams[4].InnerText == "0" || _inputParams[4].InnerText == "false") _publish = false; else _publish = true; break; case "metaWeblog.editPost": _postID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; _post = GetPost(_inputParams[3]); if (_inputParams[4].InnerText == "0" || _inputParams[4].InnerText == "false") _publish = false; else _publish = true; break; case "metaWeblog.getPost": _postID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; break; case "metaWeblog.newMediaObject": _blogID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; _media = GetMediaObject(_inputParams[3]); break; case "metaWeblog.getCategories": case "wp.getAuthors": case "wp.getPageList": case "wp.getPages": case "wp.getTags": _blogID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; break; case "metaWeblog.getRecentPosts": _blogID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; _numberOfPosts = Int32.Parse(_inputParams[3].InnerText, CultureInfo.InvariantCulture); break; case "blogger.getUsersBlogs": case "metaWeblog.getUsersBlogs": _appKey = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; break; case "blogger.deletePost": _appKey = _inputParams[0].InnerText; _postID = _inputParams[1].InnerText; _userName = _inputParams[2].InnerText; _password = _inputParams[3].InnerText; if (_inputParams[4].InnerText == "0" || _inputParams[4].InnerText == "false") _publish = false; else _publish = true; break; case "blogger.getUserInfo": _appKey = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; break; case "wp.newPage": _blogID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; _page = GetPage(_inputParams[3]); if (_inputParams[4].InnerText == "0" || _inputParams[4].InnerText == "false") _publish = false; else _publish = true; break; case "wp.getPage": _blogID = _inputParams[0].InnerText; _pageID = _inputParams[1].InnerText; _userName = _inputParams[2].InnerText; _password = _inputParams[3].InnerText; break; case "wp.editPage": _blogID = _inputParams[0].InnerText; _pageID = _inputParams[1].InnerText; _userName = _inputParams[2].InnerText; _password = _inputParams[3].InnerText; _page = GetPage(_inputParams[4]); if (_inputParams[5].InnerText == "0" || _inputParams[5].InnerText == "false") _publish = false; else _publish = true; break; case "wp.deletePage": _blogID = _inputParams[0].InnerText; _userName = _inputParams[1].InnerText; _password = _inputParams[2].InnerText; _pageID = _inputParams[3].InnerText; break; default: throw new MetaWeblogException("02", "Unknown Method. (" + _methodName + ")"); } }
/// <summary> /// metaWeblog.getRecentPosts 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="numberOfPosts"> /// number of posts to return /// </param> /// <returns> /// array of post structs /// </returns> internal List<MWAPost> GetRecentPosts(string blogId, string userName, string password, int numberOfPosts) { var sendPosts = new List<MWAPost>(); var posts = Post.Posts.Where(p => p.IsVisible).ToList(); // Set End Point var stop = numberOfPosts; if (stop > posts.Count) { stop = posts.Count; } foreach (var post in posts.GetRange(0, stop)) { var tempPost = new MWAPost { postID = post.Id.ToString(), postDate = post.DateCreated, title = post.Title, description = post.Content, link = post.AbsoluteLink.AbsoluteUri, slug = post.Slug, excerpt = post.Description, commentPolicy = post.HasCommentsEnabled ? string.Empty : "0", publish = post.IsPublished }; var tempCats = post.Categories.Select(t => Category.GetCategory(t.Id).ToString()).ToList(); tempPost.categories = tempCats; var tempTags = post.Tags.ToList(); tempPost.tags = tempTags; sendPosts.Add(tempPost); } return sendPosts; }
/// <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) { if (!Security.IsAuthorizedTo(Rights.CreateNewPosts)) { throw new MetaWeblogException("11", "User authentication failed"); } string author = String.IsNullOrEmpty(sentPost.author) ? userName : sentPost.author; var post = new Post { Author = author, Title = sentPost.title, Content = sentPost.description, IsPublished = publish, Slug = sentPost.slug, Description = sentPost.excerpt }; if (publish) { if (!post.CanPublish(author)) { throw new MetaWeblogException("11", "Not authorized to publish this Post."); } } if (sentPost.commentPolicy != string.Empty) { post.HasCommentsEnabled = sentPost.commentPolicy == "1"; } post.Categories.Clear(); foreach (var item in sentPost.categories.Where(c => c != null && c.Trim() != string.Empty)) { Category cat; if (LookupCategoryGuidByName(item, out cat)) { post.Categories.Add(cat); } else { // Allowing new categories to be added. (This breaks spec, but is supported via WLW) var newcat = new Category(item, string.Empty); newcat.Save(); post.Categories.Add(newcat); } } post.Tags.Clear(); foreach (var item in sentPost.tags.Where(item => item != null && item.Trim() != string.Empty)) { post.Tags.Add(item); } if (sentPost.postDate != new DateTime()) { post.DateCreated = sentPost.postDate; } post.Save(); return post.Id.ToString(); }
/// <summary> /// metaWeblog.editPost /// </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) { ValidateRequest(userName, password); Post post = Post.GetPost(new Guid(postID)); if (String.IsNullOrEmpty(sentPost.author)) post.Author = userName; else post.Author = sentPost.author; post.Title = sentPost.title; post.Content = sentPost.description; post.IsPublished = publish; post.Slug = sentPost.slug; post.Description = sentPost.excerpt; if (sentPost.commentPolicy != "") { if (sentPost.commentPolicy == "1") post.IsCommentsEnabled = true; else post.IsCommentsEnabled = false; } post.Categories.Clear(); foreach (string item in sentPost.categories) { Category cat; if (LookupCategoryGuidByName(item, out cat)) post.Categories.Add(cat); else { // Allowing new categories to be added. (This breaks spec, but is supported via WLW) Category newcat = new Category(item, ""); newcat.Save(); post.Categories.Add(newcat); } } post.Tags.Clear(); foreach (string item in sentPost.tags) { if (item != null && item.Trim() != string.Empty) post.Tags.Add(item); } if (sentPost.postDate != new DateTime()) post.DateCreated = sentPost.postDate.AddHours(-BlogSettings.Instance.Timezone); post.Save(); return true; }