Dotclear

source: admin/services.php @ 848:ad18a33a3cef

Revision 848:ad18a33a3cef, 8.9 KB checked in by Dsls <dsls@…>, 13 years ago (diff)

sexy step 1 : no more comments ...

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

Sites map