XoopsXmlRpcRequest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 8
dl 0
loc 24
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A render() 0 9 2
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 https://xoops.org/
14
 * @license      GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package
16
 * @since
17
 * @author       XOOPS Development Team, Kazumi Ono (AKA onokazu)
18
 */
19
20
/**
21
 * Class XoopsXmlRpcDocument
22
 */
23
class XoopsXmlRpcDocument
24
{
25
    public $_tags = [];
26
27
    /**
28
     * XoopsXmlRpcDocument constructor.
29
     */
30
    public function __construct() {}
31
32
    /**
33
     * @param $tagobj
34
     */
35
    public function add($tagobj)
36
    {
37
        $this->_tags[] = & $tagobj;
38
    }
39
40
    public function render() {}
41
}
42
43
/**
44
 * Class XoopsXmlRpcResponse
45
 */
46
class XoopsXmlRpcResponse extends XoopsXmlRpcDocument
47
{
48
    /**
49
     * @return string
50
     */
51
    public function render()
52
    {
53
        $count   = count($this->_tags);
54
        $payload = '';
55
        for ($i = 0; $i < $count; ++$i) {
56
            if (!$this->_tags[$i]->isFault()) {
57
                $payload .= $this->_tags[$i]->render();
58
            } else {
59
                return '<?xml version="1.0"?><methodResponse>' . $this->_tags[$i]->render() . '</methodResponse>';
60
            }
61
        }
62
63
        return '<?xml version="1.0"?><methodResponse><params><param>' . $payload . '</param></params></methodResponse>';
64
    }
65
}
66
67
/**
68
 * Class XoopsXmlRpcRequest
69
 */
70
class XoopsXmlRpcRequest extends XoopsXmlRpcDocument
71
{
72
    public $methodName;
73
74
    /**
75
     * @param $methodName
76
     */
77
    public function __construct($methodName)
78
    {
79
        $this->methodName = trim($methodName);
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function render()
86
    {
87
        $count   = count($this->_tags);
88
        $payload = '';
89
        for ($i = 0; $i < $count; ++$i) {
90
            $payload .= '<param>' . $this->_tags[$i]->render() . '</param>';
91
        }
92
93
        return '<?xml version="1.0"?><methodCall><methodName>' . $this->methodName . '</methodName><params>' . $payload . '</params></methodCall>';
94
    }
95
}
96
97
/**
98
 * Class XoopsXmlRpcTag
99
 */
100
class XoopsXmlRpcTag
101
{
102
    public $_fault = false;
103
104
    /**
105
     * XoopsXmlRpcTag constructor.
106
     */
107
    public function __construct() {}
108
109
    /**
110
     * @param $text
111
     *
112
     * @return mixed
113
     */
114
    public function &encode(&$text)
115
    {
116
        $text = preg_replace(["/\&([a-z\d\#]+)\;/i", "/\&/", "/\#\|\|([a-z\d\#]+)\|\|\#/i"], ["#||\\1||#", '&amp;', "&\\1;"], str_replace(['<', '>'], ['&lt;', '&gt;'], $text));
117
118
        return $text;
119
    }
120
121
    /**
122
     * @param bool $fault
123
     */
124
    public function setFault($fault = true)
125
    {
126
        $this->_fault = ((int) $fault > 0);// ? true : false;
127
    }
128
129
    /**
130
     * @return bool
131
     */
132
    public function isFault()
133
    {
134
        return $this->_fault;
135
    }
136
137
    public function render() {}
138
}
139
140
/**
141
 * Class XoopsXmlRpcFault
142
 */
143
class XoopsXmlRpcFault extends XoopsXmlRpcTag
144
{
145
    public $_code;
146
    public $_extra;
147
148
    /**
149
     * @param      $code
150
     * @param null $extra
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $extra is correct as it would always require null to be passed?
Loading history...
151
     */
152
    public function __construct($code, $extra = null)
153
    {
154
        $this->setFault(true);
155
        $this->_code  = (int) $code;
156
        $this->_extra = isset($extra) ? trim($extra) : '';
157
    }
158
159
    /**
160
     * @return string
161
     */
162
    public function render()
163
    {
164
        switch ($this->_code) {
165
            case 101:
166
                $string = 'Invalid server URI';
167
                break;
168
            case 102:
169
                $string = 'Parser parse error';
170
                break;
171
            case 103:
172
                $string = 'Module not found';
173
                break;
174
            case 104:
175
                $string = 'User authentication failed';
176
                break;
177
            case 105:
178
                $string = 'Module API not found';
179
                break;
180
            case 106:
181
                $string = 'Method response error';
182
                break;
183
            case 107:
184
                $string = 'Method not supported';
185
                break;
186
            case 108:
187
                $string = 'Invalid parameter';
188
                break;
189
            case 109:
190
                $string = 'Missing parameters';
191
                break;
192
            case 110:
193
                $string = 'Selected blog application does not exist';
194
                break;
195
            case 111:
196
                $string = 'Method permission denied';
197
                break;
198
            default:
199
                $string = 'Method response error';
200
                break;
201
        }
202
        $string .= "\n" . $this->_extra;
203
204
        return '<fault><value><struct><member><name>faultCode</name><value>' . $this->_code . '</value></member><member><name>faultString</name><value>' . $this->encode($string) . '</value></member></struct></value></fault>';
205
    }
206
}
207
208
/**
209
 * Class XoopsXmlRpcInt
210
 */
211
class XoopsXmlRpcInt extends XoopsXmlRpcTag
212
{
213
    public $_value;
214
215
    /**
216
     * @param $value
217
     */
218
    public function __construct($value)
219
    {
220
        $this->_value = (int) $value;
221
    }
222
223
    /**
224
     * @return string
225
     */
226
    public function render()
227
    {
228
        return '<value><int>' . $this->_value . '</int></value>';
229
    }
230
}
231
232
/**
233
 * Class XoopsXmlRpcDouble
234
 */
235
class XoopsXmlRpcDouble extends XoopsXmlRpcTag
236
{
237
    public $_value;
238
239
    /**
240
     * @param $value
241
     */
242
    public function __construct($value)
243
    {
244
        $this->_value = (float) $value;
245
    }
246
247
    /**
248
     * @return string
249
     */
250
    public function render()
251
    {
252
        return '<value><double>' . $this->_value . '</double></value>';
253
    }
254
}
255
256
/**
257
 * Class XoopsXmlRpcBoolean
258
 */
259
class XoopsXmlRpcBoolean extends XoopsXmlRpcTag
260
{
261
    public $_value;
262
263
    /**
264
     * @param $value
265
     */
266
    public function __construct($value)
267
    {
268
        $this->_value = (!empty($value) && false != $value) ? 1 : 0;
269
    }
270
271
    /**
272
     * @return string
273
     */
274
    public function render()
275
    {
276
        return '<value><boolean>' . $this->_value . '</boolean></value>';
277
    }
278
}
279
280
/**
281
 * Class XoopsXmlRpcString
282
 */
283
class XoopsXmlRpcString extends XoopsXmlRpcTag
284
{
285
    public $_value;
286
287
    /**
288
     * @param $value
289
     */
290
    public function __construct($value)
291
    {
292
        $this->_value = (string) $value;
293
    }
294
295
    /**
296
     * @return string
297
     */
298
    public function render()
299
    {
300
        return '<value><string>' . $this->encode($this->_value) . '</string></value>';
301
    }
302
}
303
304
/**
305
 * Class XoopsXmlRpcDatetime
306
 */
307
class XoopsXmlRpcDatetime extends XoopsXmlRpcTag
308
{
309
    public $_value;
310
311
    /**
312
     * @param $value
313
     */
314
    public function __construct($value)
315
    {
316
        if (!is_numeric($value)) {
317
            $this->_value = strtotime($value);
318
        } else {
319
            $this->_value = (int) $value;
320
        }
321
    }
322
323
    /**
324
     * @return string
325
     */
326
    public function render()
327
    {
328
        return '<value><dateTime.iso8601>' . gmstrftime('%Y%m%dT%H:%M:%S', $this->_value) . '</dateTime.iso8601></value>';
329
    }
330
}
331
332
/**
333
 * Class XoopsXmlRpcBase64
334
 */
335
class XoopsXmlRpcBase64 extends XoopsXmlRpcTag
336
{
337
    public $_value;
338
339
    /**
340
     * @param $value
341
     */
342
    public function __construct($value)
343
    {
344
        $this->_value = base64_encode($value);
345
    }
346
347
    /**
348
     * @return string
349
     */
350
    public function render()
351
    {
352
        return '<value><base64>' . $this->_value . '</base64></value>';
353
    }
354
}
355
356
/**
357
 * Class XoopsXmlRpcArray
358
 */
359
class XoopsXmlRpcArray extends XoopsXmlRpcTag
360
{
361
    public $_tags = [];
362
363
    /**
364
     * XoopsXmlRpcArray constructor.
365
     */
366
    public function __construct() {}
367
368
    /**
369
     * @param $tagobj
370
     */
371
    public function add(&$tagobj)
372
    {
373
        $this->_tags[] = & $tagobj;
374
    }
375
376
    /**
377
     * @return string
378
     */
379
    public function render()
380
    {
381
        $count = count($this->_tags);
382
        $ret   = '<value><array><data>';
383
        for ($i = 0; $i < $count; ++$i) {
384
            $ret .= $this->_tags[$i]->render();
385
        }
386
        $ret .= '</data></array></value>';
387
388
        return $ret;
389
    }
390
}
391
392
/**
393
 * Class XoopsXmlRpcStruct
394
 */
395
class XoopsXmlRpcStruct extends XoopsXmlRpcTag
396
{
397
    public $_tags = [];
398
399
    /**
400
     * XoopsXmlRpcStruct constructor.
401
     */
402
    public function __construct() {}
403
404
    /**
405
     * @param $name
406
     * @param $tagobj
407
     */
408
    public function add($name, $tagobj)
409
    {
410
        $this->_tags[] = ['name' => $name, 'value' => $tagobj];
411
    }
412
413
    /**
414
     * @return string
415
     */
416
    public function render()
417
    {
418
        $count = count($this->_tags);
419
        $ret   = '<value><struct>';
420
        for ($i = 0; $i < $count; ++$i) {
421
            $ret .= '<member><name>' . $this->encode($this->_tags[$i]['name']) . '</name>' . $this->_tags[$i]['value']->render() . '</member>';
422
        }
423
        $ret .= '</struct></value>';
424
425
        return $ret;
426
    }
427
}
428