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)

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
7# Licensed under the GPL version 2.0 license.
8# See LICENSE file or
9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10#
11# -- END LICENSE BLOCK -----------------------------------------
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'));
28$core->rest->addFunction('setSectionFold',array('dcRestMethods','setSectionFold'));
29$core->rest->addFunction('getModuleById',array('dcRestMethods','getModuleById'));
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          }
41
42          $params = array('post_id' => (integer) $get['id']);
43
44          if (isset($get['post_type'])) {
45               $params['post_type'] = $get['post_type'];
46          }
47
48          $rs = $core->blog->getPosts($params);
49
50          if ($rs->isEmpty()) {
51               throw new Exception('No post for this ID');
52          }
53
54          $rsp = new xmlTag('post');
55          $rsp->id = $rs->post_id;
56
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);
85
86          $rsp->post_display_content($rs->getContent(true));
87          $rsp->post_display_excerpt($rs->getExcerpt(true));
88
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);
100
101          return $rsp;
102     }
103
104     public static function getCommentById($core,$get)
105     {
106          if (empty($get['id'])) {
107               throw new Exception('No comment ID');
108          }
109
110          $rs = $core->blog->getComments(array('comment_id' => (integer) $get['id']));
111
112          if ($rs->isEmpty()) {
113               throw new Exception('No comment for this ID');
114          }
115
116          $rsp = new xmlTag('post');
117          $rsp->id = $rs->comment_id;
118
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);
131
132          $rsp->comment_display_content($rs->getContent(true));
133
134          if ($core->auth->userID()) {
135               $rsp->comment_ip($rs->comment_ip);
136               $rsp->comment_email($rs->comment_email);
137               $rsp->comment_spam_disp(dcAntispam::statusMessage($rs));
138          }
139
140          return $rsp;
141     }
142
143     public static function quickPost($core,$get,$post)
144     {
145          # Create category
146          if (!empty($post['new_cat_title']) && $core->auth->check('categories', $core->blog->id)) {
147
148               $cur_cat = $core->con->openCursor($core->prefix.'category');
149               $cur_cat->cat_title = $post['new_cat_title'];
150               $cur_cat->cat_url = '';
151
152               $parent_cat = !empty($post['new_cat_parent']) ? $post['new_cat_parent'] : '';
153
154               # --BEHAVIOR-- adminBeforeCategoryCreate
155               $core->callBehavior('adminBeforeCategoryCreate', $cur_cat);
156
157               $post['cat_id'] = $core->blog->addCategory($cur_cat, (integer) $parent_cat);
158
159               # --BEHAVIOR-- adminAfterCategoryCreate
160               $core->callBehavior('adminAfterCategoryCreate', $cur_cat, $post['cat_id']);
161          }
162
163          $cur = $core->con->openCursor($core->prefix.'post');
164
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;
174
175          # --BEHAVIOR-- adminBeforePostCreate
176          $core->callBehavior('adminBeforePostCreate',$cur);
177
178          $return_id = $core->blog->addPost($cur);
179
180          # --BEHAVIOR-- adminAfterPostCreate
181          $core->callBehavior('adminAfterPostCreate',$cur,$return_id);
182
183          $rsp = new xmlTag('post');
184          $rsp->id = $return_id;
185
186          $post = $core->blog->getPosts(array('post_id' => $return_id));
187
188          $rsp->post_status = $post->post_status;
189          $rsp->post_url = $post->getURL();
190          return $rsp;
191     }
192
193     public static function validatePostMarkup($core,$get,$post)
194     {
195          if (!isset($post['excerpt'])) {
196               throw new Exception('No entry excerpt');
197          }
198
199          if (!isset($post['content'])) {
200               throw new Exception('No entry content');
201          }
202
203          if (empty($post['format'])) {
204               throw new Exception('No entry format');
205          }
206
207          if (!isset($post['lang'])) {
208               throw new Exception('No entry lang');
209          }
210
211          $excerpt = $post['excerpt'];
212          $excerpt_xhtml = '';
213          $content = $post['content'];
214          $content_xhtml = '';
215          $format = $post['format'];
216          $lang = $post['lang'];
217
218          $core->blog->setPostContent(0,$format,$lang,$excerpt,$excerpt_xhtml,$content,$content_xhtml);
219
220          $rsp = new xmlTag('result');
221
222          $v = htmlValidator::validate($excerpt_xhtml.$content_xhtml);
223
224          $rsp->valid($v['valid']);
225          $rsp->errors($v['errors']);
226
227          return $rsp;
228     }
229
230     public static function getZipMediaContent($core,$get,$post)
231     {
232          if (empty($get['id'])) {
233               throw new Exception('No media ID');
234          }
235
236          $id = (integer) $get['id'];
237
238          if (!$core->auth->check('media,media_admin',$core->blog)) {
239               throw new Exception('Permission denied');
240          }
241
242          try {
243               $core->media = new dcMedia($core);
244               $file = $core->media->getFile($id);
245          } catch (Exception $e) {}
246
247          if ($file === null || $file->type != 'application/zip' || !$file->editable) {
248               throw new Exception('Not a valid file');
249          }
250
251          $rsp = new xmlTag('result');
252          $content = $core->media->getZipContent($file);
253
254          foreach ($content as $k => $v) {
255               $rsp->file($k);
256          }
257
258          return $rsp;
259     }
260
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;
267
268          $sortby = !empty($get['sortby']) ? $get['sortby'] : 'meta_type,asc';
269
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);
276
277          $sortby = explode(',',$sortby);
278          $sort = $sortby[0];
279          $order = isset($sortby[1]) ? $sortby[1] : 'asc';
280
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          }
294
295          $rs->sort($sort,$order);
296
297          $rsp = new xmlTag();
298
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);
308
309               $rsp->insertNode($metaTag);
310          }
311
312          return $rsp;
313     }
314
315     public static function setPostMeta($core,$get,$post)
316     {
317          if (empty($post['postId'])) {
318               throw new Exception('No post ID');
319          }
320
321          if (empty($post['meta']) && $post['meta'] != '0') {
322               throw new Exception('No meta');
323          }
324
325          if (empty($post['metaType'])) {
326               throw new Exception('No meta type');
327          }
328
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          }
337
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          }
344
345          return true;
346     }
347
348     public static function delMeta($core,$get,$post)
349     {
350          if (empty($post['postId'])) {
351               throw new Exception('No post ID');
352          }
353
354          if (empty($post['metaId']) && $post['metaId'] != '0') {
355               throw new Exception('No meta ID');
356          }
357
358          if (empty($post['metaType'])) {
359               throw new Exception('No meta type');
360          }
361
362          $core->meta->delPostMeta($post['postId'],$post['metaType'],$post['metaId']);
363
364          return true;
365     }
366
367     public static function searchMeta($core,$get)
368     {
369          $q = !empty($get['q']) ? $get['q'] : null;
370          $metaType = !empty($get['metaType']) ? $get['metaType'] : null;
371
372          $sortby = !empty($get['sortby']) ? $get['sortby'] : 'meta_type,asc';
373
374          $rs = $core->meta->getMetadata(array('meta_type' => $metaType));
375          $rs = $core->meta->computeMetaStats($rs);
376
377          $sortby = explode(',',$sortby);
378          $sort = $sortby[0];
379          $order = isset($sortby[1]) ? $sortby[1] : 'asc';
380
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          }
394
395          $rs->sort($sort,$order);
396
397          $rsp = new xmlTag();
398
399          while ($rs->fetch())
400          {
401               if (stripos($rs->meta_id,$q) === 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);
409
410                    $rsp->insertNode($metaTag);
411               }
412          }
413
414          return $rsp;
415     }
416
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]);
436               }
437          } else { // false == unfold section ==> add it to unfolded list
438               if ($k === false) {
439                    $toggles[]=$section;
440               };
441          }
442          $core->auth->user_prefs->toggles->put('unfolded_sections',join(',',$toggles));
443          return true;
444     }
445
446     public static function getModuleById($core, $get, $post)
447     {
448          if (empty($get['id'])) {
449               throw new Exception('No module ID');
450          }
451          if (empty($get['list'])) {
452               throw new Exception('No list ID');
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') {
467               $store = new dcStore(
468                    $core->plugins,
469                    $core->blog->settings->system->store_plugin_url
470               );
471               $store->check();
472
473               $modules = $store->get();
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)) {
484               throw new Exception('Unknow module ID');
485          }
486
487          $module = adminModulesList::sanitizeModule($id, $module);
488
489          $rsp = new xmlTag('module');
490          $rsp->id = $id;
491
492          foreach($module as $k => $v) {
493               $rsp->{$k}((string) $v);
494          }
495
496          return $rsp;
497     }
498}
Note: See TracBrowser for help on using the repository browser.

Sites map