root/devel/mod/rpc/xmlrpc/library_blogger_xmlrpc.php

Revision 1072, 23.0 kB (checked in by sven, 2 years ago)

fix minor xmlrpc bug

  • Property svn:eol-style set to native
Line 
1 <?php
2
3     /*
4      * Blogger and metaWeblog API implementation
5      */
6
7     function generic_newPost($params, $method)
8     {
9         // Number of parameters
10         $nr_params = null;            // Raise an XML-RPC error
11         
12         if ($method == "blogger.newPost")
13         {
14             $api = "blogger";
15             $nr_params = 6;
16         }
17         elseif($method == "metaWeblog.newPost")
18         {
19             $api = "metaWeblog";
20             $nr_params = 5;
21         }
22         
23         // Do we have the required number of parameters?
24         if (count($params) != $nr_params)
25         {
26             // Raise an XML-RPC error
27             return new IXR_Error(-32602, "Invalid method parameters");
28         }
29         
30         // Parse parameters
31         if ($api == "blogger")
32         {
33             $blogid   = $params[1];
34             $username = $params[2];
35             $password = $params[3];
36             $content  = $params[4];
37             $publish  = (boolean) $params[5];
38         }
39         else
40         {
41             $blogid   = $params[0];
42             $username = $params[1];
43             $password = $params[2];
44             $content  = $params[3];
45             $publish  = (boolean) $params[4];
46         }
47       
48         // Exit if no blogid provided
49         if ($blogid == "")
50         {
51             // Raise an XML-RPC error
52             return new IXR_Error(-32602, "No blog ID provided");
53         }
54
55         // Check credentials
56         $auth = run('rpc:auth', array("username" => $username,
57                                       "password" => $password));
58         if ($auth['status'] == true)
59         {
60             $user = run('users:instance', array('user_id' => $username));
61             $post = run('posts:instance');
62             
63             $post->setOwner($user->getIdent());
64             $post->setWeblog($blogid);
65
66             $title = "";
67             
68             // Access level, only public or private supported
69             if ($publish == true)
70             {
71                 $access = "PUBLIC";
72             }
73             else
74             {
75                 $access = "user".$user->getIdent();
76             }
77             
78             $post->setAccess($access);
79             
80             if ($api == "blogger")
81             {
82                 $post->setTitle($title);
83                 $post->setBody($content);
84             }
85             else
86             {
87                 // TODO possibly handle interesting mt extensions
88                 //   (String) mt_text_more (the value for the additional entry text, combine with {{cut}}?)
89                 //   (String) mt_excerpt (the value for the excerpt field)
90                 //   (Boolean) mt_allow_comments (Elgg only handles this as a global and not per post option)
91                 $post->setTitle($content['title']);
92                 $post->setBody($content['description']);
93             }
94
95             // Save first because we need a postid to be able to add the tags later
96             $post->save();
97             
98             // We support mt_keywords if request is metaWeblog
99             if ($api != "blogger" && array_key_exists('mt_keywords', $content) && $post->getIdent() != "")
100             {
101                 $keywords $content['mt_keywords'];
102                 $tags = explode(',', $keywords);
103                 sort($tags);
104
105                 foreach ($tags as $tag_name)
106                 {
107                     $tag = new Tag('tags:instance');
108                     $tag->setOwner($user->getIdent());
109                     $tag->setRef($post->getIdent()); // new post id should be available
110                     $tag->setTagName($tag_name);
111                     $tag->setAccess($access);
112                     $tag->save();
113                 }
114             }
115
116             // Do we have a new post id?
117             if ($post->getIdent() != "")
118             {
119                 return $post->getIdent();
120             }
121             else
122             {
123                 // No new post id available, item hasn't been saved so raise an XML-RPC error
124                 return new IXR_Error(-32500, "Unable to save item");
125             }
126         }
127         else
128         {
129             // Invalid credentials, raise an XML-RPC error
130             return new IXR_Error($auth['code'], $auth['message']);
131         }
132     }
133     
134     function generic_editPost($params, $method)
135     {
136         // TODO editPost is almost similar to newPost, try to merge them
137         
138         // Number of parameters
139         $nr_params = null;
140         
141         if ($method == "blogger.editPost")
142         {
143             $api = "blogger";
144             $nr_params = 6;
145         }
146         elseif($method == "metaWeblog.editPost")
147         {
148             $api = "metaWeblog";
149             $nr_params = 5;
150         }
151         
152         // Do we have the required number of parameters?
153         if (count($params) != $nr_params)
154         {
155             // Raise an XML-RPC error
156             return new IXR_Error(-32602, "Invalid method parameters");
157         }
158         
159         // Parse parameters
160         if ($api == "blogger")
161         {
162             $postid   = $params[1];
163             $username = $params[2];
164             $password = $params[3];
165             $content  = $params[4];
166             $publish  = $params[5];
167         }
168         else
169         {
170             $postid   = $params[0];
171             $username = $params[1];
172             $password = $params[2];
173             $content  = $params[3];
174             $publish  = $params[4];
175         }
176
177         // Check credentials
178         $auth = run('rpc:auth', array("username" => $username,
179                                       "password" => $password));
180       
181         if ($auth['status'] == true)
182         {
183             $post = run('posts:instance', array('id' => $postid));
184             $user = run('users:instance', array('user_id' => $username));
185
186             // Access level, only public or private supported
187             if ($publish == true)
188             {
189                 $access = "PUBLIC";
190             }
191             else
192             {
193                 $access = "user".$user->getIdent();
194             }
195             
196             $post->setAccess($access);
197             
198             if ($api == "blogger")
199             {
200                 $post->setTitle($title);
201                 $post->setBody($content);
202             }
203             else
204             {
205                 // TODO possibly handle interesting mt extensions
206                 //   (String) mt_text_more (the value for the additional entry text, combine with {{cut}}?)
207                 //   (String) mt_excerpt (the value for the excerpt field)
208                 
209                 $post->setTitle($content['title']);
210                 $post->setBody($content['description']);
211
212                 // We support mt_keywords
213                 if (array_key_exists('mt_keywords', $content) && $result != false)
214                 {
215                     // Delete existing tags first
216                     $post->deleteTags();
217
218                     // Crunch and set the keywords as tags
219                     $keywords $content['mt_keywords'];
220                     $tags = explode(',', $keywords);
221
222                     foreach ($tags as $tag)
223                     {
224                         $tag = new Tag('tags:instance');
225                         $tag->setOwner($user->getIdent());
226                         $tag->setRef($post->getIdent());
227                         $tag->setTagName($tag);
228                         $tag->setAccess($access);
229                         $tag->save();
230                     }
231                 }
232             }
233
234             // Do we have a succesfull save?
235             if ($post->save() == true)
236             {
237                 return true;
238             }
239             else
240             {
241                 // Modification are not saved, raise an XML-RPC error
242                 return new IXR_Error(-32500, "Unable to update item");
243             }
244         }
245         else
246         {
247             // Invalid credentials, raise an XML-RPC error
248             return new IXR_Error($auth['code'], $auth['message']);
249         }
250     }
251     
252     /*
253      */
254     function blogger_deletePost($params, $method)
255     {
256         // Parse parameters
257         $postid   = $params[1];
258         $username = $params[2];
259         $password = $params[3];
260         $publish  = (boolean) $params[4]; // not used
261
262         // Check credentials
263         $auth = run('rpc:auth', array("username" => $username,
264                                       "password" => $password));
265       
266         if ($auth['status'] == true)
267         {
268             $post = run('posts:instance', array('id' => $postid));
269             
270             if ($post->delete() == true)
271             {
272                 return true;
273             }
274             else
275             {
276                 // Message not deleted, raise an XML-RPC error
277                 return new IXR_Error(-32500, "Unable to delete post");
278             }
279         }
280         else
281         {
282             // Invalid credentials, raise an XML-RPC error
283             return new IXR_Error($auth['code'], $auth['message']);
284         }
285     }
286     
287     /*
288      * Description: Returns a list of the most recent posts in the system.
289      *
290      * Parameters: String appkey, String blogid, String username, String password, int numberOfPosts
291      *
292      * Return value: on success, array of structs containing ISO.8601 dateCreated, String userid,
293      * String postid, String content; on failure, fault
294      *
295      * Notes: dateCreated is in the timezone of the weblog; blogid and
296      * blogid parameters are not being used
297      */
298     function generic_getRecentPosts($params, $method)
299     {
300         // Number of parameters
301         $nr_params = null;
302         
303         if ($method == "blogger.getRecentPosts")
304         {
305             $api = "blogger";
306             $nr_params = 5;
307         }
308         elseif($method == "metaWeblog.getRecentPosts")
309         {
310             $api = "metaWeblog";
311             $nr_params = 4;
312         }
313         
314         // Do we have the required number of parameters?
315         if (count($params) != $nr_params)
316         {
317             // Raise an XML-RPC error
318             return new IXR_Error(-32602, "Invalid method parameters");
319         }
320
321         // Parse parameters
322         if ($api == "blogger")
323         {
324             $blogid   = $params[1];
325             $username = $params[2];
326             $password = $params[3];
327             $numberOfPosts  = (int) $params[4];
328         }
329         elseif($api == "metaWeblog")
330         {
331             $blogid   = $params[0];
332             $username = $params[1];
333             $password = $params[2];
334             $numberOfPosts  = (int) $params[3];
335         }
336
337         // Check credentials
338         $auth = run('rpc:auth', array("username" => $username,
339                                       "password" => $password));
340
341         // Load the user
342         $user = run('users:instance', array('user_id' => $username));
343         $user_id = $user->getIdent();
344
345         if ($auth['status'] == true)
346         {
347             // Minimum of one, no maximum (for now)
348             if ($numberOfPosts >= 1)
349             {
350                 $weblog = run('weblogs:instance', array('user_id' => $user_id,
351                                                         'blog_id' => $blogid));
352
353                 // Get the posts
354                 $posts  = array_slice($weblog->getPosts(), 0, $numberOfPosts);
355
356                 // Global results array
357                 $result = array();
358
359                 if (!empty($posts)) {
360
361                     foreach($posts as $post_id)
362                     {
363                         $post = $weblog->getPost($post_id);
364                         
365                         // Local array to hold a single post
366                         $entry = array();
367                         
368                         // Fill the post array, same for blogger and metaWeblog
369                         $entry['dateCreated'] = new IXR_Date($post->getPosted());
370                         $entry['userid']      = $weblog->getIdent(); // is username
371                         $entry['postid']      = $post->getIdent();
372                         
373                         // Here the API's differ
374                         if ($api == "blogger")
375                         {
376                             $entry['content']     = $post->getBody();
377                         }
378                         else
379                         {
380                             $entry['title']       = $post->getTitle();
381                             $entry['description'] = $post->getBody();
382                             $entry['url']         = $post->getUrl();
383                             $entry['permalink']   = $post->getPermaLink();
384                         }
385  
386                         // We support tags as mt_keywords
387                         $tags = $post->getTags();
388
389                         if (!empty($tags)) {
390
391                             $keywords = array();
392
393                             foreach($tags as $tag_id)
394                             {
395                                 $tag = $post->getTag($tag_id);
396
397                                 $keywords[] = $tag->getTagName();
398                             }
399                             
400                             $str_keywords = implode(',', $keywords);
401
402
403                             // Add the keywords
404                             $entry['mt_keywords'] = $str_keywords;
405                         }
406
407                         // Add it to the results array
408                         $result[] = $entry
409                     }
410
411                     return $result;
412                 }
413                 else
414                 {
415                     // Numbers of requested posts can't be provided, raise an XML-RPC error
416                     return new IXR_Error(806, "No Such Item");
417                 }
418             }
419             else
420             {
421                 // Wrong amount of requested posts, raise an XML-RPC error
422                 return new IXR_Error(805, "Amount parameter must be 1 or more");
423             }
424         }
425         else
426         {
427             // Invalid credentials, raise an XML-RPC error
428             return new IXR_Error($auth['code'], $auth['message']);
429         }
430     }
431
432     /*
433      * Description: Returns a list of weblogs to which an author has posting privileges.
434      *
435      * Parameters: String appkey, String username, String password
436      *
437      * Return value: on success, array of structs containing String url, String blogid,
438      * String blogName; on failure, fault
439      *
440      * Notes: Currently elgg only provides one weblog per user; appkey is not being used
441      */
442     function blogger_getUsersBlogs($params, $method)
443     {
444         // Do we have the required number of parameters?
445         if (count($params) != 3)
446         {
447             // Raise an XML-RPC error
448             return new IXR_Error(-32602, "Invalid method parameters");
449         }
450
451         // Parse parameters
452         $username = $params[1];
453         $password = $params[2];
454
455         // Check credentials
456         $auth = run('rpc:auth', array("username" => $username,
457                                       "password" => $password));
458         
459         if ($auth['status'] == true)
460         {
461             // Load the user
462             $user = run('users:instance', array('user_id' => $username));
463             $user_id = $user->getIdent();
464
465             // Get the user's blogs
466             $blogs = $user->getBlogs();
467
468             // Result array
469             $result = array();
470             
471             foreach ($blogs as $blog_id)
472             {
473                 // Load the weblog
474                 $weblog = run('weblogs:instance', array('user_id' => $user_id,
475                                                         'blog_id' => $blog_id));
476             
477                 // Array to hold the weblog data
478                 $value = array();
479             
480                 $value['url']      = $weblog->getUrl();
481                 $value['blogid']   = $weblog->getIdent();
482                 $value['blogName'] = $weblog->getTitle();
483
484                 // Add it to the result array
485                 $result[] = $value;
486             }
487             
488             return $result;
489         }
490         else
491         {
492             // Invalid credentials, raise an XML-RPC error
493             return new IXR_Error($auth['code'], $auth['message']);
494         }
495     }
496     
497     /*
498      * Description: Returns information about an author in the system.
499      *
500      * Parameters: String appkey, String username, String password
501      *
502      * Return value: on success, struct containing String userid, String firstname,
503      * String lastname, String nickname, String email, String url; on failure, fault
504      *
505      * Notes: firstname is the Elgg username up to the first space character, and lastname
506      * is the username after the first space character. nickname is elgg username.
507      * appkey is not being used.
508      */
509     function blogger_getUserInfo($params, $method)
510     {
511         // Do we have the required number of parameters?
512         if (count($params) != 3)
513         {
514             // Raise an XML-RPC error
515             return new IXR_Error(-32602, "Invalid method parameters");
516         }
517         
518         // Parse parameters
519         $username = $params[1];
520         $password = $params[2];
521         
522