OSDN Git Service

merged 3.2 original code
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / xmlrpc / api_blogger.inc.php
1 <?php\r
2 /** \r
3   * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/) \r
4   * Copyright (C) 2002-2005 The Nucleus Group\r
5   *\r
6   * This program is free software; you can redistribute it and/or\r
7   * modify it under the terms of the GNU General Public License\r
8   * as published by the Free Software Foundation; either version 2\r
9   * of the License, or (at your option) any later version.\r
10   * (see nucleus/documentation/index.html#license for more info)\r
11   *\r
12   * $Id: api_blogger.inc.php,v 1.3 2005-03-12 06:19:06 kimitake Exp $\r
13   * $NucleusJP$\r
14   */\r
15   \r
16 /*\r
17  * This file contains definitions for the methods in the Blogger API\r
18  */\r
19 \r
20 \r
21         // blogger.newPost\r
22         $f_blogger_newPost_sig = array(array(\r
23                         // return type\r
24                         $xmlrpcString,  // itemid of the new item\r
25 \r
26                         // params:\r
27                         $xmlrpcString,  // appkey (ignored)\r
28                         $xmlrpcString,  // blogid\r
29                         $xmlrpcString,  // username\r
30                         $xmlrpcString,  // password\r
31                         $xmlrpcString,  // content\r
32                         $xmlrpcBoolean, // publish boolean (set to false to create draft)\r
33 \r
34                 ));\r
35         $f_blogger_newPost_doc = "Adds a new item to the given blog. Adds it as a draft when publish is false";\r
36         function f_blogger_newPost($m) {\r
37                 $blogid = _getScalar($m,1);\r
38                 $username = _getScalar($m,2);\r
39                 $password = _getScalar($m,3);\r
40                 $content = _getScalar($m,4);\r
41                 $publish = _getScalar($m,5);\r
42                 \r
43                 $title = blogger_extractTitle($content);\r
44                 $category = blogger_extractCategory($content);\r
45                 $content = blogger_removeSpecialTags($content); \r
46 \r
47                 return _addItem($blogid, $username, $password, $title, $content, '', $publish, 0, $category);\r
48         }\r
49 \r
50         // blogger.editPost\r
51         $f_blogger_editPost_sig = array(array(\r
52                         // return type\r
53                         $xmlrpcBoolean, // true or false\r
54 \r
55                         // params:\r
56                         $xmlrpcString,  // appkey (ignored)\r
57                         $xmlrpcString,  // postid\r
58                         $xmlrpcString,  // username\r
59                         $xmlrpcString,  // password\r
60                         $xmlrpcString,  // content\r
61                         $xmlrpcBoolean, // publish boolean (only considered when dealing with a draft)\r
62 \r
63                 ));\r
64         $f_blogger_editPost_doc = "Edits an item of a blog";\r
65         function f_blogger_editPost($m) {\r
66                 global $manager;\r
67                 \r
68                 $itemid = intval(_getScalar($m,1));\r
69                 $username = _getScalar($m,2);\r
70                 $password = _getScalar($m,3);\r
71                 $content = _getScalar($m,4);\r
72                 $publish = _getScalar($m,5);\r
73 \r
74                 $title = blogger_extractTitle($content);\r
75                 $category = blogger_extractCategory($content);\r
76                 $content = blogger_removeSpecialTags($content); \r
77 \r
78                 // get old title and extended part \r
79                 if (!$manager->existsItem($itemid,1,1))\r
80                         return _error(6,"No such item ($itemid)");\r
81                 $old =& $manager->getItem($itemid,1,1);\r
82 \r
83                 $blogid = getBlogIDFromItemID($itemid);\r
84 \r
85                 $blog = new BLOG($blogid);\r
86                 $catid = $blog->getCategoryIdFromName($category);\r
87                 \r
88                 if ($old['draft'] && $publish) {\r
89                         $wasdraft = 1;\r
90                         $publish = 1;\r
91                 } else {\r
92                         $wasdraft = 0;\r
93                 }\r
94 \r
95                 return _edititem($itemid, $username, $password, $catid, $title, $content, $old['more'], $wasdraft, $publish, $old['closed']);\r
96         }\r
97 \r
98 \r
99         // blogger.getUsersBlogs\r
100         $f_blogger_getUsersBlogs_sig = array(array(\r
101                         // return type\r
102                         $xmlrpcArray,   // array containing structs containing blog info\r
103 \r
104                         // params:\r
105                         $xmlrpcString,  // appkey (ignored)\r
106                         $xmlrpcString,  // username\r
107                         $xmlrpcString,  // password\r
108                 ));\r
109         $f_blogger_getUsersBlogs_doc = "Returns a list of all the blogs where the given member is on the team";\r
110         function f_blogger_getUsersBlogs($m) {\r
111                 $username = _getScalar($m,1);\r
112                 $password = _getScalar($m,2);\r
113 \r
114                 return _getUsersBlogs($username, $password);\r
115         }\r
116 \r
117         // blogger.getRecentPosts\r
118         $f_blogger_getRecentPosts_sig = array(array(\r
119                         // return type\r
120                         $xmlrpcArray,   // array of strucs (representing items)\r
121 \r
122                         // params\r
123                         $xmlrpcString,  // appkey (ignored)\r
124                         $xmlrpcString,  // blogid\r
125                         $xmlrpcString,  // username\r
126                         $xmlrpcString,  // password\r
127                         $xmlrpcInt,     // amount of items to return (max = 20)\r
128                 ));\r
129         $f_blogger_getRecentPosts_doc = "Returns a maximum of 20 recent items";\r
130         function f_blogger_getRecentPosts($m) {\r
131                 $blogid = _getScalar($m, 1);\r
132                 $username = _getScalar($m, 2);\r
133                 $password = _getScalar($m, 3);\r
134                 $amount = _getScalar($m, 4);\r
135 \r
136                 return _getRecentItemsBlogger($blogid, $username, $password, $amount);\r
137         }\r
138 \r
139 \r
140         // blogger.getPost\r
141         $f_blogger_getPost_sig = array(array(\r
142                         // return type\r
143                         $xmlrpcStruct,  // A struct representing the item\r
144 \r
145                         // params\r
146                         $xmlrpcString,  // appkey (ignored)\r
147                         $xmlrpcString,  // postid\r
148                         $xmlrpcString,  // username\r
149                         $xmlrpcString,  // password\r
150                 ));\r
151         $f_blogger_getPost_doc = "Returns an item (only the item body!)";\r
152         function f_blogger_getPost($m) {\r
153                 $postid = _getScalar($m, 1);\r
154                 $username = _getScalar($m, 2);\r
155                 $password = _getScalar($m, 3);\r
156 \r
157                 return _getItemBlogger($postid, $username, $password);\r
158         }\r
159 \r
160 \r
161         // blogger.deletePost\r
162         $f_blogger_deletePost_sig = array(array(\r
163                         // return type\r
164                         $xmlrpcBoolean, // boolean (ok or not ok)\r
165 \r
166                         // params\r
167                         $xmlrpcString,  // appkey (ignored)\r
168                         $xmlrpcString,  // postid\r
169                         $xmlrpcString,  // username\r
170                         $xmlrpcString,  // password\r
171                         $xmlrpcBoolean, // publish (ignored)\r
172                 ));\r
173         $f_blogger_deletePost_doc = "Deletes an item";\r
174         function f_blogger_deletePost($m) {\r
175                 $postid = _getScalar($m,1);\r
176                 $username = _getScalar($m, 2);\r
177                 $password = _getScalar($m, 3);\r
178 \r
179                 return _deleteItem($postid, $username, $password);\r
180         }\r
181 \r
182         // blogger.getTemplate\r
183         $f_blogger_getTemplate_sig = array(array(\r
184                         // return type\r
185                         $xmlrpcString,  // the template\r
186 \r
187                         // params\r
188                         $xmlrpcString,  // appkey (ignored)\r
189                         $xmlrpcString,  // blogid\r
190                         $xmlrpcString,  // username\r
191                         $xmlrpcString,  // password\r
192                         $xmlrpcString,  // type of template (main/archiveIndex)\r
193                                 ));\r
194         $f_blogger_getTemplate_doc = "Returns the required part of the default skin for the given blog";\r
195         function f_blogger_getTemplate($m) {\r
196                 $blogid = _getScalar($m,1);\r
197                 $username = _getScalar($m,2);\r
198                 $password = _getScalar($m,3);\r
199                 $type = _getScalar($m,4);\r
200 \r
201                 switch($type) {\r
202                         case "main":\r
203                                 $type = "index";\r
204                                 break;\r
205                         case "archiveIndex":\r
206                                 $type = "archivelist";\r
207                                 break;\r
208                 }\r
209 \r
210                 return _getSkinPart($blogid, $username, $password, $type);\r
211         }\r
212 \r
213         // blogger.setTemplate\r
214         $f_blogger_setTemplate_sig = array(array(\r
215                         // return type\r
216                         $xmlrpcBoolean, // OK or not OK\r
217 \r
218                         // params\r
219                         $xmlrpcString,  // appkey (ignored)\r
220                         $xmlrpcString,  // blogid\r
221                         $xmlrpcString,  // username\r
222                         $xmlrpcString,  // password\r
223                         $xmlrpcString,  // template contents\r
224                         $xmlrpcString,  // type of template (main/archiveIndex)\r
225                         ));\r
226         $f_blogger_setTemplate_doc = "Changes a part of the default skin for the selected blog";\r
227         function f_blogger_setTemplate($m) {\r
228                 $blogid = _getScalar($m,1);\r
229                 $username = _getScalar($m,2);\r
230                 $password = _getScalar($m,3);\r
231                 $content = _getScalar($m,4);\r
232                 $type = _getScalar($m,5);\r
233 \r
234                 switch($type) {\r
235                         case "main":\r
236                                 $type = "index";\r
237                                 break;\r
238                         case "archiveIndex":\r
239                                 $type = "archivelist";\r
240                                 break;\r
241                 }\r
242 \r
243                 return _setSkinPart($blogid, $username, $password, $content, $type);\r
244         }\r
245 \r
246         // blogger.getUserInfo\r
247         $f_blogger_getUserInfo_sig = array(array(\r
248                         // return type\r
249                         $xmlrpcStruct,  // Struct\r
250 \r
251                         // params\r
252                         $xmlrpcString,  // appkey (ignored)\r
253                         $xmlrpcString,  // username\r
254                         $xmlrpcString,  // password\r
255                         ));\r
256         $f_blogger_getUserInfo_doc = "Returns info on the user";\r
257         function f_blogger_getUserInfo($m) {\r
258                 $username = _getScalar($m,1);\r
259                 $password = _getScalar($m,2);\r
260 \r
261                 return _getUserInfo($username, $password);\r
262         }\r
263 \r
264 \r
265         /**\r
266           * Returns a list of recent items \r
267           */\r
268         function _getRecentItemsBlogger($blogid, $username, $password, $amount) {\r
269                 \r
270                 $blogid = intval($blogid);\r
271                 $amount = intval($amount);\r
272                         \r
273                 // 1. login\r
274                 $mem = new MEMBER();\r
275                 if (!$mem->login($username, $password))\r
276                         return _error(1,"Could not log in");\r
277 \r
278                 // 2. check if allowed \r
279                 if (!BLOG::existsID($blogid))\r
280                         return _error(2,"No such blog ($blogid)");\r
281                 if (!$mem->teamRights($blogid))\r
282                         return _error(3,"Not a team member");\r
283                 $amount = intval($amount);\r
284                 if (($amount < 1) or ($amount > 20))\r
285                         return _error(5,"Amount parameter must be in range 1..20");\r
286 \r
287                 // 3. create and return list of recent items\r
288                 // Struct returned has dateCreated, userid, blogid and content\r
289                 \r
290                 $blog = new BLOG($blogid);\r
291 \r
292                 $structarray = array();         // the array in which the structs will be stored\r
293 \r
294                 $query = "SELECT mname, ibody, iauthor, ibody, inumber, ititle as title, itime, cname as category"\r
295                            .' FROM '.sql_table('item').', '.sql_table('category').', '.sql_table('member')\r
296                            ." WHERE iblog=$blogid and icat=catid and iauthor=mnumber"\r
297                            ." ORDER BY itime DESC"\r
298                            ." LIMIT $amount";\r
299                 $r = sql_query($query);\r
300                 \r
301                 while ($row = mysql_fetch_assoc($r)) {\r
302                 \r
303                         // remove linebreaks if needed\r
304                         if ($blog->convertBreaks())\r
305                                 $row['ibody'] = removeBreaks($row['ibody']);\r
306 \r
307                         $content = blogger_specialTags($row) . $row['ibody'];\r
308                 \r
309                         $newstruct = new xmlrpcval(array(\r
310                                 "userid" => new xmlrpcval($row['iauthor'],"string"),\r
311                                 "dateCreated" => new xmlrpcval(iso8601_encode(strtotime($row['itime'])),"dateTime.iso8601"),\r
312                                 "blogid" => new xmlrpcval($blogid,"string"),\r
313                                 "content" => new xmlrpcval($content,"string"),\r
314                                 "postid" => new xmlrpcval($row['inumber'],"string"),\r
315                                 "authorName" => new xmlrpcval($row['mname'],'string'),\r
316                                 "title" => new xmlrpcval($row['title'],'string'),\r
317                         ),'struct');\r
318                         array_push($structarray, $newstruct);           \r
319                 }\r
320 \r
321                 return new xmlrpcresp(new xmlrpcval( $structarray , "array"));\r
322 \r
323         }\r
324         \r
325         /**\r
326           * Returns one item (Blogger version)\r
327           */\r
328         function _getItemBlogger($itemid, $username, $password) {\r
329                 global $manager;\r
330         \r
331                 // 1. login\r
332                 $mem = new MEMBER();\r
333                 if (!$mem->login($username, $password))\r
334                         return _error(1,"Could not log in");\r
335 \r
336                 // 2. check if allowed \r
337                 if (!$manager->existsItem($itemid,1,1))\r
338                         return _error(6,"No such item ($itemid)");\r
339                 $blogid = getBlogIDFromItemID($itemid);\r
340                 if (!$mem->teamRights($blogid))\r
341                         return _error(3,"Not a team member");\r
342 \r
343                 // 3. return the item\r
344                 // Structure returned has dateCreated, userid, blogid and content\r
345 \r
346                 $item =& $manager->getItem($itemid,1,1); // (also allow drafts and future items)\r
347                 $blog = new BLOG($blogid);\r
348                 \r
349                 // get category\r
350                 $item['category'] = $blog->getCategoryName($item['catid']);\r
351                 \r
352                 // remove linebreaks if needed\r
353                 if ($blog->convertBreaks())\r
354                         $item['body'] = removeBreaks($item['body']);\r
355 \r
356                 $content = blogger_specialTags($item) . $item['body'];\r
357 \r
358                 $newstruct = new xmlrpcval(array(\r
359                         "dateCreated" => new xmlrpcval(iso8601_encode($item['timestamp']),"dateTime.iso8601"),\r
360                         "userid" => new xmlrpcval($item['authorid'],"string"),\r
361                         "blogid" => new xmlrpcval($blogid,"string"),\r
362                         "content" => new xmlrpcval($content,"string")\r
363                 ),'struct');\r
364 \r
365                 return new xmlrpcresp($newstruct);\r
366 \r
367 \r
368         }\r
369 \r
370 \r
371         function blogger_extractTitle($body) {\r
372                 return blogger_matchTag('title',$body);\r
373         }\r
374 \r
375         function blogger_extractCategory($body) {\r
376                 return blogger_matchTag('category',$body);\r
377         }       \r
378         \r
379         function blogger_matchTag($tag, $body) {\r
380                 if (preg_match("/<" . $tag .">(.+?)<\/".$tag.">/is",$body,$match)) \r
381                         return $match[1];\r
382                 else\r
383                         return "";\r
384         }\r
385 \r
386         function blogger_removeSpecialTags($body) {\r
387                 $body = preg_replace("/<title>(.+?)<\/title>/","",$body);\r
388                 $body = preg_replace("/<category>(.+?)<\/category>/","",$body);\r
389                 return trim($body);\r
390         }\r
391         \r
392         function blogger_specialTags($item) {\r
393                 $result = "<title>". $item['title']."</title>";\r
394                 $result .= "<category>".$item['category']."</category>";\r
395                 return $result;\r
396         }\r
397         \r
398 \r
399 \r
400         $functionDefs = array_merge($functionDefs,\r
401                 array(\r
402                          "blogger.getUsersBlogs" =>\r
403                          array( "function" => "f_blogger_getUsersBlogs",\r
404                                 "signature" => $f_blogger_getUsersBlogs_sig,\r
405                                 "docstring" => $f_blogger_getUsersBlogs_doc),\r
406 \r
407                          "blogger.newPost" =>\r
408                          array( "function" => "f_blogger_newPost",\r
409                                 "signature" => $f_blogger_newPost_sig,\r
410                                 "docstring" => $f_blogger_newPost_doc),\r
411 \r
412                          "blogger.editPost" =>\r
413                          array( "function" => "f_blogger_editPost",\r
414                                 "signature" => $f_blogger_editPost_sig,\r
415                                 "docstring" => $f_blogger_editPost_doc),\r
416 \r
417                          "blogger.deletePost" =>\r
418                          array( "function" => "f_blogger_deletePost",\r
419                                 "signature" => $f_blogger_deletePost_sig,\r
420                                 "docstring" => $f_blogger_deletePost_doc),\r
421 \r
422                          "blogger.getPost" =>\r
423                          array( "function" => "f_blogger_getPost",\r
424                                 "signature" => $f_blogger_getPost_sig,\r
425                                 "docstring" => $f_blogger_getPost_doc),\r
426 \r
427                          "blogger.getRecentPosts" =>\r
428                          array( "function" => "f_blogger_getRecentPosts",\r
429                                 "signature" => $f_blogger_getRecentPosts_sig,\r
430                                 "docstring" => $f_blogger_getRecentPosts_doc),\r
431 \r
432                          "blogger.getUserInfo" =>\r
433                          array( "function" => "f_blogger_getUserInfo",\r
434                                 "signature" => $f_blogger_getUserInfo_sig,\r
435                                 "docstring" => $f_blogger_getUserInfo_doc),\r
436 \r
437                          "blogger.getTemplate" =>\r
438                          array( "function" => "f_blogger_getTemplate",\r
439                                 "signature" => $f_blogger_getTemplate_sig,\r
440                                 "docstring" => $f_blogger_getTemplate_doc),\r
441 \r
442                          "blogger.setTemplate" =>\r
443                          array( "function" => "f_blogger_setTemplate",\r
444                                 "signature" => $f_blogger_setTemplate_sig,\r
445                                 "docstring" => $f_blogger_setTemplate_doc)\r
446                 \r
447                 )\r
448         );\r
449 \r
450 \r
451 ?>\r