Completed
Push — cleanup1603 ( 770f62...9b437b )
by Richard
06:42
created

BloggerApi::getRecentPosts()   C

Complexity

Conditions 9
Paths 9

Size

Total Lines 47
Code Lines 38

Duplication

Lines 17
Ratio 36.17 %

Code Coverage

Tests 0
CRAP Score 90
Metric Value
dl 17
loc 47
ccs 0
cts 41
cp 0
rs 5.2941
cc 9
eloc 38
nc 9
nop 0
crap 90
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       XOOPS Project (http://xoops.org)
14
 * @license         GNU GPL 2 or later (http://www.gnu.org/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
class BloggerApi extends XoopsXmlRpcApi
22
{
23
    /**
24
     * @param $params
25
     * @param $response
26
     * @param $module
27
     */
28 5 View Code Duplication
    public function __construct(&$params, &$response, &$module)
29 4
    {
30 5
        parent::__construct($params, $response, $module);
31 5
        $this->_setXoopsTagMap('storyid', 'postid');
32 5
        $this->_setXoopsTagMap('published', 'dateCreated');
33 5
        $this->_setXoopsTagMap('uid', 'userid');
34 5
    }
35
36
    public function newPost()
37
    {
38
        if (!$this->_checkUser($this->params[2], $this->params[3])) {
39
            $this->response->add(new XoopsXmlRpcFault(104));
40
        } else {
41
            if (!$fields =& $this->_getPostFields(null, $this->params[1])) {
42
                $this->response->add(new XoopsXmlRpcFault(106));
43 View Code Duplication
            } else {
44
                $missing = array();
45
                $post = array();
46
                foreach ($fields as $tag => $detail) {
47
                    $maptag = $this->_getXoopsTagMap($tag);
48
                    $data = $this->_getTagCdata($this->params[4], $maptag, true);
49
                    if (trim($data) == ''){
50
                        if ($detail['required']) {
51
                            $missing[] = $maptag;
52
                        }
53
                    } else {
54
                        $post[$tag] = $data;
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
                    $newparams = array();
65
                    // Xoops Api ignores App key
66
                    $newparams[0] = $this->params[1];
67
                    $newparams[1] = $this->params[2];
68
                    $newparams[2] = $this->params[3];
69
                    foreach ($post as $key => $value) {
70
                        $newparams[3][$key] = $value;
71
                        unset($value);
72
                    }
73
                    $newparams[3]['xoops_text'] = $this->params[4];
74
                    $newparams[4] = $this->params[5];
75
                    $xoopsapi = $this->_getXoopsApi($newparams);
76
                    $xoopsapi->_setUser($this->user, $this->isadmin);
77
                    $xoopsapi->newPost();
78
                }
79
            }
80
        }
81
    }
82
83
    public function editPost()
84
    {
85
        if (!$this->_checkUser($this->params[2], $this->params[3])) {
86
            $this->response->add(new XoopsXmlRpcFault(104));
87 View Code Duplication
        } else {
88
            if (!$fields = $this->_getPostFields($this->params[1])) {
89
            } else {
90
                $missing = array();
91
                $post = array();
92
                foreach ($fields as $tag => $detail) {
93
                    $data = $this->_getTagCdata($this->params[4], $tag, true);
94
                    if (trim($data) == ''){
95
                        if ($detail['required']) {
96
                            $missing[] = $tag;
97
                        }
98
                    } else {
99
                        $post[$tag] = $data;
100
                    }
101
                }
102
                if (count($missing) > 0) {
103
                    $msg = '';
104
                    foreach ($missing as $m) {
105
                        $msg .= '<' . $m . '> ';
106
                    }
107
                    $this->response->add(new XoopsXmlRpcFault(109, $msg));
108
                } else {
109
                    $newparams = array();
110
                    // XOOPS API ignores App key (index 0 of params)
111
                    $newparams[0] = $this->params[1];
112
                    $newparams[1] = $this->params[2];
113
                    $newparams[2] = $this->params[3];
114
                    foreach ($post as $key => $value) {
115
                        $newparams[3][$key] = $value;
116
                        unset($value);
117
                    }
118
                    $newparams[3]['xoops_text'] = $this->params[4];
119
                    $newparams[4] = $this->params[5];
120
                    $xoopsapi = $this->_getXoopsApi($newparams);
121
                    $xoopsapi->_setUser($this->user, $this->isadmin);
122
                    $xoopsapi->editPost();
123
                }
124
            }
125
        }
126
    }
127
128
    public function deletePost()
129
    {
130
        if (!$this->_checkUser($this->params[2], $this->params[3])) {
131
            $this->response->add(new XoopsXmlRpcFault(104));
132
        } else {
133
            // XOOPS API ignores App key (index 0 of params)
134
            array_shift($this->params);
135
            $xoopsapi = $this->_getXoopsApi($this->params);
136
            $xoopsapi->_setUser($this->user, $this->isadmin);
137
            $xoopsapi->deletePost();
138
        }
139
    }
140
141
    public function getPost()
142
    {
143
        if (!$this->_checkUser($this->params[2], $this->params[3])) {
144
            $this->response->add(new XoopsXmlRpcFault(104));
145
        } else {
146
            // XOOPS API ignores App key (index 0 of params)
147
            array_shift($this->params);
148
            $xoopsapi = $this->_getXoopsApi($this->params);
149
            $xoopsapi->_setUser($this->user, $this->isadmin);
150
            $ret = $xoopsapi->getPost(false);
0 ignored issues
show
Bug introduced by
The method getPost() does not exist on XoopsXmlRpcApi. Did you maybe mean _getPostFields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
151
            if (is_array($ret)) {
152
                $struct = new XoopsXmlRpcStruct();
153
                $content = '';
154 View Code Duplication
                foreach ($ret as $key => $value) {
155
                    $maptag = $this->_getXoopsTagMap($key);
156
                    switch($maptag) {
157
                    case 'userid':
158
                        $struct->add('userid', new XoopsXmlRpcString($value));
159
                        break;
160
                    case 'dateCreated':
161
                        $struct->add('dateCreated', new XoopsXmlRpcDatetime($value));
162
                        break;
163
                    case 'postid':
164
                        $struct->add('postid', new XoopsXmlRpcString($value));
165
                        break;
166
                    default :
167
                        $content .= '<'.$key.'>'.trim($value).'</'.$key.'>';
168
                        break;
169
                    }
170
                }
171
                $struct->add('content', new XoopsXmlRpcString($content));
172
                $this->response->add($struct);
173
            } else {
174
                $this->response->add(new XoopsXmlRpcFault(106));
175
            }
176
        }
177
    }
178
179
    public function getRecentPosts()
180
    {
181
        if (!$this->_checkUser($this->params[2], $this->params[3])) {
182
            $this->response->add(new XoopsXmlRpcFault(104));
183
        } else {
184
            // XOOPS API ignores App key (index 0 of params)
185
            array_shift($this->params);
186
            $xoopsapi = $this->_getXoopsApi($this->params);
187
            $xoopsapi->_setUser($this->user, $this->isadmin);
188
            $ret = $xoopsapi->getRecentPosts(false);
189
            if (is_array($ret)) {
190
                $arr = new XoopsXmlRpcArray();
191
                $count = count($ret);
192
                if ($count == 0) {
193
                    $this->response->add(new XoopsXmlRpcFault(106, 'Found 0 Entries'));
194
                } else {
195
                    for ($i = 0; $i < $count; ++$i) {
196
                        $struct = new XoopsXmlRpcStruct();
197
                        $content = '';
198 View Code Duplication
                        foreach($ret[$i] as $key => $value) {
199
                            $maptag = $this->_getXoopsTagMap($key);
200
                            switch($maptag) {
201
                            case 'userid':
202
                                $struct->add('userid', new XoopsXmlRpcString($value));
203
                                break;
204
                            case 'dateCreated':
205
                                $struct->add('dateCreated', new XoopsXmlRpcDatetime($value));
206
                                break;
207
                            case 'postid':
208
                                $struct->add('postid', new XoopsXmlRpcString($value));
209
                                break;
210
                            default :
211
                                $content .= '<'.$key.'>'.trim($value).'</'.$key.'>';
212
                                break;
213
                            }
214
                        }
215
                        $struct->add('content', new XoopsXmlRpcString($content));
216
                        $arr->add($struct);
217
                        unset($struct);
218
                    }
219
                    $this->response->add($arr);
220
                }
221
            } else {
222
                $this->response->add(new XoopsXmlRpcFault(106));
223
            }
224
        }
225
    }
226
227 1
    public function getUsersBlogs()
228
    {
229 1
        if (!$this->_checkUser($this->params[1], $this->params[2])) {
230 1
            $this->response->add(new XoopsXmlRpcFault(104));
231 1
        } else {
232 1
            $arr = new XoopsXmlRpcArray();
233 1
            $struct = new XoopsXmlRpcStruct();
234 1
			$xoops_url = \XoopsBaseConfig::get('url');
235 1
            $struct->add('url', new XoopsXmlRpcString($xoops_url.'/modules/'.$this->module->getVar('dirname').'/'));
236 1
            $struct->add('blogid', new XoopsXmlRpcString($this->module->getVar('mid')));
237 1
            $struct->add('blogName', new XoopsXmlRpcString('XOOPS Blog'));
238 1
            $arr->add($struct);
239 1
            $this->response->add($arr);
240
        }
241 1
    }
242
243 1
    public function getUserInfo()
244
    {
245 1
        if (!$this->_checkUser($this->params[1], $this->params[2])) {
246 1
            $this->response->add(new XoopsXmlRpcFault(104));
247 1
        } else {
248 1
            $struct = new XoopsXmlRpcStruct();
249 1
            $struct->add('nickname', new XoopsXmlRpcString($this->user->getVar('uname')));
250 1
            $struct->add('userid', new XoopsXmlRpcString($this->user->getVar('uid')));
251 1
            $struct->add('url', new XoopsXmlRpcString($this->user->getVar('url')));
252 1
            $struct->add('email', new XoopsXmlRpcString($this->user->getVar('email')));
253 1
            $struct->add('lastname', new XoopsXmlRpcString(''));
254 1
            $struct->add('firstname', new XoopsXmlRpcString($this->user->getVar('name')));
255 1
            $this->response->add($struct);
256
        }
257 1
    }
258
259 1
    public function getTemplate()
260
    {
261 1
        if (!$this->_checkUser($this->params[2], $this->params[3])) {
262
            $this->response->add(new XoopsXmlRpcFault(104));
263
        } else {
264 1
            switch ($this->params[5]) {
265 1
            case 'main':
266
                $this->response->add(new XoopsXmlRpcFault(107));
267
                break;
268 1
            case 'archiveIndex':
269
                $this->response->add(new XoopsXmlRpcFault(107));
270
                break;
271 1
            default:
272 1
                $this->response->add(new XoopsXmlRpcFault(107));
273 1
                break;
274 1
            }
275
        }
276 1
    }
277
278 1
    public function setTemplate()
279
    {
280 1
        if (!$this->_checkUser($this->params[2], $this->params[3])) {
281 1
            $this->response->add(new XoopsXmlRpcFault(104));
282 1
        } else {
283 1
            $this->response->add(new XoopsXmlRpcFault(107));
284
        }
285 1
    }
286
}
287