Dotclear

source: inc/core/class.dc.xmlrpc.php @ 3731:3770620079d4

Revision 3731:3770620079d4, 54.5 KB checked in by franck <carnet.franck.paul@…>, 7 years ago (diff)

Simplify licence block at the beginning of each file

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

Sites map