Dotclear

source: admin/services.php @ 2572:a144553cd87b

Revision 2572:a144553cd87b, 13.1 KB checked in by Dsls, 12 years ago (diff)

Better text filtering in ajax searchMeta, search is now "starts-with" search rather than "contains" search (preg_match was a bit luxurious for this test)

RevLine 
[0]1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
[1179]6# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
[0]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 -----------------------------------------
12
13#if (isset($_GET['dcxd'])) {
14#    $_COOKIE['dcxd'] = $_GET['dcxd'];
15#}
16
17require dirname(__FILE__).'/../inc/admin/prepend.php';
18
19$core->rest->addFunction('getPostById',array('dcRestMethods','getPostById'));
20$core->rest->addFunction('getCommentById',array('dcRestMethods','getCommentById'));
21$core->rest->addFunction('quickPost',array('dcRestMethods','quickPost'));
22$core->rest->addFunction('validatePostMarkup',array('dcRestMethods','validatePostMarkup'));
23$core->rest->addFunction('getZipMediaContent',array('dcRestMethods','getZipMediaContent'));
24$core->rest->addFunction('getMeta',array('dcRestMethods','getMeta'));
25$core->rest->addFunction('delMeta',array('dcRestMethods','delMeta'));
26$core->rest->addFunction('setPostMeta',array('dcRestMethods','setPostMeta'));
27$core->rest->addFunction('searchMeta',array('dcRestMethods','searchMeta'));
[1699]28$core->rest->addFunction('setSectionFold',array('dcRestMethods','setSectionFold'));
[2156]29$core->rest->addFunction('getModuleById',array('dcRestMethods','getModuleById'));
[0]30
31$core->rest->serve();
32
33/* Common REST methods */
34class dcRestMethods
35{
36     public static function getPostById($core,$get)
37     {
38          if (empty($get['id'])) {
39               throw new Exception('No post ID');
40          }
[2566]41
[0]42          $params = array('post_id' => (integer) $get['id']);
[2566]43
[0]44          if (isset($get['post_type'])) {
45               $params['post_type'] = $get['post_type'];
46          }
[2566]47
[0]48          $rs = $core->blog->getPosts($params);
[2566]49
[0]50          if ($rs->isEmpty()) {
51               throw new Exception('No post for this ID');
52          }
[2566]53
[0]54          $rsp = new xmlTag('post');
55          $rsp->id = $rs->post_id;
[2566]56
[0]57          $rsp->blog_id($rs->blog_id);
58          $rsp->user_id($rs->user_id);
59          $rsp->cat_id($rs->cat_id);
60          $rsp->post_dt($rs->post_dt);
61          $rsp->post_creadt($rs->post_creadt);
62          $rsp->post_upddt($rs->post_upddt);
63          $rsp->post_format($rs->post_format);
64          $rsp->post_url($rs->post_url);
65          $rsp->post_lang($rs->post_lang);
66          $rsp->post_title($rs->post_title);
67          $rsp->post_excerpt($rs->post_excerpt);
68          $rsp->post_excerpt_xhtml($rs->post_excerpt_xhtml);
69          $rsp->post_content($rs->post_content);
70          $rsp->post_content_xhtml($rs->post_content_xhtml);
71          $rsp->post_notes($rs->post_notes);
72          $rsp->post_status($rs->post_status);
73          $rsp->post_selected($rs->post_selected);
74          $rsp->post_open_comment($rs->post_open_comment);
75          $rsp->post_open_tb($rs->post_open_tb);
76          $rsp->nb_comment($rs->nb_comment);
77          $rsp->nb_trackback($rs->nb_trackback);
78          $rsp->user_name($rs->user_name);
79          $rsp->user_firstname($rs->user_firstname);
80          $rsp->user_displayname($rs->user_displayname);
81          $rsp->user_email($rs->user_email);
82          $rsp->user_url($rs->user_url);
83          $rsp->cat_title($rs->cat_title);
84          $rsp->cat_url($rs->cat_url);
[2566]85
[0]86          $rsp->post_display_content($rs->getContent(true));
87          $rsp->post_display_excerpt($rs->getExcerpt(true));
[2566]88
[0]89          $metaTag = new xmlTag('meta');
90          if (($meta = @unserialize($rs->post_meta)) !== false)
91          {
92               foreach ($meta as $K => $V)
93               {
94                    foreach ($V as $v) {
95                         $metaTag->$K($v);
96                    }
97               }
98          }
99          $rsp->post_meta($metaTag);
[2566]100
[0]101          return $rsp;
102     }
[2566]103
[0]104     public static function getCommentById($core,$get)
105     {
106          if (empty($get['id'])) {
107               throw new Exception('No comment ID');
108          }
[2566]109
[0]110          $rs = $core->blog->getComments(array('comment_id' => (integer) $get['id']));
[2566]111
[0]112          if ($rs->isEmpty()) {
113               throw new Exception('No comment for this ID');
114          }
[2566]115
[0]116          $rsp = new xmlTag('post');
117          $rsp->id = $rs->comment_id;
[2566]118
[0]119          $rsp->comment_dt($rs->comment_dt);
120          $rsp->comment_upddt($rs->comment_upddt);
121          $rsp->comment_author($rs->comment_author);
122          $rsp->comment_site($rs->comment_site);
123          $rsp->comment_content($rs->comment_content);
124          $rsp->comment_trackback($rs->comment_trackback);
125          $rsp->comment_status($rs->comment_status);
126          $rsp->post_title($rs->post_title);
127          $rsp->post_url($rs->post_url);
128          $rsp->post_id($rs->post_id);
129          $rsp->post_dt($rs->post_dt);
130          $rsp->user_id($rs->user_id);
[2566]131
[0]132          $rsp->comment_display_content($rs->getContent(true));
[2566]133
[0]134          if ($core->auth->userID()) {
135               $rsp->comment_ip($rs->comment_ip);
136               $rsp->comment_email($rs->comment_email);
[323]137               $rsp->comment_spam_disp(dcAntispam::statusMessage($rs));
[0]138          }
[2566]139
[0]140          return $rsp;
141     }
[2566]142
[0]143     public static function quickPost($core,$get,$post)
144     {
[1500]145          # Create category
146          if (!empty($post['new_cat_title']) && $core->auth->check('categories', $core->blog->id)) {
[2566]147
[1500]148               $cur_cat = $core->con->openCursor($core->prefix.'category');
149               $cur_cat->cat_title = $post['new_cat_title'];
150               $cur_cat->cat_url = '';
[2566]151
[1500]152               $parent_cat = !empty($post['new_cat_parent']) ? $post['new_cat_parent'] : '';
[2566]153
[1500]154               # --BEHAVIOR-- adminBeforeCategoryCreate
155               $core->callBehavior('adminBeforeCategoryCreate', $cur_cat);
[2566]156
[1500]157               $post['cat_id'] = $core->blog->addCategory($cur_cat, (integer) $parent_cat);
[2566]158
[1500]159               # --BEHAVIOR-- adminAfterCategoryCreate
160               $core->callBehavior('adminAfterCategoryCreate', $cur_cat, $post['cat_id']);
161          }
[2566]162
[0]163          $cur = $core->con->openCursor($core->prefix.'post');
[2566]164
[0]165          $cur->post_title = !empty($post['post_title']) ? $post['post_title'] : '';
166          $cur->user_id = $core->auth->userID();
167          $cur->post_content = !empty($post['post_content']) ? $post['post_content'] : '';
168          $cur->cat_id = !empty($post['cat_id']) ? (integer) $post['cat_id'] : null;
169          $cur->post_format = !empty($post['post_format']) ? $post['post_format'] : 'xhtml';
170          $cur->post_lang = !empty($post['post_lang']) ? $post['post_lang'] : '';
171          $cur->post_status = !empty($post['post_status']) ? (integer) $post['post_status'] : 0;
172          $cur->post_open_comment = (integer) $core->blog->settings->system->allow_comments;
173          $cur->post_open_tb = (integer) $core->blog->settings->system->allow_trackbacks;
[2566]174
[0]175          # --BEHAVIOR-- adminBeforePostCreate
176          $core->callBehavior('adminBeforePostCreate',$cur);
[2566]177
[0]178          $return_id = $core->blog->addPost($cur);
[2566]179
[0]180          # --BEHAVIOR-- adminAfterPostCreate
181          $core->callBehavior('adminAfterPostCreate',$cur,$return_id);
[2566]182
[0]183          $rsp = new xmlTag('post');
184          $rsp->id = $return_id;
[2566]185
[0]186          $post = $core->blog->getPosts(array('post_id' => $return_id));
[2566]187
[0]188          $rsp->post_status = $post->post_status;
189          $rsp->post_url = $post->getURL();
190          return $rsp;
191     }
[2566]192
[0]193     public static function validatePostMarkup($core,$get,$post)
194     {
195          if (!isset($post['excerpt'])) {
196               throw new Exception('No entry excerpt');
197          }
[2566]198
[0]199          if (!isset($post['content'])) {
200               throw new Exception('No entry content');
201          }
[2566]202
[0]203          if (empty($post['format'])) {
204               throw new Exception('No entry format');
205          }
[2566]206
[0]207          if (!isset($post['lang'])) {
208               throw new Exception('No entry lang');
209          }
[2566]210
[0]211          $excerpt = $post['excerpt'];
212          $excerpt_xhtml = '';
213          $content = $post['content'];
214          $content_xhtml = '';
215          $format = $post['format'];
216          $lang = $post['lang'];
[2566]217
[0]218          $core->blog->setPostContent(0,$format,$lang,$excerpt,$excerpt_xhtml,$content,$content_xhtml);
[2566]219
[0]220          $rsp = new xmlTag('result');
[2566]221
[0]222          $v = htmlValidator::validate($excerpt_xhtml.$content_xhtml);
[2566]223
[0]224          $rsp->valid($v['valid']);
225          $rsp->errors($v['errors']);
[2566]226
[0]227          return $rsp;
228     }
[2566]229
[0]230     public static function getZipMediaContent($core,$get,$post)
231     {
232          if (empty($get['id'])) {
233               throw new Exception('No media ID');
234          }
[2566]235
[0]236          $id = (integer) $get['id'];
[2566]237
[0]238          if (!$core->auth->check('media,media_admin',$core->blog)) {
239               throw new Exception('Permission denied');
240          }
[2566]241
[1537]242          try {
243               $core->media = new dcMedia($core);
244               $file = $core->media->getFile($id);
245          } catch (Exception $e) {}
[2566]246
[0]247          if ($file === null || $file->type != 'application/zip' || !$file->editable) {
248               throw new Exception('Not a valid file');
249          }
[2566]250
[0]251          $rsp = new xmlTag('result');
252          $content = $core->media->getZipContent($file);
[2566]253
[0]254          foreach ($content as $k => $v) {
255               $rsp->file($k);
256          }
[2566]257
[0]258          return $rsp;
259     }
[2566]260
[0]261     public static function getMeta($core,$get)
262     {
263          $postid = !empty($get['postId']) ? $get['postId'] : null;
264          $limit = !empty($get['limit']) ? $get['limit'] : null;
265          $metaId = !empty($get['metaId']) ? $get['metaId'] : null;
266          $metaType = !empty($get['metaType']) ? $get['metaType'] : null;
[2566]267
[0]268          $sortby = !empty($get['sortby']) ? $get['sortby'] : 'meta_type,asc';
[2566]269
[0]270          $rs = $core->meta->getMetadata(array(
271               'meta_type' => $metaType,
272               'limit' => $limit,
273               'meta_id' => $metaId,
274               'post_id' => $postid));
275          $rs = $core->meta->computeMetaStats($rs);
[2566]276
[0]277          $sortby = explode(',',$sortby);
278          $sort = $sortby[0];
279          $order = isset($sortby[1]) ? $sortby[1] : 'asc';
[2566]280
[0]281          switch ($sort) {
282               case 'metaId':
283                    $sort = 'meta_id_lower';
284                    break;
285               case 'count':
286                    $sort = 'count';
287                    break;
288               case 'metaType':
289                    $sort = 'meta_type';
290                    break;
291               default:
292                    $sort = 'meta_type';
293          }
[2566]294
[0]295          $rs->sort($sort,$order);
[2566]296
[0]297          $rsp = new xmlTag();
[2566]298
[0]299          while ($rs->fetch())
300          {
301               $metaTag = new xmlTag('meta');
302               $metaTag->type = $rs->meta_type;
303               $metaTag->uri = rawurlencode($rs->meta_id);
304               $metaTag->count = $rs->count;
305               $metaTag->percent = $rs->percent;
306               $metaTag->roundpercent = $rs->roundpercent;
307               $metaTag->CDATA($rs->meta_id);
[2566]308
[0]309               $rsp->insertNode($metaTag);
310          }
[2566]311
[0]312          return $rsp;
313     }
[2566]314
[0]315     public static function setPostMeta($core,$get,$post)
316     {
317          if (empty($post['postId'])) {
318               throw new Exception('No post ID');
319          }
[2566]320
[0]321          if (empty($post['meta']) && $post['meta'] != '0') {
322               throw new Exception('No meta');
323          }
[2566]324
[0]325          if (empty($post['metaType'])) {
326               throw new Exception('No meta type');
327          }
[2566]328
[0]329          # Get previous meta for post
330          $post_meta = $core->meta->getMetadata(array(
331               'meta_type' => $post['metaType'],
332               'post_id' => $post['postId']));
333          $pm = array();
334          while ($post_meta->fetch()) {
335               $pm[] = $post_meta->meta_id;
336          }
[2566]337
[0]338          foreach ($core->meta->splitMetaValues($post['meta']) as $m)
339          {
340               if (!in_array($m,$pm)) {
341                    $core->meta->setPostMeta($post['postId'],$post['metaType'],$m);
342               }
343          }
[2566]344
[0]345          return true;
346     }
[2566]347
[0]348     public static function delMeta($core,$get,$post)
349     {
350          if (empty($post['postId'])) {
351               throw new Exception('No post ID');
352          }
[2566]353
[0]354          if (empty($post['metaId']) && $post['metaId'] != '0') {
355               throw new Exception('No meta ID');
356          }
[2566]357
[0]358          if (empty($post['metaType'])) {
359               throw new Exception('No meta type');
360          }
[2566]361
[0]362          $core->meta->delPostMeta($post['postId'],$post['metaType'],$post['metaId']);
[2566]363
[0]364          return true;
365     }
[2566]366
[0]367     public static function searchMeta($core,$get)
368     {
369          $q = !empty($get['q']) ? $get['q'] : null;
370          $metaType = !empty($get['metaType']) ? $get['metaType'] : null;
[2566]371
[0]372          $sortby = !empty($get['sortby']) ? $get['sortby'] : 'meta_type,asc';
[2566]373
[0]374          $rs = $core->meta->getMetadata(array('meta_type' => $metaType));
375          $rs = $core->meta->computeMetaStats($rs);
[2566]376
[0]377          $sortby = explode(',',$sortby);
378          $sort = $sortby[0];
379          $order = isset($sortby[1]) ? $sortby[1] : 'asc';
[2566]380
[0]381          switch ($sort) {
382               case 'metaId':
383                    $sort = 'meta_id_lower';
384                    break;
385               case 'count':
386                    $sort = 'count';
387                    break;
388               case 'metaType':
389                    $sort = 'meta_type';
390                    break;
391               default:
392                    $sort = 'meta_type';
393          }
[2566]394
[0]395          $rs->sort($sort,$order);
[2566]396
[0]397          $rsp = new xmlTag();
[2566]398
[0]399          while ($rs->fetch())
400          {
[2572]401               if (stripos($rs->meta_id,$q) === 0) {
[0]402                    $metaTag = new xmlTag('meta');
403                    $metaTag->type = $rs->meta_type;
404                    $metaTag->uri = rawurlencode($rs->meta_id);
405                    $metaTag->count = $rs->count;
406                    $metaTag->percent = $rs->percent;
407                    $metaTag->roundpercent = $rs->roundpercent;
408                    $metaTag->CDATA($rs->meta_id);
[2566]409
[0]410                    $rsp->insertNode($metaTag);
411               }
412          }
[2566]413
[0]414          return $rsp;
415     }
[2566]416
[1699]417     public static function setSectionFold($core,$get,$post)
418     {
419          if (empty($post['section'])) {
420               throw new Exception('No section name');
421          }
422          if ($core->auth->user_prefs->toggles === null) {
423               $core->auth->user_prefs->addWorkspace('toggles');
424          }
425          $section = $post['section'];
426          $status = isset($post['value']) && ($post['value'] != 0);
427          if ($core->auth->user_prefs->toggles->prefExists('unfolded_sections')) {
428               $toggles = explode(',',trim($core->auth->user_prefs->toggles->unfolded_sections));
429          } else {
430               $toggles = array();
431          }
432          $k = array_search($section,$toggles);
433          if ($status) { // true == Fold section ==> remove it from unfolded list
434               if ($k !== false) {
435                    unset($toggles[$k]);
[2566]436               }
[1699]437          } else { // false == unfold section ==> add it to unfolded list
438               if ($k === false) {
439                    $toggles[]=$section;
[2566]440               };
[1699]441          }
442          $core->auth->user_prefs->toggles->put('unfolded_sections',join(',',$toggles));
443          return true;
444     }
[2566]445
[2156]446     public static function getModuleById($core, $get, $post)
447     {
448          if (empty($get['id'])) {
[2215]449               throw new Exception('No module ID');
[2156]450          }
451          if (empty($get['list'])) {
[2215]452               throw new Exception('No list ID');
[2156]453          }
454
455          $id = $get['id'];
456          $list = $get['list'];
457          $module = array();
458
459          if ($list == 'plugin-activate') {
460               $modules = $core->plugins->getModules();
461               if (empty($modules) || !isset($modules[$id])) {
462                    throw new Exception('Unknow module ID');
463               }
464               $module = $modules[$id];
465          }
466          elseif ($list == 'plugin-new') {
[2216]467               $store = new dcStore(
[2566]468                    $core->plugins,
[2216]469                    $core->blog->settings->system->store_plugin_url
[2156]470               );
[2216]471               $store->check();
[2156]472
[2216]473               $modules = $store->get();
[2156]474               if (empty($modules) || !isset($modules[$id])) {
475                    throw new Exception('Unknow module ID');
476               }
477               $module = $modules[$id];
478          }
479          else {
480               // behavior not implemented yet
481          }
482
483          if (empty($module)) {
[2215]484               throw new Exception('Unknow module ID');
[2156]485          }
486
[2227]487          $module = adminModulesList::sanitizeModule($id, $module);
[2156]488
489          $rsp = new xmlTag('module');
490          $rsp->id = $id;
[2215]491
[2156]492          foreach($module as $k => $v) {
493               $rsp->{$k}((string) $v);
494          }
495
496          return $rsp;
497     }
[0]498}
Note: See TracBrowser for help on using the repository browser.

Sites map