Dotclear

source: admin/services.php @ 1537:a3ac778e9132

Revision 1537:a3ac778e9132, 10.4 KB checked in by Lepeltier kévin, 11 years ago (diff)

Ticket #1406 : on encapsule new dcMedia dans des try catch pour crriger plusieurs bug.

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

Sites map