/class/xml/rpc/xoopsapi.php

https://gitlab.com/VoyaTrax/vtCMS3 · PHP · 344 lines · 303 code · 9 blank · 32 comment · 80 complexity · 3a1bafdf9f5b341b270bffdfd1a291f6 MD5 · raw file

  1. <?php
  2. /*
  3. You may not change or alter any portion of this comment or credits
  4. of supporting developers from this source code or any supporting source code
  5. which is considered copyrighted (c) material of the original comment or credit authors.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. */
  10. /**
  11. * @copyright The XOOPS Project http://sourceforge.net/projects/xoops/
  12. * @license GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
  13. * @package class
  14. * @subpackage xml
  15. * @since 1.0.0
  16. * @author Kazumi Ono (AKA onokazu)
  17. * @version $Id $
  18. */
  19. defined('XOOPS_ROOT_PATH') or die("XOOPS root path not defined");
  20. class XoopsApi extends XoopsXmlRpcApi
  21. {
  22. /**
  23. * @param array $params
  24. * @param XoopsXmlRpcResponse $response
  25. * @param XoopsModule $module
  26. */
  27. public function __construct(&$params, XoopsXmlRpcResponse &$response, XoopsModule &$module)
  28. {
  29. parent::__construct($params, $response, $module);
  30. }
  31. /**
  32. * @return void
  33. */
  34. public function newPost()
  35. {
  36. if (!$this->_checkUser($this->params[1], $this->params[2])) {
  37. $this->response->add(new XoopsXmlRpcFault(104));
  38. } else {
  39. if (!$fields = $this->_getPostFields(null, $this->params[0])) {
  40. $this->response->add(new XoopsXmlRpcFault(106));
  41. } else {
  42. $missing = array();
  43. foreach ($fields as $tag => $detail) {
  44. if (!isset($this->params[3][$tag])) {
  45. $data = $this->_getTagCdata($this->params[3]['xoops_text'], $tag, true);
  46. if (trim($data) == ''){
  47. if ($detail['required']) {
  48. $missing[] = $tag;
  49. }
  50. } else {
  51. $post[$tag] = $data;
  52. }
  53. } else {
  54. $post[$tag] = $this->params[3][$tag];
  55. }
  56. }
  57. if (count($missing) > 0) {
  58. $msg = '';
  59. foreach ($missing as $m) {
  60. $msg .= '<'.$m.'> ';
  61. }
  62. $this->response->add(new XoopsXmlRpcFault(109, $msg));
  63. } else {
  64. // will be removed... don't worry if this looks bad
  65. include_once XOOPS_ROOT_PATH.'/modules/news/class/class.newsstory.php';
  66. $story = new NewsStory();
  67. $error = false;
  68. if (intval($this->params[4]) > 0) {
  69. if (!$this->_checkAdmin()) {
  70. // non admin users cannot publish
  71. $error = true;
  72. $this->response->add(new XoopsXmlRpcFault(111));
  73. } else {
  74. $story->setType('admin');
  75. $story->setApproved(true);
  76. $story->setPublished(time());
  77. }
  78. } else {
  79. if (!$this->_checkAdmin()) {
  80. $story->setType('user');
  81. } else {
  82. $story->setType('admin');
  83. }
  84. }
  85. if (!$error) {
  86. if (isset($post['categories']) && !empty($post['categories'][0])) {
  87. $story->setTopicId(intval($post['categories'][0]['categoryId']));
  88. } else {
  89. $story->setTopicId(1);
  90. }
  91. $story->setTitle(addslashes(trim($post['title'])));
  92. if (isset($post['moretext'])) {
  93. $story->setBodytext(addslashes(trim($post['moretext'])));
  94. }
  95. if (!isset($post['hometext'])) {
  96. $story->setHometext(addslashes(trim($this->params[3]['xoops_text'])));
  97. } else {
  98. $story->setHometext(addslashes(trim($post['hometext'])));
  99. }
  100. $story->setUid($this->user->getVar('uid'));
  101. $story->setHostname($_SERVER['REMOTE_ADDR']);
  102. if (!$this->_checkAdmin()) {
  103. $story->setNohtml(1);
  104. } else {
  105. $story->setNohtml(0);
  106. }
  107. $story->setNosmiley(0);
  108. $story->setNotifyPub(1);
  109. $story->setTopicalign('R');
  110. $ret = $story->store();
  111. if (!$ret) {
  112. $this->response->add(new XoopsXmlRpcFault(106));
  113. } else {
  114. $this->response->add(new XoopsXmlRpcString($ret));
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. function editPost()
  122. {
  123. if (!$this->_checkUser($this->params[1], $this->params[2])) {
  124. $this->response->add(new XoopsXmlRpcFault(104));
  125. } else {
  126. if (!$fields = $this->_getPostFields($this->params[0])) {
  127. } else {
  128. $missing = array();
  129. foreach ($fields as $tag => $detail) {
  130. if (!isset($this->params[3][$tag])) {
  131. $data = $this->_getTagCdata($this->params[3]['xoops_text'], $tag, true);
  132. if (trim($data) == ''){
  133. if ($detail['required']) {
  134. $missing[] = $tag;
  135. }
  136. } else {
  137. $post[$tag] = $data;
  138. }
  139. } else {
  140. $post[$tag] = $this->params[3][$tag];
  141. }
  142. }
  143. if (count($missing) > 0) {
  144. $msg = '';
  145. foreach ($missing as $m) {
  146. $msg .= '<'.$m.'> ';
  147. }
  148. $this->response->add(new XoopsXmlRpcFault(109, $msg));
  149. } else {
  150. // will be removed... don't worry if this looks bad
  151. include_once XOOPS_ROOT_PATH.'/modules/news/class/class.newsstory.php';
  152. $story = new NewsStory($this->params[0]);
  153. $storyid = $story->storyid();
  154. if (empty($storyid)) {
  155. $this->response->add(new XoopsXmlRpcFault(106));
  156. } elseif (!$this->_checkAdmin()) {
  157. $this->response->add(new XoopsXmlRpcFault(111));
  158. } else {
  159. $story->setTitle(addslashes(trim($post['title'])));
  160. if (isset($post['moretext'])) {
  161. $story->setBodytext(addslashes(trim($post['moretext'])));
  162. }
  163. if (!isset($post['hometext'])) {
  164. $story->setHometext(addslashes(trim($this->params[3]['xoops_text'])));
  165. } else {
  166. $story->setHometext(addslashes(trim($post['hometext'])));
  167. }
  168. if ($this->params[4]) {
  169. $story->setApproved(true);
  170. $story->setPublished(time());
  171. }
  172. $story->setTopicalign('R');
  173. if (!$story->store()) {
  174. $this->response->add(new XoopsXmlRpcFault(106));
  175. } else {
  176. $this->response->add(new XoopsXmlRpcBoolean(true));
  177. }
  178. }
  179. }
  180. }
  181. }
  182. }
  183. function deletePost()
  184. {
  185. if (!$this->_checkUser($this->params[1], $this->params[2])) {
  186. $this->response->add(new XoopsXmlRpcFault(104));
  187. } else {
  188. if (!$this->_checkAdmin()) {
  189. $this->response->add(new XoopsXmlRpcFault(111));
  190. } else {
  191. // will be removed... don't worry if this looks bad
  192. include_once XOOPS_ROOT_PATH.'/modules/news/class/class.newsstory.php';
  193. $story = new NewsStory($this->params[0]);
  194. if (!$story->delete()) {
  195. $this->response->add(new XoopsXmlRpcFault(106));
  196. } else {
  197. $this->response->add(new XoopsXmlRpcBoolean(true));
  198. }
  199. }
  200. }
  201. }
  202. // currently returns the same struct as in metaWeblogApi
  203. function getPost($respond=true)
  204. {
  205. if (!$this->_checkUser($this->params[1], $this->params[2])) {
  206. $this->response->add(new XoopsXmlRpcFault(104));
  207. } else {
  208. // will be removed... don't worry if this looks bad
  209. include_once XOOPS_ROOT_PATH.'/modules/news/class/class.newsstory.php';
  210. $story = new NewsStory($this->params[0]);
  211. $ret = array('uid' => $story->uid(), 'published' => $story->published(), 'storyid' => $story->storyId(), 'title' => $story->title('Edit'), 'hometext' => $story->hometext('Edit'), 'moretext' => $story->bodytext('Edit'));
  212. if (!$respond) {
  213. return $ret;
  214. } else {
  215. if (!$ret) {
  216. $this->response->add(new XoopsXmlRpcFault(106));
  217. } else {
  218. $struct = new XoopsXmlRpcStruct();
  219. $content = '';
  220. foreach ($ret as $key => $value) {
  221. switch($key) {
  222. case 'uid':
  223. $struct->add('userid', new XoopsXmlRpcString($value));
  224. break;
  225. case 'published':
  226. $struct->add('dateCreated', new XoopsXmlRpcDatetime($value));
  227. break;
  228. case 'storyid':
  229. $struct->add('postid', new XoopsXmlRpcString($value));
  230. $struct->add('link', new XoopsXmlRpcString(XOOPS_URL.'/modules/news/article.php?item_id='.$value));
  231. $struct->add('permaLink', new XoopsXmlRpcString(XOOPS_URL.'/modules/news/article.php?item_id='.$value));
  232. break;
  233. case 'title':
  234. $struct->add('title', new XoopsXmlRpcString($value));
  235. break;
  236. default :
  237. $content .= '<'.$key.'>'.trim($value).'</'.$key.'>';
  238. break;
  239. }
  240. }
  241. $struct->add('description', new XoopsXmlRpcString($content));
  242. $this->response->add($struct);
  243. }
  244. }
  245. }
  246. }
  247. function getRecentPosts($respond=true)
  248. {
  249. if (!$this->_checkUser($this->params[1], $this->params[2])) {
  250. $this->response->add(new XoopsXmlRpcFault(104));
  251. } else {
  252. include_once XOOPS_ROOT_PATH.'/modules/news/class/class.newsstory.php';
  253. if (isset($this->params[4]) && intval($this->params[4]) > 0) {
  254. $stories = NewsStory::getAllPublished(intval($this->params[3]), 0, $this->params[4]);
  255. } else {
  256. $stories = NewsStory::getAllPublished(intval($this->params[3]));
  257. }
  258. $scount = count($stories);
  259. $ret = array();
  260. for ($i = 0; $i < $scount; $i++) {
  261. $ret[] = array('uid' => $stories[$i]->uid(), 'published' => $stories[$i]->published(), 'storyid' => $stories[$i]->storyId(), 'title' => $stories[$i]->title('Edit'), 'hometext' => $stories[$i]->hometext('Edit'), 'moretext' => $stories[$i]->bodytext('Edit'));
  262. }
  263. if (!$respond) {
  264. return $ret;
  265. } else {
  266. if (count($ret) == 0) {
  267. $this->response->add(new XoopsXmlRpcFault(106, 'Found 0 Entries'));
  268. } else {
  269. $arr = new XoopsXmlRpcArray();
  270. $count = count($ret);
  271. for ($i = 0; $i < $count; $i++) {
  272. $struct = new XoopsXmlRpcStruct();
  273. $content = '';
  274. foreach($ret[$i] as $key => $value) {
  275. switch($key) {
  276. case 'uid':
  277. $struct->add('userid', new XoopsXmlRpcString($value));
  278. break;
  279. case 'published':
  280. $struct->add('dateCreated', new XoopsXmlRpcDatetime($value));
  281. break;
  282. case 'storyid':
  283. $struct->add('postid', new XoopsXmlRpcString($value));
  284. $struct->add('link', new XoopsXmlRpcString(XOOPS_URL.'/modules/news/article.php?item_id='.$value));
  285. $struct->add('permaLink', new XoopsXmlRpcString(XOOPS_URL.'/modules/news/article.php?item_id='.$value));
  286. break;
  287. case 'title':
  288. $struct->add('title', new XoopsXmlRpcString($value));
  289. break;
  290. default :
  291. $content .= '<'.$key.'>'.trim($value).'</'.$key.'>';
  292. break;
  293. }
  294. }
  295. $struct->add('description', new XoopsXmlRpcString($content));
  296. $arr->add($struct);
  297. unset($struct);
  298. }
  299. $this->response->add($arr);
  300. }
  301. }
  302. }
  303. }
  304. function getCategories($respond=true)
  305. {
  306. if (!$this->_checkUser($this->params[1], $this->params[2])) {
  307. $this->response->add(new XoopsXmlRpcFault(104));
  308. } else {
  309. include_once XOOPS_ROOT_PATH.'/class/xoopstopic.php';
  310. $this->db = XoopsDatabaseFactory::getDatabaseConnection();
  311. $xt = new XoopsTopic($db->prefix('topics'));
  312. $ret = $xt->getTopicsList();
  313. if (!$respond) {
  314. return $ret;
  315. } else {
  316. if (count($ret) == 0) {
  317. $this->response->add(new XoopsXmlRpcFault(106, 'Found 0 Entries'));
  318. } else {
  319. $arr = new XoopsXmlRpcArray();
  320. foreach ($ret as $topic_id => $topic_vars) {
  321. $struct = new XoopsXmlRpcStruct();
  322. $struct->add('categoryId', new XoopsXmlRpcString($topic_id));
  323. $struct->add('categoryName', new XoopsXmlRpcString($topic_vars['title']));
  324. $struct->add('categoryPid', new XoopsXmlRpcString($topic_vars['pid']));
  325. $arr->add($struct);
  326. unset($struct);
  327. }
  328. $this->response->add($arr);
  329. }
  330. }
  331. }
  332. }
  333. }
  334. ?>