OSDN Git Service

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