[0] | 1 | <?php |
---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
| 3 | # |
---|
| 4 | # This file is part of Dotclear 2. |
---|
| 5 | # |
---|
[270] | 6 | # Copyright (c) 2003-2011 Olivier Meunier & Association Dotclear |
---|
[0] | 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 | |
---|
| 17 | require 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 */ |
---|
| 32 | class 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); |
---|
[323] | 135 | $rsp->comment_spam_disp(dcAntispam::statusMessage($rs)); |
---|
[0] | 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 | $core->media = new dcMedia($core); |
---|
| 223 | $file = $core->media->getFile($id); |
---|
| 224 | |
---|
| 225 | if ($file === null || $file->type != 'application/zip' || !$file->editable) { |
---|
| 226 | throw new Exception('Not a valid file'); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | $rsp = new xmlTag('result'); |
---|
| 230 | $content = $core->media->getZipContent($file); |
---|
| 231 | |
---|
| 232 | foreach ($content as $k => $v) { |
---|
| 233 | $rsp->file($k); |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | return $rsp; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | public static function getMeta($core,$get) |
---|
| 240 | { |
---|
| 241 | $postid = !empty($get['postId']) ? $get['postId'] : null; |
---|
| 242 | $limit = !empty($get['limit']) ? $get['limit'] : null; |
---|
| 243 | $metaId = !empty($get['metaId']) ? $get['metaId'] : null; |
---|
| 244 | $metaType = !empty($get['metaType']) ? $get['metaType'] : null; |
---|
| 245 | |
---|
| 246 | $sortby = !empty($get['sortby']) ? $get['sortby'] : 'meta_type,asc'; |
---|
| 247 | |
---|
| 248 | $rs = $core->meta->getMetadata(array( |
---|
| 249 | 'meta_type' => $metaType, |
---|
| 250 | 'limit' => $limit, |
---|
| 251 | 'meta_id' => $metaId, |
---|
| 252 | 'post_id' => $postid)); |
---|
| 253 | $rs = $core->meta->computeMetaStats($rs); |
---|
| 254 | |
---|
| 255 | $sortby = explode(',',$sortby); |
---|
| 256 | $sort = $sortby[0]; |
---|
| 257 | $order = isset($sortby[1]) ? $sortby[1] : 'asc'; |
---|
| 258 | |
---|
| 259 | switch ($sort) { |
---|
| 260 | case 'metaId': |
---|
| 261 | $sort = 'meta_id_lower'; |
---|
| 262 | break; |
---|
| 263 | case 'count': |
---|
| 264 | $sort = 'count'; |
---|
| 265 | break; |
---|
| 266 | case 'metaType': |
---|
| 267 | $sort = 'meta_type'; |
---|
| 268 | break; |
---|
| 269 | default: |
---|
| 270 | $sort = 'meta_type'; |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | $rs->sort($sort,$order); |
---|
| 274 | |
---|
| 275 | $rsp = new xmlTag(); |
---|
| 276 | |
---|
| 277 | while ($rs->fetch()) |
---|
| 278 | { |
---|
| 279 | $metaTag = new xmlTag('meta'); |
---|
| 280 | $metaTag->type = $rs->meta_type; |
---|
| 281 | $metaTag->uri = rawurlencode($rs->meta_id); |
---|
| 282 | $metaTag->count = $rs->count; |
---|
| 283 | $metaTag->percent = $rs->percent; |
---|
| 284 | $metaTag->roundpercent = $rs->roundpercent; |
---|
| 285 | $metaTag->CDATA($rs->meta_id); |
---|
| 286 | |
---|
| 287 | $rsp->insertNode($metaTag); |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | return $rsp; |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | public static function setPostMeta($core,$get,$post) |
---|
| 294 | { |
---|
| 295 | if (empty($post['postId'])) { |
---|
| 296 | throw new Exception('No post ID'); |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | if (empty($post['meta']) && $post['meta'] != '0') { |
---|
| 300 | throw new Exception('No meta'); |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | if (empty($post['metaType'])) { |
---|
| 304 | throw new Exception('No meta type'); |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | # Get previous meta for post |
---|
| 308 | $post_meta = $core->meta->getMetadata(array( |
---|
| 309 | 'meta_type' => $post['metaType'], |
---|
| 310 | 'post_id' => $post['postId'])); |
---|
| 311 | $pm = array(); |
---|
| 312 | while ($post_meta->fetch()) { |
---|
| 313 | $pm[] = $post_meta->meta_id; |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | foreach ($core->meta->splitMetaValues($post['meta']) as $m) |
---|
| 317 | { |
---|
| 318 | if (!in_array($m,$pm)) { |
---|
| 319 | $core->meta->setPostMeta($post['postId'],$post['metaType'],$m); |
---|
| 320 | } |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | return true; |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | public static function delMeta($core,$get,$post) |
---|
| 327 | { |
---|
| 328 | if (empty($post['postId'])) { |
---|
| 329 | throw new Exception('No post ID'); |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | if (empty($post['metaId']) && $post['metaId'] != '0') { |
---|
| 333 | throw new Exception('No meta ID'); |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | if (empty($post['metaType'])) { |
---|
| 337 | throw new Exception('No meta type'); |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | $core->meta->delPostMeta($post['postId'],$post['metaType'],$post['metaId']); |
---|
| 341 | |
---|
| 342 | return true; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | public static function searchMeta($core,$get) |
---|
| 346 | { |
---|
| 347 | $q = !empty($get['q']) ? $get['q'] : null; |
---|
| 348 | $metaType = !empty($get['metaType']) ? $get['metaType'] : null; |
---|
| 349 | |
---|
| 350 | $sortby = !empty($get['sortby']) ? $get['sortby'] : 'meta_type,asc'; |
---|
| 351 | |
---|
| 352 | $rs = $core->meta->getMetadata(array('meta_type' => $metaType)); |
---|
| 353 | $rs = $core->meta->computeMetaStats($rs); |
---|
| 354 | |
---|
| 355 | $sortby = explode(',',$sortby); |
---|
| 356 | $sort = $sortby[0]; |
---|
| 357 | $order = isset($sortby[1]) ? $sortby[1] : 'asc'; |
---|
| 358 | |
---|
| 359 | switch ($sort) { |
---|
| 360 | case 'metaId': |
---|
| 361 | $sort = 'meta_id_lower'; |
---|
| 362 | break; |
---|
| 363 | case 'count': |
---|
| 364 | $sort = 'count'; |
---|
| 365 | break; |
---|
| 366 | case 'metaType': |
---|
| 367 | $sort = 'meta_type'; |
---|
| 368 | break; |
---|
| 369 | default: |
---|
| 370 | $sort = 'meta_type'; |
---|
| 371 | } |
---|
| 372 | |
---|
| 373 | $rs->sort($sort,$order); |
---|
| 374 | |
---|
| 375 | $rsp = new xmlTag(); |
---|
| 376 | |
---|
| 377 | while ($rs->fetch()) |
---|
| 378 | { |
---|
| 379 | if (preg_match('/'.$q.'/i',$rs->meta_id)) { |
---|
| 380 | $metaTag = new xmlTag('meta'); |
---|
| 381 | $metaTag->type = $rs->meta_type; |
---|
| 382 | $metaTag->uri = rawurlencode($rs->meta_id); |
---|
| 383 | $metaTag->count = $rs->count; |
---|
| 384 | $metaTag->percent = $rs->percent; |
---|
| 385 | $metaTag->roundpercent = $rs->roundpercent; |
---|
| 386 | $metaTag->CDATA($rs->meta_id); |
---|
| 387 | |
---|
| 388 | $rsp->insertNode($metaTag); |
---|
| 389 | } |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | return $rsp; |
---|
| 393 | } |
---|
| 394 | } |
---|
| 395 | ?> |
---|