Dotclear

source: admin/services.php @ 3731:3770620079d4

Revision 3731:3770620079d4, 20.2 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 Backend
5 *
6 * @copyright Olivier Meunier & Association Dotclear
7 * @copyright GPL-2.0-only
8 */
9
10require dirname(__FILE__) . '/../inc/admin/prepend.php';
11
12$core->rest->addFunction('checkNewsUpdate', array('dcRestMethods', 'checkNewsUpdate'));
13$core->rest->addFunction('checkCoreUpdate', array('dcRestMethods', 'checkCoreUpdate'));
14$core->rest->addFunction('getPostById', array('dcRestMethods', 'getPostById'));
15$core->rest->addFunction('getCommentById', array('dcRestMethods', 'getCommentById'));
16$core->rest->addFunction('quickPost', array('dcRestMethods', 'quickPost'));
17$core->rest->addFunction('validatePostMarkup', array('dcRestMethods', 'validatePostMarkup'));
18$core->rest->addFunction('getZipMediaContent', array('dcRestMethods', 'getZipMediaContent'));
19$core->rest->addFunction('getMeta', array('dcRestMethods', 'getMeta'));
20$core->rest->addFunction('delMeta', array('dcRestMethods', 'delMeta'));
21$core->rest->addFunction('setPostMeta', array('dcRestMethods', 'setPostMeta'));
22$core->rest->addFunction('searchMeta', array('dcRestMethods', 'searchMeta'));
23$core->rest->addFunction('setSectionFold', array('dcRestMethods', 'setSectionFold'));
24$core->rest->addFunction('getModuleById', array('dcRestMethods', 'getModuleById'));
25
26$core->rest->serve();
27
28/* Common REST methods */
29class dcRestMethods
30{
31    public static function checkNewsUpdate($core, $get)
32    {
33        # Dotclear news
34
35        $rsp        = new xmlTag('news');
36        $rsp->check = false;
37        $ret        = __('Dotclear news not available');
38
39        if ($core->auth->user_prefs->dashboard->dcnews) {
40            try
41            {
42
43                if (empty($GLOBALS['__resources']['rss_news'])) {
44                    throw new Exception();
45                }
46                $feed_reader = new feedReader;
47                $feed_reader->setCacheDir(DC_TPL_CACHE);
48                $feed_reader->setTimeout(2);
49                $feed_reader->setUserAgent('Dotclear - http://www.dotclear.org/');
50                $feed = $feed_reader->parse($GLOBALS['__resources']['rss_news']);
51                if ($feed) {
52                    $ret = '<div class="box medium dc-box"><h3>' . __('Dotclear news') . '</h3><dl id="news">';
53                    $i   = 1;
54                    foreach ($feed->items as $item) {
55                        $dt = isset($item->link) ? '<a href="' . $item->link . '" class="outgoing" title="' . $item->title . '">' .
56                        $item->title . ' <img src="images/outgoing-blue.png" alt="" /></a>' : $item->title;
57
58                        if ($i < 3) {
59                            $ret .=
60                            '<dt>' . $dt . '</dt>' .
61                            '<dd><p><strong>' . dt::dt2str(__('%d %B %Y:'), $item->pubdate, 'Europe/Paris') . '</strong> ' .
62                            '<em>' . text::cutString(html::clean($item->content), 120) . '...</em></p></dd>';
63                        } else {
64                            $ret .=
65                            '<dt>' . $dt . '</dt>' .
66                            '<dd>' . dt::dt2str(__('%d %B %Y:'), $item->pubdate, 'Europe/Paris') . '</dd>';
67                        }
68                        $i++;
69                        if ($i > 2) {break;}
70                    }
71                    $ret .= '</dl></div>';
72                    $rsp->check = true;
73                }
74            } catch (Exception $e) {}
75        }
76        $rsp->ret = $ret;
77        return $rsp;
78    }
79
80    public static function checkCoreUpdate($core, $get)
81    {
82        # Dotclear updates notifications
83
84        $rsp        = new xmlTag('update');
85        $rsp->check = false;
86        $ret        = __('Dotclear update not available');
87
88        if ($core->auth->isSuperAdmin() && !DC_NOT_UPDATE && is_readable(DC_DIGESTS) &&
89            !$core->auth->user_prefs->dashboard->nodcupdate) {
90            $updater      = new dcUpdate(DC_UPDATE_URL, 'dotclear', DC_UPDATE_VERSION, DC_TPL_CACHE . '/versions');
91            $new_v        = $updater->check(DC_VERSION);
92            $version_info = $new_v ? $updater->getInfoURL() : '';
93
94            if ($updater->getNotify() && $new_v) {
95                // Check PHP version required
96                if (version_compare(phpversion(), $updater->getPHPVersion()) >= 0) {
97                    $ret =
98                    '<div class="dc-update"><h3>' . sprintf(__('Dotclear %s is available!'), $new_v) . '</h3> ' .
99                    '<p><a class="button submit" href="' . $core->adminurl->get("admin.update") . '">' . sprintf(__('Upgrade now'), $new_v) . '</a> ' .
100                    '<a class="button" href="' . $core->adminurl->get("admin.update", array('hide_msg' => 1)) . '">' . __('Remind me later') . '</a>' .
101                        ($version_info ? ' </p>' .
102                        '<p class="updt-info"><a href="' . $version_info . '">' . __('Information about this version') . '</a>' : '') . '</p>' .
103                        '</div>';
104                } else {
105                    $ret = '<p class="info">' .
106                    sprintf(__('A new version of Dotclear is available but needs PHP version ≥ %s, your\'s is currently %s'),
107                        $updater->getPHPVersion(), phpversion()) .
108                        '</p>';
109                }
110                $rsp->check = true;
111            } else {
112                if (version_compare(phpversion(), DC_NEXT_REQUIRED_PHP, '<')) {
113                    $ret = '<p class="info">' .
114                    sprintf(__('The next versions of Dotclear will not support PHP version < %s, your\'s is currently %s'),
115                        DC_NEXT_REQUIRED_PHP, phpversion()) .
116                        '</p>';
117                    $rsp->check = true;
118                }
119            }
120        }
121        $rsp->ret = $ret;
122        return $rsp;
123    }
124
125    public static function getPostById($core, $get)
126    {
127        if (empty($get['id'])) {
128            throw new Exception('No post ID');
129        }
130
131        $params = array('post_id' => (integer) $get['id']);
132
133        if (isset($get['post_type'])) {
134            $params['post_type'] = $get['post_type'];
135        }
136
137        $rs = $core->blog->getPosts($params);
138
139        if ($rs->isEmpty()) {
140            throw new Exception('No post for this ID');
141        }
142
143        $rsp     = new xmlTag('post');
144        $rsp->id = $rs->post_id;
145
146        $rsp->blog_id($rs->blog_id);
147        $rsp->user_id($rs->user_id);
148        $rsp->cat_id($rs->cat_id);
149        $rsp->post_dt($rs->post_dt);
150        $rsp->post_creadt($rs->post_creadt);
151        $rsp->post_upddt($rs->post_upddt);
152        $rsp->post_format($rs->post_format);
153        $rsp->post_url($rs->post_url);
154        $rsp->post_lang($rs->post_lang);
155        $rsp->post_title($rs->post_title);
156        $rsp->post_excerpt($rs->post_excerpt);
157        $rsp->post_excerpt_xhtml($rs->post_excerpt_xhtml);
158        $rsp->post_content($rs->post_content);
159        $rsp->post_content_xhtml($rs->post_content_xhtml);
160        $rsp->post_notes($rs->post_notes);
161        $rsp->post_status($rs->post_status);
162        $rsp->post_selected($rs->post_selected);
163        $rsp->post_open_comment($rs->post_open_comment);
164        $rsp->post_open_tb($rs->post_open_tb);
165        $rsp->nb_comment($rs->nb_comment);
166        $rsp->nb_trackback($rs->nb_trackback);
167        $rsp->user_name($rs->user_name);
168        $rsp->user_firstname($rs->user_firstname);
169        $rsp->user_displayname($rs->user_displayname);
170        $rsp->user_email($rs->user_email);
171        $rsp->user_url($rs->user_url);
172        $rsp->cat_title($rs->cat_title);
173        $rsp->cat_url($rs->cat_url);
174
175        $rsp->post_display_content($rs->getContent(true));
176        $rsp->post_display_excerpt($rs->getExcerpt(true));
177
178        $metaTag = new xmlTag('meta');
179        if (($meta = @unserialize($rs->post_meta)) !== false) {
180            foreach ($meta as $K => $V) {
181                foreach ($V as $v) {
182                    $metaTag->$K($v);
183                }
184            }
185        }
186        $rsp->post_meta($metaTag);
187
188        return $rsp;
189    }
190
191    public static function getCommentById($core, $get)
192    {
193        if (empty($get['id'])) {
194            throw new Exception('No comment ID');
195        }
196
197        $rs = $core->blog->getComments(array('comment_id' => (integer) $get['id']));
198
199        if ($rs->isEmpty()) {
200            throw new Exception('No comment for this ID');
201        }
202
203        $rsp     = new xmlTag('post');
204        $rsp->id = $rs->comment_id;
205
206        $rsp->comment_dt($rs->comment_dt);
207        $rsp->comment_upddt($rs->comment_upddt);
208        $rsp->comment_author($rs->comment_author);
209        $rsp->comment_site($rs->comment_site);
210        $rsp->comment_content($rs->comment_content);
211        $rsp->comment_trackback($rs->comment_trackback);
212        $rsp->comment_status($rs->comment_status);
213        $rsp->post_title($rs->post_title);
214        $rsp->post_url($rs->post_url);
215        $rsp->post_id($rs->post_id);
216        $rsp->post_dt($rs->post_dt);
217        $rsp->user_id($rs->user_id);
218
219        $rsp->comment_display_content($rs->getContent(true));
220
221        if ($core->auth->userID()) {
222            $rsp->comment_ip($rs->comment_ip);
223            $rsp->comment_email($rs->comment_email);
224            $rsp->comment_spam_disp(dcAntispam::statusMessage($rs));
225        }
226
227        return $rsp;
228    }
229
230    public static function quickPost($core, $get, $post)
231    {
232        # Create category
233        if (!empty($post['new_cat_title']) && $core->auth->check('categories', $core->blog->id)) {
234
235            $cur_cat            = $core->con->openCursor($core->prefix . 'category');
236            $cur_cat->cat_title = $post['new_cat_title'];
237            $cur_cat->cat_url   = '';
238
239            $parent_cat = !empty($post['new_cat_parent']) ? $post['new_cat_parent'] : '';
240
241            # --BEHAVIOR-- adminBeforeCategoryCreate
242            $core->callBehavior('adminBeforeCategoryCreate', $cur_cat);
243
244            $post['cat_id'] = $core->blog->addCategory($cur_cat, (integer) $parent_cat);
245
246            # --BEHAVIOR-- adminAfterCategoryCreate
247            $core->callBehavior('adminAfterCategoryCreate', $cur_cat, $post['cat_id']);
248        }
249
250        $cur = $core->con->openCursor($core->prefix . 'post');
251
252        $cur->post_title        = !empty($post['post_title']) ? $post['post_title'] : '';
253        $cur->user_id           = $core->auth->userID();
254        $cur->post_content      = !empty($post['post_content']) ? $post['post_content'] : '';
255        $cur->cat_id            = !empty($post['cat_id']) ? (integer) $post['cat_id'] : null;
256        $cur->post_format       = !empty($post['post_format']) ? $post['post_format'] : 'xhtml';
257        $cur->post_lang         = !empty($post['post_lang']) ? $post['post_lang'] : '';
258        $cur->post_status       = !empty($post['post_status']) ? (integer) $post['post_status'] : 0;
259        $cur->post_open_comment = (integer) $core->blog->settings->system->allow_comments;
260        $cur->post_open_tb      = (integer) $core->blog->settings->system->allow_trackbacks;
261
262        # --BEHAVIOR-- adminBeforePostCreate
263        $core->callBehavior('adminBeforePostCreate', $cur);
264
265        $return_id = $core->blog->addPost($cur);
266
267        # --BEHAVIOR-- adminAfterPostCreate
268        $core->callBehavior('adminAfterPostCreate', $cur, $return_id);
269
270        $rsp     = new xmlTag('post');
271        $rsp->id = $return_id;
272
273        $post = $core->blog->getPosts(array('post_id' => $return_id));
274
275        $rsp->post_status = $post->post_status;
276        $rsp->post_url    = $post->getURL();
277        return $rsp;
278    }
279
280    public static function validatePostMarkup($core, $get, $post)
281    {
282        if (!isset($post['excerpt'])) {
283            throw new Exception('No entry excerpt');
284        }
285
286        if (!isset($post['content'])) {
287            throw new Exception('No entry content');
288        }
289
290        if (empty($post['format'])) {
291            throw new Exception('No entry format');
292        }
293
294        if (!isset($post['lang'])) {
295            throw new Exception('No entry lang');
296        }
297
298        $excerpt       = $post['excerpt'];
299        $excerpt_xhtml = '';
300        $content       = $post['content'];
301        $content_xhtml = '';
302        $format        = $post['format'];
303        $lang          = $post['lang'];
304
305        $core->blog->setPostContent(0, $format, $lang, $excerpt, $excerpt_xhtml, $content, $content_xhtml);
306
307        $rsp = new xmlTag('result');
308
309        $v = htmlValidator::validate($excerpt_xhtml . $content_xhtml);
310
311        $rsp->valid($v['valid']);
312        $rsp->errors($v['errors']);
313
314        return $rsp;
315    }
316
317    public static function getZipMediaContent($core, $get, $post)
318    {
319        if (empty($get['id'])) {
320            throw new Exception('No media ID');
321        }
322
323        $id = (integer) $get['id'];
324
325        if (!$core->auth->check('media,media_admin', $core->blog)) {
326            throw new Exception('Permission denied');
327        }
328
329        try {
330            $core->media = new dcMedia($core);
331            $file        = $core->media->getFile($id);
332        } catch (Exception $e) {}
333
334        if ($file === null || $file->type != 'application/zip' || !$file->editable) {
335            throw new Exception('Not a valid file');
336        }
337
338        $rsp     = new xmlTag('result');
339        $content = $core->media->getZipContent($file);
340
341        foreach ($content as $k => $v) {
342            $rsp->file($k);
343        }
344
345        return $rsp;
346    }
347
348    public static function getMeta($core, $get)
349    {
350        $postid   = !empty($get['postId']) ? $get['postId'] : null;
351        $limit    = !empty($get['limit']) ? $get['limit'] : null;
352        $metaId   = !empty($get['metaId']) ? $get['metaId'] : null;
353        $metaType = !empty($get['metaType']) ? $get['metaType'] : null;
354
355        $sortby = !empty($get['sortby']) ? $get['sortby'] : 'meta_type,asc';
356
357        $rs = $core->meta->getMetadata(array(
358            'meta_type' => $metaType,
359            'limit'     => $limit,
360            'meta_id'   => $metaId,
361            'post_id'   => $postid));
362        $rs = $core->meta->computeMetaStats($rs);
363
364        $sortby = explode(',', $sortby);
365        $sort   = $sortby[0];
366        $order  = isset($sortby[1]) ? $sortby[1] : 'asc';
367
368        switch ($sort) {
369            case 'metaId':
370                $sort = 'meta_id_lower';
371                break;
372            case 'count':
373                $sort = 'count';
374                break;
375            case 'metaType':
376                $sort = 'meta_type';
377                break;
378            default:
379                $sort = 'meta_type';
380        }
381
382        $rs->sort($sort, $order);
383
384        $rsp = new xmlTag();
385
386        while ($rs->fetch()) {
387            $metaTag               = new xmlTag('meta');
388            $metaTag->type         = $rs->meta_type;
389            $metaTag->uri          = rawurlencode($rs->meta_id);
390            $metaTag->count        = $rs->count;
391            $metaTag->percent      = $rs->percent;
392            $metaTag->roundpercent = $rs->roundpercent;
393            $metaTag->CDATA($rs->meta_id);
394
395            $rsp->insertNode($metaTag);
396        }
397
398        return $rsp;
399    }
400
401    public static function setPostMeta($core, $get, $post)
402    {
403        if (empty($post['postId'])) {
404            throw new Exception('No post ID');
405        }
406
407        if (empty($post['meta']) && $post['meta'] != '0') {
408            throw new Exception('No meta');
409        }
410
411        if (empty($post['metaType'])) {
412            throw new Exception('No meta type');
413        }
414
415        # Get previous meta for post
416        $post_meta = $core->meta->getMetadata(array(
417            'meta_type' => $post['metaType'],
418            'post_id'   => $post['postId']));
419        $pm = array();
420        while ($post_meta->fetch()) {
421            $pm[] = $post_meta->meta_id;
422        }
423
424        foreach ($core->meta->splitMetaValues($post['meta']) as $m) {
425            if (!in_array($m, $pm)) {
426                $core->meta->setPostMeta($post['postId'], $post['metaType'], $m);
427            }
428        }
429
430        return true;
431    }
432
433    public static function delMeta($core, $get, $post)
434    {
435        if (empty($post['postId'])) {
436            throw new Exception('No post ID');
437        }
438
439        if (empty($post['metaId']) && $post['metaId'] != '0') {
440            throw new Exception('No meta ID');
441        }
442
443        if (empty($post['metaType'])) {
444            throw new Exception('No meta type');
445        }
446
447        $core->meta->delPostMeta($post['postId'], $post['metaType'], $post['metaId']);
448
449        return true;
450    }
451
452    public static function searchMeta($core, $get)
453    {
454        $q        = !empty($get['q']) ? $get['q'] : null;
455        $metaType = !empty($get['metaType']) ? $get['metaType'] : null;
456
457        $sortby = !empty($get['sortby']) ? $get['sortby'] : 'meta_type,asc';
458
459        $rs = $core->meta->getMetadata(array('meta_type' => $metaType));
460        $rs = $core->meta->computeMetaStats($rs);
461
462        $sortby = explode(',', $sortby);
463        $sort   = $sortby[0];
464        $order  = isset($sortby[1]) ? $sortby[1] : 'asc';
465
466        switch ($sort) {
467            case 'metaId':
468                $sort = 'meta_id_lower';
469                break;
470            case 'count':
471                $sort = 'count';
472                break;
473            case 'metaType':
474                $sort = 'meta_type';
475                break;
476            default:
477                $sort = 'meta_type';
478        }
479
480        $rs->sort($sort, $order);
481
482        $rsp = new xmlTag();
483
484        while ($rs->fetch()) {
485            if (stripos($rs->meta_id, $q) === 0) {
486                $metaTag               = new xmlTag('meta');
487                $metaTag->type         = $rs->meta_type;
488                $metaTag->uri          = rawurlencode($rs->meta_id);
489                $metaTag->count        = $rs->count;
490                $metaTag->percent      = $rs->percent;
491                $metaTag->roundpercent = $rs->roundpercent;
492                $metaTag->CDATA($rs->meta_id);
493
494                $rsp->insertNode($metaTag);
495            }
496        }
497
498        return $rsp;
499    }
500
501    public static function setSectionFold($core, $get, $post)
502    {
503        if (empty($post['section'])) {
504            throw new Exception('No section name');
505        }
506        if ($core->auth->user_prefs->toggles === null) {
507            $core->auth->user_prefs->addWorkspace('toggles');
508        }
509        $section = $post['section'];
510        $status  = isset($post['value']) && ($post['value'] != 0);
511        if ($core->auth->user_prefs->toggles->prefExists('unfolded_sections')) {
512            $toggles = explode(',', trim($core->auth->user_prefs->toggles->unfolded_sections));
513        } else {
514            $toggles = array();
515        }
516        $k = array_search($section, $toggles);
517        if ($status) {
518            // true == Fold section ==> remove it from unfolded list
519            if ($k !== false) {
520                unset($toggles[$k]);
521            }
522        } else {
523            // false == unfold section ==> add it to unfolded list
524            if ($k === false) {
525                $toggles[] = $section;
526            };
527        }
528        $core->auth->user_prefs->toggles->put('unfolded_sections', join(',', $toggles));
529        return true;
530    }
531
532    public static function getModuleById($core, $get, $post)
533    {
534        if (empty($get['id'])) {
535            throw new Exception('No module ID');
536        }
537        if (empty($get['list'])) {
538            throw new Exception('No list ID');
539        }
540
541        $id     = $get['id'];
542        $list   = $get['list'];
543        $module = array();
544
545        if ($list == 'plugin-activate') {
546            $modules = $core->plugins->getModules();
547            if (empty($modules) || !isset($modules[$id])) {
548                throw new Exception('Unknow module ID');
549            }
550            $module = $modules[$id];
551        } elseif ($list == 'plugin-new') {
552            $store = new dcStore(
553                $core->plugins,
554                $core->blog->settings->system->store_plugin_url
555            );
556            $store->check();
557
558            $modules = $store->get();
559            if (empty($modules) || !isset($modules[$id])) {
560                throw new Exception('Unknow module ID');
561            }
562            $module = $modules[$id];
563        } else {
564            // behavior not implemented yet
565        }
566
567        if (empty($module)) {
568            throw new Exception('Unknow module ID');
569        }
570
571        $module = adminModulesList::sanitizeModule($id, $module);
572
573        $rsp     = new xmlTag('module');
574        $rsp->id = $id;
575
576        foreach ($module as $k => $v) {
577            $rsp->{$k}((string) $v);
578        }
579
580        return $rsp;
581    }
582}
Note: See TracBrowser for help on using the repository browser.

Sites map