Skip to content

Commit 1a1fba1

Browse files
committed
helpers\View: Reduce nesting in makeBaseUrl
Use early return to make the code slightly more legible. This is also a prerequisite for overriding just the path when non-absolute URL is configured.
1 parent 896f9b3 commit 1a1fba1

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

src/helpers/View.php

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -47,43 +47,47 @@ public function getBaseUrl(): Uri {
4747
}
4848

4949
private function makeBaseUrl(): Uri {
50-
// base url in config.ini file
5150
$base = $this->configuration->baseUrl;
52-
if ($base === null) {
53-
// auto generate base url
54-
$protocol = 'http';
55-
if ((isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
56-
|| (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https')
57-
|| (isset($_SERVER['HTTP_HTTPS']) && $_SERVER['HTTP_HTTPS'] === 'https')) {
58-
$protocol = 'https';
59-
}
6051

61-
// check for SSL proxy
62-
if (isset($_SERVER['HTTP_X_FORWARDED_SERVER']) && isset($_SERVER['HTTP_X_FORWARDED_HOST'])
63-
&& ($_SERVER['HTTP_X_FORWARDED_SERVER'] === $_SERVER['HTTP_X_FORWARDED_HOST'])) {
64-
$subdir = '/' . preg_replace('/\/[^\/]+$/', '', $_SERVER['PHP_SELF']);
65-
$host = $_SERVER['HTTP_X_FORWARDED_SERVER'];
66-
} else {
67-
$subdir = '';
68-
if (PHP_SAPI !== 'cli') {
69-
$subdir = rtrim(strtr(dirname($_SERVER['SCRIPT_NAME']), '\\', '/'), '/');
70-
}
71-
$host = $_SERVER['SERVER_NAME'];
72-
}
52+
if ($base !== null) {
53+
// base url in config.ini file
54+
return $base;
55+
}
7356

74-
$port = '';
75-
if (isset($_SERVER['SERVER_PORT'])
76-
&& (($protocol === 'http' && $_SERVER['SERVER_PORT'] != '80')
77-
|| ($protocol === 'https' && $_SERVER['SERVER_PORT'] != '443'))) {
78-
$port = ':' . $_SERVER['SERVER_PORT'];
79-
}
80-
// Override the port if nginx is the front end and the traffic is being forwarded
81-
if (isset($_SERVER['HTTP_X_FORWARDED_PORT'])) {
82-
$port = ':' . $_SERVER['HTTP_X_FORWARDED_PORT'];
57+
// auto generate base url
58+
59+
$protocol = 'http';
60+
if ((isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
61+
|| (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https')
62+
|| (isset($_SERVER['HTTP_HTTPS']) && $_SERVER['HTTP_HTTPS'] === 'https')) {
63+
$protocol = 'https';
64+
}
65+
66+
// check for SSL proxy
67+
if (isset($_SERVER['HTTP_X_FORWARDED_SERVER']) && isset($_SERVER['HTTP_X_FORWARDED_HOST'])
68+
&& ($_SERVER['HTTP_X_FORWARDED_SERVER'] === $_SERVER['HTTP_X_FORWARDED_HOST'])) {
69+
$subdir = '/' . preg_replace('/\/[^\/]+$/', '', $_SERVER['PHP_SELF']);
70+
$host = $_SERVER['HTTP_X_FORWARDED_SERVER'];
71+
} else {
72+
$subdir = '';
73+
if (PHP_SAPI !== 'cli') {
74+
$subdir = rtrim(strtr(dirname($_SERVER['SCRIPT_NAME']), '\\', '/'), '/');
8375
}
76+
$host = $_SERVER['SERVER_NAME'];
77+
}
8478

85-
$base = new Uri($protocol . '://' . $host . $port . $subdir . '/');
79+
$port = '';
80+
if (isset($_SERVER['SERVER_PORT'])
81+
&& (($protocol === 'http' && $_SERVER['SERVER_PORT'] != '80')
82+
|| ($protocol === 'https' && $_SERVER['SERVER_PORT'] != '443'))) {
83+
$port = ':' . $_SERVER['SERVER_PORT'];
8684
}
85+
// Override the port if nginx is the front end and the traffic is being forwarded
86+
if (isset($_SERVER['HTTP_X_FORWARDED_PORT'])) {
87+
$port = ':' . $_SERVER['HTTP_X_FORWARDED_PORT'];
88+
}
89+
90+
$base = new Uri($protocol . '://' . $host . $port . $subdir . '/');
8791

8892
return $base;
8993
}

0 commit comments

Comments
 (0)