From 9c6e9421006e35722ad7d867d78100917348fffc Mon Sep 17 00:00:00 2001 From: gggeek Date: Tue, 31 Jan 2023 20:17:14 +0000 Subject: [PATCH] cleanups --- src/Encoder.php | 23 ++++++++++++----------- src/Request.php | 21 +++++++++++---------- src/Server.php | 25 +++++++++++++------------ src/Traits/ParserAware.php | 1 + 4 files changed, 37 insertions(+), 33 deletions(-) diff --git a/src/Encoder.php b/src/Encoder.php index b897af5a..6709e6ca 100644 --- a/src/Encoder.php +++ b/src/Encoder.php @@ -331,19 +331,20 @@ class Encoder XMLParser::ACCEPT_REQUEST | XMLParser::ACCEPT_RESPONSE | XMLParser::ACCEPT_VALUE | XMLParser::ACCEPT_FAULT, $parserOptions ); + $_xh = $xmlRpcParser->_xh['isf']; - if ($xmlRpcParser->_xh['isf'] > 1) { - // test that $xmlrpc->_xh['value'] is an obj, too??? + if ($_xh['isf'] > 1) { + // test that $_xh['value'] is an obj, too??? - $this->getLogger()->error('XML-RPC: ' . $xmlRpcParser->_xh['isf_reason']); + $this->getLogger()->error('XML-RPC: ' . $_xh['isf_reason']); return false; } - switch ($xmlRpcParser->_xh['rt']) { + switch ($_xh['rt']) { case 'methodresponse': - $v = $xmlRpcParser->_xh['value']; - if ($xmlRpcParser->_xh['isf'] == 1) { + $v = $_xh['value']; + if ($_xh['isf'] == 1) { /** @var Value $vc */ $vc = $v['faultCode']; /** @var Value $vs */ @@ -355,18 +356,18 @@ class Encoder return $r; case 'methodcall': - $req = new Request($xmlRpcParser->_xh['method']); - for ($i = 0; $i < count($xmlRpcParser->_xh['params']); $i++) { - $req->addParam($xmlRpcParser->_xh['params'][$i]); + $req = new Request($_xh['method']); + for ($i = 0; $i < count($_xh['params']); $i++) { + $req->addParam($_xh['params'][$i]); } return $req; case 'value': - return $xmlRpcParser->_xh['value']; + return $_xh['value']; case 'fault': // EPI api emulation - $v = $xmlRpcParser->_xh['value']; + $v = $_xh['value']; // use a known error code /** @var Value $vc */ $vc = isset($v['faultCode']) ? $v['faultCode']->scalarVal() : PhpXmlRpc::$xmlrpcerr['invalid_return']; diff --git a/src/Request.php b/src/Request.php index 2dbcf18b..8f1660f9 100644 --- a/src/Request.php +++ b/src/Request.php @@ -309,9 +309,10 @@ class Request $xmlRpcParser = $this->getParser(); $xmlRpcParser->parse($data, $returnType, XMLParser::ACCEPT_RESPONSE, $options); + $_xh = $xmlRpcParser->_xh['isf']; // first error check: xml not well-formed - if ($xmlRpcParser->_xh['isf'] == 3) { + if ($_xh['isf'] == 3) { // BC break: in the past for some cases we used the error message: 'XML error at line 1, check URL' @@ -320,25 +321,25 @@ class Request // there could be proxies meddling with the request, or network data corruption... $r = new Response(0, PhpXmlRpc::$xmlrpcerr['invalid_xml'], - PhpXmlRpc::$xmlrpcstr['invalid_xml'] . ' ' . $xmlRpcParser->_xh['isf_reason'], '', $httpResponse); + PhpXmlRpc::$xmlrpcstr['invalid_xml'] . ' ' . $_xh['isf_reason'], '', $httpResponse); if ($this->debug > 0) { - $this->getLogger()->debug($xmlRpcParser->_xh['isf_reason']); + $this->getLogger()->debug($_xh['isf_reason']); } } // second error check: xml well-formed but not xml-rpc compliant - elseif ($xmlRpcParser->_xh['isf'] == 2) { + elseif ($_xh['isf'] == 2) { $r = new Response(0, PhpXmlRpc::$xmlrpcerr['xml_not_compliant'], - PhpXmlRpc::$xmlrpcstr['xml_not_compliant'] . ' ' . $xmlRpcParser->_xh['isf_reason'], '', $httpResponse); + PhpXmlRpc::$xmlrpcstr['xml_not_compliant'] . ' ' . $_xh['isf_reason'], '', $httpResponse); /// @todo echo something for the user? check if it was already done by the parser... //if ($this->debug > 0) { - // $this->getLogger()->debug($xmlRpcParser->_xh['isf_reason']); + // $this->getLogger()->debug($_xh['isf_reason']); //} } // third error check: parsing of the response has somehow gone boink. /// @todo shall we omit this check, since we trust the parsing code? - elseif ($xmlRpcParser->_xh['isf'] > 3 || $returnType == XMLParser::RETURN_XMLRPCVALS && !is_object($xmlRpcParser->_xh['value'])) { + elseif ($_xh['isf'] > 3 || $returnType == XMLParser::RETURN_XMLRPCVALS && !is_object($_xh['value'])) { // something odd has happened and it's time to generate a client side error indicating something odd went on $r = new Response(0, PhpXmlRpc::$xmlrpcerr['xml_parsing_error'], PhpXmlRpc::$xmlrpcstr['xml_parsing_error'], '', $httpResponse @@ -348,13 +349,13 @@ class Request } else { if ($this->debug > 1) { $this->getLogger()->debug( - "---PARSED---\n".var_export($xmlRpcParser->_xh['value'], true)."\n---END---" + "---PARSED---\n".var_export($_xh['value'], true)."\n---END---" ); } - $v = $xmlRpcParser->_xh['value']; + $v = $_xh['value']; - if ($xmlRpcParser->_xh['isf']) { + if ($_xh['isf']) { /// @todo we should test (here or preferably in the parser) if server sent an int and a string, and/or /// coerce them into such... if ($returnType == XMLParser::RETURN_XMLRPCVALS) { diff --git a/src/Server.php b/src/Server.php index 85fbaa4c..73f828e0 100644 --- a/src/Server.php +++ b/src/Server.php @@ -691,43 +691,44 @@ class Server $xmlRpcParser = $this->getParser(); try { $xmlRpcParser->parse($data, $this->functions_parameters_type, XMLParser::ACCEPT_REQUEST, $options); + $_xh = $xmlRpcParser->_xh['isf']; } catch (NoSuchMethodException $e) { return new Response(0, $e->getCode(), $e->getMessage()); } - if ($xmlRpcParser->_xh['isf'] == 3) { + if ($_xh['isf'] == 3) { // (BC) we return XML error as a faultCode - preg_match('/^XML error ([0-9]+)/', $xmlRpcParser->_xh['isf_reason'], $matches); + preg_match('/^XML error ([0-9]+)/', $_xh['isf_reason'], $matches); return new Response( 0, PhpXmlRpc::$xmlrpcerrxml + (int)$matches[1], - $xmlRpcParser->_xh['isf_reason']); - } elseif ($xmlRpcParser->_xh['isf']) { + $_xh['isf_reason']); + } elseif ($_xh['isf']) { /// @todo separate better the various cases, as we have done in Request::parseResponse: invalid xml-rpc vs. /// parsing error return new Response( 0, PhpXmlRpc::$xmlrpcerr['invalid_request'], - PhpXmlRpc::$xmlrpcstr['invalid_request'] . ' ' . $xmlRpcParser->_xh['isf_reason']); + PhpXmlRpc::$xmlrpcstr['invalid_request'] . ' ' . $_xh['isf_reason']); } else { // small layering violation in favor of speed and memory usage: we should allow the 'execute' method handle // this, but in the most common scenario (xml-rpc values type server with some methods registered as phpvals) // that would mean a useless encode+decode pass if ($this->functions_parameters_type != 'xmlrpcvals' || - (isset($this->dmap[$xmlRpcParser->_xh['method']]['parameters_type']) && - ($this->dmap[$xmlRpcParser->_xh['method']]['parameters_type'] != 'xmlrpcvals') + (isset($this->dmap[$_xh['method']]['parameters_type']) && + ($this->dmap[$_xh['method']]['parameters_type'] != 'xmlrpcvals') ) ) { if ($this->debug > 1) { - $this->debugmsg("\n+++PARSED+++\n" . var_export($xmlRpcParser->_xh['params'], true) . "\n+++END+++"); + $this->debugmsg("\n+++PARSED+++\n" . var_export($_xh['params'], true) . "\n+++END+++"); } - return $this->execute($xmlRpcParser->_xh['method'], $xmlRpcParser->_xh['params'], $xmlRpcParser->_xh['pt']); + return $this->execute($_xh['method'], $_xh['params'], $_xh['pt']); } else { // build a Request object with data parsed from xml and add parameters in - $req = new Request($xmlRpcParser->_xh['method']); - for ($i = 0; $i < count($xmlRpcParser->_xh['params']); $i++) { - $req->addParam($xmlRpcParser->_xh['params'][$i]); + $req = new Request($_xh['method']); + for ($i = 0; $i < count($_xh['params']); $i++) { + $req->addParam($_xh['params'][$i]); } if ($this->debug > 1) { diff --git a/src/Traits/ParserAware.php b/src/Traits/ParserAware.php index 24ab1c2c..dbdfef4d 100644 --- a/src/Traits/ParserAware.php +++ b/src/Traits/ParserAware.php @@ -8,6 +8,7 @@ trait ParserAware { protected static $parser; + /// @todo feature-creep: allow passing in $options (but then, how to deal with changing options between invocations?) public function getParser() { if (self::$parser === null) { -- 2.47.0