Skip to content

Commit 93cd584

Browse files
pocketarcgxgpet
andauthored
Merge gxgpet's XML-RPC fixes.
* Fixes XML-RPC compatibility with PHP 8 * Adds PHPUnit test for XML-RPC lib --------- Co-authored-by: George Petculescu <gxgpet@gmail.com>
1 parent 91a3d80 commit 93cd584

6 files changed

Lines changed: 270 additions & 91 deletions

File tree

system/libraries/Xmlrpc.php

Lines changed: 77 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,8 +1151,7 @@ public function parseResponse($fp)
11511151
//-------------------------------------
11521152

11531153
$parser = xml_parser_create($this->xmlrpc_defencoding);
1154-
$pname = (string) $parser;
1155-
$this->xh[$pname] = array(
1154+
$this->xh = array(
11561155
'isf' => 0,
11571156
'ac' => '',
11581157
'headers' => array(),
@@ -1175,7 +1174,7 @@ public function parseResponse($fp)
11751174
{
11761175
break;
11771176
}
1178-
$this->xh[$pname]['headers'][] = $line;
1177+
$this->xh['headers'][] = $line;
11791178
}
11801179
$data = implode("\r\n", $lines);
11811180

@@ -1193,43 +1192,43 @@ public function parseResponse($fp)
11931192
xml_parser_free($parser);
11941193

11951194
// Got ourselves some badness, it seems
1196-
if ($this->xh[$pname]['isf'] > 1)
1195+
if ($this->xh['isf'] > 1)
11971196
{
11981197
if ($this->debug === TRUE)
11991198
{
1200-
echo "---Invalid Return---\n".$this->xh[$pname]['isf_reason']."---Invalid Return---\n\n";
1199+
echo "---Invalid Return---\n".$this->xh['isf_reason']."---Invalid Return---\n\n";
12011200
}
12021201

1203-
return new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return'].' '.$this->xh[$pname]['isf_reason']);
1202+
return new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return'].' '.$this->xh['isf_reason']);
12041203
}
1205-
elseif ( ! is_object($this->xh[$pname]['value']))
1204+
elseif ( ! is_object($this->xh['value']))
12061205
{
1207-
return new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return'].' '.$this->xh[$pname]['isf_reason']);
1206+
return new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return'].' '.$this->xh['isf_reason']);
12081207
}
12091208

12101209
// Display XML content for debugging
12111210
if ($this->debug === TRUE)
12121211
{
12131212
echo '<pre>';
12141213

1215-
if (count($this->xh[$pname]['headers']) > 0)
1214+
if (count($this->xh['headers']) > 0)
12161215
{
12171216
echo "---HEADERS---\n";
1218-
foreach ($this->xh[$pname]['headers'] as $header)
1217+
foreach ($this->xh['headers'] as $header)
12191218
{
12201219
echo $header."\n";
12211220
}
12221221
echo "---END HEADERS---\n\n";
12231222
}
12241223

12251224
echo "---DATA---\n".htmlspecialchars($data)."\n---END DATA---\n\n---PARSED---\n";
1226-
var_dump($this->xh[$pname]['value']);
1225+
var_dump($this->xh['value']);
12271226
echo "\n---END PARSED---</pre>";
12281227
}
12291228

12301229
// Send response
1231-
$v = $this->xh[$pname]['value'];
1232-
if ($this->xh[$pname]['isf'])
1230+
$v = $this->xh['value'];
1231+
if ($this->xh['isf'])
12331232
{
12341233
$errno_v = $v->me['struct']['faultCode'];
12351234
$errstr_v = $v->me['struct']['faultString'];
@@ -1248,7 +1247,7 @@ public function parseResponse($fp)
12481247
$r = new XML_RPC_Response($v);
12491248
}
12501249

1251-
$r->headers = $this->xh[$pname]['headers'];
1250+
$r->headers = $this->xh['headers'];
12521251
return $r;
12531252
}
12541253

@@ -1279,26 +1278,24 @@ public function parseResponse($fp)
12791278
*/
12801279
public function open_tag($the_parser, $name)
12811280
{
1282-
$the_parser = (string) $the_parser;
1283-
12841281
// If invalid nesting, then return
1285-
if ($this->xh[$the_parser]['isf'] > 1) return;
1282+
if ($this->xh['isf'] > 1) return;
12861283

12871284
// Evaluate and check for correct nesting of XML elements
1288-
if (count($this->xh[$the_parser]['stack']) === 0)
1285+
if (count($this->xh['stack']) === 0)
12891286
{
12901287
if ($name !== 'METHODRESPONSE' && $name !== 'METHODCALL')
12911288
{
1292-
$this->xh[$the_parser]['isf'] = 2;
1293-
$this->xh[$the_parser]['isf_reason'] = 'Top level XML-RPC element is missing';
1289+
$this->xh['isf'] = 2;
1290+
$this->xh['isf_reason'] = 'Top level XML-RPC element is missing';
12941291
return;
12951292
}
12961293
}
12971294
// not top level element: see if parent is OK
1298-
elseif ( ! in_array($this->xh[$the_parser]['stack'][0], $this->valid_parents[$name], TRUE))
1295+
elseif ( ! in_array($this->xh['stack'][0], $this->valid_parents[$name], TRUE))
12991296
{
1300-
$this->xh[$the_parser]['isf'] = 2;
1301-
$this->xh[$the_parser]['isf_reason'] = 'XML-RPC element '.$name.' cannot be child of '.$this->xh[$the_parser]['stack'][0];
1297+
$this->xh['isf'] = 2;
1298+
$this->xh['isf_reason'] = 'XML-RPC element '.$name.' cannot be child of '.$this->xh['stack'][0];
13021299
return;
13031300
}
13041301

@@ -1308,22 +1305,22 @@ public function open_tag($the_parser, $name)
13081305
case 'ARRAY':
13091306
// Creates array for child elements
13101307
$cur_val = array('value' => array(), 'type' => $name);
1311-
array_unshift($this->xh[$the_parser]['valuestack'], $cur_val);
1308+
array_unshift($this->xh['valuestack'], $cur_val);
13121309
break;
13131310
case 'METHODNAME':
13141311
case 'NAME':
1315-
$this->xh[$the_parser]['ac'] = '';
1312+
$this->xh['ac'] = '';
13161313
break;
13171314
case 'FAULT':
1318-
$this->xh[$the_parser]['isf'] = 1;
1315+
$this->xh['isf'] = 1;
13191316
break;
13201317
case 'PARAM':
1321-
$this->xh[$the_parser]['value'] = NULL;
1318+
$this->xh['value'] = NULL;
13221319
break;
13231320
case 'VALUE':
1324-
$this->xh[$the_parser]['vt'] = 'value';
1325-
$this->xh[$the_parser]['ac'] = '';
1326-
$this->xh[$the_parser]['lv'] = 1;
1321+
$this->xh['vt'] = 'value';
1322+
$this->xh['ac'] = '';
1323+
$this->xh['lv'] = 1;
13271324
break;
13281325
case 'I4':
13291326
case 'INT':
@@ -1332,23 +1329,23 @@ public function open_tag($the_parser, $name)
13321329
case 'DOUBLE':
13331330
case 'DATETIME.ISO8601':
13341331
case 'BASE64':
1335-
if ($this->xh[$the_parser]['vt'] !== 'value')
1332+
if ($this->xh['vt'] !== 'value')
13361333
{
13371334
//two data elements inside a value: an error occurred!
1338-
$this->xh[$the_parser]['isf'] = 2;
1339-
$this->xh[$the_parser]['isf_reason'] = 'There is a '.$name.' element following a '
1340-
.$this->xh[$the_parser]['vt'].' element inside a single value';
1335+
$this->xh['isf'] = 2;
1336+
$this->xh['isf_reason'] = 'There is a '.$name.' element following a '
1337+
.$this->xh['vt'].' element inside a single value';
13411338
return;
13421339
}
13431340

1344-
$this->xh[$the_parser]['ac'] = '';
1341+
$this->xh['ac'] = '';
13451342
break;
13461343
case 'MEMBER':
13471344
// Set name of <member> to nothing to prevent errors later if no <name> is found
1348-
$this->xh[$the_parser]['valuestack'][0]['name'] = '';
1345+
$this->xh['valuestack'][0]['name'] = '';
13491346

13501347
// Set NULL value to check to see if value passed for this param/member
1351-
$this->xh[$the_parser]['value'] = NULL;
1348+
$this->xh['value'] = NULL;
13521349
break;
13531350
case 'DATA':
13541351
case 'METHODCALL':
@@ -1358,15 +1355,15 @@ public function open_tag($the_parser, $name)
13581355
break;
13591356
default:
13601357
/// An Invalid Element is Found, so we have trouble
1361-
$this->xh[$the_parser]['isf'] = 2;
1362-
$this->xh[$the_parser]['isf_reason'] = 'Invalid XML-RPC element found: '.$name;
1358+
$this->xh['isf'] = 2;
1359+
$this->xh['isf_reason'] = 'Invalid XML-RPC element found: '.$name;
13631360
break;
13641361
}
13651362

13661363
// Add current element name to stack, to allow validation of nesting
1367-
array_unshift($this->xh[$the_parser]['stack'], $name);
1364+
array_unshift($this->xh['stack'], $name);
13681365

1369-
$name === 'VALUE' OR $this->xh[$the_parser]['lv'] = 0;
1366+
$name === 'VALUE' OR $this->xh['lv'] = 0;
13701367
}
13711368

13721369
// --------------------------------------------------------------------
@@ -1380,27 +1377,25 @@ public function open_tag($the_parser, $name)
13801377
*/
13811378
public function closing_tag($the_parser, $name)
13821379
{
1383-
$the_parser = (string) $the_parser;
1384-
1385-
if ($this->xh[$the_parser]['isf'] > 1) return;
1380+
if ($this->xh['isf'] > 1) return;
13861381

13871382
// Remove current element from stack and set variable
13881383
// NOTE: If the XML validates, then we do not have to worry about
13891384
// the opening and closing of elements. Nesting is checked on the opening
13901385
// tag so we be safe there as well.
13911386

1392-
$curr_elem = array_shift($this->xh[$the_parser]['stack']);
1387+
$curr_elem = array_shift($this->xh['stack']);
13931388

13941389
switch ($name)
13951390
{
13961391
case 'STRUCT':
13971392
case 'ARRAY':
1398-
$cur_val = array_shift($this->xh[$the_parser]['valuestack']);
1399-
$this->xh[$the_parser]['value'] = isset($cur_val['values']) ? $cur_val['values'] : array();
1400-
$this->xh[$the_parser]['vt'] = strtolower($name);
1393+
$cur_val = array_shift($this->xh['valuestack']);
1394+
$this->xh['value'] = isset($cur_val['values']) ? $cur_val['values'] : array();
1395+
$this->xh['vt'] = strtolower($name);
14011396
break;
14021397
case 'NAME':
1403-
$this->xh[$the_parser]['valuestack'][0]['name'] = $this->xh[$the_parser]['ac'];
1398+
$this->xh['valuestack'][0]['name'] = $this->xh['ac'];
14041399
break;
14051400
case 'BOOLEAN':
14061401
case 'I4':
@@ -1409,87 +1404,87 @@ public function closing_tag($the_parser, $name)
14091404
case 'DOUBLE':
14101405
case 'DATETIME.ISO8601':
14111406
case 'BASE64':
1412-
$this->xh[$the_parser]['vt'] = strtolower($name);
1407+
$this->xh['vt'] = strtolower($name);
14131408

14141409
if ($name === 'STRING')
14151410
{
1416-
$this->xh[$the_parser]['value'] = $this->xh[$the_parser]['ac'];
1411+
$this->xh['value'] = $this->xh['ac'];
14171412
}
14181413
elseif ($name === 'DATETIME.ISO8601')
14191414
{
1420-
$this->xh[$the_parser]['vt'] = $this->xmlrpcDateTime;
1421-
$this->xh[$the_parser]['value'] = $this->xh[$the_parser]['ac'];
1415+
$this->xh['vt'] = $this->xmlrpcDateTime;
1416+
$this->xh['value'] = $this->xh['ac'];
14221417
}
14231418
elseif ($name === 'BASE64')
14241419
{
1425-
$this->xh[$the_parser]['value'] = base64_decode($this->xh[$the_parser]['ac']);
1420+
$this->xh['value'] = base64_decode($this->xh['ac']);
14261421
}
14271422
elseif ($name === 'BOOLEAN')
14281423
{
14291424
// Translated BOOLEAN values to TRUE AND FALSE
1430-
$this->xh[$the_parser]['value'] = (bool) $this->xh[$the_parser]['ac'];
1425+
$this->xh['value'] = (bool) $this->xh['ac'];
14311426
}
14321427
elseif ($name=='DOUBLE')
14331428
{
14341429
// we have a DOUBLE
14351430
// we must check that only 0123456789-.<space> are characters here
1436-
$this->xh[$the_parser]['value'] = preg_match('/^[+-]?[eE0-9\t \.]+$/', $this->xh[$the_parser]['ac'])
1437-
? (float) $this->xh[$the_parser]['ac']
1431+
$this->xh['value'] = preg_match('/^[+-]?[eE0-9\t \.]+$/', $this->xh['ac'])
1432+
? (float) $this->xh['ac']
14381433
: 'ERROR_NON_NUMERIC_FOUND';
14391434
}
14401435
else
14411436
{
14421437
// we have an I4/INT
14431438
// we must check that only 0123456789-<space> are characters here
1444-
$this->xh[$the_parser]['value'] = preg_match('/^[+-]?[0-9\t ]+$/', $this->xh[$the_parser]['ac'])
1445-
? (int) $this->xh[$the_parser]['ac']
1439+
$this->xh['value'] = preg_match('/^[+-]?[0-9\t ]+$/', $this->xh['ac'])
1440+
? (int) $this->xh['ac']
14461441
: 'ERROR_NON_NUMERIC_FOUND';
14471442
}
1448-
$this->xh[$the_parser]['ac'] = '';
1449-
$this->xh[$the_parser]['lv'] = 3; // indicate we've found a value
1443+
$this->xh['ac'] = '';
1444+
$this->xh['lv'] = 3; // indicate we've found a value
14501445
break;
14511446
case 'VALUE':
14521447
// This if() detects if no scalar was inside <VALUE></VALUE>
1453-
if ($this->xh[$the_parser]['vt'] == 'value')
1448+
if ($this->xh['vt'] == 'value')
14541449
{
1455-
$this->xh[$the_parser]['value'] = $this->xh[$the_parser]['ac'];
1456-
$this->xh[$the_parser]['vt'] = $this->xmlrpcString;
1450+
$this->xh['value'] = $this->xh['ac'];
1451+
$this->xh['vt'] = $this->xmlrpcString;
14571452
}
14581453

14591454
// build the XML-RPC value out of the data received, and substitute it
1460-
$temp = new XML_RPC_Values($this->xh[$the_parser]['value'], $this->xh[$the_parser]['vt']);
1455+
$temp = new XML_RPC_Values($this->xh['value'], $this->xh['vt']);
14611456

1462-
if (count($this->xh[$the_parser]['valuestack']) && $this->xh[$the_parser]['valuestack'][0]['type'] === 'ARRAY')
1457+
if (count($this->xh['valuestack']) && $this->xh['valuestack'][0]['type'] === 'ARRAY')
14631458
{
14641459
// Array
1465-
$this->xh[$the_parser]['valuestack'][0]['values'][] = $temp;
1460+
$this->xh['valuestack'][0]['values'][] = $temp;
14661461
}
14671462
else
14681463
{
14691464
// Struct
1470-
$this->xh[$the_parser]['value'] = $temp;
1465+
$this->xh['value'] = $temp;
14711466
}
14721467
break;
14731468
case 'MEMBER':
1474-
$this->xh[$the_parser]['ac'] = '';
1469+
$this->xh['ac'] = '';
14751470

14761471
// If value add to array in the stack for the last element built
1477-
if ($this->xh[$the_parser]['value'])
1472+
if ($this->xh['value'])
14781473
{
1479-
$this->xh[$the_parser]['valuestack'][0]['values'][$this->xh[$the_parser]['valuestack'][0]['name']] = $this->xh[$the_parser]['value'];
1474+
$this->xh['valuestack'][0]['values'][$this->xh['valuestack'][0]['name']] = $this->xh['value'];
14801475
}
14811476
break;
14821477
case 'DATA':
1483-
$this->xh[$the_parser]['ac'] = '';
1478+
$this->xh['ac'] = '';
14841479
break;
14851480
case 'PARAM':
1486-
if ($this->xh[$the_parser]['value'])
1481+
if ($this->xh['value'])
14871482
{
1488-
$this->xh[$the_parser]['params'][] = $this->xh[$the_parser]['value'];
1483+
$this->xh['params'][] = $this->xh['value'];
14891484
}
14901485
break;
14911486
case 'METHODNAME':
1492-
$this->xh[$the_parser]['method'] = ltrim($this->xh[$the_parser]['ac']);
1487+
$this->xh['method'] = ltrim($this->xh['ac']);
14931488
break;
14941489
case 'PARAMS':
14951490
case 'FAULT':
@@ -1514,24 +1509,22 @@ public function closing_tag($the_parser, $name)
15141509
*/
15151510
public function character_data($the_parser, $data)
15161511
{
1517-
$the_parser = (string) $the_parser;
1518-
1519-
if ($this->xh[$the_parser]['isf'] > 1) return; // XML Fault found already
1512+
if ($this->xh['isf'] > 1) return; // XML Fault found already
15201513

15211514
// If a value has not been found
1522-
if ($this->xh[$the_parser]['lv'] !== 3)
1515+
if ($this->xh['lv'] !== 3)
15231516
{
1524-
if ($this->xh[$the_parser]['lv'] === 1)
1517+
if ($this->xh['lv'] === 1)
15251518
{
1526-
$this->xh[$the_parser]['lv'] = 2; // Found a value
1519+
$this->xh['lv'] = 2; // Found a value
15271520
}
15281521

1529-
if ( ! isset($this->xh[$the_parser]['ac']))
1522+
if ( ! isset($this->xh['ac']))
15301523
{
1531-
$this->xh[$the_parser]['ac'] = '';
1524+
$this->xh['ac'] = '';
15321525
}
15331526

1534-
$this->xh[$the_parser]['ac'] .= $data;
1527+
$this->xh['ac'] .= $data;
15351528
}
15361529
}
15371530

0 commit comments

Comments
 (0)