Always display debug messages even when there's badly encoded chars in them
authorgggeek <giunta.gaetano@gmail.com>
Sun, 12 Apr 2015 11:27:00 +0000 (12:27 +0100)
committergggeek <giunta.gaetano@gmail.com>
Sun, 12 Apr 2015 11:27:00 +0000 (12:27 +0100)
NEWS
src/Client.php
src/Helper/Http.php
src/Helper/Logger.php [new file with mode: 0644]
src/PhpXmlRpc.php
src/Request.php

diff --git a/NEWS b/NEWS
index d54c51f..9f0bd69 100644 (file)
--- 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
 
index 7473afe..d71ef27 100644 (file)
@@ -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 "<PRE>\n".htmlentities($message)."\n</PRE>";
-        } else {
-            print "\n$message\n";
-        }
-        // let the client see this now in case http times out...
-        flush();
-    }
 }
index 2fc7b80..0fa3f51 100644 (file)
@@ -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 "<PRE>\n".htmlentities($message)."\n</PRE>";
-        } else {
-            print "\n$message\n";
-        }
-    }
 }
diff --git a/src/Helper/Logger.php b/src/Helper/Logger.php
new file mode 100644 (file)
index 0000000..cb0ab10
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: gg
+ * Date: 12/04/2015
+ * Time: 12:11
+ */
+
+namespace PhpXmlRpc\Helper;
+
+
+class Logger
+{
+    /**
+     * Echoes a debug message, taking care of escaping it when not in console mode.
+     * NB: if the encoding of the message is not known or wrong, and we are working in web mode, there is no guarantee
+     *     of 100% accuracy, which kind of defeats the purpose of debugging
+     *
+     * @param string $message
+     * @param string $encoding
+     */
+    public static function debugMessage($message, $encoding=null)
+    {
+        if (PHP_SAPI != 'cli') {
+            $flags = ENT_COMPAT | ENT_HTML401 | ENT_SUBSTITUTE;
+            if ($encoding != null) {
+                print "<PRE>\n".htmlentities($message, $flags, $encoding)."\n</PRE>";
+            } else {
+                print "<PRE>\n".htmlentities($message, $flags)."\n</PRE>";
+            }
+        } 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
index deb697a..88b8f70 100644 (file)
@@ -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
             }
         }
     }
+
 }
index 66e723f..6eb7671 100644 (file)
@@ -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, '<!-- SERVER DEBUG INFO (BASE64 ENCODED):');
-            if ($start) {
-                $start += strlen('<!-- SERVER DEBUG INFO (BASE64 ENCODED):');
-                $end = 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</PRE>";
-            }
-        }
-
         // 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, '<!-- SERVER DEBUG INFO (BASE64 ENCODED):');
+            if ($start) {
+                $start += strlen('<!-- SERVER DEBUG INFO (BASE64 ENCODED):');
+                $end = 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 "<PRE>\n".htmlentities($message)."\n</PRE>";
-            else
-                print "<PRE>\n".htmlspecialchars($message)."\n</PRE>";
-        } else {
-            print "\n$message\n";
-        }
-    }
 }