OSDN Git Service

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