Dotclear

source: admin/services.php @ 782:01efbf050a8a

Revision 782:01efbf050a8a, 11.1 KB checked in by Dsls <dsls@…>, 14 years ago (diff)

Merged last default changes with formfilter branch

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2011 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('searchCommentAuthor',array('dcRestMethods','searchCommentAuthor'));
28$core->rest->addFunction('searchMeta',array('dcRestMethods','searchMeta'));
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          $cur = $core->con->openCursor($core->prefix.'post');
145         
146          $cur->post_title = !empty($post['post_title']) ? $post['post_title'] : '';
147          $cur->user_id = $core->auth->userID();
148          $cur->post_content = !empty($post['post_content']) ? $post['post_content'] : '';
149          $cur->cat_id = !empty($post['cat_id']) ? (integer) $post['cat_id'] : null;
150          $cur->post_format = !empty($post['post_format']) ? $post['post_format'] : 'xhtml';
151          $cur->post_lang = !empty($post['post_lang']) ? $post['post_lang'] : '';
152          $cur->post_status = !empty($post['post_status']) ? (integer) $post['post_status'] : 0;
153          $cur->post_open_comment = (integer) $core->blog->settings->system->allow_comments;
154          $cur->post_open_tb = (integer) $core->blog->settings->system->allow_trackbacks;
155         
156          # --BEHAVIOR-- adminBeforePostCreate
157          $core->callBehavior('adminBeforePostCreate',$cur);
158         
159          $return_id = $core->blog->addPost($cur);
160         
161          # --BEHAVIOR-- adminAfterPostCreate
162          $core->callBehavior('adminAfterPostCreate',$cur,$return_id);
163         
164          $rsp = new xmlTag('post');
165          $rsp->id = $return_id;
166         
167          $post = $core->blog->getPosts(array('post_id' => $return_id));
168         
169          $rsp->post_status = $post->post_status;
170          $rsp->post_url = $post->getURL();
171          return $rsp;
172     }
173     
174     public static function validatePostMarkup($core,$get,$post)
175     {
176          if (!isset($post['excerpt'])) {
177               throw new Exception('No entry excerpt');
178          }
179         
180          if (!isset($post['content'])) {
181               throw new Exception('No entry content');
182          }
183         
184          if (empty($post['format'])) {
185               throw new Exception('No entry format');
186          }
187         
188          if (!isset($post['lang'])) {
189               throw new Exception('No entry lang');
190          }
191         
192          $excerpt = $post['excerpt'];
193          $excerpt_xhtml = '';
194          $content = $post['content'];
195          $content_xhtml = '';
196          $format = $post['format'];
197          $lang = $post['lang'];
198         
199          $core->blog->setPostContent(0,$format,$lang,$excerpt,$excerpt_xhtml,$content,$content_xhtml);
200         
201          $rsp = new xmlTag('result');
202         
203          $v = htmlValidator::validate($excerpt_xhtml.$content_xhtml);
204         
205          $rsp->valid($v['valid']);
206          $rsp->errors($v['errors']);
207         
208          return $rsp;
209     }
210     
211     public static function getZipMediaContent($core,$get,$post)
212     {
213          if (empty($get['id'])) {
214               throw new Exception('No media ID');
215          }
216         
217          $id = (integer) $get['id'];
218         
219          if (!$core->auth->check('media,media_admin',$core->blog)) {
220               throw new Exception('Permission denied');
221          }
222         
223          $core->media = new dcMedia($core);
224          $file = $core->media->getFile($id);
225         
226          if ($file === null || $file->type != 'application/zip' || !$file->editable) {
227               throw new Exception('Not a valid file');
228          }
229         
230          $rsp = new xmlTag('result');
231          $content = $core->media->getZipContent($file);
232         
233          foreach ($content as $k => $v) {
234               $rsp->file($k);
235          }
236         
237          return $rsp;
238     }
239     
240     public static function getMeta($core,$get)
241     {
242          $postid = !empty($get['postId']) ? $get['postId'] : null;
243          $limit = !empty($get['limit']) ? $get['limit'] : null;
244          $metaId = !empty($get['metaId']) ? $get['metaId'] : null;
245          $metaType = !empty($get['metaType']) ? $get['metaType'] : null;
246         
247          $sortby = !empty($get['sortby']) ? $get['sortby'] : 'meta_type,asc';
248         
249          $rs = $core->meta->getMetadata(array(
250               'meta_type' => $metaType,
251               'limit' => $limit,
252               'meta_id' => $metaId,
253               'post_id' => $postid));
254          $rs = $core->meta->computeMetaStats($rs);
255         
256          $sortby = explode(',',$sortby);
257          $sort = $sortby[0];
258          $order = isset($sortby[1]) ? $sortby[1] : 'asc';
259         
260          switch ($sort) {
261               case 'metaId':
262                    $sort = 'meta_id_lower';
263                    break;
264               case 'count':
265                    $sort = 'count';
266                    break;
267               case 'metaType':
268                    $sort = 'meta_type';
269                    break;
270               default:
271                    $sort = 'meta_type';
272          }
273         
274          $rs->sort($sort,$order);
275         
276          $rsp = new xmlTag();
277         
278          while ($rs->fetch())
279          {
280               $metaTag = new xmlTag('meta');
281               $metaTag->type = $rs->meta_type;
282               $metaTag->uri = rawurlencode($rs->meta_id);
283               $metaTag->count = $rs->count;
284               $metaTag->percent = $rs->percent;
285               $metaTag->roundpercent = $rs->roundpercent;
286               $metaTag->CDATA($rs->meta_id);
287               
288               $rsp->insertNode($metaTag);
289          }
290         
291          return $rsp;
292     }
293     
294     public static function setPostMeta($core,$get,$post)
295     {
296          if (empty($post['postId'])) {
297               throw new Exception('No post ID');
298          }
299         
300          if (empty($post['meta']) && $post['meta'] != '0') {
301               throw new Exception('No meta');
302          }
303         
304          if (empty($post['metaType'])) {
305               throw new Exception('No meta type');
306          }
307         
308          # Get previous meta for post
309          $post_meta = $core->meta->getMetadata(array(
310               'meta_type' => $post['metaType'],
311               'post_id' => $post['postId']));
312          $pm = array();
313          while ($post_meta->fetch()) {
314               $pm[] = $post_meta->meta_id;
315          }
316         
317          foreach ($core->meta->splitMetaValues($post['meta']) as $m)
318          {
319               if (!in_array($m,$pm)) {
320                    $core->meta->setPostMeta($post['postId'],$post['metaType'],$m);
321               }
322          }
323         
324          return true;
325     }
326     
327     public static function delMeta($core,$get,$post)
328     {
329          if (empty($post['postId'])) {
330               throw new Exception('No post ID');
331          }
332         
333          if (empty($post['metaId']) && $post['metaId'] != '0') {
334               throw new Exception('No meta ID');
335          }
336         
337          if (empty($post['metaType'])) {
338               throw new Exception('No meta type');
339          }
340         
341          $core->meta->delPostMeta($post['postId'],$post['metaType'],$post['metaId']);
342         
343          return true;
344     }
345     
346     public static function searchMeta($core,$get)
347     {
348          $q = !empty($get['q']) ? $get['q'] : null;
349          $metaType = !empty($get['metaType']) ? $get['metaType'] : null;
350         
351          $sortby = !empty($get['sortby']) ? $get['sortby'] : 'meta_type,asc';
352         
353          $rs = $core->meta->getMetadata(array('meta_type' => $metaType));
354          $rs = $core->meta->computeMetaStats($rs);
355         
356          $sortby = explode(',',$sortby);
357          $sort = $sortby[0];
358          $order = isset($sortby[1]) ? $sortby[1] : 'asc';
359         
360          switch ($sort) {
361               case 'metaId':
362                    $sort = 'meta_id_lower';
363                    break;
364               case 'count':
365                    $sort = 'count';
366                    break;
367               case 'metaType':
368                    $sort = 'meta_type';
369                    break;
370               default:
371                    $sort = 'meta_type';
372          }
373         
374          $rs->sort($sort,$order);
375         
376          $rsp = new xmlTag();
377         
378          while ($rs->fetch())
379          {
380               if (preg_match('/'.$q.'/i',$rs->meta_id)) {
381                    $metaTag = new xmlTag('meta');
382                    $metaTag->type = $rs->meta_type;
383                    $metaTag->uri = rawurlencode($rs->meta_id);
384                    $metaTag->count = $rs->count;
385                    $metaTag->percent = $rs->percent;
386                    $metaTag->roundpercent = $rs->roundpercent;
387                    $metaTag->CDATA($rs->meta_id);
388                   
389                    $rsp->insertNode($metaTag);
390               }
391          }
392         
393          return $rsp;
394     }
395
396     public static function searchCommentAuthor($core,$get)
397     {
398          $q = !empty($get['q']) ? strtoupper($get['q']) : null;
399         
400          $strReq = 'SELECT DISTINCT C.comment_author '.
401               'FROM '.$core->prefix.'comment C '.
402               'INNER JOIN '.$core->prefix.'post P on C.post_id = P.post_id '.
403               'WHERE P.blog_id = \''.$core->blog->id.'\' '.
404               'AND UPPER(comment_author) like \'%'.$core->con->escape($q).'%\''
405               ;
406          //echo $strReq;
407          $rs = $core->con->select($strReq);
408          $rsp = new xmlTag();
409         
410          while ($rs->fetch())
411          {
412               $author = new xmlTag('author');
413               $author->name=$rs->comment_author;
414               $rsp->insertNode($author);
415          }
416         
417          return $rsp;
418     }
419
420}
421?>
Note: See TracBrowser for help on using the repository browser.

Sites map