webmaster@1
|
1 <?php |
webmaster@11
|
2 // $Id: blogapi.module,v 1.115.2.5 2008/10/08 20:12:17 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@9
|
132 'blogapi_mt_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@11
|
225 $valid = blogapi_status_error_check($edit, $publish); |
webmaster@11
|
226 if ($valid !== TRUE) { |
webmaster@11
|
227 return $valid; |
webmaster@11
|
228 } |
webmaster@11
|
229 |
webmaster@1
|
230 node_validate($edit); |
webmaster@1
|
231 if ($errors = form_get_errors()) { |
webmaster@1
|
232 return blogapi_error(implode("\n", $errors)); |
webmaster@1
|
233 } |
webmaster@1
|
234 |
webmaster@1
|
235 $node = node_submit($edit); |
webmaster@1
|
236 node_save($node); |
webmaster@1
|
237 if ($node->nid) { |
webmaster@1
|
238 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
|
239 // blogger.newPost returns a string so we cast the nid to a string by putting it in double quotes: |
webmaster@1
|
240 return "$node->nid"; |
webmaster@1
|
241 } |
webmaster@1
|
242 |
webmaster@1
|
243 return blogapi_error(t('Error storing post.')); |
webmaster@1
|
244 } |
webmaster@1
|
245 |
webmaster@1
|
246 /** |
webmaster@1
|
247 * Blogging API callback. Modifies the specified blog node. |
webmaster@1
|
248 */ |
webmaster@1
|
249 function blogapi_blogger_edit_post($appkey, $postid, $username, $password, $content, $publish) { |
webmaster@1
|
250 |
webmaster@1
|
251 $user = blogapi_validate_user($username, $password); |
webmaster@1
|
252 |
webmaster@1
|
253 if (!$user->uid) { |
webmaster@1
|
254 return blogapi_error($user); |
webmaster@1
|
255 } |
webmaster@1
|
256 |
webmaster@1
|
257 $node = node_load($postid); |
webmaster@1
|
258 if (!$node) { |
webmaster@1
|
259 return blogapi_error(t('n/a')); |
webmaster@1
|
260 } |
webmaster@1
|
261 // Let the teaser be re-generated. |
webmaster@1
|
262 unset($node->teaser); |
webmaster@1
|
263 |
webmaster@1
|
264 if (!node_access('update', $node)) { |
webmaster@1
|
265 return blogapi_error(t('You do not have permission to update this post.')); |
webmaster@1
|
266 } |
webmaster@11
|
267 // Save the original status for validation of permissions. |
webmaster@11
|
268 $original_status = $node->status; |
webmaster@1
|
269 $node->status = $publish; |
webmaster@1
|
270 |
webmaster@1
|
271 // check for bloggerAPI vs. metaWeblogAPI |
webmaster@1
|
272 if (is_array($content)) { |
webmaster@1
|
273 $node->title = $content['title']; |
webmaster@1
|
274 $node->body = $content['description']; |
webmaster@1
|
275 _blogapi_mt_extra($node, $content); |
webmaster@1
|
276 } |
webmaster@1
|
277 else { |
webmaster@1
|
278 $node->title = blogapi_blogger_title($content); |
webmaster@1
|
279 $node->body = $content; |
webmaster@1
|
280 } |
webmaster@1
|
281 |
webmaster@1
|
282 node_invoke_nodeapi($node, 'blogapi edit'); |
webmaster@1
|
283 |
webmaster@11
|
284 $valid = blogapi_status_error_check($node, $original_status); |
webmaster@11
|
285 if ($valid !== TRUE) { |
webmaster@11
|
286 return $valid; |
webmaster@11
|
287 } |
webmaster@11
|
288 |
webmaster@1
|
289 node_validate($node); |
webmaster@1
|
290 if ($errors = form_get_errors()) { |
webmaster@1
|
291 return blogapi_error(implode("\n", $errors)); |
webmaster@1
|
292 } |
webmaster@1
|
293 |
webmaster@1
|
294 if (user_access('administer nodes') && !isset($edit['date'])) { |
webmaster@1
|
295 $node->date = format_date($node->created, 'custom', 'Y-m-d H:i:s O'); |
webmaster@1
|
296 } |
webmaster@1
|
297 $node = node_submit($node); |
webmaster@1
|
298 node_save($node); |
webmaster@1
|
299 if ($node->nid) { |
webmaster@1
|
300 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
|
301 return TRUE; |
webmaster@1
|
302 } |
webmaster@1
|
303 |
webmaster@1
|
304 return blogapi_error(t('Error storing post.')); |
webmaster@1
|
305 } |
webmaster@1
|
306 |
webmaster@1
|
307 /** |
webmaster@1
|
308 * Blogging API callback. Returns a specified blog node. |
webmaster@1
|
309 */ |
webmaster@1
|
310 function blogapi_blogger_get_post($appkey, $postid, $username, $password) { |
webmaster@1
|
311 $user = blogapi_validate_user($username, $password); |
webmaster@1
|
312 if (!$user->uid) { |
webmaster@1
|
313 return blogapi_error($user); |
webmaster@1
|
314 } |
webmaster@1
|
315 |
webmaster@1
|
316 $node = node_load($postid); |
webmaster@1
|
317 |
webmaster@1
|
318 return _blogapi_get_post($node, TRUE); |
webmaster@1
|
319 } |
webmaster@1
|
320 |
webmaster@1
|
321 /** |
webmaster@11
|
322 * Check that the user has permission to save the node with the chosen status. |
webmaster@11
|
323 * |
webmaster@11
|
324 * @return |
webmaster@11
|
325 * TRUE if no error, or the blogapi_error(). |
webmaster@11
|
326 */ |
webmaster@11
|
327 function blogapi_status_error_check($node, $original_status) { |
webmaster@11
|
328 |
webmaster@11
|
329 $node = (object) $node; |
webmaster@11
|
330 |
webmaster@11
|
331 $node_type_default = variable_get('node_options_'. $node->type, array('status', 'promote')); |
webmaster@11
|
332 |
webmaster@11
|
333 // If we don't have the 'administer nodes' permission and the status is |
webmaster@11
|
334 // changing or for a new node the status is not the content type's default, |
webmaster@11
|
335 // then return an error. |
webmaster@11
|
336 if (!user_access('administer nodes') && (($node->status != $original_status) || (empty($node->nid) && $node->status != in_array('status', $node_type_default)))) { |
webmaster@11
|
337 if ($node->status) { |
webmaster@11
|
338 return blogapi_error(t('You do not have permission to publish this type of post. Please save it as a draft instead.')); |
webmaster@11
|
339 } |
webmaster@11
|
340 else { |
webmaster@11
|
341 return blogapi_error(t('You do not have permission to save this post as a draft. Please publish it instead.')); |
webmaster@11
|
342 } |
webmaster@11
|
343 } |
webmaster@11
|
344 return TRUE; |
webmaster@11
|
345 } |
webmaster@11
|
346 |
webmaster@11
|
347 |
webmaster@11
|
348 /** |
webmaster@1
|
349 * Blogging API callback. Removes the specified blog node. |
webmaster@1
|
350 */ |
webmaster@1
|
351 function blogapi_blogger_delete_post($appkey, $postid, $username, $password, $publish) { |
webmaster@1
|
352 $user = blogapi_validate_user($username, $password); |
webmaster@1
|
353 if (!$user->uid) { |
webmaster@1
|
354 return blogapi_error($user); |
webmaster@1
|
355 } |
webmaster@1
|
356 |
webmaster@1
|
357 node_delete($postid); |
webmaster@1
|
358 return TRUE; |
webmaster@1
|
359 } |
webmaster@1
|
360 |
webmaster@1
|
361 /** |
webmaster@1
|
362 * Blogging API callback. Returns the latest few postings in a user's blog. $bodies TRUE |
webmaster@1
|
363 * <a href="http://movabletype.org/docs/mtmanual_programmatic.html#item_mt%2EgetRecentPostTitles"> |
webmaster@1
|
364 * returns a bandwidth-friendly list</a>. |
webmaster@1
|
365 */ |
webmaster@1
|
366 function blogapi_blogger_get_recent_posts($appkey, $blogid, $username, $password, $number_of_posts, $bodies = TRUE) { |
webmaster@1
|
367 // Remove unused appkey (from bloggerAPI). |
webmaster@1
|
368 $user = blogapi_validate_user($username, $password); |
webmaster@1
|
369 if (!$user->uid) { |
webmaster@1
|
370 return blogapi_error($user); |
webmaster@1
|
371 } |
webmaster@1
|
372 |
webmaster@1
|
373 if (($error = _blogapi_validate_blogid($blogid)) !== TRUE) { |
webmaster@1
|
374 // Return an error if not configured type. |
webmaster@1
|
375 return $error; |
webmaster@1
|
376 } |
webmaster@1
|
377 |
webmaster@1
|
378 if ($bodies) { |
webmaster@1
|
379 $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
|
380 } |
webmaster@1
|
381 else { |
webmaster@1
|
382 $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
|
383 } |
webmaster@1
|
384 $blogs = array(); |
webmaster@1
|
385 while ($blog = db_fetch_object($result)) { |
webmaster@1
|
386 $blogs[] = _blogapi_get_post($blog, $bodies); |
webmaster@1
|
387 } |
webmaster@1
|
388 return $blogs; |
webmaster@1
|
389 } |
webmaster@1
|
390 |
webmaster@1
|
391 function blogapi_metaweblog_new_post($blogid, $username, $password, $content, $publish) { |
webmaster@1
|
392 return blogapi_blogger_new_post('0123456789ABCDEF', $blogid, $username, $password, $content, $publish); |
webmaster@1
|
393 } |
webmaster@1
|
394 |
webmaster@1
|
395 function blogapi_metaweblog_edit_post($postid, $username, $password, $content, $publish) { |
webmaster@1
|
396 return blogapi_blogger_edit_post('0123456789ABCDEF', $postid, $username, $password, $content, $publish); |
webmaster@1
|
397 } |
webmaster@1
|
398 |
webmaster@1
|
399 function blogapi_metaweblog_get_post($postid, $username, $password) { |
webmaster@1
|
400 return blogapi_blogger_get_post('01234567890ABCDEF', $postid, $username, $password); |
webmaster@1
|
401 } |
webmaster@1
|
402 |
webmaster@1
|
403 /** |
webmaster@1
|
404 * Blogging API callback. Inserts a file into Drupal. |
webmaster@1
|
405 */ |
webmaster@1
|
406 function blogapi_metaweblog_new_media_object($blogid, $username, $password, $file) { |
webmaster@1
|
407 $user = blogapi_validate_user($username, $password); |
webmaster@1
|
408 if (!$user->uid) { |
webmaster@1
|
409 return blogapi_error($user); |
webmaster@1
|
410 } |
webmaster@1
|
411 |
webmaster@9
|
412 $usersize = 0; |
webmaster@9
|
413 $uploadsize = 0; |
webmaster@9
|
414 |
webmaster@9
|
415 $roles = array_intersect(user_roles(FALSE, 'administer content with blog api'), $user->roles); |
webmaster@9
|
416 |
webmaster@9
|
417 foreach ($roles as $rid => $name) { |
webmaster@9
|
418 $extensions .= ' '. strtolower(variable_get("blogapi_extensions_$rid", variable_get('blogapi_extensions_default', 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp'))); |
webmaster@9
|
419 $usersize= max($usersize, variable_get("blogapi_usersize_$rid", variable_get('blogapi_usersize_default', 1)) * 1024 * 1024); |
webmaster@9
|
420 $uploadsize = max($uploadsize, variable_get("blogapi_uploadsize_$rid", variable_get('blogapi_uploadsize_default', 1)) * 1024 * 1024); |
webmaster@9
|
421 } |
webmaster@9
|
422 |
webmaster@9
|
423 $filesize = strlen($file['bits']); |
webmaster@9
|
424 |
webmaster@9
|
425 if ($filesize > $uploadsize) { |
webmaster@9
|
426 return blogapi_error(t('It is not possible to upload the file, because it exceeded the maximum filesize of @maxsize.', array('@maxsize' => format_size($uploadsize)))); |
webmaster@9
|
427 } |
webmaster@9
|
428 |
webmaster@9
|
429 if (_blogapi_space_used($user->uid) + $filesize > $usersize) { |
webmaster@9
|
430 return blogapi_error(t('The file can not be attached to this post, because the disk quota of @quota has been reached.', array('@quota' => format_size($usersize)))); |
webmaster@9
|
431 } |
webmaster@9
|
432 |
webmaster@9
|
433 // Only allow files with whitelisted extensions and convert remaining dots to |
webmaster@9
|
434 // underscores to prevent attacks via non-terminal executable extensions with |
webmaster@9
|
435 // files such as exploit.php.jpg. |
webmaster@9
|
436 |
webmaster@9
|
437 $whitelist = array_unique(explode(' ', trim($extensions))); |
webmaster@9
|
438 |
webmaster@1
|
439 $name = basename($file['name']); |
webmaster@9
|
440 |
webmaster@9
|
441 if ($extension_position = strrpos($name, '.')) { |
webmaster@9
|
442 $filename = drupal_substr($name, 0, $extension_position); |
webmaster@9
|
443 $final_extension = drupal_substr($name, $extension_position + 1); |
webmaster@9
|
444 |
webmaster@9
|
445 if (!in_array(strtolower($final_extension), $whitelist)) { |
webmaster@9
|
446 return blogapi_error(t('It is not possible to upload the file, because it is only possible to upload files with the following extensions: @extensions', array('@extensions' => implode(' ', $whitelist)))); |
webmaster@9
|
447 } |
webmaster@9
|
448 |
webmaster@9
|
449 $filename = str_replace('.', '_', $filename); |
webmaster@9
|
450 $filename .= '.'. $final_extension; |
webmaster@9
|
451 } |
webmaster@9
|
452 |
webmaster@1
|
453 $data = $file['bits']; |
webmaster@1
|
454 |
webmaster@1
|
455 if (!$data) { |
webmaster@1
|
456 return blogapi_error(t('No file sent.')); |
webmaster@1
|
457 } |
webmaster@1
|
458 |
webmaster@9
|
459 if (!$file = file_save_data($data, $filename)) { |
webmaster@1
|
460 return blogapi_error(t('Error storing file.')); |
webmaster@1
|
461 } |
webmaster@1
|
462 |
webmaster@9
|
463 $row = new stdClass(); |
webmaster@9
|
464 $row->uid = $user->uid; |
webmaster@9
|
465 $row->filepath = $file; |
webmaster@9
|
466 $row->filesize = $filesize; |
webmaster@9
|
467 |
webmaster@9
|
468 drupal_write_record('blogapi_files', $row); |
webmaster@9
|
469 |
webmaster@1
|
470 // Return the successful result. |
webmaster@1
|
471 return array('url' => file_create_url($file), 'struct'); |
webmaster@1
|
472 } |
webmaster@1
|
473 /** |
webmaster@1
|
474 * Blogging API callback. Returns a list of the taxonomy terms that can be |
webmaster@1
|
475 * associated with a blog node. |
webmaster@1
|
476 */ |
webmaster@1
|
477 function blogapi_metaweblog_get_category_list($blogid, $username, $password) { |
webmaster@11
|
478 $user = blogapi_validate_user($username, $password); |
webmaster@11
|
479 if (!$user->uid) { |
webmaster@11
|
480 return blogapi_error($user); |
webmaster@11
|
481 } |
webmaster@11
|
482 |
webmaster@1
|
483 if (($error = _blogapi_validate_blogid($blogid)) !== TRUE) { |
webmaster@1
|
484 // Return an error if not configured type. |
webmaster@1
|
485 return $error; |
webmaster@1
|
486 } |
webmaster@1
|
487 |
webmaster@1
|
488 $vocabularies = module_invoke('taxonomy', 'get_vocabularies', $blogid, 'vid'); |
webmaster@1
|
489 $categories = array(); |
webmaster@1
|
490 if ($vocabularies) { |
webmaster@1
|
491 foreach ($vocabularies as $vocabulary) { |
webmaster@1
|
492 $terms = module_invoke('taxonomy', 'get_tree', $vocabulary->vid, 0, -1); |
webmaster@1
|
493 foreach ($terms as $term) { |
webmaster@1
|
494 $term_name = $term->name; |
webmaster@1
|
495 foreach (module_invoke('taxonomy', 'get_parents', $term->tid, 'tid') as $parent) { |
webmaster@1
|
496 $term_name = $parent->name .'/'. $term_name; |
webmaster@1
|
497 } |
webmaster@1
|
498 $categories[] = array('categoryName' => $term_name, 'categoryId' => $term->tid); |
webmaster@1
|
499 } |
webmaster@1
|
500 } |
webmaster@1
|
501 } |
webmaster@1
|
502 return $categories; |
webmaster@1
|
503 } |
webmaster@1
|
504 |
webmaster@1
|
505 function blogapi_metaweblog_get_recent_posts($blogid, $username, $password, $number_of_posts) { |
webmaster@1
|
506 return blogapi_blogger_get_recent_posts('0123456789ABCDEF', $blogid, $username, $password, $number_of_posts, TRUE); |
webmaster@1
|
507 } |
webmaster@1
|
508 |
webmaster@1
|
509 function blogapi_mt_get_recent_post_titles($blogid, $username, $password, $number_of_posts) { |
webmaster@1
|
510 return blogapi_blogger_get_recent_posts('0123456789ABCDEF', $blogid, $username, $password, $number_of_posts, FALSE); |
webmaster@1
|
511 } |
webmaster@1
|
512 |
webmaster@1
|
513 function blogapi_mt_get_category_list($blogid, $username, $password) { |
webmaster@1
|
514 return blogapi_metaweblog_get_category_list($blogid, $username, $password); |
webmaster@1
|
515 } |
webmaster@1
|
516 |
webmaster@1
|
517 /** |
webmaster@1
|
518 * Blogging API callback. Returns a list of the taxonomy terms that are |
webmaster@1
|
519 * assigned to a particular node. |
webmaster@1
|
520 */ |
webmaster@1
|
521 function blogapi_mt_get_post_categories($postid, $username, $password) { |
webmaster@1
|
522 $user = blogapi_validate_user($username, $password); |
webmaster@1
|
523 if (!$user->uid) { |
webmaster@1
|
524 return blogapi_error($user); |
webmaster@1
|
525 } |
webmaster@1
|
526 |
webmaster@1
|
527 $node = node_load($postid); |
webmaster@1
|
528 $terms = module_invoke('taxonomy', 'node_get_terms', $node, 'tid'); |
webmaster@1
|
529 $categories = array(); |
webmaster@1
|
530 foreach ($terms as $term) { |
webmaster@1
|
531 $term_name = $term->name; |
webmaster@1
|
532 foreach (module_invoke('taxonomy', 'get_parents', $term->tid, 'tid') as $parent) { |
webmaster@1
|
533 $term_name = $parent->name .'/'. $term_name; |
webmaster@1
|
534 } |
webmaster@1
|
535 $categories[] = array('categoryName' => $term_name, 'categoryId' => $term->tid, 'isPrimary' => TRUE); |
webmaster@1
|
536 } |
webmaster@1
|
537 |
webmaster@1
|
538 return $categories; |
webmaster@1
|
539 } |
webmaster@1
|
540 |
webmaster@1
|
541 /** |
webmaster@1
|
542 * Blogging API callback. Assigns taxonomy terms to a particular node. |
webmaster@1
|
543 */ |
webmaster@1
|
544 function blogapi_mt_set_post_categories($postid, $username, $password, $categories) { |
webmaster@1
|
545 $user = blogapi_validate_user($username, $password); |
webmaster@1
|
546 if (!$user->uid) { |
webmaster@1
|
547 return blogapi_error($user); |
webmaster@1
|
548 } |
webmaster@1
|
549 |
webmaster@1
|
550 $node = node_load($postid); |
webmaster@1
|
551 $node->taxonomy = array(); |
webmaster@1
|
552 foreach ($categories as $category) { |
webmaster@1
|
553 $node->taxonomy[] = $category['categoryId']; |
webmaster@1
|
554 } |
webmaster@11
|
555 $validated = blogapi_mt_validate_terms($node); |
webmaster@11
|
556 if ($validated !== TRUE) { |
webmaster@11
|
557 return $validated; |
webmaster@11
|
558 } |
webmaster@1
|
559 node_save($node); |
webmaster@1
|
560 return TRUE; |
webmaster@1
|
561 } |
webmaster@1
|
562 |
webmaster@1
|
563 /** |
webmaster@11
|
564 * Blogging API helper - find allowed taxonomy terms for a node type. |
webmaster@11
|
565 */ |
webmaster@11
|
566 function blogapi_mt_validate_terms($node) { |
webmaster@11
|
567 // We do a lot of heavy lifting here since taxonomy module doesn't have a |
webmaster@11
|
568 // stand-alone validation function. |
webmaster@11
|
569 if (module_exists('taxonomy')) { |
webmaster@11
|
570 $found_terms = array(); |
webmaster@11
|
571 if (!empty($node->taxonomy)) { |
webmaster@11
|
572 $term_list = array_unique($node->taxonomy); |
webmaster@11
|
573 $params = $term_list; |
webmaster@11
|
574 $params[] = $node->type; |
webmaster@11
|
575 $result = db_query(db_rewrite_sql("SELECT t.tid, t.vid FROM {term_data} t INNER JOIN {vocabulary_node_types} n ON t.vid = n.vid WHERE t.tid IN (". db_placeholders($term_list) .") AND n.type = '%s'", 't', 'tid'), $params); |
webmaster@11
|
576 $found_terms = array(); |
webmaster@11
|
577 $found_count = 0; |
webmaster@11
|
578 while ($term = db_fetch_object($result)) { |
webmaster@11
|
579 $found_terms[$term->vid][$term->tid] = $term->tid; |
webmaster@11
|
580 $found_count++; |
webmaster@11
|
581 } |
webmaster@11
|
582 // If the counts don't match, some terms are invalid or not accessible to this user. |
webmaster@11
|
583 if (count($term_list) != $found_count) { |
webmaster@11
|
584 return blogapi_error(t('Invalid categories submitted.')); |
webmaster@11
|
585 } |
webmaster@11
|
586 } |
webmaster@11
|
587 // Look up all the vocabularies for this node type. |
webmaster@11
|
588 $result2 = db_query(db_rewrite_sql("SELECT v.vid, v.name, v.required, v.multiple FROM {vocabulary} v INNER JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE n.type = '%s'", 'v', 'vid'), $node->type); |
webmaster@11
|
589 // Check each vocabulary associated with this node type. |
webmaster@11
|
590 while ($vocabulary = db_fetch_object($result2)) { |
webmaster@11
|
591 // Required vocabularies must have at least one term. |
webmaster@11
|
592 if ($vocabulary->required && empty($found_terms[$vocabulary->vid])) { |
webmaster@11
|
593 return blogapi_error(t('A category from the @vocabulary_name vocabulary is required.', array('@vocabulary_name' => $vocabulary->name))); |
webmaster@11
|
594 } |
webmaster@11
|
595 // Vocabularies that don't allow multiple terms may have at most one. |
webmaster@11
|
596 if (!($vocabulary->multiple) && (isset($found_terms[$vocabulary->vid]) && count($found_terms[$vocabulary->vid]) > 1)) { |
webmaster@11
|
597 return blogapi_error(t('You may only choose one category from the @vocabulary_name vocabulary.'), array('@vocabulary_name' => $vocabulary->name)); |
webmaster@11
|
598 } |
webmaster@11
|
599 } |
webmaster@11
|
600 } |
webmaster@11
|
601 elseif (!empty($node->taxonomy)) { |
webmaster@11
|
602 return blogapi_error(t('Error saving categories. This feature is not available.')); |
webmaster@11
|
603 } |
webmaster@11
|
604 return TRUE; |
webmaster@11
|
605 } |
webmaster@11
|
606 |
webmaster@11
|
607 /** |
webmaster@1
|
608 * Blogging API callback. Sends a list of available input formats. |
webmaster@1
|
609 */ |
webmaster@1
|
610 function blogapi_mt_supported_text_filters() { |
webmaster@1
|
611 // NOTE: we're only using anonymous' formats because the MT spec |
webmaster@1
|
612 // does not allow for per-user formats. |
webmaster@1
|
613 $formats = filter_formats(); |
webmaster@1
|
614 |
webmaster@1
|
615 $filters = array(); |
webmaster@1
|
616 foreach ($formats as $format) { |
webmaster@1
|
617 $filter['key'] = $format->format; |
webmaster@1
|
618 $filter['label'] = $format->name; |
webmaster@1
|
619 $filters[] = $filter; |
webmaster@1
|
620 } |
webmaster@1
|
621 |
webmaster@1
|
622 return $filters; |
webmaster@1
|
623 } |
webmaster@1
|
624 |
webmaster@1
|
625 /** |
webmaster@1
|
626 * Blogging API callback. Publishes the given node |
webmaster@1
|
627 */ |
webmaster@9
|
628 function blogapi_mt_publish_post($postid, $username, $password) { |
webmaster@1
|
629 $user = blogapi_validate_user($username, $password); |
webmaster@1
|
630 if (!$user->uid) { |
webmaster@1
|
631 return blogapi_error($user); |
webmaster@1
|
632 } |
webmaster@1
|
633 $node = node_load($postid); |
webmaster@1
|
634 if (!$node) { |
webmaster@1
|
635 return blogapi_error(t('Invalid post.')); |
webmaster@1
|
636 } |
webmaster@1
|
637 |
webmaster@11
|
638 // Nothing needs to be done if already published. |
webmaster@11
|
639 if ($node->status) { |
webmaster@11
|
640 return; |
webmaster@11
|
641 } |
webmaster@11
|
642 |
webmaster@11
|
643 if (!node_access('update', $node) || !user_access('administer nodes')) { |
webmaster@1
|
644 return blogapi_error(t('You do not have permission to update this post.')); |
webmaster@1
|
645 } |
webmaster@1
|
646 |
webmaster@11
|
647 $node->status = 1; |
webmaster@1
|
648 node_save($node); |
webmaster@1
|
649 |
webmaster@1
|
650 return TRUE; |
webmaster@1
|
651 } |
webmaster@1
|
652 |
webmaster@1
|
653 /** |
webmaster@1
|
654 * Prepare an error message for returning to the XMLRPC caller. |
webmaster@1
|
655 */ |
webmaster@1
|
656 function blogapi_error($message) { |
webmaster@1
|
657 static $xmlrpcusererr; |
webmaster@1
|
658 if (!is_array($message)) { |
webmaster@1
|
659 $message = array($message); |
webmaster@1
|
660 } |
webmaster@1
|
661 |
webmaster@1
|
662 $message = implode(' ', $message); |
webmaster@1
|
663 |
webmaster@1
|
664 return xmlrpc_error($xmlrpcusererr + 1, strip_tags($message)); |
webmaster@1
|
665 } |
webmaster@1
|
666 |
webmaster@1
|
667 /** |
webmaster@1
|
668 * Ensure that the given user has permission to edit a blog. |
webmaster@1
|
669 */ |
webmaster@1
|
670 function blogapi_validate_user($username, $password) { |
webmaster@1
|
671 global $user; |
webmaster@1
|
672 |
webmaster@1
|
673 $user = user_authenticate(array('name' => $username, 'pass' => $password)); |
webmaster@1
|
674 |
webmaster@1
|
675 if ($user->uid) { |
webmaster@1
|
676 if (user_access('administer content with blog api', $user)) { |
webmaster@1
|
677 return $user; |
webmaster@1
|
678 } |
webmaster@1
|
679 else { |
webmaster@1
|
680 return t('You do not have permission to edit this blog.'); |
webmaster@1
|
681 } |
webmaster@1
|
682 } |
webmaster@1
|
683 else { |
webmaster@1
|
684 return t('Wrong username or password.'); |
webmaster@1
|
685 } |
webmaster@1
|
686 } |
webmaster@1
|
687 |
webmaster@1
|
688 /** |
webmaster@1
|
689 * For the blogger API, extract the node title from the contents field. |
webmaster@1
|
690 */ |
webmaster@1
|
691 function blogapi_blogger_title(&$contents) { |
webmaster@1
|
692 if (eregi('<title>([^<]*)</title>', $contents, $title)) { |
webmaster@1
|
693 $title = strip_tags($title[0]); |
webmaster@1
|
694 $contents = ereg_replace('<title>[^<]*</title>', '', $contents); |
webmaster@1
|
695 } |
webmaster@1
|
696 else { |
webmaster@1
|
697 list($title, $contents) = explode("\n", $contents, 2); |
webmaster@1
|
698 } |
webmaster@1
|
699 return $title; |
webmaster@1
|
700 } |
webmaster@1
|
701 |
webmaster@1
|
702 function blogapi_admin_settings() { |
webmaster@1
|
703 $node_types = array_map('check_plain', node_get_types('names')); |
webmaster@1
|
704 $defaults = isset($node_types['blog']) ? array('blog' => 1) : array(); |
webmaster@1
|
705 $form['blogapi_node_types'] = array( |
webmaster@1
|
706 '#type' => 'checkboxes', |
webmaster@1
|
707 '#title' => t('Enable for external blogging clients'), |
webmaster@1
|
708 '#required' => TRUE, |
webmaster@1
|
709 '#default_value' => variable_get('blogapi_node_types', $defaults), |
webmaster@1
|
710 '#options' => $node_types, |
webmaster@1
|
711 '#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
|
712 ); |
webmaster@1
|
713 |
webmaster@9
|
714 $blogapi_extensions_default = variable_get('blogapi_extensions_default', 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp'); |
webmaster@9
|
715 $blogapi_uploadsize_default = variable_get('blogapi_uploadsize_default', 1); |
webmaster@9
|
716 $blogapi_usersize_default = variable_get('blogapi_usersize_default', 1); |
webmaster@9
|
717 |
webmaster@9
|
718 $form['settings_general'] = array( |
webmaster@9
|
719 '#type' => 'fieldset', |
webmaster@9
|
720 '#title' => t('File settings'), |
webmaster@9
|
721 '#collapsible' => TRUE, |
webmaster@9
|
722 ); |
webmaster@9
|
723 |
webmaster@9
|
724 $form['settings_general']['blogapi_extensions_default'] = array( |
webmaster@9
|
725 '#type' => 'textfield', |
webmaster@9
|
726 '#title' => t('Default permitted file extensions'), |
webmaster@9
|
727 '#default_value' => $blogapi_extensions_default, |
webmaster@9
|
728 '#maxlength' => 255, |
webmaster@9
|
729 '#description' => t('Default extensions that users can upload. Separate extensions with a space and do not include the leading dot.'), |
webmaster@9
|
730 ); |
webmaster@9
|
731 |
webmaster@9
|
732 $form['settings_general']['blogapi_uploadsize_default'] = array( |
webmaster@9
|
733 '#type' => 'textfield', |
webmaster@9
|
734 '#title' => t('Default maximum file size per upload'), |
webmaster@9
|
735 '#default_value' => $blogapi_uploadsize_default, |
webmaster@9
|
736 '#size' => 5, |
webmaster@9
|
737 '#maxlength' => 5, |
webmaster@9
|
738 '#description' => t('The default maximum file size a user can upload.'), |
webmaster@9
|
739 '#field_suffix' => t('MB') |
webmaster@9
|
740 ); |
webmaster@9
|
741 |
webmaster@9
|
742 $form['settings_general']['blogapi_usersize_default'] = array( |
webmaster@9
|
743 '#type' => 'textfield', |
webmaster@9
|
744 '#title' => t('Default total file size per user'), |
webmaster@9
|
745 '#default_value' => $blogapi_usersize_default, |
webmaster@9
|
746 '#size' => 5, |
webmaster@9
|
747 '#maxlength' => 5, |
webmaster@9
|
748 '#description' => t('The default maximum size of all files a user can have on the site.'), |
webmaster@9
|
749 '#field_suffix' => t('MB') |
webmaster@9
|
750 ); |
webmaster@9
|
751 |
webmaster@9
|
752 $form['settings_general']['upload_max_size'] = array('#value' => '<p>'. t('Your PHP settings limit the maximum file size per upload to %size.', array('%size' => format_size(file_upload_max_size()))).'</p>'); |
webmaster@9
|
753 |
webmaster@9
|
754 $roles = user_roles(0, 'administer content with blog api'); |
webmaster@9
|
755 $form['roles'] = array('#type' => 'value', '#value' => $roles); |
webmaster@9
|
756 |
webmaster@9
|
757 foreach ($roles as $rid => $role) { |
webmaster@9
|
758 $form['settings_role_'. $rid] = array( |
webmaster@9
|
759 '#type' => 'fieldset', |
webmaster@9
|
760 '#title' => t('Settings for @role', array('@role' => $role)), |
webmaster@9
|
761 '#collapsible' => TRUE, |
webmaster@9
|
762 '#collapsed' => TRUE, |
webmaster@9
|
763 ); |
webmaster@9
|
764 $form['settings_role_'. $rid]['blogapi_extensions_'. $rid] = array( |
webmaster@9
|
765 '#type' => 'textfield', |
webmaster@9
|
766 '#title' => t('Permitted file extensions'), |
webmaster@9
|
767 '#default_value' => variable_get('blogapi_extensions_'. $rid, $blogapi_extensions_default), |
webmaster@9
|
768 '#maxlength' => 255, |
webmaster@9
|
769 '#description' => t('Extensions that users in this role can upload. Separate extensions with a space and do not include the leading dot.'), |
webmaster@9
|
770 ); |
webmaster@9
|
771 $form['settings_role_'. $rid]['blogapi_uploadsize_'. $rid] = array( |
webmaster@9
|
772 '#type' => 'textfield', |
webmaster@9
|
773 '#title' => t('Maximum file size per upload'), |
webmaster@9
|
774 '#default_value' => variable_get('blogapi_uploadsize_'. $rid, $blogapi_uploadsize_default), |
webmaster@9
|
775 '#size' => 5, |
webmaster@9
|
776 '#maxlength' => 5, |
webmaster@9
|
777 '#description' => t('The maximum size of a file a user can upload (in megabytes).'), |
webmaster@9
|
778 ); |
webmaster@9
|
779 $form['settings_role_'. $rid]['blogapi_usersize_'. $rid] = array( |
webmaster@9
|
780 '#type' => 'textfield', |
webmaster@9
|
781 '#title' => t('Total file size per user'), |
webmaster@9
|
782 '#default_value' => variable_get('blogapi_usersize_'. $rid, $blogapi_usersize_default), |
webmaster@9
|
783 '#size' => 5, |
webmaster@9
|
784 '#maxlength' => 5, |
webmaster@9
|
785 '#description' => t('The maximum size of all files a user can have on the site (in megabytes).'), |
webmaster@9
|
786 ); |
webmaster@9
|
787 } |
webmaster@9
|
788 |
webmaster@1
|
789 return system_settings_form($form); |
webmaster@1
|
790 } |
webmaster@1
|
791 |
webmaster@1
|
792 function blogapi_menu() { |
webmaster@1
|
793 $items['blogapi/rsd'] = array( |
webmaster@1
|
794 'title' => 'RSD', |
webmaster@1
|
795 'page callback' => 'blogapi_rsd', |
webmaster@1
|
796 'access arguments' => array('access content'), |
webmaster@1
|
797 'type' => MENU_CALLBACK, |
webmaster@1
|
798 ); |
webmaster@1
|
799 $items['admin/settings/blogapi'] = array( |
webmaster@1
|
800 'title' => 'Blog API', |
webmaster@1
|
801 'description' => 'Configure the content types available to external blogging clients.', |
webmaster@1
|
802 'page callback' => 'drupal_get_form', |
webmaster@1
|
803 'page arguments' => array('blogapi_admin_settings'), |
webmaster@1
|
804 'access arguments' => array('administer site configuration'), |
webmaster@1
|
805 'type' => MENU_NORMAL_ITEM, |
webmaster@1
|
806 ); |
webmaster@1
|
807 |
webmaster@1
|
808 return $items; |
webmaster@1
|
809 } |
webmaster@1
|
810 |
webmaster@1
|
811 function blogapi_init() { |
webmaster@1
|
812 if (drupal_is_front_page()) { |
webmaster@1
|
813 drupal_add_link(array('rel' => 'EditURI', |
webmaster@1
|
814 'type' => 'application/rsd+xml', |
webmaster@1
|
815 'title' => t('RSD'), |
webmaster@1
|
816 'href' => url('blogapi/rsd', array('absolute' => TRUE)))); |
webmaster@1
|
817 } |
webmaster@1
|
818 } |
webmaster@1
|
819 |
webmaster@1
|
820 function blogapi_rsd() { |
webmaster@1
|
821 global $base_url; |
webmaster@1
|
822 |
webmaster@1
|
823 $xmlrpc = $base_url .'/xmlrpc.php'; |
webmaster@1
|
824 $base = url('', array('absolute' => TRUE)); |
webmaster@1
|
825 $blogid = 1; # until we figure out how to handle multiple bloggers |
webmaster@1
|
826 |
webmaster@1
|
827 drupal_set_header('Content-Type: application/rsd+xml; charset=utf-8'); |
webmaster@1
|
828 print <<<__RSD__ |
webmaster@1
|
829 <?xml version="1.0"?> |
webmaster@1
|
830 <rsd version="1.0" xmlns="http://archipelago.phrasewise.com/rsd"> |
webmaster@1
|
831 <service> |
webmaster@1
|
832 <engineName>Drupal</engineName> |
webmaster@1
|
833 <engineLink>http://drupal.org/</engineLink> |
webmaster@1
|
834 <homePageLink>$base</homePageLink> |
webmaster@1
|
835 <apis> |
webmaster@1
|
836 <api name="MetaWeblog" preferred="false" apiLink="$xmlrpc" blogID="$blogid" /> |
webmaster@1
|
837 <api name="Blogger" preferred="false" apiLink="$xmlrpc" blogID="$blogid" /> |
webmaster@1
|
838 <api name="MovableType" preferred="true" apiLink="$xmlrpc" blogID="$blogid" /> |
webmaster@1
|
839 </apis> |
webmaster@1
|
840 </service> |
webmaster@1
|
841 </rsd> |
webmaster@1
|
842 __RSD__; |
webmaster@1
|
843 } |
webmaster@1
|
844 |
webmaster@1
|
845 /** |
webmaster@1
|
846 * Handles extra information sent by clients according to MovableType's spec. |
webmaster@1
|
847 */ |
webmaster@1
|
848 function _blogapi_mt_extra(&$node, $struct) { |
webmaster@1
|
849 if (is_array($node)) { |
webmaster@1
|
850 $was_array = TRUE; |
webmaster@1
|
851 $node = (object)$node; |
webmaster@1
|
852 } |
webmaster@1
|
853 |
webmaster@1
|
854 // mt_allow_comments |
webmaster@1
|
855 if (array_key_exists('mt_allow_comments', $struct)) { |
webmaster@1
|
856 switch ($struct['mt_allow_comments']) { |
webmaster@1
|
857 case 0: |
webmaster@1
|
858 $node->comment = COMMENT_NODE_DISABLED; |
webmaster@1
|
859 break; |
webmaster@1
|
860 case 1: |
webmaster@1
|
861 $node->comment = COMMENT_NODE_READ_WRITE; |
webmaster@1
|
862 break; |
webmaster@1
|
863 case 2: |
webmaster@1
|
864 $node->comment = COMMENT_NODE_READ_ONLY; |
webmaster@1
|
865 break; |
webmaster@1
|
866 } |
webmaster@1
|
867 } |
webmaster@1
|
868 |
webmaster@1
|
869 // merge the 3 body sections (description, mt_excerpt, mt_text_more) into |
webmaster@1
|
870 // one body |
webmaster@1
|
871 if ($struct['mt_excerpt']) { |
webmaster@1
|
872 $node->body = $struct['mt_excerpt'] .'<!--break-->'. $node->body; |
webmaster@1
|
873 } |
webmaster@1
|
874 if ($struct['mt_text_more']) { |
webmaster@1
|
875 $node->body = $node->body .'<!--extended-->'. $struct['mt_text_more']; |
webmaster@1
|
876 } |
webmaster@1
|
877 |
webmaster@1
|
878 // mt_convert_breaks |
webmaster@1
|
879 if ($struct['mt_convert_breaks']) { |
webmaster@1
|
880 $node->format = $struct['mt_convert_breaks']; |
webmaster@1
|
881 } |
webmaster@1
|
882 |
webmaster@1
|
883 // dateCreated |
webmaster@1
|
884 if ($struct['dateCreated']) { |
webmaster@1
|
885 $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
|
886 } |
webmaster@1
|
887 |
webmaster@1
|
888 if ($was_array) { |
webmaster@1
|
889 $node = (array)$node; |
webmaster@1
|
890 } |
webmaster@1
|
891 } |
webmaster@1
|
892 |
webmaster@1
|
893 function _blogapi_get_post($node, $bodies = TRUE) { |
webmaster@1
|
894 $xmlrpcval = array( |
webmaster@1
|
895 'userid' => $node->name, |
webmaster@1
|
896 'dateCreated' => xmlrpc_date($node->created), |
webmaster@1
|
897 'title' => $node->title, |
webmaster@1
|
898 'postid' => $node->nid, |
webmaster@1
|
899 'link' => url('node/'. $node->nid, array('absolute' => TRUE)), |
webmaster@1
|
900 'permaLink' => url('node/'. $node->nid, array('absolute' => TRUE)), |
webmaster@1
|
901 ); |
webmaster@1
|
902 if ($bodies) { |
webmaster@1
|
903 if ($node->comment == 1) { |
webmaster@1
|
904 $comment = 2; |
webmaster@1
|
905 } |
webmaster@1
|
906 else if ($node->comment == 2) { |
webmaster@1
|
907 $comment = 1; |
webmaster@1
|
908 } |
webmaster@1
|
909 $xmlrpcval['content'] = "<title>$node->title</title>$node->body"; |
webmaster@1
|
910 $xmlrpcval['description'] = $node->body; |
webmaster@1
|
911 // Add MT specific fields |
webmaster@1
|
912 $xmlrpcval['mt_allow_comments'] = (int) $comment; |
webmaster@1
|
913 $xmlrpcval['mt_convert_breaks'] = $node->format; |
webmaster@1
|
914 } |
webmaster@1
|
915 |
webmaster@1
|
916 return $xmlrpcval; |
webmaster@1
|
917 } |
webmaster@1
|
918 |
webmaster@1
|
919 /** |
webmaster@1
|
920 * Validate blog ID, which maps to a content type in Drupal. |
webmaster@1
|
921 * |
webmaster@1
|
922 * Only content types configured to work with Blog API are supported. |
webmaster@1
|
923 * |
webmaster@1
|
924 * @return |
webmaster@1
|
925 * TRUE if the content type is supported and the user has permission |
webmaster@1
|
926 * to post, or a blogapi_error() XML construct otherwise. |
webmaster@1
|
927 */ |
webmaster@1
|
928 function _blogapi_validate_blogid($blogid) { |
webmaster@1
|
929 $types = _blogapi_get_node_types(); |
webmaster@1
|
930 if (in_array($blogid, $types, TRUE)) { |
webmaster@1
|
931 return TRUE; |
webmaster@1
|
932 } |
webmaster@1
|
933 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
|
934 } |
webmaster@1
|
935 |
webmaster@1
|
936 function _blogapi_get_node_types() { |
webmaster@1
|
937 $available_types = array_keys(array_filter(variable_get('blogapi_node_types', array('blog' => 1)))); |
webmaster@1
|
938 $types = array(); |
webmaster@1
|
939 foreach (node_get_types() as $type => $name) { |
webmaster@1
|
940 if (node_access('create', $type) && in_array($type, $available_types)) { |
webmaster@1
|
941 $types[] = $type; |
webmaster@1
|
942 } |
webmaster@1
|
943 } |
webmaster@1
|
944 |
webmaster@1
|
945 return $types; |
webmaster@1
|
946 } |
webmaster@9
|
947 |
webmaster@9
|
948 function _blogapi_space_used($uid) { |
webmaster@9
|
949 return db_result(db_query('SELECT SUM(filesize) FROM {blogapi_files} f WHERE f.uid = %d', $uid)); |
webmaster@9
|
950 } |