webmaster@1
|
1 <?php |
webmaster@1
|
2 // $Id: blogapi.module,v 1.115.2.1 2008/02/07 20:11:02 goba Exp $ |
webmaster@1
|
3 |
webmaster@1
|
4 /** |
webmaster@1
|
5 * @file |
webmaster@1
|
6 * Enable users to post using applications that support XML-RPC blog APIs. |
webmaster@1
|
7 */ |
webmaster@1
|
8 |
webmaster@1
|
9 /** |
webmaster@1
|
10 * Implementation of hook_help(). |
webmaster@1
|
11 */ |
webmaster@1
|
12 function blogapi_help($path, $arg) { |
webmaster@1
|
13 switch ($path) { |
webmaster@1
|
14 case 'admin/help#blogapi': |
webmaster@1
|
15 $output = '<p>'. t("The Blog API module allows your site's users to access and post to their blogs from external blogging clients. External blogging clients are available for a wide range of desktop operating systems, and generally provide a feature-rich graphical environment for creating and editing posts.") .'</p>'; |
webmaster@1
|
16 $output .= '<p>'. t('<a href="@ecto-link">Ecto</a>, a blogging client available for both Mac OS X and Microsoft Windows, can be used with Blog API. Blog API also supports <a href="@blogger-api">Blogger API</a>, <a href="@metaweblog-api">MetaWeblog API</a>, and most of the <a href="@movabletype-api">Movable Type API</a>. Blogging clients and other services (e.g. <a href="@flickr">Flickr\'s</a> "post to blog") that support these APIs may also be compatible.', array('@ecto-link' => url('http://infinite-sushi.com/software/ecto/'), '@blogger-api' => url('http://www.blogger.com/developers/api/1_docs/'), '@metaweblog-api' => url('http://www.xmlrpc.com/metaWeblogApi'), '@movabletype-api' => url('http://www.movabletype.org/docs/mtmanual_programmatic.html'), '@flickr' => url('http://www.flickr.com'))) .'</p>'; |
webmaster@1
|
17 $output .= '<p>'. t('Select the content types available to external clients on the <a href="@blogapi-settings">Blog API settings page</a>. If supported and available, each content type will be displayed as a separate "blog" by the external client.', array('@blogapi-settings' => url('admin/settings/blogapi'))) .'</p>'; |
webmaster@1
|
18 $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@blogapi">Blog API module</a>.', array('@blogapi' => url('http://drupal.org/handbook/modules/blogapi/'))) .'</p>'; |
webmaster@1
|
19 return $output; |
webmaster@1
|
20 } |
webmaster@1
|
21 } |
webmaster@1
|
22 |
webmaster@1
|
23 /** |
webmaster@1
|
24 * Implementation of hook_perm(). |
webmaster@1
|
25 */ |
webmaster@1
|
26 function blogapi_perm() { |
webmaster@1
|
27 return array('administer content with blog api'); |
webmaster@1
|
28 } |
webmaster@1
|
29 |
webmaster@1
|
30 /** |
webmaster@1
|
31 * Implementation of hook_xmlrpc(). |
webmaster@1
|
32 */ |
webmaster@1
|
33 function blogapi_xmlrpc() { |
webmaster@1
|
34 return array( |
webmaster@1
|
35 array( |
webmaster@1
|
36 'blogger.getUsersBlogs', |
webmaster@1
|
37 'blogapi_blogger_get_users_blogs', |
webmaster@1
|
38 array('array', 'string', 'string', 'string'), |
webmaster@1
|
39 t('Returns a list of blogs to which an author has posting privileges.')), |
webmaster@1
|
40 array( |
webmaster@1
|
41 'blogger.getUserInfo', |
webmaster@1
|
42 'blogapi_blogger_get_user_info', |
webmaster@1
|
43 array('struct', 'string', 'string', 'string'), |
webmaster@1
|
44 t('Returns information about an author in the system.')), |
webmaster@1
|
45 array( |
webmaster@1
|
46 'blogger.newPost', |
webmaster@1
|
47 'blogapi_blogger_new_post', |
webmaster@1
|
48 array('string', 'string', 'string', 'string', 'string', 'string', 'boolean'), |
webmaster@1
|
49 t('Creates a new post, and optionally publishes it.')), |
webmaster@1
|
50 array( |
webmaster@1
|
51 'blogger.editPost', |
webmaster@1
|
52 'blogapi_blogger_edit_post', |
webmaster@1
|
53 array('boolean', 'string', 'string', 'string', 'string', 'string', 'boolean'), |
webmaster@1
|
54 t('Updates the information about an existing post.')), |
webmaster@1
|
55 array( |
webmaster@1
|
56 'blogger.getPost', |
webmaster@1
|
57 'blogapi_blogger_get_post', |
webmaster@1
|
58 array('struct', 'string', 'string', 'string', 'string'), |
webmaster@1
|
59 t('Returns information about a specific post.')), |
webmaster@1
|
60 array( |
webmaster@1
|
61 'blogger.deletePost', |
webmaster@1
|
62 'blogapi_blogger_delete_post', |
webmaster@1
|
63 array('boolean', 'string', 'string', 'string', 'string', 'boolean'), |
webmaster@1
|
64 t('Deletes a post.')), |
webmaster@1
|
65 array( |
webmaster@1
|
66 'blogger.getRecentPosts', |
webmaster@1
|
67 'blogapi_blogger_get_recent_posts', |
webmaster@1
|
68 array('array', 'string', 'string', 'string', 'string', 'int'), |
webmaster@1
|
69 t('Returns a list of the most recent posts in the system.')), |
webmaster@1
|
70 array( |
webmaster@1
|
71 'metaWeblog.newPost', |
webmaster@1
|
72 'blogapi_metaweblog_new_post', |
webmaster@1
|
73 array('string', 'string', 'string', 'string', 'struct', 'boolean'), |
webmaster@1
|
74 t('Creates a new post, and optionally publishes it.')), |
webmaster@1
|
75 array( |
webmaster@1
|
76 'metaWeblog.editPost', |
webmaster@1
|
77 'blogapi_metaweblog_edit_post', |
webmaster@1
|
78 array('boolean', 'string', 'string', 'string', 'struct', 'boolean'), |
webmaster@1
|
79 t('Updates information about an existing post.')), |
webmaster@1
|
80 array( |
webmaster@1
|
81 'metaWeblog.getPost', |
webmaster@1
|
82 'blogapi_metaweblog_get_post', |
webmaster@1
|
83 array('struct', 'string', 'string', 'string'), |
webmaster@1
|
84 t('Returns information about a specific post.')), |
webmaster@1
|
85 array( |
webmaster@1
|
86 'metaWeblog.newMediaObject', |
webmaster@1
|
87 'blogapi_metaweblog_new_media_object', |
webmaster@1
|
88 array('string', 'string', 'string', 'string', 'struct'), |
webmaster@1
|
89 t('Uploads a file to your webserver.')), |
webmaster@1
|
90 array( |
webmaster@1
|
91 'metaWeblog.getCategories', |
webmaster@1
|
92 'blogapi_metaweblog_get_category_list', |
webmaster@1
|
93 array('struct', 'string', 'string', 'string'), |
webmaster@1
|
94 t('Returns a list of all categories to which the post is assigned.')), |
webmaster@1
|
95 array( |
webmaster@1
|
96 'metaWeblog.getRecentPosts', |
webmaster@1
|
97 'blogapi_metaweblog_get_recent_posts', |
webmaster@1
|
98 array('array', 'string', 'string', 'string', 'int'), |
webmaster@1
|
99 t('Returns a list of the most recent posts in the system.')), |
webmaster@1
|
100 array( |
webmaster@1
|
101 'mt.getRecentPostTitles', |
webmaster@1
|
102 'blogapi_mt_get_recent_post_titles', |
webmaster@1
|
103 array('array', 'string', 'string', 'string', 'int'), |
webmaster@1
|
104 t('Returns a bandwidth-friendly list of the most recent posts in the system.')), |
webmaster@1
|
105 array( |
webmaster@1
|
106 'mt.getCategoryList', |
webmaster@1
|
107 'blogapi_mt_get_category_list', |
webmaster@1
|
108 array('array', 'string', 'string', 'string'), |
webmaster@1
|
109 t('Returns a list of all categories defined in the blog.')), |
webmaster@1
|
110 array( |
webmaster@1
|
111 'mt.getPostCategories', |
webmaster@1
|
112 'blogapi_mt_get_post_categories', |
webmaster@1
|
113 array('array', 'string', 'string', 'string'), |
webmaster@1
|
114 t('Returns a list of all categories to which the post is assigned.')), |
webmaster@1
|
115 array( |
webmaster@1
|
116 'mt.setPostCategories', |
webmaster@1
|
117 'blogapi_mt_set_post_categories', |
webmaster@1
|
118 array('boolean', 'string', 'string', 'string', 'array'), |
webmaster@1
|
119 t('Sets the categories for a post.')), |
webmaster@1
|
120 array( |
webmaster@1
|
121 'mt.supportedMethods', |
webmaster@1
|
122 'xmlrpc_server_list_methods', |
webmaster@1
|
123 array('array'), |
webmaster@1
|
124 t('Retrieve information about the XML-RPC methods supported by the server.')), |
webmaster@1
|
125 array( |
webmaster@1
|
126 'mt.supportedTextFilters', |
webmaster@1
|
127 'blogapi_mt_supported_text_filters', |
webmaster@1
|
128 array('array'), |
webmaster@1
|
129 t('Retrieve information about the text formatting plugins supported by the server.')), |
webmaster@1
|
130 array( |
webmaster@1
|
131 'mt.publishPost', |
webmaster@1
|
132 'blogap_mti_publish_post', |
webmaster@1
|
133 array('boolean', 'string', 'string', 'string'), |
webmaster@1
|
134 t('Publish (rebuild) all of the static files related to an entry from your blog. Equivalent to saving an entry in the system (but without the ping).'))); |
webmaster@1
|
135 } |
webmaster@1
|
136 |
webmaster@1
|
137 /** |
webmaster@1
|
138 * Blogging API callback. Finds the URL of a user's blog. |
webmaster@1
|
139 */ |
webmaster@1
|
140 |
webmaster@1
|
141 function blogapi_blogger_get_users_blogs($appid, $username, $password) { |
webmaster@1
|
142 |
webmaster@1
|
143 $user = blogapi_validate_user($username, $password); |
webmaster@1
|
144 if ($user->uid) { |
webmaster@1
|
145 $types = _blogapi_get_node_types(); |
webmaster@1
|
146 $structs = array(); |
webmaster@1
|
147 foreach ($types as $type) { |
webmaster@1
|
148 $structs[] = array('url' => url('blog/'. $user->uid, array('absolute' => TRUE)), 'blogid' => $type, 'blogName' => $user->name .": ". $type); |
webmaster@1
|
149 } |
webmaster@1
|
150 return $structs; |
webmaster@1
|
151 } |
webmaster@1
|
152 else { |
webmaster@1
|
153 return blogapi_error($user); |
webmaster@1
|
154 } |
webmaster@1
|
155 } |
webmaster@1
|
156 |
webmaster@1
|
157 /** |
webmaster@1
|
158 * Blogging API callback. Returns profile information about a user. |
webmaster@1
|
159 */ |
webmaster@1
|
160 function blogapi_blogger_get_user_info($appkey, $username, $password) { |
webmaster@1
|
161 $user = blogapi_validate_user($username, $password); |
webmaster@1
|
162 |
webmaster@1
|
163 if ($user->uid) { |
webmaster@1
|
164 $name = explode(' ', $user->realname ? $user->realname : $user->name, 2); |
webmaster@1
|
165 return array( |
webmaster@1
|
166 'userid' => $user->uid, |
webmaster@1
|
167 'lastname' => $name[1], |
webmaster@1
|
168 'firstname' => $name[0], |
webmaster@1
|
169 'nickname' => $user->name, |
webmaster@1
|
170 'email' => $user->mail, |
webmaster@1
|
171 'url' => url('blog/'. $user->uid, array('absolute' => TRUE))); |
webmaster@1
|
172 } |
webmaster@1
|
173 else { |
webmaster@1
|
174 return blogapi_error($user); |
webmaster@1
|
175 } |
webmaster@1
|
176 } |
webmaster@1
|
177 |
webmaster@1
|
178 /** |
webmaster@1
|
179 * Blogging API callback. Inserts a new blog post as a node. |
webmaster@1
|
180 */ |
webmaster@1
|
181 function blogapi_blogger_new_post($appkey, $blogid, $username, $password, $content, $publish) { |
webmaster@1
|
182 $user = blogapi_validate_user($username, $password); |
webmaster@1
|
183 if (!$user->uid) { |
webmaster@1
|
184 return blogapi_error($user); |
webmaster@1
|
185 } |
webmaster@1
|
186 |
webmaster@1
|
187 if (($error = _blogapi_validate_blogid($blogid)) !== TRUE) { |
webmaster@1
|
188 // Return an error if not configured type. |
webmaster@1
|
189 return $error; |
webmaster@1
|
190 } |
webmaster@1
|
191 |
webmaster@1
|
192 $edit = array(); |
webmaster@1
|
193 $edit['type'] = $blogid; |
webmaster@1
|
194 // get the node type defaults |
webmaster@1
|
195 $node_type_default = variable_get('node_options_'. $edit['type'], array('status', 'promote')); |
webmaster@1
|
196 $edit['uid'] = $user->uid; |
webmaster@1
|
197 $edit['name'] = $user->name; |
webmaster@1
|
198 $edit['promote'] = in_array('promote', $node_type_default); |
webmaster@1
|
199 $edit['comment'] = variable_get('comment_'. $edit['type'], 2); |
webmaster@1
|
200 $edit['revision'] = in_array('revision', $node_type_default); |
webmaster@1
|
201 $edit['format'] = FILTER_FORMAT_DEFAULT; |
webmaster@1
|
202 $edit['status'] = $publish; |
webmaster@1
|
203 |
webmaster@1
|
204 // check for bloggerAPI vs. metaWeblogAPI |
webmaster@1
|
205 if (is_array($content)) { |
webmaster@1
|
206 $edit['title'] = $content['title']; |
webmaster@1
|
207 $edit['body'] = $content['description']; |
webmaster@1
|
208 _blogapi_mt_extra($edit, $content); |
webmaster@1
|
209 } |
webmaster@1
|
210 else { |
webmaster@1
|
211 $edit['title'] = blogapi_blogger_title($content); |
webmaster@1
|
212 $edit['body'] = $content; |
webmaster@1
|
213 } |
webmaster@1
|
214 |
webmaster@1
|
215 if (!node_access('create', $edit['type'])) { |
webmaster@1
|
216 return blogapi_error(t('You do not have permission to create this type of post.')); |
webmaster@1
|
217 } |
webmaster@1
|
218 |
webmaster@1
|
219 if (user_access('administer nodes') && !isset($edit['date'])) { |
webmaster@1
|
220 $edit['date'] = format_date(time(), 'custom', 'Y-m-d H:i:s O'); |
webmaster@1
|
221 } |
webmaster@1
|
222 |
webmaster@1
|
223 node_invoke_nodeapi($edit, 'blogapi new'); |
webmaster@1
|
224 |
webmaster@1
|
225 node_validate($edit); |
webmaster@1
|
226 if ($errors = form_get_errors()) { |
webmaster@1
|
227 return blogapi_error(implode("\n", $errors)); |
webmaster@1
|
228 } |
webmaster@1
|
229 |
webmaster@1
|
230 $node = node_submit($edit); |
webmaster@1
|
231 node_save($node); |
webmaster@1
|
232 if ($node->nid) { |
webmaster@1
|
233 watchdog('content', '@type: added %title using blog API.', array('@type' => $node->type, '%title' => $node->title), WATCHDOG_NOTICE, l(t('view'), "node/$node->nid")); |
webmaster@1
|
234 // blogger.newPost returns a string so we cast the nid to a string by putting it in double quotes: |
webmaster@1
|
235 return "$node->nid"; |
webmaster@1
|
236 } |
webmaster@1
|
237 |
webmaster@1
|
238 return blogapi_error(t('Error storing post.')); |
webmaster@1
|
239 } |
webmaster@1
|
240 |
webmaster@1
|
241 /** |
webmaster@1
|
242 * Blogging API callback. Modifies the specified blog node. |
webmaster@1
|
243 */ |
webmaster@1
|
244 function blogapi_blogger_edit_post($appkey, $postid, $username, $password, $content, $publish) { |
webmaster@1
|
245 |
webmaster@1
|
246 $user = blogapi_validate_user($username, $password); |
webmaster@1
|
247 |
webmaster@1
|
248 if (!$user->uid) { |
webmaster@1
|
249 return blogapi_error($user); |
webmaster@1
|
250 } |
webmaster@1
|
251 |
webmaster@1
|
252 $node = node_load($postid); |
webmaster@1
|
253 if (!$node) { |
webmaster@1
|
254 return blogapi_error(t('n/a')); |
webmaster@1
|
255 } |
webmaster@1
|
256 // Let the teaser be re-generated. |
webmaster@1
|
257 unset($node->teaser); |
webmaster@1
|
258 |
webmaster@1
|
259 if (!node_access('update', $node)) { |
webmaster@1
|
260 return blogapi_error(t('You do not have permission to update this post.')); |
webmaster@1
|
261 } |
webmaster@1
|
262 |
webmaster@1
|
263 $node->status = $publish; |
webmaster@1
|
264 |
webmaster@1
|
265 // check for bloggerAPI vs. metaWeblogAPI |
webmaster@1
|
266 if (is_array($content)) { |
webmaster@1
|
267 $node->title = $content['title']; |
webmaster@1
|
268 $node->body = $content['description']; |
webmaster@1
|
269 _blogapi_mt_extra($node, $content); |
webmaster@1
|
270 } |
webmaster@1
|
271 else { |
webmaster@1
|
272 $node->title = blogapi_blogger_title($content); |
webmaster@1
|
273 $node->body = $content; |
webmaster@1
|
274 } |
webmaster@1
|
275 |
webmaster@1
|
276 node_invoke_nodeapi($node, 'blogapi edit'); |
webmaster@1
|
277 |
webmaster@1
|
278 node_validate($node); |
webmaster@1
|
279 if ($errors = form_get_errors()) { |
webmaster@1
|
280 return blogapi_error(implode("\n", $errors)); |
webmaster@1
|
281 } |
webmaster@1
|
282 |
webmaster@1
|
283 if (user_access('administer nodes') && !isset($edit['date'])) { |
webmaster@1
|
284 $node->date = format_date($node->created, 'custom', 'Y-m-d H:i:s O'); |
webmaster@1
|
285 } |
webmaster@1
|
286 $node = node_submit($node); |
webmaster@1
|
287 node_save($node); |
webmaster@1
|
288 if ($node->nid) { |
webmaster@1
|
289 watchdog('content', '@type: updated %title using Blog API.', array('@type' => $node->type, '%title' => $node->title), WATCHDOG_NOTICE, l(t('view'), "node/$node->nid")); |
webmaster@1
|
290 return TRUE; |
webmaster@1
|
291 } |
webmaster@1
|
292 |
webmaster@1
|
293 return blogapi_error(t('Error storing post.')); |
webmaster@1
|
294 } |
webmaster@1
|
295 |
webmaster@1
|
296 /** |
webmaster@1
|
297 * Blogging API callback. Returns a specified blog node. |
webmaster@1
|
298 */ |
webmaster@1
|
299 function blogapi_blogger_get_post($appkey, $postid, $username, $password) { |
webmaster@1
|
300 $user = blogapi_validate_user($username, $password); |
webmaster@1
|
301 if (!$user->uid) { |
webmaster@1
|
302 return blogapi_error($user); |
webmaster@1
|
303 } |
webmaster@1
|
304 |
webmaster@1
|
305 $node = node_load($postid); |
webmaster@1
|
306 |
webmaster@1
|
307 return _blogapi_get_post($node, TRUE); |
webmaster@1
|
308 } |
webmaster@1
|
309 |
webmaster@1
|
310 /** |
webmaster@1
|
311 * Blogging API callback. Removes the specified blog node. |
webmaster@1
|
312 */ |
webmaster@1
|
313 function blogapi_blogger_delete_post($appkey, $postid, $username, $password, $publish) { |
webmaster@1
|
314 $user = blogapi_validate_user($username, $password); |
webmaster@1
|
315 if (!$user->uid) { |
webmaster@1
|
316 return blogapi_error($user); |
webmaster@1
|
317 } |
webmaster@1
|
318 |
webmaster@1
|
319 node_delete($postid); |
webmaster@1
|
320 return TRUE; |
webmaster@1
|
321 } |
webmaster@1
|
322 |
webmaster@1
|
323 /** |
webmaster@1
|
324 * Blogging API callback. Returns the latest few postings in a user's blog. $bodies TRUE |
webmaster@1
|
325 * <a href="http://movabletype.org/docs/mtmanual_programmatic.html#item_mt%2EgetRecentPostTitles"> |
webmaster@1
|
326 * returns a bandwidth-friendly list</a>. |
webmaster@1
|
327 */ |
webmaster@1
|
328 function blogapi_blogger_get_recent_posts($appkey, $blogid, $username, $password, $number_of_posts, $bodies = TRUE) { |
webmaster@1
|
329 // Remove unused appkey (from bloggerAPI). |
webmaster@1
|
330 $user = blogapi_validate_user($username, $password); |
webmaster@1
|
331 if (!$user->uid) { |
webmaster@1
|
332 return blogapi_error($user); |
webmaster@1
|
333 } |
webmaster@1
|
334 |
webmaster@1
|
335 if (($error = _blogapi_validate_blogid($blogid)) !== TRUE) { |
webmaster@1
|
336 // Return an error if not configured type. |
webmaster@1
|
337 return $error; |
webmaster@1
|
338 } |
webmaster@1
|
339 |
webmaster@1
|
340 if ($bodies) { |
webmaster@1
|
341 $result = db_query_range("SELECT n.nid, n.title, r.body, r.format, n.comment, n.created, u.name FROM {node} n, {node_revisions} r, {users} u WHERE n.uid = u.uid AND n.vid = r.vid AND n.type = '%s' AND n.uid = %d ORDER BY n.created DESC", $blogid, $user->uid, 0, $number_of_posts); |
webmaster@1
|
342 } |
webmaster@1
|
343 else { |
webmaster@1
|
344 $result = db_query_range("SELECT n.nid, n.title, n.created, u.name FROM {node} n, {users} u WHERE n.uid = u.uid AND n.type = '%s' AND n.uid = %d ORDER BY n.created DESC", $blogid, $user->uid, 0, $number_of_posts); |
webmaster@1
|
345 } |
webmaster@1
|
346 $blogs = array(); |
webmaster@1
|
347 while ($blog = db_fetch_object($result)) { |
webmaster@1
|
348 $blogs[] = _blogapi_get_post($blog, $bodies); |
webmaster@1
|
349 } |
webmaster@1
|
350 return $blogs; |
webmaster@1
|
351 } |
webmaster@1
|
352 |
webmaster@1
|
353 function blogapi_metaweblog_new_post($blogid, $username, $password, $content, $publish) { |
webmaster@1
|
354 return blogapi_blogger_new_post('0123456789ABCDEF', $blogid, $username, $password, $content, $publish); |
webmaster@1
|
355 } |
webmaster@1
|
356 |
webmaster@1
|
357 function blogapi_metaweblog_edit_post($postid, $username, $password, $content, $publish) { |
webmaster@1
|
358 return blogapi_blogger_edit_post('0123456789ABCDEF', $postid, $username, $password, $content, $publish); |
webmaster@1
|
359 } |
webmaster@1
|
360 |
webmaster@1
|
361 function blogapi_metaweblog_get_post($postid, $username, $password) { |
webmaster@1
|
362 return blogapi_blogger_get_post('01234567890ABCDEF', $postid, $username, $password); |
webmaster@1
|
363 } |
webmaster@1
|
364 |
webmaster@1
|
365 /** |
webmaster@1
|
366 * Blogging API callback. Inserts a file into Drupal. |
webmaster@1
|
367 */ |
webmaster@1
|
368 function blogapi_metaweblog_new_media_object($blogid, $username, $password, $file) { |
webmaster@1
|
369 $user = blogapi_validate_user($username, $password); |
webmaster@1
|
370 if (!$user->uid) { |
webmaster@1
|
371 return blogapi_error($user); |
webmaster@1
|
372 } |
webmaster@1
|
373 |
webmaster@1
|
374 $name = basename($file['name']); |
webmaster@1
|
375 $data = $file['bits']; |
webmaster@1
|
376 |
webmaster@1
|
377 if (!$data) { |
webmaster@1
|
378 return blogapi_error(t('No file sent.')); |
webmaster@1
|
379 } |
webmaster@1
|
380 |
webmaster@1
|
381 if (!$file = file_save_data($data, $name)) { |
webmaster@1
|
382 return blogapi_error(t('Error storing file.')); |
webmaster@1
|
383 } |
webmaster@1
|
384 |
webmaster@1
|
385 // Return the successful result. |
webmaster@1
|
386 return array('url' => file_create_url($file), 'struct'); |
webmaster@1
|
387 } |
webmaster@1
|
388 /** |
webmaster@1
|
389 * Blogging API callback. Returns a list of the taxonomy terms that can be |
webmaster@1
|
390 * associated with a blog node. |
webmaster@1
|
391 */ |
webmaster@1
|
392 function blogapi_metaweblog_get_category_list($blogid, $username, $password) { |
webmaster@1
|
393 if (($error = _blogapi_validate_blogid($blogid)) !== TRUE) { |
webmaster@1
|
394 // Return an error if not configured type. |
webmaster@1
|
395 return $error; |
webmaster@1
|
396 } |
webmaster@1
|
397 |
webmaster@1
|
398 $vocabularies = module_invoke('taxonomy', 'get_vocabularies', $blogid, 'vid'); |
webmaster@1
|
399 $categories = array(); |
webmaster@1
|
400 if ($vocabularies) { |
webmaster@1
|
401 foreach ($vocabularies as $vocabulary) { |
webmaster@1
|
402 $terms = module_invoke('taxonomy', 'get_tree', $vocabulary->vid, 0, -1); |
webmaster@1
|
403 foreach ($terms as $term) { |
webmaster@1
|
404 $term_name = $term->name; |
webmaster@1
|
405 foreach (module_invoke('taxonomy', 'get_parents', $term->tid, 'tid') as $parent) { |
webmaster@1
|
406 $term_name = $parent->name .'/'. $term_name; |
webmaster@1
|
407 } |
webmaster@1
|
408 $categories[] = array('categoryName' => $term_name, 'categoryId' => $term->tid); |
webmaster@1
|
409 } |
webmaster@1
|
410 } |
webmaster@1
|
411 } |
webmaster@1
|
412 return $categories; |
webmaster@1
|
413 } |
webmaster@1
|
414 |
webmaster@1
|
415 function blogapi_metaweblog_get_recent_posts($blogid, $username, $password, $number_of_posts) { |
webmaster@1
|
416 return blogapi_blogger_get_recent_posts('0123456789ABCDEF', $blogid, $username, $password, $number_of_posts, TRUE); |
webmaster@1
|
417 } |
webmaster@1
|
418 |
webmaster@1
|
419 function blogapi_mt_get_recent_post_titles($blogid, $username, $password, $number_of_posts) { |
webmaster@1
|
420 return blogapi_blogger_get_recent_posts('0123456789ABCDEF', $blogid, $username, $password, $number_of_posts, FALSE); |
webmaster@1
|
421 } |
webmaster@1
|
422 |
webmaster@1
|
423 function blogapi_mt_get_category_list($blogid, $username, $password) { |
webmaster@1
|
424 return blogapi_metaweblog_get_category_list($blogid, $username, $password); |
webmaster@1
|
425 } |
webmaster@1
|
426 |
webmaster@1
|
427 /** |
webmaster@1
|
428 * Blogging API callback. Returns a list of the taxonomy terms that are |
webmaster@1
|
429 * assigned to a particular node. |
webmaster@1
|
430 */ |
webmaster@1
|
431 function blogapi_mt_get_post_categories($postid, $username, $password) { |
webmaster@1
|
432 $user = blogapi_validate_user($username, $password); |
webmaster@1
|
433 if (!$user->uid) { |
webmaster@1
|
434 return blogapi_error($user); |
webmaster@1
|
435 } |
webmaster@1
|
436 |
webmaster@1
|
437 $node = node_load($postid); |
webmaster@1
|
438 $terms = module_invoke('taxonomy', 'node_get_terms', $node, 'tid'); |
webmaster@1
|
439 $categories = array(); |
webmaster@1
|
440 foreach ($terms as $term) { |
webmaster@1
|
441 $term_name = $term->name; |
webmaster@1
|
442 foreach (module_invoke('taxonomy', 'get_parents', $term->tid, 'tid') as $parent) { |
webmaster@1
|
443 $term_name = $parent->name .'/'. $term_name; |
webmaster@1
|
444 } |
webmaster@1
|
445 $categories[] = array('categoryName' => $term_name, 'categoryId' => $term->tid, 'isPrimary' => TRUE); |
webmaster@1
|
446 } |
webmaster@1
|
447 |
webmaster@1
|
448 return $categories; |
webmaster@1
|
449 } |
webmaster@1
|
450 |
webmaster@1
|
451 /** |
webmaster@1
|
452 * Blogging API callback. Assigns taxonomy terms to a particular node. |
webmaster@1
|
453 */ |
webmaster@1
|
454 function blogapi_mt_set_post_categories($postid, $username, $password, $categories) { |
webmaster@1
|
455 $user = blogapi_validate_user($username, $password); |
webmaster@1
|
456 if (!$user->uid) { |
webmaster@1
|
457 return blogapi_error($user); |
webmaster@1
|
458 } |
webmaster@1
|
459 |
webmaster@1
|
460 $node = node_load($postid); |
webmaster@1
|
461 $node->taxonomy = array(); |
webmaster@1
|
462 foreach ($categories as $category) { |
webmaster@1
|
463 $node->taxonomy[] = $category['categoryId']; |
webmaster@1
|
464 } |
webmaster@1
|
465 node_save($node); |
webmaster@1
|
466 return TRUE; |
webmaster@1
|
467 } |
webmaster@1
|
468 |
webmaster@1
|
469 /** |
webmaster@1
|
470 * Blogging API callback. Sends a list of available input formats. |
webmaster@1
|
471 */ |
webmaster@1
|
472 function blogapi_mt_supported_text_filters() { |
webmaster@1
|
473 // NOTE: we're only using anonymous' formats because the MT spec |
webmaster@1
|
474 // does not allow for per-user formats. |
webmaster@1
|
475 $formats = filter_formats(); |
webmaster@1
|
476 |
webmaster@1
|
477 $filters = array(); |
webmaster@1
|
478 foreach ($formats as $format) { |
webmaster@1
|
479 $filter['key'] = $format->format; |
webmaster@1
|
480 $filter['label'] = $format->name; |
webmaster@1
|
481 $filters[] = $filter; |
webmaster@1
|
482 } |
webmaster@1
|
483 |
webmaster@1
|
484 return $filters; |
webmaster@1
|
485 } |
webmaster@1
|
486 |
webmaster@1
|
487 /** |
webmaster@1
|
488 * Blogging API callback. Publishes the given node |
webmaster@1
|
489 */ |
webmaster@1
|
490 function blogap_mti_publish_post($postid, $username, $password) { |
webmaster@1
|
491 $user = blogapi_validate_user($username, $password); |
webmaster@1
|
492 if (!$user->uid) { |
webmaster@1
|
493 return blogapi_error($user); |
webmaster@1
|
494 } |
webmaster@1
|
495 $node = node_load($postid); |
webmaster@1
|
496 if (!$node) { |
webmaster@1
|
497 return blogapi_error(t('Invalid post.')); |
webmaster@1
|
498 } |
webmaster@1
|
499 |
webmaster@1
|
500 $node->status = 1; |
webmaster@1
|
501 if (!node_access('update', $node)) { |
webmaster@1
|
502 return blogapi_error(t('You do not have permission to update this post.')); |
webmaster@1
|
503 } |
webmaster@1
|
504 |
webmaster@1
|
505 node_save($node); |
webmaster@1
|
506 |
webmaster@1
|
507 return TRUE; |
webmaster@1
|
508 } |
webmaster@1
|
509 |
webmaster@1
|
510 /** |
webmaster@1
|
511 * Prepare an error message for returning to the XMLRPC caller. |
webmaster@1
|
512 */ |
webmaster@1
|
513 function blogapi_error($message) { |
webmaster@1
|
514 static $xmlrpcusererr; |
webmaster@1
|
515 if (!is_array($message)) { |
webmaster@1
|
516 $message = array($message); |
webmaster@1
|
517 } |
webmaster@1
|
518 |
webmaster@1
|
519 $message = implode(' ', $message); |
webmaster@1
|
520 |
webmaster@1
|
521 return xmlrpc_error($xmlrpcusererr + 1, strip_tags($message)); |
webmaster@1
|
522 } |
webmaster@1
|
523 |
webmaster@1
|
524 /** |
webmaster@1
|
525 * Ensure that the given user has permission to edit a blog. |
webmaster@1
|
526 */ |
webmaster@1
|
527 function blogapi_validate_user($username, $password) { |
webmaster@1
|
528 global $user; |
webmaster@1
|
529 |
webmaster@1
|
530 $user = user_authenticate(array('name' => $username, 'pass' => $password)); |
webmaster@1
|
531 |
webmaster@1
|
532 if ($user->uid) { |
webmaster@1
|
533 if (user_access('administer content with blog api', $user)) { |
webmaster@1
|
534 return $user; |
webmaster@1
|
535 } |
webmaster@1
|
536 else { |
webmaster@1
|
537 return t('You do not have permission to edit this blog.'); |
webmaster@1
|
538 } |
webmaster@1
|
539 } |
webmaster@1
|
540 else { |
webmaster@1
|
541 return t('Wrong username or password.'); |
webmaster@1
|
542 } |
webmaster@1
|
543 } |
webmaster@1
|
544 |
webmaster@1
|
545 /** |
webmaster@1
|
546 * For the blogger API, extract the node title from the contents field. |
webmaster@1
|
547 */ |
webmaster@1
|
548 function blogapi_blogger_title(&$contents) { |
webmaster@1
|
549 if (eregi('<title>([^<]*)</title>', $contents, $title)) { |
webmaster@1
|
550 $title = strip_tags($title[0]); |
webmaster@1
|
551 $contents = ereg_replace('<title>[^<]*</title>', '', $contents); |
webmaster@1
|
552 } |
webmaster@1
|
553 else { |
webmaster@1
|
554 list($title, $contents) = explode("\n", $contents, 2); |
webmaster@1
|
555 } |
webmaster@1
|
556 return $title; |
webmaster@1
|
557 } |
webmaster@1
|
558 |
webmaster@1
|
559 function blogapi_admin_settings() { |
webmaster@1
|
560 $node_types = array_map('check_plain', node_get_types('names')); |
webmaster@1
|
561 $defaults = isset($node_types['blog']) ? array('blog' => 1) : array(); |
webmaster@1
|
562 $form['blogapi_node_types'] = array( |
webmaster@1
|
563 '#type' => 'checkboxes', |
webmaster@1
|
564 '#title' => t('Enable for external blogging clients'), |
webmaster@1
|
565 '#required' => TRUE, |
webmaster@1
|
566 '#default_value' => variable_get('blogapi_node_types', $defaults), |
webmaster@1
|
567 '#options' => $node_types, |
webmaster@1
|
568 '#description' => t('Select the content types available to external blogging clients via Blog API. If supported, each enabled content type will be displayed as a separate "blog" by the external client.') |
webmaster@1
|
569 ); |
webmaster@1
|
570 |
webmaster@1
|
571 return system_settings_form($form); |
webmaster@1
|
572 } |
webmaster@1
|
573 |
webmaster@1
|
574 function blogapi_menu() { |
webmaster@1
|
575 $items['blogapi/rsd'] = array( |
webmaster@1
|
576 'title' => 'RSD', |
webmaster@1
|
577 'page callback' => 'blogapi_rsd', |
webmaster@1
|
578 'access arguments' => array('access content'), |
webmaster@1
|
579 'type' => MENU_CALLBACK, |
webmaster@1
|
580 ); |
webmaster@1
|
581 $items['admin/settings/blogapi'] = array( |
webmaster@1
|
582 'title' => 'Blog API', |
webmaster@1
|
583 'description' => 'Configure the content types available to external blogging clients.', |
webmaster@1
|
584 'page callback' => 'drupal_get_form', |
webmaster@1
|
585 'page arguments' => array('blogapi_admin_settings'), |
webmaster@1
|
586 'access arguments' => array('administer site configuration'), |
webmaster@1
|
587 'type' => MENU_NORMAL_ITEM, |
webmaster@1
|
588 ); |
webmaster@1
|
589 |
webmaster@1
|
590 return $items; |
webmaster@1
|
591 } |
webmaster@1
|
592 |
webmaster@1
|
593 function blogapi_init() { |
webmaster@1
|
594 if (drupal_is_front_page()) { |
webmaster@1
|
595 drupal_add_link(array('rel' => 'EditURI', |
webmaster@1
|
596 'type' => 'application/rsd+xml', |
webmaster@1
|
597 'title' => t('RSD'), |
webmaster@1
|
598 'href' => url('blogapi/rsd', array('absolute' => TRUE)))); |
webmaster@1
|
599 } |
webmaster@1
|
600 } |
webmaster@1
|
601 |
webmaster@1
|
602 function blogapi_rsd() { |
webmaster@1
|
603 global $base_url; |
webmaster@1
|
604 |
webmaster@1
|
605 $xmlrpc = $base_url .'/xmlrpc.php'; |
webmaster@1
|
606 $base = url('', array('absolute' => TRUE)); |
webmaster@1
|
607 $blogid = 1; # until we figure out how to handle multiple bloggers |
webmaster@1
|
608 |
webmaster@1
|
609 drupal_set_header('Content-Type: application/rsd+xml; charset=utf-8'); |
webmaster@1
|
610 print <<<__RSD__ |
webmaster@1
|
611 <?xml version="1.0"?> |
webmaster@1
|
612 <rsd version="1.0" xmlns="http://archipelago.phrasewise.com/rsd"> |
webmaster@1
|
613 <service> |
webmaster@1
|
614 <engineName>Drupal</engineName> |
webmaster@1
|
615 <engineLink>http://drupal.org/</engineLink> |
webmaster@1
|
616 <homePageLink>$base</homePageLink> |
webmaster@1
|
617 <apis> |
webmaster@1
|
618 <api name="MetaWeblog" preferred="false" apiLink="$xmlrpc" blogID="$blogid" /> |
webmaster@1
|
619 <api name="Blogger" preferred="false" apiLink="$xmlrpc" blogID="$blogid" /> |
webmaster@1
|
620 <api name="MovableType" preferred="true" apiLink="$xmlrpc" blogID="$blogid" /> |
webmaster@1
|
621 </apis> |
webmaster@1
|
622 </service> |
webmaster@1
|
623 </rsd> |
webmaster@1
|
624 __RSD__; |
webmaster@1
|
625 } |
webmaster@1
|
626 |
webmaster@1
|
627 /** |
webmaster@1
|
628 * Handles extra information sent by clients according to MovableType's spec. |
webmaster@1
|
629 */ |
webmaster@1
|
630 function _blogapi_mt_extra(&$node, $struct) { |
webmaster@1
|
631 if (is_array($node)) { |
webmaster@1
|
632 $was_array = TRUE; |
webmaster@1
|
633 $node = (object)$node; |
webmaster@1
|
634 } |
webmaster@1
|
635 |
webmaster@1
|
636 // mt_allow_comments |
webmaster@1
|
637 if (array_key_exists('mt_allow_comments', $struct)) { |
webmaster@1
|
638 switch ($struct['mt_allow_comments']) { |
webmaster@1
|
639 case 0: |
webmaster@1
|
640 $node->comment = COMMENT_NODE_DISABLED; |
webmaster@1
|
641 break; |
webmaster@1
|
642 case 1: |
webmaster@1
|
643 $node->comment = COMMENT_NODE_READ_WRITE; |
webmaster@1
|
644 break; |
webmaster@1
|
645 case 2: |
webmaster@1
|
646 $node->comment = COMMENT_NODE_READ_ONLY; |
webmaster@1
|
647 break; |
webmaster@1
|
648 } |
webmaster@1
|
649 } |
webmaster@1
|
650 |
webmaster@1
|
651 // merge the 3 body sections (description, mt_excerpt, mt_text_more) into |
webmaster@1
|
652 // one body |
webmaster@1
|
653 if ($struct['mt_excerpt']) { |
webmaster@1
|
654 $node->body = $struct['mt_excerpt'] .'<!--break-->'. $node->body; |
webmaster@1
|
655 } |
webmaster@1
|
656 if ($struct['mt_text_more']) { |
webmaster@1
|
657 $node->body = $node->body .'<!--extended-->'. $struct['mt_text_more']; |
webmaster@1
|
658 } |
webmaster@1
|
659 |
webmaster@1
|
660 // mt_convert_breaks |
webmaster@1
|
661 if ($struct['mt_convert_breaks']) { |
webmaster@1
|
662 $node->format = $struct['mt_convert_breaks']; |
webmaster@1
|
663 } |
webmaster@1
|
664 |
webmaster@1
|
665 // dateCreated |
webmaster@1
|
666 if ($struct['dateCreated']) { |
webmaster@1
|
667 $node->date = format_date(mktime($struct['dateCreated']->hour, $struct['dateCreated']->minute, $struct['dateCreated']->second, $struct['dateCreated']->month, $struct['dateCreated']->day, $struct['dateCreated']->year), 'custom', 'Y-m-d H:i:s O'); |
webmaster@1
|
668 } |
webmaster@1
|
669 |
webmaster@1
|
670 if ($was_array) { |
webmaster@1
|
671 $node = (array)$node; |
webmaster@1
|
672 } |
webmaster@1
|
673 } |
webmaster@1
|
674 |
webmaster@1
|
675 function _blogapi_get_post($node, $bodies = TRUE) { |
webmaster@1
|
676 $xmlrpcval = array( |
webmaster@1
|
677 'userid' => $node->name, |
webmaster@1
|
678 'dateCreated' => xmlrpc_date($node->created), |
webmaster@1
|
679 'title' => $node->title, |
webmaster@1
|
680 'postid' => $node->nid, |
webmaster@1
|
681 'link' => url('node/'. $node->nid, array('absolute' => TRUE)), |
webmaster@1
|
682 'permaLink' => url('node/'. $node->nid, array('absolute' => TRUE)), |
webmaster@1
|
683 ); |
webmaster@1
|
684 if ($bodies) { |
webmaster@1
|
685 if ($node->comment == 1) { |
webmaster@1
|
686 $comment = 2; |
webmaster@1
|
687 } |
webmaster@1
|
688 else if ($node->comment == 2) { |
webmaster@1
|
689 $comment = 1; |
webmaster@1
|
690 } |
webmaster@1
|
691 $xmlrpcval['content'] = "<title>$node->title</title>$node->body"; |
webmaster@1
|
692 $xmlrpcval['description'] = $node->body; |
webmaster@1
|
693 // Add MT specific fields |
webmaster@1
|
694 $xmlrpcval['mt_allow_comments'] = (int) $comment; |
webmaster@1
|
695 $xmlrpcval['mt_convert_breaks'] = $node->format; |
webmaster@1
|
696 } |
webmaster@1
|
697 |
webmaster@1
|
698 return $xmlrpcval; |
webmaster@1
|
699 } |
webmaster@1
|
700 |
webmaster@1
|
701 /** |
webmaster@1
|
702 * Validate blog ID, which maps to a content type in Drupal. |
webmaster@1
|
703 * |
webmaster@1
|
704 * Only content types configured to work with Blog API are supported. |
webmaster@1
|
705 * |
webmaster@1
|
706 * @return |
webmaster@1
|
707 * TRUE if the content type is supported and the user has permission |
webmaster@1
|
708 * to post, or a blogapi_error() XML construct otherwise. |
webmaster@1
|
709 */ |
webmaster@1
|
710 function _blogapi_validate_blogid($blogid) { |
webmaster@1
|
711 $types = _blogapi_get_node_types(); |
webmaster@1
|
712 if (in_array($blogid, $types, TRUE)) { |
webmaster@1
|
713 return TRUE; |
webmaster@1
|
714 } |
webmaster@1
|
715 return blogapi_error(t("Blog API module is not configured to support the %type content type, or you don't have sufficient permissions to post this type of content.", array('%type' => $blogid))); |
webmaster@1
|
716 } |
webmaster@1
|
717 |
webmaster@1
|
718 function _blogapi_get_node_types() { |
webmaster@1
|
719 $available_types = array_keys(array_filter(variable_get('blogapi_node_types', array('blog' => 1)))); |
webmaster@1
|
720 $types = array(); |
webmaster@1
|
721 foreach (node_get_types() as $type => $name) { |
webmaster@1
|
722 if (node_access('create', $type) && in_array($type, $available_types)) { |
webmaster@1
|
723 $types[] = $type; |
webmaster@1
|
724 } |
webmaster@1
|
725 } |
webmaster@1
|
726 |
webmaster@1
|
727 return $types; |
webmaster@1
|
728 } |