Dotclear

Ticket #1197: JSON_REST_service_step1_PHP_part.patch

File JSON_REST_service_step1_PHP_part.patch, 16.8 KB (added by JcDenis, 13 years ago)

Premier essai de la partie PHP du service REST JSON

  • new file admin/services_json.php

    # HG changeset patch
    # User JcDenis
    # Date 1353254036 -3600
    # Node ID 634481f5e7376d531c62f204880baefbf8b20f88
    # Parent  4a5d59418a795bf5170892851d3c019a9d208be7
    JSON REST service. Step 1. PHP part
    
    diff -r 4a5d59418a79 -r 634481f5e737 admin/services_json.php
    - +  
     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->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 */ 
     32class 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 
  • inc/core/class.dc.core.php

    diff -r 4a5d59418a79 -r 634481f5e737 inc/core/class.dc.core.php
    a b  
    3737     public $media;      ///< <b>dcMedia</b>           dcMedia object 
    3838     public $postmedia;  ///< <b>dcPostMedia</b>       dcPostMedia object 
    3939     public $rest;       ///< <b>dcRestServer</b> dcRestServer object 
     40     public $jsonrest;   ///< <b>dcJsonRestServer</b>  dcJsonRestServer object 
    4041     public $log;        ///< <b>dcLog</b>             dcLog object 
    4142      
    4243     private $versions = null; 
     
    8788          $this->plugins = new dcModules($this); 
    8889           
    8990          $this->rest = new dcRestServer($this); 
     91          $this->jsonrest = new dcJsonRestServer($this); 
    9092           
    9193          $this->meta = new dcMeta($this); 
    9294           
  • new file inc/core/class.dc.jsonrest.php

    diff -r 4a5d59418a79 -r 634481f5e737 inc/core/class.dc.jsonrest.php
    - +  
     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 ----------------------------------------- 
     12if (!defined('DC_RC_PATH')) { return; } 
     13 
     14/** 
     15@ingroup DC_CORE 
     16@brief Dotclear JSON REST server extension 
     17 
     18This class handles dcCore instance in each rest method call. 
     19Instance of this class is provided by dcCore $jsonrest. 
     20*/ 
     21class dcJsonRestServer 
     22{ 
     23     public $core;       ///< dcCore instance 
     24     public $rsp;        ///< Server response (array) 
     25     public $functions = array();  ///< array of Server's functions 
     26      
     27     /** 
     28     Object constructor. 
     29      
     30     @param    core      <b>dcCore</b>       dcCore instance 
     31     */ 
     32     public function __construct($core) 
     33     { 
     34          $this->core =& $core; 
     35          $this->rsp = new jsonContent(); 
     36     } 
     37      
     38     /** 
     39     This adds a new function to the server. <var>$callback</var> should be 
     40     a valid PHP callback. Callback function takes three arguments:  
     41     dcCore instance and GET values and POST values. 
     42      
     43     @param string  $name          Function name 
     44     @param callback     $callback      Callback function 
     45     */ 
     46     public function addFunction($name, $callback) 
     47     { 
     48          if (is_callable($callback)) { 
     49               $this->functions[$name] = $callback; 
     50          } 
     51     } 
     52      
     53     /** 
     54     Rest method call. 
     55      
     56     @param    name      <b>string</b>       Method name 
     57     @param    get       <b>array</b>        GET parameters copy 
     58     @param    post      <b>array</b>        POST parameters copy 
     59     @return   <b>mixed</b>   Rest method result 
     60     */ 
     61     protected function callFunction($name,$get,$post) 
     62     { 
     63          if (isset($this->functions[$name])) { 
     64               return call_user_func($this->functions[$name],$this->core,$get,$post); 
     65          } 
     66     } 
     67      
     68     /** 
     69     This method creates the main server. 
     70      
     71     @param string  $encoding      Server charset 
     72     */ 
     73     public function serve() 
     74     { 
     75          $get = array(); 
     76          if (isset($_GET)) { 
     77               $get = $_GET; 
     78          } 
     79           
     80          $post = array(); 
     81          if (isset($_POST)) { 
     82               $post = $_POST; 
     83          } 
     84           
     85          if (!isset($_REQUEST['f'])) { 
     86               $this->rsp->setCode(2); 
     87               $this->rsp->setMessage('No function given'); 
     88               $this->getJSON(); 
     89               return false; 
     90          } 
     91           
     92          if (!isset($this->functions[$_REQUEST['f']])) { 
     93               $this->rsp->setCode(3); 
     94               $this->rsp->setMessage('Function does not exist'); 
     95               $this->getJSON(); 
     96               return false; 
     97          } 
     98           
     99          try { 
     100               $res = $this->callFunction($_REQUEST['f'],$get,$post); 
     101          } catch (Exception $e) { 
     102               $this->rsp->setCode(4); 
     103               $this->rsp->setMessage($e->getMessage()); 
     104               $this->getJSON(); 
     105               return false; 
     106          } 
     107           
     108          $this->rsp->setCode(0); 
     109          $this->rsp->setMessage("No error"); 
     110          $this->rsp->setData($res); 
     111          $this->getJSON(); 
     112           
     113          return true; 
     114     } 
     115      
     116     private function getJSON() 
     117     { 
     118          if (!function_exists('json_encode')) { 
     119               header('Content-Type: text/plain; charset=UTF-8'); 
     120               http::head(500); 
     121               exit(); 
     122          } 
     123           
     124          header('Content-Type: text/javascript; charset=UTF-8'); 
     125          http::head($this->rsp->getStatus() == 'ok' ? 200 : 400); 
     126           
     127          echo $this->rsp->toJSON(); 
     128     } 
     129} 
     130 
     131class jsonContent 
     132{ 
     133     public $code; 
     134     public $message; 
     135     public $data; 
     136      
     137     public function __construct() 
     138     { 
     139          $this->code = 1; 
     140          $this->message = 'no data'; 
     141          $this->data = array(); 
     142     } 
     143      
     144     public function setCode($code) 
     145     { 
     146          $this->code = abs((integer) $code); 
     147     } 
     148      
     149     public function setMessage($message) 
     150     { 
     151          $this->message = (string) $message; 
     152     } 
     153      
     154     public function setData($data) 
     155     { 
     156          $this->data = $data; 
     157     } 
     158      
     159     public function getStatus() 
     160     { 
     161          return $this->code > 0 ? 'failed' : 'ok'; 
     162     } 
     163      
     164     /** 
     165     Return a json string like this: 
     166     {"error":{"status":"failed","code":2,"message":"No function given"},"data":[]} 
     167     */ 
     168     public function toJSON() 
     169     { 
     170          $res = array( 
     171               'error' => array( 
     172                    'status' => $this->getStatus(), 
     173                    'code' => $this->code, 
     174                    'message' => $this->message 
     175               ), 
     176               'data' => $this->data 
     177          ); 
     178           
     179          return json_encode($res); 
     180     } 
     181} 
     182?> 
     183 No newline at end of file 
  • inc/prepend.php

    diff -r 4a5d59418a79 -r 634481f5e737 inc/prepend.php
    a b  
    3636$__autoload['dcModules']                = dirname(__FILE__).'/core/class.dc.modules.php'; 
    3737$__autoload['dcThemes']                 = dirname(__FILE__).'/core/class.dc.themes.php'; 
    3838$__autoload['dcRestServer']             = dirname(__FILE__).'/core/class.dc.rest.php'; 
     39$__autoload['dcJsonRestServer']              = dirname(__FILE__).'/core/class.dc.jsonrest.php'; 
    3940$__autoload['dcNamespace']              = dirname(__FILE__).'/core/class.dc.namespace.php'; 
    4041$__autoload['dcSettings']               = dirname(__FILE__).'/core/class.dc.settings.php'; 
    4142$__autoload['dcTrackback']              = dirname(__FILE__).'/core/class.dc.trackback.php'; 

Sites map