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

Revision 365, 9.1 kB (checked in by misja, 3 years ago)

* XML-RPC for the masses! The package is now self-supporting and does no longer depend on PEAR.
* The package was broken because of the recent code changes. Most if not all bugs should be fixed now.
* Added an experimental global variable $RPC to allow other packages to register their calls in the XML-RPC server. See README for some details.

  • Property svn:eol-style set to native
Line 
1 <?php
2
3     /* MoveableType API implementation
4      *
5      * Full implementation except for the mt.getTrackbackPings call
6      */
7
8     function mt_getRecenPostTitles($params, $method)
9     {
10         // Number of parameters
11         $nr_params = (int) 4;
12         
13         // Do we have the required number of parameters?
14         if (count($params) != $nr_params)
15         {
16             // Raise an XML-RPC error
17             return new IXR_Error(-32602, "Invalid method parameters");
18         }
19
20         // Parse parameters
21         
22         $blogid        = $params[0];
23         $username      = $params[1];
24         $password      = $params[2];
25         $numberOfPosts = $params[3];
26
27         // Check credentials
28         $auth = run('rpc:auth', array("username" => $username,
29                                       "password" => $password));
30         
31         if ($auth['status'] == true)
32         {
33             // Get a weblog instance
34             $weblog = run('weblogs:instance', array('user_id' => $username,
35                                                     'blog_id' => $blogid));
36
37             // Minimum of one, no maximum (for now)
38             if ($numberOfPosts >= 1)
39             {
40                 // Get the posts
41                 $posts  = array_slice($weblog->getPosts(), 0, $numberOfPosts);
42
43                 // Global results array
44                 $result = array();
45                 
46                 if (sizeof($posts) > 0)
47                 {
48                     foreach($posts as $post_id)
49                     {
50                         // Get the post object
51                         $post = $weblog->getPost($post_id);
52
53                         // Local array to hold a single post
54                         $entry = array();
55                         
56                         // Fill the post array, same for blogger and metaWeblog
57                         $entry['dateCreated'] = new IXR_Date($post->getPosted());
58                         $entry['userid']      = (int) $weblog->getIdent(); // is username for now
59                         $entry['postid']      = (int) $post->getIdent();
60                         $entry['title']       = $post->getTitle();
61                     }
62                     return $entry;
63                 }
64                 else
65                 {
66                     // Numbers of requested posts can't be provided, raise an XML-RPC error
67                     return new IXR_Error(806, "No Such Item");
68                 }
69             }
70             else
71             {
72                 // Wrong amount of requested posts, raise an XML-RPC error
73                 return new IXR_Error(805, "Amount parameter must be 1 or more");
74             }
75         }
76         else
77         {
78             // Invalid credentials, raise an XML-RPC error
79             return new IXR_Error($auth['code'], $auth['message']);
80         }
81     }
82
83     // returns an array of structs containing String categoryId and String categoryName; on failure, fault.
84     function mt_getCategoryList($params, $method)
85     {
86         // Number of parameters
87         $nr_params = 3;
88         
89         // Do we have the required number of parameters?
90         if (count($params) != $nr_params)
91         {
92             // Raise an XML-RPC error
93             return new IXR_Error(-32602, "Invalid method parameters");
94         }
95
96         // Parse parameters
97         $username = $params[1];
98         $password = $params[2];
99
100         // Check credentials
101         $auth = run('rpc:auth', array("username" => $username,
102                                       "password" => $password));
103
104         $cats = array();
105                 
106         if ($auth['status'] == true)
107         {
108             // Global results array
109             $results = array();
110
111             /* TODO think of a way to possibly use categories (or not) because it doesn't work.
112              * Some clients _need_ the categories so just provide them with a default one.
113              */
114
115             $results['categoryId'] = '999';
116             $results['categoryName'] = 'Default category';
117
118             // Append
119             $cats[] = $results;
120
121             return $cats;
122         }
123         else
124         {
125             // Invalid credentials, raise an XML-RPC error
126             return new IXR_Error($auth['code'], $auth['message']);
127         }
128     }
129
130     /* TODO Right now this function only returns a list of tags, which is not quite what the spec demands.
131      * Unfortunately elgg doesn't do categories, so think of something else   
132      */
133     function mt_getPostCategories($params, $method)
134     {
135         // Number of parameters
136         $nr_params = 3;
137         
138         // Do we have the required number of parameters?
139         if (count($params) != $nr_params)
140         {
141             // Raise an XML-RPC error
142             return new IXR_Error(-32602, "Invalid method parameters");           
143         }
144
145         // Parse parameters
146         $postId   = (int) $params[0];
147         $username = $params[1];
148         $password = $params[2];
149
150
151         // Check credentials
152         $auth = run('rpc:auth', array("username" => $username,
153                                       "password" => $password));
154         
155         if ($auth['status'] == true)
156         {
157             // Global results array
158             $results = array();
159
160             /* TODO think of a way to possibly use categories (or not) because it doesn't work.
161              * Som clients _need_ the categories so just provide them with a default one.
162              */
163
164             $results['categoryId'] = '999';
165             $results['categoryName'] = 'Default category';
166
167             return $results;
168         }
169         else
170         {
171             // Invalid credentials, raise an XML-RPC error
172             return new IXR_Error($auth['code'], $auth['message']);
173         }
174     }
175
176     /* TODO This function will always return true. Categories aren't available in elgg so
177      * think of something else
178      */
179     function mt_setPostCategories($params, $method)
180     {
181         // Number of parameters
182         $nr_params = 4;
183         
184         // Do we have the required number of parameters?
185         if (count($params) != $nr_params)
186         {
187             // Raise an XML-RPC error
188             return new IXR_Error(-32602, "Invalid method parameters");
189         }
190
191         // Parse parameters
192         $postId     = $params[0];
193         $username   = $params[1];
194         $password   = $params[2];
195         $categories = $params[3];
196
197         // Check credentials
198         $auth = run('rpc:auth', array("username" => $username,
199                                       "password" => $password));
200         
201         if ($auth['status'] == true)
202         {
203             // Array to hold the decoded tag id's
204             $tagIds = array();
205             
206             // Unpack the encoded categories/tags
207             foreach($categories as $value)
208             {
209                 // Process the following:
210                 // (string) $value['categoryId']
211                 // (boolean)$value['isPrimary']
212             }
213
214             /* TODO categories don't map to tags, think of something else */
215             return true;
216         }
217         else
218         {
219             // Invalid credentials, raise an XML-RPC error
220             return new IXR_Error($auth['code'], $auth['message']);
221         }
222     }
223
224     function mt_supportedMethods()
225     {
226         // TODO We will return the handler array. Not very safe since its content depends
227         // on the library loading order. It will do for now.
228         // Refactor to use the introsppection API
229         
230         $result = array();
231         
232         // $handlers is declared global so we can fetch it via $GLOBALS
233         foreach($GLOBALS['handlers'] as $key => $value)
234         {
235             $result[] = $key;
236         }
237
238         return $result;
239     }
240
241     function mt_supportedTextFilters($params, $method)
242     {
243         // Not supported, return an empty array
244         
245         $result = array();
246         
247         return $result;
248     }
249
250     /* This function just sets the access level to public*/
251     function mt_publishPost($params, $method)
252     {
253         // Number of parameters
254         $nr_params = 3;
255         
256         // Do we have the required number of parameters?
257         if ($params->getNumParams() != $nr_params)
258         {
259             // Raise an XML-RPC error
260             return new IXR_Error(-32602, "Invalid method parameters");
261         }
262
263         // Parse parameters
264         $postId   = $params[0];
265         $username = $params[1];
266         $password = $params[2];
267
268
269         // Check credentials
270         $auth = run('rpc:auth', array("username" => $username,
271                                       "password" => $password));
272         
273         if ($auth['status'] == true)
274         {
275             // Publish means public access
276             $access = "PUBLISH";
277
278             $post = run('posts:instance', array('id' => $postId));
279
280             $post->setAccess($access);
281             
282             if ($post->save() == true)
283             {
284                 return true;
285             }
286             else
287             {
288                 // The status hasn't been saved, raise an XML-RPC error
289                 return new IXR_Error(-32500, "Unable to publish post");           
290             }
291             
292         }
293         else
294         {
295             // Invalid credentials, raise an XML-RPC error
296             return new IXR_Error($auth['code'], $auth['message']);
297         }
298     }
299 ?>
300
Note: See TracBrowser for help on using the browser.