-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccapiclient.php
388 lines (331 loc) · 11.1 KB
/
ccapiclient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
/* CentovaCast PHP API Client Example
* Copyright 2007-2008, Centova Technologies Inc.
* ===========================================================================
*
* This file provides an example interface to the CentovaCast XML API.
* An example of usage is provided in the example.php script accompanying
* this class.
*
* Note that all of the methods defined in the classes below should be
* considered private; method overloading is used to dynamically handle
* calls to what would be the public methods of each class.
*
*/
namespace Box\Mod\Servicecentovacast;
require_once dirname(__FILE__) . '/class_HTTPRetriever.php';
// This library was originally designed to support object overloading, but
// PHP's support for this appears to be flaky and prone to segfaulting
// (particularly in 4.x) so it's disabled by default.
define('CCAPI_NO_OVERLOAD', true);
/* CCBaseAPIClient
*
* Base class for all CentovaCast API classes
*/
class CCBaseAPIClient
{
public $debug = false;
public $debugconsole = false;
public $encoding = 'UTF-8';
/**
* @param string $payload
*/
public function build_request_packet($methodname, $payload)
{
return sprintf(
'<?xml version="1.0" encoding="' . $this->encoding . '"?>' .
'<centovacast>' .
'<request class="%s" method="%s"%s>' .
'%s' .
'</request>' .
'</centovacast>',
htmlentities($this->classname),
htmlentities($methodname),
($this->debug ? ' debug="enabled"' : '') .
($this->debugconsole ? ' debugconsole="' . htmlentities($this->debugconsole) . '"' : ''),
$payload
);
}
public function cc_initialize($ccurl)
{
$this->ccurl = $ccurl;
$this->http = new HTTPRetriever();
$this->http->HTTPRetriever();
$this->http->headers['User-Agent'] = 'CentovaCast PHP API Client';
}
public function build_argument_payload($functionargs)
{
return $this->build_argument_xml($functionargs[0]);
}
public function build_argument_xml($args)
{
$payload = '';
foreach ($args as $name => $value) {
$payload .= sprintf('<%s>%s</%s>', $name, htmlentities($value), $name);
}
return $payload;
}
public function parse_data($data)
{
if (!preg_match('/<data[^\>]*?>([\s\S]+)<\/data>/i', $data, $matches)) {
return false;
}
[, $rowxml] = $matches;
$rows = [];
if (!preg_match_all('/<row[^\>]*?>([\s\S]*?)<\/row>/i', $rowxml, $rowmatches, PREG_SET_ORDER)) {
return $rows;
}
foreach ($rowmatches as $k => $rowmatch) {
$fields = [];
[, $fieldxml] = $rowmatch;
if (preg_match_all('/<field(?:\s+name\s*=\s*"([^"]*?)")?[^\>]*?>([\s\S]*?)<\/field>/i', $fieldxml, $fieldmatches, PREG_SET_ORDER)) {
foreach ($fieldmatches as $k => $fieldmatch) {
[, $fieldname, $fieldvalue] = $fieldmatch;
if (strlen($fieldname)) {
$fields[$fieldname] = $fieldvalue;
} else {
$fields[] = $fieldvalue;
}
}
}
$rows[] = $fields;
}
return $rows;
}
public function parse_response_packet($packet)
{
$this->raw_response = $packet;
if (!preg_match('/<centovacast([^\>]+)>([\s\S]+)<\/centovacast>/i', $packet, $matches)) {
return $this->set_error('Invalid response packet received from API server');
}
$cctags = $matches[1];
if (preg_match('/version="([^\"]+)"/i', $cctags, $tagmatches)) {
$this->remote_version = $tagmatches[1];
} else {
$this->remote_version = false;
}
[, , $payload] = $matches;
if (!preg_match('/<response.*?type\s*=\s*"([^"]+)"[^\>]*>([\s\S]+)<\/response>/i', $payload, $matches)) {
return $this->set_error('Empty or unrecognized response packet received from API server');
}
[, $type, $data] = $matches;
if (preg_match('/<message[^\>]*>([\s\S]+)<\/message>/i', $data, $matches)) {
$this->message = $matches[1];
} else {
$this->message = '(Message not provided by API server)';
}
switch (strtolower($type)) {
case 'error':
return $this->set_error($this->message);
case 'success':
$this->data = $this->parse_data($data);
$this->bb_data = $this->bb_data($packet);
$this->success = true;
return;
default:
return $this->set_error('Invalid response type received from API server');
}
}
/**
* @param string $packet
*/
public function api_request($packet)
{
$url = $this->ccurl;
$apiscript = 'api.php';
if (substr($url, -strlen($apiscript) - 1) != '/' . $apiscript) {
if ('/' != substr($url, -1)) {
$url .= '/';
}
$url .= $apiscript;
}
$this->success = false;
$postdata = $packet;
if (!$this->http->post($url, $postdata)) {
$this->set_error('Error contacting server: ' . $this->http->get_error());
return;
}
$this->parse_response_packet($this->http->response);
$this->raw_request = $packet;
$this->raw_response = $this->http->raw_response;
}
/**
* @param string $msg
*/
public function set_error($msg)
{
$this->success = false;
$this->error = $msg;
return false;
}
/* Overloaded method handler; simply passes the request to
* the _call() method.
*
*/
public function __call($name, $args)
{
return $this->_call($name, $args);
}
/* For use when object overloading is not available.
*
* Usage: $obj->call('methodname',$arg1,$arg2,...)
*/
public function call()
{
$args = func_get_args();
$name = array_shift($args);
$this->_call($name, $args);
return true;
}
/* Private dispatch method for API calls. Used by __call() (for
* overloaded method calls) and call() (for direct calls).
*
*/
public function _call($name, $args)
{
$this->methodname = $name;
$payload = $this->build_argument_payload($args);
$packet = $this->build_request_packet($name, $payload);
$this->api_request($packet);
}
public function bb_data($packet)
{
$array = $this->xmlstr_to_array($packet);
if (isset($array['response']['data'])) {
return $array['response']['data'];
}
if (isset($array['response']['@attributes']['type']) && 'success' == $array['response']['@attributes']['type']) {
return true;
}
return $array;
}
/**
* convert xml string to php array - useful to get a serializable value.
*
* @param string $xmlstr
*
* @return array
*
* @author Adrien aka Gaarf
*/
public function xmlstr_to_array($xmlstr)
{
$doc = new \DOMDocument();
$doc->loadXML($xmlstr);
return $this->domnode_to_array($doc->documentElement);
}
/**
* @param DOMElement $node
*/
public function domnode_to_array($node)
{
$output = [];
switch ($node->nodeType) {
case XML_CDATA_SECTION_NODE:
case XML_TEXT_NODE:
$output = trim($node->textContent);
break;
case XML_ELEMENT_NODE:
for ($i = 0, $m = $node->childNodes->length; $i < $m; ++$i) {
$child = $node->childNodes->item($i);
$v = $this->domnode_to_array($child);
if (isset($child->tagName)) {
$t = $child->tagName;
if (!isset($output[$t])) {
$output[$t] = [];
}
$output[$t][] = $v;
} elseif ($v) {
$output = (string) $v;
}
}
if (is_array($output)) {
if ($node->attributes->length) {
$a = [];
foreach ($node->attributes as $attrName => $attrNode) {
$a[$attrName] = (string) $attrNode->value;
}
$output['@attributes'] = $a;
}
foreach ($output as $t => $v) {
if (is_array($v) && 1 == count($v) && '@attributes' != $t) {
$output[$t] = $v[0];
}
}
}
break;
}
if (empty($output)) {
$output = null;
}
return $output;
}
}
/* CCServerAPIClient
*
* Provides an interface to the Server class of the CentovaCast XML API.
*/
class CCServerAPIClient extends CCBaseAPIClient
{
public $classname = 'server';
public function CCServerAPIClient($ccurl)
{
$this->cc_initialize($ccurl);
}
public function build_argument_payload($functionargs)
{
if (count($functionargs) < 3) {
trigger_error(sprintf('Function %s requires a minimum of 3 arguments, %d given', $this->methodname, count($functionargs)), E_USER_WARNING);
}
$username = $functionargs[0];
$password = $functionargs[1];
$arguments = $functionargs[2];
if (!is_array($arguments)) {
$arguments = [];
}
$arguments = array_merge(
[
'username' => $username,
'password' => $password,
],
$arguments
);
return $this->build_argument_xml($arguments);
}
}
/* CCSystemAPIClient
*
* Provides an interface to the System class of the CentovaCast XML API.
*/
class CCSystemAPIClient extends CCBaseAPIClient
{
public $classname = 'system';
public function CCSystemAPIClient($ccurl)
{
$this->cc_initialize($ccurl);
}
public function build_argument_payload($functionargs)
{
if (count($functionargs) < 2) {
trigger_error(sprintf('Function %s requires a minimum of 2 arguments, %d given', $this->methodname, count($functionargs)), E_USER_WARNING);
}
$adminpassword = $functionargs[0];
$arguments = $functionargs[1];
if (!is_array($arguments)) {
$arguments = [];
}
$arguments = array_merge(
['password' => $adminpassword],
$arguments
);
return $this->build_argument_xml($arguments);
}
}
if (!defined('CCAPI_NO_OVERLOAD')) {
if (function_exists('overload')) {
overload('CCSystemAPIClient');
overload('CCServerAPIClient');
} elseif (version_compare(PHP_VERSION, '5.0.0', '<')) {
exit('The CentovaCast PHP API client requires that object overloading support is built into PHP.');
}
}