OSDN Git Service

git-svn-id: https://svn.sourceforge.jp/svnroot/nucleus-jp/nucleus-jp/trunk@910 1ca29b...
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / xmlrpc / api_metaweblog.inc.php
1 <?php
2
3 /*
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5  * Copyright (C) 2002-2009 The Nucleus Group
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * (see nucleus/documentation/index.html#license for more info)
12  */
13
14 /**
15  *      This file contains definitions for the methods of the metaWeblog API
16  *
17  * @license http://nucleuscms.org/license.txt GNU General Public License
18  * @copyright Copyright (C) 2002-2009 The Nucleus Group
19  * @version $Id$
20  * $NucleusJP: api_metaweblog.inc.php,v 1.5 2006/07/17 20:03:45 kimitake Exp $
21  */
22
23
24         // metaWeblog.newPost
25         $f_metaWeblog_newPost_sig = array(array(
26                         // return type
27                         $xmlrpcString,  // itemid of the new item
28
29                         // params:
30                         $xmlrpcString,  // blogid
31                         $xmlrpcString,  // username
32                         $xmlrpcString,  // password
33                         $xmlrpcStruct,  // content
34                         $xmlrpcBoolean, // publish boolean (set to false to create draft)
35
36                 ));
37         $f_metaWeblog_newPost_doc = "Adds a new item to the given blog. Adds it as a draft when publish is false";
38         function f_metaWeblog_newPost($m) {
39                 global $manager;
40                 
41                 $blogid =                       _getScalar($m,0);
42                 $username =             _getScalar($m,1);
43                 $password =             _getScalar($m,2);
44                 $struct =                       $m->getParam(3);
45                 
46                 $content =              _getStructVal($struct, 'description');
47                 $more =                 _getStructVal($struct, 'mt_text_more');
48                 $title =                _getStructVal($struct, 'title');
49
50                 // category is optional (thus: be careful)!
51                 $catlist = $struct->structmem('categories');
52                 if ($catlist && ($catlist->kindOf() == "array") && ($catlist->arraysize() > 0))
53                         $category = _getArrayVal($catlist, 0);
54                 
55                 
56 //              $comments = (int) _getStructVal($struct, 'mt_allow_comments') ? 0 : 1;\r
57                 $comments = $struct->structmem('mt_allow_comments');\r
58                 if ($comments) {\r
59                         $closed = (intval(_getStructVal($struct, 'mt_allow_comments')) == 1) ? 0 : 1;\r
60                 } else {\r
61                         $closed = 0;\r
62                 }\r
63                                         $publish = _getScalar($m,4);
64
65
66                 // Add item
67 //              $res = _addItem($blogid, $username, $password, $title, $content, $more, $publish, $comments, $category);\r
68                 $res = _addItem($blogid, $username, $password, $title, $content, $more, $publish, $closed, $category);\r
69                                                         
70                 // Handle trackbacks
71                 $trackbacks = array();
72                 $tblist = $struct->structmem('mt_tb_ping_urls');
73                 if ($tblist && ($tblist->kindOf() == "array") && ($tblist->arraysize() > 0)) {
74                         
75                         for ($i = 0; $i < $tblist->arraysize(); $i++) {
76                                 $trackbacks[] = _getArrayVal($tblist, $i);
77                         }
78                         
79                         $manager->notify('SendTrackback', array ('tb_id' => $itemid, 'urls' => & $trackbacks));
80                 }
81
82                 return $res;
83         }
84
85
86         // metaWeblog.getCategories
87         $f_metaWeblog_getCategories_sig = array(array(
88                 // return
89                 $xmlrpcStruct,  // categories for blog
90
91                 // params
92                 $xmlrpcString,  // blogid
93                 $xmlrpcString,  // username
94                 $xmlrpcString,  // password
95
96         ));
97         $f_metaWeblog_getCategories_doc = "Returns the categories for a given blog";
98         function f_metaWeblog_getCategories($m) {
99                 $blogid =       _getScalar($m,0);
100                 $username =     _getScalar($m,1);
101                 $password =     _getScalar($m,2);
102
103                 return _categoryList($blogid, $username, $password);
104         }
105
106
107         // metaWeblog.getPost
108         $f_metaWeblog_getPost_sig = array(array(
109                 // return
110                 $xmlrpcStruct,  // the juice
111
112                 // params
113                 $xmlrpcString,  // itemid
114                 $xmlrpcString,  // username
115                 $xmlrpcString,  // password
116
117         ));
118         $f_metaWeblog_getPost_doc = "Retrieves a post";
119         function f_metaWeblog_getPost($m) {
120                 $itemid =       _getScalar($m,0);
121                 $username =     _getScalar($m,1);
122                 $password =     _getScalar($m,2);
123
124                 return _mw_getPost($itemid, $username, $password);
125         }
126
127
128         // metaWeblog.editPost
129         $f_metaWeblog_editPost_sig = array(array(
130                         // return type
131                         $xmlrpcBoolean, // true
132
133                         // params:
134                         $xmlrpcString,  // itemid
135                         $xmlrpcString,  // username
136                         $xmlrpcString,  // password
137                         $xmlrpcStruct,  // content
138                         $xmlrpcBoolean, // publish boolean (set to false to create draft)
139
140                 ));
141         $f_metaWeblog_editPost_doc = "Edits an item";
142         function f_metaWeblog_editPost($m) {
143                 global $manager;
144
145                 $itemid =                       _getScalar($m,0);
146                 $username =             _getScalar($m,1);
147                 $password =             _getScalar($m,2);
148
149                 $category = '';
150                 $struct =                       $m->getParam(3);
151                         $content =              _getStructVal($struct, 'description');
152                         $title =                _getStructVal($struct, 'title');
153
154                         // category is optional (thus: be careful)!
155                         $catlist = $struct->structmem('categories');
156                         if ($catlist && ($catlist->kindOf() == "array") && ($catlist->arraysize() > 0)) {
157                                 $category = _getArrayVal($catlist, 0);
158                         }
159
160                 $publish = _getScalar($m,4);
161
162                 
163                 // get old title and extended part
164                 if (!$manager->existsItem($itemid,1,1))
165                         return _error(6,"No such item ($itemid)");
166                 $blogid = getBlogIDFromItemID($itemid);
167
168                 $old =& $manager->getItem($itemid,1,1);
169
170                 if ($category == '')
171                 {
172                         // leave category unchanged when not present
173                         $catid = $old['catid'];
174                 }
175                 else
176                 {
177                         $blog = new BLOG($blogid);
178                         $catid = $blog->getCategoryIdFromName($category);
179                 }
180
181                 if ($old['draft'] && $publish) {
182                         $wasdraft = 1;
183                         $publish = 1;
184                 } else {
185                         $wasdraft = 0;
186                 }
187
188                 $more = $struct->structmem('mt_text_more');
189                 if ($more) {
190                         $more = _getStructVal($struct, 'mt_text_more');
191                 } else {
192                         $more = $old['more'];
193                 }
194                 
195                 $comments = $struct->structmem('mt_allow_comments');
196                 if ($comments) {
197 //                      $comments = (int) _getStructVal($struct, 'mt_allow_comments') ? 0 : 1;\r
198                         $closed = (intval(_getStructVal($struct, 'mt_allow_comments')) == 1) ? 0 : 1;\r
199                 } else {
200 //                      $comments = $old['closed'];\r
201                         $closed = $old['closed'];\r
202                 }
203
204 //              $res = _edititem($itemid, $username, $password, $catid, $title, $content, $more, $wasdraft, $publish, $comments);\r
205                 $res = _edititem($itemid, $username, $password, $catid, $title, $content, $more, $wasdraft, $publish, $closed);
206                 // Handle trackbacks
207                 $trackbacks = array();
208                 $tblist = $struct->structmem('mt_tb_ping_urls');
209                 if ($tblist && ($tblist->kindOf() == "array") && ($tblist->arraysize() > 0)) {
210                         
211                         for ($i = 0; $i < $tblist->arraysize(); $i++) {
212                                 $trackbacks[] = _getArrayVal($tblist, $i);
213                         }
214                         
215                         $manager->notify('SendTrackback', array ('tb_id' => $itemid, 'urls' => & $trackbacks));
216                 }
217
218                 return $res;
219         }
220
221         // metaWeblog.newMediaObject
222         $f_metaWeblog_newMediaObject_sig = array(array(
223                 //  return type
224                 $xmlrpcStruct,          // "url" element
225
226                 // params
227                 $xmlrpcString,          // blogid
228                 $xmlrpcString,          // username
229                 $xmlrpcString,          // password
230                 $xmlrpcStruct           // 'name', 'type' and 'bits'
231         ));
232         $f_metaWeblog_newMediaObject_doc = 'Uploads a file to to the media library of the user';
233         function f_metaWeblog_newMediaObject($m) {
234                 $blogid         = _getScalar($m, 0);
235                 $username       = _getScalar($m, 1);
236                 $password       = _getScalar($m, 2);
237
238                 $struct         = $m->getParam(3);
239                         $name   = _getStructVal($struct, 'name');
240                         $type   = _getStructVal($struct, 'type');
241                         $bits   = _getStructVal($struct, 'bits');
242
243                 return _newMediaObject($blogid, $username, $password, array('name' => $name, 'type' => $type, 'bits' => $bits));
244         }
245
246         // metaWeblog.getRecentPosts
247         $f_metaWeblog_getRecentPosts_sig = array(array(
248                 // return type
249                 $xmlrpcStruct,          // array of structs
250
251                 // params
252                 $xmlrpcString,          // blogid
253                 $xmlrpcString,          // username
254                 $xmlrpcString,          // password
255                 $xmlrpcInt                      // number of posts
256         ));
257         $f_metaWeblog_getRecentPosts_doc = 'Returns recent weblog items.';
258         function f_metaWeblog_getRecentPosts($m) {
259                 $blogid         = _getScalar($m, 0);
260                 $username       = _getScalar($m, 1);
261                 $password       = _getScalar($m, 2);
262                 $amount         = intval(_getScalar($m, 3));
263
264                 return _getRecentItemsMetaWeblog($blogid, $username, $password, $amount);
265         }
266
267         function _getRecentItemsMetaWeblog($blogid, $username, $password, $amount) {
268
269                 $blogid = intval($blogid);
270                 $amount = intval($amount);
271
272                 // 1. login
273                 $mem = new MEMBER();
274                 if (!$mem->login($username, $password))
275                         return _error(1,"Could not log in");
276
277                 // 2. check if allowed
278                 if (!BLOG::existsID($blogid))
279                         return _error(2,"No such blog ($blogid)");
280                 if (!$mem->teamRights($blogid))
281                         return _error(3,"Not a team member");
282                 $amount = intval($amount);
283                 if (($amount < 1) or ($amount > 20))
284                         return _error(5,"Amount parameter must be in range 1..20");
285
286                 // 3. create and return list of recent items
287                 // Struct returned has dateCreated, userid, blogid and content
288
289                 $blog = new BLOG($blogid);
290
291                 $structarray = array();         // the array in which the structs will be stored
292
293                 $query = "SELECT mname, ibody, imore, iauthor, ibody, inumber, ititle as title, itime, cname as category, iclosed"
294                            .' FROM '.sql_table('item').', '.sql_table('category').', '.sql_table('member')
295                            ." WHERE iblog=$blogid and icat=catid and iauthor=mnumber"
296                            ." ORDER BY itime DESC"
297                            ." LIMIT $amount";
298                 $r = sql_query($query);
299
300                 while ($row = mysql_fetch_assoc($r)) {
301
302                         // remove linebreaks if needed
303                         if ($blog->convertBreaks()) {
304                                 $row['ibody'] = removeBreaks($row['ibody']);
305                                 $row['imore'] = removeBreaks($row['imore']);
306                         }
307
308                         $newstruct = new xmlrpcval(array(
309                                 "dateCreated" => new xmlrpcval(iso8601_encode(strtotime($row['itime'])),"dateTime.iso8601"),
310                                 "userid" => new xmlrpcval($row['iauthor'],"string"),
311                                 "blogid" => new xmlrpcval($blogid,"string"),
312                                 "postid" => new xmlrpcval($row['inumber'],"string"),
313                                 "description" => new xmlrpcval($row['ibody'],"string"),
314                                 "title" => new xmlrpcval($row['title'],"string"),
315                                 "categories" => new xmlrpcval(
316                                                 array(
317                                                         new xmlrpcval($row['category'], "string")
318                                                 )
319                                                 ,"array"),
320                                 
321                                                 
322                                 "mt_text_more"          => new xmlrpcval($row['imore'], "string"),
323                                 "mt_allow_comments" => new xmlrpcval($row['iclosed'] ? 0 : 1, "int"),
324                                 "mt_allow_pings"        => new xmlrpcval(1, "int")
325                         ),'struct');
326
327                 //TODO: String link?
328                 //TODO: String permaLink?
329
330
331                         array_push($structarray, $newstruct);
332                 }
333
334                 return new xmlrpcresp(new xmlrpcval( $structarray , "array"));
335         }
336
337         function _newMediaObject($blogid, $username, $password, $info) {
338                 global $CONF, $DIR_MEDIA, $DIR_LIBS;
339
340                 // - login
341                 $mem = new MEMBER();
342                 if (!$mem->login($username, $password))
343                         return _error(1,'Could not log in');
344
345                 // - check if team member
346                 if (!BLOG::existsID($blogid))
347                         return _error(2,"No such blog ($blogid)");
348                 if (!$mem->teamRights($blogid))
349                         return _error(3,'Not a team member');
350
351                 $b = new BLOG($blogid);
352
353                 // - decode data
354                 $data = $info['bits']; // decoding was done transparantly by xmlrpclib
355
356                 // - check filesize
357                 if (strlen($data) > $CONF['MaxUploadSize'])
358                         return _error(9, 'filesize is too big');
359
360
361                 // - check if filetype is allowed (check filename)
362                 $filename = $info['name'];
363                 $ok = 0;
364                 $allowedtypes = explode (',', $CONF['AllowedTypes']);
365                 foreach ( $allowedtypes as $type )
366                         if (eregi("\." .$type. "$",$filename)) $ok = 1;
367                 if (!$ok)
368                         _error(8, 'Filetype is not allowed');
369
370                 // - add file to media library
371                 include_once($DIR_LIBS . 'MEDIA.php');  // media classes
372
373                 // always use private media library of member
374                 $collection = $mem->getID();
375
376                 // prefix filename with current date (YYYY-MM-DD-)
377                 // this to avoid nameclashes
378                 if ($CONF['MediaPrefix'])
379                         $filename = strftime("%Y%m%d-", time()) . $filename;
380
381                 $res = MEDIA::addMediaObjectRaw($collection, $filename, $data);
382                 if ($res)
383                         return _error(10, $res);
384
385                 // - return URL
386                 $urlstruct = new xmlrpcval(array(
387                         "url" => new xmlrpcval($CONF['MediaURL'] . $collection. '/' . $filename,'string')
388                 ),'struct');
389
390                 return new xmlrpcresp($urlstruct);
391         }
392
393         function _categoryList($blogid, $username, $password) {
394                 // 1. login
395                 $mem = new MEMBER();
396                 if (!$mem->login($username, $password))
397                         return _error(1,"Could not log in");
398
399                 // check if on team and blog exists
400                 if (!BLOG::existsID($blogid))
401                         return _error(2,"No such blog ($blogid)");
402                 if (!$mem->teamRights($blogid))
403                         return _error(3,"Not a team member");
404
405                 $b = new BLOG($blogid);
406
407                 $categorystruct = array();
408
409                 $query =  "SELECT cname, cdesc, catid"
410                                 . ' FROM '.sql_table('category')
411                                 . " WHERE cblog=" . intval($blogid)
412                                 . " ORDER BY cname";
413                 $r = sql_query($query);
414
415                 while ($obj = mysql_fetch_object($r)) {
416
417                         $categorystruct[$obj->cname] = new xmlrpcval(
418                                 array(
419                                         "description" => new xmlrpcval($obj->cdesc,"string"),
420                                         "htmlUrl" => new xmlrpcval($b->getURL() . "?catid=" . $obj->catid ,"string"),
421                                         "rssUrl" => new xmlrpcval("","string")
422                                 )
423                         ,'struct');
424                 }
425
426
427                 return new xmlrpcresp(new xmlrpcval( $categorystruct , "struct"));
428
429         }
430
431
432         function _mw_getPost($itemid, $username, $password) {
433                 global $manager;
434
435                 // 1. login
436                 $mem = new MEMBER();
437                 if (!$mem->login($username, $password))
438                         return _error(1,"Could not log in");
439
440                 // 2. check if allowed
441                 if (!$manager->existsItem($itemid,1,1))
442                         return _error(6,"No such item ($itemid)");
443                 $blogid = getBlogIDFromItemID($itemid);
444                 if (!$mem->teamRights($blogid))
445                         return _error(3,"Not a team member");
446
447                 // 3. return the item
448                 $item =& $manager->getItem($itemid,1,1); // (also allow drafts and future items)
449
450                 $b = new BLOG($blogid);
451                 if ($b->convertBreaks()) {
452                         $item['body'] = removeBreaks($item['body']);
453                         $item['more'] = removeBreaks($item['more']);
454                 }
455
456                 $categoryname = $b->getCategoryName($item['catid']);
457
458                 $newstruct = new xmlrpcval(array(
459                         "dateCreated" => new xmlrpcval(iso8601_encode($item['timestamp']),"dateTime.iso8601"),
460                         "userid" => new xmlrpcval($item['authorid'],"string"),
461                         "blogid" => new xmlrpcval($blogid,"string"),
462                         "postid" => new xmlrpcval($itemid, "string"),
463                         "description" => new xmlrpcval($item['body'],"string"),
464                         "title" => new xmlrpcval($item['title'],"string"),
465                         "categories" => new xmlrpcval(
466                                         array(
467                                                 new xmlrpcval($categoryname, "string")
468                                         )
469                                         ,"array"),
470                                 
471                         "mt_text_more"          => new xmlrpcval($item['more'], "string"),
472                         "mt_allow_comments" => new xmlrpcval($item['closed'] ? 0 : 1, "int"),
473                         "mt_allow_pings"        => new xmlrpcval(1, "int")
474                 ),'struct');
475
476                 //TODO: add "String link" to struct?
477                 //TODO: add "String permaLink" to struct?
478
479                 return new xmlrpcresp($newstruct);
480
481         }
482
483         $functionDefs = array_merge($functionDefs,
484                 array(
485                          "metaWeblog.newPost" =>
486                          array(
487                                 "function" => "f_metaWeblog_newPost",
488                                 "signature" => $f_metaWeblog_newPost_sig,
489                                 "docstring" => $f_metaWeblog_newPost_doc
490                          ),
491
492                          "metaWeblog.getCategories" =>
493                          array(
494                                 "function" => "f_metaWeblog_getCategories",
495                                 "signature" => $f_metaWeblog_getCategories_sig,
496                                 "docstring" => $f_metaWeblog_getCategories_doc
497                          ),
498
499                          "metaWeblog.getPost" =>
500                          array(
501                                 "function" => "f_metaWeblog_getPost",
502                                 "signature" => $f_metaWeblog_getPost_sig,
503                                 "docstring" => $f_metaWeblog_getPost_doc
504                          ),
505
506                          "metaWeblog.editPost" =>
507                          array(
508                                 "function" => "f_metaWeblog_editPost",
509                                 "signature" => $f_metaWeblog_editPost_sig,
510                                 "docstring" => $f_metaWeblog_editPost_doc
511                          ),
512
513                          'metaWeblog.newMediaObject' =>
514                          array(
515                                 'function' => 'f_metaWeblog_newMediaObject',
516                                 'signature' => $f_metaWeblog_newMediaObject_sig,
517                                 'docstring' => $f_metaWeblog_newMediaObject_doc
518                          ),
519
520                          'metaWeblog.getRecentPosts' =>
521                          array(
522                                 'function' => 'f_metaWeblog_getRecentPosts',
523                                 'signature' => $f_metaWeblog_getRecentPosts_sig,
524                                 'docstring' => $f_metaWeblog_getRecentPosts_doc
525                          )
526
527                 )
528         );
529 ?>