From: gggeek Date: Sun, 12 Apr 2015 11:27:00 +0000 (+0100) Subject: Always display debug messages even when there's badly encoded chars in them X-Git-Tag: 4.0.0-alpha^2~97 X-Git-Url: http://git.onelab.eu/?p=plcapi.git;a=commitdiff_plain;h=81ff420c14ca2c24b7e204b1c8ec1405739b88da Always display debug messages even when there's badly encoded chars in them --- diff --git a/NEWS b/NEWS index d54c51f..9f0bd69 100644 --- a/NEWS +++ b/NEWS @@ -46,6 +46,9 @@ PLEASE READ CAREFULLY THE NOTES BELOW to insure a smooth upgrade. * fixed: the function decode_xml() would not decode an xml with character set declaration in the xml prologue +* improved: echo all debug messages even when there are characters in them which php deems in a wrong encoding + (this is visible e.g. in the debugger) + XML-RPC for PHP version 3.0.0 - 2014/6/15 diff --git a/src/Client.php b/src/Client.php index 7473afe..d71ef27 100644 --- a/src/Client.php +++ b/src/Client.php @@ -2,6 +2,8 @@ namespace PhpXmlRpc; +use PhpXmlRpc\Helper\Logger; + class Client { /// @todo: do these need to be public? @@ -551,7 +553,7 @@ class Client $payload; if ($this->debug > 1) { - $this->debugMessage("---SENDING---\n$op\n---END---"); + Logger::debugMessage("---SENDING---\n$op\n---END---"); } if ($timeout > 0) { @@ -707,9 +709,7 @@ class Client } if ($this->debug > 1) { - $this->debugMessage("---SENDING---\n$payload\n---END---"); - // let the client see this now in case http times out... - flush(); + Logger::debugMessage("---SENDING---\n$payload\n---END---"); } if (!$keepAlive || !$this->xmlrpc_curl_handle) { @@ -848,7 +848,7 @@ class Client $message .= $name . ': ' . $val . "\n"; } $message .= "---END---"; - $this->debugMessage($message); + Logger::debugMessage($message); } if (!$result) { @@ -987,7 +987,7 @@ class Client if ($this->return_type == 'xml') { return $rets; } elseif ($this->return_type == 'phpvals') { - ///@todo test this code branch... + /// @todo test this code branch... $rets = $result->value(); if (!is_array($rets)) { return false; // bad return type from system.multicall @@ -1071,20 +1071,4 @@ class Client return $response; } } - - /** - * Echoes a debug message, taking care of escaping it when not in console mode - * - * @param string $message - */ - protected function debugMessage($message) - { - if (PHP_SAPI != 'cli') { - print "
\n".htmlentities($message)."\n
"; - } else { - print "\n$message\n"; - } - // let the client see this now in case http times out... - flush(); - } } diff --git a/src/Helper/Http.php b/src/Helper/Http.php index 2fc7b80..0fa3f51 100644 --- a/src/Helper/Http.php +++ b/src/Helper/Http.php @@ -3,6 +3,7 @@ namespace PhpXmlRpc\Helper; use PhpXmlRpc\PhpXmlRpc; +use PhpXmlRpc\Helper\Logger; class Http { @@ -198,7 +199,7 @@ class Http foreach ($httpResponse['cookies'] as $header => $value) { $msg .= "COOKIE: $header={$value['value']}\n"; } - $this->debugMessage($msg); + Logger::debugMessage($msg); } // if CURL was used for the call, http headers have been processed, @@ -222,12 +223,12 @@ class Http if ($httpResponse['headers']['content-encoding'] == 'deflate' && $degzdata = @gzuncompress($data)) { $data = $degzdata; if ($debug) { - $this->debugMessage("---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n$data\n---END---"); + Logger::debugMessage("---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n$data\n---END---"); } } elseif ($httpResponse['headers']['content-encoding'] == 'gzip' && $degzdata = @gzinflate(substr($data, 10))) { $data = $degzdata; if ($debug) { - $this->debugMessage("---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n$data\n---END---"); + Logger::debugMessage("---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n$data\n---END---"); } } else { error_log('XML-RPC: ' . __METHOD__ . ': errors occurred when trying to decode the deflated data received from server'); @@ -243,18 +244,4 @@ class Http return $httpResponse; } - - /** - * Echoes a debug message, taking care of escaping it when not in console mode - * - * @param string $message - */ - protected function debugMessage($message) - { - if (PHP_SAPI != 'cli') { - print "
\n".htmlentities($message)."\n
"; - } else { - print "\n$message\n"; - } - } } diff --git a/src/Helper/Logger.php b/src/Helper/Logger.php new file mode 100644 index 0000000..cb0ab10 --- /dev/null +++ b/src/Helper/Logger.php @@ -0,0 +1,38 @@ +\n".htmlentities($message, $flags, $encoding)."\n"; + } else { + print "
\n".htmlentities($message, $flags)."\n
"; + } + } else { + print "\n$message\n"; + } + + // let the user see this now in case there's a time out later... + flush(); + } +} \ No newline at end of file diff --git a/src/PhpXmlRpc.php b/src/PhpXmlRpc.php index deb697a..88b8f70 100644 --- a/src/PhpXmlRpc.php +++ b/src/PhpXmlRpc.php @@ -66,7 +66,7 @@ class PhpXmlRpc // The encoding used internally by PHP. // String values received as xml will be converted to this, and php strings will be converted to xml // as if having been coded with this - public static $xmlrpc_internalencoding = "ISO-8859-1"; // TODO: maybe this would be better as UTF-8, or at least configurable? + public static $xmlrpc_internalencoding = "ISO-8859-1"; // TODO: maybe this would be better as UTF-8 public static $xmlrpcName = "XML-RPC for PHP"; public static $xmlrpcVersion = "4.0.0.beta"; @@ -136,4 +136,5 @@ class PhpXmlRpc } } } + } diff --git a/src/Request.php b/src/Request.php index 66e723f..6eb7671 100644 --- a/src/Request.php +++ b/src/Request.php @@ -4,6 +4,7 @@ namespace PhpXmlRpc; use PhpXmlRpc\Helper\Http; use PhpXmlRpc\Helper\XMLParser; +use PhpXmlRpc\Helper\Logger; class Request { @@ -168,8 +169,7 @@ class Request public function parseResponse($data = '', $headersProcessed = false, $returnType = 'xmlrpcvals') { if ($this->debug) { - // by maHo, replaced htmlspecialchars with htmlentities - $this->debugMessage("---GOT---\n$data\n---END---"); + Logger::debugMessage("---GOT---\n$data\n---END---"); } $this->httpResponse = array('raw_data' => $data, 'headers' => array(), 'cookies' => array()); @@ -194,16 +194,6 @@ class Request } } - if ($this->debug) { - $start = strpos($data, '', $start); - $comments = substr($data, $start, $end - $start); - $this->debugMessage("---SERVER DEBUG INFO (DECODED) ---\n\t" . str_replace("\n", "\n\t", base64_decode($comments))) . "\n---END---\n"; - } - } - // be tolerant of extra whitespace in response body $data = trim($data); @@ -216,6 +206,20 @@ class Request $data = substr($data, 0, $pos + 17); } + // try to 'guestimate' the character encoding of the received response + $respEncoding = XMLParser::guessEncoding(@$this->httpResponse['headers']['content-type'], $data); + + if ($this->debug) { + $start = strpos($data, '', $start); + $comments = substr($data, $start, $end - $start); + Logger::debugMessage("---SERVER DEBUG INFO (DECODED) ---\n\t" . + str_replace("\n", "\n\t", base64_decode($comments)) . "\n---END---", $respEncoding); + } + } + // if user wants back raw xml, give it to him if ($returnType == 'xml') { $r = new Response($data, 0, '', 'xml'); @@ -226,9 +230,6 @@ class Request return $r; } - // try to 'guestimate' the character encoding of the received response - $respEncoding = XMLParser::guessEncoding(@$this->httpResponse['headers']['content-type'], $data); - if ($respEncoding != '') { // Since parsing will fail if charset is not specified in the xml prologue, @@ -317,8 +318,8 @@ class Request PhpXmlRpc::$xmlrpcstr['invalid_return']); } else { if ($this->debug) { - $this->debugMessage( - "---PARSED---\n".var_export($xmlRpcParser->_xh['value'], true)."\n---END---", false + Logger::debugMessage( + "---PARSED---\n".var_export($xmlRpcParser->_xh['value'], true)."\n---END---" ); } @@ -326,8 +327,7 @@ class Request $v = &$xmlRpcParser->_xh['value']; if ($xmlRpcParser->_xh['isf']) { - /// @todo we should test here if server sent an int and a string, - /// and/or coerce them into such... + /// @todo we should test here if server sent an int and a string, and/or coerce them into such... if ($returnType == 'xmlrpcvals') { $errNo_v = $v->structmem('faultCode'); $errStr_v = $v->structmem('faultString'); @@ -355,22 +355,4 @@ class Request return $r; } - - /** - * Echoes a debug message, taking care of escaping it when not in console mode - * - * @param string $message - * @param bool $encodeEntities when false, escapes using htmlspecialchars instead of htmlentities - */ - protected function debugMessage($message, $encodeEntities = true) - { - if (PHP_SAPI != 'cli') { - if ($encodeEntities) - print "
\n".htmlentities($message)."\n
"; - else - print "
\n".htmlspecialchars($message)."\n
"; - } else { - print "\n$message\n"; - } - } }