patches for Net/URL.php and HTTP/Request.php

From: Date: Mon, 30 Sep 2002 11:33:15 +0000
Subject: patches for Net/URL.php and HTTP/Request.php
Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-9711@lists.php.net to get a copy of this message
Greetings! These patches add an ability to pass arrays as second parameters of addQueryString() and addPostData() methods. This is useful in emulating the submission of forms containing <input type="checkbox"> and <select multiple> fields. Besides, a patch for Net/URL.php fixes its handling of such cases in URLs. Consider the following: $url = new Net_URL('http://www.example.com/index.html?foo=1&foo=2&bar=0'); echo $url->getUrl(); used to output 'http://www.example.com/index.html?foo=2&bar=0'! In a nutshell, my patch replaces the "PHP-centric" handling of multiple parameters with a same name in URL by a more generic one. This _will_ break BC, but considering the example I provided I think the breakage can be justified. -- С уважением, Алексей Борзов отдел интернет-проектов ООО "РДВ-Медиа" http://www.rdw.ru http://www.vashdosug.ru

--- /usr/local/share/pear/Net/URL.php Wed Aug 14 23:12:38 2002 +++ ./URL.php Mon Sep 30 15:19:10 2002 @@ -206,7 +206,11 @@ */ function addQueryString($name, $value, $preencoded = false) { - $this->querystring[$name] = $preencoded ? $value : urlencode($value); + if ($preencoded) { + $this->querystring[$name] = $value; + } else { + $this->querystring[$name] = is_array($value)? array_map('urlencode', $value): urlencode($value); + } } /** @@ -244,8 +248,8 @@ if (!empty($this->querystring)) { foreach ($this->querystring as $name => $value) { if (is_array($value)) { - foreach ($value as $k => $v) { - $querystring[] = sprintf('%s[%s]=%s', $name, $k, $v); + foreach ($value as $v) { + $querystring[] = $name . '=' . $v; } } else { $querystring[] = $name . '=' . $value; @@ -268,19 +272,18 @@ */ function _parseRawQuerystring($querystring) { - 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); - } - } - + $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; + } + } return $qs; } } --- /usr/local/share/pear/HTTP/Request.php Tue Sep 17 14:02:20 2002 +++ ./Request.php Mon Sep 30 15:19:10 2002 @@ -310,7 +310,11 @@ */ function addPostData($name, $value, $preencoded = false) { - $this->_postData[$name] = $preencoded ? $value : urlencode($value); + if ($preencoded) { + $this->_postData[$name] = $value; + } else { + $this->_postData[$name] = is_array($value)? array_map('urlencode', $value): urlencode($value); + } } /** @@ -434,7 +438,13 @@ // Post data if it's an array if (!empty($this->_postData) AND is_array($this->_postData)) { foreach($this->_postData as $name => $value) { - $postdata[] = $name . '=' . $value; + if (!is_array($value)) { + $postdata[] = $name . '=' . $value; + } else { + foreach ($value as $element) { + $postdata[] = $name . '=' . $element; + } + } } $postdata = implode('&', $postdata); $request .= 'Content-Length: ' . strlen($postdata) . "\r\n\r\n";
« previous php.pear.dev (#9711) next »