Dotclear

source: admin/services.php @ 852:969647a6c35e

Revision 852:969647a6c35e, 8.2 KB checked in by Dsls <dsls@…>, 13 years ago (diff)

sexy step 3 : no more media.

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('quickPost',array('dcRestMethods','quickPost'));
21$core->rest->addFunction('validatePostMarkup',array('dcRestMethods','validatePostMarkup'));
22$core->rest->addFunction('getMeta',array('dcRestMethods','getMeta'));
23$core->rest->addFunction('delMeta',array('dcRestMethods','delMeta'));
24$core->rest->addFunction('setPostMeta',array('dcRestMethods','setPostMeta'));
25$core->rest->addFunction('searchMeta',array('dcRestMethods','searchMeta'));
26
27$core->rest->serve();
28
29/* Common REST methods */
30class dcRestMethods
31{
32     public static function getPostById($core,$get)
33     {
34          if (empty($get['id'])) {
35               throw new Exception('No post ID');
36          }
37         
38          $params = array('post_id' => (integer) $get['id']);
39         
40          if (isset($get['post_type'])) {
41               $params['post_type'] = $get['post_type'];
42          }
43         
44          $rs = $core->blog->getPosts($params);
45         
46          if ($rs->isEmpty()) {
47               throw new Exception('No post for this ID');
48          }
49         
50          $rsp = new xmlTag('post');
51          $rsp->id = $rs->post_id;
52         
53          $rsp->blog_id($rs->blog_id);
54          $rsp->user_id($rs->user_id);
55          $rsp->cat_id($rs->cat_id);
56          $rsp->post_dt($rs->post_dt);
57          $rsp->post_creadt($rs->post_creadt);
58          $rsp->post_upddt($rs->post_upddt);
59          $rsp->post_format($rs->post_format);
60          $rsp->post_url($rs->post_url);
61          $rsp->post_lang($rs->post_lang);
62          $rsp->post_title($rs->post_title);
63          $rsp->post_excerpt($rs->post_excerpt);
64          $rsp->post_excerpt_xhtml($rs->post_excerpt_xhtml);
65          $rsp->post_content($rs->post_content);
66          $rsp->post_content_xhtml($rs->post_content_xhtml);
67          $rsp->post_notes($rs->post_notes);
68          $rsp->post_status($rs->post_status);
69          $rsp->post_selected($rs->post_selected);
70          $rsp->user_name($rs->user_name);
71          $rsp->user_firstname($rs->user_firstname);
72          $rsp->user_displayname($rs->user_displayname);
73          $rsp->user_email($rs->user_email);
74          $rsp->user_url($rs->user_url);
75          $rsp->cat_title($rs->cat_title);
76          $rsp->cat_url($rs->cat_url);
77         
78          $rsp->post_display_content($rs->getContent(true));
79          $rsp->post_display_excerpt($rs->getExcerpt(true));
80         
81          $metaTag = new xmlTag('meta');
82          if (($meta = @unserialize($rs->post_meta)) !== false)
83          {
84               foreach ($meta as $K => $V)
85               {
86                    foreach ($V as $v) {
87                         $metaTag->$K($v);
88                    }
89               }
90          }
91          $rsp->post_meta($metaTag);
92         
93          return $rsp;
94     }
95     
96     
97     public static function quickPost($core,$get,$post)
98     {
99          $cur = $core->con->openCursor($core->prefix.'post');
100         
101          $cur->post_title = !empty($post['post_title']) ? $post['post_title'] : '';
102          $cur->user_id = $core->auth->userID();
103          $cur->post_content = !empty($post['post_content']) ? $post['post_content'] : '';
104          $cur->cat_id = !empty($post['cat_id']) ? (integer) $post['cat_id'] : null;
105          $cur->post_format = !empty($post['post_format']) ? $post['post_format'] : 'xhtml';
106          $cur->post_lang = !empty($post['post_lang']) ? $post['post_lang'] : '';
107          $cur->post_status = !empty($post['post_status']) ? (integer) $post['post_status'] : 0;
108         
109          # --BEHAVIOR-- adminBeforePostCreate
110          $core->callBehavior('adminBeforePostCreate',$cur);
111         
112          $return_id = $core->blog->addPost($cur);
113         
114          # --BEHAVIOR-- adminAfterPostCreate
115          $core->callBehavior('adminAfterPostCreate',$cur,$return_id);
116         
117          $rsp = new xmlTag('post');
118          $rsp->id = $return_id;
119         
120          $post = $core->blog->getPosts(array('post_id' => $return_id));
121         
122          $rsp->post_status = $post->post_status;
123          $rsp->post_url = $post->getURL();
124          return $rsp;
125     }
126     
127     public static function validatePostMarkup($core,$get,$post)
128     {
129          if (!isset($post['excerpt'])) {
130               throw new Exception('No entry excerpt');
131          }
132         
133          if (!isset($post['content'])) {
134               throw new Exception('No entry content');
135          }
136         
137          if (empty($post['format'])) {
138               throw new Exception('No entry format');
139          }
140         
141          if (!isset($post['lang'])) {
142               throw new Exception('No entry lang');
143          }
144         
145          $excerpt = $post['excerpt'];
146          $excerpt_xhtml = '';
147          $content = $post['content'];
148          $content_xhtml = '';
149          $format = $post['format'];
150          $lang = $post['lang'];
151         
152          $core->blog->setPostContent(0,$format,$lang,$excerpt,$excerpt_xhtml,$content,$content_xhtml);
153         
154          $rsp = new xmlTag('result');
155         
156          $v = htmlValidator::validate($excerpt_xhtml.$content_xhtml);
157         
158          $rsp->valid($v['valid']);
159          $rsp->errors($v['errors']);
160         
161          return $rsp;
162     }
163     
164     public static function getMeta($core,$get)
165     {
166          $postid = !empty($get['postId']) ? $get['postId'] : null;
167          $limit = !empty($get['limit']) ? $get['limit'] : null;
168          $metaId = !empty($get['metaId']) ? $get['metaId'] : null;
169          $metaType = !empty($get['metaType']) ? $get['metaType'] : null;
170         
171          $sortby = !empty($get['sortby']) ? $get['sortby'] : 'meta_type,asc';
172         
173          $rs = $core->meta->getMetadata(array(
174               'meta_type' => $metaType,
175               'limit' => $limit,
176               'meta_id' => $metaId,
177               'post_id' => $postid));
178          $rs = $core->meta->computeMetaStats($rs);
179         
180          $sortby = explode(',',$sortby);
181          $sort = $sortby[0];
182          $order = isset($sortby[1]) ? $sortby[1] : 'asc';
183         
184          switch ($sort) {
185               case 'metaId':
186                    $sort = 'meta_id_lower';
187                    break;
188               case 'count':
189                    $sort = 'count';
190                    break;
191               case 'metaType':
192                    $sort = 'meta_type';
193                    break;
194               default:
195                    $sort = 'meta_type';
196          }
197         
198          $rs->sort($sort,$order);
199         
200          $rsp = new xmlTag();
201         
202          while ($rs->fetch())
203          {
204               $metaTag = new xmlTag('meta');
205               $metaTag->type = $rs->meta_type;
206               $metaTag->uri = rawurlencode($rs->meta_id);
207               $metaTag->count = $rs->count;
208               $metaTag->percent = $rs->percent;
209               $metaTag->roundpercent = $rs->roundpercent;
210               $metaTag->CDATA($rs->meta_id);
211               
212               $rsp->insertNode($metaTag);
213          }
214         
215          return $rsp;
216     }
217     
218     public static function setPostMeta($core,$get,$post)
219     {
220          if (empty($post['postId'])) {
221               throw new Exception('No post ID');
222          }
223         
224          if (empty($post['meta']) && $post['meta'] != '0') {
225               throw new Exception('No meta');
226          }
227         
228          if (empty($post['metaType'])) {
229               throw new Exception('No meta type');
230          }
231         
232          # Get previous meta for post
233          $post_meta = $core->meta->getMetadata(array(
234               'meta_type' => $post['metaType'],
235               'post_id' => $post['postId']));
236          $pm = array();
237          while ($post_meta->fetch()) {
238               $pm[] = $post_meta->meta_id;
239          }
240         
241          foreach ($core->meta->splitMetaValues($post['meta']) as $m)
242          {
243               if (!in_array($m,$pm)) {
244                    $core->meta->setPostMeta($post['postId'],$post['metaType'],$m);
245               }
246          }
247         
248          return true;
249     }
250     
251     public static function delMeta($core,$get,$post)
252     {
253          if (empty($post['postId'])) {
254               throw new Exception('No post ID');
255          }
256         
257          if (empty($post['metaId']) && $post['metaId'] != '0') {
258               throw new Exception('No meta ID');
259          }
260         
261          if (empty($post['metaType'])) {
262               throw new Exception('No meta type');
263          }
264         
265          $core->meta->delPostMeta($post['postId'],$post['metaType'],$post['metaId']);
266         
267          return true;
268     }
269     
270     public static function searchMeta($core,$get)
271     {
272          $q = !empty($get['q']) ? $get['q'] : null;
273          $metaType = !empty($get['metaType']) ? $get['metaType'] : null;
274         
275          $sortby = !empty($get['sortby']) ? $get['sortby'] : 'meta_type,asc';
276         
277          $rs = $core->meta->getMetadata(array('meta_type' => $metaType));
278          $rs = $core->meta->computeMetaStats($rs);
279         
280          $sortby = explode(',',$sortby);
281          $sort = $sortby[0];
282          $order = isset($sortby[1]) ? $sortby[1] : 'asc';
283         
284          switch ($sort) {
285               case 'metaId':
286                    $sort = 'meta_id_lower';
287                    break;
288               case 'count':
289                    $sort = 'count';
290                    break;
291               case 'metaType':
292                    $sort = 'meta_type';
293                    break;
294               default:
295                    $sort = 'meta_type';
296          }
297         
298          $rs->sort($sort,$order);
299         
300          $rsp = new xmlTag();
301         
302          while ($rs->fetch())
303          {
304               if (preg_match('/'.$q.'/i',$rs->meta_id)) {
305                    $metaTag = new xmlTag('meta');
306                    $metaTag->type = $rs->meta_type;
307                    $metaTag->uri = rawurlencode($rs->meta_id);
308                    $metaTag->count = $rs->count;
309                    $metaTag->percent = $rs->percent;
310                    $metaTag->roundpercent = $rs->roundpercent;
311                    $metaTag->CDATA($rs->meta_id);
312                   
313                    $rsp->insertNode($metaTag);
314               }
315          }
316         
317          return $rsp;
318     }
319}
320?>
Note: See TracBrowser for help on using the repository browser.

Sites map