Dotclear

source: admin/services.php @ 1538:b956d78197c5

Revision 1538:b956d78197c5, 11.1 KB checked in by Franck Paul <carnet.franck.paul@…>, 11 years ago (diff)

Merged in lipki/dotclear/ticket #1406-2 (pull request #59) - fixes #1406

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          try {
241               $core->media = new dcMedia($core);
242               $file = $core->media->getFile($id);
243          } catch (Exception $e) {}
244         
245          if ($file === null || $file->type != 'application/zip' || !$file->editable) {
246               throw new Exception('Not a valid file');
247          }
248         
249          $rsp = new xmlTag('result');
250          $content = $core->media->getZipContent($file);
251         
252          foreach ($content as $k => $v) {
253               $rsp->file($k);
254          }
255         
256          return $rsp;
257     }
258     
259     public static function getMeta($core,$get)
260     {
261          $postid = !empty($get['postId']) ? $get['postId'] : null;
262          $limit = !empty($get['limit']) ? $get['limit'] : null;
263          $metaId = !empty($get['metaId']) ? $get['metaId'] : null;
264          $metaType = !empty($get['metaType']) ? $get['metaType'] : null;
265         
266          $sortby = !empty($get['sortby']) ? $get['sortby'] : 'meta_type,asc';
267         
268          $rs = $core->meta->getMetadata(array(
269               'meta_type' => $metaType,
270               'limit' => $limit,
271               'meta_id' => $metaId,
272               'post_id' => $postid));
273          $rs = $core->meta->computeMetaStats($rs);
274         
275          $sortby = explode(',',$sortby);
276          $sort = $sortby[0];
277          $order = isset($sortby[1]) ? $sortby[1] : 'asc';
278         
279          switch ($sort) {
280               case 'metaId':
281                    $sort = 'meta_id_lower';
282                    break;
283               case 'count':
284                    $sort = 'count';
285                    break;
286               case 'metaType':
287                    $sort = 'meta_type';
288                    break;
289               default:
290                    $sort = 'meta_type';
291          }
292         
293          $rs->sort($sort,$order);
294         
295          $rsp = new xmlTag();
296         
297          while ($rs->fetch())
298          {
299               $metaTag = new xmlTag('meta');
300               $metaTag->type = $rs->meta_type;
301               $metaTag->uri = rawurlencode($rs->meta_id);
302               $metaTag->count = $rs->count;
303               $metaTag->percent = $rs->percent;
304               $metaTag->roundpercent = $rs->roundpercent;
305               $metaTag->CDATA($rs->meta_id);
306               
307               $rsp->insertNode($metaTag);
308          }
309         
310          return $rsp;
311     }
312     
313     public static function setPostMeta($core,$get,$post)
314     {
315          if (empty($post['postId'])) {
316               throw new Exception('No post ID');
317          }
318         
319          if (empty($post['meta']) && $post['meta'] != '0') {
320               throw new Exception('No meta');
321          }
322         
323          if (empty($post['metaType'])) {
324               throw new Exception('No meta type');
325          }
326         
327          # Get previous meta for post
328          $post_meta = $core->meta->getMetadata(array(
329               'meta_type' => $post['metaType'],
330               'post_id' => $post['postId']));
331          $pm = array();
332          while ($post_meta->fetch()) {
333               $pm[] = $post_meta->meta_id;
334          }
335         
336          foreach ($core->meta->splitMetaValues($post['meta']) as $m)
337          {
338               if (!in_array($m,$pm)) {
339                    $core->meta->setPostMeta($post['postId'],$post['metaType'],$m);
340               }
341          }
342         
343          return true;
344     }
345     
346     public static function delMeta($core,$get,$post)
347     {
348          if (empty($post['postId'])) {
349               throw new Exception('No post ID');
350          }
351         
352          if (empty($post['metaId']) && $post['metaId'] != '0') {
353               throw new Exception('No meta ID');
354          }
355         
356          if (empty($post['metaType'])) {
357               throw new Exception('No meta type');
358          }
359         
360          $core->meta->delPostMeta($post['postId'],$post['metaType'],$post['metaId']);
361         
362          return true;
363     }
364     
365     public static function searchMeta($core,$get)
366     {
367          $q = !empty($get['q']) ? $get['q'] : null;
368          $metaType = !empty($get['metaType']) ? $get['metaType'] : null;
369         
370          $sortby = !empty($get['sortby']) ? $get['sortby'] : 'meta_type,asc';
371         
372          $rs = $core->meta->getMetadata(array('meta_type' => $metaType));
373          $rs = $core->meta->computeMetaStats($rs);
374         
375          $sortby = explode(',',$sortby);
376          $sort = $sortby[0];
377          $order = isset($sortby[1]) ? $sortby[1] : 'asc';
378         
379          switch ($sort) {
380               case 'metaId':
381                    $sort = 'meta_id_lower';
382                    break;
383               case 'count':
384                    $sort = 'count';
385                    break;
386               case 'metaType':
387                    $sort = 'meta_type';
388                    break;
389               default:
390                    $sort = 'meta_type';
391          }
392         
393          $rs->sort($sort,$order);
394         
395          $rsp = new xmlTag();
396         
397          while ($rs->fetch())
398          {
399               if (preg_match('/'.$q.'/i',$rs->meta_id)) {
400                    $metaTag = new xmlTag('meta');
401                    $metaTag->type = $rs->meta_type;
402                    $metaTag->uri = rawurlencode($rs->meta_id);
403                    $metaTag->count = $rs->count;
404                    $metaTag->percent = $rs->percent;
405                    $metaTag->roundpercent = $rs->roundpercent;
406                    $metaTag->CDATA($rs->meta_id);
407                   
408                    $rsp->insertNode($metaTag);
409               }
410          }
411         
412          return $rsp;
413     }
414}
415?>
Note: See TracBrowser for help on using the repository browser.

Sites map