OSDN Git Service

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