OSDN Git Service

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