OSDN Git Service

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