aboutsummaryrefslogtreecommitdiff
path: root/xmlrpc.php
blob: 3682b1a44a915c784090c89be6b3ccd5ee9c4e1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<?php

$request_xml = file_get_contents("php://input");

// check prerequisites
if(!function_exists('xmlrpc_server_create')) { exit('No XML-RPC support detected!'); }
if(empty($request_xml)) { exit('XML-RPC server accepts POST requests only.'); }

// load config
require_once(__DIR__.DIRECTORY_SEPARATOR.'config.php');

if(!function_exists('str_starts_with')) {
	function str_starts_with($haystack, $needle) {
		if(empty($needle)) return true;
		return mb_substr($haystack, 0, mb_strlen($needle)) === $needle;
	}
}

function check_credentials($username, $password) {
	global $config;

	$xmlrpc_auth = $config['admin_pass'];
	if(!empty($config['app_token'])) {
		$xmlrpc_auth = $config['app_token'];
	}

	if($username == $config['admin_user'] && $password == $xmlrpc_auth) {
		return true;
	} else {
		return false;
	}
}

function say_hello($method_name, $args) {
	return 'Hello';
}

function make_post($post, $method='metaWeblog') {
	global $config;

	$date_created = date('Y-m-d\TH:i:s', $post['post_timestamp']).$config['local_time_offset'];
	$date_created_gmt = gmdate('Y-m-d\TH:i:s', $post['post_timestamp']).'Z';
	if(!empty($post['post_edited']) && !is_null($post['post_edited'])) {
		$date_modified = date('Y-m-d\TH:i:s', $post['post_edited']).$config['local_time_offset'];
		$date_modified_gmt = gmdate('Y-m-d\TH:i:s', $post['post_edited']).'Z';
	} else {
		$date_modified = date('Y-m-d\TH:i:s', 0).$config['local_time_offset'];
		$date_modified = null;
		$date_modified_gmt = gmdate('Y-m-d\TH:i:s', 0).'Z';
	}

	@xmlrpc_set_type($date_created, 'datetime');
	@xmlrpc_set_type($date_created_gmt, 'datetime');
	@xmlrpc_set_type($date_modified, 'datetime');
	@xmlrpc_set_type($date_modified_gmt, 'datetime');

	if(str_starts_with($method, 'microblog')) {
		// convert the post format to a microblog post
		// similar to metaWeblog.recentPosts but with offset parameter for paging,
		// consistent field names
		$mb_post = [
			'id' => (int) $post['id'],
			'date_created' => $date_created,
			'date_modified' => $date_modified,
			'permalink' => $config['url'].'/'.$post['id'],
			'title' => '',
			'description' => ($post['post_content']),
			'categories' => [],
			'post_status' => 'published',
			'author' => [
				'name' => $config['microblog_account'],
				'username' => $config['admin_user']
			]
		];

		return $mb_post;
	} else {
		// convert the post format to a standard metaWeblog post
		$mw_post = [
			'postid' => (int) $post['id'],
			'title' => '',
			'description' => ($post['post_content']), // Post content
			'link' => $config['url'].'/'.$post['id'], // Post URL
			// string userid†: ID of post author.
			'dateCreated' => $date_created,
			'date_created_gmt' => $date_created_gmt,
			'date_modified' => $date_modified,
			'date_modified_gmt' => $date_modified_gmt,
			// string wp_post_thumbnail†
			'permalink' => $config['url'].'/'.$post['id'], // Post URL, equivalent to link.
			'categories' => [], // Names of categories assigned to the post.
			'mt_keywords' => '', // Names of tags assigned to the post.
			'mt_excerpt' => '',
			'mt_text_more' => '', // Post "Read more" text.
		];

		return $mw_post;
	}
}

function mw_get_recent_posts($method_name, $args) {

	list($_, $username, $password, $amount) = $args;
	$offset = 0;
	if($method_name == 'microblog.getPosts' && !empty($args[4])) {
		$offset = $args[4];
	}

	if(!check_credentials($username, $password)) {
		return [
			'faultCode' => 403,
			'faultString' => 'Incorrect username or password.'
		];
	}

	if(!$amount) $amount = 25;
	$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);
	}

	return $mw_posts;
}

function mw_get_post($method_name, $args) {

	list($post_id, $username, $password) = $args;

	if(!check_credentials($username, $password)) {
		return [
			'faultCode' => 403,
			'faultString' => 'Incorrect username or password.'
		];
	}

	$post = db_select_post($post_id);
	if($post) {
		if($method_name == 'microblog.getPost') {
			$mw_post = make_post($post, $method_name);
		} else {
			$mw_post = make_post($post);
		}
		
		return $mw_post;
	} else {
		return [
			'faultCode' => 400,
			'faultString' => 'Could not fetch post.'
		];
	}
}

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
	list($blog_id, $username, $password, $content, $_) = $args;

	if(!check_credentials($username, $password)) {
		return [
			'faultCode' => 403,
			'faultString' => 'Incorrect username or password.'
		];
	}

	if($method_name == 'microblog.newPost') {
		$post = [
			// 'post_title' => $content['title'],
			'post_content' => $content['description'],
			'post_timestamp' => time(),
			// 'post_categories' => $content['categories'],
			// 'post_status' => $content['post_status'],
		];

		// use a specific timestamp, if provided
		if(isset($content['date_created'])) {
			$post['post_timestamp'] = $content['date_created']->timestamp;
		}
	} else {
		$post = [
			// 'post_hp' => $content['flNotOnHomePage'],
			'post_timestamp' => time(),
			// 'post_title' => $content['title'],
			'post_content' => $content['description'],
			// 'post_url' => $content['link'],
		];

		// use a specific timestamp, if provided
		if(isset($content['dateCreated'])) {
			$post['post_timestamp'] = $content['dateCreated']->timestamp;
		}
	}

	$insert_id = db_insert($post['post_content'], $post['post_timestamp']);
	if($insert_id && $insert_id > 0) {
		// success
		rebuild_feeds();

		return (int) $insert_id;
	} else {
		// insert failed
		// error codes: https://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php
		// more error codes? https://github.com/zendframework/zend-xmlrpc/blob/master/src/Fault.php
		return [
			'faultCode' => 400,
			'faultString' => 'Could not create post.'
		];
	}
}

function mw_edit_post($method_name, $args) {

	// post_id, unknown, unknown, array of post content
	list($post_id, $username, $password, $content, $_) = $args;

	if(!check_credentials($username, $password)) {
		return [
			'faultCode' => 403,
			'faultString' => 'Incorrect username or password.'
		];
	}

	if($method_name == 'microblog.editPost') {
		$post = [
			// 'post_title' => $content['title'],
			'post_content' => $content['description'],
			'post_timestamp' => null,
			// 'post_categories' => $content['categories'],
			// 'post_status' => $content['post_status'],
		];

		if(!empty($content['date_created'])) {
			$post['post_timestamp'] = $content['date_created']->timestamp;
		}
	} else {
		$post = [
			// 'post_hp' => $content['flNotOnHomePage'],
			// 'post_title' => $content['title'],
			'post_timestamp' => null,
			'post_content' => $content['description'],
			// 'post_url' => $content['link'],
		];

		if(empty($content['dateCreated'])) {
			$post['post_timestamp'] = $content['dateCreated']->timestamp;
		}
	}

	$update = db_update($post_id, $post['post_content'], $post['post_timestamp']);
	if($update && $update > 0) {
		// success
		rebuild_feeds();

		return true;
	} else {
		return [
			'faultCode' => 400,
			'faultString' => 'Could not write post update.'
		];
	}
}

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_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_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;
}

// 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, '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

// micro.blog methods
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');

/*
// 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');
xmlrpc_server_register_method($server, 'microblog.editPage', 'say_hello');
xmlrpc_server_register_method($server, 'microblog.deletePage', 'say_hello');
*/

// https://docstore.mik.ua/orelly/webprog/pcook/ch12_08.htm
$response = xmlrpc_server_call_method($server, $request_xml, null, [
	'escaping' => 'markup',
	'encoding' => 'UTF-8'
]);

if($response) {
	header('Content-Type: text/xml; charset=utf-8');
	// error_log($request_xml."\n\n".$response."\n", 3, __DIR__.'/log.txt');
	echo($response);
}