OSDN Git Service

90ef82fd7f3232b5f4ad694d123153487c27692f
[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-2011 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-2011 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;
57                 $comments = $struct->structmem('mt_allow_comments');
58                 if ($comments) {
59                         $closed = (intval(_getStructVal($struct, 'mt_allow_comments')) == 1) ? 0 : 1;
60                 } else {
61                         $closed = 0;
62                 }
63                                         $publish = _getScalar($m,4);
64
65
66                 // Add item
67 //              $res = _addItem($blogid, $username, $password, $title, $content, $more, $publish, $comments, $category);
68                 $res = _addItem($blogid, $username, $password, $title, $content, $more, $publish, $closed, $category);
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;
198                         $closed = (intval(_getStructVal($struct, 'mt_allow_comments')) == 1) ? 0 : 1;
199                 } else {
200 //                      $comments = $old['closed'];
201                         $closed = $old['closed'];
202                 }
203
204 //              $res = _edititem($itemid, $username, $password, $catid, $title, $content, $more, $wasdraft, $publish, $comments);
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 = sql_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                 {
367                         //if (eregi("\." .$type. "$",$filename)) $ok = 1;
368                         if (preg_match("#\." .$type. "$#i",$filename)) $ok = 1;
369                 }
370                 if (!$ok)
371                         _error(8, 'Filetype is not allowed');
372
373                 // - add file to media library
374                 //include_once($DIR_LIBS . 'MEDIA.php');        // media classes
375                 include_libs('MEDIA.php',true,false);
376
377                 // always use private media library of member
378                 $collection = $mem->getID();
379
380                 // prefix filename with current date (YYYY-MM-DD-)
381                 // this to avoid nameclashes
382                 if ($CONF['MediaPrefix'])
383                         $filename = strftime("%Y%m%d-", time()) . $filename;
384
385                 $res = MEDIA::addMediaObjectRaw($collection, $filename, $data);
386                 if ($res)
387                         return _error(10, $res);
388
389                 // - return URL
390                 $urlstruct = new xmlrpcval(array(
391                         "url" => new xmlrpcval($CONF['MediaURL'] . $collection. '/' . $filename,'string')
392                 ),'struct');
393
394                 return new xmlrpcresp($urlstruct);
395         }
396
397         function _categoryList($blogid, $username, $password) {
398                 // 1. login
399                 $mem = new MEMBER();
400                 if (!$mem->login($username, $password))
401                         return _error(1,"Could not log in");
402
403                 // check if on team and blog exists
404                 if (!BLOG::existsID($blogid))
405                         return _error(2,"No such blog ($blogid)");
406                 if (!$mem->teamRights($blogid))
407                         return _error(3,"Not a team member");
408
409                 $b = new BLOG($blogid);
410
411                 $categorystruct = array();
412
413                 $query =  "SELECT cname, cdesc, catid"
414                                 . ' FROM '.sql_table('category')
415                                 . " WHERE cblog=" . intval($blogid)
416                                 . " ORDER BY cname";
417                 $r = sql_query($query);
418
419                 while ($obj = sql_fetch_object($r)) {
420
421                         $categorystruct[$obj->cname] = new xmlrpcval(
422                                 array(
423                                         "description" => new xmlrpcval($obj->cdesc,"string"),
424                                         "htmlUrl" => new xmlrpcval($b->getURL() . "?catid=" . $obj->catid ,"string"),
425                                         "rssUrl" => new xmlrpcval("","string")
426                                 )
427                         ,'struct');
428                 }
429
430
431                 return new xmlrpcresp(new xmlrpcval( $categorystruct , "struct"));
432
433         }
434
435
436         function _mw_getPost($itemid, $username, $password) {
437                 global $manager;
438
439                 // 1. login
440                 $mem = new MEMBER();
441                 if (!$mem->login($username, $password))
442                         return _error(1,"Could not log in");
443
444                 // 2. check if allowed
445                 if (!$manager->existsItem($itemid,1,1))
446                         return _error(6,"No such item ($itemid)");
447                 $blogid = getBlogIDFromItemID($itemid);
448                 if (!$mem->teamRights($blogid))
449                         return _error(3,"Not a team member");
450
451                 // 3. return the item
452                 $item =& $manager->getItem($itemid,1,1); // (also allow drafts and future items)
453
454                 $b = new BLOG($blogid);
455                 if ($b->convertBreaks()) {
456                         $item['body'] = removeBreaks($item['body']);
457                         $item['more'] = removeBreaks($item['more']);
458                 }
459
460                 $categoryname = $b->getCategoryName($item['catid']);
461
462                 $newstruct = new xmlrpcval(array(
463                         "dateCreated" => new xmlrpcval(iso8601_encode($item['timestamp']),"dateTime.iso8601"),
464                         "userid" => new xmlrpcval($item['authorid'],"string"),
465                         "blogid" => new xmlrpcval($blogid,"string"),
466                         "postid" => new xmlrpcval($itemid, "string"),
467                         "description" => new xmlrpcval($item['body'],"string"),
468                         "title" => new xmlrpcval($item['title'],"string"),
469                         "categories" => new xmlrpcval(
470                                         array(
471                                                 new xmlrpcval($categoryname, "string")
472                                         )
473                                         ,"array"),
474                                 
475                         "mt_text_more"          => new xmlrpcval($item['more'], "string"),
476                         "mt_allow_comments" => new xmlrpcval($item['closed'] ? 0 : 1, "int"),
477                         "mt_allow_pings"        => new xmlrpcval(1, "int")
478                 ),'struct');
479
480                 //TODO: add "String link" to struct?
481                 //TODO: add "String permaLink" to struct?
482
483                 return new xmlrpcresp($newstruct);
484
485         }
486
487         $functionDefs = array_merge($functionDefs,
488                 array(
489                          "metaWeblog.newPost" =>
490                          array(
491                                 "function" => "f_metaWeblog_newPost",
492                                 "signature" => $f_metaWeblog_newPost_sig,
493                                 "docstring" => $f_metaWeblog_newPost_doc
494                          ),
495
496                          "metaWeblog.getCategories" =>
497                          array(
498                                 "function" => "f_metaWeblog_getCategories",
499                                 "signature" => $f_metaWeblog_getCategories_sig,
500                                 "docstring" => $f_metaWeblog_getCategories_doc
501                          ),
502
503                          "metaWeblog.getPost" =>
504                          array(
505                                 "function" => "f_metaWeblog_getPost",
506                                 "signature" => $f_metaWeblog_getPost_sig,
507                                 "docstring" => $f_metaWeblog_getPost_doc
508                          ),
509
510                          "metaWeblog.editPost" =>
511                          array(
512                                 "function" => "f_metaWeblog_editPost",
513                                 "signature" => $f_metaWeblog_editPost_sig,
514                                 "docstring" => $f_metaWeblog_editPost_doc
515                          ),
516
517                          'metaWeblog.newMediaObject' =>
518                          array(
519                                 'function' => 'f_metaWeblog_newMediaObject',
520                                 'signature' => $f_metaWeblog_newMediaObject_sig,
521                                 'docstring' => $f_metaWeblog_newMediaObject_doc
522                          ),
523
524                          'metaWeblog.getRecentPosts' =>
525                          array(
526                                 'function' => 'f_metaWeblog_getRecentPosts',
527                                 'signature' => $f_metaWeblog_getRecentPosts_sig,
528                                 'docstring' => $f_metaWeblog_getRecentPosts_doc
529                          )
530
531                 )
532         );
533 ?>