Improve phpdocs; fix Request dumping http headers with proper format
[plcapi.git] / src / Request.php
index 7004b54..a136bb3 100644 (file)
@@ -19,7 +19,7 @@ class Request
 
     /**
      * @param string $methodName the name of the method to invoke
-     * @param array $params array of parameters to be passed to the method (xmlrpcval objects)
+     * @param Value[] $params array of parameters to be passed to the method (Value objects)
      */
     public function __construct($methodName, $params = array())
     {
@@ -74,7 +74,7 @@ class Request
     /**
      * Gets/sets the xmlrpc method to be invoked.
      *
-     * @param string $meth the method to be set (leave empty not to set it)
+     * @param string $methodName the method to be set (leave empty not to set it)
      *
      * @return string the method that will be invoked
      */
@@ -104,7 +104,7 @@ class Request
     /**
      * Add a parameter to the list of parameters to be used upon method invocation.
      *
-     * @param Value $par
+     * @param Value $param
      *
      * @return boolean false on failure
      */
@@ -133,7 +133,7 @@ class Request
     }
 
     /**
-     * Returns the number of parameters in the messge.
+     * Returns the number of parameters in the message.
      *
      * @return integer the number of parameters currently set
      */
@@ -143,7 +143,7 @@ class Request
     }
 
     /**
-     * Given an open file handle, read all data available and parse it as axmlrpc response.
+     * Given an open file handle, read all data available and parse it as axmlrpc response.
      * NB: the file handle is not closed by this function.
      * NNB: might have trouble in rare cases to work on network streams, as we
      *      check for a read of 0 bytes instead of feof($fp).
@@ -297,17 +297,16 @@ class Request
         }
 
         $data = substr($data, $bd);
-
-        /// @todo when in CLI mode, do not html-encode the output
+        
         if ($this->debug && count($this->httpResponse['headers'])) {
-            print "</PRE>\n";
+            $msg = '';
             foreach ($this->httpResponse['headers'] as $header => $value) {
-                print htmlentities("HEADER: $header: $value\n");
+                $msg .= "HEADER: $header: $value\n";
             }
             foreach ($this->httpResponse['cookies'] as $header => $value) {
-                print htmlentities("COOKIE: $header={$value['value']}\n");
+                $msg .= "COOKIE: $header={$value['value']}\n";
             }
-            print "</PRE>\n";
+            $this->debugMessage($msg);
         }
 
         // if CURL was used for the call, http headers have been processed,
@@ -333,12 +332,12 @@ class Request
                         if ($this->httpResponse['headers']['content-encoding'] == 'deflate' && $degzdata = @gzuncompress($data)) {
                             $data = $degzdata;
                             if ($this->debug) {
-                                print "<PRE>---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n" . htmlentities($data) . "\n---END---</PRE>";
+                                $this->debugMessage("---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n$data\n---END---");
                             }
                         } elseif ($this->httpResponse['headers']['content-encoding'] == 'gzip' && $degzdata = @gzinflate(substr($data, 10))) {
                             $data = $degzdata;
                             if ($this->debug) {
-                                print "<PRE>---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n" . htmlentities($data) . "\n---END---</PRE>";
+                                $this->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');
@@ -372,7 +371,7 @@ class Request
     {
         if ($this->debug) {
             // by maHo, replaced htmlspecialchars with htmlentities
-            print "<PRE>---GOT---\n" . htmlentities($data) . "\n---END---\n</PRE>";
+            $this->debugMessage("---GOT---\n$data\n---END---");
         }
 
         $this->httpResponse = array();
@@ -405,7 +404,7 @@ class Request
                 $start += strlen('<!-- SERVER DEBUG INFO (BASE64 ENCODED):');
                 $end = strpos($data, '-->', $start);
                 $comments = substr($data, $start, $end - $start);
-                print "<PRE>---SERVER DEBUG INFO (DECODED) ---\n\t" . htmlentities(str_replace("\n", "\n\t", base64_decode($comments))) . "\n---END---\n</PRE>";
+                $this->debugMessage("---SERVER DEBUG INFO (DECODED) ---\n\t" . str_replace("\n", "\n\t", base64_decode($comments))) . "\n---END---\n</PRE>";
             }
         }
 
@@ -512,11 +511,9 @@ class Request
                 PhpXmlRpc::$xmlrpcstr['invalid_return']);
         } else {
             if ($this->debug) {
-                print "<PRE>---PARSED---\n";
-                // somehow htmlentities chokes on var_export, and some full html string...
-                //print htmlentitites(var_export($xmlRpcParser->_xh['value'], true));
-                print htmlspecialchars(var_export($xmlRpcParser->_xh['value'], true));
-                print "\n---END---</PRE>";
+                $this->debugMessage(
+                    "---PARSED---\n".var_export($xmlRpcParser->_xh['value'], true)."\n---END---", false
+                );
             }
 
             // note that using =& will raise an error if $xmlRpcParser->_xh['st'] does not generate an object.
@@ -552,4 +549,22 @@ class Request
 
         return $r;
     }
+
+    /**
+     * Echoes a debug message, taking care of escaping it when not in console mode
+     *
+     * @param string $message
+     */
+    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";
+        }
+    }
 }