OSDN Git Service

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