class.dc.xmlrpc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. <?php
  2. # ***** BEGIN LICENSE BLOCK *****
  3. # This file is part of DotClear.
  4. # Copyright (c) 2004 Olivier Meunier and contributors. 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. require dirname(__FILE__).'/../incutio/class.IXR_Library.php';
  23. class DotClearRPC extends IXR_IntrospectionServer
  24. {
  25. var $blog;
  26. var $blog_url;
  27. var $blog_name;
  28. var $debug = false;
  29. var $raw_debug = false;
  30. var $debug_file ='/tmp/dotclear-xmlrpc.log';
  31. function DotClearRPC(&$blog)
  32. {
  33. $this->IXR_IntrospectionServer();
  34. $this->blog = $blog;
  35. $this->blog_url = '';
  36. $this->blog_name = '';
  37. # Méthodes blogger
  38. $this->addCallback('blogger.newPost','this:blogger_newPost',
  39. array('string','string','string','string','string','string','integer'),
  40. 'New post');
  41. $this->addCallback('blogger.editPost','this:blogger_editPost',
  42. array('boolean','string','string','string','string','string','integer'),
  43. 'Edit a post');
  44. $this->addCallback('blogger.deletePost','this:blogger_deletePost',
  45. array('string','string','string','string','string','integer'),
  46. 'Delete a post');
  47. $this->addCallback('blogger.getRecentPosts','this:blogger_getRecentPosts',
  48. array('array','string','string','string','string','integer'),
  49. 'Return a list of recent posts');
  50. $this->addCallback('blogger.getPost','this:blogger_getPost',
  51. array('struct','string','integer','string','string'),
  52. 'Return a posts by ID');
  53. $this->addCallback('blogger.getUsersBlogs','this:blogger_getUserBlogs',
  54. array('struct','string','string','string'),
  55. 'Return user\'s blog');
  56. $this->addCallback('blogger.getUserInfo','this:blogger_getUserInfo',
  57. array('struct','string','string','string'),
  58. 'Return User Info');
  59. # Méthode metaWeblog
  60. $this->addCallback('metaWeblog.newPost','this:mw_newPost',
  61. array('string','string','string','string','struct','boolean'),
  62. 'Creates a new post, and optionnaly publishes it.');
  63. $this->addCallback('metaWeblog.getRecentPosts','this:mw_getRecentPosts',
  64. array('array','string','string','string','integer'),
  65. 'List of most recent posts in the system');
  66. $this->addCallback('metaWeblog.getPost','this:mw_getPost',
  67. array('struct','string','string','string'),
  68. 'Returns information about a specific post');
  69. $this->addCallback('metaWeblog.editPost','this:mw_editPost',
  70. array('boolean','string','string','string','struct','boolean'),
  71. 'Updates information about an existing entry');
  72. # Méthodes mt
  73. $this->addCallback('mt.getRecentPostTitles','this:mt_getRecentPostTitles',
  74. array('array','string','string','string','integer'),
  75. 'List of most recent posts in the system');
  76. $this->addCallback('mt.getCategoryList','this:mt_getCategoryList',
  77. array('array','string','string','string'),
  78. 'List of all categories defined in the weblog');
  79. $this->addCallback('mt.getPostCategories','this:mt_getPostCategories',
  80. array('array','string','string','string'),
  81. 'List of all categories to which the post is assigned');
  82. $this->addCallback('mt.setPostCategories','this:mt_setPostCategories',
  83. array('boolean','string','string','string','array'),
  84. 'Sets the categories for a post');
  85. $this->addCallback('mt.supportedMethods','this:listMethods',
  86. array(),'Retrieve information about the XML-RPC methods supported by the server.');
  87. }
  88. function serve($encoding)
  89. {
  90. if ($this->raw_debug && !empty($GLOBALS['HTTP_RAW_POST_DATA'])) {
  91. $this->__debugTrace('XML/RPC',$GLOBALS['HTTP_RAW_POST_DATA']);
  92. }
  93. parent::serve(false,$encoding);
  94. }
  95. function setBlogName($name)
  96. {
  97. $this->blog_name = $name;
  98. }
  99. function setBlogUrl($url)
  100. {
  101. $this->blog_url = $url;
  102. }
  103. function __debugTrace($function,$msg)
  104. {
  105. if ($this->debug)
  106. {
  107. if (($fp = @fopen($this->debug_file,'a')) !== false)
  108. {
  109. if (is_array($msg)) {
  110. $msg = implode(' : ',$msg);
  111. }
  112. fwrite($fp,'['.date('r').']'.' '.$function.' - '.$msg."\n");
  113. fclose($fp);
  114. }
  115. }
  116. }
  117. /* Méthodes blogger
  118. --------------------------------------------------- */
  119. function blogger_getUserInfo($args)
  120. {
  121. $this->__debugTrace('blogger.getUserInfo',$args);
  122. return $this->_getUserInfo($args[1],$args[2]);
  123. }
  124. function blogger_getUserBlogs($args)
  125. {
  126. $this->__debugTrace('blogger.getUsersBlogs',$args);
  127. return $this->_getUserBlogs($args[1],$args[2]);
  128. }
  129. function blogger_getPost($args)
  130. {
  131. $this->__debugTrace('blogger.getPost',$args);
  132. return $this->_getPost($args[2],$args[3],$args[1],'blogger');
  133. }
  134. function blogger_getRecentPosts($args)
  135. {
  136. $this->__debugTrace('blogger.getRecentPosts',$args);
  137. return $this->_getRecentPosts($args[2],$args[3],$args[4],'blogger');
  138. }
  139. function blogger_newPost($args)
  140. {
  141. $this->__debugTrace('blogger.newPost',$args);
  142. return $this->_newPost($args[2],$args[3],$args[4],array(),$args[5]);
  143. }
  144. function blogger_editPost($args)
  145. {
  146. $this->__debugTrace('blogger.editPost',$args);
  147. return $this->_editPost($args[2],$args[3],$args[1],$args[4],array(),$args[5]);
  148. }
  149. function blogger_deletePost($args)
  150. {
  151. $this->__debugTrace('blogger.deletePost',$args);
  152. return $this->_deletePost($args[2],$args[3],$args[1],1);
  153. }
  154. /* Méthode MT
  155. --------------------------------------------------- */
  156. function mt_getRecentPostTitles($args)
  157. {
  158. $this->__debugTrace('mt.getRecentPostTitles',$args);
  159. return $this->_getRecentPosts($args[1],$args[2],$args[3],'mt');
  160. }
  161. function mt_getCategoryList($args)
  162. {
  163. $this->__debugTrace('mt.getCategoryList',$args);
  164. return $this->_getCategories($args[1],$args[2]);
  165. }
  166. function mt_getPostCategories($args)
  167. {
  168. $this->__debugTrace('mt.getCategoryList',$args);
  169. return $this->_getPostCat($args[1],$args[2],$args[0]);
  170. }
  171. function mt_setPostCategories($args)
  172. {
  173. $this->__debugTrace('mt.setPostCategories',$args);
  174. return $this->_setPostCat($args[1],$args[2],$args[0],$args[3]);
  175. }
  176. /* Méthode meatWeblog
  177. --------------------------------------------------- */
  178. function mw_newPost($args)
  179. {
  180. $this->__debugTrace('metaWeblog.newPost',$args);
  181. return $this->_newPost($args[1],$args[2],'',$args[3],$args[4]);
  182. }
  183. function mw_getRecentPosts($args)
  184. {
  185. $this->__debugTrace('metaWeblog.getRecentPosts',$args);
  186. return $this->_getRecentPosts($args[1],$args[2],$args[3],'mw');
  187. }
  188. function mw_getPost($args)
  189. {
  190. $this->__debugTrace('metaWeblog.getPost',$args);
  191. return $this->_getPost($args[1],$args[2],$args[0],'mw');
  192. }
  193. function mw_editPost($args)
  194. {
  195. $this->__debugTrace('metaWeblog.editPost',$args);
  196. return $this->_editPost($args[1],$args[2],$args[0],'',$args[3],$args[4]);
  197. }
  198. /* Fonctions génériques
  199. --------------------------------------------------- */
  200. function _getUserInfo($user,$pwd)
  201. {
  202. if ($this->blog->checkUser($user,$pwd,1) === false) {
  203. return new IXR_Error(801,'Login error : '.$user);
  204. }
  205. if (($rs = $this->blog->getUser($user)) === false)
  206. {
  207. return new IXR_Error(801,'Login error : '.$user);
  208. }
  209. else
  210. {
  211. return array(
  212. 'userid' => $rs->f('user_id'),
  213. 'firstname' => $rs->f('user_prenom'),
  214. 'lastname' => $rs->f('user_nom'),
  215. 'nickname' => $rs->f('user_pseudo'),
  216. 'email' => $rs->f('user_email'),
  217. 'url' => $this->blog_url
  218. );
  219. }
  220. }
  221. function _getUserBlogs($user,$pwd)
  222. {
  223. if ($this->blog->checkUser($user,$pwd,1) === false)
  224. {
  225. return new IXR_Error(801,'Erreur de login');
  226. }
  227. else
  228. {
  229. return array(array(
  230. 'url' => $this->blog_url,
  231. 'blogid' => '1',
  232. 'blogName' => $this->blog_name
  233. ));
  234. }
  235. }
  236. function _getCategories($user,$pwd)
  237. {
  238. if ($this->blog->checkUser($user,$pwd,1) === false) {
  239. return new IXR_Error(801,'Erreur de login');
  240. }
  241. if (($rs = $this->blog->getCat()) === false)
  242. {
  243. return new IXR_Error(802,'Erreur SQL');
  244. }
  245. else
  246. {
  247. $res = array();
  248. while (!$rs->EOF())
  249. {
  250. $res[] = array(
  251. 'categoryId' => $rs->f('cat_id'),
  252. 'categoryName' => $rs->f('cat_libelle')
  253. );
  254. $rs->moveNext();
  255. }
  256. return $res;
  257. }
  258. }
  259. function _getPostCat($user,$pwd,$postid)
  260. {
  261. if ($this->blog->checkUser($user,$pwd,1) === false) {
  262. return new IXR_Error(801,'Erreur de login');
  263. }
  264. if(empty($postid)) {
  265. return new IXR_Error(806,'Pas de billet pour cet ID');
  266. }
  267. $rs = $this->blog->getPostByID($postid);
  268. if($rs->isEmpty()) {
  269. return new IXR_Error(806,'Pas de billet pour cet ID');
  270. }
  271. $res[0]['categoryName'] = $rs->f('cat_libelle');
  272. $res[0]['categoryId'] = $rs->f('cat_id');
  273. $res[0]['isPrimary'] = true;
  274. return $res;
  275. }
  276. function _getPost($user,$pwd,$postid,$type='blogger')
  277. {
  278. if ($this->blog->checkUser($user,$pwd,1) === false) {
  279. return new IXR_Error(801,'Erreur de login');
  280. }
  281. if(empty($postid)) {
  282. return new IXR_Error(806,'Pas de billet pour cet ID');
  283. }
  284. $rs = $this->blog->getPostByID($postid);
  285. if($rs->isEmpty()) {
  286. return new IXR_Error(806,'Pas de billet pour cet ID');
  287. }
  288. $content = $rs->f('post_content');
  289. $chapo = $rs->f('post_chapo');
  290. $titre = $rs->f('post_titre');
  291. $post_ts = $rs->getTS();
  292. $open_tb = $rs->f('post_open_tb');
  293. $open_comment = $rs->f('post_open_comment');
  294. $perm_url = $rs->getPermURL();
  295. $res['dateCreated'] = new IXR_Date($post_ts);
  296. $res['userid'] = $rs->f('user_id');
  297. if ($type == 'blogger') {
  298. $res['content'] = $content;
  299. }
  300. if ($type == 'mw') {
  301. $res['postid'] = $postid;
  302. $res['title'] = $titre;
  303. $res['description'] = $content;
  304. $res['link'] = $res['permalink'] = $perm_url;
  305. $res['mt_excerpt'] = $chapo;
  306. $res['mt_text_more'] = '';
  307. $res['mt_allow_comments'] = (integer) $open_comment;
  308. $res['mt_allow_pings'] = (integer) $open_tb;
  309. $res['mt_convert_breaks'] = '';
  310. $res['mt_keywords'] = '';
  311. }
  312. return $res;
  313. }
  314. function _getRecentPosts($user,$pwd,$numberofposts,$type='blogger')
  315. {
  316. if ($this->blog->checkUser($user,$pwd,1) === false) {
  317. return new IXR_Error(801,'Erreur de login');
  318. }
  319. if($numberofposts > 40) {
  320. return new IXR_Error(805,'Ne peut obtenir plus de 40 billets');
  321. }
  322. $this->blog->setUser($user);
  323. if (($news = $this->blog->getLastNews($numberofposts)) === false)
  324. {
  325. return new IXR_Error(802,'Erreur MySQL');
  326. }
  327. else
  328. {
  329. $res = array();
  330. while (!$news->EOF())
  331. {
  332. $content = $news->f('post_content');
  333. $chapo = $news->f('post_chapo');
  334. $titre = $news->f('post_titre');
  335. $post_ts = $news->getTS();
  336. $open_tb = $news->f('post_open_tb');
  337. $open_comment = $news->f('post_open_comment');
  338. $perm_url = $news->getPermURL();
  339. $tres['dateCreated'] = new IXR_Date($post_ts);
  340. $tres['userid'] = $news->f('user_id');
  341. $tres['postid'] = $news->f('post_id');
  342. if ($type == 'blogger') {
  343. $tres['content'] = $content;
  344. }
  345. if ($type == 'mt' || $type == 'mw') {
  346. $tres['title'] = $titre;
  347. }
  348. if ($type == 'mw') {
  349. $tres['description'] = $content;
  350. $tres['link'] = $tres['permalink'] = $perm_url;
  351. $tres['mt_excerpt'] = $chapo;
  352. $tres['mt_text_more'] = '';
  353. $tres['mt_allow_comments'] = (integer) $open_comment;
  354. $tres['mt_allow_pings'] = (integer) $open_tb;
  355. $tres['mt_convert_breaks'] = '';
  356. $tres['mt_keywords'] = '';
  357. }
  358. $res[] = $tres;
  359. unset($tres);
  360. $news->moveNext();
  361. }
  362. return $res;
  363. }
  364. }
  365. function _newPost($user,$pwd,$content,$struct=array(),$publish=true)
  366. {
  367. if ($this->blog->checkUser($user,$pwd,1) === false) {
  368. return new IXR_Error(801,'Erreur de login');
  369. }
  370. $title = (!empty($struct['title'])) ? $struct['title'] : '-';
  371. $description = (!empty($struct['description'])) ? $struct['description'] : NULL;
  372. $open_comment = (isset($struct['mt_allow_comments'])) ? $struct['mt_allow_comments'] : 1;
  373. $open_tb = (isset($struct['mt_allow_pings'])) ? $struct['mt_allow_pings'] : 1;
  374. $chapo = (!empty($struct['mt_excerpt'])) ? $struct['mt_excerpt'] : '';
  375. if ($description !== NULL) {
  376. $content = $description;
  377. }
  378. if (empty($content)) {
  379. return new IXR_Error(804,'Impossible de créer un billet vide');
  380. }
  381. $rsUser = $this->blog->getUser($user);
  382. $cat_id = $rsUser->f('user_pref_cat');
  383. $user_lang = $rsUser->f('user_lang');
  384. $publish = (boolean) $publish;
  385. if (($post_id = $this->blog->addPost($user,$title,'',$chapo,$content,'',
  386. $cat_id,'html',$publish,$open_comment,$open_tb,$user_lang,false,
  387. $rsUser->f('user_delta'))) !== false) {
  388. return (string) $post_id;
  389. } else {
  390. return new IXR_Error(802,'Erreur MySQL');
  391. }
  392. }
  393. function _editPost($user,$pwd,$postid,$content,$struct=array(),$publish=true)
  394. {
  395. if ($this->blog->checkUser($user,$pwd,1) === false) {
  396. return new IXR_Error(801,'Erreur de login');
  397. }
  398. $title = (!empty($struct['title'])) ? $struct['title'] : NULL;
  399. $description = (!empty($struct['description'])) ? $struct['description'] : NULL;
  400. $post_dt = (!empty($struct['dateCreated'])) ? strtotime($struct['dateCreated']) : '';
  401. $open_comment = (isset($struct['mt_allow_comments'])) ? $struct['mt_allow_comments'] : NULL;
  402. $open_tb = (isset($struct['mt_allow_pings'])) ? $struct['mt_allow_pings'] : NULL;
  403. $chapo = (!empty($struct['mt_excerpt'])) ? $struct['mt_excerpt'] : '';
  404. if (!$post_dt) {
  405. $post_dt = '';
  406. }
  407. if ($description !== NULL) {
  408. $content = $description;
  409. }
  410. if (empty($content)) {
  411. return new IXR_Error(804,'Impossible de créer un billet vide');
  412. }
  413. $rsUser = $this->blog->getUser($user);
  414. $rsPost = $this->blog->getPostByID($postid);
  415. if ($user != $rsPost->f('user_id') && $rsUser->f('user_level') < 9) {
  416. return new IXR_Error(807,'Permissions insuffisantes');
  417. }
  418. if ($open_comment === NULL) {
  419. $open_comment = $rsPost->f('post_open_comment');
  420. }
  421. if ($open_tb === NULL) {
  422. $open_tb = $rsPost->f('post_open_tb');
  423. }
  424. $publish = (boolean) $publish;
  425. $cat_id = $rsPost->f('cat_id');
  426. if ($title == NULL) {
  427. $title = $rsPost->f('post_titre');
  428. }
  429. $post_lang = $rsPost->f('post_lang');
  430. $post_selected = $rsPost->f('post_selected');
  431. if ($this->blog->updPost($postid,$title,'',$chapo,$content,'',$cat_id,
  432. 'html',$publish,$open_comment,$open_tb,$post_dt,$post_lang,$post_selected) !== false) {
  433. return true;
  434. } else {
  435. $this->__debugTrace('_editPost','Erreur: '.$this->blog->error(true));
  436. return new IXR_Error(802,'Erreur MySQL');
  437. }
  438. }
  439. function _deletePost($user,$pwd,$postid,$publish)
  440. {
  441. if ($this->blog->checkUser($user,$pwd,1) === false) {
  442. return new IXR_Error(801,'Erreur de login');
  443. }
  444. $rsUser = $this->blog->getUser($user);
  445. $rsPost = $this->blog->getPostByID($postid);
  446. if ($user != $rsPost->f('user_id') && $rsUser->f('user_level') < 9) {
  447. return new IXR_Error(807,'Permissions insuffisantes');
  448. }
  449. if ($publish) {
  450. if ($this->blog->delPost($postid) !== false) {
  451. return true;
  452. } else {
  453. return new IXR_Error(802,'Erreur MySQL');
  454. }
  455. }
  456. }
  457. function _setPostCat($user,$pwd,$postid,$cats)
  458. {
  459. if ($this->blog->checkUser($user,$pwd,1) === false) {
  460. return new IXR_Error(801,'Erreur de login');
  461. }
  462. $cat_id = (!empty($cats[0]['categoryId'])) ? $cats[0]['categoryId'] : NULL;
  463. foreach($cats as $v)
  464. {
  465. if (isset($v['isPrimary']) && $v['isPrimary']) {
  466. $cat_id = $v['categoryId'];
  467. break;
  468. }
  469. }
  470. if ($cat_id !== NULL) {
  471. if ($this->blog->updPostCat($postid,$cat_id) !== false) {
  472. return true;
  473. } else {
  474. return new IXR_Error(802,'Erreur MySQL');
  475. }
  476. }
  477. }
  478. }
  479. ?>