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
|
<?php
// $Id$
function blogapi_help($section) {
switch ($section) {
case 'admin/help#blogapi':
return t('This module adds support for several XML-RPC based blogging APIs. Specifically, it currently implements the %bloggerAPI, %metaweblogAPI, and most of the %moveabletype extensions. This allows users to contribute to drupal using external GUI applications, which can often offer richer functionality that online forms based editing', array('%bloggerAPI' => '<a href="http://www.blogger.com/developers/api/1_docs/">Blogger API</a>', '%metaweblogAPI' => '<a href="http://www.xmlrpc.com/metaWeblogApi">MetaWeblog API</a>', '%moveabletype' => '<a href="http://www.movabletype.org/docs/mtmanual_programmatic.html">Moveable Type API</a>'));
case 'admin/system/modules#description':
return t('Enable users to post using applications that support XML-RPC blog APIs');
}
}
function blogapi_xmlrpc() {
$methods = array('blogger.getUsersBlogs' => array('function' => 'blogapi_get_users_blogs'),
'blogger.getUserInfo' => array('function' => 'blogapi_get_user_info'),
'blogger.newPost' => array('function' => 'blogapi_new_post'),
'blogger.editPost' => array('function' => 'blogapi_edit_post'),
'blogger.deletePost' => array('function' => 'blogapi_delete_post'),
'blogger.getRecentPosts' => array('function' => 'blogapi_get_recent_posts'),
'metaWeblog.newPost' => array('function' => 'blogapi_new_post'),
'metaWeblog.editPost' => array('function' => 'blogapi_edit_post'),
'metaWeblog.getPost' => array('function' => 'blogapi_get_post'),
'metaWeblog.newMediaObject' => array('function' => 'blogapi_new_media_object'),
'metaWeblog.getCategories' => array('function' => 'blogapi_get_categories'),
'metaWeblog.getRecentPosts' => array('function' => 'blogapi_get_recent_posts'),
'mt.getCategoryList' => array('function' => 'blogapi_get_category_list'),
'mt.getPostCategories' => array('function' => 'blogapi_get_post_categories'),
'mt.setPostCategories' => array('function' => 'blogapi_set_post_categories')
);
return $methods;
}
/** api functions */
function blogapi_get_users_blogs($req_params) {
$params = blogapi_convert($req_params);
// Remove unused appkey from bloggerAPI.
if (count($params) == 6) {
$params = array_slice($params, 1);
}
$user = blogapi_validate_user($params[1], $params[2]);
if ($user->uid) {
$struct = new xmlrpcval(array('url' => new xmlrpcval(url('blog/' . $user->uid)),
'blogid' => new xmlrpcval($user->uid),
'blogName' => new xmlrpcval($user->name . "'s blog")),
'struct');
$resp = new xmlrpcval(array($struct), "array");
return new xmlrpcresp($resp);
}
else {
return blogapi_error($user);
}
}
function blogapi_get_user_info($req_params) {
$params = blogapi_convert($req_params);
$user = blogapi_validate_user($params[1], $params[2]);
if ($user->uid) {
$name = explode(' ', $user->realname ? $user->realname : $user->name, 2);
$struct = new xmlrpcval(array('userid' => new xmlrpcval($user->uid, 'string'),
'lastname' => new xmlrpcval($name[1], 'string'),
'firstname' => new xmlrpcval($name[0], 'string'),
'nickname' => new xmlrpcval($user->name, 'string'),
'email' => new xmlrpcval($user->mail, 'string'),
'url' => new xmlrpcval(url('blog/view/' . $user->uid), 'string')),
'struct');
return new xmlrpcresp($struct);
}
else {
return blogapi_error($user);
}
}
function blogapi_new_post($req_params) {
$params = blogapi_convert($req_params);
// Remove unused appkey from bloggerAPI.
if (count($params) == 6) {
$params = array_slice($params, 1);
}
$user = blogapi_validate_user($params[1], $params[2]);
if (!$user->uid) {
return blogapi_error($user);
}
$promote = variable_get("node_promote_blog", 0);
$comment = variable_get("node_comment_blog", 2);
$moderate = variable_get("node_moderate_blog", 0);
$revision = variable_get("node_revision_blog", 0);
// check for bloggerAPI vs. metaWeblogAPI
if (is_array($params[3])) {
$title = $params[3]['title'];
$body = $params[3]['description'];
}
else {
$title = blogapi_blogger_title($params[3]);
$body = $params[3];
}
if (!valid_input_data($title, $body)) {
return blogapi_error(t("Terminated request because of suspicious input data."));
}
$node = node_validate(array('type' => 'blog',
'uid' => $user->uid,
'name' => $user->name,
'title' => $title,
'body' => $body,
'status' => $params[4],
'promote' => $promote,
'comment' => $comment,
'moderate' => $moderate,
'revision' => $revision
), $error);
if (count($error) > 0) {
return blogapi_error($error);
}
if (!node_access("create", $node)) {
return blogapi_error(message_access());
}
$nid = node_save($node);
if ($nid) {
watchdog("special", "$node->type: added '$node->title' using blog API", l(t("view post"), "node/view/$nid"));
return new xmlrpcresp(new xmlrpcval($nid, 'string'));
}
return blogapi_error(t('error storing post'));
}
function blogapi_edit_post($req_params) {
$params = blogapi_convert($req_params);
if (count($params) == 6) {
$params = array_slice($params, 1);
}
$user = blogapi_validate_user($params[1], $params[2]);
if (!$user->uid) {
return blogapi_error($user);
}
$node = node_load(array('nid' => $params[0]));
if (!$node) {
return blogapi_error(message_na());
}
if (!node_access('update', $node)) {
return blogapi_error(message_access());
}
// check for bloggerAPI vs. metaWeblogAPI
if (is_array($params[3])) {
$title = $params[3]['title'];
$body = $params[3]['description'];
}
else {
$title = blogapi_blogger_title($params[3]);
$body = $params[3];
}
if (!valid_input_data($title, $body)) {
return blogapi_error(t("Terminated request because of suspicious input data."));
}
$node->title = $title;
$node->body = $body;
$node->status = $params[4];
$node = node_validate($node, $error);
if (count($error) > 0) {
return blogapi_error($error);
}
$terms = module_invoke('taxonomy', 'node_get_terms', $node->nid, 'tid');
foreach ($terms as $term) {
$node->taxonomy[] = $term->tid;
}
$nid = node_save($node);
if ($nid) {
watchdog("special", "$node->type: updated '$node->title' using blog API", l(t("view post"), "node/view/$nid"));
return new xmlrpcresp(new xmlrpcval(true, "boolean"));
}
return blogapi_error(t('error storing node'));
}
function blogapi_get_post($req_params) {
$params = blogapi_convert($req_params);
$user = blogapi_validate_user($params[1], $params[2]);
if (!$user->uid) {
return blogapi_error($user);
}
$node = node_load(array('nid' => $params[0]));
$blog = new xmlrpcval(array('userid' => new xmlrpcval($node->name, 'string'),
'dateCreated' => new xmlrpcval(iso8601_encode($node->created), "dateTime.iso8601"),
'title' => new xmlrpcval($node->title, 'string'),
'description' => new xmlrpcval($node->body, 'string'),
'postid' => new xmlrpcval($node->nid, 'string')),
'struct');
return new xmlrpcresp($blog);
}
function blogapi_delete_post($req_params) {
$params = blogapi_convert($req_params);
$user = blogapi_validate_user($params[2], $params[3]);
if (!$user->uid) {
return blogapi_error($user);
}
$ret = node_delete(array('nid' => $params[1], 'confirm' => 1));
return new xmlrpcresp(new xmlrpcval(true, "boolean"));
}
function blogapi_new_media_object($req_params) {
return blogapi_error('not implemented');
}
function blogapi_get_category_list($req_params) {
$vocabularies = module_invoke('taxonomy', 'get_vocabularies', 'blog', 'vid');
$categories = array();
if ($vocabularies) {
foreach ($vocabularies as $vocabulary) {
$terms = module_invoke('taxonomy', 'get_tree', $vocabulary->vid, 0, -1, 'tid');
foreach ($terms as $term) {
$term_name = $term->name;
foreach (module_invoke('taxonomy', 'get_parents', $term->tid, 'tid') as $parent) {
$term_name = $parent->name . '/' . $term_name;
}
$categories[] = new xmlrpcval(array('categoryName' => new xmlrpcval($term_name, 'string'),
'categoryId' => new xmlrpcval($term->tid, 'string')),
'struct');
}
}
}
return new xmlrpcresp(new xmlrpcval($categories, "array"));
}
function blogapi_get_post_categories($req_params) {
$params = blogapi_convert($req_params);
$user = blogapi_validate_user($params[1], $params[2]);
if (!$user->uid) {
return blogapi_error($user);
}
$terms = module_invoke('taxonomy', 'node_get_terms', $params[0], 'tid');
$categories = array();
foreach ($terms as $term) {
$term_name = $term->name;
foreach (module_invoke('taxonomy', 'get_parents', $term->tid, 'tid') as $parent) {
$term_name = $parent->name . '/' . $term_name;
}
$categories[] = new xmlrpcval(array('categoryName' => new xmlrpcval($term_name, 'string'),
'categoryId' => new xmlrpcval($term->tid, 'string'),
'isPrimary' => new xmlrpcval(true, 'boolean')),
'struct');
}
return new xmlrpcresp(new xmlrpcval($categories, "array"));
}
function blogapi_set_post_categories($req_params) {
$params = blogapi_convert($req_params);
$user = blogapi_validate_user($params[1], $params[2]);
if (!$user->uid) {
return blogapi_error($user);
}
$nid = $params[0];
$terms = array();
foreach ($params[3] as $category) {
$terms[] = $category['categoryId']->scalarval();
}
module_invoke('taxonomy', 'node_save', $nid, $terms);
return new xmlrpcresp(new xmlrpcval(true, 'boolean'));
}
function blogapi_get_recent_posts($req_params) {
$params = blogapi_convert($req_params);
// Remove unused appkey (from bloggerAPI).
if (count($params) == 5) {
$params = array_slice($params, 1);
}
$user = blogapi_validate_user($params[1], $params[2]);
if (!$user->uid) {
return blogapi_error($user);
}
$result = db_query_range("SELECT n.nid, n.title, n.body, n.created, u.name FROM {node} n, {users} u WHERE n.uid=u.uid AND n.type = 'blog' AND n.uid = %d ORDER BY n.created DESC", $user->uid, 0, $params[3]);
while ($blog = db_fetch_object($result)) {
$blogs[] = new xmlrpcval(array('userid' => new xmlrpcval($blog->name, 'string'),
'dateCreated' => new xmlrpcval(iso8601_encode($blog->created), "dateTime.iso8601"),
'content' => new xmlrpcval("<title>$blog->title</title>$blog->body", 'string'),
'title' => new xmlrpcval($blog->title, 'string'),
'description' => new xmlrpcval($blog->body, 'string'),
'postid' => new xmlrpcval($blog->nid, 'string')),
'struct');
}
return new xmlrpcresp(new xmlrpcval($blogs, "array"));
}
/** helper functions */
function blogapi_convert($params) {
$cparams = array();
$num_params= $params->getNumParams();
for ($i = 0; $i < $num_params; $i++) {
$sn = $params->getParam($i);
$cparams[] = $sn->getval();
}
return $cparams;
}
function blogapi_error($message) {
global $xmlrpcusererr;
if (is_array($message)) {
$message = implode('', $message);
}
return new xmlrpcresp(0, $xmlrpcusererr + 1, strip_tags($message));
}
function blogapi_validate_user($username, $password) {
global $user;
$user = user_load(array('name' => $username, 'pass' => $password, 'status' => 1));
if ($user->uid) {
if (user_access('maintain personal blog')) {
return $user;
}
else {
return message_access();
}
}
else {
return t('Wrong username or password.');
}
}
function blogapi_blogger_title(&$contents) {
if (eregi("<title>([^<]*)</title>", $contents, $title)) {
$title = strip_tags($title[0]);
$contents = ereg_replace("<title>[^<]*</title>", "", $contents);
}
else {
list($title, $rest) = explode("\n", $contents, 2);
}
return $title;
}
?>
|