[ Index ]
 

Code source de Dotclear 2.0-beta6

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/inc/core/ -> class.dc.xmlrpc.php (source)

   1  <?php
   2  # ***** BEGIN LICENSE BLOCK *****
   3  # This file is part of DotClear.
   4  # Copyright (c) 2005 Olivier Meunier. All rights
   5  # reserved.
   6  #
   7  # DotClear is free software; you can redistribute it and/or modify
   8  # it under the terms of the GNU General Public License as published by
   9  # the Free Software Foundation; either version 2 of the License, or
  10  # (at your option) any later version.
  11  # 
  12  # DotClear is distributed in the hope that it will be useful,
  13  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  # GNU General Public License for more details.
  16  # 
  17  # You should have received a copy of the GNU General Public License
  18  # along with DotClear; if not, write to the Free Software
  19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20  #
  21  # ***** END LICENSE BLOCK *****
  22  
  23  class dcXmlRpc extends xmlrpcIntrospectionServer
  24  {
  25      public $core;
  26      private $blog_id;
  27      private $debug = false;
  28      private $debug_file = '/tmp/dotclear-xmlrpc.log';
  29      private $trace_args = true;
  30      private $trace_response = true;
  31      
  32  	public function __construct(&$core,$blog_id)
  33      {
  34          parent::__construct();
  35          
  36          $this->core =& $core;
  37          $this->blog_id = $blog_id;
  38          
  39          # Blogger methods
  40          $this->addCallback('blogger.newPost',array($this,'blogger_newPost'),
  41              array('string','string','string','string','string','string','integer'),
  42              'New post');
  43          
  44          $this->addCallback('blogger.editPost',array($this,'blogger_editPost'),
  45              array('boolean','string','string','string','string','string','integer'),
  46              'Edit a post');
  47          
  48          $this->addCallback('blogger.getPost',array($this,'blogger_getPost'),
  49              array('struct','string','integer','string','string'),
  50              'Return a posts by ID');
  51          
  52          $this->addCallback('blogger.deletePost',array($this,'blogger_deletePost'),
  53              array('string','string','string','string','string','integer'),
  54              'Delete a post');
  55          
  56          $this->addCallback('blogger.getRecentPosts',array($this,'blogger_getRecentPosts'),
  57              array('array','string','string','string','string','integer'),
  58              'Return a list of recent posts');
  59          
  60          $this->addCallback('blogger.getUsersBlogs',array($this,'blogger_getUserBlogs'),
  61              array('struct','string','string','string'),
  62              "Return user's blog");
  63          
  64          $this->addCallback('blogger.getUserInfo',array($this,'blogger_getUserInfo'),
  65              array('struct','string','string','string'),
  66              'Return User Info');
  67          
  68          # Metaweblog methods
  69          $this->addCallback('metaWeblog.newPost',array($this,'mw_newPost'),
  70              array('string','string','string','string','struct','boolean'),
  71              'Creates a new post, and optionnaly publishes it.');
  72          
  73          $this->addCallback('metaWeblog.editPost',array($this,'mw_editPost'),
  74              array('boolean','string','string','string','struct','boolean'),
  75              'Updates information about an existing entry');
  76          
  77          $this->addCallback('metaWeblog.getPost',array($this,'mw_getPost'),
  78              array('struct','string','string','string'),
  79              'Returns information about a specific post');
  80          
  81          $this->addCallback('metaWeblog.getRecentPosts',array($this,'mw_getRecentPosts'),
  82              array('array','string','string','string','integer'),
  83              'List of most recent posts in the system');
  84          
  85          $this->addCallback('metaWeblog.newMediaObject',array($this,'mw_newMediaObject'),
  86              array('string','string','string','string','struct'),
  87              'Download a file on the web server');
  88          
  89          # MovableType methods
  90          $this->addCallback('mt.getRecentPostTitles',array($this,'mt_getRecentPostTitles'),
  91              array('array','string','string','string','integer'),
  92              'List of most recent posts in the system');
  93          
  94          $this->addCallback('mt.getCategoryList',array($this,'mt_getCategoryList'),
  95              array('array','string','string','string'),
  96              'List of all categories defined in the weblog');
  97          
  98          $this->addCallback('mt.getPostCategories',array($this,'mt_getPostCategories'),
  99              array('array','string','string','string'),
 100              'List of all categories to which the post is assigned');
 101          
 102          $this->addCallback('mt.setPostCategories',array($this,'mt_setPostCategories'),
 103              array('boolean','string','string','string','array'),
 104              'Sets the categories for a post');
 105          
 106          $this->addCallback('mt.publishPost',array($this,'mt_publishPost'),
 107              array('boolean','string','string','string'),
 108              'Retrieve pings list for a post');
 109          
 110          $this->addCallback('mt.supportedMethods',array($this,'listMethods'),
 111              array(),'Retrieve information about the XML-RPC methods supported by the server.');
 112          
 113          $this->addCallback('mt.supportedTextFilters',array($this,'mt_supportedTextFilters'),
 114              array(),'Retrieve information about supported text filters.');
 115      }
 116      
 117  	public function serve($data=false,$encoding='UTF-8')
 118      {
 119          parent::serve(false,$encoding);
 120      }
 121      
 122  	public function call($methodname,$args)
 123      {
 124          try {
 125              $rsp = parent::call($methodname,$args);
 126              $this->debugTrace($methodname,$args,$rsp);
 127              return $rsp;
 128          } catch (Exception $e) {
 129              $this->debugTrace($methodname,$args,array($e->getMessage(),$e->getCode()));
 130              throw $e;
 131          }
 132          
 133      }
 134      
 135  	private function debugTrace($methodname,$args,$rsp)
 136      {
 137          if (!$this->debug) {
 138              return;
 139          }
 140          
 141          if (($fp = @fopen($this->debug_file,'a')) !== false)
 142          {
 143              fwrite($fp,'['.date('r').']'.' '.$methodname);
 144              
 145              if ($this->trace_args) {
 146                  fwrite($fp,"\n- args ---\n".var_export($args,1));
 147              }
 148              
 149              if ($this->trace_response) {
 150                  fwrite($fp,"\n- response ---\n".var_export($rsp,1));
 151              }
 152              fwrite($fp,"\n");
 153              fclose($fp);
 154          }
 155      }
 156      
 157      /* Internal methods
 158      --------------------------------------------------- */
 159  	private function setUser($user_id,$pwd)
 160      {
 161          if ($this->core->auth->checkUser($user_id,$pwd) !== true) {
 162              throw new Exception('Login error');
 163          }
 164          
 165          $this->core->getUserBlogs();
 166          return true;
 167      }
 168      
 169  	private function setBlog()
 170      {
 171          if (!$this->blog_id) {
 172              throw new Exception('No blog ID given.');
 173          }
 174          
 175          $this->core->setBlog($this->blog_id);
 176          
 177          if (!$this->core->blog->id) {
 178              $this->core->blog = null;
 179              throw new Exception('Blog does not exist.');
 180          }
 181          
 182          if (!$this->core->blog->settings->enable_xmlrpc ||
 183          !$this->core->auth->check('usage,contentadmin',$this->core->blog->id)) {
 184              $this->core->blog = null;
 185              throw new Exception('Not enough permissions on this blog.');
 186          }
 187          
 188          $this->core->plugins->loadModules(DC_PLUGINS_ROOT,'xmlrpc',$this->core->auth->getInfo('user_lang'));
 189          return true;
 190      }
 191      
 192  	private function getPostRS($post_id,$user,$pwd)
 193      {
 194          $this->setUser($user,$pwd);
 195          $this->setBlog();
 196          $rs = $this->core->blog->getPosts(array('post_id' => (integer) $post_id));
 197          
 198          if ($rs->isEmpty()) {
 199              throw new Exception('This entry does not exist');
 200          }
 201          
 202          return $rs;
 203      }
 204      
 205      /* Generic methods
 206      --------------------------------------------------- */
 207  	private function newPost($blog_id,$user,$pwd,$content,$struct=array(),$publish=true)
 208      {
 209          $this->setUser($user,$pwd);
 210          $this->setBlog();
 211          
 212          $title = !empty($struct['title']) ? $struct['title'] : '';
 213          $excerpt = !empty($struct['mt_excerpt']) ? $struct['mt_excerpt'] : '';
 214          $description = !empty($struct['description']) ? $struct['description'] : null;
 215          $dateCreated = !empty($struct['dateCreated']) ? $struct['dateCreated'] : null;
 216          $open_comment = isset($struct['mt_allow_comments']) ? $struct['mt_allow_comments'] : 1;
 217          $open_tb = isset($struct['mt_allow_pings']) ? $struct['mt_allow_pings'] : 1;
 218          
 219          if ($description !== null) {
 220              $content = $description;
 221          }
 222          
 223          if (!$title) {
 224              $title = text::cutString(html::clean($content),25).'...';
 225          }
 226          
 227          $excerpt_xhtml = $this->core->callFormater('xhtml',$excerpt);
 228          $content_xhtml = $this->core->callFormater('xhtml',$content);
 229          
 230          if (empty($content)) {
 231              throw new Exception('Cannot create an empty entry');
 232          }
 233          
 234          $cur = $this->core->con->openCursor($this->core->prefix.'post');
 235          
 236          $cur->user_id = $this->core->auth->userID();
 237          $cur->post_lang = $this->core->auth->getInfo('user_lang');
 238          $cur->post_title = trim($title);
 239          $cur->post_content = $content;
 240          $cur->post_excerpt = $excerpt;
 241          $cur->post_content_xhtml = $content_xhtml;
 242          $cur->post_excerpt_xhtml = $excerpt_xhtml;
 243          $cur->post_open_comment = (integer) ($open_comment == 1);
 244          $cur->post_open_tb = (integer) ($open_tb == 1);
 245          $cur->post_status = (integer) $publish;
 246          $cur->post_format = 'xhtml';
 247          
 248          if ($dateCreated && strtotime($dateCreated)) {
 249              $cur->post_dt = date('Y-m-d H:i:00',strtotime($dateCreated));
 250          }
 251          
 252          # --BEHAVIOR-- xmlrpcBeforeNewPost
 253          $this->core->callBehavior('xmlrpcBeforeNewPost',$this,$cur,$content,$struct,$publish);
 254          
 255          $post_id = $this->core->blog->addPost($cur);
 256          
 257          # --BEHAVIOR-- xmlrpcAfterNewPost
 258          $this->core->callBehavior('xmlrpcAfterNewPost',$this,$post_id,$cur,$content,$struct,$publish);
 259          
 260          return (string) $post_id;
 261      }
 262      
 263  	private function editPost($post_id,$user,$pwd,$content,$struct=array(),$publish=true)
 264      {
 265          $post_id = (integer) $post_id;
 266          
 267          $post = $this->getPostRS($post_id,$user,$pwd);
 268          
 269          $title = (!empty($struct['title'])) ? $struct['title'] : '';
 270          $excerpt = (!empty($struct['mt_excerpt'])) ? $struct['mt_excerpt'] : '';
 271          $description = (!empty($struct['description'])) ? $struct['description'] : null;
 272          $dateCreated = !empty($struct['dateCreated']) ? $struct['dateCreated'] : null;
 273          $open_comment = (isset($struct['mt_allow_comments'])) ? $struct['mt_allow_comments'] : 1;
 274          $open_tb = (isset($struct['mt_allow_pings'])) ? $struct['mt_allow_pings'] : 1;
 275          
 276          if ($description !== null) {
 277              $content = $description;
 278          }
 279          
 280          if (!$title) {
 281              $title = text::cutString(html::clean($content),25).'...';
 282          }
 283          
 284          $excerpt_xhtml = $this->core->callFormater('xhtml',$excerpt);
 285          $content_xhtml = $this->core->callFormater('xhtml',$content);
 286          
 287          if (empty($content)) {
 288              throw new Exception('Cannot create an empty entry');
 289          }
 290          
 291          $cur = $this->core->con->openCursor($this->core->prefix.'post');
 292          
 293          $cur->post_title = trim($title);
 294          $cur->post_content = $content;
 295          $cur->post_excerpt = $excerpt;
 296          $cur->post_content_xhtml = $content_xhtml;
 297          $cur->post_excerpt_xhtml = $excerpt_xhtml;
 298          $cur->post_open_comment = (integer) ($open_comment == 1);
 299          $cur->post_open_tb = (integer) ($open_tb == 1);
 300          $cur->post_status = (integer) $publish;
 301          $cur->post_format = 'xhtml';
 302          $cur->post_url = $post->post_url;
 303          
 304          if ($dateCreated && strtotime($dateCreated)) {
 305              $cur->post_dt = date('Y-m-d H:i:00',strtotime($dateCreated));
 306          } else {
 307              $cur->post_dt = $post->post_dt;
 308          }
 309          
 310          # --BEHAVIOR-- xmlrpcBeforeEditPost
 311          $this->core->callBehavior('xmlrpcBeforeEditPost',$this,$post_id,$cur,$content,$struct,$publish);
 312          
 313          $this->core->blog->updPost($post_id,$cur);
 314          
 315          # --BEHAVIOR-- xmlrpcAfterEditPost
 316          $this->core->callBehavior('xmlrpcAfterEditPost',$this,$post_id,$cur,$content,$struct,$publish);
 317          
 318          return true;
 319      }
 320      
 321  	private function getPost($post_id,$user,$pwd,$type='mw')
 322      {
 323          $post_id = (integer) $post_id;
 324          
 325          $post = $this->getPostRS($post_id,$user,$pwd);
 326          
 327          $res = array();
 328          
 329          $res['dateCreated'] = new xmlrpcDate($post->getTS());
 330          $res['userid'] = $post->user_id;
 331          $res['postid'] = $post->post_id;
 332          
 333          if ($type == 'blogger') {
 334              $res['content'] = $post->post_content_xhtml;
 335          }
 336          
 337          if ($type == 'mt' || $type == 'mw') {
 338              $res['title'] = $post->post_title;
 339          }
 340          
 341          if ($type == 'mw') {
 342              $res['description'] = $post->post_content_xhtml;
 343              $res['link'] = $res['permaLink'] = $post->getURL();
 344              $res['mt_excerpt'] = $post->post_excerpt_xhtml;
 345              $res['mt_text_more'] = '';
 346              $res['mt_allow_comments'] = (integer) $post->post_open_comment;
 347              $res['mt_allow_pings'] = (integer) $post->post_open_tb;
 348              $res['mt_convert_breaks'] = '';
 349              $res['mt_keywords'] = '';
 350          }
 351          
 352          # --BEHAVIOR-- xmlrpcGetPostInfo
 353          $this->core->callBehavior('xmlrpcGetPostInfo',$this,$type,array(&$res));
 354          
 355          return $res;
 356      }
 357      
 358  	private function deletePost($post_id,$user,$pwd)
 359      {
 360          $post_id = (integer) $post_id;
 361          
 362          $this->getPostRS($post_id,$user,$pwd);
 363          $this->core->blog->delPost($post_id);
 364          
 365          return true;
 366      }
 367      
 368  	private function getRecentPosts($blog_id,$user,$pwd,$nb_post,$type='mw')
 369      {
 370          $this->setUser($user,$pwd);
 371          $this->setBlog();
 372          
 373          $nb_post = (integer) $nb_post;
 374          
 375          if ($nb_post > 50) {
 376              throw new Exception('Cannot retrieve more than 50 entries');
 377          }
 378          
 379          $params = array();
 380          $params['limit'] = $nb_post;
 381          
 382          $posts = $this->core->blog->getPosts($params);
 383          
 384          $res = array();
 385          while ($posts->fetch())
 386          {
 387              $tres = array();
 388              
 389              $tres['dateCreated'] = new xmlrpcDate($posts->getTS());
 390              $tres['userid'] = $posts->user_id;
 391              $tres['postid'] = $posts->post_id;
 392              
 393              if ($type == 'blogger') {
 394                  $tres['content'] = $posts->post_content_xhtml;
 395              }
 396              
 397              if ($type == 'mt' || $type == 'mw') {
 398                  $tres['title'] = $posts->post_title;
 399              }
 400              
 401              if ($type == 'mw') {
 402                  $tres['description'] = $posts->post_content_xhtml;
 403                  $tres['link'] = $tres['permaLink'] = $posts->getURL();
 404                  $tres['mt_excerpt'] = $posts->post_excerpt_xhtml;
 405                  $tres['mt_text_more'] = '';
 406                  $tres['mt_allow_comments'] = (integer) $posts->post_open_comment;
 407                  $tres['mt_allow_pings'] = (integer) $posts->post_open_tb;
 408                  $tres['mt_convert_breaks'] = '';
 409                  $tres['mt_keywords'] = '';
 410              }
 411              
 412              # --BEHAVIOR-- xmlrpcGetPostInfo
 413              $this->core->callBehavior('xmlrpcGetPostInfo',$this,$type,array(&$tres));
 414              
 415              $res[] = $tres;
 416          }
 417          
 418          return $res;
 419      }
 420      
 421  	private function getUsersBlogs($user,$pwd)
 422      {
 423          $this->setUser($user,$pwd);
 424          $this->setBlog();
 425          
 426          return array(array(
 427              'url' => $this->core->blog->url,
 428              'blogid' => '1',
 429              'blogName' => $this->core->blog->name
 430          ));
 431      }
 432      
 433  	private function getUserInfo($user,$pwd)
 434      {
 435          $this->setUser($user,$pwd);
 436          
 437          return array(
 438              'userid' => $this->core->auth->userID(),
 439              'firstname' => $this->core->auth->getInfo('user_firstname'),
 440              'lastname' => $this->core->auth->getInfo('user_name'),
 441              'nickname' => $this->core->auth->getInfo('user_displayname'),
 442              'email' => $this->core->auth->getInfo('user_email'),
 443              'url' => $this->core->auth->getInfo('user_url')
 444          );
 445      }
 446      
 447  	private function getCategoryList($blog_id,$user,$pwd)
 448      {
 449          $this->setUser($user,$pwd);
 450          $this->setBlog();
 451          $rs = $this->core->blog->getCategories();
 452          
 453          $res = array();
 454          while ($rs->fetch()) {
 455              $res[] = array(
 456                  'categoryId' => (string) $rs->cat_id,
 457                  'categoryName' => $rs->cat_title
 458              );
 459          }
 460          
 461          return $res;
 462      }
 463      
 464  	private function getPostCategories($post_id,$user,$pwd)
 465      {
 466          $post_id = (integer) $post_id;
 467          
 468          $post = $this->getPostRS($post_id,$user,$pwd);
 469          
 470          return array(
 471              array(
 472              'categoryName' => $post->cat_title,
 473              'categoryId' => (string) $post->cat_id,
 474              'isPrimary' => true
 475              )
 476          );
 477      }
 478      
 479  	private function setPostCategories($post_id,$user,$pwd,$categories)
 480      {
 481          $post_id = (integer) $post_id;
 482          
 483          $post = $this->getPostRS($post_id,$user,$pwd);
 484          
 485          $cat_id = (!empty($categories[0]['categoryId'])) ? (integer) $categories[0]['categoryId'] : NULL;
 486          
 487          foreach($categories as $v)
 488          {
 489              if (isset($v['isPrimary']) && $v['isPrimary']) {
 490                  $cat_id = $v['categoryId'];
 491                  break;
 492              }
 493          }
 494          
 495          # w.bloggar sends -1 for no category.
 496          if ($cat_id == -1) {
 497              $cat_id = null;
 498          }
 499          
 500          $this->core->blog->updPostCategory($post_id,(integer) $cat_id);
 501          
 502          return true;
 503      }
 504      
 505  	private function publishPost($post_id,$user,$pwd)
 506      {
 507          $post_id = (integer) $post_id;
 508          
 509          $this->getPostRS($post_id,$user,$pwd);
 510          
 511          # --BEHAVIOR-- xmlrpcBeforePublishPost
 512          $this->core->callBehavior('xmlrpcBeforePublishPost',$this,$post_id);
 513          
 514          $this->core->blog->updPostStatus($post_id,1);
 515          
 516          # --BEHAVIOR-- xmlrpcAfterPublishPost
 517          $this->core->callBehavior('xmlrpcAfterPublishPost',$this,$post_id);
 518          
 519          return true;
 520      }
 521      
 522  	private function newMediaObject($blog_id,$user,$pwd,$file)
 523      {
 524          if (empty($file['name'])) {
 525              throw new Exception('No file name');
 526          }
 527          
 528          if (empty($file['bits'])) {
 529              throw new Exception('No file content');
 530          }
 531          
 532          $file_name = $file['name'];
 533          $file_bits = base64_decode($file['bits']);
 534          
 535          $this->setUser($user,$pwd);
 536          $this->setBlog();
 537          
 538          $media = new dcMedia($this->core);
 539          $media_id = $media->uploadBits($file_name,$file_bits);
 540          
 541          $f = $media->getFile($media_id);
 542          return array('url' => $f->file_url);
 543      }
 544      
 545      /* Blogger methods
 546      --------------------------------------------------- */
 547  	public function blogger_newPost($appkey,$blogid,$username,$password,$content,$publish)
 548      {
 549          return $this->newPost($blogid,$username,$password,$content,array(),$publish);
 550      }
 551      
 552  	public function blogger_editPost($appkey,$postid,$username,$password,$content,$publish)
 553      {
 554          return $this->editPost($postid,$username,$password,$content,array(),$publish);
 555      }
 556      
 557  	public function blogger_getPost($appkey,$postid,$username,$password)
 558      {
 559          return $this->getPost($postid,$username,$password,'blogger');
 560      }
 561      
 562  	public function blogger_deletePost($appkey,$postid,$username,$password,$publish)
 563      {
 564          return $this->deletePost($postid,$username,$password);
 565      }
 566      
 567  	public function blogger_getRecentPosts($appkey,$blogid,$username,$password,$numberOfPosts)
 568      {
 569          return $this->getRecentPosts($blogid,$username,$password,$numberOfPosts,'blogger');
 570      }
 571      
 572  	public function blogger_getUserBlogs($appkey,$username,$password)
 573      {
 574          return $this->getUsersBlogs($username,$password);
 575      }
 576      
 577  	public function blogger_getUserInfo($appkey,$username,$password)
 578      {
 579          return $this->getUserInfo($username,$password);
 580      }
 581      
 582      
 583      /* Metaweblog methods
 584      ------------------------------------------------------- */
 585  	public function mw_newPost($blogid,$username,$password,$content,$publish)
 586      {
 587          return $this->newPost($blogid,$username,$password,'',$content,$publish);
 588      }
 589      
 590  	public function mw_editPost($postid,$username,$password,$content,$publish)
 591      {
 592          return $this->editPost($postid,$username,$password,'',$content,$publish);
 593      }
 594      
 595  	public function mw_getPost($postid,$username,$password)
 596      {
 597          return $this->getPost($postid,$username,$password,'mw');
 598      }
 599      
 600  	public function mw_getRecentPosts($blogid,$username,$password,$numberOfPosts)
 601      {
 602          return $this->getRecentPosts($blogid,$username,$password,$numberOfPosts,'mw');
 603      }
 604      
 605  	public function mw_newMediaObject($blogid,$username,$password,$file)
 606      {
 607          return $this->newMediaObject($blogid,$username,$password,$file);
 608      }
 609      
 610      /* MovableType methods
 611      --------------------------------------------------- */
 612  	public function mt_getRecentPostTitles($blogid,$username,$password,$numberOfPosts)
 613      {
 614          return $this->getRecentPosts($blogid,$username,$password,$numberOfPosts,'mt');
 615      }
 616      
 617  	public function mt_getCategoryList($blogid,$username,$password)
 618      {
 619          return $this->getCategoryList($blogid,$username,$password);
 620      }
 621      
 622  	public function mt_getPostCategories($postid,$username,$password)
 623      {
 624          return $this->getPostCategories($postid,$username,$password);
 625      }
 626      
 627  	public function mt_setPostCategories($postid,$username,$password,$categories)
 628      {
 629          return $this->setPostCategories($postid,$username,$password,$categories);
 630      }
 631      
 632  	public function mt_publishPost($postid,$username,$password)
 633      {
 634          return $this->publishPost($postid,$username,$password);
 635      }
 636      
 637  	public function mt_supportedTextFilters()
 638      {
 639          return array();
 640      }
 641  }
 642  ?>


Généré le : Fri Feb 23 22:16:06 2007 par Balluche grâce à PHPXref 0.7