diff options
author | Arno Richter <oelna@oelna.de> | 2022-12-13 20:52:51 +0100 |
---|---|---|
committer | Arno Richter <oelna@oelna.de> | 2022-12-13 20:52:51 +0100 |
commit | b7f338941dcac2a21df2cc4e7fada572616997a2 (patch) | |
tree | 8e59f7c4291902f085b543357de097d0912fa27c | |
parent | 24e62005fc0ad4496032206ec0538b3299e4cedb (diff) | |
download | microblog-b7f338941dcac2a21df2cc4e7fada572616997a2.tar.gz microblog-b7f338941dcac2a21df2cc4e7fada572616997a2.tar.bz2 microblog-b7f338941dcac2a21df2cc4e7fada572616997a2.zip |
fixes and final touches for the XMLRPC micro.blog API
-rw-r--r-- | xmlrpc.php | 244 |
1 files changed, 113 insertions, 131 deletions
@@ -8,6 +8,7 @@ if(empty($request_xml)) { exit('XML-RPC server accepts POST requests only.'); } // load config require_once(__DIR__.DIRECTORY_SEPARATOR.'config.php'); +$logfile = __DIR__.DS.'log.txt'; if(!function_exists('str_starts_with')) { function str_starts_with($haystack, $needle) { @@ -98,6 +99,89 @@ function make_post($post, $method='metaWeblog') { } } +function mw_get_users_blogs($method_name, $args) { + global $config; + + list($_, $username, $password) = $args; + + if(!check_credentials($username, $password)) { + return [ + 'faultCode' => 403, + 'faultString' => 'Incorrect username or password.' + ]; + } + + $bloginfo = [ + 'blogid' => '1', + 'url' => $config['url'], + 'blogName' => (empty($config['microblog_account']) ? "" : $config['microblog_account'] . "'s ").' microblog', + ]; + + return $bloginfo; +} + +function mw_get_categories($method_name, $args) { + + list($_, $username, $password) = $args; + + if(!check_credentials($username, $password)) { + return [ + 'faultCode' => 403, + 'faultString' => 'Incorrect username or password.' + ]; + } + + // we don't support categories, so only return a fake one + if($method_name == 'microblog.getCategories') { + $categories = [ + /* + [ + 'id' => '1', + 'name' => 'default', + ] + */ + ]; + } else { + $categories = [ + /* + [ + 'description' => 'Default', + 'htmlUrl' => '', + 'rssUrl' => '', + 'title' => 'default', + 'categoryid' => '1', + ] + */ + ]; + } + + return $categories; +} + +function mw_get_user_info($method_name, $args) { + global $config; + + list($_, $username, $password) = $args; + + if(!check_credentials($username, $password)) { + return [ + 'faultCode' => 403, + 'faultString' => 'Incorrect username or password.' + ]; + } + + $userinfo = [ + 'userid' => '1', + 'firstname' => '', + 'lastname' => '', + 'nickname' => $config['microblog_account'], + 'email' => '', + 'url' => $config['url'], + ]; + + return $userinfo; +} + function mw_get_recent_posts($method_name, $args) { list($_, $username, $password, $amount) = $args; @@ -117,24 +201,10 @@ function mw_get_recent_posts($method_name, $args) { $amount = min($amount, 200); // cap the max available posts at 200 (?) $posts = db_select_posts(null, $amount, 'asc', $offset); - var_dump($posts); if(empty($posts)) return []; - if($method_name == 'microblog.getPosts') { - // currently the same as metaWeblog - /* - $mw_posts = array_map( - function($posts) { - return make_post($posts, 'microblog'); - }, - $posts - ); - */ - // https://stackoverflow.com/a/22735187/3625228 - $mw_posts = array_map('make_post', $posts, array_fill(0, count($posts), $method_name)); - } else { - $mw_posts = array_map('make_post', $posts); - } + // call make_post() on all items + $mw_posts = array_map('make_post', $posts, array_fill(0, count($posts), $method_name)); return $mw_posts; } @@ -167,35 +237,6 @@ function mw_get_post($method_name, $args) { } } -function mw_delete_post($method_name, $args) { - - if($method_name == 'microblog.deletePost') { - list($post_id, $username, $password) = $args; - } else { - // blogger.deletePost - list($_, $post_id, $username, $password, $_) = $args; - } - - if(!check_credentials($username, $password)) { - return [ - 'faultCode' => 403, - 'faultString' => 'Incorrect username or password.' - ]; - } - - $success = db_delete($post_id); - if($success > 0) { - rebuild_feeds(); - - return true; - } else { - return [ - 'faultCode' => 400, - 'faultString' => 'Could not delete post.' - ]; - } -} - function mw_new_post($method_name, $args) { // blog_id, unknown, unknown, array of post content, unknown @@ -286,7 +327,7 @@ function mw_edit_post($method_name, $args) { // 'post_url' => $content['link'], ]; - if(empty($content['dateCreated'])) { + if(!empty($content['dateCreated'])) { $post['post_timestamp'] = $content['dateCreated']->timestamp; } } @@ -305,45 +346,15 @@ function mw_edit_post($method_name, $args) { } } -function mw_get_categories($method_name, $args) { - - list($_, $username, $password) = $args; - - if(!check_credentials($username, $password)) { - return [ - 'faultCode' => 403, - 'faultString' => 'Incorrect username or password.' - ]; - } +function mw_delete_post($method_name, $args) { - // we don't support categories, so only return a fake one - if($method_name == 'microblog.getCategories') { - $categories = [ - [ - 'id' => '1', - 'name' => 'default', - ] - ]; + if($method_name == 'microblog.deletePost') { + list($post_id, $username, $password) = $args; } else { - $categories = [ - [ - 'description' => 'Default', - 'htmlUrl' => '', - 'rssUrl' => '', - 'title' => 'default', - 'categoryid' => '1', - ] - ]; + // blogger.deletePost + list($_, $post_id, $username, $password, $_) = $args; } - return $categories; -} - -function mw_get_users_blogs($method_name, $args) { - global $config; - - list($_, $username, $password) = $args; - if(!check_credentials($username, $password)) { return [ 'faultCode' => 403, @@ -351,81 +362,52 @@ function mw_get_users_blogs($method_name, $args) { ]; } - $bloginfo = [ - 'blogid' => '1', - 'url' => $config['url'], - 'blogName' => (empty($config['microblog_account']) ? "" : $config['microblog_account'] . "'s ").' microblog', - ]; - - return $bloginfo; -} - -function mw_get_user_info($method_name, $args) { - global $config; - - list($_, $username, $password) = $args; + $success = db_delete($post_id); + if($success > 0) { + rebuild_feeds(); - if(!check_credentials($username, $password)) { + return true; + } else { return [ - 'faultCode' => 403, - 'faultString' => 'Incorrect username or password.' + 'faultCode' => 400, + 'faultString' => 'Could not delete post.' ]; } - - $userinfo = [ - 'userid' => '1', - 'firstname' => '', - 'lastname' => '', - 'nickname' => $config['microblog_account'], - 'email' => '', - 'url' => $config['url'], - ]; - - return $userinfo; } -// micro.blog API methods -// https://help.micro.blog/t/micro-blog-xml-rpc-api/108 - -// currently, the functions can be reused directly (disabled) -/* -use function mw_get_recent_posts as mb_get_posts; -use function mw_get_post as mb_get_post; -use function mw_delete_post as mb_delete_post; -use function mw_get_categories as mb_get_categories; -use function mw_new_post as mb_new_post; -use function mw_edit_post as mb_edit_post; -*/ -// use function mw_new_mediaobject as mb_new_mediaobject; // unsupported - // https://codex.wordpress.org/XML-RPC_MetaWeblog_API // https://community.devexpress.com/blogs/theprogressbar/metablog.ashx // idea: http://www.hixie.ch/specs/pingback/pingback#TOC3 $server = xmlrpc_server_create(); xmlrpc_server_register_method($server, 'demo.sayHello', 'say_hello'); + xmlrpc_server_register_method($server, 'blogger.getUsersBlogs', 'mw_get_users_blogs'); xmlrpc_server_register_method($server, 'blogger.getUserInfo', 'mw_get_user_info'); +xmlrpc_server_register_method($server, 'blogger.deletePost', 'mw_delete_post'); + xmlrpc_server_register_method($server, 'metaWeblog.getCategories', 'mw_get_categories'); xmlrpc_server_register_method($server, 'metaWeblog.getRecentPosts', 'mw_get_recent_posts'); -xmlrpc_server_register_method($server, 'metaWeblog.getPosts', 'mw_get_recent_posts'); // convenience xmlrpc_server_register_method($server, 'metaWeblog.newPost', 'mw_new_post'); xmlrpc_server_register_method($server, 'metaWeblog.editPost', 'mw_edit_post'); xmlrpc_server_register_method($server, 'metaWeblog.getPost', 'mw_get_post'); -xmlrpc_server_register_method($server, 'blogger.deletePost', 'mw_delete_post'); -xmlrpc_server_register_method($server, 'metaWeblog.deletePost', 'mw_delete_post'); // does this exist? -// xmlrpc_server_register_method($server, 'metaWeblog.newMediaObject', 'mw_new_mediaobject'); // todo +// xmlrpc_server_register_method($server, 'metaWeblog.newMediaObject', 'mw_new_mediaobject'); + +// non-standard convenience? +xmlrpc_server_register_method($server, 'metaWeblog.getPosts', 'mw_get_recent_posts'); +xmlrpc_server_register_method($server, 'metaWeblog.deletePost', 'mw_delete_post'); -// micro.blog methods +// micro.blog API methods (currently just using the metaWeblog functions) +// https://help.micro.blog/t/micro-blog-xml-rpc-api/108 +xmlrpc_server_register_method($server, 'microblog.getCategories', 'mw_get_categories'); xmlrpc_server_register_method($server, 'microblog.getPosts', 'mw_get_recent_posts'); xmlrpc_server_register_method($server, 'microblog.getPost', 'mw_get_post'); xmlrpc_server_register_method($server, 'microblog.newPost', 'mw_new_post'); xmlrpc_server_register_method($server, 'microblog.editPost', 'mw_edit_post'); xmlrpc_server_register_method($server, 'microblog.deletePost', 'mw_delete_post'); -xmlrpc_server_register_method($server, 'microblog.getCategories', 'mw_get_categories'); -// xmlrpc_server_register_method($server, 'microblog.newMediaObject', 'mb_new_mediaobject'); +// xmlrpc_server_register_method($server, 'microblog.newMediaObject', 'mw_new_mediaobject'); +// micro.blog pages are not supported /* -// pages are not supported xmlrpc_server_register_method($server, 'microblog.getPages', 'say_hello'); xmlrpc_server_register_method($server, 'microblog.getPage', 'say_hello'); xmlrpc_server_register_method($server, 'microblog.newPage', 'say_hello'); @@ -441,6 +423,6 @@ $response = xmlrpc_server_call_method($server, $request_xml, null, [ if($response) { header('Content-Type: text/xml; charset=utf-8'); - // error_log($request_xml."\n\n".$response."\n", 3, __DIR__.'/log.txt'); + // error_log($request_xml."\n\n".$response."\n\n", 3, $logfile); echo($response); } |