Dotclear

source: inc/core/class.dc.xmlrpc.php @ 776:4ce635c0ca26

Revision 776:4ce635c0ca26, 44.3 KB checked in by Dsls <dsls@…>, 12 years ago (diff)

Last updates to getURLFor : removed blog url from function

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2011 Olivier Meunier & Association Dotclear
7# Licensed under the GPL version 2.0 license.
8# See LICENSE file or
9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10#
11# -- END LICENSE BLOCK -----------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14class dcXmlRpc extends xmlrpcIntrospectionServer
15{
16     public $core;
17     private $blog_id;
18     private $blog_loaded = false;
19     private $debug = false;
20     private $debug_file = '/tmp/dotclear-xmlrpc.log';
21     private $trace_args = true;
22     private $trace_response = true;
23     
24     public function __construct($core,$blog_id)
25     {
26          parent::__construct();
27         
28          $this->core =& $core;
29          $this->blog_id = $blog_id;
30         
31          # Blogger methods
32          $this->addCallback('blogger.newPost',array($this,'blogger_newPost'),
33               array('string','string','string','string','string','string','integer'),
34               'New post');
35         
36          $this->addCallback('blogger.editPost',array($this,'blogger_editPost'),
37               array('boolean','string','string','string','string','string','integer'),
38               'Edit a post');
39         
40          $this->addCallback('blogger.getPost',array($this,'blogger_getPost'),
41               array('struct','string','integer','string','string'),
42               'Return a posts by ID');
43         
44          $this->addCallback('blogger.deletePost',array($this,'blogger_deletePost'),
45               array('string','string','string','string','string','integer'),
46               'Delete a post');
47         
48          $this->addCallback('blogger.getRecentPosts',array($this,'blogger_getRecentPosts'),
49               array('array','string','string','string','string','integer'),
50               'Return a list of recent posts');
51         
52          $this->addCallback('blogger.getUsersBlogs',array($this,'blogger_getUserBlogs'),
53               array('struct','string','string','string'),
54               "Return user's blog");
55         
56          $this->addCallback('blogger.getUserInfo',array($this,'blogger_getUserInfo'),
57               array('struct','string','string','string'),
58               'Return User Info');
59         
60          # Metaweblog methods
61          $this->addCallback('metaWeblog.newPost',array($this,'mw_newPost'),
62               array('string','string','string','string','struct','boolean'),
63               'Creates a new post, and optionnaly publishes it.');
64         
65          $this->addCallback('metaWeblog.editPost',array($this,'mw_editPost'),
66               array('boolean','string','string','string','struct','boolean'),
67               'Updates information about an existing entry');
68         
69          $this->addCallback('metaWeblog.getPost',array($this,'mw_getPost'),
70               array('struct','string','string','string'),
71               'Returns information about a specific post');
72         
73          $this->addCallback('metaWeblog.getRecentPosts',array($this,'mw_getRecentPosts'),
74               array('array','string','string','string','integer'),
75               'List of most recent posts in the system');
76         
77          $this->addCallback('metaWeblog.getCategories',array($this,'mw_getCategories'),
78               array('array','string','string','string'),
79               'List of all categories defined in the weblog');
80         
81          $this->addCallback('metaWeblog.newMediaObject',array($this,'mw_newMediaObject'),
82               array('struct','string','string','string','struct'),
83               'Upload a file on the web server');
84         
85          # MovableType methods
86          $this->addCallback('mt.getRecentPostTitles',array($this,'mt_getRecentPostTitles'),
87               array('array','string','string','string','integer'),
88               'List of most recent posts in the system');
89         
90          $this->addCallback('mt.getCategoryList',array($this,'mt_getCategoryList'),
91               array('array','string','string','string'),
92               'List of all categories defined in the weblog');
93         
94          $this->addCallback('mt.getPostCategories',array($this,'mt_getPostCategories'),
95               array('array','string','string','string'),
96               'List of all categories to which the post is assigned');
97         
98          $this->addCallback('mt.setPostCategories',array($this,'mt_setPostCategories'),
99               array('boolean','string','string','string','array'),
100               'Sets the categories for a post');
101         
102          $this->addCallback('mt.publishPost',array($this,'mt_publishPost'),
103               array('boolean','string','string','string'),
104               'Retrieve pings list for a post');
105         
106          $this->addCallback('mt.supportedMethods',array($this,'listMethods'),
107               array(),'Retrieve information about the XML-RPC methods supported by the server.');
108         
109          $this->addCallback('mt.supportedTextFilters',array($this,'mt_supportedTextFilters'),
110               array(),'Retrieve information about supported text filters.');
111         
112          # WordPress methods
113          $this->addCallback('wp.getUsersBlogs',array($this,'wp_getUsersBlogs'),
114               array('array','string','string'),
115               'Retrieve the blogs of the user.');
116         
117          $this->addCallback('wp.getPage',array($this,'wp_getPage'),
118               array('struct','integer','integer','string','string'),
119               'Get the page identified by the page ID.');
120         
121          $this->addCallback('wp.getPages',array($this,'wp_getPages'),
122               array('array','integer','string','string','integer'),
123               'Get an array of all the pages on a blog.');
124         
125          $this->addCallback('wp.newPage',array($this,'wp_newPage'),
126               array('integer','integer','string','string','struct','boolean'),
127               'Create a new page.');
128         
129          $this->addCallback('wp.deletePage',array($this,'wp_deletePage'),
130               array('boolean','integer','string','string','integer'),
131               'Removes a page from the blog.');
132         
133          $this->addCallback('wp.editPage',array($this,'wp_editPage'),
134               array('boolean','integer','integer','string','string','struct','boolean'),
135               'Make changes to a blog page.');
136         
137          $this->addCallback('wp.getPageList',array($this,'wp_getPageList'),
138               array('array','integer','string','string'),
139               'Get an array of all the pages on a blog. Just the minimum details, lighter than wp.getPages.');
140         
141          $this->addCallback('wp.getAuthors',array($this,'wp_getAuthors'),
142               array('array','integer','string','string'),
143               'Get an array of users for the blog.');
144         
145          $this->addCallback('wp.getCategories',array($this,'wp_getCategories'),
146               array('array','integer','string','string'),
147               'Get an array of available categories on a blog.');
148         
149          $this->addCallback('wp.getTags',array($this,'wp_getTags'),
150               array('array','integer','string','string'),
151               'Get list of all tags for the blog.');
152         
153          $this->addCallback('wp.newCategory',array($this,'wp_newCategory'),
154               array('integer','integer','string','string','struct'),
155               'Create a new category.');
156         
157          $this->addCallback('wp.deleteCategory',array($this,'wp_deleteCategory'),
158               array('boolean','integer','string','string','integer'),
159               'Delete a category with a given ID.');
160         
161          $this->addCallback('wp.suggestCategories',array($this,'wp_suggestCategories'),
162               array('array','integer','string','string','string','integer'),
163               'Get an array of categories that start with a given string.');
164         
165          $this->addCallback('wp.uploadFile',array($this,'wp_uploadFile'),
166               array('struct','integer','string','string','struct'),
167               'Upload a file');
168         
169          $this->addCallback('wp.getPostStatusList',array($this,'wp_getPostStatusList'),
170               array('array','integer','string','string'),
171               'Retrieve all of the post statuses.');
172         
173          $this->addCallback('wp.getPageStatusList',array($this,'wp_getPageStatusList'),
174               array('array','integer','string','string'),
175               'Retrieve all of the pages statuses.');
176         
177          $this->addCallback('wp.getPageTemplates',array($this,'wp_getPageTemplates'),
178               array('struct','integer','string','string'),
179               'Retrieve page templates.');
180         
181          $this->addCallback('wp.getOptions',array($this,'wp_getOptions'),
182               array('struct','integer','string','string','array'),
183               'Retrieve blog options');
184         
185          $this->addCallback('wp.setOptions',array($this,'wp_setOptions'),
186               array('struct','integer','string','string','struct'),
187               'Update blog options');
188         
189          $this->addCallback('wp.getComment',array($this,'wp_getComment'),
190               array('struct','integer','string','string','integer'),
191               "Gets a comment, given it's comment ID.");
192         
193          $this->addCallback('wp.getCommentCount',array($this,'wp_getCommentCount'),
194               array('array','integer','string','string','integer'),
195               'Retrieve comment count.');
196         
197          $this->addCallback('wp.getComments',array($this,'wp_getComments'),
198               array('array','integer','string','string','struct'),
199               'Gets a set of comments for a given post.');
200         
201          $this->addCallback('wp.deleteComment',array($this,'wp_deleteComment'),
202               array('boolean','integer','string','string','integer'),
203               'Delete a comment with given ID.');
204         
205          $this->addCallback('wp.editComment',array($this,'wp_editComment'),
206               array('boolean','integer','string','string','integer','struct'),
207               'Edit a comment with given ID.');
208         
209          $this->addCallback('wp.newComment',array($this,'wp_newComment'),
210               array('integer','integer','string','string','integer','struct'),
211               'Create a new comment for a given post ID.');
212         
213          $this->addCallback('wp.getCommentStatusList',array($this,'wp_getCommentStatusList'),
214               array('array','integer','string','string'),
215               'Retrieve all of the comment statuses.');
216     }
217     
218     public function serve($data=false,$encoding='UTF-8')
219     {
220          parent::serve(false,$encoding);
221     }
222     
223     public function call($methodname,$args)
224     {
225          try {
226               $rsp = @parent::call($methodname,$args);
227               $this->debugTrace($methodname,$args,$rsp);
228               return $rsp;
229          } catch (Exception $e) {
230               $this->debugTrace($methodname,$args,array($e->getMessage(),$e->getCode()));
231               throw $e;
232          }
233     }
234     
235     private function debugTrace($methodname,$args,$rsp)
236     {
237          if (!$this->debug) {
238               return;
239          }
240         
241          if (($fp = @fopen($this->debug_file,'a')) !== false)
242          {
243               fwrite($fp,'['.date('r').']'.' '.$methodname);
244               
245               if ($this->trace_args) {
246                    fwrite($fp,"\n- args ---\n".var_export($args,1));
247               }
248               
249               if ($this->trace_response) {
250                    fwrite($fp,"\n- response ---\n".var_export($rsp,1));
251               }
252               fwrite($fp,"\n");
253               fclose($fp);
254          }
255     }
256     
257     /* Internal methods
258     --------------------------------------------------- */
259     private function setUser($user_id,$pwd)
260     {
261          if ($this->core->auth->userID() == $user_id) {
262               return true;
263          }
264         
265          if ($this->core->auth->checkUser($user_id,$pwd) !== true) {
266               throw new Exception('Login error');
267          }
268         
269          return true;
270     }
271     
272     private function setBlog()
273     {
274          if (!$this->blog_id) {
275               throw new Exception('No blog ID given.');
276          }
277         
278          if ($this->blog_loaded) {
279               return true;
280          }
281         
282          $this->core->setBlog($this->blog_id);
283          $this->blog_loaded = true;
284         
285          if (!$this->core->blog->id) {
286               $this->core->blog = null;
287               throw new Exception('Blog does not exist.');
288          }
289         
290          if (!$this->core->blog->settings->system->enable_xmlrpc ||
291          !$this->core->auth->check('usage,contentadmin',$this->core->blog->id)) {
292               $this->core->blog = null;
293               throw new Exception('Not enough permissions on this blog.');
294          }
295         
296          foreach ($this->core->plugins->getModules() as $id => $m) {
297               $this->core->plugins->loadNsFile($id,'xmlrpc');
298          }
299         
300          return true;
301     }
302     
303     private function getPostRS($post_id,$user,$pwd,$post_type='post')
304     {
305          $this->setUser($user,$pwd);
306          $this->setBlog();
307          $rs = $this->core->blog->getPosts(array(
308               'post_id' => (integer) $post_id,
309               'post_type' => $post_type
310          ));
311         
312          if ($rs->isEmpty()) {
313               throw new Exception('This entry does not exist');
314          }
315         
316          return $rs;
317     }
318     
319     private function getCatID($cat_url)
320     {
321          $rs = $this->core->blog->getCategories(array('cat_url' => $cat_url));
322         
323          return $rs->isEmpty() ? null : $rs->cat_id;
324     }
325     
326     /* Generic methods
327     --------------------------------------------------- */
328     private function newPost($blog_id,$user,$pwd,$content,$struct=array(),$publish=true)
329     {
330          $this->setUser($user,$pwd);
331          $this->setBlog();
332         
333          $title = !empty($struct['title']) ? $struct['title'] : '';
334          $excerpt = !empty($struct['mt_excerpt']) ? $struct['mt_excerpt'] : '';
335          $description = !empty($struct['description']) ? $struct['description'] : null;
336          $dateCreated = !empty($struct['dateCreated']) ? $struct['dateCreated'] : null;
337          $open_comment = isset($struct['mt_allow_comments']) ? $struct['mt_allow_comments'] : 1;
338          $open_tb = isset($struct['mt_allow_pings']) ? $struct['mt_allow_pings'] : 1;
339         
340          if ($description !== null) {
341               $content = $description;
342          }
343         
344          if (!$title) {
345               $title = text::cutString(html::clean($content),25).'...';
346          }
347         
348          $excerpt_xhtml = $this->core->callFormater('xhtml',$excerpt);
349          $content_xhtml = $this->core->callFormater('xhtml',$content);
350         
351          if (empty($content)) {
352               throw new Exception('Cannot create an empty entry');
353          }
354         
355          $cur = $this->core->con->openCursor($this->core->prefix.'post');
356         
357          $cur->user_id = $this->core->auth->userID();
358          $cur->post_lang = $this->core->auth->getInfo('user_lang');
359          $cur->post_title = trim($title);
360          $cur->post_content = $content;
361          $cur->post_excerpt = $excerpt;
362          $cur->post_content_xhtml = $content_xhtml;
363          $cur->post_excerpt_xhtml = $excerpt_xhtml;
364          $cur->post_open_comment = (integer) ($open_comment == 1);
365          $cur->post_open_tb = (integer) ($open_tb == 1);
366          $cur->post_status = (integer) $publish;
367          $cur->post_format = 'xhtml';
368         
369          if ($dateCreated) {
370               if ($dateCreated instanceof xmlrpcDate) {
371                    $cur->post_dt = date('Y-m-d H:i:00',$dateCreated->getTimestamp());
372               } elseif (is_string($dateCreated) && @strtotime($dateCreated)) {
373                    $cur->post_dt = date('Y-m-d H:i:00',strtotime($dateCreated));
374               }
375          }
376         
377          # Categories in an array
378          if (isset($struct['categories']) && is_array($struct['categories']))
379          {
380               $categories = $struct['categories'];
381               $cat_id = !empty($categories[0]) ? $categories[0] : null;
382               
383               $cur->cat_id = $this->getCatID($cat_id);
384          }
385         
386          if (isset($struct['wp_slug'])) {
387               $cur->post_url = $struct['wp_slug'];
388          }
389         
390          if (isset($struct['wp_password'])) {
391               $cur->post_password = $struct['wp_password'];
392          }
393         
394          $cur->post_type = 'post';
395          if (!empty($struct['post_type'])) {
396               $cur->post_type = $struct['post_type'];
397          }
398         
399          if ($cur->post_type == 'post')
400          {
401               # --BEHAVIOR-- xmlrpcBeforeNewPost
402               $this->core->callBehavior('xmlrpcBeforeNewPost',$this,$cur,$content,$struct,$publish);
403               
404               $post_id = $this->core->blog->addPost($cur);
405               
406               # --BEHAVIOR-- xmlrpcAfterNewPost
407               $this->core->callBehavior('xmlrpcAfterNewPost',$this,$post_id,$cur,$content,$struct,$publish);
408          }
409          elseif ($cur->post_type == 'page')
410          {
411               if (isset($struct['wp_page_order'])) {
412                    $cur->post_position = (integer) $struct['wp_page_order'];
413               }
414               
415               $this->core->blog->settings->system->post_url_format = '{t}';
416               
417               $post_id = $this->core->blog->addPost($cur);
418          }
419          else
420          {
421               throw new Exception('Invalid post type',401);
422          }
423         
424          return (string) $post_id;
425     }
426     
427     private function editPost($post_id,$user,$pwd,$content,$struct=array(),$publish=true)
428     {
429          $post_id = (integer) $post_id;
430         
431          $post_type = 'post';
432          if (!empty($struct['post_type'])) {
433               $post_type = $struct['post_type'];
434          }
435         
436          $post = $this->getPostRS($post_id,$user,$pwd,$post_type);
437         
438          $title = (!empty($struct['title'])) ? $struct['title'] : '';
439          $excerpt = (!empty($struct['mt_excerpt'])) ? $struct['mt_excerpt'] : '';
440          $description = (!empty($struct['description'])) ? $struct['description'] : null;
441          $dateCreated = !empty($struct['dateCreated']) ? $struct['dateCreated'] : null;
442          $open_comment = (isset($struct['mt_allow_comments'])) ? $struct['mt_allow_comments'] : 1;
443          $open_tb = (isset($struct['mt_allow_pings'])) ? $struct['mt_allow_pings'] : 1;
444         
445          if ($description !== null) {
446               $content = $description;
447          }
448         
449          if (!$title) {
450               $title = text::cutString(html::clean($content),25).'...';
451          }
452         
453          $excerpt_xhtml = $this->core->callFormater('xhtml',$excerpt);
454          $content_xhtml = $this->core->callFormater('xhtml',$content);
455         
456          if (empty($content)) {
457               throw new Exception('Cannot create an empty entry');
458          }
459         
460          $cur = $this->core->con->openCursor($this->core->prefix.'post');
461         
462          $cur->post_type = $post_type;
463          $cur->post_title = trim($title);
464          $cur->post_content = $content;
465          $cur->post_excerpt = $excerpt;
466          $cur->post_content_xhtml = $content_xhtml;
467          $cur->post_excerpt_xhtml = $excerpt_xhtml;
468          $cur->post_open_comment = (integer) ($open_comment == 1);
469          $cur->post_open_tb = (integer) ($open_tb == 1);
470          $cur->post_status = (integer) $publish;
471          $cur->post_format = 'xhtml';
472          $cur->post_url = $post->post_url;
473         
474         
475          if ($dateCreated) {
476               if ($dateCreated instanceof xmlrpcDate) {
477                    $cur->post_dt = date('Y-m-d H:i:00',$dateCreated->getTimestamp());
478               } elseif (is_string($dateCreated) && @strtotime($dateCreated)) {
479                    $cur->post_dt = date('Y-m-d H:i:00',strtotime($dateCreated));
480               }
481          } else {
482               $cur->post_dt = $post->post_dt;
483          }
484         
485          # Categories in an array
486          if (isset($struct['categories']) && is_array($struct['categories']))
487          {
488               $categories = $struct['categories'];
489               $cat_id = !empty($categories[0]) ? $categories[0] : null;
490               
491               $cur->cat_id = $this->getCatID($cat_id);
492          }
493         
494          if (isset($struct['wp_slug'])) {
495               $cur->post_url = $struct['wp_slug'];
496          }
497         
498          if (isset($struct['wp_password'])) {
499               $cur->post_password = $struct['wp_password'];
500          }
501         
502          if ($cur->post_type == 'post')
503          {
504               # --BEHAVIOR-- xmlrpcBeforeEditPost
505               $this->core->callBehavior('xmlrpcBeforeEditPost',$this,$post_id,$cur,$content,$struct,$publish);
506               
507               $this->core->blog->updPost($post_id,$cur);
508               
509               # --BEHAVIOR-- xmlrpcAfterEditPost
510               $this->core->callBehavior('xmlrpcAfterEditPost',$this,$post_id,$cur,$content,$struct,$publish);
511          }
512          elseif ($cur->post_type == 'page')
513          {
514               if (isset($struct['wp_page_order'])) {
515                    $cur->post_position = (integer) $struct['wp_page_order'];
516               }
517               
518               $this->core->blog->settings->system->post_url_format = '{t}';
519               
520               $this->core->blog->updPost($post_id,$cur);
521          }
522          else
523          {
524               throw new Exception('Invalid post type',401);
525          }
526         
527          return true;
528     }
529     
530     private function getPost($post_id,$user,$pwd,$type='mw')
531     {
532          $post_id = (integer) $post_id;
533         
534          $post = $this->getPostRS($post_id,$user,$pwd);
535         
536          $res = new ArrayObject();
537         
538          $res['dateCreated'] = new xmlrpcDate($post->getTS());
539          $res['userid'] = $post->user_id;
540          $res['postid'] = $post->post_id;
541         
542          if ($post->cat_id) {
543               $res['categories'] = array($post->cat_url);
544          }
545         
546          if ($type == 'blogger') {
547               $res['content'] = $post->post_content_xhtml;
548          }
549         
550          if ($type == 'mt' || $type == 'mw') {
551               $res['title'] = $post->post_title;
552          }
553         
554          if ($type == 'mw') {
555               $res['description'] = $post->post_content_xhtml;
556               $res['link'] = $res['permaLink'] = $post->getURL();
557               $res['mt_excerpt'] = $post->post_excerpt_xhtml;
558               $res['mt_text_more'] = '';
559               $res['mt_allow_comments'] = (integer) $post->post_open_comment;
560               $res['mt_allow_pings'] = (integer) $post->post_open_tb;
561               $res['mt_convert_breaks'] = '';
562               $res['mt_keywords'] = '';
563          }
564         
565          # --BEHAVIOR-- xmlrpcGetPostInfo
566          $this->core->callBehavior('xmlrpcGetPostInfo',$this,$type,array(&$res));
567         
568          return $res;
569     }
570     
571     private function deletePost($post_id,$user,$pwd)
572     {
573          $post_id = (integer) $post_id;
574         
575          $this->getPostRS($post_id,$user,$pwd);
576          $this->core->blog->delPost($post_id);
577         
578          return true;
579     }
580     
581     private function getRecentPosts($blog_id,$user,$pwd,$nb_post,$type='mw')
582     {
583          $this->setUser($user,$pwd);
584          $this->setBlog();
585         
586          $nb_post = (integer) $nb_post;
587         
588          if ($nb_post > 50) {
589               throw new Exception('Cannot retrieve more than 50 entries');
590          }
591         
592          $params = array();
593          $params['limit'] = $nb_post;
594         
595          $posts = $this->core->blog->getPosts($params);
596         
597          $res = array();
598          while ($posts->fetch())
599          {
600               $tres = array();
601               
602               $tres['dateCreated'] = new xmlrpcDate($posts->getTS());
603               $tres['userid'] = $posts->user_id;
604               $tres['postid'] = $posts->post_id;
605               
606               if ($posts->cat_id) {
607                    $tres['categories'] = array($posts->cat_url);
608               }
609               
610               if ($type == 'blogger') {
611                    $tres['content'] = $posts->post_content_xhtml;
612               }
613               
614               if ($type == 'mt' || $type == 'mw') {
615                    $tres['title'] = $posts->post_title;
616               }
617               
618               if ($type == 'mw') {
619                    $tres['description'] = $posts->post_content_xhtml;
620                    $tres['link'] = $tres['permaLink'] = $posts->getURL();
621                    $tres['mt_excerpt'] = $posts->post_excerpt_xhtml;
622                    $tres['mt_text_more'] = '';
623                    $tres['mt_allow_comments'] = (integer) $posts->post_open_comment;
624                    $tres['mt_allow_pings'] = (integer) $posts->post_open_tb;
625                    $tres['mt_convert_breaks'] = '';
626                    $tres['mt_keywords'] = '';
627               }
628               
629               # --BEHAVIOR-- xmlrpcGetPostInfo
630               $this->core->callBehavior('xmlrpcGetPostInfo',$this,$type,array(&$tres));
631               
632               $res[] = $tres;
633          }
634         
635          return $res;
636     }
637     
638     private function getUserBlogs($user,$pwd)
639     {
640          $this->setUser($user,$pwd);
641          $this->setBlog();
642         
643          return array(array(
644               'url' => $this->core->blog->url,
645               'blogid' => '1',
646               'blogName' => $this->core->blog->name
647          ));
648     }
649     
650     private function getUserInfo($user,$pwd)
651     {
652          $this->setUser($user,$pwd);
653         
654          return array(
655               'userid' => $this->core->auth->userID(),
656               'firstname' => $this->core->auth->getInfo('user_firstname'),
657               'lastname' => $this->core->auth->getInfo('user_name'),
658               'nickname' => $this->core->auth->getInfo('user_displayname'),
659               'email' => $this->core->auth->getInfo('user_email'),
660               'url' => $this->core->auth->getInfo('user_url')
661          );
662     }
663     
664     private function getCategories($blog_id,$user,$pwd)
665     {
666          $this->setUser($user,$pwd);
667          $this->setBlog();
668          $rs = $this->core->blog->getCategories();
669         
670          $res = array();
671         
672          $l = $rs->level;
673          $stack = array('',$rs->cat_url);
674         
675          while ($rs->fetch())
676          {
677               $d = $rs->level - $l;
678               if ($d == 0) {
679                    array_pop($stack);
680                    $parent = end($stack);
681               } elseif ($d > 0) {
682                    $parent = end($stack);
683               } elseif ($d < 0) {
684                    $D = abs($d);
685                    for ($i=0; $i<=$D; $i++) {
686                         array_pop($stack);
687                    }
688                    $parent = end($stack);
689               }
690               
691               $res[] = array(
692                    'categoryId' => $rs->cat_url,
693                    'parentId' => $parent,
694                    'description' => $rs->cat_title,
695                    'categoryName' => $rs->cat_url,
696                    'htmlUrl' => $this->core->blog->url.
697                         $this->core->url->getURLFor('category',$rs->cat_url),
698                    'rssUrl' => $this->core->blog->url.
699                         $this->core->url->getURLFor('feed','category/'.$rs->cat_url.'/rss2')
700               );
701               
702               $stack[] = $rs->cat_url;
703               $l = $rs->level;
704          }
705         
706          return $res;
707     }
708     
709     private function getPostCategories($post_id,$user,$pwd)
710     {
711          $post_id = (integer) $post_id;
712         
713          $post = $this->getPostRS($post_id,$user,$pwd);
714         
715          return array(
716               array(
717               'categoryName' => $post->cat_url,
718               'categoryId' => (string) $post->cat_url,
719               'isPrimary' => true
720               )
721          );
722     }
723     
724     private function setPostCategories($post_id,$user,$pwd,$categories)
725     {
726          $post_id = (integer) $post_id;
727         
728          $post = $this->getPostRS($post_id,$user,$pwd);
729         
730          $cat_id = (!empty($categories[0]['categoryId'])) ? $categories[0]['categoryId'] : null;
731         
732          foreach($categories as $v)
733          {
734               if (isset($v['isPrimary']) && $v['isPrimary']) {
735                    $cat_id = $v['categoryId'];
736                    break;
737               }
738          }
739         
740          # w.bloggar sends -1 for no category.
741          if ($cat_id == -1) {
742               $cat_id = null;
743          }
744         
745          if ($cat_id) {
746               $cat_id = $this->getCatID($cat_id);
747          }
748         
749          $this->core->blog->updPostCategory($post_id,(integer) $cat_id);
750         
751          return true;
752     }
753     
754     private function publishPost($post_id,$user,$pwd)
755     {
756          $post_id = (integer) $post_id;
757         
758          $this->getPostRS($post_id,$user,$pwd);
759         
760          # --BEHAVIOR-- xmlrpcBeforePublishPost
761          $this->core->callBehavior('xmlrpcBeforePublishPost',$this,$post_id);
762         
763          $this->core->blog->updPostStatus($post_id,1);
764         
765          # --BEHAVIOR-- xmlrpcAfterPublishPost
766          $this->core->callBehavior('xmlrpcAfterPublishPost',$this,$post_id);
767         
768          return true;
769     }
770     
771     private function newMediaObject($blog_id,$user,$pwd,$file)
772     {
773          if (empty($file['name'])) {
774               throw new Exception('No file name');
775          }
776         
777          if (empty($file['bits'])) {
778               throw new Exception('No file content');
779          }
780         
781          $file_name = $file['name'];
782          $file_bits = $file['bits'];
783         
784          $this->setUser($user,$pwd);
785          $this->setBlog();
786         
787          $media = new dcMedia($this->core);
788         
789          $dir_name = path::clean(dirname($file_name));
790          $file_name = basename($file_name);
791         
792          $dir_name = preg_replace('!^/!','',$dir_name);
793          if ($dir_name != '')
794          {
795               $dir = explode('/',$dir_name);
796               $cwd = './';
797               foreach ($dir as $v)
798               {
799                    $v = files::tidyFileName($v);
800                    $cwd .= $v.'/';
801                    $media->makeDir($v);
802                    $media->chdir($cwd);
803               }
804          }
805         
806          $media_id = $media->uploadBits($file_name,$file_bits);
807         
808          $f = $media->getFile($media_id);
809          return array(
810               'file' => $file_name,
811               'url' => $f->file_url,
812               'type' => files::getMimeType($file_name)
813          );
814     }
815     
816     private function translateWpStatus($s)
817     {
818          $status = array(
819               'draft' => -2,
820               'pending' => -2,
821               'private' => 0,
822               'publish' => 1,
823               'scheduled' => -1
824          );
825         
826          if (is_int($s)) {
827               $status = array_flip($status);
828               return isset($status[$s]) ? $status[$s] : $status[-2];
829          } else {
830               return isset($status[$s]) ? $status[$s] : $status['pending'];
831          }
832     }
833     
834     private function translateWpCommentstatus($s)
835     {
836          $status = array(
837               'hold' => -1,
838               'approve' => 0,
839               'spam' => -2
840          );
841         
842          if (is_int($s)) {
843               $status = array_flip($status);
844               return isset($status[$s]) ? $status[$s] : $status[0];
845          } else {
846               return isset($status[$s]) ? $status[$s] : $status['approve'];
847          }
848     }
849     
850     private function translateWpOptions($options=array())
851     {
852          $timezone = 0;
853          if ($this->core->blog->settings->system->blog_timezone) {
854               $timezone = dt::getTimeOffset($this->core->blog->settings->system->blog_timezone)/3600;
855          }
856         
857          $res = array (
858              'software_name' => array (
859                    'desc' => 'Software Name',
860                    'readonly' => true,
861                    'value' => 'Dotclear'
862               ),
863               'software_version' => array (
864                    'desc' => 'Software Version',
865                    'readonly' => true,
866                    'value' => DC_VERSION
867               ),
868               'blog_url' => array (
869                    'desc' => 'Blog URL',
870                    'readonly' => true,
871                    'value' => $this->core->blog->url
872               ),
873               'time_zone' => array (
874                    'desc' => 'Time Zone',
875                    'readonly' => true,
876                    'value' => (string) $timezone
877               ),
878               'blog_title' => array (
879                    'desc' => 'Blog Title',
880                    'readonly' => false,
881                    'value' => $this->core->blog->name
882               ),
883               'blog_tagline' => array (
884                    'desc' => 'Blog Tagline',
885                    'readonly' => false,
886                    'value' => $this->core->blog->desc
887               ),
888               'date_format' => array (
889                    'desc' => 'Date Format',
890                    'readonly' => false,
891                    'value' => $this->core->blog->settings->system->date_format
892               ),
893               'time_format' => array (
894                    'desc' => 'Time Format',
895                    'readonly' => false,
896                    'value' => $this->core->blog->settings->system->time_format
897               )
898          );
899         
900          if (!empty($options))
901          {
902               $r = array();
903               foreach ($options as $v) {
904                    if (isset($res[$v])) {
905                         $r[$v] = $res[$v];
906                    }
907               }
908               return $r;
909          }
910         
911          return $res;
912     }
913     
914     private function getPostStatusList($blog_id,$user,$pwd)
915     {
916          $this->setUser($user,$pwd);
917          $this->setBlog();
918         
919          return array(
920               'draft' => 'Draft',
921               'pending' => 'Pending Review',
922               'private' => 'Private',
923               'publish' => 'Published',
924               'scheduled' => 'Scheduled'
925          );
926     }
927     
928     private function getPageStatusList($blog_id,$user,$pwd)
929     {
930          $this->setUser($user,$pwd);
931          $this->setBlog();
932          $this->checkPagesPermission();
933         
934          return array(
935               'draft' => 'Draft',
936               'private' => 'Private',
937               'published' => 'Published',
938               'scheduled' => 'Scheduled'
939          );
940     }
941     
942     private function checkPagesPermission()
943     {
944          if (!$this->core->plugins->moduleExists('pages')) {
945               throw new Exception('Pages management is not available on this blog.');
946          }
947         
948          if (!$this->core->auth->check('pages,contentadmin',$this->core->blog->id)) {
949               throw new Exception('Not enough permissions to edit pages.',401);
950          }
951     }
952     
953     private function getPages($blog_id,$user,$pwd,$limit=null,$id=null)
954     {
955          $this->setUser($user,$pwd);
956          $this->setBlog();
957          $this->checkPagesPermission();
958         
959          $params = array(
960               'post_type' => 'page',
961               'order' => 'post_position ASC, post_title ASC'
962          );
963         
964          if ($id) {
965               $params['post_id'] = (integer) $id;
966          }
967          if ($limit) {
968               $params['limit'] = $limit;
969          }
970         
971          $posts = $this->core->blog->getPosts($params);
972         
973          $res = array();
974          while ($posts->fetch())
975          {
976               $tres = array(
977                    "dateCreated"            => new xmlrpcDate($posts->getTS()),
978                    "userid"                 => $posts->user_id,
979                    "page_id"                => $posts->post_id,
980                    "page_status"            => $this->translateWpStatus((integer) $posts->post_status),
981                    "description"            => $posts->post_content_xhtml,
982                    "title"                  => $posts->post_title,
983                    "link"                   => $posts->getURL(),
984                    "permaLink"              => $posts->getURL(),
985                    "categories"             => array(),
986                    "excerpt"                => $posts->post_excerpt_xhtml,
987                    "text_more"              => '',
988                    "mt_allow_comments"      => (integer) $posts->post_open_comment,
989                    "mt_allow_pings"         => (integer) $posts->post_open_tb,
990                    "wp_slug"                => $posts->post_url,
991                    "wp_password"            => $posts->post_password,
992                    "wp_author"              => $posts->getAuthorCN(),
993                    "wp_page_parent_id"      => 0,
994                    "wp_page_parent_title"   => '',
995                    "wp_page_order"          => $posts->post_position,
996                    "wp_author_id"           => $posts->user_id,
997                    "wp_author_display_name" => $posts->getAuthorCN(),
998                    "date_created_gmt"       => new xmlrpcDate(dt::iso8601($posts->getTS(),$posts->post_tz)),
999                    "custom_fields"          => array(),
1000                    "wp_page_template"       => 'default'
1001               );
1002               
1003               # --BEHAVIOR-- xmlrpcGetPageInfo
1004               $this->core->callBehavior('xmlrpcGetPageInfo',$this,array(&$tres));
1005               
1006               $res[] = $tres;
1007          }
1008         
1009          return $res;
1010     }
1011     
1012     private function newPage($blog_id,$user,$pwd,$struct,$publish)
1013     {
1014          $this->setUser($user,$pwd);
1015          $this->setBlog();
1016          $this->checkPagesPermission();
1017         
1018          $struct['post_type'] = 'page';
1019         
1020          return $this->newPost($blog_id,$user,$pwd,null,$struct,$publish);
1021     }
1022     
1023     private function editPage($page_id,$user,$pwd,$struct,$publish)
1024     {
1025          $this->setUser($user,$pwd);
1026          $this->setBlog();
1027          $this->checkPagesPermission();
1028         
1029          $struct['post_type'] = 'page';
1030         
1031          return $this->editPost($page_id,$user,$pwd,null,$struct,$publish);
1032     }
1033     
1034     private function deletePage($page_id,$user,$pwd)
1035     {
1036          $this->setUser($user,$pwd);
1037          $this->setBlog();
1038          $this->checkPagesPermission();
1039         
1040          $page_id = (integer) $page_id;
1041         
1042          $this->getPostRS($page_id,$user,$pwd,'page');
1043          $this->core->blog->delPost($page_id);
1044         
1045          return true;
1046     }
1047     
1048     private function getAuthors($user,$pwd)
1049     {
1050          $this->setUser($user,$pwd);
1051          $this->setBlog();
1052         
1053          $rs = $this->core->getBlogPermissions($this->core->blog->id);
1054          $res = array();
1055         
1056          foreach($rs as $k => $v)
1057          {
1058               $res[] = array(
1059                    'user_id' => $k,
1060                    'user_login' => $k,
1061                    'display_name' => dcUtils::getUserCN($k,$v['name'],$v['firstname'],$v['displayname'])
1062               );
1063          }
1064          return $res;
1065     }
1066     
1067     private function getTags($user,$pwd)
1068     {
1069          $this->setUser($user,$pwd);
1070          $this->setBlog();
1071         
1072          $tags = $this->core->meta->getMeta('tag');
1073          $tags->sort('meta_id_lower','asc');
1074         
1075          $res = array();
1076          $url   = $this->core->blog->url.
1077               $this->core->url->getURLFor('tag','%s');
1078          $f_url = $this->core->blog->url.
1079               $this->core->url->getURLFor('tag_feed','%s');
1080          while ($tags->fetch())
1081          {
1082               $res[] = array(
1083                    'tag_id'       => $tags->meta_id,
1084                    'name'         => $tags->meta_id,
1085                    'count'        => $tags->count,
1086                    'slug'         => $tags->meta_id,
1087                    'html_url'     => sprintf($url,$tags->meta_id),
1088                    'rss_url'      => sprintf($f_url,$tags->meta_id)
1089               );
1090          }
1091          return $res;
1092     }
1093     
1094     private function newCategory($user,$pwd,$struct)
1095     {
1096          $this->setUser($user,$pwd);
1097          $this->setBlog();
1098         
1099          if (empty($struct['name'])) {
1100               throw new Exception('You mus give a category name.');
1101          }
1102         
1103          $cur = $this->core->con->openCursor($this->core->prefix.'category');
1104          $cur->cat_title = $struct['name'];
1105         
1106          if (!empty($struct['slug'])) {
1107               $cur->cat_url = $struct['slug'];
1108          }
1109          if (!empty($struct['category_description'])) {
1110               $cur->cat_desc = $struct['category_description'];
1111               if (html::clean($cur->cat_desc) == $cur->cat_desc) {
1112                    $cur->cat_desc = '<p>'.$cur->cat_desc.'</p>';
1113               }
1114          }
1115         
1116          $parent = !empty($struct['category_parent']) ? (integer) $struct['category_parent'] : 0;
1117         
1118          $id = $this->core->blog->addCategory($cur,$parent);
1119          $rs = $this->core->blog->getCategory($id);
1120          return $rs->cat_url;
1121     }
1122     
1123     private function deleteCategory($user,$pwd,$cat_id)
1124     {
1125          $this->setUser($user,$pwd);
1126          $this->setBlog();
1127         
1128          $c = $this->core->blog->getCategories(array('cat_url' => $cat_id));
1129          if ($c->isEmpty()) {
1130               throw new Exception(__('This category does not exist.'));
1131          }
1132          $cat_id = $c->cat_id;
1133          unset($c);
1134         
1135          $this->core->blog->delCategory((integer) $cat_id);
1136          return true;
1137     }
1138     
1139     private function searchCategories($user,$pwd,$category,$limit)
1140     {
1141          $this->setUser($user,$pwd);
1142          $this->setBlog();
1143         
1144          $strReq = 'SELECT cat_id, cat_title, cat_url '.
1145                    'FROM '.$this->core->prefix.'category '.
1146                    "WHERE blog_id = '".$this->core->con->escape($this->core->blog->id)."' ".
1147                    "AND LOWER(cat_title) LIKE LOWER('%".$this->core->con->escape($category)."%') ".
1148                    ($limit > 0 ? $this->core->con->limit($limit) : '');
1149         
1150          $rs = $this->core->con->select($strReq);
1151         
1152          $res = array();
1153          while ($rs->fetch())
1154          {
1155               $res[] = array(
1156                    'category_id' => $rs->cat_url,
1157                    'category_name' => $rs->cat_url
1158               );
1159          }
1160          return $res;
1161     }
1162     
1163     private function countComments($user,$pwd,$post_id)
1164     {
1165          $this->setUser($user,$pwd);
1166          $this->setBlog();
1167         
1168          $res = array(
1169               'approved' => 0,
1170               'awaiting_moderation' => 0,
1171               'spam' => 0,
1172               'total' => 0
1173          );
1174          $rs = $this->core->blog->getComments(array('post_id' => $post_id));
1175         
1176          while ($rs->fetch()) {
1177               $res['total']++;
1178               if ($rs->comment_status == 1) {
1179                    $res['approved']++;
1180               } elseif ($rs->comment_status == -2) {
1181                    $res['spam']++;
1182               } else {
1183                    $res['awaiting_moderation']++;
1184               }
1185          }
1186          return $res;
1187     }
1188     
1189     private function getComments($user,$pwd,$struct,$id=null)
1190     {
1191          $this->setUser($user,$pwd);
1192          $this->setBlog();
1193         
1194          $params = array();
1195         
1196          if (!empty($struct['status'])) {
1197               $params['comment_status'] = $this->translateWpCommentstatus($struct['status']);
1198          }
1199         
1200          if (!empty($struct['post_id'])) {
1201               $params['post_id'] = (integer) $struct['post_id'];
1202          }
1203         
1204          if (isset($id)) {
1205               $params['comment_id'] = $id;
1206          }
1207         
1208          $offset = !empty($struct['offset']) ? (integer) $struct['offset'] : 0;
1209          $limit = !empty($struct['number']) ? (integer) $struct['number'] : 10;
1210          $params['limit'] = array($offset,$limit);
1211         
1212          $rs = $this->core->blog->getComments($params);
1213          $res = array();
1214          while ($rs->fetch())
1215          {
1216               $res[] = array(
1217                    'date_created_gmt'       => new xmlrpcDate($rs->getTS()),
1218                    'user_id'                => $rs->user_id,
1219                    'comment_id'             => $rs->comment_id,
1220                    'parent'                 => 0,
1221                    'status'                 => $this->translateWpCommentstatus((integer) $rs->comment_status),
1222                    'content'                => $rs->comment_content,
1223                    'link'                   => $rs->getPostURL().'#c'.$rs->comment_id,
1224                    'post_id'                => $rs->post_id,
1225                    'post_title'             => $rs->post_title,
1226                    'author'                 => $rs->comment_author,
1227                    'author_url'             => $rs->comment_site,
1228                    'author_email'           => $rs->comment_email,
1229                    'author_ip'              => $rs->comment_ip
1230               );
1231          }
1232          return $res;
1233     }
1234     
1235     private function addComment($user,$pwd,$post_id,$struct)
1236     {
1237          $this->setUser($user,$pwd);
1238          $this->setBlog();
1239         
1240          if (empty($struct['content'])) {
1241               throw new Exception('Sorry, you cannot post an empty comment',401);
1242          }
1243         
1244          if (is_numeric($post_id)) {
1245               $p['post_id'] = $post_id;
1246          } else {
1247               $p['post_url'] = $post_id;
1248          }
1249          $rs = $this->core->blog->getPosts($p);
1250          if ($rs->isEmpty()) {
1251               throw new Exception('Sorry, no such post.',404);
1252          }
1253         
1254          $cur = $this->core->con->openCursor($this->core->prefix.'comment');
1255         
1256          $cur->comment_author = $this->core->auth->getInfo('user_cn');
1257          $cur->comment_email = $this->core->auth->getInfo('user_email');
1258          $cur->comment_site = $this->core->auth->getInfo('user_url');
1259         
1260          $cur->comment_content = $struct['content'];
1261          $cur->post_id = (integer) $post_id;
1262         
1263          $id = $this->core->blog->addComment($cur);
1264          return $id;
1265     }
1266     
1267     private function updComment($user,$pwd,$comment_id,$struct)
1268     {
1269          $this->setUser($user,$pwd);
1270          $this->setBlog();
1271         
1272          $cur = $this->core->con->openCursor($this->core->prefix.'comment');
1273         
1274          if (isset($struct['status'])) {
1275               $cur->comment_status = $this->translateWpCommentstatus($struct['status']);
1276          }
1277         
1278          if (isset($struct['date_created_gmt'])) {
1279               if ($struct['date_created_gmt'] instanceof xmlrpcDate) {
1280                    $cur->comment_dt = date('Y-m-d H:i:00',$struct['date_created_gmt']->getTimestamp());
1281               } elseif (is_string($struct['date_created_gmt']) && @strtotime($struct['date_created_gmt'])) {
1282                    $cur->comment_dt = date('Y-m-d H:i:00',strtotime($struct['date_created_gmt']));
1283               }
1284               $cur->comment_dt = $struct['date_created_gmt'];
1285          }
1286         
1287          if (isset($struct['content'])) {
1288               $cur->comment_content = $struct['content'];
1289          }
1290         
1291          if (isset($struct['author'])) {
1292               $cur->comment_author = $struct['author'];
1293          }
1294         
1295          if (isset($struct['author_url'])) {
1296               $cur->comment_site = $struct['author_url'];
1297          }
1298         
1299          if (isset($struct['author_email'])) {
1300               $cur->comment_email = $struct['author_email'];
1301          }
1302         
1303          $this->core->blog->updComment($comment_id,$cur);
1304          return true;
1305     }
1306     
1307     private function delComment($user,$pwd,$comment_id)
1308     {
1309          $this->setUser($user,$pwd);
1310          $this->setBlog();
1311         
1312          $this->core->blog->delComment($comment_id);
1313          return true;
1314     }
1315     
1316     /* Blogger methods
1317     --------------------------------------------------- */
1318     public function blogger_newPost($appkey,$blogid,$username,$password,$content,$publish)
1319     {
1320          return $this->newPost($blogid,$username,$password,$content,array(),$publish);
1321     }
1322     
1323     public function blogger_editPost($appkey,$postid,$username,$password,$content,$publish)
1324     {
1325          return $this->editPost($postid,$username,$password,$content,array(),$publish);
1326     }
1327     
1328     public function blogger_getPost($appkey,$postid,$username,$password)
1329     {
1330          return $this->getPost($postid,$username,$password,'blogger');
1331     }
1332     
1333     public function blogger_deletePost($appkey,$postid,$username,$password,$publish)
1334     {
1335          return $this->deletePost($postid,$username,$password);
1336     }
1337     
1338     public function blogger_getRecentPosts($appkey,$blogid,$username,$password,$numberOfPosts)
1339     {
1340          return $this->getRecentPosts($blogid,$username,$password,$numberOfPosts,'blogger');
1341     }
1342     
1343     public function blogger_getUserBlogs($appkey,$username,$password)
1344     {
1345          return $this->getUserBlogs($username,$password);
1346     }
1347     
1348     public function blogger_getUserInfo($appkey,$username,$password)
1349     {
1350          return $this->getUserInfo($username,$password);
1351     }
1352     
1353     
1354     /* Metaweblog methods
1355     ------------------------------------------------------- */
1356     public function mw_newPost($blogid,$username,$password,$content,$publish)
1357     {
1358          return $this->newPost($blogid,$username,$password,'',$content,$publish);
1359     }
1360     
1361     public function mw_editPost($postid,$username,$password,$content,$publish)
1362     {
1363          return $this->editPost($postid,$username,$password,'',$content,$publish);
1364     }
1365     
1366     public function mw_getPost($postid,$username,$password)
1367     {
1368          return $this->getPost($postid,$username,$password,'mw');
1369     }
1370     
1371     public function mw_getRecentPosts($blogid,$username,$password,$numberOfPosts)
1372     {
1373          return $this->getRecentPosts($blogid,$username,$password,$numberOfPosts,'mw');
1374     }
1375     
1376     public function mw_getCategories($blogid,$username,$password)
1377     {
1378          return $this->getCategories($blogid,$username,$password);
1379     }
1380     
1381     public function mw_newMediaObject($blogid,$username,$password,$file)
1382     {
1383          return $this->newMediaObject($blogid,$username,$password,$file);
1384     }
1385     
1386     /* MovableType methods
1387     --------------------------------------------------- */
1388     public function mt_getRecentPostTitles($blogid,$username,$password,$numberOfPosts)
1389     {
1390          return $this->getRecentPosts($blogid,$username,$password,$numberOfPosts,'mt');
1391     }
1392     
1393     public function mt_getCategoryList($blogid,$username,$password)
1394     {
1395          return $this->getCategories($blogid,$username,$password);
1396     }
1397     
1398     public function mt_getPostCategories($postid,$username,$password)
1399     {
1400          return $this->getPostCategories($postid,$username,$password);
1401     }
1402     
1403     public function mt_setPostCategories($postid,$username,$password,$categories)
1404     {
1405          return $this->setPostCategories($postid,$username,$password,$categories);
1406     }
1407     
1408     public function mt_publishPost($postid,$username,$password)
1409     {
1410          return $this->publishPost($postid,$username,$password);
1411     }
1412     
1413     public function mt_supportedTextFilters()
1414     {
1415          return array();
1416     }
1417     
1418     /* WordPress methods
1419     --------------------------------------------------- */
1420     public function wp_getUsersBlogs($username,$password)
1421     {
1422          return $this->getUserBlogs($username,$password);
1423     }
1424     
1425     public function wp_getPage($blogid,$pageid,$username,$password)
1426     {
1427          $res = $this->getPages($blogid,$username,$password,null,$pageid);
1428         
1429          if (empty($res)) {
1430               throw new Exception('Sorry, no such page',404);
1431          }
1432         
1433          return $res[0];
1434     }
1435     
1436     public function wp_getPages($blogid,$username,$password,$num=10)
1437     {
1438          return $this->getPages($blogid,$username,$password,$num);
1439     }
1440     
1441     public function wp_newPage($blogid,$username,$password,$content,$publish)
1442     {
1443          return $this->newPage($blogid,$username,$password,$content,$publish);
1444     }
1445     
1446     public function wp_deletePage($blogid,$username,$password,$pageid)
1447     {
1448          return $this->deletePage($pageid,$username,$password);
1449     }
1450     
1451     public function wp_editPage($blogid,$pageid,$username,$password,$content,$publish)
1452     {
1453          return $this->editPage($pageid,$username,$password,$content,$publish);
1454     }
1455     
1456     public function wp_getPageList($blogid,$username,$password)
1457     {
1458          $A = $this->getPages($blogid,$username,$password);
1459          $res = array();
1460          foreach ($A as $v) {
1461               $res[] = array(
1462                    'page_id' => $v['page_id'],
1463                    'page_title' => $v['title'],
1464                    'page_parent_id' => $v['wp_page_parent_id'],
1465                    'dateCreated' => $v['dateCreated'],
1466                    'date_created_gmt' => $v['date_created_gmt']
1467               );
1468          }
1469          return $res;
1470     }
1471     
1472     public function wp_getAuthors($blogid,$username,$password)
1473     {
1474          return $this->getAuthors($username,$password);
1475     }
1476     
1477     public function wp_getCategories($blogid,$username,$password)
1478     {
1479          return $this->getCategories($blogid,$username,$password);
1480     }
1481     
1482     public function wp_getTags($blogid,$username,$password)
1483     {
1484          return $this->getTags($username,$password);
1485     }
1486     
1487     public function wp_newCategory($blogid,$username,$password,$content)
1488     {
1489          return $this->newCategory($username,$password,$content);
1490     }
1491     
1492     public function wp_deleteCategory($blogid,$username,$password,$categoryid)
1493     {
1494          return $this->deleteCategory($username,$password,$categoryid);
1495     }
1496     
1497     public function wp_suggestCategories($blogid,$username,$password,$category,$max_results=0)
1498     {
1499          return $this->searchCategories($username,$password,$category,$max_results);
1500     }
1501     
1502     public function wp_uploadFile($blogid,$username,$password,$file)
1503     {
1504          return $this->newMediaObject($blogid,$username,$password,$file);
1505     }
1506     
1507     public function wp_getPostStatusList($blogid,$username,$password)
1508     {
1509          return $this->getPostStatusList($blogid,$username,$password);
1510     }
1511     
1512     public function wp_getPageStatusList($blogid,$username,$password)
1513     {
1514          return $this->getPostStatusList($blogid,$username,$password);
1515     }
1516     
1517     public function wp_getPageTemplates($blogid,$username,$password)
1518     {
1519          return array('Default' => 'default');
1520     }
1521     
1522     public function wp_getOptions($blogid,$username,$password,$options=array())
1523     {
1524          $this->setUser($username,$password);
1525          $this->setBlog();
1526         
1527          return $this->translateWpOptions($options);
1528     }
1529     
1530     public function wp_setOptions($blogid,$username,$password,$options)
1531     {
1532          $this->setUser($username,$password);
1533          $this->setBlog();
1534         
1535          if (!$this->core->auth->check('admin',$this->core->blog->id)) {
1536               throw new Exception('Not enough permissions to edit options.',401);
1537          }
1538         
1539          $opt = $this->translateWpOptions();
1540         
1541          $done = array();
1542          $blog_changes = false;
1543          $cur = $this->core->con->openCursor($this->core->prefix.'blog');
1544         
1545          $this->core->blog->settings->addNamespace('system');
1546         
1547          foreach ($options as $name => $value)
1548          {
1549               if (!isset($opt[$name]) || $opt[$name]['readonly']) {
1550                    continue;
1551               }
1552               
1553               switch ($name)
1554               {
1555                    case 'blog_title':
1556                         $blog_changes = true;
1557                         $cur->blog_name = $value;
1558                         $done[] = $name;
1559                         break;
1560                    case 'blog_tagline':
1561                         $blog_changes = true;
1562                         $cur->blog_desc = $value;
1563                         $done[] = $name;
1564                         break;
1565                    case 'date_format':
1566                         $this->core->blog->settings->system->put('date_format',$value);
1567                         $done[] = $name;
1568                         break;
1569                    case 'time_format':
1570                         $this->core->blog->settings->system->put('time_format',$value);
1571                         $done[] = $name;
1572                         break;
1573               }
1574          }
1575         
1576          if ($blog_changes) {
1577               $this->core->updBlog($this->core->blog->id,$cur);
1578               $this->core->setBlog($this->core->blog->id);
1579          }
1580         
1581          return $this->translateWpOptions($done);
1582     }
1583     
1584     public function wp_getComment($blogid,$username,$password,$commentid)
1585     {
1586          $res = $this->getComments($username,$password,array(),$commentid);
1587         
1588          if (empty($res)) {
1589               throw new Exception('Sorry, no such comment',404);
1590          }
1591         
1592          return $res[0];
1593     }
1594     
1595     public function wp_getCommentCount($blogid,$username,$password,$postid)
1596     {
1597          return $this->countComments($username,$password,$postid);
1598     }
1599     
1600     public function wp_getComments($blogid,$username,$password,$struct)
1601     {
1602          return $this->getComments($username,$password,$struct);
1603     }
1604     
1605     public function wp_deleteComment($blogid,$username,$password,$commentid)
1606     {
1607          return $this->delComment($username,$password,$commentid);
1608     }
1609     
1610     public function wp_editComment($blogid,$username,$password,$commentid,$content)
1611     {
1612          return $this->updComment($username,$password,$commentid,$content);
1613     }
1614     
1615     public function wp_newComment($blogid,$username,$password,$postid,$content)
1616     {
1617          return $this->addComment($username,$password,$postid,$content);
1618     }
1619     
1620     public function wp_getCommentStatusList($blogid,$username,$password)
1621     {
1622          $this->setUser($username,$password);
1623          $this->setBlog();
1624         
1625          return array(
1626               'hold' => 'Unapproved',
1627               'approve' => 'Approved',
1628               'spam' => 'Spam'
1629          );
1630     }
1631}
1632?>
Note: See TracBrowser for help on using the repository browser.

Sites map