From 8bd22f391a7f2cfed99b6d2781d27204dc7d445d Mon Sep 17 00:00:00 2001 From: gggeek Date: Thu, 2 Feb 2023 12:12:45 +0000 Subject: [PATCH] move xml_header method from server to response; shuffle class methods to improve readability --- src/Request.php | 136 +++++++++++++++++++++++++---------------------- src/Response.php | 15 +++++- src/Server.php | 41 +++++++------- src/Value.php | 87 +++++++++++++++--------------- 4 files changed, 151 insertions(+), 128 deletions(-) diff --git a/src/Request.php b/src/Request.php index 7f177b0a..6a99d92e 100644 --- a/src/Request.php +++ b/src/Request.php @@ -49,75 +49,63 @@ class Request } /** - * @internal this function will become protected in the future + * Gets/sets the xml-rpc method to be invoked. * - * @param string $charsetEncoding - * @return string + * @param string $methodName the method to be set (leave empty not to set it) + * @return string the method that will be invoked */ - public function xml_header($charsetEncoding = '') + public function method($methodName = '') { - if ($charsetEncoding != '') { - return "\n\n"; - } else { - return "\n\n"; + if ($methodName != '') { + $this->methodname = $methodName; } + + return $this->methodname; } /** - * @internal this function will become protected in the future + * Add a parameter to the list of parameters to be used upon method invocation. + * Checks that $params is actually a Value object and not a plain php value. * - * @return string + * @param Value $param + * @return boolean false on failure */ - public function xml_footer() + public function addParam($param) { - return ''; + // check: do not add to self params which are not xml-rpc values + if (is_object($param) && is_a($param, 'PhpXmlRpc\Value')) { + $this->params[] = $param; + + return true; + } else { + $this->getLogger()->error('XML-RPC: ' . __METHOD__ . ': value passed in must be a PhpXmlRpc\Value'); + return false; + } } /** - * @internal this function will become protected in the future (and be folded into serialize) + * Returns the nth parameter in the request. The index zero-based. * - * @param string $charsetEncoding - * @return void + * @param integer $i the index of the parameter to fetch (zero based) + * @return Value the i-th parameter */ - public function createPayload($charsetEncoding = '') + public function getParam($i) { - $this->logDeprecationUnlessCalledBy('serialize'); - - if ($charsetEncoding != '') { - $this->content_type = 'text/xml; charset=' . $charsetEncoding; - } else { - $this->content_type = 'text/xml'; - } - - $this->payload = $this->xml_header($charsetEncoding); - $this->payload .= '' . $this->getCharsetEncoder()->encodeEntities( - $this->methodname, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "\n"; - $this->payload .= "\n"; - foreach ($this->params as $p) { - $this->payload .= "\n" . $p->serialize($charsetEncoding) . - "\n"; - } - $this->payload .= "\n"; - $this->payload .= $this->xml_footer(); + return $this->params[$i]; } /** - * Gets/sets the xml-rpc method to be invoked. + * Returns the number of parameters in the message. * - * @param string $methodName the method to be set (leave empty not to set it) - * @return string the method that will be invoked + * @return integer the number of parameters currently set */ - public function method($methodName = '') + public function getNumParams() { - if ($methodName != '') { - $this->methodname = $methodName; - } - - return $this->methodname; + return count($this->params); } /** - * Returns xml representation of the message. XML prologue included. + * Returns xml representation of the message, XML prologue included. Sets `payload` and `content_type` properties * * @param string $charsetEncoding * @return string the xml representation of the message, xml prologue included @@ -130,44 +118,62 @@ class Request } /** - * Add a parameter to the list of parameters to be used upon method invocation. - * Checks that $params is actually a Value object and not a plain php value. + * @internal this function will become protected in the future (and be folded into serialize) * - * @param Value $param - * @return boolean false on failure + * @param string $charsetEncoding + * @return void */ - public function addParam($param) + public function createPayload($charsetEncoding = '') { - // check: do not add to self params which are not xml-rpc values - if (is_object($param) && is_a($param, 'PhpXmlRpc\Value')) { - $this->params[] = $param; + $this->logDeprecationUnlessCalledBy('serialize'); - return true; + if ($charsetEncoding != '') { + $this->content_type = 'text/xml; charset=' . $charsetEncoding; } else { - $this->getLogger()->error('XML-RPC: ' . __METHOD__ . ': value passed in must be a PhpXmlRpc\Value'); - return false; + $this->content_type = 'text/xml'; + } + + $result = $this->xml_header($charsetEncoding); + $result .= '' . $this->getCharsetEncoder()->encodeEntities( + $this->methodname, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) . "\n"; + $result .= "\n"; + foreach ($this->params as $p) { + $result .= "\n" . $p->serialize($charsetEncoding) . + "\n"; } + $result .= "\n"; + $result .= $this->xml_footer(); + + $this->payload = $result; } /** - * Returns the nth parameter in the request. The index zero-based. + * @internal this function will become protected in the future (and be folded into serialize) * - * @param integer $i the index of the parameter to fetch (zero based) - * @return Value the i-th parameter + * @param string $charsetEncoding + * @return string */ - public function getParam($i) + public function xml_header($charsetEncoding = '') { - return $this->params[$i]; + $this->logDeprecationUnlessCalledBy('createPayload'); + + if ($charsetEncoding != '') { + return "\n\n"; + } else { + return "\n\n"; + } } /** - * Returns the number of parameters in the message. + * @internal this function will become protected in the future (and be folded into serialize) * - * @return integer the number of parameters currently set + * @return string */ - public function getNumParams() + public function xml_footer() { - return count($this->params); + $this->logDeprecationUnlessCalledBy('createPayload'); + + return ''; } /** @@ -274,7 +280,7 @@ class Request /// @todo what if there is no end tag? $end = strpos($data, '-->', $start); $comments = substr($data, $start, $end - $start); - $this->getLogger()->debug("---SERVER DEBUG INFO (DECODED) ---\n\t" . + $this->getLogger()->debug("---SERVER DEBUG INFO (DECODED)---\n\t" . str_replace("\n", "\n\t", base64_decode($comments)) . "\n---END---", array('encoding' => $respEncoding)); } } diff --git a/src/Response.php b/src/Response.php index 2665e995..ad7e2f1b 100644 --- a/src/Response.php +++ b/src/Response.php @@ -144,7 +144,7 @@ class Response } /** - * Returns xml representation of the response. XML prologue not included. + * Returns xml representation of the response, XML prologue _not_ included. Sets `payload` and `content_type` properties * * @param string $charsetEncoding the charset to be used for serialization. If null, US-ASCII is assumed * @return string the xml representation of the response @@ -192,6 +192,19 @@ class Response return $result; } + /** + * @param string $charsetEncoding + * @return string + */ + public function xml_header($charsetEncoding = '') + { + if ($charsetEncoding != '') { + return "\n"; + } else { + return "\n"; + } + } + // *** BC layer *** // we have to make this return by ref in order to allow calls such as `$resp->_cookies['name'] = ['value' => 'something'];` diff --git a/src/Server.php b/src/Server.php index 16c6253b..33428f65 100644 --- a/src/Server.php +++ b/src/Server.php @@ -399,7 +399,7 @@ class Server static::$_xmlrpcs_occurred_errors . "+++END+++"); } - $payload = $this->xml_header($respCharset); + $payload = $resp->xml_header($respCharset); if ($this->debug > 0) { $payload = $payload . $this->serializeDebug($respCharset); } @@ -767,9 +767,7 @@ class Server if (!isset($dmap[$methodName]['function'])) { // No such method - return new Response(0, - PhpXmlRpc::$xmlrpcerr['unknown_method'], - PhpXmlRpc::$xmlrpcstr['unknown_method']); + return new Response(0, PhpXmlRpc::$xmlrpcerr['unknown_method'], PhpXmlRpc::$xmlrpcstr['unknown_method']); } // Check signature @@ -1001,19 +999,6 @@ class Server $this->debug_info .= $string . "\n"; } - /** - * @param string $charsetEncoding - * @return string - */ - protected function xml_header($charsetEncoding = '') - { - if ($charsetEncoding != '') { - return "\n"; - } else { - return "\n"; - } - } - /** * @param string $methName * @return bool @@ -1023,7 +1008,6 @@ class Server return (strpos($methName, "system.") === 0); } - /** * @param array $dmap * @return $this @@ -1376,7 +1360,7 @@ class Server public static function _xmlrpcs_multicall($server, $req) { $result = array(); - // let accept a plain list of php parameters, beside a single xml-rpc msg object + // let's accept a plain list of php parameters, beside a single xml-rpc msg object if (is_object($req)) { $calls = $req->getParam(0); foreach ($calls as $call) { @@ -1438,4 +1422,23 @@ class Server } } } + + // *** BC layer *** + + /** + * @param string $charsetEncoding + * @return string + * + * @deprecated this method was moved to the Response class + */ + protected function xml_header($charsetEncoding = '') + { + $this->logDeprecation('Method ' . __METHOD__ . ' is deprecated'); + + if ($charsetEncoding != '') { + return "\n"; + } else { + return "\n"; + } + } } diff --git a/src/Value.php b/src/Value.php index 3cb9c456..88972ca9 100644 --- a/src/Value.php +++ b/src/Value.php @@ -245,6 +245,50 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess } } + + /** + * Returns the value of a scalar xml-rpc value (base 64 decoding is automatically handled here) + * + * @return mixed + */ + public function scalarVal() + { + $b = reset($this->me); + + return $b; + } + + /** + * Returns the type of the xml-rpc value. + * + * @return string For integers, 'int' is always returned in place of 'i4'. 'i8' is considered a separate type and + * returned as such + */ + public function scalarTyp() + { + reset($this->me); + $a = key($this->me); + if ($a == static::$xmlrpcI4) { + $a = static::$xmlrpcInt; + } + + return $a; + } + + /** + * Returns the xml representation of the value. XML prologue not included. + * + * @param string $charsetEncoding the charset to be used for serialization. If null, US-ASCII is assumed + * @return string + */ + public function serialize($charsetEncoding = '') + { + $val = reset($this->me); + $typ = key($this->me); + + return '' . $this->serializeData($typ, $val, $charsetEncoding) . "\n"; + } + /** * @param string $typ * @param Value[]|mixed $val @@ -347,49 +391,6 @@ class Value implements \Countable, \IteratorAggregate, \ArrayAccess return $rs; } - /** - * Returns the xml representation of the value. XML prologue not included. - * - * @param string $charsetEncoding the charset to be used for serialization. If null, US-ASCII is assumed - * @return string - */ - public function serialize($charsetEncoding = '') - { - $val = reset($this->me); - $typ = key($this->me); - - return '' . $this->serializeData($typ, $val, $charsetEncoding) . "\n"; - } - - /** - * Returns the value of a scalar xml-rpc value (base 64 decoding is automatically handled here) - * - * @return mixed - */ - public function scalarVal() - { - $b = reset($this->me); - - return $b; - } - - /** - * Returns the type of the xml-rpc value. - * - * @return string For integers, 'int' is always returned in place of 'i4'. 'i8' is considered a separate type and - * returned as such - */ - public function scalarTyp() - { - reset($this->me); - $a = key($this->me); - if ($a == static::$xmlrpcI4) { - $a = static::$xmlrpcInt; - } - - return $a; - } - /** * Returns the number of members in an xml-rpc value: * - 0 for uninitialized values -- 2.47.0