1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: use Xoops\Core\Kernel\Handlers\XoopsModule;
13:
14: 15: 16: 17: 18: 19: 20: 21: 22:
23:
24: class XoopsApi extends XoopsXmlRpcApi
25: {
26: 27: 28: 29: 30:
31: public function __construct(array &$params, XoopsXmlRpcResponse $response, XoopsModule $module)
32: {
33: parent::__construct($params, $response, $module);
34: }
35:
36: 37: 38:
39: public function newPost()
40: {
41: if (!$this->_checkUser($this->params[1], $this->params[2])) {
42: $this->response->add(new XoopsXmlRpcFault(104));
43: } else {
44: if (!$fields = $this->_getPostFields(null, $this->params[0])) {
45: $this->response->add(new XoopsXmlRpcFault(106));
46: } else {
47: $missing = array();
48: foreach ($fields as $tag => $detail) {
49: if (!isset($this->params[3][$tag])) {
50: $data = $this->_getTagCdata($this->params[3]['xoops_text'], $tag, true);
51: if (trim($data) == ''){
52: if ($detail['required']) {
53: $missing[] = $tag;
54: }
55: } else {
56: $post[$tag] = $data;
57: }
58: } else {
59: $post[$tag] = $this->params[3][$tag];
60: }
61: }
62: if (count($missing) > 0) {
63: $msg = '';
64: foreach ($missing as $m) {
65: $msg .= '<'.$m.'> ';
66: }
67: $this->response->add(new XoopsXmlRpcFault(109, $msg));
68: } else {
69:
70: if (!XoopsLoad::loadFile(\XoopsBaseConfig::get('root-path').'/modules/news/class/class.newsstory.php', true)) {
71: $this->response->add(new XoopsXmlRpcFault(103));
72: return;
73: }
74: $story = new NewsStory();
75: $error = false;
76: if ((int)($this->params[4]) > 0) {
77: if (!$this->_checkAdmin()) {
78:
79: $error = true;
80: $this->response->add(new XoopsXmlRpcFault(111));
81: } else {
82: $story->setType('admin');
83: $story->setApproved(true);
84: $story->setPublished(time());
85: }
86: } else {
87: if (!$this->_checkAdmin()) {
88: $story->setType('user');
89: } else {
90: $story->setType('admin');
91: }
92: }
93: if (!$error) {
94: if (isset($post['categories']) && !empty($post['categories'][0])) {
95: $story->setTopicId((int)($post['categories'][0]['categoryId']));
96: } else {
97: $story->setTopicId(1);
98: }
99: $story->setTitle(addslashes(trim($post['title'])));
100: if (isset($post['moretext'])) {
101: $story->setBodytext(addslashes(trim($post['moretext'])));
102: }
103: if (!isset($post['hometext'])) {
104: $story->setHometext(addslashes(trim($this->params[3]['xoops_text'])));
105: } else {
106: $story->setHometext(addslashes(trim($post['hometext'])));
107: }
108: $story->setUid($this->user->getVar('uid'));
109: $story->setHostname($_SERVER['REMOTE_ADDR']);
110: if (!$this->_checkAdmin()) {
111: $story->setNohtml(1);
112: } else {
113: $story->setNohtml(0);
114: }
115: $story->setNosmiley(0);
116: $story->setNotifyPub(1);
117: $story->setTopicalign('R');
118: $ret = $story->store();
119: if (!$ret) {
120: $this->response->add(new XoopsXmlRpcFault(106));
121: } else {
122: $this->response->add(new XoopsXmlRpcString($ret));
123: }
124: }
125: }
126: }
127: }
128: }
129:
130: function editPost()
131: {
132: if (!$this->_checkUser($this->params[1], $this->params[2])) {
133: $this->response->add(new XoopsXmlRpcFault(104));
134: } else {
135: if (!$fields = $this->_getPostFields($this->params[0])) {
136: } else {
137: $missing = array();
138: foreach ($fields as $tag => $detail) {
139: if (!isset($this->params[3][$tag])) {
140: $data = $this->_getTagCdata($this->params[3]['xoops_text'], $tag, true);
141: if (trim($data) == ''){
142: if ($detail['required']) {
143: $missing[] = $tag;
144: }
145: } else {
146: $post[$tag] = $data;
147: }
148: } else {
149: $post[$tag] = $this->params[3][$tag];
150: }
151: }
152: if (count($missing) > 0) {
153: $msg = '';
154: foreach ($missing as $m) {
155: $msg .= '<'.$m.'> ';
156: }
157: $this->response->add(new XoopsXmlRpcFault(109, $msg));
158: } else {
159:
160: if (!XoopsLoad::loadFile(\XoopsBaseConfig::get('root-path').'/modules/news/class/class.newsstory.php', true)) {
161: $this->response->add(new XoopsXmlRpcFault(103));
162: return;
163: }
164: $story = new NewsStory($this->params[0]);
165: $storyid = $story->storyid();
166: if (empty($storyid)) {
167: $this->response->add(new XoopsXmlRpcFault(106));
168: } elseif (!$this->_checkAdmin()) {
169: $this->response->add(new XoopsXmlRpcFault(111));
170: } else {
171: $story->setTitle(addslashes(trim($post['title'])));
172: if (isset($post['moretext'])) {
173: $story->setBodytext(addslashes(trim($post['moretext'])));
174: }
175: if (!isset($post['hometext'])) {
176: $story->setHometext(addslashes(trim($this->params[3]['xoops_text'])));
177: } else {
178: $story->setHometext(addslashes(trim($post['hometext'])));
179: }
180: if ($this->params[4]) {
181: $story->setApproved(true);
182: $story->setPublished(time());
183: }
184: $story->setTopicalign('R');
185: if (!$story->store()) {
186: $this->response->add(new XoopsXmlRpcFault(106));
187: } else {
188: $this->response->add(new XoopsXmlRpcBoolean(true));
189: }
190: }
191: }
192: }
193: }
194: }
195:
196: function deletePost()
197: {
198: if (!$this->_checkUser($this->params[1], $this->params[2])) {
199: $this->response->add(new XoopsXmlRpcFault(104));
200: } else {
201: if (!$this->_checkAdmin()) {
202: $this->response->add(new XoopsXmlRpcFault(111));
203: } else {
204:
205: if (!XoopsLoad::loadFile(\XoopsBaseConfig::get('root-path').'/modules/news/class/class.newsstory.php', true)) {
206: $this->response->add(new XoopsXmlRpcFault(103));
207: return;
208: }
209: $story = new NewsStory($this->params[0]);
210: if (!$story->delete()) {
211: $this->response->add(new XoopsXmlRpcFault(106));
212: } else {
213: $this->response->add(new XoopsXmlRpcBoolean(true));
214: }
215: }
216: }
217: }
218:
219:
220: function getPost($respond=true)
221: {
222: if (!$this->_checkUser($this->params[1], $this->params[2])) {
223: $this->response->add(new XoopsXmlRpcFault(104));
224: } else {
225:
226: if (!XoopsLoad::loadFile(\XoopsBaseConfig::get('root-path').'/modules/news/class/class.newsstory.php', true)) {
227: $this->response->add(new XoopsXmlRpcFault(103));
228: return;
229: }
230: $story = new NewsStory($this->params[0]);
231: $ret = array('uid' => $story->uid(), 'published' => $story->published(), 'storyid' => $story->storyId(), 'title' => $story->title('Edit'), 'hometext' => $story->hometext('Edit'), 'moretext' => $story->bodytext('Edit'));
232: if (!$respond) {
233: return $ret;
234: } else {
235: if (!$ret) {
236: $this->response->add(new XoopsXmlRpcFault(106));
237: } else {
238: $struct = new XoopsXmlRpcStruct();
239: $content = '';
240: foreach ($ret as $key => $value) {
241: switch($key) {
242: case 'uid':
243: $struct->add('userid', new XoopsXmlRpcString($value));
244: break;
245: case 'published':
246: $struct->add('dateCreated', new XoopsXmlRpcDatetime($value));
247: break;
248: case 'storyid':
249: $struct->add('postid', new XoopsXmlRpcString($value));
250: $struct->add('link', new XoopsXmlRpcString(\XoopsBaseConfig::get('url').'/modules/news/article.php?item_id='.$value));
251: $struct->add('permaLink', new XoopsXmlRpcString(\XoopsBaseConfig::get('url').'/modules/news/article.php?item_id='.$value));
252: break;
253: case 'title':
254: $struct->add('title', new XoopsXmlRpcString($value));
255: break;
256: default :
257: $content .= '<'.$key.'>'.trim($value).'</'.$key.'>';
258: break;
259: }
260: }
261: $struct->add('description', new XoopsXmlRpcString($content));
262: $this->response->add($struct);
263: }
264: }
265: }
266: }
267:
268: function getRecentPosts($respond=true)
269: {
270: if (!$this->_checkUser($this->params[1], $this->params[2])) {
271: $this->response->add(new XoopsXmlRpcFault(104));
272: } else {
273: if (!XoopsLoad::loadFile(\XoopsBaseConfig::get('root-path').'/modules/news/class/class.newsstory.php', true)) {
274: $this->response->add(new XoopsXmlRpcFault(103));
275: return;
276: }
277: if (isset($this->params[4]) && (int)($this->params[4]) > 0) {
278: $stories = NewsStory::getAllPublished((int)($this->params[3]), 0, $this->params[4]);
279: } else {
280: $stories = NewsStory::getAllPublished((int)($this->params[3]));
281: }
282: $scount = count($stories);
283: $ret = array();
284: for ($i = 0; $i < $scount; ++$i) {
285: $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'));
286: }
287: if (!$respond) {
288: return $ret;
289: } else {
290: if (count($ret) == 0) {
291: $this->response->add(new XoopsXmlRpcFault(106, 'Found 0 Entries'));
292: } else {
293: $arr = new XoopsXmlRpcArray();
294: $count = count($ret);
295: for ($i = 0; $i < $count; ++$i) {
296: $struct = new XoopsXmlRpcStruct();
297: $content = '';
298: foreach($ret[$i] as $key => $value) {
299: switch($key) {
300: case 'uid':
301: $struct->add('userid', new XoopsXmlRpcString($value));
302: break;
303: case 'published':
304: $struct->add('dateCreated', new XoopsXmlRpcDatetime($value));
305: break;
306: case 'storyid':
307: $struct->add('postid', new XoopsXmlRpcString($value));
308: $struct->add('link', new XoopsXmlRpcString(\XoopsBaseConfig::get('url').'/modules/news/article.php?item_id='.$value));
309: $struct->add('permaLink', new XoopsXmlRpcString(\XoopsBaseConfig::get('url').'/modules/news/article.php?item_id='.$value));
310: break;
311: case 'title':
312: $struct->add('title', new XoopsXmlRpcString($value));
313: break;
314: default :
315: $content .= '<'.$key.'>'.trim($value).'</'.$key.'>';
316: break;
317: }
318: }
319: $struct->add('description', new XoopsXmlRpcString($content));
320: $arr->add($struct);
321: unset($struct);
322: }
323: $this->response->add($arr);
324: }
325: }
326: }
327: }
328:
329: function getCategories($respond=true)
330: {
331: global $xoopsDB;
332: if (!$this->_checkUser($this->params[1], $this->params[2])) {
333: $this->response->add(new XoopsXmlRpcFault(104));
334: } else {
335: if (!XoopsLoad::loadFile(\XoopsBaseConfig::get('root-path').'/class/xoopstopic.php', true)) {
336: $this->response->add(new XoopsXmlRpcFault(103));
337: return;
338: }
339:
340: $xt = new XoopsTopic($xoopsDB->prefix('topics'));
341: $ret = $xt->getTopicsList();
342: if (!$respond) {
343: return $ret;
344: } else {
345: if (count($ret) == 0) {
346: $this->response->add(new XoopsXmlRpcFault(106, 'Found 0 Entries'));
347: } else {
348: $arr = new XoopsXmlRpcArray();
349: foreach ($ret as $topic_id => $topic_vars) {
350: $struct = new XoopsXmlRpcStruct();
351: $struct->add('categoryId', new XoopsXmlRpcString($topic_id));
352: $struct->add('categoryName', new XoopsXmlRpcString($topic_vars['title']));
353: $struct->add('categoryPid', new XoopsXmlRpcString($topic_vars['pid']));
354: $arr->add($struct);
355: unset($struct);
356: }
357: $this->response->add($arr);
358: }
359: }
360: }
361: }
362: }
363: