Skip to content

Commit 0416f54

Browse files
committed
fix: Redesign Horde::url() to fix undefined array key errors
Also remove redudant parentheses and do not add '//' in case of empty host.
1 parent 46d9297 commit 0416f54

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

lib/Horde.php

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,7 @@ public static function assertDriverConfig(
542542
public static function url($uri, $full = false, $opts = [])
543543
{
544544
if (is_array($opts)) {
545-
$append_session = $opts['append_session']
546-
?? 0;
545+
$append_session = $opts['append_session'] ?? 0;
547546
if (!empty($opts['force_ssl'])) {
548547
$full = true;
549548
}
@@ -552,17 +551,14 @@ public static function url($uri, $full = false, $opts = [])
552551
$opts = [];
553552
}
554553

555-
$puri = parse_url($uri);
556-
/* @todo Fix for PHP < 5.3.6 */
557-
if (isset($puri['fragment']) && !isset($puri['path'])) {
558-
$pos = strpos(
559-
$uri,
560-
'/',
561-
strpos($uri, $puri['host']) + strlen($puri['host'])
562-
);
563-
$puri['path'] = substr($uri, $pos, strpos($uri, '#', $pos) - $pos);
554+
$puri = parse_url($uri) ?: [];
555+
556+
/* Normalize missing components */
557+
foreach (['host', 'path'] as $key) {
558+
if (!isset($puri[$key])) {
559+
$puri[$key] = '';
560+
}
564561
}
565-
/* End fix */
566562

567563
$url = '';
568564
$schemeRegexp = '|^([a-zA-Z][a-zA-Z0-9+.-]{0,19})://|';
@@ -577,10 +573,10 @@ public static function url($uri, $full = false, $opts = [])
577573
if ($full &&
578574
!isset($puri['scheme']) &&
579575
!preg_match($schemeRegexp, $webroot)) {
576+
580577
/* Store connection parameters in local variables. */
581578
$server_name = $GLOBALS['conf']['server']['name'];
582-
$server_port = $GLOBALS['conf']['server']['port']
583-
?? '';
579+
$server_port = $GLOBALS['conf']['server']['port'] ?? '';
584580

585581
$protocol = 'http';
586582
switch ($GLOBALS['conf']['use_ssl']) {
@@ -604,38 +600,46 @@ public static function url($uri, $full = false, $opts = [])
604600

605601
/* If using a non-standard port, add to the URL. */
606602
if (!empty($server_port) &&
607-
((($protocol == 'http') && ($server_port != 80)) ||
608-
(($protocol == 'https') && ($server_port != 443)))) {
603+
($protocol === 'http' && $server_port != 80 ||
604+
$protocol === 'https' && $server_port != 443)) {
609605
$server_name .= ':' . $server_port;
610606
}
611607

612608
$url = $protocol . '://' . $server_name;
609+
613610
} elseif (isset($puri['scheme'])) {
614-
$url = $puri['scheme'] . '://' . $puri['host'];
615611

616-
/* If using a non-standard port, add to the URL. */
617-
if (isset($puri['port']) &&
618-
((($puri['scheme'] == 'http') && ($puri['port'] != 80)) ||
619-
(($puri['scheme'] == 'https') && ($puri['port'] != 443)))) {
620-
$url .= ':' . $puri['port'];
612+
$url = $puri['scheme'] . ':';
613+
if ($puri['host'] !== '') {
614+
$url .= '//' . $puri['host'];
615+
616+
/* If using a non-standard port, add to the URL. */
617+
if (isset($puri['port']) &&
618+
($puri['scheme'] === 'http' && $puri['port'] != 80 ||
619+
$puri['scheme'] === 'https' && $puri['port'] != 443)) {
620+
$url .= ':' . $puri['port'];
621+
}
621622
}
622623
}
623624

624-
if (isset($puri['path']) &&
625-
(substr($puri['path'], 0, 1) == '/') &&
625+
if (substr($puri['path'], 0, 1) === '/' &&
626626
(!preg_match($schemeRegexp, $webroot) ||
627-
(preg_match($schemeRegexp, $webroot) && isset($puri['scheme'])))) {
627+
preg_match($schemeRegexp, $webroot) && isset($puri['scheme']))) {
628+
628629
$url .= $puri['path'];
629-
} elseif (isset($puri['path']) && preg_match($schemeRegexp, $webroot)) {
630-
if (substr($puri['path'], 0, 1) == '/') {
630+
631+
} elseif ($puri['path'] !== '' && preg_match($schemeRegexp, $webroot)) {
632+
633+
if (substr($puri['path'], 0, 1) === '/') {
631634
$pwebroot = parse_url($webroot);
632635
$url = $pwebroot['scheme'] . '://' . $pwebroot['host']
633636
. $puri['path'];
634637
} else {
635638
$url = $webroot . '/' . $puri['path'];
636639
}
640+
637641
} else {
638-
$url .= '/' . ($webroot ? $webroot . '/' : '') . ($puri['path'] ?? '');
642+
$url .= '/' . ($webroot ? $webroot . '/' : '') . $puri['path'];
639643
}
640644

641645
if (isset($puri['query'])) {
@@ -648,8 +652,8 @@ public static function url($uri, $full = false, $opts = [])
648652
$ob = new Horde_Url($url, $full);
649653

650654
if (empty($GLOBALS['conf']['session']['use_only_cookies']) &&
651-
(($append_session == 1) ||
652-
(($append_session == 0) && !isset($_COOKIE[session_name()])))) {
655+
($append_session == 1 ||
656+
$append_session == 0 && !isset($_COOKIE[session_name()]))) {
653657
$ob->add(session_name(), session_id());
654658
}
655659

0 commit comments

Comments
 (0)