| 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 | |
| 17 | require dirname(__FILE__).'/../inc/admin/prepend.php'; |
| 18 | |
| 19 | $core->jsonrest->addFunction('getPostById',array('dcJsonRestMethods','getPostById')); |
| 20 | $core->jsonrest->addFunction('getCommentById',array('dcJsonRestMethods','getCommentById')); |
| 21 | $core->jsonrest->addFunction('quickPost',array('dcJsonRestMethods','quickPost')); |
| 22 | $core->jsonrest->addFunction('validatePostMarkup',array('dcJsonRestMethods','validatePostMarkup')); |
| 23 | $core->jsonrest->addFunction('getZipMediaContent',array('dcJsonRestMethods','getZipMediaContent')); |
| 24 | $core->jsonrest->addFunction('getMeta',array('dcJsonRestMethods','getMeta')); |
| 25 | $core->jsonrest->addFunction('delMeta',array('dcJsonRestMethods','delMeta')); |
| 26 | $core->jsonrest->addFunction('setPostMeta',array('dcJsonRestMethods','setPostMeta')); |
| 27 | $core->jsonrest->addFunction('searchMeta',array('dcJsonRestMethods','searchMeta')); |
| 28 | |
| 29 | $core->jsonrest->serve(); |
| 30 | |
| 31 | /* Common REST methods */ |
| 32 | class dcJsonRestMethods |
| 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 = array( |
| 53 | 'id' => $rs->post_id, |
| 54 | |
| 55 | 'blog_id' => $rs->blog_id, |
| 56 | 'user_id' => $rs->user_id, |
| 57 | 'cat_id' => $rs->cat_id, |
| 58 | 'post_dt' => $rs->post_dt, |
| 59 | 'post_creadt' => $rs->post_creadt, |
| 60 | 'post_upddt' => $rs->post_upddt, |
| 61 | 'post_format' => $rs->post_format, |
| 62 | 'post_url' => $rs->post_url, |
| 63 | 'post_lang' => $rs->post_lang, |
| 64 | 'post_title' => $rs->post_title, |
| 65 | 'post_excerpt' => $rs->post_excerpt, |
| 66 | 'post_excerpt_xhtml' => $rs->post_excerpt_xhtml, |
| 67 | 'post_content' => $rs->post_content, |
| 68 | 'post_content_xhtml' => $rs->post_content_xhtml, |
| 69 | 'post_notes' => $rs->post_notes, |
| 70 | 'post_status' => $rs->post_status, |
| 71 | 'post_selected' => $rs->post_selected, |
| 72 | 'post_open_comment' => $rs->post_open_comment, |
| 73 | 'post_open_tb' => $rs->post_open_tb, |
| 74 | 'nb_comment' => $rs->nb_comment, |
| 75 | 'nb_trackback' => $rs->nb_trackback, |
| 76 | 'user_name' => $rs->user_name, |
| 77 | 'user_firstname' => $rs->user_firstname, |
| 78 | 'user_displayname' => $rs->user_displayname, |
| 79 | 'user_email' => $rs->user_email, |
| 80 | 'user_url' => $rs->user_url, |
| 81 | 'cat_title' => $rs->cat_title, |
| 82 | 'cat_url' => $rs->cat_url, |
| 83 | |
| 84 | 'post_display_content' => $rs->getContent(true), |
| 85 | 'post_display_excerpt' => $rs->getExcerpt(true) |
| 86 | ); |
| 87 | |
| 88 | if (($meta = @unserialize($rs->post_meta)) !== false) |
| 89 | { |
| 90 | foreach ($meta as $K => $V) |
| 91 | { |
| 92 | foreach ($V as $v) { |
| 93 | $rsp['post_meta'][$K] = $v; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return array('post' => $rsp); |
| 99 | } |
| 100 | |
| 101 | public static function getCommentById($core,$get) |
| 102 | { |
| 103 | if (empty($get['id'])) { |
| 104 | throw new Exception('No comment ID'); |
| 105 | } |
| 106 | |
| 107 | $rs = $core->blog->getComments(array('comment_id' => (integer) $get['id'])); |
| 108 | |
| 109 | if ($rs->isEmpty()) { |
| 110 | throw new Exception('No comment for this ID'); |
| 111 | } |
| 112 | |
| 113 | $rsp = array( |
| 114 | 'id' => $rs->comment_id, |
| 115 | |
| 116 | 'comment_dt' => $rs->comment_dt, |
| 117 | 'comment_upddt' => $rs->comment_upddt, |
| 118 | 'comment_author' => $rs->comment_author, |
| 119 | 'comment_site' => $rs->comment_site, |
| 120 | 'comment_content' => $rs->comment_content, |
| 121 | 'comment_trackback' => $rs->comment_trackback, |
| 122 | 'comment_status' => $rs->comment_status, |
| 123 | 'post_title' => $rs->post_title, |
| 124 | 'post_url' => $rs->post_url, |
| 125 | 'post_id' => $rs->post_id, |
| 126 | 'post_dt' => $rs->post_dt, |
| 127 | 'user_id' => $rs->user_id, |
| 128 | |
| 129 | 'comment_display_content' => $rs->getContent(true) |
| 130 | ); |
| 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 array('post' => $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 | $post = $core->blog->getPosts(array('post_id' => $return_id)); |
| 164 | |
| 165 | $rsp = array( |
| 166 | 'id' => $return_id, |
| 167 | 'post_status' => $post->post_status, |
| 168 | 'post_url' => $post->getURL() |
| 169 | ); |
| 170 | |
| 171 | return array('post' => $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 | $v = htmlValidator::validate($excerpt_xhtml.$content_xhtml); |
| 202 | |
| 203 | $rsp = array( |
| 204 | 'valid' => $v['valid'], |
| 205 | 'errors' => $v['errors'] |
| 206 | ); |
| 207 | |
| 208 | return array('result' => $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 | $content = $core->media->getZipContent($file); |
| 231 | |
| 232 | $rsp = array(); |
| 233 | foreach ($content as $k => $v) { |
| 234 | $rsp['file'][] = $k; |
| 235 | } |
| 236 | |
| 237 | return array('result' => $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 = array(); |
| 277 | |
| 278 | while ($rs->fetch()) |
| 279 | { |
| 280 | $rsp['meta'][] = array( |
| 281 | 'type' => $rs->meta_type, |
| 282 | 'uri' => rawurlencode($rs->meta_id), |
| 283 | 'count' => $rs->count, |
| 284 | 'percent' => $rs->percent, |
| 285 | 'roundpercent' => $rs->roundpercent, |
| 286 | 'data' => $rs->meta_id |
| 287 | ); |
| 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 = array(); |
| 376 | |
| 377 | while ($rs->fetch()) |
| 378 | { |
| 379 | if (preg_match('/'.$q.'/i',$rs->meta_id)) { |
| 380 | $rsp['meta'][] = array( |
| 381 | 'type' => $rs->meta_type, |
| 382 | 'uri' => rawurlencode($rs->meta_id), |
| 383 | 'count' => $rs->count, |
| 384 | 'percent' => $rs->percent, |
| 385 | 'roundpercent' => $rs->roundpercent, |
| 386 | 'data' => $rs->meta_id |
| 387 | ); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | return $rsp; |
| 392 | } |
| 393 | } |
| 394 | ?> |
| 395 | No newline at end of file |