aboutsummaryrefslogtreecommitdiff
path: root/xmlrpc.php
blob: 9438592647194bff7ad106e5a5b3595a551bd073 (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
<?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');

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 mw_make_post($post) {
	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']) && $post['post_edited'] > 0) {
		$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_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');

	// 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;

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

	if(!$amount) $amount = 25;

	$posts = db_select_posts(time()+10, $amount);
	$mw_posts = array_map('mw_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) {
		$mw_post = mw_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 == 'blogger.deletePost') {
		list($_, $post_id, $username, $password, $_) = $args;
	} else {
		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.'
		];
	}

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

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

	$post = [
		// 'post_hp' => $content['flNotOnHomePage'],
		'post_timestamp' => $content['dateCreated']->timestamp,
		// 'post_title' => $content['title'],
		'post_content' => $content['description'],
		// 'post_url' => $content['link'],
	];

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

// 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.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', 'say_hello'); // todo

// 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');
	echo($response);
}