You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
5.3 KiB

  1. using CookComputing.XmlRpc;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Web.Security;
  7. public interface IMetaWeblog
  8. {
  9. #region MetaWeblog API
  10. [XmlRpcMethod("metaWeblog.newPost")]
  11. string AddPost(string blogid, string username, string password, Post post, bool publish);
  12. [XmlRpcMethod("metaWeblog.editPost")]
  13. bool UpdatePost(string postid, string username, string password, Post post, bool publish);
  14. [XmlRpcMethod("metaWeblog.getPost")]
  15. object GetPost(string postid, string username, string password);
  16. [XmlRpcMethod("metaWeblog.getCategories")]
  17. object[] GetCategories(string blogid, string username, string password);
  18. [XmlRpcMethod("metaWeblog.getRecentPosts")]
  19. object[] GetRecentPosts(string blogid, string username, string password, int numberOfPosts);
  20. [XmlRpcMethod("metaWeblog.newMediaObject")]
  21. object NewMediaObject(string blogid, string username, string password, MediaObject mediaObject);
  22. #endregion
  23. #region Blogger API
  24. [XmlRpcMethod("blogger.deletePost")]
  25. [return: XmlRpcReturnValue(Description = "Returns true.")]
  26. bool DeletePost(string key, string postid, string username, string password, bool publish);
  27. [XmlRpcMethod("blogger.getUsersBlogs")]
  28. object[] GetUsersBlogs(string key, string username, string password);
  29. #endregion
  30. }
  31. public class MetaWeblogHandler : XmlRpcService, IMetaWeblog
  32. {
  33. string IMetaWeblog.AddPost(string blogid, string username, string password, Post post, bool publish)
  34. {
  35. ValidateUser(username, password);
  36. post.Slug = PostHandler.CreateSlug(post.Title);
  37. post.IsPublished = publish;
  38. Storage.Save(post);
  39. return post.ID;
  40. }
  41. bool IMetaWeblog.UpdatePost(string postid, string username, string password, Post post, bool publish)
  42. {
  43. ValidateUser(username, password);
  44. Post match = Storage.GetAllPosts().FirstOrDefault(p => p.ID == postid);
  45. if (match != null)
  46. {
  47. match.Title = post.Title;
  48. match.Content = post.Content;
  49. match.Slug = post.Slug;
  50. match.Categories = post.Categories;
  51. match.IsPublished = publish;
  52. match.PubDate = post.PubDate;
  53. Storage.Save(match);
  54. }
  55. return match != null;
  56. }
  57. bool IMetaWeblog.DeletePost(string key, string postid, string username, string password, bool publish)
  58. {
  59. ValidateUser(username, password);
  60. Post post = Storage.GetAllPosts().FirstOrDefault(p => p.ID == postid);
  61. if (post != null)
  62. {
  63. Storage.Delete(post);
  64. }
  65. return post != null;
  66. }
  67. object IMetaWeblog.GetPost(string postid, string username, string password)
  68. {
  69. ValidateUser(username, password);
  70. Post post = Storage.GetAllPosts().FirstOrDefault(p => p.ID == postid);
  71. if (post == null)
  72. throw new XmlRpcFaultException(0, "Post does not exist");
  73. return new
  74. {
  75. description = post.Content,
  76. title = post.Title,
  77. dateCreated = post.PubDate,
  78. wp_slug = post.Slug,
  79. categories = post.Categories.ToArray(),
  80. postid = post.ID
  81. };
  82. }
  83. object[] IMetaWeblog.GetRecentPosts(string blogid, string username, string password, int numberOfPosts)
  84. {
  85. ValidateUser(username, password);
  86. List<object> list = new List<object>();
  87. foreach (var post in Storage.GetAllPosts().Take(numberOfPosts))
  88. {
  89. var info = new
  90. {
  91. description = post.Content,
  92. title = post.Title,
  93. dateCreated = post.PubDate,
  94. wp_slug = post.Slug,
  95. postid = post.ID
  96. };
  97. list.Add(info);
  98. }
  99. return list.ToArray();
  100. }
  101. object[] IMetaWeblog.GetCategories(string blogid, string username, string password)
  102. {
  103. ValidateUser(username, password);
  104. var list = new List<object>();
  105. var categories = Storage.GetAllPosts().SelectMany(p => p.Categories);
  106. foreach (string category in categories.Distinct())
  107. {
  108. list.Add(new { title = category });
  109. }
  110. return list.ToArray();
  111. }
  112. object IMetaWeblog.NewMediaObject(string blogid, string username, string password, MediaObject media)
  113. {
  114. ValidateUser(username, password);
  115. string path = Blog.SaveFileToDisk(media.bits, Path.GetExtension(media.name));
  116. return new { url = path };
  117. }
  118. object[] IMetaWeblog.GetUsersBlogs(string key, string username, string password)
  119. {
  120. ValidateUser(username, password);
  121. return new[]
  122. {
  123. new
  124. {
  125. blogid = "1",
  126. blogName = ConfigurationManager.AppSettings.Get("blog:name"),
  127. url = Context.Request.Url.Scheme + "://" + Context.Request.Url.Authority
  128. }
  129. };
  130. }
  131. private void ValidateUser(string username, string password)
  132. {
  133. if (!FormsAuthentication.Authenticate(username, password))
  134. {
  135. throw new XmlRpcFaultException(0, "User is not valid!");
  136. }
  137. }
  138. }
  139. [XmlRpcMissingMapping(MappingAction.Ignore)]
  140. public struct MediaObject
  141. {
  142. public string name;
  143. public string type;
  144. public byte[] bits;
  145. }