2010-03-01 17:13:36 -08:00
|
|
|
|
using System;
|
2010-03-02 12:10:49 -08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
using System.Web.Routing;
|
2010-03-03 23:31:42 -08:00
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
using Orchard.Blogs.Drivers;
|
2010-03-02 12:10:49 -08:00
|
|
|
|
using Orchard.Blogs.Models;
|
|
|
|
|
using Orchard.ContentManagement;
|
2010-03-01 17:13:36 -08:00
|
|
|
|
using Orchard.Core.XmlRpc;
|
|
|
|
|
using Orchard.Core.XmlRpc.Models;
|
2010-05-19 14:33:51 -07:00
|
|
|
|
using Orchard.Environment.Extensions;
|
2010-03-01 17:13:36 -08:00
|
|
|
|
using Orchard.Logging;
|
2010-03-02 12:10:49 -08:00
|
|
|
|
using Orchard.Mvc.Extensions;
|
|
|
|
|
using Orchard.Security;
|
|
|
|
|
using Orchard.Blogs.Extensions;
|
2010-03-01 17:13:36 -08:00
|
|
|
|
|
|
|
|
|
namespace Orchard.Blogs.Services {
|
2010-03-03 23:31:42 -08:00
|
|
|
|
[UsedImplicitly]
|
2010-05-19 14:33:51 -07:00
|
|
|
|
[OrchardFeature("Remote Blog Publishing")]
|
2010-03-01 17:13:36 -08:00
|
|
|
|
public class XmlRpcHandler : IXmlRpcHandler {
|
|
|
|
|
private readonly IBlogService _blogService;
|
2010-03-02 12:10:49 -08:00
|
|
|
|
private readonly IBlogPostService _blogPostService;
|
|
|
|
|
private readonly IContentManager _contentManager;
|
|
|
|
|
private readonly IAuthorizationService _authorizationService;
|
|
|
|
|
private readonly IMembershipService _membershipService;
|
|
|
|
|
private readonly RouteCollection _routeCollection;
|
|
|
|
|
|
|
|
|
|
public XmlRpcHandler(IBlogService blogService, IBlogPostService blogPostService, IContentManager contentManager,
|
|
|
|
|
IAuthorizationService authorizationService, IMembershipService membershipService,
|
|
|
|
|
RouteCollection routeCollection) {
|
2010-03-01 17:13:36 -08:00
|
|
|
|
_blogService = blogService;
|
2010-03-02 12:10:49 -08:00
|
|
|
|
_blogPostService = blogPostService;
|
|
|
|
|
_contentManager = contentManager;
|
|
|
|
|
_authorizationService = authorizationService;
|
|
|
|
|
_membershipService = membershipService;
|
|
|
|
|
_routeCollection = routeCollection;
|
2010-03-01 17:13:36 -08:00
|
|
|
|
Logger = NullLogger.Instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ILogger Logger { get; set; }
|
|
|
|
|
|
|
|
|
|
public void Process(XmlRpcContext context) {
|
2010-03-02 12:10:49 -08:00
|
|
|
|
var urlHelper = new UrlHelper(context.ControllerContext.RequestContext, _routeCollection);
|
2010-03-01 17:13:36 -08:00
|
|
|
|
|
|
|
|
|
if (context.Request.MethodName == "blogger.getUsersBlogs") {
|
2010-03-02 12:10:49 -08:00
|
|
|
|
var result = MetaWeblogGetUserBlogs(urlHelper,
|
|
|
|
|
Convert.ToString(context.Request.Params[0].Value),
|
|
|
|
|
Convert.ToString(context.Request.Params[1].Value),
|
|
|
|
|
Convert.ToString(context.Request.Params[2].Value));
|
|
|
|
|
|
|
|
|
|
context.Response = new XRpcMethodResponse().Add(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (context.Request.MethodName == "metaWeblog.getRecentPosts") {
|
|
|
|
|
var result = MetaWeblogGetRecentPosts(urlHelper,
|
|
|
|
|
Convert.ToString(context.Request.Params[0].Value),
|
|
|
|
|
Convert.ToString(context.Request.Params[1].Value),
|
|
|
|
|
Convert.ToString(context.Request.Params[2].Value),
|
|
|
|
|
Convert.ToInt32(context.Request.Params[3].Value));
|
|
|
|
|
|
|
|
|
|
context.Response = new XRpcMethodResponse().Add(result);
|
2010-03-01 17:13:36 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (context.Request.MethodName == "metaWeblog.newPost") {
|
|
|
|
|
var result = MetaWeblogNewPost(
|
|
|
|
|
Convert.ToString(context.Request.Params[0].Value),
|
|
|
|
|
Convert.ToString(context.Request.Params[1].Value),
|
|
|
|
|
Convert.ToString(context.Request.Params[2].Value),
|
|
|
|
|
(XRpcStruct)context.Request.Params[3].Value,
|
|
|
|
|
Convert.ToBoolean(context.Request.Params[4].Value));
|
|
|
|
|
|
|
|
|
|
context.Response = new XRpcMethodResponse().Add(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (context.Request.MethodName == "metaWeblog.getPost") {
|
|
|
|
|
var result = MetaWeblogGetPost(
|
2010-03-02 12:10:49 -08:00
|
|
|
|
urlHelper,
|
2010-03-01 17:13:36 -08:00
|
|
|
|
Convert.ToInt32(context.Request.Params[0].Value),
|
|
|
|
|
Convert.ToString(context.Request.Params[1].Value),
|
|
|
|
|
Convert.ToString(context.Request.Params[2].Value));
|
|
|
|
|
context.Response = new XRpcMethodResponse().Add(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (context.Request.MethodName == "metaWeblog.editPost") {
|
|
|
|
|
var result = MetaWeblogEditPost(
|
|
|
|
|
Convert.ToInt32(context.Request.Params[0].Value),
|
|
|
|
|
Convert.ToString(context.Request.Params[1].Value),
|
|
|
|
|
Convert.ToString(context.Request.Params[2].Value),
|
|
|
|
|
(XRpcStruct)context.Request.Params[3].Value,
|
|
|
|
|
Convert.ToBoolean(context.Request.Params[4].Value));
|
|
|
|
|
context.Response = new XRpcMethodResponse().Add(result);
|
|
|
|
|
}
|
2010-03-02 12:10:49 -08:00
|
|
|
|
|
|
|
|
|
if (context.Request.MethodName == "blogger.deletePost") {
|
|
|
|
|
var result = MetaWeblogDeletePost(
|
|
|
|
|
Convert.ToString(context.Request.Params[0].Value),
|
|
|
|
|
Convert.ToString(context.Request.Params[1].Value),
|
|
|
|
|
Convert.ToString(context.Request.Params[2].Value),
|
|
|
|
|
Convert.ToString(context.Request.Params[3].Value),
|
|
|
|
|
Convert.ToBoolean(context.Request.Params[4].Value));
|
|
|
|
|
context.Response = new XRpcMethodResponse().Add(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private XRpcArray MetaWeblogGetUserBlogs(UrlHelper urlHelper,
|
|
|
|
|
string appkey,
|
|
|
|
|
string userName,
|
|
|
|
|
string password) {
|
|
|
|
|
|
|
|
|
|
var user = _membershipService.ValidateUser(userName, password);
|
|
|
|
|
_authorizationService.CheckAccess(StandardPermissions.AccessFrontEnd, user, null);
|
|
|
|
|
|
|
|
|
|
var array = new XRpcArray();
|
|
|
|
|
foreach (var blog in _blogService.Get()) {
|
|
|
|
|
array.Add(new XRpcStruct()
|
|
|
|
|
.Set("url", urlHelper.AbsoluteAction(() => urlHelper.Blog(blog.Slug)))
|
|
|
|
|
.Set("blogid", blog.Id)
|
|
|
|
|
.Set("blogName", blog.Name));
|
|
|
|
|
}
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private XRpcArray MetaWeblogGetRecentPosts(
|
|
|
|
|
UrlHelper urlHelper,
|
|
|
|
|
string blogId,
|
|
|
|
|
string userName,
|
|
|
|
|
string password,
|
|
|
|
|
int numberOfPosts) {
|
|
|
|
|
|
|
|
|
|
var user = _membershipService.ValidateUser(userName, password);
|
|
|
|
|
_authorizationService.CheckAccess(StandardPermissions.AccessFrontEnd, user, null);
|
|
|
|
|
|
|
|
|
|
var blog = _contentManager.Get<Blog>(Convert.ToInt32(blogId));
|
|
|
|
|
if (blog == null)
|
|
|
|
|
throw new ArgumentException();
|
|
|
|
|
|
|
|
|
|
var array = new XRpcArray();
|
|
|
|
|
foreach (var blogPost in _blogPostService.Get(blog).Take(numberOfPosts)) {
|
|
|
|
|
array.Add(CreateBlogStruct(blogPost, urlHelper));
|
|
|
|
|
}
|
|
|
|
|
return array;
|
2010-03-01 17:13:36 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int MetaWeblogNewPost(
|
|
|
|
|
string blogId,
|
2010-03-02 12:10:49 -08:00
|
|
|
|
string userName,
|
2010-03-01 17:13:36 -08:00
|
|
|
|
string password,
|
|
|
|
|
XRpcStruct content,
|
|
|
|
|
bool publish) {
|
|
|
|
|
|
2010-03-02 12:10:49 -08:00
|
|
|
|
var user = _membershipService.ValidateUser(userName, password);
|
|
|
|
|
_authorizationService.CheckAccess(Permissions.EditBlogPost, user, null);
|
|
|
|
|
|
|
|
|
|
var blog = _contentManager.Get<Blog>(Convert.ToInt32(blogId));
|
|
|
|
|
if (blog == null)
|
|
|
|
|
throw new ArgumentException();
|
|
|
|
|
|
|
|
|
|
var title = content.Optional<string>("title");
|
|
|
|
|
var description = content.Optional<string>("description");
|
|
|
|
|
var slug = content.Optional<string>("wp_slug");
|
2010-03-01 17:13:36 -08:00
|
|
|
|
|
2010-03-02 12:10:49 -08:00
|
|
|
|
var blogPost = _contentManager.New<BlogPost>(BlogPostDriver.ContentType.Name);
|
|
|
|
|
blogPost.Blog = blog;
|
|
|
|
|
blogPost.Title = title;
|
|
|
|
|
blogPost.Slug = slug;
|
|
|
|
|
blogPost.Text = description;
|
2010-03-02 12:38:54 -08:00
|
|
|
|
blogPost.Creator = user;
|
2010-03-01 17:13:36 -08:00
|
|
|
|
|
2010-03-02 12:10:49 -08:00
|
|
|
|
_contentManager.Create(blogPost.ContentItem, VersionOptions.Draft);
|
2010-03-01 17:13:36 -08:00
|
|
|
|
|
2010-03-02 12:10:49 -08:00
|
|
|
|
if (publish)
|
|
|
|
|
_blogPostService.Publish(blogPost);
|
|
|
|
|
|
|
|
|
|
return blogPost.Id;
|
2010-03-01 17:13:36 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private XRpcStruct MetaWeblogGetPost(
|
2010-03-02 12:10:49 -08:00
|
|
|
|
UrlHelper urlHelper,
|
2010-03-01 17:13:36 -08:00
|
|
|
|
int postId,
|
2010-03-02 12:10:49 -08:00
|
|
|
|
string userName,
|
2010-03-01 17:13:36 -08:00
|
|
|
|
string password) {
|
|
|
|
|
|
2010-03-02 12:10:49 -08:00
|
|
|
|
var user = _membershipService.ValidateUser(userName, password);
|
|
|
|
|
_authorizationService.CheckAccess(StandardPermissions.AccessFrontEnd, user, null);
|
2010-03-01 17:13:36 -08:00
|
|
|
|
|
2010-03-02 12:10:49 -08:00
|
|
|
|
var blogPost = _blogPostService.Get(postId);
|
|
|
|
|
if (blogPost == null)
|
|
|
|
|
throw new ArgumentException();
|
2010-03-01 17:13:36 -08:00
|
|
|
|
|
2010-03-02 12:10:49 -08:00
|
|
|
|
return CreateBlogStruct(blogPost, urlHelper);
|
2010-03-01 17:13:36 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool MetaWeblogEditPost(
|
|
|
|
|
int postId,
|
2010-03-02 12:10:49 -08:00
|
|
|
|
string userName,
|
2010-03-01 17:13:36 -08:00
|
|
|
|
string password,
|
|
|
|
|
XRpcStruct content,
|
|
|
|
|
bool publish) {
|
|
|
|
|
|
2010-03-02 12:10:49 -08:00
|
|
|
|
var user = _membershipService.ValidateUser(userName, password);
|
|
|
|
|
_authorizationService.CheckAccess(StandardPermissions.AccessFrontEnd, user, null);
|
2010-03-01 17:13:36 -08:00
|
|
|
|
|
2010-03-02 12:10:49 -08:00
|
|
|
|
var blogPost = _blogPostService.Get(postId, VersionOptions.DraftRequired);
|
|
|
|
|
if (blogPost == null)
|
|
|
|
|
throw new ArgumentException();
|
2010-03-01 17:13:36 -08:00
|
|
|
|
|
|
|
|
|
|
2010-03-02 12:10:49 -08:00
|
|
|
|
var title = content.Optional<string>("title");
|
|
|
|
|
var description = content.Optional<string>("description");
|
|
|
|
|
var slug = content.Optional<string>("wp_slug");
|
2010-03-01 17:13:36 -08:00
|
|
|
|
|
2010-03-02 12:10:49 -08:00
|
|
|
|
blogPost.Title = title;
|
|
|
|
|
blogPost.Slug = slug;
|
|
|
|
|
blogPost.Text = description;
|
2010-03-01 17:13:36 -08:00
|
|
|
|
|
2010-03-02 12:10:49 -08:00
|
|
|
|
if (publish) {
|
|
|
|
|
_blogPostService.Publish(blogPost);
|
|
|
|
|
}
|
2010-03-01 17:13:36 -08:00
|
|
|
|
|
2010-03-02 12:10:49 -08:00
|
|
|
|
return true;
|
2010-03-01 17:13:36 -08:00
|
|
|
|
}
|
|
|
|
|
|
2010-03-02 12:10:49 -08:00
|
|
|
|
private bool MetaWeblogDeletePost(
|
|
|
|
|
string appkey,
|
|
|
|
|
string postId,
|
|
|
|
|
string userName,
|
|
|
|
|
string password,
|
|
|
|
|
bool publish) {
|
|
|
|
|
var user = _membershipService.ValidateUser(userName, password);
|
|
|
|
|
_authorizationService.CheckAccess(StandardPermissions.AccessFrontEnd, user, null);
|
|
|
|
|
|
|
|
|
|
var blogPost = _blogPostService.Get(Convert.ToInt32(postId), VersionOptions.Latest);
|
|
|
|
|
if (blogPost == null)
|
|
|
|
|
throw new ArgumentException();
|
|
|
|
|
|
|
|
|
|
_blogPostService.Delete(blogPost);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static XRpcStruct CreateBlogStruct(BlogPost blogPost, UrlHelper urlHelper) {
|
2010-03-02 16:04:01 -08:00
|
|
|
|
var url = urlHelper.AbsoluteAction(() => urlHelper.BlogPost(blogPost));
|
2010-03-02 12:10:49 -08:00
|
|
|
|
return new XRpcStruct()
|
|
|
|
|
.Set("postid", blogPost.Id)
|
|
|
|
|
.Set("dateCreated", blogPost.CreatedUtc)
|
|
|
|
|
.Set("title", blogPost.Title)
|
|
|
|
|
.Set("wp_slug", blogPost.Slug)
|
|
|
|
|
.Set("description", blogPost.Text)
|
|
|
|
|
.Set("link", url)
|
|
|
|
|
.Set("permaLink", url);
|
|
|
|
|
}
|
2010-03-01 17:13:36 -08:00
|
|
|
|
}
|
|
|
|
|
}
|