OSDN Git Service

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