diff --git a/lib/Horde/Url.php b/lib/Horde/Url.php index af66173..36e14a7 100644 --- a/lib/Horde/Url.php +++ b/lib/Horde/Url.php @@ -264,16 +264,10 @@ public function toString($raw = false, $full = true) } } - if ($params = $this->parameters) { - foreach ($params as $p => &$v) { - // TODO: Investigate if it should be done for all (or some) other objects - if ($v instanceof Horde_Url) { - $v = strval($v); - } - } - unset($v); - - $url .= '?' . http_build_query($params, "", $raw ? '&' : '&'); + if ($source = $this->parameters) { + $params = []; + self::encodeParameters($source, '', $params); + $url .= '?' . implode($raw ? '&' : '&', $params); } if ($this->anchor) { @@ -283,6 +277,38 @@ public function toString($raw = false, $full = true) return $url; } + protected static function encodeParameters($source, $prefix, &$params) + { + $index = 0; + + foreach ($source as $p => $v) { + if (strlen($prefix)) { + if ($index >= 0 && $p !== $index) { + $index = -1; + } + if ($index >= 0) { + $p = ''; + ++$index; + } else { + $p = rawurlencode($p); + } + $p = $prefix . '[' . $p . ']'; + } else { + $p = rawurlencode($p); + } + + if (is_array($v)) { + self::encodeParameters($v, $p, $params); + } else { + $v = (string) $v; + if (strlen($v)) { + $p .= '=' . rawurlencode($v); + } + $params[] = $p; + } + } + } + /** * Creates the full URL string. *