Dotclear

source: admin/services.php @ 1500:555ae7c1320a

Revision 1500:555ae7c1320a, 11.1 KB checked in by Denis Jean-Christian <contact@…>, 12 years ago (diff)

Add missing javascript for new category on quickpost, fixes #1569

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

Sites map