PageRenderTime 34ms CodeModel.GetById 14ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

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