-
Notifications
You must be signed in to change notification settings - Fork 2
/
xmlrpc.php
368 lines (298 loc) · 10.5 KB
/
xmlrpc.php
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
<?php
include_once("settings.php");
include_once("libs/xmlrpcs.inc");
include_once("libs/xmlrpc.inc");
$xmlrpc_internalencoding = "UTF8";
function _error($errcode, $errstring) {
global $xmlrpcerruser;
return new xmlrpcresp(0, $xmlrpcerruser + $errcode, $errstring);
}
function _login($su, $sp) {
if (isset($su) == false || ($su->scalartyp() != "string") ||
isset($sp) == false || ($sp->scalartyp() != "string")) {
return _error(3, "Incorrect parameters");
}
global $blog_baseurl, $admin_name, $admin_email, $admin_password;
$username = $su->scalarval();
$password = $sp->scalarval();
if ($username != $admin_name || md5($password) != $admin_password) {
return _error(6, "Login failed");
}
return TRUE;
}
function _blogger_extractTitle($body) {
return _blogger_matchTag('title',$body);
}
function _blogger_extractCategory($body) {
return _blogger_matchTag('category',$body);
}
function _blogger_matchTag($tag, $body) {
if (preg_match("/<" . $tag .">(.+?)<\/".$tag.">/is",$body,$match))
return $match[1];
else
return "";
}
function _blogger_removeSpecialTags($body) {
$body = preg_replace("/<title>(.+?)<\/title>/","",$body);
$body = preg_replace("/<category>(.+?)<\/category>/","",$body);
return trim($body);
}
function _blogger_specialTags($entry) {
$result = "<title>". $entry['title']."</title>";
$result .= "<category>".$entry['category']."</category>";
return $result;
}
function blogger_newPost($params) {
$su = $params->getParam(2);
$sp = $params->getParam(3);
$sc = $params->getParam(4);
$r = _login($su, $sp);
if ($r !== TRUE) {
return $r;
}
$content = $sc->scalarval();
$title = _blogger_extractTitle($content);
$category = _blogger_extractCategory($content);
$body = _blogger_removeSpecialTags($content);
$blogid = Entry::createEntry($title, $body, time(), $category, array());
return new xmlrpcresp(new xmlrpcval($blogid, "string"));
}
function blogger_editPost($params) {
$sid = $params->getParam(1);
$su = $params->getParam(2);
$sp = $params->getParam(3);
$sc = $params->getParam(4);
$r = _login($su, $sp);
if ($r !== TRUE) {
return $r;
}
$content = $sc->scalarval();
$title = _blogger_extractTitle($content);
$category = _blogger_extractCategory($content);
$body = _blogger_removeSpecialTags($content);
$blogid = $sid->scalarval();
$e = Entry::getEntry($blogid);
$date = $e->date;
Entry::editEntry($blogid, $title, $body, $date, $category);
return new xmlrpcresp(new xmlrpcval(1, "boolean"));
}
function blogger_getPost($params) {
$sid = $params->getParam(1);
$su = $params->getParam(2);
$sp = $params->getParam(3);
$r = _login($su, $sp);
if ($r !== TRUE) {
return $r;
}
$blogid = $sid->scalarval();
$entry = Entry::getEntry($blogid);
$content = _blogger_specialTags($entry) . $e->getBody();
$entrystruct = new xmlrpcval(array("content" => new xmlrpcval($content, "string"),
"userid" => new xmlrpcval($admin_name, "string"),
"postid" => new xmlrpcval($entry->entryId, "string"),
"dateCreated" => new xmlrpcval(iso8601_encode(strtotime($entry->date)), "dateTime.iso8601")
), "struct");
return $entrystruct;
}
function blogger_deletePost($params) {
$sid = $params->getParam(1);
$su = $params->getParam(2);
$sp = $params->getParam(3);
$r = _login($su, $sp);
if ($r !== TRUE) {
return $r;
}
$blogid = $sid->scalarval();
Entry::deleteEntry($blogid);
return new xmlrpcresp(new xmlrpcval(1, "boolean"));
}
function blogger_getRecentPosts($params) {
$su = $params->getParam(2);
$sp = $params->getParam(3);
$sn = $params->getParam(4);
$num = $sn->scalarval();
$r = _login($su, $sp);
if ($r !== TRUE) {
return $r;
}
$structarray = array();
$entries = Entry::getRecentEntries($num);
foreach($entries as $e) {
$content = _blogger_specialTags($e) . $e->getBody();
$entrystruct = new xmlrpcval(array("content" => new xmlrpcval($content, "string"),
"userid" => new xmlrpcval($admin_name, "string"),
"postid" => new xmlrpcval($e->entryId, "string"),
"dateCreated" => new xmlrpcval(iso8601_encode(strtotime($e->date)), "dateTime.iso8601")
), "struct");
$structarray[] = $entrystruct;
}
return new xmlrpcresp(new xmlrpcval($structarray, "array"));
}
function blogger_getUsersBlogs($params) {
$su = $params->getParam(1);
$sp = $params->getParam(2);
$r = _login($su, $sp);
if ($r !== TRUE) {
return $r;
}
global $blog_baseurl, $blog_name;
$structarray = array();
$blogstruct = new xmlrpcval(array("url" => new xmlrpcval($blog_baseurl, "string"),
"blogid" => new xmlrpcval($blog_name, "string"),
"blogName" => new xmlrpcval($blog_name, "string")
), "struct");
$structarray[] = $blogstruct;
return new xmlrpcresp(new xmlrpcval($structarray, "array"));
}
function blogger_getUserInfo($params) {
$su = $params->getParam(1);
$sp = $params->getParam(2);
$r = _login($su, $sp);
if ($r !== TRUE) {
return $r;
}
global $blog_baseurl, $admin_name, $admin_email, $admin_password;
$result_struct = new xmlrpcval(array("nickname" => new xmlrpcval($admin_name, "string"),
"userid" => new xmlrpcval($admin_name, "string"),
"url" => new xmlrpcval($blog_baseurl, "string"),
"email" => new xmlrpcval($admin_email, "string"),
"lastname" => new xmlrpcval("", "string"),
"firstname" => new xmlrpcval($admin_name, "string"),
),"struct");
return new xmlrpcresp($result_struct);
}
function metaWeblog_newPost($params) {
// $sb = $params->getParam(1);
$su = $params->getParam(1);
$sp = $params->getParam(2);
$ts = $params->getParam(3);
// $spb = $params->getParam(4);
$r = _login($su, $sp);
if ($r !== TRUE) {
return $r;
}
$item = $ts->getval();
$title = $item['title'];
$content = $item['description'];
$category = $item['category'];
$body = $content;
$blogid = Entry::createEntry($title, $body, time(), $category, array(), "html");
return new xmlrpcresp(new xmlrpcval($blogid, "string"));
}
function metaWeblog_editPost($params) {
$sid = $params->getParam(0);
$su = $params->getParam(1);
$sp = $params->getParam(2);
$ts = $params->getParam(3);
// $spb = $params->getParam(4);
$r = _login($su, $sp);
if ($r !== TRUE) {
return $r;
}
$item = $ts->getval();
$title = $item['title'];
$body = $item['description'];
$category = $item['category'];
$blogid = $sid->scalarval();
$ret = Entry::editEntry($blogid, $title, $body, time(), $category);
return new xmlrpcresp(new xmlrpcval($ret, "string"));
}
function metaWeblog_getPost($params) {
$sid = $params->getParam(0);
$su = $params->getParam(1);
$sp = $params->getParam(2);
$r = _login($su, $sp);
if ($r !== TRUE) {
return $r;
}
$blogid = $sid->scalarval();
$e = Entry::getEntry($blogid);
$content = $e->getBody();
$categories = array ($e->category->name);
$entrystruct = new xmlrpcval(array("dateCreated" => new xmlrpcval(iso8601_encode(strtotime($e->date)), "dateTime.iso8601"),
"userid" => new xmlrpcval($admin_name, "string"),
"postid" => new xmlrpcval($e->entryId, "string"),
"title" => new xmlrpcval($e->title, "string"),
"link" => new xmlrpcval($e->getHref(), "string"),
"categories" => new xmlrpcval($categories, "array"),
"description" => new xmlrpcval($$content, "string")
), "struct");
return $entrystruct;
}
function metaWeblog_newMediaObject($params) {
$sid = $params->getParam(0);
$su = $params->getParam(1);
$sp = $params->getParam(2);
$ts = $params->getParam(3);
$r = _login($su, $sp);
if ($r !== TRUE) {
return $r;
}
$obj = $ts->getval();
if (!isset ($item['name']) || !isset ($item['type'])||!isset ($item['bits']) ) {
// not enough parameter
}
$name = $item['name'];
$type = $item['type'];
$bits = $item['bits'];
return new xmlrpcresp(new xmlrpcval("not yet", "string"));
}
function metaWeblog_getCategories($params) {
// $sid = $params->getParam(0);
$su = $params->getParam(1);
$sp = $params->getParam(2);
$r = _login($su, $sp);
if ($r !== TRUE) {
return $r;
}
$categories = Category::getCategoryList();
foreach($categories as $c) {
$categorystruct = new xmlrpcval(array("Description" => $c->name,
"htmlUrl" => $c->getHref(),
"rssUrl" => $c->getRssHref()),
"struct");
$structarray[] = $categorystruct;
}
return new xmlrpcresp(new xmlrpcval($structarray, "array"));
}
function metawebLog_getRecentPosts($params) {
$su = $params->getParam(1);
$sp = $params->getParam(2);
$sn = $params->getParam(3);
$num = $sn->scalarval();
$r = _login($su, $sp);
if ($r !== TRUE) {
return $r;
}
$structarray = array();
$entries = Entry::getRecentEntries($num);
foreach($entries as $e) {
$content = _blogger_specialTags($e) . $e->getBody();
$categories = array ($e->category->name);
$entrystruct = new xmlrpcval(array("dateCreated" => new xmlrpcval(iso8601_encode(strtotime($e->date)), "dateTime.iso8601"),
"userid" => new xmlrpcval($admin_name, "string"),
"postid" => new xmlrpcval($e->entryId, "string"),
"title" => new xmlrpcval($e->title, "string"),
"link" => new xmlrpcval($e->getHref(), "string"),
"categories" => new xmlrpcval($categories, "array"),
"description" => new xmlrpcval($content, "string")
), "struct");
$structarray[] = $entrystruct;
}
return new xmlrpcresp(new xmlrpcval($structarray, "array"));
}
$s = new xmlrpc_server(array("metaWeblog.newPost" => array("function" => "metaWeblog_newPost"),
"metaWeblog.editPost" => array("function" => "metaWeblog_editPost"),
"metaWeblog.deletePost" => array("function" => "metaWeblog_deletePost"),
"metaWeblog.newMediaObject" => array("function" => "metaWeblog_newMediaObject"),
"metaWeblog.getCategories" => array("function" => "metaWeblog_getCategories"),
"metaWeblog.getRecentPosts" => array("function" => "metaweblog_getRecentPosts"),
"blogger.getPost" => array("function" => "blogger_getPost"),
"blogger.newPost" => array("function" => "blogger_newPost"),
"blogger.editPost" => array("function" => "blogger_editPost"),
"blogger.deletePost" => array("function" => "blogger_deletePost"),
"blogger.getRecentPosts" => array("function" => "blogger_getRecentPosts"),
"blogger.getUsersBlogs" => array("function" => "blogger_getUsersBlogs"),
"blogger.getUserInfo" => array("function" => "blogger_getUserInfo")));
# vim: ts=8 sw=2 sts=2 noet
?>