Re: patches for Net/URL.php and HTTP/Request.php, second attempt
| From: | Alexey Borzov | Date: | Mon, 14 Oct 2002 12:40:12 +0000 |
| Subject: | Re: patches for Net/URL.php and HTTP/Request.php, second attempt | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-10048@lists.php.net to get a copy of this message | ||
Greetings!
Oops. Forgot to actually attach the files. :]
At 14.10.2002, 16:38, you wrote:
AB> Greetings!
AB> This is another attempt to get rid of '[key]' after array
AB> variables. This time, I preserved backwards compatibility.
AB> Consider the following
--
С уважением, Алексей Борзов
отдел интернет-проектов ООО "РДВ-Медиа"
http://www.rdw.ru
http://www.vashdosug.ru
--- pear/HTTP_Request/Request.php Mon Oct 14 14:22:12 2002 +++ mod/Request.php Mon Oct 14 16:19:59 2002 @@ -138,6 +138,12 @@ var $_timeout; /** + * Whether to append [key] to array variable names or not + * @var bool + */ + var $_appendArrayBraces; + + /** * Constructor * * Sets up the object @@ -156,7 +162,6 @@ */ function HTTP_Request($url, $params = array()) { - $this->_url =& new Net_URL($url); $this->_sock =& new Net_Socket(); $this->_method = HTTP_REQUEST_METHOD_GET; $this->_http = HTTP_REQUEST_HTTP_VER_1_1; @@ -169,9 +174,14 @@ $this->_timeout = null; + // to make array handling backwards compatible + $this->_appendArrayBraces = true; + foreach ($params as $key => $value) { $this->{'_' . $key} = $value; } + // moved here to honor the previously set $_appendArrayBraces value + $this->_url =& new Net_URL($url, $this->_appendArrayBraces); // Default useragent $this->addHeader('User-Agent', 'PEAR HTTP_Request class ( http://pear.php.net/ )'); @@ -445,7 +455,11 @@ foreach($this->_postData as $name => $value) { if (is_array($value)) { foreach ($value as $k => $v) { - $postdata[] = sprintf('%s[%s]=%s', $name, $k, $v); + if ($this->_appendArrayBraces) { + $postdata[] = sprintf('%s[%s]=%s', $name, $k, $v); + } else { + $postdata[] = $name . '=' . $v; + } } } else { $postdata[] = $name . '=' . $value; --- pear/Net_URL/URL.php Mon Oct 14 14:23:22 2002 +++ mod/URL.php Mon Oct 14 16:20:10 2002 @@ -91,6 +91,12 @@ var $anchor; /** + * Whether to append [key] to array variable names or not + * @var bool + */ + var $_appendArrayBraces; + + /** * Constructor * * Parses the given url and stores the various parts @@ -98,10 +104,12 @@ * * @param $url The url */ - function Net_URL($url = null) + function Net_URL($url = null, $appendArrayBraces = true) { global $HTTP_SERVER_VARS; + $this->_appendArrayBraces = $appendArrayBraces; + /** * Figure out host/port */ @@ -206,7 +214,6 @@ */ function addQueryString($name, $value, $preencoded = false) { - $this->querystring[$name] = $preencoded ? $value : urlencode($value); if ($preencoded) { $this->querystring[$name] = $value; } else { @@ -250,7 +257,11 @@ foreach ($this->querystring as $name => $value) { if (is_array($value)) { foreach ($value as $k => $v) { - $querystring[] = sprintf('%s[%s]=%s', $name, $k, $v); + if ($this->_appendArrayBraces) { + $querystring[] = sprintf('%s[%s]=%s', $name, $k, $v); + } else { + $querystring[] = $name . '=' . $v; + } } } else { $querystring[] = $name . '=' . $value; @@ -273,18 +284,36 @@ */ function _parseRawQuerystring($querystring) { - parse_str($querystring, $qs); + // the builtin parse_str() function expects array + // variables to have braces appended + if ($this->_appendArrayBraces) { + parse_str($querystring, $qs); + + foreach ($qs as $key => $value) { + if (is_array($value)) { + foreach ($value as $k => $v) { + $value[$k] = rawurlencode($v); + } + $qs[$key] = $value; + } else { + $qs[$key] = rawurlencode($value); + } + } - foreach ($qs as $key => $value) { - if (is_array($value)) { - foreach ($value as $k => $v) { - $value[$k] = rawurlencode($v); + } else { + $parts = explode('&', $querystring); + $qs = array(); + foreach ($parts as $var) { + list($name, $value) = explode('=', $var); + if (isset($qs[$name]) && is_array($qs[$name])) { + $qs[$name][] = $value; + } elseif(isset($qs[$name])) { + $qs[$name] = array($qs[$name], $value); + } else { + $qs[$name] = $value; } - $qs[$key] = $value; - } else { - $qs[$key] = rawurlencode($value); } - } + } return $qs; }
--- pear/HTTP_Request/Request.php Mon Oct 14 14:22:12 2002 +++ mod/Request.php Mon Oct 14 16:19:59 2002 @@ -138,6 +138,12 @@ var $_timeout; /** + * Whether to append [key] to array variable names or not + * @var bool + */ + var $_appendArrayBraces; + + /** * Constructor * * Sets up the object @@ -156,7 +162,6 @@ */ function HTTP_Request($url, $params = array()) { - $this->_url =& new Net_URL($url); $this->_sock =& new Net_Socket(); $this->_method = HTTP_REQUEST_METHOD_GET; $this->_http = HTTP_REQUEST_HTTP_VER_1_1; @@ -169,9 +174,14 @@ $this->_timeout = null; + // to make array handling backwards compatible + $this->_appendArrayBraces = true; + foreach ($params as $key => $value) { $this->{'_' . $key} = $value; } + // moved here to honor the previously set $_appendArrayBraces value + $this->_url =& new Net_URL($url, $this->_appendArrayBraces); // Default useragent $this->addHeader('User-Agent', 'PEAR HTTP_Request class ( http://pear.php.net/ )'); @@ -445,7 +455,11 @@ foreach($this->_postData as $name => $value) { if (is_array($value)) { foreach ($value as $k => $v) { - $postdata[] = sprintf('%s[%s]=%s', $name, $k, $v); + if ($this->_appendArrayBraces) { + $postdata[] = sprintf('%s[%s]=%s', $name, $k, $v); + } else { + $postdata[] = $name . '=' . $v; + } } } else { $postdata[] = $name . '=' . $value; --- pear/Net_URL/URL.php Mon Oct 14 14:23:22 2002 +++ mod/URL.php Mon Oct 14 16:20:10 2002 @@ -91,6 +91,12 @@ var $anchor; /** + * Whether to append [key] to array variable names or not + * @var bool + */ + var $_appendArrayBraces; + + /** * Constructor * * Parses the given url and stores the various parts @@ -98,10 +104,12 @@ * * @param $url The url */ - function Net_URL($url = null) + function Net_URL($url = null, $appendArrayBraces = true) { global $HTTP_SERVER_VARS; + $this->_appendArrayBraces = $appendArrayBraces; + /** * Figure out host/port */ @@ -206,7 +214,6 @@ */ function addQueryString($name, $value, $preencoded = false) { - $this->querystring[$name] = $preencoded ? $value : urlencode($value); if ($preencoded) { $this->querystring[$name] = $value; } else { @@ -250,7 +257,11 @@ foreach ($this->querystring as $name => $value) { if (is_array($value)) { foreach ($value as $k => $v) { - $querystring[] = sprintf('%s[%s]=%s', $name, $k, $v); + if ($this->_appendArrayBraces) { + $querystring[] = sprintf('%s[%s]=%s', $name, $k, $v); + } else { + $querystring[] = $name . '=' . $v; + } } } else { $querystring[] = $name . '=' . $value; @@ -273,18 +284,36 @@ */ function _parseRawQuerystring($querystring) { - parse_str($querystring, $qs); + // the builtin parse_str() function expects array + // variables to have braces appended + if ($this->_appendArrayBraces) { + parse_str($querystring, $qs); + + foreach ($qs as $key => $value) { + if (is_array($value)) { + foreach ($value as $k => $v) { + $value[$k] = rawurlencode($v); + } + $qs[$key] = $value; + } else { + $qs[$key] = rawurlencode($value); + } + } - foreach ($qs as $key => $value) { - if (is_array($value)) { - foreach ($value as $k => $v) { - $value[$k] = rawurlencode($v); + } else { + $parts = explode('&', $querystring); + $qs = array(); + foreach ($parts as $var) { + list($name, $value) = explode('=', $var); + if (isset($qs[$name]) && is_array($qs[$name])) { + $qs[$name][] = $value; + } elseif(isset($qs[$name])) { + $qs[$name] = array($qs[$name], $value); + } else { + $qs[$name] = $value; } - $qs[$key] = $value; - } else { - $qs[$key] = rawurlencode($value); } - } + } return $qs; }