OSDN Git Service

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