Dotclear

source: admin/services.php @ 1699:5b68157bf85e

Revision 1699:5b68157bf85e, 12.1 KB checked in by Dsls, 11 years ago (diff)

Turned cookies into user_prefs for sections folding/unfolding, closes #1573

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

Sites map