Skip to content

Commit bf2b51b

Browse files
authored
Merge pull request #4 from amulet1/tostring_fixes
Reimplement parameters encoding in Horde_Url's toString() method to restore the intended functionality
2 parents d7ba2d1 + 9f40452 commit bf2b51b

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

lib/Horde/Url.php

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,10 @@ public function toString($raw = false, $full = true)
264264
}
265265
}
266266

267-
if ($params = $this->parameters) {
268-
foreach ($params as $p => &$v) {
269-
// TODO: Investigate if it should be done for all (or some) other objects
270-
if ($v instanceof Horde_Url) {
271-
$v = strval($v);
272-
}
273-
}
274-
unset($v);
275-
276-
$url .= '?' . http_build_query($params, "", $raw ? '&' : '&');
267+
if ($source = $this->parameters) {
268+
$params = [];
269+
self::encodeParameters($source, '', $params);
270+
$url .= '?' . implode($raw ? '&' : '&', $params);
277271
}
278272

279273
if ($this->anchor) {
@@ -283,6 +277,38 @@ public function toString($raw = false, $full = true)
283277
return $url;
284278
}
285279

280+
protected static function encodeParameters($source, $prefix, &$params)
281+
{
282+
$index = 0;
283+
284+
foreach ($source as $p => $v) {
285+
if (strlen($prefix)) {
286+
if ($index >= 0 && $p !== $index) {
287+
$index = -1;
288+
}
289+
if ($index >= 0) {
290+
$p = '';
291+
++$index;
292+
} else {
293+
$p = rawurlencode($p);
294+
}
295+
$p = $prefix . '[' . $p . ']';
296+
} else {
297+
$p = rawurlencode($p);
298+
}
299+
300+
if (is_array($v)) {
301+
self::encodeParameters($v, $p, $params);
302+
} else {
303+
$v = (string) $v;
304+
if (strlen($v)) {
305+
$p .= '=' . rawurlencode($v);
306+
}
307+
$params[] = $p;
308+
}
309+
}
310+
}
311+
286312
/**
287313
* Creates the full URL string.
288314
*

0 commit comments

Comments
 (0)