Jelajahi Sumber

ZF-11671: added feature for unmasking query paramsGet

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24387 44c647ce-9c0f-0410-b52a-842ac1e357ba
bate 14 tahun lalu
induk
melakukan
bac94c238a
1 mengubah file dengan 54 tambahan dan 0 penghapusan
  1. 54 0
      library/Zend/Http/Client.php

+ 54 - 0
library/Zend/Http/Client.php

@@ -247,6 +247,20 @@ class Zend_Http_Client
     protected $redirectCounter = 0;
     protected $redirectCounter = 0;
 
 
     /**
     /**
+     * Status for unmasking GET array params
+     *
+     * @var boolean
+     */
+    protected $_unmaskStatus = false;
+
+    /**
+     * Status if the http_build_query function escapes brackets
+     *
+     * @var boolean
+     */
+    protected $_queryBracketsEscaped = true;
+
+    /**
      * Fileinfo magic database resource
      * Fileinfo magic database resource
      *
      *
      * This variable is populated the first time _detectFileMimeType is called
      * This variable is populated the first time _detectFileMimeType is called
@@ -271,6 +285,8 @@ class Zend_Http_Client
         if ($config !== null) {
         if ($config !== null) {
             $this->setConfig($config);
             $this->setConfig($config);
         }
         }
+
+        $this->_queryBracketsEscaped = version_compare(phpversion(), '5.1.3', '>=');
     }
     }
 
 
     /**
     /**
@@ -791,6 +807,35 @@ class Zend_Http_Client
     }
     }
 
 
     /**
     /**
+     * Set the unmask feature for GET parameters as array
+     *
+     * Example:
+     * foo%5B0%5D=a&foo%5B1%5D=b
+     * becomes
+     * foo=a&foo=b
+     *
+     * This is usefull for some services
+     *
+     * @param boolean $status
+     * @return Zend_Http_Client
+     */
+    public function setUnmaskStatus($status = true)
+    {
+        $this->_unmaskStatus = (BOOL)$status;
+        return $this;
+    }
+
+    /**
+     * Returns the currently configured unmask status
+     *
+     * @return boolean
+     */
+    public function getUnmaskStatus()
+    {
+        return $this->_unmaskStatus;
+    }
+
+    /**
      * Clear all GET and POST parameters
      * Clear all GET and POST parameters
      *
      *
      * Should be used to reset the request parameters if the client is
      * Should be used to reset the request parameters if the client is
@@ -987,6 +1032,15 @@ class Zend_Http_Client
                     $query = str_replace('+', '%20', $query);
                     $query = str_replace('+', '%20', $query);
                 }
                 }
 
 
+                // @see ZF-11671 to unmask for some services to foo=val1&foo=val2
+                if ($this->getUnmaskStatus()) {
+                    if ($this->_queryBracketsEscaped) {
+                        $query = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $query);
+                    } else {
+                        $query = preg_replace('/\\[(?:[0-9]|[1-9][0-9]+)\\]=/', '=', $query);
+                    }
+                }
+
                 $uri->setQuery($query);
                 $uri->setQuery($query);
             }
             }